3

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?

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
Ramz
  • 7,116
  • 6
  • 63
  • 88
  • You seem to be mixing up the terms *pitch* and *modulation* - these are two very different things - can you try to be a little clearer about what it is exactly that you are trying to achieve ? – Paul R Mar 19 '12 at 13:01
  • i mean modulation bro please give me a solution – Ramz Mar 19 '12 at 13:02
  • You need to improve your question, otherwise it will most likely get closed - the title says *pitch*, the question itself says *modulation*, but you haven't given any details or clarification as to what you are trying to achieve, e.g. what *kind* of modulation ? – Paul R Mar 19 '12 at 16:01

1 Answers1

5

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

    }
}
Dinesh
  • 6,500
  • 10
  • 42
  • 77
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75