Record sound and change its pitch when playing in android?

I am making a project to record sound and play it in a different modulation. I've searched all over the net but I couldn't find a solution. I went through this example but it didn't give a solution. Can anyone suggest an idea or sample code for modulating an audio file in Android?

+3


source to share


1 answer


I just found the answer to both of them after a lot of trial and error ....

I am sending you working code for both.

Microphone sound recording

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();
    }
}

      

Change the pitch and play it.



After you figured everything out, I decided to go with SoundPool to change the Pitch. A playback speed of 2.0 makes the sound play at twice its original frequency, and a playback speed of 0.5 makes it play at half the original frequency. The playback speed range is 0.5 to 2.0. But it worked at frequencies below and above 0.5 and 2.0.

I am posting my working code,

but as its simple for demo purpose, here you need to manually change the "play speed" every time you install the app, for example: "sp.play (explosion, 1,1,0,0,1,5f)", here " 1.5f "is the playback speed. You can easily create an EditView or something similar to set the playback speed value at runtime.

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

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);
         }

    }
}

      

+5


source







All Articles