📄 midisystem.java
字号:
MidiFileFormat sb = sr.getMidiFileFormat(stream); if (sb != null) return sb; } throw new InvalidMidiDataException("Can't read MidiFileFormat from stream"); } /** * Read a MidiFileFormat object from the given url. * * @param url the url from which to read the MidiFileFormat * @return the MidiFileFormat object * @throws InvalidMidiDataException if we were unable to read the MidiFileFormat * @throws IOException if an I/O error happened while reading */ public static MidiFileFormat getMidiFileFormat(URL url) throws InvalidMidiDataException, IOException { Iterator readers = ServiceFactory.lookupProviders(MidiFileReader.class); while (readers.hasNext()) { MidiFileReader sr = (MidiFileReader) readers.next(); MidiFileFormat sb = sr.getMidiFileFormat(url); if (sb != null) return sb; } throw new InvalidMidiDataException("Cannot read from url " + url); } /** * Read a MidiFileFormat object from the given file. * * @param file the file from which to read the MidiFileFormat * @return the MidiFileFormat object * @throws InvalidMidiDataException if we were unable to read the MidiFileFormat * @throws IOException if an I/O error happened while reading */ public static MidiFileFormat getMidiFileFormat(File file) throws InvalidMidiDataException, IOException { Iterator readers = ServiceFactory.lookupProviders(MidiFileReader.class); while (readers.hasNext()) { MidiFileReader sr = (MidiFileReader) readers.next(); MidiFileFormat sb = sr.getMidiFileFormat(file); if (sb != null) return sb; } throw new InvalidMidiDataException("Can't read MidiFileFormat from file " + file); } /** * Read a Sequence object from the given stream. * * @param stream the stream from which to read the Sequence * @return the Sequence object * @throws InvalidMidiDataException if we were unable to read the Sequence * @throws IOException if an I/O error happened while reading */ public static Sequence getSequence(InputStream stream) throws InvalidMidiDataException, IOException { Iterator readers = ServiceFactory.lookupProviders(MidiFileReader.class); while (readers.hasNext()) { MidiFileReader sr = (MidiFileReader) readers.next(); Sequence sq = sr.getSequence(stream); if (sq != null) return sq; } throw new InvalidMidiDataException("Can't read Sequence from stream"); } /** * Read a Sequence object from the given url. * * @param url the url from which to read the Sequence * @return the Sequence object * @throws InvalidMidiDataException if we were unable to read the Sequence * @throws IOException if an I/O error happened while reading */ public static Sequence getSequence(URL url) throws InvalidMidiDataException, IOException { Iterator readers = ServiceFactory.lookupProviders(MidiFileReader.class); while (readers.hasNext()) { MidiFileReader sr = (MidiFileReader) readers.next(); Sequence sq = sr.getSequence(url); if (sq != null) return sq; } throw new InvalidMidiDataException("Cannot read from url " + url); } /** * Read a Sequence object from the given file. * * @param file the file from which to read the Sequence * @return the Sequence object * @throws InvalidMidiDataException if we were unable to read the Sequence * @throws IOException if an I/O error happened while reading */ public static Sequence getSequence(File file) throws InvalidMidiDataException, IOException { Iterator readers = ServiceFactory.lookupProviders(MidiFileReader.class); while (readers.hasNext()) { MidiFileReader sr = (MidiFileReader) readers.next(); Sequence sq = sr.getSequence(file); if (sq != null) return sq; } throw new InvalidMidiDataException("Can't read Sequence from file " + file); } /** * Return an array of supported MIDI file types on this system. * * @return the array of supported MIDI file types */ public static int[] getMidiFileTypes() { // We only support a max of 3 MIDI file types. boolean supported[] = new boolean[3]; // The number of supported formats. int count = 0; Iterator writers = ServiceFactory.lookupProviders(MidiFileWriter.class); while (writers.hasNext()) { MidiFileWriter fw = (MidiFileWriter) writers.next(); int types[] = fw.getMidiFileTypes(); for (int i = types.length; i > 0;) { int type = types[--i]; if (supported[type] == false) { count++; supported[type] = true; } } } int result[] = new int[count]; for (int i = supported.length; i > 0;) { if (supported[--i]) result[--count] = i; } return result; } /** * Return true if the system supports writing files of type fileType. * * @param fileType the MIDI file type we want to write * @return true if we can write fileType files, false otherwise */ public static boolean isFileTypeSupported(int fileType) { Iterator writers = ServiceFactory.lookupProviders(MidiFileWriter.class); while (writers.hasNext()) { MidiFileWriter fw = (MidiFileWriter) writers.next(); if (fw.isFileTypeSupported(fileType)) return true; } return false; } /** * Return an array of supported MIDI file types on this system * for the given sequnce. * * @param sequence the sequnce to write * @return the array of supported MIDI file types */ public static int[] getMidiFileTypes(Sequence sequence) { // We only support a max of 3 MIDI file types. boolean supported[] = new boolean[3]; // The number of supported formats. int count = 0; Iterator writers = ServiceFactory.lookupProviders(MidiFileWriter.class); while (writers.hasNext()) { MidiFileWriter fw = (MidiFileWriter) writers.next(); int types[] = fw.getMidiFileTypes(sequence); for (int i = types.length; i > 0;) { int type = types[--i]; if (supported[type] == false) { count++; supported[type] = true; } } } int result[] = new int[count]; for (int i = supported.length; i > 0;) { if (supported[--i]) result[--count] = i; } return result; } /** * Return true if the system supports writing files of type fileType * for the given sequence. * * @param fileType the MIDI file type we want to write * @param sequence the Sequence we want to write * @return true if we can write fileType files for sequence, false otherwise */ public static boolean isFileTypeSupported(int fileType, Sequence sequence) { Iterator writers = ServiceFactory.lookupProviders(MidiFileWriter.class); while (writers.hasNext()) { MidiFileWriter fw = (MidiFileWriter) writers.next(); if (fw.isFileTypeSupported(fileType, sequence)) return true; } return false; } /** * Write a sequence to an output stream using a specific MIDI file format. * * @param in the sequence to write * @param fileType the MIDI file format to use * @param out the output stream to write to * @return the number of bytes written * @throws IOException if an I/O exception happens * @throws IllegalArgumentException if fileType is not supported for in */ public static int write(Sequence in, int fileType, OutputStream out) throws IOException { Iterator writers = ServiceFactory.lookupProviders(MidiFileWriter.class); while (writers.hasNext()) { MidiFileWriter fw = (MidiFileWriter) writers.next(); if (fw.isFileTypeSupported(fileType, in)) return fw.write(in, fileType, out); } throw new IllegalArgumentException("File type " + fileType + " is not supported"); } /** * Write a sequence to a file using a specific MIDI file format. * * @param in the sequence to write * @param fileType the MIDI file format to use * @param out the file to write to * @return the number of bytes written * @throws IOException if an I/O exception happens * @throws IllegalArgumentException if fileType is not supported for in */ public static int write(Sequence in, int fileType, File out) throws IOException { Iterator writers = ServiceFactory.lookupProviders(MidiFileWriter.class); while (writers.hasNext()) { MidiFileWriter fw = (MidiFileWriter) writers.next(); if (fw.isFileTypeSupported(fileType, in)) return fw.write(in, fileType, out); } throw new IllegalArgumentException("File type " + fileType + " is not supported"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -