Reputation: 6835
I've been trying to adjust the pitch of a record piece of audio following with code from:
http://developer.android.com/guide/topics/media/audio-capture.html
My guess is that this adjustment should be done with the MediaRecorder
.
http://developer.android.com/reference/android/media/MediaRecorder.html
However, I am unsure which method to call to change the pitch?
Looking through SO, I found changing the frequency of a soundfile in android but I am confused as to how to integrate a SoundPool
with the elements in the audio capture guide I am following. Is there a more straightforward solution based on the developer guide I am using?
Upvotes: 2
Views: 6247
Reputation: 996
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mSoundPool = new SoundPool(size, AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap<Integer, Integer>();
mSoundPoolMap.put(index, mSoundPool.load(context, R.raw.sound, 1));
mSoundPool.play(id, streamVolume, streamVolume, 1, loop, 1f);
The frequency is the 1f part. If you change it to a value between .5f and 2.0f that should slow down or speed up the sample, which changes the pitch.
Here's some code from one of my apps:
private SoundPool soundpool;
private HashMap<Integer, Integer> soundsMap;
protected void onCreate(Bundle savedInstanceState) {
soundpool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
soundsMap = new HashMap<Integer, Integer>();
soundsMap.put(cowbell1, soundpool.load(this, R.raw.cowbell, 1));
soundsMap.put(cowbell2, soundpool.load(this, R.raw.cowbell1, 1));
soundsMap.put(cowbell3, soundpool.load(this, R.raw.windhh3, 1));
}
public void playSound(int sound, float fSpeed) {
AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = streamVolumeCurrent / streamVolumeMax;
soundpool.play(soundsMap.get(sound), volume, volume, 1, 0, fSpeed);
}
To call a sound I use this:
playSound(cowbell1, 1.0f);
or
playSound(cowbell2, 1.0f);
The rate can be changed by altering the 1.0f value.
If you're still having trouble post your code and I'll have a look at it.
Upvotes: 4