1 #include "love_sdlmixer.h" 2 #include "mod_sdlmixer.h" 3 4 // LOVE 5 #include <love/Core.h> 6 #include <love/Exception.h> 7 8 // SDL and friends. 9 #include <SDL.h> 10 #include <SDL_mixer.h> 11 12 namespace love_sdlmixer 13 { 14 15 // Required modules + Core. 16 love::Core * core = 0; 17 love::Filesystem * filesystem = 0; 18 module_init(int argc,char ** argv,love::Core * core)19 bool module_init(int argc, char ** argv, love::Core * core) 20 { 21 std::cout << "INIT love.audio [" << "SDL_mixer" << "]" << std::endl; 22 23 // Get modules. 24 filesystem = core->getFilesystem(); 25 26 // Verify all. 27 if(!filesystem->verify()) 28 { 29 std::cerr << "Required module filesystem not loaded." << std::endl; 30 return false; 31 } 32 33 love_sdlmixer::core = core; 34 35 int bits = 0; 36 int audio_rate,audio_channels,audio_buffers= love::AUDIO_BUFFER_DEFAULT; 37 Uint16 audio_format; 38 39 // High: 44100 40 // Medium: 22050 41 // Low: 11025 42 43 if(Mix_OpenAudio(love::AUDIO_QUALITY_HIGH, MIX_DEFAULT_FORMAT, love::AUDIO_MODE_STEREO, audio_buffers)<0) 44 { 45 std::cout << "SDLMixerAudio: Unable to open audio!" << std::endl; 46 return false; 47 } 48 49 // @todo All this must be configurable. 50 51 Mix_AllocateChannels(8); 52 Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); 53 bits=audio_format&0xFF; 54 Mix_Volume(-1,MIX_MAX_VOLUME); 55 Mix_VolumeMusic(MIX_MAX_VOLUME); 56 57 58 // Set function pointers and load module. 59 { 60 love::Audio * a = core->getAudio(); 61 a->loaded = true; 62 } 63 64 return true; 65 } 66 module_quit()67 bool module_quit() 68 { 69 Mix_CloseAudio(); 70 std::cout << "QUIT love.audio [" << "SDL_mixer" << "]" << std::endl; 71 return true; 72 } 73 module_open(void * vm)74 bool module_open(void * vm) 75 { 76 lua_State * s = (lua_State *)vm; 77 if(s == 0) 78 return false; 79 luaopen_mod_sdlmixer(s); 80 return true; 81 } 82 newSound(const char * filename)83 pSound newSound(const char * filename) 84 { 85 love::pFile * file = filesystem->getFile(filename, love::FILE_READ); 86 pSound sound(new Sound(*file)); 87 if(!sound->load()) 88 { 89 std::stringstream err; 90 err << "Could not load sound \"" << filename << "\"."; 91 core->error(err.str().c_str()); 92 } 93 delete file; // sound has copy of the file at this point. 94 return sound; 95 } 96 newMusic(const char * filename)97 pMusic newMusic(const char * filename) 98 { 99 love::pFile * file = filesystem->getFile( filename , love::FILE_READ); 100 pMusic music(new Music(*file)); 101 if(!music->load()) 102 { 103 std::stringstream err; 104 err << "Could not load music \"" << filename << "\"."; 105 core->error(err.str().c_str()); 106 } 107 delete file; // music has copy of the file at this point. 108 return music; 109 } 110 isPlaying()111 bool isPlaying() 112 { 113 // Is sound playing? 114 int sound = Mix_Playing(-1); 115 116 // Is music playing? 117 int music = Mix_PlayingMusic(); 118 119 // If both are 0, none are playing 120 return (sound + music != 0); 121 } 122 isPaused()123 bool isPaused() 124 { 125 // Is sound paused? 126 int sound = Mix_Paused(-1); 127 128 // Is music paused? 129 int music = Mix_PausedMusic(); 130 131 // If both are 0, none are playing 132 return (sound + music != 0); 133 } 134 pause()135 void pause() 136 { 137 // Pause all sound 138 Mix_Pause(-1); 139 140 // Pause music 141 Mix_PauseMusic(); 142 } 143 stop()144 void stop() 145 { 146 // Stop all sounds 147 Mix_HaltChannel(-1); 148 149 // Stop music 150 Mix_HaltMusic(); 151 } 152 resume()153 void resume() 154 { 155 // Resume sounds 156 Mix_Resume(-1); 157 158 // Resume music. 159 Mix_ResumeMusic(); 160 } 161 setChannels(int channels)162 void setChannels(int channels) 163 { 164 // Allocate channels. 165 Mix_AllocateChannels(channels); 166 } 167 setMode(int frequency,int mode,int buffersize)168 void setMode(int frequency, int mode, int buffersize) 169 { 170 // Close previous setting. 171 Mix_CloseAudio(); 172 173 // Open new setting. 174 if(Mix_OpenAudio(frequency, MIX_DEFAULT_FORMAT, mode, buffersize)<0) 175 { 176 std::stringstream err; 177 err << "Could not set audio mode: " << Mix_GetError(); 178 core->error(err.str().c_str()); 179 return; 180 } 181 } 182 setVolume(float volume)183 void setVolume(float volume) 184 { 185 // Set the volume for all channels 186 Mix_Volume(-1,(int)(MIX_MAX_VOLUME * volume)); 187 188 // Set the volume for music 189 Mix_VolumeMusic((int)(MIX_MAX_VOLUME * volume)); 190 } 191 play(const pSound & sound,int loop,int channel)192 void play(const pSound & sound, int loop, int channel) 193 { 194 sound->play(loop); 195 } 196 play(const pMusic & music,int loop)197 void play(const pMusic & music, int loop) 198 { 199 music->play(loop); 200 } 201 202 } // love_sdlmixer 203