cuthbertLab / music21

music21 is a Toolkit for Computational Musicology
https://www.music21.org/
Other
2.09k stars 397 forks source link

How to save file as wav, or convert midi file to wav #522

Closed UncoDong closed 4 years ago

UncoDong commented 4 years ago

Hello there. For a stream s, I use s.write('midi', fp=r'C:\Users\Mypath') to save midi file, but I can't play it any way. So I tried to use Timidity,Wondershare UniConverter. or other software to covert it to wav , but it didn't work.I know there is a free online convert website https://www.zamzar.com/, it works well. But cause I'd like to convert many midi files like this, so a localy convert tool is necessary. The tool writen by code is also ok for me. If who can help me, I will express my gratitude to him/her.

Cause my mother language is not Engilsh, please forgive my poor English...

UncoDong commented 4 years ago

I finally solved this problem just now, I fund java's package javax.sound could do it well, like the code:

import javax.sound.sampled.*;

void Convert(String midipath,String wavpath) { AudioInputStream audioStream = AudioSystem.getAudioInputStream(new File(midipath)); AudioSystem.write(audioStream, AudioFileFormat.Type.WAVE, new File(wavpath)); }

I have already write it as a jar file, anyone whant to use can visit my github