Miss
Miss

Reputation: 41

How to slow down and stretch audio in Android?

I have figured out that MediaPlayer class doesn't support changing of playback speed. I tried Soundpool, it works, but when I slow it down to 0.5f the speed, the pitch becomes weird (low pitch). I want to change the speed, but keep the same audio characteristic so if I slow it down, it stretches the audio but it doesn't make the pitch lower. What is the best day to do this? Thank you in advance for any help!

Upvotes: 4

Views: 5359

Answers (2)

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

This may be of some use. After trying everything out, i decided to go with SoundPool to change the Pitch, A playback rate of 2.0 causes the sound to play at a freq double its original, and a playback rate of 0.5 causes it to play at half its original frequency. The playback rate range is 0.5 to 2.0. But it did work with freq lower and higher than 0.5 and 2.0.

I am posting my working code,

but as its just for demo purpose, Here you need to Manually change the "playback rate", everytime you install the application for eg: "sp.play(explosion, 1,1,0,0,1.5f)" here "1.5f" is the playback rate. One can easily create an EditView,or something similar to set the value of playback rate at run time.

In this app, you just need to tap on the app's screen to play the music at the set playback rate.

import java.io.IOException;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SoundPoolActivity extends Activity implements OnClickListener {

    SoundPool sp;
    int explosion = 0;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       View v = new View(this);
       v.setOnClickListener(this);
       setContentView(v);
       sp = new SoundPool(1,AudioManager.STREAM_MUSIC,0);
       //explosion = sp.load(this, R.raw.hh,0);
       explosion = sp.load("/sdcard/hh.m4a",0);


    }


    public void onClick(View v){
         if (explosion!=0){

             sp.play(explosion, 1,1,0,0,2.3f);
         }

    }
}

Upvotes: 2

Pindatjuh
Pindatjuh

Reputation: 10526

Check out this website: http://www.pitchtech.ch/

"The PitchTech research project aims in providing high quality audio transformations implemented in Java." It hosts numerous research articles, with enough information for you to build your own, or use one of their implemented audio transformations.

I've found it using this Google search: Audio transformations Stretch.

Upvotes: 1

Related Questions