Ramz
Ramz

Reputation: 7164

Record a sound and change its pitch when played in android?

I am doing a project to record sound and play it in a different modulation. I have searched the whole web, but I could not find a solution. I have gone through this example but it did not give a solution. Can anyone suggest an idea or sample code to modulate a sound file in Android?

Upvotes: 3

Views: 7257

Answers (1)

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

I just found the answer to both of them after lots of trial and errors....

I am posting you the working code for both of them.

Recording the Audio from Mic.

import java.io.File;

 import java.io.IOException;

import android.app.Activity;

import android.content.ContentResolver;

import android.content.ContentValues;

import android.content.Intent;

import android.media.MediaRecorder;

import android.net.Uri;

import android.os.Bundle;

import android.os.Environment;

import android.provider.MediaStore;

import android.util.Log;

import android.view.View;

import android.widget.Toast;

public class SoundRecordingActivity extends Activity {

    MediaRecorder recorder;
    File audiofile = null;
    private static final String TAG = "SoundRecordingActivity";
    private View startButton;
    private View stopButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        startButton = findViewById(R.id.start);
        stopButton = findViewById(R.id.stop);
    }

    public void startRecording(View view) throws IOException {

        startButton.setEnabled(false);
        stopButton.setEnabled(true);

        File sampleDir = Environment.getExternalStorageDirectory();
        try {
            audiofile = File.createTempFile("sound", ".m4a", sampleDir);
        } catch (IOException e) {
            Log.e(TAG, "sdcard access error");
            return;
        }
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        recorder.setOutputFile(audiofile.getAbsolutePath());
        recorder.prepare();
        recorder.start();
    }

    public void stopRecording(View view) {
        startButton.setEnabled(true);
        stopButton.setEnabled(false);
        recorder.stop();
        recorder.release();
        addRecordingToMediaLibrary();
    }

    protected void addRecordingToMediaLibrary() {
        ContentValues values = new ContentValues(4);
        long current = System.currentTimeMillis();
        values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName());
        values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
        values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
        values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());
        ContentResolver contentResolver = getContentResolver();

        Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Uri newUri = contentResolver.insert(base, values);

        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
        Toast.makeText(this, "Added File " + newUri, Toast.LENGTH_LONG).show();
    }
}

Changing the Pitch and Playing it .

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 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: 5

Related Questions