RE: Question about OpenAl Versions
"Bruce Clay" <[email protected]>
| Newsgroups | gmane.comp.lib.openal |
|---|---|
| Message-ID | <23F8522698B84A128CAC0C2ED70AAF38@bclayPC> |
> I did write a WaveFile class to create and playback wave files but I am
> having problems with ausio that I capture to a wave file. Playback of
> downloaded wave files all sounds clean but there is a crackle at the
> beginning of the files I create. After that the audio seems to be ok.
Seems like the start of the data chunk of wave files you create may have
garbage in it. I can't say without seeing code, though.
Chris:
Thanks again for your reply.
The OpenAL code that I am using to create the file follows. The wave file
code follows that.
int OpenAlWrapper::RecordToWaveFile(std::string deviceName, int sampleRate,
int numChannels, int numBits, int
sampleBufSize, std::string filename)
{
int status = -1;
char errBuf[255];
FILE *fdes = NULL;
unsigned char *dataBuf;
char *extentPtr;
extentPtr = (char *)strrchr(filename.c_str(), '.');
if (extentPtr != NULL)
{
extentPtr++;
if ((stricmp(extentPtr, "WAV") == 0) && (mWaveFile != NULL))
{
dataBuf = new unsigned char [sampleBufSize * numBits
/ 8];
// sampleRate = 22050;
// collecting 16 bit samples
int numSamples = sampleBufSize;
// audioFormat = AL_FORMAT_MONO16;
int audioFormat = GetAudioFormat(numChannels,
numBits);
if ((dataBuf != NULL) &&
(alcIsExtensionPresent(NULL, "ALC_EXT_CAPTURE") == AL_TRUE ))
{
if (deviceName.length() > 0)
{
mCurrCaptureDevice =
alcCaptureOpenDevice (deviceName.c_str(),
sampleRate, audioFormat, sampleBufSize);
}
else
{
mCurrCaptureDevice =
alcCaptureOpenDevice (NULL, sampleRate,
audioFormat, sampleBufSize);
}
int error = alGetError();
if (error != AL_NO_ERROR)
{
sprintf(errBuf, "Could not open
capture device: %s", alGetString (error));
}
else
{
fdes =
mWaveFile->CreateWaveFile(filename, numChannels, sampleRate, numBits);
if (fdes != NULL)
{
status =
StartAudioCapture(mCurrCaptureDevice);
if (status == 0)
{
int numSamples =
sampleBufSize;
int totalSamples =
0;
memset(dataBuf, 0,
numSamples);
// get the first block of data
status =
GetCapturedSamples(mCurrCaptureDevice, numSamples, dataBuf);
unsigned long
startChunkPos = ftell(fdes);
mWaveFile->StartDataChunk(fdes, numSamples * numBits / 8, dataBuf);
mStopRecording =
false;
while
((mStopRecording != true) && (status == 0) && (numSamples > 0))
{
status =
GetCapturedSamples(mCurrCaptureDevice, numSamples, dataBuf);
if ((status
== 0) && (numSamples > 0))
{
status = mWaveFile->AddToDataChunk(fdes, numSamples * numBits / 8, dataBuf);
#ifdef WIN32
MSG
message;
while (::PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
{
::TranslateMessage(&message);
::DispatchMessage(&message);
}
#endif // WIN32
}
}
int chunkSize =
ftell(fdes) - startChunkPos;
status =
mWaveFile->CloseDataChunk(fdes);
status =
mWaveFile->CloseWaveFile(fdes, chunkSize);
}
}
alcCaptureCloseDevice(mCurrCaptureDevice);
mCurrCaptureDevice = 0;
}
delete dataBuf;
}
}
}
return(status);
}
Wave file code
////////////////////////////////////////////////////////////////////////////
////////////////////////
FILE *WaveFile::CreateWaveFile(std::string filename, int numChannels,
int sampleRate, int bitsPerSample)
{
int status = 0;
FILE *fdes = NULL;
unsigned long sizeIndex;
unsigned long fileSize = 0;
struct riffHeaderRec header;
struct formatChunkRec formatChunk;
int fieldLen = 0;
if (filename.length() > 0)
{
mCurrFilePos = 0;
fdes = fopen(filename.c_str(), "wb");
if (fdes != NULL)
{
strncpy(header.chunkType, RIFF_TAG,
strlen(RIFF_TAG));
header.chunkSize = 0;
strncpy(header.subType, WAVE_TAG, strlen(WAVE_TAG));
// save the location of the size index. we will have to back up to
hear to write the size in the file
sizeIndex = (unsigned int)&header.chunkSize -
(unsigned int)&header;
fwrite(&header, sizeof(header), 1, fdes);
formatChunk.format = WAVE_FORMAT_PCM;
formatChunk.numChannels = numChannels;
formatChunk.samplesPerSec = sampleRate;
formatChunk.avgBytesPerSec = sampleRate *
numChannels * bitsPerSample / 8;
formatChunk.blockAlign = numChannels * bitsPerSample
/ 8;
formatChunk.bitsPerSample = bitsPerSample;
fwrite(FORMAT_TAG, strlen(FORMAT_TAG), 1, fdes);
fieldLen = sizeof(formatChunk);
fwrite(&fieldLen, sizeof(fieldLen), 1, fdes);
fwrite(&formatChunk, fieldLen, 1, fdes);
status = 0;
}
}
return(fdes);
} // end of CreateWaveFile method
////////////////////////////////////////////////////////////////////////////
////////////////////////
int WaveFile::StartDataChunk(FILE *fdes, int chunkSize, unsigned char
*chunkData)
{
int status = -1;
struct chunkHdrRec chunkBuf;
if ((fdes != NULL) && (chunkData != NULL))
{
strncpy(chunkBuf.chunkId, DATA_TAG, strlen(DATA_TAG));
chunkBuf.chunkSize = 0;
mStartChunkPos = ftell(fdes);
fwrite(&chunkBuf, sizeof(chunkBuf), 1, fdes);
fwrite(chunkData, chunkSize, 1, fdes);
}
return(status);
}
////////////////////////////////////////////////////////////////////////////
////////////////////////
int WaveFile::AddToDataChunk(FILE *fdes, int chunkSize, unsigned char
*chunkData)
{
int status = -1;
if ((fdes != NULL) && (chunkData != NULL))
{
fwrite(chunkData, chunkSize, 1, fdes);
status = 0;
}
return(status);
}
////////////////////////////////////////////////////////////////////////////
////////////////////////
int WaveFile::CloseDataChunk(FILE *fdes)
{
int status = -1;
unsigned int chunkSize;
if (fdes != NULL)
{
unsigned long currPos = ftell(fdes);
chunkSize = currPos - mStartChunkPos;
fseek(fdes, mStartChunkPos + sizeof(chunkSize), SEEK_SET);
fwrite(&chunkSize, chunkSize, 1, fdes);
// move back to end of file for next chunk
fseek(fdes, 0, SEEK_END);
status = 0;
}
return(status);
}
_______________________________________________
Openal mailing list
[email protected]
http://opensource.creative.com/mailman/listinfo/openal