6

Can anyone help me how to convert .mid files into any audio format as .wav or .mp3 format by programmatically.

Community
  • 1
  • 1
sandeep
  • 481
  • 1
  • 6
  • 14
  • did you already tried the mediaplayer class? it can play midi files, maybe you can intercept the output buffer and store it as a wave. – lelloman Feb 18 '13 at 10:16
  • Yes i used it to play midi file but i don't know how to save it? do you have any idea? – sandeep Feb 19 '13 at 05:59
  • could you post the code you wrote to play the midi? – lelloman Feb 19 '13 at 23:41
  • I used media player for it to play.. – sandeep Feb 20 '13 at 04:39
  • yes I know, since I'm interested in this question, having some code to start would help me a lot to help you! – lelloman Feb 20 '13 at 12:52
  • Hi,Thanks for giving response buddy.. i am using media player to play the midi file.Iam just give the midi file to media player and play it by using start() method.And i want the ouput to be as .wav or mp3 etc in sdcard.And the code is as MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.midi_sound); mediaPlayer.prepare(); mediaPlayer.start(); – sandeep Feb 21 '13 at 09:18

1 Answers1

5

I thought it was possible to use an AudioRecorder to record directly from a MediaPlayer, setting the Mediaplayer as audiosource of the recorder,but it seems there's no way to do that. So, I thought two really-not-clean ways:

1- Most of devices have got a 3.5mm jack socket with 3 channels, 2 for stereo output and one for microphone input. What you'd need, is a cable that split the three signals so that you can connect the stereo output to the input, in a sort of short circuit, and record the midi from the microphone input. I used it to pass an audio stream source to the phone, elaborate it and then send it to a third devices. The wire I'm talking about is very similar to RCA connectors with stereo audio + video, I know it sounds mad, but it depends on what you're doing.

2- Assuming that you don't need to record the midi while actually playing it, you can read the midi file and then synthesize the sound yourself. This is really tough, specially when have to deal with sounds of different instruments (strings,drums etc), using samples could reduce the work, maybe.

I know this is not the expected answer but it's better than nothing, if you are so desperate to try one of these method I can provide some sample code and links.

EDIT:

ok, that was mad. I found another way, use Visualizer class. The purpose of visualizer is not to get PCM to record it, but (surprisingly) to visualize the sound wave, so there might be some quality issues. However you can save PCM to wave format, in order to do it, you have to add a header to the raw PCM array. For wave file format take a look here. Here an example, it just shows the byte array got from MediaPlayer in a TextView, but it seems to work...!

android.permission.RECORD_AUDIO
android.permission.MODIFY_AUDIO_SETTINGS

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Play" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pause" />

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TextView
            android:id="@+id/textview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </ScrollView>

MainActivity.java

package com.example.midify;

import java.util.Arrays;

import android.media.MediaPlayer;
import android.media.audiofx.Visualizer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
    MediaPlayer mp;
    TextView tv;
    Visualizer mVisualizer;

    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            tv.setText((String) msg.obj);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mp = MediaPlayer.create(this, R.raw.midi);

        Button btn = (Button) findViewById(R.id.btn);
        Button btn2 = (Button) findViewById(R.id.btn2);
        tv = (TextView) findViewById(R.id.textview);

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (!mp.isPlaying()) {
                    mp.start();
                    init_visualizer();
                }
            }
        });

        btn2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (mp.isPlaying()) {
                    mp.pause();
                    mVisualizer.release();
                }
            }
        });
    }

    @Override
    public void finish() {
        mp.release();
        mVisualizer.release();

        super.finish();
    }

    private void PassData(byte[] data) {
        String txt = Arrays.toString(data);
        Message msg = handler.obtainMessage();
        msg.obj = txt;
        handler.sendMessage(msg);
    }

    public void init_visualizer() {
        mVisualizer = new Visualizer(mp.getAudioSessionId());
        mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);

        Visualizer.OnDataCaptureListener captureListener = new Visualizer.OnDataCaptureListener() {
            @Override
            public void onWaveFormDataCapture(Visualizer visualizer,
                    byte[] bytes, int samplingRate) {
                PassData(bytes);
            }

            @Override
            public void onFftDataCapture(Visualizer visualizer, byte[] bytes,
                    int samplingRate) {

            }
        };

        mVisualizer.setDataCaptureListener(captureListener,
                Visualizer.getMaxCaptureRate(), true, false);

        mVisualizer.setEnabled(true);
        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                mVisualizer.setEnabled(false);
            }
        });
    }
}
Community
  • 1
  • 1
lelloman
  • 13,883
  • 5
  • 63
  • 85
  • Hi,thanks for answering again.We can save the output from the microphone but it is not good to store the sound from mic because firstly it will add some blur sound to it and second is,if we were talking something when we are saving the sound from microphone then our voice will also merge with it..so i am not using mic to save the output. Thanks a lot again .. if u have any suggestion please help.. – sandeep Feb 22 '13 at 04:40
  • Hi,i convert that pcm(byte data) to wav format but the output is not perfect.It is giving blur sound and some music of midi file when we play it.Thanks... – sandeep Feb 27 '13 at 09:53
  • @sandeep uff, this is quite frustrating, if you find a better solution please advice me, I'd really like to know how to solve this – lelloman Feb 27 '13 at 23:47
  • surely i wil let u know once i have done it..thnks for ur cooperation.. – sandeep Mar 01 '13 at 09:36