1

所以基本上我只想用主要是客观的 c 代码来监控 iPhone 中的麦克风拾取的当前 dB 水平。我在谷歌上环顾四周,看起来您需要使用 AudioQueues 进行一些“核心”c 编程才能使其正常工作。我不擅长 C 编码,所以下面的代码是我尝试使用最低限度的 C 编码来获得可以发送回我温暖而熟悉的 Objective-C 领域的结果。问题是,我似乎无法创建 AudioQueue ,我返回的“状态”是 -50,即“未知错误”。

我实际上是在我的 viewDidLoad 中设置音频队列并在其上方声明 ac 回调,并从回调中获取当前的 dB 值并将其发送回目标 c 土地,但回调永远不会被调用,因为 AudioQueue 不是正确创建,我不知道为什么。任何人都能够看到我做错了什么,或者将我指向这个音频内容的目标 c 包装器的方向。干杯。

@interface ViewController ()
{
    AudioQueueRef mQueue;
}

- (void)receivedSoundOfPower:(Float32)power;

@end

@implementation ViewController


void MyInputBufferHandler(  void *                              inUserData,
                                      AudioQueueRef                     inAQ,
                                      AudioQueueBufferRef                   inBuffer,
                                      const AudioTimeStamp *                inStartTime,
                                      UInt32                                inNumPackets,
                                      const AudioStreamPacketDescription*   inPacketDesc)
{
    ViewController *self = (__bridge id)inUserData;

    AudioQueueLevelMeterState meters[1];
    UInt32 dlen = sizeof(meters);
    OSStatus status = AudioQueueGetProperty(inAQ,kAudioQueueProperty_CurrentLevelMeterDB,meters,&dlen);
    if (status == 0)
    {
        Float32 peakPower = meters[0].mPeakPower;
        [self receivedSoundOfPower:peakPower];
    }
}

- (void)receivedSoundOfPower:(Float32)power
{
    NSLog(@"%f", power);
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    OSStatus status =AudioQueueNewInput(kAudioStreamAnyRate,
                                MyInputBufferHandler,
                                (__bridge void *)(self),
                                NULL,
                                NULL,
                                0,
                                &(mQueue));

    NSLog(@"%ld", status);

    // Turn on level metering (iOS 2.0 and later)
    UInt32 on = 1;
    AudioQueueSetProperty(mQueue,kAudioQueueProperty_EnableLevelMetering,&on,sizeof(on));

    // Do any additional setup after loading the view, typically from a nib.
}
4

1 回答 1

6

您不需要使用音频队列来获取数据库值。你可以做更多类似的事情:

- (void)setup {
    // record audio to /dev/null
    NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];

    // some settings
    NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
                              [NSNumber numberWithFloat: 44100.0],                 AVSampleRateKey,
                              [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
                              [NSNumber numberWithInt: 2],                         AVNumberOfChannelsKey,
                              [NSNumber numberWithInt: AVAudioQualityMax],         AVEncoderAudioQualityKey,
                              nil];

    // create a AVAudioRecorder
    NSError *error;
    recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];

    if (recorder) {
        [recorder prepareToRecord];
        recorder.meteringEnabled = YES;
        [recorder record];
        levelTimer = [NSTimer scheduledTimerWithTimeInterval:0.01f target:self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
    }
}

- (void)levelTimerCallback:(NSTimer *)timer {
    [recorder updateMeters];

    // here is the DB!
    float peakDecebels =  [recorder peakPowerForChannel:1];
            float avaeragePower = [recorder averagePowerForChannel:1];
}

此外,如果您想访问音频数据,这个库非常棒:http ://alexbw.github.com/novocaine/

于 2012-10-14T13:14:51.243 回答