监控可可触摸中的当前dB级别(iPhone)

时间:2012-10-14 13:02:21

标签: objective-c cocoa-touch

所以基本上我只想监听iPhone中麦克风(s?)拾取的当前dB级别,主要是客观的c代码。我在谷歌上环顾四周,看起来你需要做一些"铁杆" c使用AudioQueues进行编程以使其正常工作。我对C编码并不擅长,所以下面的代码是我尝试使用最低限度的C编码来获得一个结果,我可以将其发送回我温暖熟悉的Objective-c土地。问题是,我似乎无法创建AudioQueue,' status'我回来的是-50,这是'未知错误'。

我基本上在我的viewDidLoad中设置音频队列并在其上面声明交流回调,并且在回调中我抓住当前的dB值并将其发送回目标c地但是回调永远不会被调用,因为AudioQueue没有正确创建,我不知道为什么。任何人都能看到我做错的事情,或者指向我这个音频内容的客观包装器的方向。欢呼声。

@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.
}

1 个答案:

答案 0 :(得分: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/