I'm writing an accompaniment application that continuously needs to play specific notes (or even chords). I have a thread running that figures out which note I need to play, but I have no idea where to begin regarding the actual playback. I know I can create an audiotrack and write a sine wave to it, but for this project a simple tone won't cut it. So I'm guessing I either need to use MIDI (can android do that?) or to somehow take a sample and change its pitch on the fly, but I don't know if that's even possible.
Asked
Active
Viewed 949 times
1
-
possible duplicate of [Android app for composing music: Beginner](http://stackoverflow.com/questions/2094812/android-app-for-composing-music-beginner) – PA. Dec 20 '11 at 06:40
-
A very quick search has lead me to some promising hits, including the above SO post, or this one stackoverflow.com/questions/4056846/available-music-api – PA. Dec 20 '11 at 06:42
1 Answers
1
All I can say is to check out pitch-shifting (which you seem to have heard of) and soundpool (which would require some recording of your own) and these 2 links:
Audio Playback Rate in Android
Programmatically increase the pitch of an array of audio samples
the second link seems to have more info.
-
Thanks. Alright, so I can change the playback rate by some multiplier, but how do I determine that if I want to play some note? If I have a sample recorded at say C5, would it be as easy as lerping between 0.5 (one octave done) and 1.0, in increments of 1/12 (so one increment is one of 12 notes)? multiplier = 0.5 + ((1.0 - 0.5) * (t/12f)) – Dmiters Dec 21 '11 at 03:53
-
@ThePwnStick Don't "lerp". Musical notes are on a logarithmic scale: each semitone is `2**(1/12)` times the frequency of the one below it. If you don't like the math you could also just search for "notes hertz" and find a table with the actual frequency of each note. – Laurence Gonsalves Dec 23 '11 at 00:12
-
@ThePwnStick Also note that playback rate in Android is somewhat limited, so you'll want to have multiple samples, say one per octave. You then choose the "nearest" sample for the pitch you need, and set the rate to adjust it to the correct pitch. Just speculating here, but if you record an acoustic instrument it _may_ be best to use a single recording and then use a tool like `sox` or Audacity to produce the per-octave samples, otherwise there may be a noticeable discontinuity between octaves. – Laurence Gonsalves Dec 23 '11 at 00:17