| 1 |
#include "SoundPlayer.h" |
| 2 |
|
| 3 |
#include <vorbis/vorbisfile.h> |
| 4 |
#include <iostream> |
| 5 |
#include <vector> |
| 6 |
#include "string/case_conv.h" |
| 7 |
|
| 8 |
#include "os/path.h" |
| 9 |
#include <memory> |
| 10 |
|
| 11 |
// We need the usleep() command. Be sure to include the windows.h |
| 12 |
// header after the local textfilestream.h because there are definition conflicts otherwise. |
| 13 |
#ifdef WIN32 |
| 14 |
#include <windows.h> |
| 15 |
#define usleep(x) Sleep((x)/1000) |
| 16 |
#else |
| 17 |
#include <unistd.h> |
| 18 |
#endif |
| 19 |
|
| 20 |
#include "WavFileLoader.h" |
| 21 |
#include "OggFileLoader.h" |
| 22 |
|
| 23 |
namespace sound |
| 24 |
{ |
| 25 |
|
| 26 |
// Constructor |
| 27 |
SoundPlayer::SoundPlayer() : |
| 28 |
_initialised(false), |
| 29 |
_context(NULL), |
| 30 |
_buffer(0), |
| 31 |
_source(0) |
| 32 |
{ |
| 33 |
// Disable the timer, to make sure |
| 34 |
_timer.Connect(wxEVT_TIMER, wxTimerEventHandler(SoundPlayer::onTimerIntervalReached), NULL, this); |
| 35 |
_timer.Stop(); |
| 36 |
} |
| 37 |
|
| 38 |
void SoundPlayer::initialise() { |
| 39 |
// Create device |
| 40 |
ALCdevice* device = alcOpenDevice(NULL); |
| 41 |
|
| 42 |
if (device != NULL) { |
| 43 |
// Create context |
| 44 |
_context = alcCreateContext(device, NULL); |
| 45 |
|
| 46 |
if (_context != NULL) { |
| 47 |
// Make context current |
| 48 |
if (!alcMakeContextCurrent(_context)) { |
| 49 |
alcDestroyContext(_context); |
| 50 |
alcCloseDevice(device); |
| 51 |
_context = NULL; |
| 52 |
|
| 53 |
rError() << "Could not make ALC context current." << std::endl; |
| 54 |
} |
| 55 |
|
| 56 |
// Success |
| 57 |
_initialised = true; |
| 58 |
rMessage() << "SoundPlayer: OpenAL context successfully set up." << std::endl; |
| 59 |
} |
| 60 |
else { |
| 61 |
alcCloseDevice(device); |
| 62 |
rError() << "Could not create ALC context." << std::endl; |
| 63 |
} |
| 64 |
} |
| 65 |
else { |
| 66 |
rError() << "Could not open ALC device." << std::endl; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
SoundPlayer::~SoundPlayer() { |
| 71 |
clearBuffer(); |
| 72 |
|
| 73 |
// Unset the context |
| 74 |
if (alcMakeContextCurrent(NULL)) { |
| 75 |
// Destroy the context and close device if appropriate |
| 76 |
if (_context != NULL) { |
| 77 |
ALCdevice* device = alcGetContextsDevice(_context); |
| 78 |
alcDestroyContext(_context); |
| 79 |
|
| 80 |
if (alcGetError(device) != ALC_NO_ERROR) { |
| 81 |
rError() << "Could not destroy ALC context." << std::endl; |
| 82 |
} |
| 83 |
|
| 84 |
if (!alcCloseDevice(device)) { |
| 85 |
rError() << "Could not close ALC device." << std::endl; |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
else { |
| 90 |
rError() << "Could not reset ALC context." << std::endl; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
void SoundPlayer::onTimerIntervalReached(wxTimerEvent& ev) |
| 95 |
{ |
| 96 |
// Check for active source and buffer |
| 97 |
if (_source != 0 && _buffer != 0) |
| 98 |
{ |
| 99 |
ALint state; |
| 100 |
// Query the state of the source |
| 101 |
alGetSourcei(_source, AL_SOURCE_STATE, &state); |
| 102 |
|
| 103 |
if (state == AL_STOPPED) |
| 104 |
{ |
| 105 |
// Erase the buffer |
| 106 |
clearBuffer(); |
| 107 |
|
| 108 |
// Disable the timer to stop calling this function |
| 109 |
_timer.Stop(); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
void SoundPlayer::clearBuffer() |
| 115 |
{ |
| 116 |
// Check if there is an active buffer |
| 117 |
if (_source != 0) { |
| 118 |
// Stop playing |
| 119 |
alSourceStop(_source); |
| 120 |
alDeleteSources(1, &_source); |
| 121 |
_source = 0; |
| 122 |
|
| 123 |
if (_buffer != 0) { |
| 124 |
// Free the buffer |
| 125 |
alDeleteBuffers(1, &_buffer); |
| 126 |
_buffer = 0; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
_timer.Stop(); |
| 131 |
} |
| 132 |
|
| 133 |
void SoundPlayer::stop() |
| 134 |
{ |
| 135 |
clearBuffer(); |
| 136 |
} |
| 137 |
|
| 138 |
void SoundPlayer::play(ArchiveFile& file, bool loopSound) |
| 139 |
{ |
| 140 |
// If we're not initialised yet, do it now |
| 141 |
if (!_initialised) |
| 142 |
{ |
| 143 |
initialise(); |
| 144 |
} |
| 145 |
|
| 146 |
// Stop any previous playback operations, that might be still active |
| 147 |
clearBuffer(); |
| 148 |
|
| 149 |
// Retrieve the extension |
| 150 |
std::string ext = os::getExtension(file.getName()); |
| 151 |
|
| 152 |
if (string::to_lower_copy(ext) == "ogg") |
| 153 |
{ |
| 154 |
createBufferDataFromOgg(file); |
| 155 |
} |
| 156 |
else |
| 157 |
{ |
| 158 |
createBufferDataFromWav(file); |
| 159 |
} |
| 160 |
|
| 161 |
if (_buffer != 0) |
| 162 |
{ |
| 163 |
alGenSources(1, &_source); |
| 164 |
|
| 165 |
// Assign the buffer to the source and play it |
| 166 |
alSourcei(_source, AL_BUFFER, _buffer); |
| 167 |
|
| 168 |
// Set the looping flag |
| 169 |
alSourcei(_source, AL_LOOPING, loopSound ? AL_TRUE : AL_FALSE); |
| 170 |
|
| 171 |
// greebo: Wait 10 msec. to fix a problem with buffers not being played |
| 172 |
// maybe the AL needs time to push the data? |
| 173 |
usleep(10000); |
| 174 |
|
| 175 |
alSourcePlay(_source); |
| 176 |
|
| 177 |
// Enable the periodic buffer check, this destructs the buffer |
| 178 |
// as soon as the playback has finished |
| 179 |
_timer.Start(200); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
void SoundPlayer::createBufferDataFromWav(ArchiveFile& file) |
| 184 |
{ |
| 185 |
// Must be a wave file |
| 186 |
try |
| 187 |
{ |
| 188 |
// Create an AL sound buffer directly from the buffer in memory |
| 189 |
_buffer = WavFileLoader::LoadFromStream(file.getInputStream()); |
| 190 |
} |
| 191 |
catch (std::runtime_error& e) |
| 192 |
{ |
| 193 |
rError() << "SoundPlayer: Error opening WAV file: " << e.what() << std::endl; |
| 194 |
_buffer = 0; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
void SoundPlayer::createBufferDataFromOgg(ArchiveFile& file) |
| 199 |
{ |
| 200 |
// Must be an OGG file |
| 201 |
try |
| 202 |
{ |
| 203 |
// Create an AL sound buffer directly from the buffer in memory |
| 204 |
_buffer = OggFileLoader::LoadFromFile(file); |
| 205 |
} |
| 206 |
catch (std::runtime_error & e) |
| 207 |
{ |
| 208 |
rError() << "SoundPlayer: Error opening OGG file: " << e.what() << std::endl; |
| 209 |
_buffer = 0; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
} // namespace sound |