829N/A/*
6321N/A * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
829N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
829N/A *
829N/A * This code is free software; you can redistribute it and/or modify it
829N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
829N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
829N/A *
829N/A * This code is distributed in the hope that it will be useful, but WITHOUT
829N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
829N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
829N/A * version 2 for more details (a copy is included in the LICENSE file that
829N/A * accompanied this code).
829N/A *
829N/A * You should have received a copy of the GNU General Public License version
829N/A * 2 along with this work; if not, write to the Free Software Foundation,
829N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
829N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
829N/A */
829N/Apackage com.sun.media.sound;
829N/A
829N/Aimport java.io.File;
829N/Aimport java.io.IOException;
829N/Aimport java.io.InputStream;
829N/Aimport java.net.URL;
829N/A
829N/Aimport javax.sound.midi.InvalidMidiDataException;
829N/Aimport javax.sound.midi.MetaMessage;
829N/Aimport javax.sound.midi.MidiEvent;
829N/Aimport javax.sound.midi.MidiMessage;
829N/Aimport javax.sound.midi.MidiSystem;
829N/Aimport javax.sound.midi.MidiUnavailableException;
829N/Aimport javax.sound.midi.Receiver;
829N/Aimport javax.sound.midi.Sequence;
829N/Aimport javax.sound.midi.Track;
829N/Aimport javax.sound.sampled.AudioFileFormat;
829N/Aimport javax.sound.sampled.AudioFormat;
829N/Aimport javax.sound.sampled.AudioInputStream;
829N/Aimport javax.sound.sampled.UnsupportedAudioFileException;
829N/Aimport javax.sound.sampled.AudioFileFormat.Type;
829N/Aimport javax.sound.sampled.spi.AudioFileReader;
829N/A
829N/A/**
829N/A * MIDI File Audio Renderer/Reader
829N/A *
829N/A * @author Karl Helgason
829N/A */
6321N/Apublic final class SoftMidiAudioFileReader extends AudioFileReader {
829N/A
829N/A public static final Type MIDI = new Type("MIDI", "mid");
829N/A private static AudioFormat format = new AudioFormat(44100, 16, 2, true, false);
829N/A
829N/A public AudioFileFormat getAudioFileFormat(Sequence seq)
829N/A throws UnsupportedAudioFileException, IOException {
829N/A
829N/A long totallen = seq.getMicrosecondLength() / 1000000;
829N/A long len = (long) (format.getFrameRate() * (totallen + 4));
829N/A return new AudioFileFormat(MIDI, format, (int) len);
829N/A }
829N/A
829N/A public AudioInputStream getAudioInputStream(Sequence seq)
829N/A throws UnsupportedAudioFileException, IOException {
829N/A AudioSynthesizer synth = (AudioSynthesizer) new SoftSynthesizer();
829N/A AudioInputStream stream;
829N/A Receiver recv;
829N/A try {
829N/A stream = synth.openStream(format, null);
829N/A recv = synth.getReceiver();
829N/A } catch (MidiUnavailableException e) {
829N/A throw new IOException(e.toString());
829N/A }
829N/A float divtype = seq.getDivisionType();
829N/A Track[] tracks = seq.getTracks();
829N/A int[] trackspos = new int[tracks.length];
829N/A int mpq = 500000;
829N/A int seqres = seq.getResolution();
829N/A long lasttick = 0;
829N/A long curtime = 0;
829N/A while (true) {
829N/A MidiEvent selevent = null;
829N/A int seltrack = -1;
829N/A for (int i = 0; i < tracks.length; i++) {
829N/A int trackpos = trackspos[i];
829N/A Track track = tracks[i];
829N/A if (trackpos < track.size()) {
829N/A MidiEvent event = track.get(trackpos);
829N/A if (selevent == null || event.getTick() < selevent.getTick()) {
829N/A selevent = event;
829N/A seltrack = i;
829N/A }
829N/A }
829N/A }
829N/A if (seltrack == -1)
829N/A break;
829N/A trackspos[seltrack]++;
829N/A long tick = selevent.getTick();
829N/A if (divtype == Sequence.PPQ)
829N/A curtime += ((tick - lasttick) * mpq) / seqres;
829N/A else
829N/A curtime = (long) ((tick * 1000000.0 * divtype) / seqres);
829N/A lasttick = tick;
829N/A MidiMessage msg = selevent.getMessage();
829N/A if (msg instanceof MetaMessage) {
829N/A if (divtype == Sequence.PPQ) {
829N/A if (((MetaMessage) msg).getType() == 0x51) {
829N/A byte[] data = ((MetaMessage) msg).getData();
829N/A mpq = ((data[0] & 0xff) << 16)
829N/A | ((data[1] & 0xff) << 8) | (data[2] & 0xff);
829N/A }
829N/A }
829N/A } else {
829N/A recv.send(msg, curtime);
829N/A }
829N/A }
829N/A
829N/A long totallen = curtime / 1000000;
829N/A long len = (long) (stream.getFormat().getFrameRate() * (totallen + 4));
829N/A stream = new AudioInputStream(stream, stream.getFormat(), len);
829N/A return stream;
829N/A }
829N/A
829N/A public AudioInputStream getAudioInputStream(InputStream inputstream)
829N/A throws UnsupportedAudioFileException, IOException {
829N/A
829N/A inputstream.mark(200);
829N/A Sequence seq;
829N/A try {
829N/A seq = MidiSystem.getSequence(inputstream);
829N/A } catch (InvalidMidiDataException e) {
829N/A inputstream.reset();
829N/A throw new UnsupportedAudioFileException();
829N/A } catch (IOException e) {
829N/A inputstream.reset();
829N/A throw new UnsupportedAudioFileException();
829N/A }
829N/A return getAudioInputStream(seq);
829N/A }
829N/A
829N/A public AudioFileFormat getAudioFileFormat(URL url)
829N/A throws UnsupportedAudioFileException, IOException {
829N/A Sequence seq;
829N/A try {
829N/A seq = MidiSystem.getSequence(url);
829N/A } catch (InvalidMidiDataException e) {
829N/A throw new UnsupportedAudioFileException();
829N/A } catch (IOException e) {
829N/A throw new UnsupportedAudioFileException();
829N/A }
829N/A return getAudioFileFormat(seq);
829N/A }
829N/A
829N/A public AudioFileFormat getAudioFileFormat(File file)
829N/A throws UnsupportedAudioFileException, IOException {
829N/A Sequence seq;
829N/A try {
829N/A seq = MidiSystem.getSequence(file);
829N/A } catch (InvalidMidiDataException e) {
829N/A throw new UnsupportedAudioFileException();
829N/A } catch (IOException e) {
829N/A throw new UnsupportedAudioFileException();
829N/A }
829N/A return getAudioFileFormat(seq);
829N/A }
829N/A
829N/A public AudioInputStream getAudioInputStream(URL url)
829N/A throws UnsupportedAudioFileException, IOException {
829N/A Sequence seq;
829N/A try {
829N/A seq = MidiSystem.getSequence(url);
829N/A } catch (InvalidMidiDataException e) {
829N/A throw new UnsupportedAudioFileException();
829N/A } catch (IOException e) {
829N/A throw new UnsupportedAudioFileException();
829N/A }
829N/A return getAudioInputStream(seq);
829N/A }
829N/A
829N/A public AudioInputStream getAudioInputStream(File file)
829N/A throws UnsupportedAudioFileException, IOException {
829N/A if (!file.getName().toLowerCase().endsWith(".mid"))
829N/A throw new UnsupportedAudioFileException();
829N/A Sequence seq;
829N/A try {
829N/A seq = MidiSystem.getSequence(file);
829N/A } catch (InvalidMidiDataException e) {
829N/A throw new UnsupportedAudioFileException();
829N/A } catch (IOException e) {
829N/A throw new UnsupportedAudioFileException();
829N/A }
829N/A return getAudioInputStream(seq);
829N/A }
829N/A
829N/A public AudioFileFormat getAudioFileFormat(InputStream inputstream)
829N/A throws UnsupportedAudioFileException, IOException {
829N/A
829N/A inputstream.mark(200);
829N/A Sequence seq;
829N/A try {
829N/A seq = MidiSystem.getSequence(inputstream);
829N/A } catch (InvalidMidiDataException e) {
829N/A inputstream.reset();
829N/A throw new UnsupportedAudioFileException();
829N/A } catch (IOException e) {
829N/A inputstream.reset();
829N/A throw new UnsupportedAudioFileException();
829N/A }
829N/A return getAudioFileFormat(seq);
829N/A }
829N/A}