diff --git a/simgear/sound/openal_test1.cxx b/simgear/sound/openal_test1.cxx index 7909f2cd..c503d028 100644 --- a/simgear/sound/openal_test1.cxx +++ b/simgear/sound/openal_test1.cxx @@ -43,6 +43,33 @@ static void print_openal_error( ALuint error ) { } } +ALuint createBufferFromFile(const SGPath& path) +{ + ALuint buffer = -1; +#ifdef ENABLE_SOUND + unsigned int format; + unsigned int block_align; + ALsizei size; + ALfloat sampleFrequency; + ALvoid* data = loadWAVFromFile(path, format, size, sampleFrequency, block_align); + assert(data); + + alGenBuffers(1, &buffer); + if (alGetError() != AL_NO_ERROR) { + free(data); + throw sg_io_exception("OpenAL buffer allocation failed", sg_location(path.str())); + } + + alBufferData (buffer, format, data, size, (ALsizei) sampleFrequency); + if (alGetError() != AL_NO_ERROR) { + alDeleteBuffers(1, &buffer); + free(data); + throw sg_io_exception("OpenAL setting buffer data failed", sg_location(path.str())); + } +#endif + return buffer; +} + int main( int argc, char *argv[] ) { @@ -111,7 +138,7 @@ int main( int argc, char *argv[] ) source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0; // Load the sample file - buffer = simgear::createBufferFromFile(SGPath(AUDIOFILE)); + buffer = createBufferFromFile(SGPath(AUDIOFILE)); if (buffer == AL_NONE) { SG_LOG( SG_GENERAL, SG_ALERT, "Failed to buffer data."); } diff --git a/simgear/sound/readwav.cxx b/simgear/sound/readwav.cxx index 71ec14b4..7e90eb01 100644 --- a/simgear/sound/readwav.cxx +++ b/simgear/sound/readwav.cxx @@ -291,9 +291,9 @@ namespace !wavReadLE (fd, byteRate) || !wavReadLE (fd, blockAlign) || !wavReadLE (fd, bitsPerSample)) - { + { throw sg_io_exception("corrupt or truncated WAV data", b->path); - } + } if (!gzSkip(fd, chunkLength - 16)) throw sg_io_exception("corrupt or truncated WAV data", b->path); @@ -319,7 +319,7 @@ namespace compressed = true; codec = codecLinear; } else { - bitsPerSample *= 4; /* uLaw is 16-bit packed into 8 bits */ + bitsPerSample *= 4; /* adpcm is 16-bit packed into 4 bits */ codec = codecIMA4; } @@ -389,32 +389,4 @@ ALvoid* loadWAVFromFile(const SGPath& path, unsigned int& format, ALsizei& size, return data; } -ALuint createBufferFromFile(const SGPath& path) -{ - ALuint buffer = -1; -#ifdef ENABLE_SOUND - unsigned int format; - unsigned int block_align; - ALsizei size; - ALfloat sampleFrequency; - ALvoid* data = loadWAVFromFile(path, format, size, sampleFrequency, block_alight); - assert(data); - - alGenBuffers(1, &buffer); - if (alGetError() != AL_NO_ERROR) { - free(data); - throw sg_io_exception("OpenAL buffer allocation failed", sg_location(path.str())); - } - - alBufferData (buffer, format, data, size, (ALsizei) sampleFrequency); - alBufferi (buffer, AL_UNPACK_BLOCK_ALIGNMENT_SOFT, block_align); - if (alGetError() != AL_NO_ERROR) { - alDeleteBuffers(1, &buffer); - free(data); - throw sg_io_exception("OpenAL setting buffer data failed", sg_location(path.str())); - } -#endif - return buffer; -} - } // of namespace simgear diff --git a/simgear/sound/readwav.hxx b/simgear/sound/readwav.hxx index 7957250f..14c891e9 100644 --- a/simgear/sound/readwav.hxx +++ b/simgear/sound/readwav.hxx @@ -14,11 +14,12 @@ // forward decls class SGPath; +#define DEFAULT_IMA4_BLOCKSIZE 36 +#define BLOCKSIZE_TO_SMP(a) ((a) > 1) ? (((a)-4)*2) : 1 + namespace simgear { ALvoid* loadWAVFromFile(const SGPath& path, unsigned int& format, ALsizei& size, ALfloat& freqf, unsigned int& block_align); - - ALuint createBufferFromFile(const SGPath& path); } #endif // of SG_SOUND_READWAV_HXX diff --git a/simgear/sound/soundmgr_openal.cxx b/simgear/sound/soundmgr_openal.cxx index 7e48da8c..ea7e77c7 100644 --- a/simgear/sound/soundmgr_openal.cxx +++ b/simgear/sound/soundmgr_openal.cxx @@ -570,25 +570,38 @@ unsigned int SGSoundMgr::request_buffer(SGSoundSample *sample) sample_data = sample->get_data(); } + ALenum format = AL_NONE; + switch( sample->get_format() ) + { + case SG_SAMPLE_MONO16: + format = AL_FORMAT_MONO16; + break; + case SG_SAMPLE_MONO8: + format = AL_FORMAT_MONO8; + break; + case SG_SAMPLE_MULAW: + format = AL_FORMAT_MONO_MULAW_EXT; + break; + case SG_SAMPLE_ADPCM: + format = AL_FORMAT_MONO_IMA4; + break; + default: + SG_LOG(SG_SOUND, SG_ALERT, "unsupported audio format"); + return buffer; + } + // create an OpenAL buffer handle alGenBuffers(1, &buffer); if ( !testForError("generate buffer") ) { // Copy data to the internal OpenAL buffer - ALenum format = AL_NONE; - unsigned int fmt = sample->get_format(); - if (fmt == SG_SAMPLE_MONO16) format = AL_FORMAT_MONO16; - else if (fmt == SG_SAMPLE_MONO8) format = AL_FORMAT_MONO8; - else if (fmt == SG_SAMPLE_MULAW) format = AL_FORMAT_MONO_MULAW_EXT; - else if (fmt == SG_SAMPLE_ADPCM) format = AL_FORMAT_MONO_IMA4; - ALsizei size = sample->get_size(); ALsizei freq = sample->get_frequency(); alBufferData( buffer, format, sample_data, size, freq ); - if (_block_support) { - ALsizei block_align = sample->get_block_align(); - alBufferi (buffer, AL_UNPACK_BLOCK_ALIGNMENT_SOFT, block_align); + if (format == AL_FORMAT_MONO_IMA4 && _block_support) { + ALsizei samples_block = BLOCKSIZE_TO_SMP( sample->get_block_align() ); + alBufferi (buffer, AL_UNPACK_BLOCK_ALIGNMENT_SOFT, samples_block ); } if ( !testForError("buffer add data") ) { @@ -783,14 +796,14 @@ bool SGSoundMgr::load( const std::string &samplepath, return false; unsigned int format; - unsigned int block_align; + unsigned int blocksz; ALsizei size; ALsizei freq; ALvoid *data; ALfloat freqf; - data = simgear::loadWAVFromFile(samplepath, format, size, freqf, block_align ); + data = simgear::loadWAVFromFile(samplepath, format, size, freqf, blocksz); freq = (ALsizei)freqf; if (data == NULL) { throw sg_io_exception("Failed to load wav file", sg_location(samplepath)); @@ -803,7 +816,7 @@ bool SGSoundMgr::load( const std::string &samplepath, *dbuf = (void *)data; *fmt = (int)format; - *block = (int)block_align; + *block = (int)blocksz; *sz = (size_t)size; *frq = (int)freq; diff --git a/simgear/sound/xmlsound.cxx b/simgear/sound/xmlsound.cxx index 2becb55a..f0d6e56e 100644 --- a/simgear/sound/xmlsound.cxx +++ b/simgear/sound/xmlsound.cxx @@ -464,7 +464,7 @@ SGXmlSound::update (double dt) for(i = 0; i < max; i++) { double p = 1.0; - if (_volume[i].expr) { + if (_pitch[i].expr) { p = _pitch[i].expr->getValue(NULL); expr = true; continue;