2

I'm trying to set allow my users to set a BPM of a AVAudioPlayer, from what I see the rate property of AVAudioPlayer should be the one I want to alter if I'm changing the frequency of a player. My question is, if I were to present it as a BPM setting, is there an easy way to convert from BPM to rate? For example, .8 rate == 120BPM?

Thanks!

ttoine
  • 192
  • 1
  • 12
awater
  • 33
  • 5

2 Answers2

2

You would have to know the BPM of the track at normal speed. When you have that, you can convert with

newBPM = normalBPM x rate

Say at normal speed the track is 120 BPM

Then at 0.5 speed it will be 120 x 0.5 = 60 BPM

At double speed (rate = 2) it will be 120 x 2.0 = 240 BPM

If you don't know the BPM of a track at normal speed you will need to do some more in-depth audio processing with beat-detection algorithms:

How to detect the BPM of a song in php

If you want to avoid pitch shifting when changing the audio rate, you will also need to check out time stretching algorithms

Using such algorithms is beyond the scope of AVAudioPlayer - you will need to look at lower-level Core Audio functions (Audio Queues / Audio Units)

Community
  • 1
  • 1
foundry
  • 31,615
  • 9
  • 90
  • 125
1

I don't believe there is any relation between BPM and the rate property of the AVAudioPlayer. For example, relevant documentation states:

This property’s default value of 1.0 provides normal playback rate. The available range is from 0.5 for half-speed playback through 2.0 for double-speed playback.

So as far as I can tell, this means that you would need to know the BPM of the track going in. (If you know how to get this great, I personally couldn't tell you) Then, reguardless of what the BPM is, if the rate is 1 playback will continue at the default BPM. If rate is .5 then playback will be half BPM and if rate is 2, then playback will be double BPM.

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281