Java Tutorial/Development/MIDI Sound
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
- 1 Determine the duration of a Midi audio file
- 2 Determining the File Format of a Midi Audio File
- 3 Determining When a Midi Audio Player Has Finished Playing
- 4 Load and play Midi audio
- 5 Loading and Playing Midi Audio
- 6 Play a streaming Midi audio
- 7 Playing Streaming Midi Audio
- 8 Setting the Volume of Playing Midi Audio
Determine the duration of a Midi audio file
import java.io.File;
import java.net.URL;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
public class Main {
public static void main(String[] argv) throws Exception {
Sequence sequence = MidiSystem.getSequence(new File("midiaudiofile"));
sequence = MidiSystem.getSequence(new URL("http://hostname/midiaudiofile"));
// Create a sequencer for the sequence
Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();
sequencer.setSequence(sequence);
double durationInSecs = sequencer.getMicrosecondLength() / 1000000.0;
}
}
Determining the File Format of a Midi Audio File
import java.io.File;
import java.net.URL;
import javax.sound.midi.MidiFileFormat;
import javax.sound.midi.MidiSystem;
public class Main {
public static void main(String[] argv) throws Exception {
// From file
MidiFileFormat fformat = MidiSystem.getMidiFileFormat(new File("midifile"));
// From URL
// fformat = MidiSystem.getMidiFileFormat(new URL("http://hostname/midifile"));
// Get file format
switch (fformat.getType()) {
case 0:
// mid
break;
case 1:
// rmf
break;
}
}
}
Determining When a Midi Audio Player Has Finished Playing
import javax.sound.midi.MetaEventListener;
import javax.sound.midi.MetaMessage;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequencer;
public class Main {
public static void main(String[] argv) throws Exception {
Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();
sequencer.addMetaEventListener(new MetaEventListener() {
public void meta(MetaMessage event) {
if (event.getType() == 47) {
// Sequencer is done playing
}
}
});
}
}
Load and play Midi audio
import java.io.File;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
public class Main {
public static void main(String[] argv) throws Exception {
// From file
Sequence sequence = MidiSystem.getSequence(new File("midiaudiofile"));
// From URL
// sequence = MidiSystem.getSequence(new
// URL("http://hostname/midiaudiofile"));
// Create a sequencer for the sequence
Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();
sequencer.setSequence(sequence);
// Start playing
sequencer.start();
}
}
Loading and Playing Midi Audio
import java.io.File;
import java.net.URL;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
public class Main {
public static void main(String[] argv) throws Exception {
Sequence sequence = MidiSystem.getSequence(new File("midifile"));
// From URL
sequence = MidiSystem.getSequence(new URL("http://hostname/midifile"));
// Create a sequencer for the sequence
Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();
sequencer.setSequence(sequence);
// Start playing
sequencer.start();
}
}
Play a streaming Midi audio
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequencer;
public class Main {
public static void main(String[] argv) throws Exception {
Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();
// From file
InputStream input = new BufferedInputStream(new FileInputStream(new File("midiaudiofile")));
// From URL
input = new BufferedInputStream(new URL("http://hostname/rmffile").openStream());
sequencer.setSequence(input);
// Start playing
sequencer.start();
}
}
Playing Streaming Midi Audio
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequencer;
public class Main {
public static void main(String[] argv) throws Exception {
Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();
// From file
InputStream is = new BufferedInputStream(new FileInputStream(new File(
"midifile")));
// From URL
// is = new BufferedInputStream(new URL("http://hostname/rmffile")
// .openStream());
sequencer.setSequence(is);
// Start playing
sequencer.start();
}
}
Setting the Volume of Playing Midi Audio
import javax.sound.midi.MidiChannel;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequencer;
import javax.sound.midi.Synthesizer;
public class Main {
public static void main(String[] argv) throws Exception {
Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();
if (sequencer instanceof Synthesizer) {
Synthesizer synthesizer = (Synthesizer) sequencer;
MidiChannel[] channels = synthesizer.getChannels();
// gain is a value between 0 and 1 (loudest)
double gain = 0.9D;
for (int i = 0; i < channels.length; i++) {
channels[i].controlChange(7, (int) (gain * 127.0));
}
}
}
}