Convert .mid file to any audio format in .wav or .mp3 format in android?

Can someone help me convert .mid files to any audio format in .wav or .mp3 format programmatically.

+6
source share
1 answer

I thought you could use AudioRecorder to record directly from MediaPlayer by setting Mediaplayer as the audio source of the recorder, but it seems like no way to do that. So, I thought that there are two really not clean ways:

1- Most devices have a 3.5 mm jack with 3 channels, 2 for stereo output and one for microphone input. What you need is a cable that separates the three signals so that you can connect the stereo output to the input as a short circuit and record MIDI from the microphone input. I used it to transfer the source of the audio stream to the phone, develop it, and then send it to third devices. The wire I'm talking about is very similar to RCA connectors with stereo audio + video, I know that it sounds crazy, but it depends on what you do.

2- Assuming you don’t need to record midi while playing it, you can read the midi file and then synthesize the sound yourself. This is very difficult, especially when you have to deal with the sounds of different instruments (strings, drums, etc.). Using samples may possibly reduce the work.

I know this is not an expected answer, but it is better than nothing, if you are so desperate to try one of these methods, I can provide an example of code and links.

EDIT:

OK, that was crazy. I found another way: Visualizer class. The goal of the visualizer is not to get PCM for recording, but (surprisingly) to visualize the sound wave, so there may be some quality problems.However, you can save PCM in the wave format, to do this, you need to add a header to the original PCM array. For the wave file format, see here . Here is an example: it just shows an array of bytes received 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); } }); } } 
+5
source

All Articles