Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert .mid file to any audio format as .wav or .mp3 file in android? [closed]

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

like image 490
sandeep Avatar asked Feb 18 '13 10:02

sandeep


People also ask

Can a .MID file be converted to MP3?

How to convert a MIDI to a MP3 file? Choose the MIDI file that you want to convert. Select MP3 as the the format you want to convert your MIDI file to. Click "Convert" to convert your MIDI file.

How do you change audio format on Android?

Click the drop-down menu button next to “Format” and then select the format that you want for the music file. Double-click on the hyperlink for the “Output path” and select the file path where you want to save the music file. Click the “Start conversion” button.


1 Answers

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);
            }
        });
    }
}
like image 195
lelloman Avatar answered Oct 02 '22 15:10

lelloman