![](/img/trans.png)
[英]Chapter 4 of Learning Core Audio not working due to AudioQueueNewInput failing with fmt?
[英]Learning Core Audio with Book Sample Code not working
您好,我正在嘗試學習核心音頻服務器這本書: http : //www.amazon.com/Learning-Core-Audio-Hands-On-Programming/dp/0321636848
但是當我嘗試運行此代碼時:
#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioToolbox.h>
#define SAMPLE_RATE 44100
#define DURATION 5
#define FILENAME_FORMAT @"%0.03f-test.aif"
int main(int argc, const char * argv[])
{
@autoreleasepool
{
if(argc<2)
return -1;
double hz = 44;
assert(hz>0);
NSLog(@"Generating hz tone:%f",hz);
NSString* fileName = [NSString stringWithFormat:FILENAME_FORMAT, hz];
NSString* filePath = [[[NSFileManager defaultManager]currentDirectoryPath]
stringByAppendingPathComponent:fileName];
NSURL* fileURL = [NSURL fileURLWithPath:filePath];
AudioStreamBasicDescription asbd;
memset(&asbd, 0, sizeof(asbd));
asbd.mSampleRate = SAMPLE_RATE;
asbd.mFormatID = kAudioFormatLinearPCM;
asbd.mFormatFlags = kAudioFormatFlagIsBigEndian | kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
asbd.mBitsPerChannel = 16;
asbd.mChannelsPerFrame = 1;
asbd.mFramesPerPacket = 1;
asbd.mBytesPerFrame = 2;
asbd.mBytesPerPacket = 2;
AudioFileID audioFile;
OSStatus audioErr = noErr;
audioErr = AudioFileCreateWithURL((__bridge CFURLRef)fileURL, kAudioFileAIFFType, &asbd, kAudioFileFlags_EraseFile, &audioFile);
assert(audioErr == noErr);
long maxSampleCount = SAMPLE_RATE * DURATION;
long sampleCount = 0;
UInt32 bytesToWrite = 2;
double waveLengthInSamples = SAMPLE_RATE / hz;
while(sampleCount < maxSampleCount)
{
for(int i=0;i<waveLengthInSamples;i++)
{
SInt16 sample;
if(i<waveLengthInSamples/2)
sample = CFSwapInt16BigToHost(SHRT_MAX);
else
sample = CFSwapInt16BigToHost(SHRT_MIN);
audioErr = AudioFileWriteBytes(audioFile, false, sampleCount*2, &bytesToWrite, &sample);
assert(audioErr = noErr);
sampleCount++;
}
}
audioErr = AudioFileClose(audioFile);
assert(audioErr = noErr);
}
return 0;
}
程序退出,並顯示以下錯誤代碼:程序結束於退出代碼:255
誰能幫我? 我下載了示例代碼,並發生了相同的錯誤。 我正在使用xcode 5和64位Macbook。 謝謝你的幫助。
您似乎已修改了本書的代碼,以將音調顯式設置為44Hz。
double hz = 44;
但是,原始代碼希望您將音調作為命令行參數輸入。 這些行正在檢查該參數,如果找不到參數,則返回-1(或255)。
if(argc<2)
return -1;
刪除這兩行以刪除參數檢查。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.