Mark Sachs
2013-01-13 20:19:26 UTC
(Apologies for a possible double post: I tried to post first through
old.nabble.com's Web interface and it spit up a bunch of 500 errors.)
I'm trying to use OpenAL version 1.1 on Mac OSX Mountain Lion. I'm running
into a problem, though: a sound which plays back crisply in Audacity and
through OSX's Preview feature plays back muffled in my OpenAL code. It's as
if the sound fades in briefly at its start and then out at its end. No
unusual features of any kind are in use; I stripped everything down to a
minimal test program and it's still happening. I'm at a complete loss
here... anyone have any idea what's going on?
I've uploaded the sound in question to my web site at:
http://project-apollo.net/temp/shop_transact.wav
And here's the complete source code of the minimal playback program.
#include <unistd.h>
#include <iostream>
#include <vector>
using std::vector;
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
unsigned short ReadLE2(FILE* f)
{
unsigned char buffer[2];
fread(buffer, 2, 1, f);
return (buffer[1] << 8) + buffer[0];
}
uint ReadLE4(FILE* f)
{
unsigned char buffer[4];
fread(buffer, 4, 1, f);
return (buffer[3] << 24) + (buffer[2] << 16) + (buffer[1] << 8) +
buffer[0];
}
const char* ReadChunk4(FILE* f)
{
static char buffer[4];
if (fread(buffer, 4, 1, f) != 1)
return NULL; // EOF
return buffer;
}
bool CheckChunk4(FILE* f, const char* str)
{
const char* data = ReadChunk4(f);
return(strncmp(data, str, 4) == 0);
}
void Skip(FILE* f, uint bytes)
{
fseek(f, bytes, SEEK_CUR);
}
int main(int argc, const char * argv[])
{
ALCdevice* mDevice;
ALCcontext* mContext;
mDevice = alcOpenDevice("");
mContext = alcCreateContext(mDevice, NULL);
alcMakeContextCurrent(mContext);
ALuint mBuffer;
alGenBuffers(1, &mBuffer);
FILE* f = fopen("./shop_transact.wav", "rb");
// start reading chunks!
ALenum format;
ALsizei frequency;
vector<char> data;
while(1)
{
const char* chunkName = ReadChunk4(f);
if (!chunkName)
break;
if (strncmp(chunkName, "RIFF", 4) == 0)
{
// This is the RIFF type header.
Skip(f, 4); // Skip file size.
CheckChunk4(f, "WAVE");
continue;
}
if (strncmp(chunkName, "fmt ", 4) == 0)
{
// This is the format chunk.
uint dataSize = ReadLE4(f);
ReadLE2(f);
short channels = ReadLE2(f);
frequency = ReadLE4(f);
Skip(f, 4); // Skip BPS.
Skip(f, 2); // Skip block align.
short bits = ReadLE2(f);
if (channels == 1)
format = (bits == 8 ? AL_FORMAT_MONO8 : AL_FORMAT_MONO16);
else
format = (bits == 8 ? AL_FORMAT_STEREO8 : AL_FORMAT_STEREO16);
// Skip any extra data, though none should be present...
if (dataSize > 16)
Skip(f, dataSize - 16 + (dataSize % 2 == 1 ? 1 : 0));
continue;
}
if (strncmp(chunkName, "data", 4) == 0)
{
// This is the data chunk.
uint dataSize = ReadLE4(f);
data.resize(dataSize);
fread(&data[0], 1, dataSize, f);
if (dataSize % 2 == 1)
Skip(f, 1);
continue;
}
// Skip all other chunks.
uint dataSize = ReadLE4(f);
Skip(f, dataSize);
if (dataSize % 2 == 1)
Skip(f, 1);
}
fclose(f);
alBufferData(mBuffer, format, &data[0], (ALsizei)data.size(), frequency);
ALuint mSource;
alGenSources(1, &mSource);
alSourcei(mSource, AL_BUFFER, mBuffer);
std::cout << "Press a key to play the sound.\n";
while (1)
{
alSourcePlay(mSource);
if (getchar() == 'q')
{
alDeleteSources(0, &mSource);
alDeleteBuffers(0, &mBuffer);
alcDestroyContext(mContext);
alcCloseDevice(mDevice);
exit(0);
}
}
}
--
Mark Sachs
***@gmail.com
old.nabble.com's Web interface and it spit up a bunch of 500 errors.)
I'm trying to use OpenAL version 1.1 on Mac OSX Mountain Lion. I'm running
into a problem, though: a sound which plays back crisply in Audacity and
through OSX's Preview feature plays back muffled in my OpenAL code. It's as
if the sound fades in briefly at its start and then out at its end. No
unusual features of any kind are in use; I stripped everything down to a
minimal test program and it's still happening. I'm at a complete loss
here... anyone have any idea what's going on?
I've uploaded the sound in question to my web site at:
http://project-apollo.net/temp/shop_transact.wav
And here's the complete source code of the minimal playback program.
#include <unistd.h>
#include <iostream>
#include <vector>
using std::vector;
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
unsigned short ReadLE2(FILE* f)
{
unsigned char buffer[2];
fread(buffer, 2, 1, f);
return (buffer[1] << 8) + buffer[0];
}
uint ReadLE4(FILE* f)
{
unsigned char buffer[4];
fread(buffer, 4, 1, f);
return (buffer[3] << 24) + (buffer[2] << 16) + (buffer[1] << 8) +
buffer[0];
}
const char* ReadChunk4(FILE* f)
{
static char buffer[4];
if (fread(buffer, 4, 1, f) != 1)
return NULL; // EOF
return buffer;
}
bool CheckChunk4(FILE* f, const char* str)
{
const char* data = ReadChunk4(f);
return(strncmp(data, str, 4) == 0);
}
void Skip(FILE* f, uint bytes)
{
fseek(f, bytes, SEEK_CUR);
}
int main(int argc, const char * argv[])
{
ALCdevice* mDevice;
ALCcontext* mContext;
mDevice = alcOpenDevice("");
mContext = alcCreateContext(mDevice, NULL);
alcMakeContextCurrent(mContext);
ALuint mBuffer;
alGenBuffers(1, &mBuffer);
FILE* f = fopen("./shop_transact.wav", "rb");
// start reading chunks!
ALenum format;
ALsizei frequency;
vector<char> data;
while(1)
{
const char* chunkName = ReadChunk4(f);
if (!chunkName)
break;
if (strncmp(chunkName, "RIFF", 4) == 0)
{
// This is the RIFF type header.
Skip(f, 4); // Skip file size.
CheckChunk4(f, "WAVE");
continue;
}
if (strncmp(chunkName, "fmt ", 4) == 0)
{
// This is the format chunk.
uint dataSize = ReadLE4(f);
ReadLE2(f);
short channels = ReadLE2(f);
frequency = ReadLE4(f);
Skip(f, 4); // Skip BPS.
Skip(f, 2); // Skip block align.
short bits = ReadLE2(f);
if (channels == 1)
format = (bits == 8 ? AL_FORMAT_MONO8 : AL_FORMAT_MONO16);
else
format = (bits == 8 ? AL_FORMAT_STEREO8 : AL_FORMAT_STEREO16);
// Skip any extra data, though none should be present...
if (dataSize > 16)
Skip(f, dataSize - 16 + (dataSize % 2 == 1 ? 1 : 0));
continue;
}
if (strncmp(chunkName, "data", 4) == 0)
{
// This is the data chunk.
uint dataSize = ReadLE4(f);
data.resize(dataSize);
fread(&data[0], 1, dataSize, f);
if (dataSize % 2 == 1)
Skip(f, 1);
continue;
}
// Skip all other chunks.
uint dataSize = ReadLE4(f);
Skip(f, dataSize);
if (dataSize % 2 == 1)
Skip(f, 1);
}
fclose(f);
alBufferData(mBuffer, format, &data[0], (ALsizei)data.size(), frequency);
ALuint mSource;
alGenSources(1, &mSource);
alSourcei(mSource, AL_BUFFER, mBuffer);
std::cout << "Press a key to play the sound.\n";
while (1)
{
alSourcePlay(mSource);
if (getchar() == 'q')
{
alDeleteSources(0, &mSource);
alDeleteBuffers(0, &mBuffer);
alcDestroyContext(mContext);
alcCloseDevice(mDevice);
exit(0);
}
}
}
--
Mark Sachs
***@gmail.com