2013-03-24 51 views
1

我正在使用ExtAudioFileRead將WAV文件作爲float * buffer讀入內存。但是,我不確定結果 - 當我打印出結果時,我得到的值是-1到+1(理論上應該是正確的),但我怎麼能確定它們是正確的呢?如何查看ExtAudioFileRead的結果?

- (float *) readTestFileAndSize: (int *) size 
{ 
CFStringRef str = CFStringCreateWithCString(
              NULL, 
              [[[NSBundle mainBundle] pathForResource: @"25" ofType:@"wav"] UTF8String], 
              kCFStringEncodingMacRoman 
              ); 
CFURLRef inputFileURL = CFURLCreateWithFileSystemPath(
                 kCFAllocatorDefault, 
                 str, 
                 kCFURLPOSIXPathStyle, 
                 false 
                ); 

ExtAudioFileRef fileRef; 
ExtAudioFileOpenURL(inputFileURL, &fileRef); 

SInt64       theFileLengthInFrames = 0; 
// Get the total frame count 
UInt32 thePropertySize = sizeof(theFileLengthInFrames); 

ExtAudioFileGetProperty(fileRef, kExtAudioFileProperty_FileLengthFrames, &thePropertySize, &theFileLengthInFrames); 

AudioStreamBasicDescription audioFormat; 
audioFormat.mSampleRate = 44100; 
audioFormat.mFormatID = kAudioFormatLinearPCM; 
audioFormat.mFormatFlags = kLinearPCMFormatFlagIsFloat; 
audioFormat.mBitsPerChannel = sizeof(Float32) * 8; 
audioFormat.mChannelsPerFrame = 1; // Mono 
audioFormat.mBytesPerFrame = audioFormat.mChannelsPerFrame * sizeof(Float32); // == sizeof(Float32) 
audioFormat.mFramesPerPacket = 1; 
audioFormat.mBytesPerPacket = audioFormat.mFramesPerPacket * audioFormat.mBytesPerFrame; // = sizeof(Float32) 

// 3) Apply audio format to the Extended Audio File 
ExtAudioFileSetProperty(
         fileRef, 
         kExtAudioFileProperty_ClientDataFormat, 
         sizeof (AudioStreamBasicDescription), //= audioFormat 
         &audioFormat); 

int numSamples = 1024; //How many samples to read in at a time 
UInt32 sizePerPacket = audioFormat.mBytesPerPacket; // = sizeof(Float32) = 32bytes 
UInt32 packetsPerBuffer = numSamples; 
UInt32 outputBufferSize = packetsPerBuffer * sizePerPacket; 

// So the lvalue of outputBuffer is the memory location where we have reserved space 
UInt8 *outputBuffer = (UInt8 *)malloc(sizeof(UInt8 *) * outputBufferSize); 

NSLog(@"outputBufferSize - %llu", theFileLengthInFrames); 

float* total = malloc(theFileLengthInFrames * sizeof(float)); 

*size = theFileLengthInFrames; 

AudioBufferList convertedData; 

convertedData.mNumberBuffers = 1; // Set this to 1 for mono 
convertedData.mBuffers[0].mNumberChannels = audioFormat.mChannelsPerFrame; //also = 1 
convertedData.mBuffers[0].mDataByteSize = outputBufferSize; 
convertedData.mBuffers[0].mData = outputBuffer; // 

int totalBytes = 0; 

UInt32 frameCount = numSamples; 
while (frameCount > 0) { 

    ExtAudioFileRead(fileRef, &frameCount, &convertedData); 
    if (frameCount > 0) { 
     AudioBuffer audioBuffer = convertedData.mBuffers[0]; 
     float *samplesAsCArray = (float *)audioBuffer.mData; 

     memcpy(total + totalBytes, samplesAsCArray, frameCount * sizeof(float)); 

     totalBytes += frameCount; 
    } 
} 

return total; 
} 

回答

0

只有測試,我能想到的幾種方法:

  • 比較已加載到的東西加載數據的數據,你知道作品

  • 播放音頻數據以某種方式返回(可能使用AudioQueue