6

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

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
P i
  • 29,020
  • 36
  • 159
  • 267

1 Answers1

2

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.

lucius
  • 8,665
  • 3
  • 34
  • 41
  • What *are* you asking about then? If it's about hardware, you should specify what hardware, since they're not all the same. – lucius Nov 27 '10 at 21:01
  • Ohmu is asking how to set an arbitrary sample rate. He/she is not asking how to access the hardware sample rate. I have the same problem at present. I want my RemoteIO AudioUnit to work at 22050 to reduce latency issues with what's going on in the software. – Max MacLeod Apr 27 '11 at 16:58
  • Curiously, setting kAudioSessionProperty_PreferredHardwareSampleRate works running on the iPhone itself. It doesn't work in the simulator however. – Max MacLeod Apr 28 '11 at 12:35
  • Downvoted as this doesn't answer the OP's question, and hasn't been updated despite the other comments that point this out. – j b Aug 09 '13 at 15:08