Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Audio Units: setting arbitrary sample rate

Can I set any sample rate I want? What are the restrictions?

How about the hardware sample rate? And once that is set, what is the restriction on the internal sample rates passed between units?

I'm guessing that the actual hardware rate may have to be some bit shift of 44.1KHz, and any internal sample rates must be a downward bit shift of this original value (e.g. 22.1KHz, 11.05KHz). Is this close?

As far as I understand,
1. I set the hardware sample rate from the Audio Session.
2. The system will set a sample rate as close as it is able to the sample rate I specified.
3. I then query the audio session for the same property I set, which will give me the actual sample rate it is using

At the level of audio units, specifically the RemoteIO unit, documentation states that the two points at which the unit connects to the hardware (ie the input scope of the microphone (input) bus and the output scope of the speaker (output) bus), the sample rate may be retrieved but not set.

However, I attempt to access this value while constructing the remote I/O unit, and it returns zero. I guess maybe I need to start the unit before I can get meaningful data from its connections (the act of starting it probably creates the connections). So the solution here appears to be to get the sample rate from the audio session and use that, as per the above.

NEED TAG: Audio-Unit

like image 875
P i Avatar asked Nov 05 '22 06:11

P i


1 Answers

I'm assuming you're on iOS since you mention AudioSessions. So you'll want to:

  1. Check for audio input hardware. AudioSessionGetProperty (kAudioSessionProperty_AudioInputAvailable...)

  2. Set audio session to "play & record" mode. AudioSessionSetProperty (kAudioSessionProperty_AudioCategory...) with kAudioSessionCategory_PlayAndRecord

  3. Activate the session. AudioSessionSetActive()

  4. Get the preferred sample rate. AudioSessionGetProperty (kAudioSessionProperty_CurrentHardwareSampleRate)

Then you can set up your audio processing chain with the correct sample rate.

As for playing back audio, you can use any sample rate and the API should convert it to the hardware's output sample rate. Obviously if you use a very high sample rate, it'l consume a lot of memory and CPU time.

like image 185
lucius Avatar answered Nov 09 '22 11:11

lucius