我从wav文件(其采样率为44100 is,长时间为2秒)中获得AudioBufferList。但我拿不到44100*2=88200样本。实际上,我得到了一个包含512 nNumberBuffers的AudiobufferList。如何从AudioBufferList获取样本?
发布于 2014-11-06 15:13:00
下面是我从wav文件中获取样本的方法
-(float *) GetSoundFile:(NSString *)fileName
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:[fileName stringByDeletingPathExtension]
ofType:[fileName pathExtension]];
NSURL *audioURL = [NSURL fileURLWithPath:path];
if (!audioURL)
{
NSLog(@"file: %@ not found.", fileName);
}
OSStatus err = 0;
theFileLengthInFrames = 0; //this is global
AudioStreamBasicDescription theFileFormat;
UInt32 thePropertySize = sizeof(theFileFormat);
ExtAudioFileRef extRef = NULL;
void* theData = NULL;
AudioStreamBasicDescription theOutputFormat;
// Open a file with ExtAudioFileOpen()
err = ExtAudioFileOpenURL((__bridge CFURLRef)(audioURL), &extRef);
// Get the audio data format
err = ExtAudioFileGetProperty(extRef, kExtAudioFileProperty_FileDataFormat, &thePropertySize, &theFileFormat);
theOutputFormat.mSampleRate = samplingRate = theFileFormat.mSampleRate;
theOutputFormat.mChannelsPerFrame = numChannels = 1;
theOutputFormat.mFormatID = kAudioFormatLinearPCM;
theOutputFormat.mBytesPerFrame = sizeof(Float32) * theOutputFormat.mChannelsPerFrame ;
theOutputFormat.mFramesPerPacket = theFileFormat.mFramesPerPacket;
theOutputFormat.mBytesPerPacket = theOutputFormat.mFramesPerPacket * theOutputFormat.mBytesPerFrame;
theOutputFormat.mBitsPerChannel = sizeof(Float32) * 8 ;
theOutputFormat.mFormatFlags = 9 | 12;
//set the output property
err = ExtAudioFileSetProperty(extRef, kExtAudioFileProperty_ClientDataFormat, sizeof(AudioStreamBasicDescription), &theOutputFormat);
// Here I Get the total frame count and write it into gloabal variable
thePropertySize = sizeof(theFileLengthInFrames);
err = ExtAudioFileGetProperty(extRef, kExtAudioFileProperty_FileLengthFrames, &thePropertySize, &theFileLengthInFrames);
UInt32 dataSize = (UInt32)(theFileLengthInFrames * theOutputFormat.mBytesPerFrame);
theData = malloc(dataSize);
AudioBufferList theDataBuffer;
if (theData)
{
theDataBuffer.mNumberBuffers = 1;
theDataBuffer.mBuffers[0].mDataByteSize = dataSize;
theDataBuffer.mBuffers[0].mNumberChannels = theOutputFormat.mChannelsPerFrame;
theDataBuffer.mBuffers[0].mData = theData;
// Read the data into an AudioBufferList
err = ExtAudioFileRead(extRef, (UInt32*)&theFileLengthInFrames, &theDataBuffer);
}
return (float*)theDataBuffer.mBuffers[0].mData;
//here is the data that has thefilelengthInframes amount frames
}发布于 2013-02-11 18:29:51
AudioBufferList不应设置为固定大小,因为它是一个临时缓冲区,依赖于硬件。大小是不可预测的,您只能设置一个更好的大小,而且不允许在两次读取调用之间保持相同的大小。要获得88200个采样(或您喜欢的),您必须使用AudioBufferList中的采样,按时间递增地填充新缓冲区。我建议使用这个循环缓冲区https://github.com/michaeltyson/TPCircularBuffer。希望这对你有所帮助
发布于 2012-09-18 00:18:58
据我所知,AudioBufferList必须由您配置以接收数据,然后必须由某个读取函数(例如ExtAudioFileRead())填充。因此,您应该通过分配所需的缓冲区(通常是1或2)来准备它,将nNumberBuffers设置为该数字,并将音频数据读取给它们。AudioBufferList只是存储该缓冲区,它们将包含帧值。
https://stackoverflow.com/questions/12445227
复制相似问题