< prev index next >

src/java.desktop/share/classes/com/sun/media/sound/SoftMidiAudioFileReader.java

Print this page


   1 /*
   2  * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.media.sound;
  27 
  28 import java.io.EOFException;
  29 import java.io.IOException;
  30 import java.io.InputStream;
  31 
  32 import javax.sound.midi.InvalidMidiDataException;
  33 import javax.sound.midi.MetaMessage;
  34 import javax.sound.midi.MidiEvent;
  35 import javax.sound.midi.MidiMessage;
  36 import javax.sound.midi.MidiSystem;
  37 import javax.sound.midi.MidiUnavailableException;
  38 import javax.sound.midi.Receiver;
  39 import javax.sound.midi.Sequence;
  40 import javax.sound.midi.Track;
  41 import javax.sound.sampled.AudioFileFormat;
  42 import javax.sound.sampled.AudioFileFormat.Type;
  43 import javax.sound.sampled.AudioFormat;
  44 import javax.sound.sampled.AudioInputStream;
  45 import javax.sound.sampled.UnsupportedAudioFileException;
  46 
  47 /**
  48  * MIDI File Audio Renderer/Reader.
  49  *
  50  * @author Karl Helgason
  51  */
  52 public final class SoftMidiAudioFileReader extends SunFileReader {
  53 
  54     private static final Type MIDI = new Type("MIDI", "mid");
  55 
  56     private static final AudioFormat format = new AudioFormat(44100, 16, 2,
  57                                                               true, false);
  58 
  59     private static AudioFileFormat getAudioFileFormat(final Sequence seq) {
  60         long totallen = seq.getMicrosecondLength() / 1000000;
  61         long len = (long) (format.getFrameRate() * (totallen + 4));
  62         return new AudioFileFormat(MIDI, format, (int) len);
  63     }
  64 
  65     private AudioInputStream getAudioInputStream(final Sequence seq)
  66             throws InvalidMidiDataException {
  67         AudioSynthesizer synth = (AudioSynthesizer) new SoftSynthesizer();
  68         AudioInputStream stream;
  69         Receiver recv;
  70         try {
  71             stream = synth.openStream(format, null);
  72             recv = synth.getReceiver();
  73         } catch (MidiUnavailableException e) {
  74             throw new InvalidMidiDataException(e.toString());
  75         }
  76         float divtype = seq.getDivisionType();
  77         Track[] tracks = seq.getTracks();
  78         int[] trackspos = new int[tracks.length];
  79         int mpq = 500000;
  80         int seqres = seq.getResolution();
  81         long lasttick = 0;
  82         long curtime = 0;


 123         long totallen = curtime / 1000000;
 124         long len = (long) (stream.getFormat().getFrameRate() * (totallen + 4));
 125         stream = new AudioInputStream(stream, stream.getFormat(), len);
 126         return stream;
 127     }
 128 
 129     @Override
 130     public AudioInputStream getAudioInputStream(final InputStream stream)
 131             throws UnsupportedAudioFileException, IOException {
 132         stream.mark(200);
 133         try {
 134             return getAudioInputStream(MidiSystem.getSequence(stream));
 135         } catch (InvalidMidiDataException | EOFException ignored) {
 136             // stream is unsupported or the header is less than was expected
 137             stream.reset();
 138             throw new UnsupportedAudioFileException();
 139         }
 140     }
 141 
 142     @Override
 143     AudioFileFormat getAudioFileFormatImpl(final InputStream stream)
 144             throws UnsupportedAudioFileException, IOException {
 145         try {
 146             return getAudioFileFormat(MidiSystem.getSequence(stream));
 147         } catch (final InvalidMidiDataException ignored) {
 148             throw new UnsupportedAudioFileException();
 149         }
 150     }
 151 }
   1 /*
   2  * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.media.sound;
  27 
  28 import java.io.EOFException;
  29 import java.io.IOException;
  30 import java.io.InputStream;
  31 
  32 import javax.sound.midi.InvalidMidiDataException;
  33 import javax.sound.midi.MetaMessage;
  34 import javax.sound.midi.MidiEvent;
  35 import javax.sound.midi.MidiMessage;
  36 import javax.sound.midi.MidiSystem;
  37 import javax.sound.midi.MidiUnavailableException;
  38 import javax.sound.midi.Receiver;
  39 import javax.sound.midi.Sequence;
  40 import javax.sound.midi.Track;

  41 import javax.sound.sampled.AudioFileFormat.Type;
  42 import javax.sound.sampled.AudioFormat;
  43 import javax.sound.sampled.AudioInputStream;
  44 import javax.sound.sampled.UnsupportedAudioFileException;
  45 
  46 /**
  47  * MIDI File Audio Renderer/Reader.
  48  *
  49  * @author Karl Helgason
  50  */
  51 public final class SoftMidiAudioFileReader extends SunFileReader {
  52 
  53     private static final Type MIDI = new Type("MIDI", "mid");
  54 
  55     private static final AudioFormat format = new AudioFormat(44100, 16, 2,
  56                                                               true, false);
  57 
  58     private static StandardFileFormat getAudioFileFormat(final Sequence seq) {
  59         long totallen = seq.getMicrosecondLength() / 1000000;
  60         long len = (long) (format.getFrameRate() * (totallen + 4));
  61         return new StandardFileFormat(MIDI, format, len);
  62     }
  63 
  64     private AudioInputStream getAudioInputStream(final Sequence seq)
  65             throws InvalidMidiDataException {
  66         AudioSynthesizer synth = (AudioSynthesizer) new SoftSynthesizer();
  67         AudioInputStream stream;
  68         Receiver recv;
  69         try {
  70             stream = synth.openStream(format, null);
  71             recv = synth.getReceiver();
  72         } catch (MidiUnavailableException e) {
  73             throw new InvalidMidiDataException(e.toString());
  74         }
  75         float divtype = seq.getDivisionType();
  76         Track[] tracks = seq.getTracks();
  77         int[] trackspos = new int[tracks.length];
  78         int mpq = 500000;
  79         int seqres = seq.getResolution();
  80         long lasttick = 0;
  81         long curtime = 0;


 122         long totallen = curtime / 1000000;
 123         long len = (long) (stream.getFormat().getFrameRate() * (totallen + 4));
 124         stream = new AudioInputStream(stream, stream.getFormat(), len);
 125         return stream;
 126     }
 127 
 128     @Override
 129     public AudioInputStream getAudioInputStream(final InputStream stream)
 130             throws UnsupportedAudioFileException, IOException {
 131         stream.mark(200);
 132         try {
 133             return getAudioInputStream(MidiSystem.getSequence(stream));
 134         } catch (InvalidMidiDataException | EOFException ignored) {
 135             // stream is unsupported or the header is less than was expected
 136             stream.reset();
 137             throw new UnsupportedAudioFileException();
 138         }
 139     }
 140 
 141     @Override
 142     StandardFileFormat getAudioFileFormatImpl(final InputStream stream)
 143             throws UnsupportedAudioFileException, IOException {
 144         try {
 145             return getAudioFileFormat(MidiSystem.getSequence(stream));
 146         } catch (final InvalidMidiDataException ignored) {
 147             throw new UnsupportedAudioFileException();
 148         }
 149     }
 150 }
< prev index next >