首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以IMA4格式在iPhone上录制单声道

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

Stack Overflow用户
提问于 2010-09-29 01:04:22
回答 2查看 2.7K关注 0票数 0

我正在使用苹果开发人员网站上的SpeakHear示例应用程序来创建音频记录应用程序。我尝试使用kAudioFormatAppleIMA4系统常量直接记录到IMA4格式。这被列为可用的格式之一,但每次我设置音频格式变量并传递和设置它时,我都会得到一个'fmt?‘错误。下面是我用来设置音频格式变量的代码:

代码语言:javascript
复制
#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;

}

下面是我使用该函数的地方:

代码语言:javascript
复制
- (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;
} 

谢谢你的帮助!-Matt

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-09-29 06:56:18

我不确定您传递的所有格式标志是否都是正确的;IMA4 ( IIRC,代表IMA ADPCM 4:1)是4位的( 16位的4:1压缩),其中包含一些头部。

根据AudioStreamBasicDescription的文档

  • mBytesPerFrame应该是0,因为格式是compressed.
  • mBitsPerChannel应该是0,因为格式是compressed.
  • mFormatFlags应该是0,因为没有什么可选择的。

A根据afconvert -f caff -t ima4 -c 1 blah.aiff blah.caf,然后是afinfo blah.caf

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

original IMA spec中的参考算法没有那么有用(它是扫描的光学字符识别,网站也有扫描)。

票数 3
EN

Stack Overflow用户

发布于 2012-10-11 02:54:36

在@tc的顶部。已经说过,使用以下代码根据ID自动填充您的描述会更容易:

代码语言:javascript
复制
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不需要任何其他附加信息,但其他格式需要(通常是通道数和采样率)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3815231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档