在 iPhone 上以 IMA4 格式录制单声道

发布于 2024-09-25 06:23:06 字数 3866 浏览 1 评论 0原文

我正在使用 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;
} 

感谢您的帮助! -马特

I'm using the SpeakHear sample app on Apple's developer site to create an audio recording app. I'm attempting to record directly to IMA4 format using the kAudioFormatAppleIMA4 system constant. This is listed as one of the usable formats, but every time I set up my audio format variable and pass and set it, I get a 'fmt?' error. Here is the code I use to set up the audio format variable:

#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;

}

And here is where I use that function:

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

Thanks for the help!
-Matt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

书间行客 2024-10-02 06:23:06

我不确定您传递的所有格式标志是否正确; IMA4(IIRC 代表 IMA ADPCM 4:1)是 4 位(从 16 位进行 4:1 压缩),带有一些标头。

根据 AudioStreamBasicDescription

  • mBytesPerFrame 应为 0,因为格式已压缩。
  • mBitsPerChannel 应为 0,因为格式已压缩。
  • mFormatFlags 可能应该为 0,因为没有什么可以选择的。

根据 afconvert -f caff -t ima4 -c 1 blah.aiff blah.cafafinfo blah.caf

  • mBytesPerPacket 应为 34,
  • mFramesPerPacket 应为 64。您也许可以将它们设置为 0。

原始 IMA 规范中的参考算法没有那么有用(这是扫描件的 OCR,该网站也有扫描件)。

I'm not sure that all the format flags you're passing are correct; IMA4 (which, IIRC, stands for IMA ADPCM 4:1) is 4-bit (4:1 compression from 16 bits) with some headers.

According to the docs for AudioStreamBasicDescription:

  • mBytesPerFrame should be 0, since the format is compressed.
  • mBitsPerChannel should be 0, since the format is compressed.
  • mFormatFlags should probably be 0, since there is nothing to choose.

Aaccording to afconvert -f caff -t ima4 -c 1 blah.aiff blah.caf followed by afinfo blah.caf:

  • mBytesPerPacket should be 34, and
  • mFramesPerPacket should be 64. You might be able to set these to 0 instead.

The reference algorithm in the original IMA spec is not that helpful (It's an OCR of scans, the site also has the scans).

°如果伤别离去 2024-10-02 06:23:06

最重要的是@tc。已经说过,使用此方法可以更轻松地根据 ID 自动填充您的描述:

AudioStreamBasicDescription streamDescription;
UInt32 streamDesSize = sizeof(streamDescription);
memset(&streamDescription, 0, streamDesSize);
streamDescription.mFormatID         = kAudioFormatiLBC;

OSStatus status;
status = AudioFormatGetProperty(kAudioFormatProperty_FormatInfo, 0, NULL, &streamDesSize, &streamDescription);
assert(status==noErr);

这样您就无需费心猜测某些格式的功能。请注意,尽管在此示例中 kAudioFormatiLBC 不需要任何其他附加信息,但其他格式需要(通常是通道数和采样率)。

On top of what @tc. has already said, it's easier to automatically populate your descriptions based on the IDs using this:

AudioStreamBasicDescription streamDescription;
UInt32 streamDesSize = sizeof(streamDescription);
memset(&streamDescription, 0, streamDesSize);
streamDescription.mFormatID         = kAudioFormatiLBC;

OSStatus status;
status = AudioFormatGetProperty(kAudioFormatProperty_FormatInfo, 0, NULL, &streamDesSize, &streamDescription);
assert(status==noErr);

This way you don't need to bother with guessing the features of certain formats. Be warned, although in this example the kAudioFormatiLBC didn't need any other additional info, other formats do (usually the number of channels and the sample rate).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文