Add support for native mulaw encoded samples if the OpenAL implementation supports it

This commit is contained in:
Erik Hofman 2016-05-30 11:46:41 +02:00
parent d1a808c630
commit ae375f44f2
4 changed files with 26 additions and 12 deletions

View File

@ -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();

View File

@ -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... */

View File

@ -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();

View File

@ -46,9 +46,6 @@
#else
# include <AL/al.h>
# include <AL/alc.h>
# ifdef HAVE_AL_EXT_H
# include <AL/alext.h>
# endif
#endif
#include <simgear/structure/SGSharedPtr.hxx>