From c53f05cbb1564b7d1bf12845bac9fe027ad17994 Mon Sep 17 00:00:00 2001 From: Erik Hofman Date: Mon, 30 May 2016 11:46:41 +0200 Subject: [PATCH] Add support for native mulaw encoded samples if the OpenAL implementation supports it --- simgear/sound/openal_test2.cxx | 2 +- simgear/sound/readwav.cxx | 23 +++++++++++++++++------ simgear/sound/soundmgr_openal.cxx | 10 ++++++++-- simgear/sound/soundmgr_openal_private.hxx | 3 --- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/simgear/sound/openal_test2.cxx b/simgear/sound/openal_test2.cxx index 260eea97..dc81a9bf 100644 --- a/simgear/sound/openal_test2.cxx +++ b/simgear/sound/openal_test2.cxx @@ -38,7 +38,7 @@ int main( int argc, char *argv[] ) { printf("playing sample1\n"); sleep(1); - SGSoundSample *sample2 = new SGSoundSample("jet.wav", srcDir); + SGSoundSample *sample2 = new SGSoundSample("jet_ulaw.wav", srcDir); sample2->set_volume(0.5); sample2->set_pitch(0.4); sample2->play_looped(); diff --git a/simgear/sound/readwav.cxx b/simgear/sound/readwav.cxx index 4f96d883..1618449c 100644 --- a/simgear/sound/readwav.cxx +++ b/simgear/sound/readwav.cxx @@ -52,11 +52,16 @@ namespace } }; - unsigned int formatConstruct(ALint numChannels, ALint bitsPerSample) + unsigned int formatConstruct(ALint numChannels, ALint bitsPerSample, bool compressed) { unsigned int rv = 0; - if (numChannels == 1 && bitsPerSample == 8) rv = SG_SAMPLE_MONO8; - if (numChannels == 1 && bitsPerSample == 16) rv = SG_SAMPLE_MONO16; + if (!compressed) { + if (numChannels == 1 && bitsPerSample == 16) rv = SG_SAMPLE_MONO16; + else if (numChannels == 1 && bitsPerSample == 8) rv = SG_SAMPLE_MONO8; + } else { + if (numChannels == 1 && bitsPerSample == 4) rv = SG_SAMPLE_ADPCM; + else if (numChannels == 1 && bitsPerSample == 8) rv = SG_SAMPLE_MULAW; + } return rv; } @@ -150,6 +155,7 @@ namespace assert(b->data == NULL); bool found_header = false; + bool compressed = false; uint32_t chunkLength; int32_t magic; uint16_t audioFormat; @@ -205,15 +211,20 @@ namespace codec = (bitsPerSample == 8 || sgIsLittleEndian()) ? codecLinear : codecPCM16BE; break; case 7: /* uLaw */ - bitsPerSample *= 2; /* uLaw is 16-bit packed into 8 bits */ - codec = codecULaw; + if (alIsExtensionPresent((ALchar *)"AL_EXT_mulaw")) { + compressed = true; + codec = codecLinear; + } else { + bitsPerSample *= 2; /* uLaw is 16-bit packed into 8 bits */ + codec = codecULaw; + } break; default: throw sg_io_exception("unsupported WAV encoding", b->path); } b->frequency = samplesPerSecond; - b->format = formatConstruct(numChannels, bitsPerSample); + b->format = formatConstruct(numChannels, bitsPerSample, compressed); } else if (magic == WAV_DATA_4CC) { if (!found_header) { /* ToDo: A bit wrong to check here, fmt chunk could come later... */ diff --git a/simgear/sound/soundmgr_openal.cxx b/simgear/sound/soundmgr_openal.cxx index 1dfbafa7..337a2373 100644 --- a/simgear/sound/soundmgr_openal.cxx +++ b/simgear/sound/soundmgr_openal.cxx @@ -51,10 +51,15 @@ using std::vector; #define MAX_SOURCES 128 - #ifndef ALC_ALL_DEVICES_SPECIFIER # define ALC_ALL_DEVICES_SPECIFIER 0x1013 #endif +#ifndef AL_FORMAT_MONO_MULAW_EXT +# define AL_FORMAT_MONO_MULAW_EXT 0x10014 +#endif +#ifndef AL_FORMAT_MONO_IMA4 +# define AL_FORMAT_MONO_IMA4 0x1300 +#endif class SGSoundMgr::SoundManagerPrivate { @@ -565,8 +570,9 @@ unsigned int SGSoundMgr::request_buffer(SGSoundSample *sample) ALenum format = AL_NONE; unsigned int fmt = sample->get_format(); - if (fmt == SG_SAMPLE_MONO8) format = AL_FORMAT_MONO8; 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; ALsizei size = sample->get_size(); ALsizei freq = sample->get_frequency(); diff --git a/simgear/sound/soundmgr_openal_private.hxx b/simgear/sound/soundmgr_openal_private.hxx index d1b4f85a..796652c5 100644 --- a/simgear/sound/soundmgr_openal_private.hxx +++ b/simgear/sound/soundmgr_openal_private.hxx @@ -46,9 +46,6 @@ #else # include # include -# ifdef HAVE_AL_EXT_H -# include -# endif #endif #include -- 2.39.5