简体   繁体   English

iOS,AVAudioSession和Novocaine:如何动态设置采样率?

[英]IOS, AVAudioSession and Novocaine: how to set the sample rate dynamically?

For pitch detection, I use the Novocaine framework in order to process my data captured by the microphone. 对于音高检测,我使用Novocaine框架来处理麦克风捕获的数据。

Initially - I overwrote the original Novocaine class in order to set my own (reduced) sample rate: 最初-我改写了原始的Novocaine类,以设置自己的(降低的)采样率:

 _inputFormat.mSampleRate = PREFERRED_SAMPLING_RATE;// my own value _outputFormat.mSampleRate = PREFERRED_SAMPLING_RATE; // my own value 

I reduced the sample rate to 44100.0 / 4.0 in order to capture also the low frequencies (s.th. between 20 Hz and 100 Hz). 我将采样率降低到44100.0 / 4.0,以便同时捕获低频(例如20 Hz至100 Hz之间的频率)。 This works very fine! 这很好用!

When I try to get the higher frequencies (s.th. starting from 500 Hz), I get deviations due to the low sample rate. 当我尝试获得更高的频率(从500 Hz开始)时,由于采样率低,我会出现偏差。 Thus I need to increase the sample rate to 44100.0 in order to achieve more precision. 因此,我需要将采样率提高到44100.0,以实现更高的精度。

My question: as Novocaine is a singleton, is there a way to change the sample rate DYNAMICALLY within the output block? 我的问题:由于Novocaine是单例,是否有办法在输出块内动态更改采样率?

As Novocaine relies on AVAudioSession, I've tried in vain to do the following: 由于Novocaine依赖AVAudioSession,所以我尝试了以下尝试:

[audioManager setOutputBlock:^(float *data, UInt32 numFrames, UInt32 numChannels) {

    self->ringBuffer->FetchInterleavedData(data, numFrames, numChannels);
    [[AVAudioSession sharedInstance] setPreferredSampleRate:NEW_SAMPLING_RATE error:nil];
…  }

But this doesn't have no effect on the sample rate of the Novocaine framework. 但这对Novocaine框架的采样率没有影响。

Is there a possibility to change the sample rate without creating a new Audio session? 是否可以在不创建新音频会话的情况下更改采样率? The problem is, that the output block belongs to the current running session. 问题是输出块属于当前正在运行的会话。

When you call setPreferredSampleRate:error: , the system may not respond to the sample rate change immediately (or ever). 当您调用setPreferredSampleRate:error: ,系统可能不会立即(或曾经)响应采样率的变化。 From the docs: 从文档:

Your requested changes might not take effect immediately. 您请求的更改可能不会立即生效。 For example, you can use the setPreferredDataSource:error: method on the built-in microphone port to select the front microphone while a headset is plugged in—this preference does not take effect until the headset is unplugged and the audio route changes. 例如,您可以在插入头戴式耳机时使用内置麦克风端口上的setPreferredDataSource:error:方法来选择前置麦克风-该偏好设置在拔下头戴式耳机且音频路由更改后才会生效。

There's also guidance on changing parameters in Technical Q&A QA1631 AVAudioSession - Requesting Audio Session Preferences : 技术问答QA1631 AVAudioSession-请求音频会话首选项中,还提供了有关更改参数的指南:

In most cases where setting a preferred value causes some sort of audio system reconfiguration with an active audio session, audio data I/O will be stopped and then restarted. 在大多数情况下,设置首选值会导致某些音频系统重新配置为具有活动的音频会话,则音频数据I / O将停止然后重新启动。 Therefore, if an application plans to set multiple preferred values, it is generally advisable to deactivate the session first, set the preferences, reactivate the session and then check the actual values. 因此,如果应用程序计划设置多个首选值,通常建议先停用会话,设置首选项,重新激活会话,然后检查实际值。

And per this post in the Apple Developer Forum, the iPhone 6S and 6S+ only support 48KHz through the built-in speaker. 根据苹果开发者论坛上的这篇文章 ,iPhone 6S和6S +仅通过内置扬声器支持48KHz。 You should never assume that the preferred sample rate you request will be the sample rate used by the hardware. 您永远不应假设您请求的首选采样率将是硬件使用的采样率。


Separately, I'm not sure why you would be seeing 'deviations' at 500Hz with a sample rate of 11025Hz -- you should be able to accurately capture that. 另外,我不确定为什么您会在11025Hz的采样率下看到500Hz的“偏差”-您应该能够准确地捕获到它。 You should also be able to capture 20-100Hz with a sample rate of 44100Hz - you'll just need to increase your buffer size by a factor of 4. 您还应该能够以44100Hz的采样率捕获20-100Hz-您只需要将缓冲区大小增加4倍即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM