| 1 |
/* |
| 2 |
* Copyright (C) 2004 Ivo Danihelka ([email protected]) |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
*/ |
| 9 |
#include "SDLSoundAgent.h" |
| 10 |
|
| 11 |
#include "Log.h" |
| 12 |
#include "Path.h" |
| 13 |
#include "ExInfo.h" |
| 14 |
#include "SDLException.h" |
| 15 |
#include "MixException.h" |
| 16 |
#include "Random.h" |
| 17 |
#include "BaseMsg.h" |
| 18 |
#include "OptionAgent.h" |
| 19 |
|
| 20 |
BaseMsg *SDLSoundAgent::ms_finished = NULL; |
| 21 |
//----------------------------------------------------------------- |
| 22 |
/** |
| 23 |
* Init sound subsystem. |
| 24 |
* @throws SDLException when SDL_INIT_AUDIO cannot be initialized |
| 25 |
* @throws MixException when SDL_mixer cannot be initialized |
| 26 |
*/ |
| 27 |
void |
| 28 |
SDLSoundAgent::own_init() |
| 29 |
{ |
| 30 |
SoundAgent::own_init(); |
| 31 |
} |
| 32 |
//----------------------------------------------------------------- |
| 33 |
void |
| 34 |
SDLSoundAgent::own_shutdown() |
| 35 |
{ |
| 36 |
stopMusic(); |
| 37 |
Mix_CloseAudio(); |
| 38 |
SDL_QuitSubSystem(SDL_INIT_AUDIO); |
| 39 |
} |
| 40 |
//----------------------------------------------------------------- |
| 41 |
/** |
| 42 |
* Reinit the sound subsystem. |
| 43 |
*/ |
| 44 |
void |
| 45 |
SDLSoundAgent::reinit() |
| 46 |
{ |
| 47 |
m_music = NULL; |
| 48 |
m_looper = NULL; |
| 49 |
m_soundVolume = MIX_MAX_VOLUME; |
| 50 |
m_musicVolume = MIX_MAX_VOLUME; |
| 51 |
if(SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) { |
| 52 |
throw SDLException(ExInfo("SDL_InitSubSystem")); |
| 53 |
} |
| 54 |
|
| 55 |
int frequency = |
| 56 |
OptionAgent::agent()->getAsInt("sound_frequency", 44100); |
| 57 |
if(Mix_OpenAudio(frequency, MIX_DEFAULT_FORMAT, 2, 1024) < 0) { |
| 58 |
throw MixException(ExInfo("Mix_OpenAudio")); |
| 59 |
} |
| 60 |
Mix_AllocateChannels(16); |
| 61 |
|
| 62 |
SoundAgent::reinit(); |
| 63 |
} |
| 64 |
//----------------------------------------------------------------- |
| 65 |
/** |
| 66 |
* Play this sound. |
| 67 |
* @param sound chunk to play |
| 68 |
* @param volume percentage sound volume |
| 69 |
* @param loops numer of loops. 0=play once, 1=play twice, -1=play infinite |
| 70 |
* |
| 71 |
* @return channel number where the sound is played, |
| 72 |
* return -1 on error or when sound is NULL |
| 73 |
*/ |
| 74 |
int |
| 75 |
SDLSoundAgent::playSound(Mix_Chunk *sound, int volume, int loops) |
| 76 |
{ |
| 77 |
int channel = -1; |
| 78 |
if (sound) { |
| 79 |
channel = Mix_PlayChannel(-1, sound, loops); |
| 80 |
if (-1 == channel) { |
| 81 |
//NOTE: maybe there are too few open channels |
| 82 |
LOG_WARNING(ExInfo("cannot play sound") |
| 83 |
.addInfo("Mix", Mix_GetError())); |
| 84 |
} |
| 85 |
else { |
| 86 |
Mix_Volume(channel, m_soundVolume * volume / 100); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
return channel; |
| 91 |
} |
| 92 |
|
| 93 |
//----------------------------------------------------------------- |
| 94 |
/** |
| 95 |
* Set sound volume. |
| 96 |
* NOTE: all already running sound will get equals volume |
| 97 |
* @param volume percentage volume, e.g. 30%=30 |
| 98 |
*/ |
| 99 |
void |
| 100 |
SDLSoundAgent::setSoundVolume(int volume) |
| 101 |
{ |
| 102 |
m_soundVolume = MIX_MAX_VOLUME * volume / 100; |
| 103 |
if (m_soundVolume > MIX_MAX_VOLUME) { |
| 104 |
m_soundVolume = MIX_MAX_VOLUME; |
| 105 |
} |
| 106 |
else if (m_soundVolume < 0) { |
| 107 |
m_soundVolume = 0; |
| 108 |
} |
| 109 |
Mix_Volume(-1, m_soundVolume); |
| 110 |
} |
| 111 |
|
| 112 |
//--------------------------------------------------------------------------- |
| 113 |
// Music part |
| 114 |
//--------------------------------------------------------------------------- |
| 115 |
|
| 116 |
/** |
| 117 |
* Play music. |
| 118 |
* @param file path to music file (i.e. *.ogg) |
| 119 |
* @param finished send this message when music is finished. |
| 120 |
* If finished is NULL, play music forever. |
| 121 |
*/ |
| 122 |
void |
| 123 |
SDLSoundAgent::playMusic(const Path &file, |
| 124 |
BaseMsg *finished) |
| 125 |
{ |
| 126 |
// The same music is not restarted when it is not needed. |
| 127 |
if (m_playingPath == file.getPosixName() |
| 128 |
&& ms_finished == NULL && finished == NULL) { |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
stopMusic(); |
| 133 |
m_playingPath = file.getPosixName(); |
| 134 |
|
| 135 |
if (finished) { |
| 136 |
ms_finished = finished; |
| 137 |
m_music = Mix_LoadMUS(file.getNative().c_str()); |
| 138 |
if (m_music && (0 == Mix_PlayMusic(m_music, 1))) { |
| 139 |
Mix_HookMusicFinished(musicFinished); |
| 140 |
} |
| 141 |
else { |
| 142 |
LOG_WARNING(ExInfo("cannot play music") |
| 143 |
.addInfo("music", file.getNative()) |
| 144 |
.addInfo("Mix", Mix_GetError())); |
| 145 |
} |
| 146 |
} |
| 147 |
else { |
| 148 |
m_looper = new SDLMusicLooper(file); |
| 149 |
m_looper->setVolume(m_musicVolume); |
| 150 |
m_looper->start(); |
| 151 |
} |
| 152 |
} |
| 153 |
//----------------------------------------------------------------- |
| 154 |
/** |
| 155 |
* @param volume percentage volume, e.g. 30%=30 |
| 156 |
*/ |
| 157 |
void |
| 158 |
SDLSoundAgent::setMusicVolume(int volume) |
| 159 |
{ |
| 160 |
m_musicVolume = MIX_MAX_VOLUME * volume / 100; |
| 161 |
if (m_musicVolume > MIX_MAX_VOLUME) { |
| 162 |
m_musicVolume = MIX_MAX_VOLUME; |
| 163 |
} |
| 164 |
else if (m_musicVolume < 0) { |
| 165 |
m_musicVolume = 0; |
| 166 |
} |
| 167 |
Mix_VolumeMusic(m_musicVolume); |
| 168 |
if (m_looper) { |
| 169 |
m_looper->setVolume(m_musicVolume); |
| 170 |
} |
| 171 |
} |
| 172 |
//----------------------------------------------------------------- |
| 173 |
void |
| 174 |
SDLSoundAgent::stopMusic() |
| 175 |
{ |
| 176 |
if (m_looper) { |
| 177 |
m_looper->stop(); |
| 178 |
delete m_looper; |
| 179 |
m_looper = NULL; |
| 180 |
} |
| 181 |
|
| 182 |
if(Mix_PlayingMusic()) { |
| 183 |
Mix_HookMusicFinished(NULL); |
| 184 |
Mix_HaltMusic(); |
| 185 |
} |
| 186 |
if (m_music) { |
| 187 |
Mix_FreeMusic(m_music); |
| 188 |
m_music = NULL; |
| 189 |
} |
| 190 |
if (ms_finished) { |
| 191 |
delete ms_finished; |
| 192 |
ms_finished = NULL; |
| 193 |
} |
| 194 |
m_playingPath = ""; |
| 195 |
} |
| 196 |
//----------------------------------------------------------------- |
| 197 |
/** |
| 198 |
* Callback called when music is finished. |
| 199 |
* NOTE: no one exception can be passed to "C" SDL_mixer code |
| 200 |
*/ |
| 201 |
void |
| 202 |
SDLSoundAgent::musicFinished() |
| 203 |
{ |
| 204 |
try { |
| 205 |
if (ms_finished) { |
| 206 |
ms_finished->sendClone(); |
| 207 |
} |
| 208 |
else { |
| 209 |
LOG_WARNING(ExInfo("NULL == ms_finished")); |
| 210 |
} |
| 211 |
} |
| 212 |
catch (std::exception &e) { |
| 213 |
LOG_WARNING(ExInfo("musicFinished error") |
| 214 |
.addInfo("what", e.what())); |
| 215 |
} |
| 216 |
catch (...) { |
| 217 |
LOG_ERROR(ExInfo("musicFinished error - unknown exception")); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|