我编写了这段代码,使用Audio ToolBox框架创建了一个音频文件sine.aif。
我有兴趣在不运行手机的情况下捕获此文件的数据延迟,只需在命令行上运行它并捕获时间。这可能吗?我对Core Audio的了解不多。
以下是代码:
#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioToolbox.h>
#define SAMPLE_RATE 44100
#define DURATION 5.0
#define FILENAME_FORMAT @"%0.3f-sine.aif"
int main(int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]init];
if(argc < 2) {
printf("Usage:CAToneFileGenerator 261.526\n");
return -1;
}
double hz = atof(argv[1]);
assert (hz > 0);
NSLog (@"generating %f hz tone", hz);
NSString *fileName = [NSString stringWithFormat:FILENAME_FORMAT, hz];
NSString *filePath = [[[NSFileManager defaultManager]currentDirectoryPath]stringByAppendingPathComponent:fileName];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
//Prepare the format
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;
//Set up the file
AudioFileID audioFile;
OSStatus audioErr = noErr;
audioErr = AudioFileCreateWithURL((CFURLRef)fileURL, kAudioFileAIFFType, &asbd, kAudioFileFlags_EraseFile, &audioFile);
assert(audioErr == noErr);
//Start writing samples
long maxSampleCount = SAMPLE_RATE * DURATION;
//NSLog (@"maxSampleCount %long", maxSampleCount);
long sampleCount = 0;
UInt32 bytesToWrite = 2;
double wavelengthInSamples = SAMPLE_RATE / hz;
NSLog (@"wavelengthInSamples %f", wavelengthInSamples);
while (sampleCount < maxSampleCount) {
for (int i=0; i < wavelengthInSamples; i++) {
// sine wave
SInt16 sample = CFSwapInt16HostToBig ((SInt16)SHRT_MAX * sin(2 * M_PI * (i / wavelengthInSamples)));
audioErr = AudioFileWriteBytes(audioFile, false, sampleCount*2, &bytesToWrite, &sample);
assert(audioErr == noErr);
sampleCount++;
}
}
audioErr = AudioFileClose(audioFile);
assert(audioErr == noErr);
NSLog (@"wrote %d samples", sampleCount);
[pool drain];
return 0;
}