iphone - 如何查看ExtAudioFileRead的结果?

标签 iphone ios audio core-audio

我正在使用ExtAudioFileRead将WAV文件作为float *缓冲区读入内存。但是,我不确定结果-当我打印出来时,得到的值是-1到+ 1(理论上应该是正确的),但是如何确定它们是正确的呢?

- (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;
}

最佳答案

我能想到的只有几种测试方法:

  • 将您已加载的数据与您知道的有效方法加载的数据进行比较
  • 以某种方式(可能使用AudioQueue)播放音频数据
  • 关于iphone - 如何查看ExtAudioFileRead的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15599873/

    相关文章:

    iphone - 内存错误访问 : Questions with releases, 协议(protocol)和委托(delegate)

    ios - Admob Native Advanced 不可点击

    objective-c - 如何在 ios 版本中设置 Aviary 裁剪预设?

    flash - 带有背景音频的快退/前进SWF

    c# - 在C#中使用Windows Media Player播放声音

    c# - 带有(一点点)Unity 3D 的 iOS 项目?

    iphone - 在 iPhone/iPod Touch 上本地存储包含 333,000 行的 sqlite 数据库是否可行?

    iphone - UITableView 编辑模式不工作

    ios - swift : UIPickerViewController delegates not called after Class-Extraction

    javascript - 如何通过音频切换向网页添加背景音乐?