2

我试图通过使用 Xcode 和 AudioFileServices 将它们写入 WAV 文件来保存一些原始 PCM 值,但是我很难将简单的正弦波写入文件。

这是我的代码:

// Set up output file
AudioStreamBasicDescription outputFormat = {0};
outputFormat.mSampleRate = 44100;
outputFormat.mFormatID = kAudioFormatLinearPCM;
outputFormat.mBitsPerChannel = 32;
outputFormat.mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked;
outputFormat.mFramesPerPacket = 1;
outputFormat.mChannelsPerFrame = 1;
outputFormat.mBytesPerFrame = 4;
outputFormat.mBytesPerPacket = 4;

AudioFileID outputFile;
CFURLRef outputFileURL =
CFURLCreateWithFileSystemPath(kCFAllocatorDefault, CFSTR("output.wav"), kCFURLPOSIXPathStyle, FALSE);
CheckError(AudioFileCreateWithURL(outputFileURL, kAudioFileWAVEType, &outputFormat, kAudioFileFlags_EraseFile, &outputFile), "Could not open output File");
CFRelease(outputFileURL);

Float32 duration = 3.0;
Float32 freq = 261.37;
Float32 amplitude = 0.3;
UInt32 sizeOfBuffer = (UInt32)(outputFormat.mSampleRate*duration)*sizeof(Float32);
Float32 *audioBuffer = (Float32*)malloc(sizeOfBuffer);
printf("Creating Buffer.... \n");
for (int i = 0; i<(int)(outputFormat.mSampleRate*duration); i++)
    audioBuffer[i] = amplitude*sinf(i*freq/outputFormat.mSampleRate*2*M_PI);

printf("Writing to File.... \n");
// Write to Output File
CheckError(AudioFileWriteBytes(outputFile, FALSE, 0, &sizeOfBuffer, &audioBuffer), "Could not write bytes to file");

printf("Write Succesful!\n");
CheckError(AudioFileClose(outputFile), "Could not close file");

该代码成功地写入了一个持续时间正确的文件,但听起来真的很扭曲。我错过了什么吗?

4

1 回答 1

2

回答了我自己的问题。当我使用AudioFileWriteBytes&audioBuffer,我应该刚刚通过audioBuffer,因为它已经是一个指针。

这是正确的代码:

// Set up output file
AudioStreamBasicDescription outputFormat = {0};
outputFormat.mSampleRate = 44100;
outputFormat.mFormatID = kAudioFormatLinearPCM;
outputFormat.mBitsPerChannel = 32;
outputFormat.mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked;
outputFormat.mFramesPerPacket = 1;
outputFormat.mChannelsPerFrame = 1;
outputFormat.mBytesPerFrame = 4;
outputFormat.mBytesPerPacket = 4;

AudioFileID outputFile;
CFURLRef outputFileURL =
CFURLCreateWithFileSystemPath(kCFAllocatorDefault, CFSTR("output.wav"), kCFURLPOSIXPathStyle, FALSE);
CheckError(AudioFileCreateWithURL(outputFileURL, kAudioFileWAVEType, &outputFormat, kAudioFileFlags_EraseFile, &outputFile), "Could not open output File");
CFRelease(outputFileURL);

Float32 duration = 3.0;
Float32 freq = 261.37;
Float32 amplitude = 0.3;
UInt32 sizeOfBuffer = (UInt32)(outputFormat.mSampleRate*duration)*sizeof(Float32);
Float32 *audioBuffer = (Float32*)malloc(sizeOfBuffer);
printf("Creating Buffer.... \n");
for (int i = 0; i<(int)(outputFormat.mSampleRate*duration); i++)
    audioBuffer[i] = amplitude*sinf(i*freq/outputFormat.mSampleRate*2*M_PI);

printf("Writing to File.... \n");
// Write to Output File
CheckError(AudioFileWriteBytes(outputFile, FALSE, 0, &sizeOfBuffer, audioBuffer), "Could not write bytes to file");

printf("Write Succesful!\n");
CheckError(AudioFileClose(outputFile), "Could not close file");
于 2014-02-19T16:35:48.423 回答