Changing Pitch and Frequency of Recorded Audio

前端 未结 1 407
一向
一向 2021-01-07 01: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

相关标签:
1条回答
  • 2021-01-07 01:30
    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.

    0 讨论(0)
提交回复
热议问题