Problem Loading Audio File into Buffer
Problem Loading Audio File into Buffer
- Subject: Problem Loading Audio File into Buffer
- From: Jannik <email@hidden>
- Date: Fri, 06 Jan 2006 00:45:57 +0000 (GMT)
Hello AU list. :) This is my first email to the list, but have been reading other posts for some time now. I
have some years programming experience, also with object oriented languages, but am a C++ newbie.
I have spent a long time trying to learn the AU framework with what I find to be somewhat incomplete
documentation, and am writing an AU sample based synthesizer for my final year university computer
science project. I have tried combining code from the ConvertFile and SinSynth CoreAudio example
projects. I want to open a short file, load it into an AudioFileBuffer, and play back this buffer repeatedly.
Below I have pasted the method which opens a file and loads it into an AudioFileBuffer, and the method
which presents audio data to the pulling AU host. I do get some sound, but it is noise. I think I am
having a problem with the AudioBufferList, and the pointers to them. Is it possibly because the
ExtAudioFileRead function in the first while loop reads audio file data in chunks, and the returned audio
is continously overwritten (seems the original ConvertFile did the conversion in chunks)? If so, how
would I go about preventing this? Any help would be highly appreciated! :) I am at a complete loss.
Best regards,
Jannik
AudioBufferList OpenFile (FSRef &inputFSRef)
{
ExtAudioFileRef infile;
// Use file in specific location
const char* inputFile = "/Users/akindo/file.aif";
// input file.
CFStringRef filename = CFStringCreateWithCString (NULL, inputFile, kCFStringEncodingUTF8);
CFURLRef infileurl = CFURLCreateWithFileSystemPath (NULL, filename, kCFURLPOSIXPathStyle,
true);
CFRelease (filename);
// Assign file location to &inputFSRef.
Boolean res = CFURLGetFSRef(infileurl, &inputFSRef);
CFRelease (infileurl);
XThrowIfError (!res, "CFURLGetFSRef");
// first open the input file
OSStatus err = ExtAudioFileOpen (&inputFSRef, &infile);
XThrowIfError (err, "ExtAudioFileOpen");
// get the input file format
CAStreamBasicDescription inputFormat;
UInt32 size = sizeof(inputFormat);
err = ExtAudioFileGetProperty(infile, kExtAudioFileProperty_FileDataFormat, &size, &inputFormat);
XThrowIfError (err, "ExtAudioFileGetProperty kExtAudioFileProperty_FileDataFormat");
// set up buffers
UInt32 kSrcBufSize = 32768;
char srcBuffer[kSrcBufSize];
AudioBufferList fillBufList;
fillBufList.mNumberBuffers = 1;
fillBufList.mBuffers[0].mNumberChannels = inputFormat.mChannelsPerFrame;
fillBufList.mBuffers[0].mDataByteSize = kSrcBufSize;
fillBufList.mBuffers[0].mData = srcBuffer;
UInt32 numFrames = (kSrcBufSize / inputFormat.mBytesPerPacket) *
inputFormat.mFramesPerPacket; //normally 1
// Read the file.
while (1)
{
err = ExtAudioFileRead (infile, &numFrames, &fillBufList);
if (err || !numFrames) {
// this is our termination condition
break;
}
// This was from the old ConvertFile project which convert from one format to another.
//err = ExtAudioFileWrite(outfile, numFrames, &fillBufList);
//XThrowIfError (err, "ExtAudioFileWrite");
}
// Close file.
ExtAudioFileDispose(infile);
// Return audioFileBuffer
return fillBufList;
}
#pragma mark TestNote Methods
OSStatus TestNote::Render(UInt32 inNumFrames, AudioBufferList& inBufferList)
{
// Load audio file into "audioFileBuffer"
AudioBufferList audioFileBuffer;
FSRef inputFSRef;
audioFileBuffer = OpenFile(inputFSRef);
float *left, *right;
float globalVol = GetGlobalParameter(kGlobalVolumeParam);
int numChans = inBufferList.mNumberBuffers;
if (numChans > 2) return -1;
// Pointers for audio output data.
left = (float*)inBufferList.mBuffers[0].mData;
right = numChans == 2 ? (float*)inBufferList.mBuffers[1].mData : 0;
// Pointers for audio file data.
float *audioFileLeft = (float*)audioFileBuffer.mBuffers[0].mData;
float *audioFileRight = numChans == 2 ? (float*)audioFileBuffer.mBuffers[1].mData : 0;
double sampleRate = SampleRate();
double freq = Frequency() * (twopi/sampleRate);
#if DEBUG_PRINT_RENDER
printf("TestNote::Render X %d %g %g\n", this, GetState(), phase, amp);
#endif
switch (GetState())
{
case kNoteState_Attacked :
case kNoteState_Sostenutoed :
case kNoteState_ReleasedButSostenutoed :
case kNoteState_ReleasedButSustained :
{
UInt32 fileFrame = 0;
for (UInt32 frame=0; frame<inNumFrames; ++frame)
{
// Reached end of file, start over.
if (frame == numberOfFrames) {
fileFrame = 0;
}
// Assign audio file buffer data to output buffer.
left[frame] = audioFileLeft[fileFrame];
if (right) right[frame] = audioFileRight[fileFrame];
++fileFrame;
}
}
break;
case kNoteState_Released :
...
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden