001    /* MidiFileWriter.java -- MIDI file writing services
002       Copyright (C) 2005 Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package javax.sound.midi.spi;
040    
041    import java.io.File;
042    import java.io.IOException;
043    import java.io.OutputStream;
044    
045    import javax.sound.midi.Sequence;
046    
047    /**
048     * MidiFileWriter provides MIDI file writing services.
049     *
050     * There are three types of Standard MIDI File (SMF) formats,
051     * represented by integers 0, 1, and 2.
052     *
053     * Type 0 files contain a single track and represents a single song
054     * performance.
055     * Type 1 may contain multiple tracks for a single song performance.
056     * Type 2 may contain multiple tracks, each representing a
057     * separate song performance.
058     *
059     * See http://en.wikipedia.org/wiki/MIDI#MIDI_file_formats for more
060     * information.
061     *
062     * @author Anthony Green (green@redhat.com)
063     * @since 1.3
064     *
065     */
066    public abstract class MidiFileWriter
067    {
068      /**
069       * Return the MIDI file types supported by this writer.
070       *
071       * @return the MIDI file types, or an empty array
072       */
073      public abstract int[] getMidiFileTypes();
074    
075      /**
076       * Return the MIDI file types supported by this writer for the
077       * given sequence.
078       *
079       * @param sequence the sequence we'd like to write
080       * @return the MIDI file types, or an empty array
081       */
082      public abstract int[] getMidiFileTypes(Sequence sequence);
083    
084      /**
085       * Returns true if this writer supports the given file type.
086       *
087       * @param fileType the file type we're asking about
088       * @return true if this writer supports fileType, false otherwise
089       */
090      public boolean isFileTypeSupported(int fileType)
091      {
092        int types[] = getMidiFileTypes();
093        for (int i = types.length; i > 0;)
094        {
095          if (types[--i] == fileType)
096            return true;
097        }
098        return false;
099      }
100    
101      /**
102       * Returns true if this writer supports the given file type for the
103       * given sequence.
104       *
105       * @param fileType the file type we're asking about
106       * @param sequence the sequence we'd like to write
107       * @return true if this writer supports fileType, false otherwise
108       */
109      public boolean isFileTypeSupported(int fileType, Sequence sequence)
110      {
111        int types[] = getMidiFileTypes(sequence);
112        for (int i = types.length; i > 0;)
113        {
114          if (types[--i] == fileType)
115            return true;
116        }
117        return false;
118      }
119    
120      /**
121       * Write a sequence to a stream using the specified MIDI file type.
122       *
123       * @param in the sequence to write
124       * @param fileType the MIDI file type to use
125       * @param out the output stream to write to
126       * @return the number of byte written
127       * @throws IOException if an I/O exception happens
128       */
129      public abstract int write(Sequence in, int fileType, OutputStream out)
130        throws IOException;
131    
132      /**
133       * Write a sequence to a file using the specified MIDI file type.
134       *
135       * @param in the sequence to write
136       * @param fileType the MIDI file type to use
137       * @param out the file to write to
138       * @return the number of byte written
139       * @throws IOException if an I/O exception happens
140       */
141      public abstract int write(Sequence in, int fileType, File out)
142        throws IOException;
143    }