Changing Pitch and Frequency of Recorded Audio

你。 提交于 2019-11-30 19:20:05

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/18992943/changing-pitch-and-frequency-of-recorded-audio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!