12

I have got one midi file and i want to play that file on web page, currently i am using midi.js player for playing but it is not working on mobile browsers.

Please guide me how to play that file or else how can i play midi or convert it into mp3

Here is my code

  $data = fopen ($midi, 'rb');
 $size= filesize($midi);
 $contents = fread ($data, $size);
 fclose($data);
 $encoded= base64_encode($contents);

 $encode = "'data:audio/midi;base64,".$encoded."=='";

and finally passing base64 value to midi.js

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
chitranjna
  • 131
  • 1
  • 6

7 Answers7

15

Guessing you're on a linux machine... The simplest way is to play it with timidity and pipe the output ffmpeg:

timidity song.mid -Ow -o - | ffmpeg -i - -acodec libmp3lame -ab 64k song.mp3
phreaknerd
  • 161
  • 4
  • You asked for mp3-conversion right? You have to convert that locally to mp3 if you don't have root/admin-access to the server. Alternativley you could do it like suggested [here](http://stackoverflow.com/questions/7677366/about-embed-midi-files-on-a-webpage) – phreaknerd Apr 30 '13 at 09:51
  • i have used this code for playing midi but not working – chitranjna Apr 30 '13 at 10:09
  • This is working on the server, as long as you have timidity, ffmpeg plus codecs/lame installed. You can create a bash (or php) script that run this conversion whenever a midi-file is uploaded to the server. – phreaknerd Apr 30 '13 at 12:15
3

If you're using Linux, use avconv instead of ffmpeg because ffmpeg is being deprecated:

timidity input.midi -Ow -o - | avconv -i - -acodec libmp3lame -b 64k output.mp3

Or lame:

timidity -Ow -o - input.midi | lame - output.mp3

Edit

FFmpeg was never "deprecated"

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Vinicius José Latorre
  • 2,661
  • 1
  • 13
  • 6
  • 1
    I think `ffmpeg` was undeprecated. – Ciro Santilli OurBigBook.com Nov 23 '20 at 12:51
  • 1
    @CiroSantilliOurBigBook.com a [discussion of this on Hacker News](https://news.ycombinator.com/item?id=9948257) ([archive](https://web.archive.org/web/20230616155210/https://news.ycombinator.com/item?id=9948257)) points to certain Linux distros and forks stating that `ffmpeg` was deprecated. The deprecation was never true... – Mr. Polywhirl Jun 16 '23 at 15:48
  • Although `avconv` and `lame` work, `ffmpeg` is still viable. – Mr. Polywhirl Jun 16 '23 at 15:50
2

FluidSynth + FFmpeg

An alternative to timidity:

sudo apt install fluidsynth ffmpeg
fluidsynth -a alsa -T raw -F - /usr/share/sounds/sf2/FluidR3_GM.sf2 MIDI_sample.mid |
  ffmpeg -f s32le -i - MIDI_sample.mp3

Tested on Ubuntu 20.04, FluidSynth 2.1.1-2, and this MIDI file: https://en.wikipedia.org/wiki/File:MIDI_sample.mid

Related: https://softwarerecs.stackexchange.com/questions/10915/automatically-turn-midi-files-into-wav-or-mp3/76955#76955

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
1

I ran into a similar issue with MIDI.js not working in a mobile browser. It turned out that I had included the ogg files but was missing the mp3s (for some reason the MIDI.js only provides ogg versions of synth_drum).

Instrument files for all 128 General MIDI sounds can be found at https://github.com/gleitz/midi-js-soundfonts.

gleitz
  • 595
  • 4
  • 12
1

First install the Timidity package, In Ubuntu just run:

sudo apt-get install -y timidity

Then run this command:

timidity input.mid -Ow -o out.mp3

You can change the format to WAV or other formats too.

MohKoma
  • 964
  • 9
  • 9
0

This worked for me:

timidity my_midi_file.mid -Ow -o - | lame - -b 64 my_converted_midi.mp3

running on ubuntu 18.04

BabaNew
  • 884
  • 1
  • 13
  • 27
-3

Audacity is an open-source audio converter.

Frank
  • 81
  • 4
  • Audacity doesn't handle MIDI files. – cleong Apr 30 '13 at 09:04
  • i am unable to find any code on audacity...can you help me out – chitranjna Apr 30 '13 at 09:45
  • You would need to import the MIDI and then export it as an mp3 format. You stated you could get away with using an mp3 instead of the MIDI. [Here](http://www.wikihow.com/Make-an-MP3-or-WAV-out-of-a-MIDI-Using-Audacity) is an article that walks you through it step by step. – Frank Apr 30 '13 at 09:52
  • but using audacity i have to convert it manually but i want it to be done dynamically on server side as they do http://solmire.com/ – chitranjna Apr 30 '13 at 10:01
  • I would suggest you upload the midi and the mp3, then use PHP to determine the User Agent or device type with `get_browser()` or `$_SERVER['HTTP_USER_AGENT']`... serve up the proper file based upon compatibility. – Frank Apr 30 '13 at 17:37
  • If you want it done on the fly it will use more processor of the server, and cause the page to be slower than determining where to direct the user. – Frank Apr 30 '13 at 23:59