我正在使用 Apple 开发者网站上的 SpeakHear 示例应用来创建录音应用。我正在尝试使用 kAudioFormatAppleIMA4 系统常量直接录制为 IMA4 格式。这被列为可用格式之一,但每次我设置音频格式变量并传递和设置它时,我都会收到“fmt?”错误。这是我用来设置音频格式变量的代码:
#define kAudioRecordingFormat kAudioFormatAppleIMA4
#define kAudioRecordingType kAudioFileCAFType
#define kAudioRecordingSampleRate 16000.00
#define kAudioRecordingChannelsPerFrame 1
#define kAudioRecordingFramesPerPacket 1
#define kAudioRecordingBitsPerChannel 16
#define kAudioRecordingBytesPerPacket 2
#define kAudioRecordingBytesPerFrame 2
- (void) setupAudioFormat: (UInt32) formatID {
// Obtains the hardware sample rate for use in the recording
// audio format. Each time the audio route changes, the sample rate
// needs to get updated.
UInt32 propertySize = sizeof (self.hardwareSampleRate);
OSStatus err = AudioSessionGetProperty (
kAudioSessionProperty_CurrentHardwareSampleRate,
&propertySize,
&hardwareSampleRate
);
if(err != 0){
NSLog(@"AudioRecorder::setupAudioFormat - error getting audio session property");
}
audioFormat.mSampleRate = kAudioRecordingSampleRate;
NSLog (@"Hardware sample rate = %f", self.audioFormat.mSampleRate);
audioFormat.mFormatID = formatID;
audioFormat.mChannelsPerFrame = kAudioRecordingChannelsPerFrame;
audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
audioFormat.mFramesPerPacket = kAudioRecordingFramesPerPacket;
audioFormat.mBitsPerChannel = kAudioRecordingBitsPerChannel;
audioFormat.mBytesPerPacket = kAudioRecordingBytesPerPacket;
audioFormat.mBytesPerFrame = kAudioRecordingBytesPerFrame;
}
这里是我使用该函数的地方:
- (id) initWithURL: fileURL {
NSLog (@"initializing a recorder object.");
self = [super init];
if (self != nil) {
// Specify the recording format. Options are:
//
// kAudioFormatLinearPCM
// kAudioFormatAppleLossless
// kAudioFormatAppleIMA4
// kAudioFormatiLBC
// kAudioFormatULaw
// kAudioFormatALaw
//
// When targeting the Simulator, SpeakHere uses linear PCM regardless of the format
// specified here. See the setupAudioFormat: method in this file.
[self setupAudioFormat: kAudioRecordingFormat];
OSStatus result = AudioQueueNewInput (
&audioFormat,
recordingCallback,
self, // userData
NULL, // run loop
NULL, // run loop mode
0, // flags
&queueObject
);
NSLog (@"Attempted to create new recording audio queue object. Result: %f", result);
// get the recording format back from the audio queue's audio converter --
// the file may require a more specific stream description than was
// necessary to create the encoder.
UInt32 sizeOfRecordingFormatASBDStruct = sizeof (audioFormat);
AudioQueueGetProperty (
queueObject,
kAudioQueueProperty_StreamDescription, // this constant is only available in iPhone OS
&audioFormat,
&sizeOfRecordingFormatASBDStruct
);
AudioQueueAddPropertyListener (
[self queueObject],
kAudioQueueProperty_IsRunning,
audioQueuePropertyListenerCallback,
self
);
[self setAudioFileURL: (CFURLRef) fileURL];
[self enableLevelMetering];
}
return self;
}
感谢您的帮助! -马特
最佳答案
我不确定您传递的所有格式标志是否正确; IMA4(IIRC,代表 IMA ADPCM 4:1)是 4 位(从 16 位压缩为 4:1),带有一些 header 。
根据 AudioStreamBasicDescription 的文档:
- mBytesPerFrame 应为 0,因为格式已压缩。
- mBitsPerChannel 应为 0,因为格式已压缩。
- mFormatFlags 可能应该为 0,因为没有什么可供选择。
根据 afconvert -f caff -t ima4 -c 1 blah.aiff blah.caf
后跟 afinfo blah.caf
:
- mBytesPerPacket 应为 34,并且
- mFramesPerPacket 应为 64。您可以将它们设置为 0。
引用算法在original IMA spec没那么有用(它是扫描的 OCR,该站点也有扫描)。
关于iphone - 在 iPhone 上以 IMA4 格式录制单声道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3815231/