public Sequence getSequence(InputStream stream) throws InvalidMidiDataException, IOException { SMFParser smfParser = new SMFParser(); MidiFileFormat format = getMidiFileFormatFromStream(stream, MidiFileFormat.UNKNOWN_LENGTH, smfParser); // must be MIDI Type 0 or Type 1 if ((format.getType() != 0) && (format.getType() != 1)) { throw new InvalidMidiDataException("Invalid or unsupported file type: " + format.getType()); } // construct the sequence object Sequence sequence = new Sequence(format.getDivisionType(), format.getResolution()); // for each track, go to the beginning and read the track events for (int i = 0; i < smfParser.tracks; i++) { if (smfParser.nextTrack()) { smfParser.readTrack(sequence.createTrack()); } else { break; } } return sequence; }
/** * Obtains the file types that this provider can write from the * sequence specified. * @param sequence the sequence for which midi file type support * is queried * @return array of file types. If no file types are supported, * returns an array of length 0. */ public int[] getMidiFileTypes(Sequence sequence){ int typesArray[]; Track tracks[] = sequence.getTracks(); if( tracks.length==1 ) { typesArray = new int[2]; typesArray[0] = MIDI_TYPE_0; typesArray[1] = MIDI_TYPE_1; } else { typesArray = new int[1]; typesArray[0] = MIDI_TYPE_1; } return typesArray; }
public int write(Sequence in, int type, OutputStream out) throws IOException { byte [] buffer = null; int bytesRead = 0; long bytesWritten = 0; if( !isFileTypeSupported(type,in) ) { throw new IllegalArgumentException("Could not write MIDI file"); } // First get the fileStream from this sequence InputStream fileStream = getFileStream(type,in); if (fileStream == null) { throw new IllegalArgumentException("Could not write MIDI file"); } buffer = new byte[bufferSize]; while( (bytesRead = fileStream.read( buffer )) >= 0 ) { out.write( buffer, 0, (int)bytesRead ); bytesWritten += bytesRead; } // Done....return bytesWritten return (int) bytesWritten; }
@Override public Sequence getSequence(InputStream stream) throws InvalidMidiDataException, IOException { SMFParser smfParser = new SMFParser(); MidiFileFormat format = getMidiFileFormatFromStream(stream, MidiFileFormat.UNKNOWN_LENGTH, smfParser); // must be MIDI Type 0 or Type 1 if ((format.getType() != 0) && (format.getType() != 1)) { throw new InvalidMidiDataException("Invalid or unsupported file type: " + format.getType()); } // construct the sequence object Sequence sequence = new Sequence(format.getDivisionType(), format.getResolution()); // for each track, go to the beginning and read the track events for (int i = 0; i < smfParser.tracks; i++) { if (smfParser.nextTrack()) { smfParser.readTrack(sequence.createTrack()); } else { break; } } return sequence; }
/** * Obtains the file types that this provider can write from the * sequence specified. * @param sequence the sequence for which midi file type support * is queried * @return array of file types. If no file types are supported, * returns an array of length 0. */ @Override public int[] getMidiFileTypes(Sequence sequence){ int typesArray[]; Track tracks[] = sequence.getTracks(); if( tracks.length==1 ) { typesArray = new int[2]; typesArray[0] = MIDI_TYPE_0; typesArray[1] = MIDI_TYPE_1; } else { typesArray = new int[1]; typesArray[0] = MIDI_TYPE_1; } return typesArray; }
@Override public int write(Sequence in, int type, OutputStream out) throws IOException { Objects.requireNonNull(out); if (!isFileTypeSupported(type, in)) { throw new IllegalArgumentException("Could not write MIDI file"); } byte [] buffer = null; int bytesRead = 0; long bytesWritten = 0; // First get the fileStream from this sequence InputStream fileStream = getFileStream(type,in); if (fileStream == null) { throw new IllegalArgumentException("Could not write MIDI file"); } buffer = new byte[bufferSize]; while( (bytesRead = fileStream.read( buffer )) >= 0 ) { out.write( buffer, 0, bytesRead ); bytesWritten += bytesRead; } // Done....return bytesWritten return (int) bytesWritten; }
public int write(Sequence in, int type, OutputStream out) throws IOException { Objects.requireNonNull(out); if (!isFileTypeSupported(type, in)) { throw new IllegalArgumentException("Could not write MIDI file"); } byte [] buffer = null; int bytesRead = 0; long bytesWritten = 0; // First get the fileStream from this sequence InputStream fileStream = getFileStream(type,in); if (fileStream == null) { throw new IllegalArgumentException("Could not write MIDI file"); } buffer = new byte[bufferSize]; while( (bytesRead = fileStream.read( buffer )) >= 0 ) { out.write( buffer, 0, bytesRead ); bytesWritten += bytesRead; } // Done....return bytesWritten return (int) bytesWritten; }
public void resetTracks(){ try { Sequence sequence = this.getSequencer().getSequence(); if(sequence != null){ Track[] tracks = sequence.getTracks(); if( tracks != null ){ int count = tracks.length; for( int i = 0 ; i < count; i++ ){ this.setSolo( i , false ); this.setMute( i , false ); } } } } catch (Throwable throwable) { throwable.printStackTrace(); } }
@Override public int RegisterSong(byte[] data) { try { Sequence sequence; ByteArrayInputStream bis; try { // If data is a midi file, load it directly bis = new ByteArrayInputStream(data); sequence = MidiSystem.getSequence(bis); } catch (InvalidMidiDataException ex) { // Well, it wasn't. Dude. bis = new ByteArrayInputStream(data); sequence = MusReader.getSequence(bis); } sequencer.stop(); // stops current music if any sequencer.setSequence(sequence); // Create a sequencer for the sequence songloaded=true; } catch (Exception e) { e.printStackTrace(); return -1; } // In good old C style, we return 0 upon success? return 0; }
/** * Exports the location of each slice into a MIDI file */ public Sequence exportMidiSequence(final File file, final int ppq) throws IOException { final double tempoBps = (double) getTempo() / 60.0; final MidiSequenceBuilder builder = new MidiSequenceBuilder(ppq); final Track track = builder.getTrack(); final int n = markers.size(); int startTick = 0; int key = 35; for (int i = 0; i < n; i++) { final int location = getLocation(i); startTick = (int) Math.round(ppq * tempoBps * location / samplingRate); int tickLength = 32; builder.addNote(track, startTick, tickLength, key++); } if (file != null) { builder.save(file); } return builder.getSequence(); }
public void setupMidi() throws Exception { synthesizer = MidiSystem.getSynthesizer(); synthesizer.open(); midiChannels = synthesizer.getChannels(); sequencer = MidiSystem.getSequencer(); sequence = new Sequence(Sequence.PPQ, 10); Soundbank sb = synthesizer.getDefaultSoundbank(); if (sb != null) { instruments = synthesizer.getDefaultSoundbank().getInstruments(); for (int i = 0; i < instruments.length; i++) { System.out.printf("%4d: %s%n", i, instruments[i]); } if (instruments.length > 0) { synthesizer.loadInstrument(instruments[0]); midiChannels[currentChannel].programChange(0); } if (instruments.length > 14) { synthesizer.loadInstrument(instruments[14]); midiChannels[currentChannel].programChange(14); } } }
/** * Loads all drum sequences found in a given directory. * * @param dirPath * @return */ public static List<MidiSequence> getDrumSequencesInDirectory(String dirPath) { File folder = new File(dirPath); File[] listOfFiles = folder.listFiles(); List<MidiSequence> drumSequences = new ArrayList<MidiSequence>(); for (File file : listOfFiles) { String path = file.getAbsolutePath(); Sequence sequence = MidiUtilities.loadSequence(path); if (sequence != null) { MidiSequence drumSequence = new MidiSequence(); drumSequence.setFileName(file.getName()); drumSequence.setDirectoryPath(dirPath); drumSequence.setMidiSequence(sequence); drumSequences.add(drumSequence); } } return drumSequences; }
/** * Gets the number of microseconds per quarter note for a sequence, used to * determine its BPM. * * @param sequence * @return */ private static int getMicrosecondsPerQuarterNote(Sequence sequence) { // Check all MIDI tracks for MIDI_SET_TEMPO message for (Track track : sequence.getTracks()) { for (int i = 0; i < track.size(); i++) { MidiEvent event = track.get(i); MidiMessage message = event.getMessage(); if (message instanceof MetaMessage) { MetaMessage m = (MetaMessage) message; byte[] data = m.getData(); int type = m.getType(); if (type == MIDI_SET_TEMPO) { return ((data[0] & 0xff) << 16) | ((data[1] & 0xff) << 8) | (data[2] & 0xff); } } } } return 0; }
/** * Adds this Note at the specified time on the specified Track and * channel in the specified Sequence, then returns the time that a * sequential Note should be added. * @param seq the Sequence to which to add this Note * @param track the Track in the Sequence to which to add this Note * @param time the time at which to start this Note * @param ch the channel on which to put this Note * @param transposition amount by which to transpose this note in semitones * @param sendBankSelect * @return * @throws javax.sound.midi.InvalidMidiDataException */ public long render(Sequence seq, Track track, long time, int ch, int transposition, boolean sendBankSelect) throws InvalidMidiDataException { if( sendBankSelect ) { } int dur = getRhythmValue(); long offTime = time + dur * seq.getResolution() / BEAT; render(seq, track, time, offTime, ch, transposition); return offTime; }
public void go() { setUpGui(); try { Sequencer sequencer = MidiSystem.getSequencer(); sequencer.open(); sequencer.addControllerEventListener(ml, new int[] {127}); Sequence seq = new Sequence(Sequence.PPQ, 4); Track track = seq.createTrack(); int r = 0; for (int i = 0; i < 60; i+=4) { r = (int) ((Math.random() * 50) + 1); track.add(makeEvent(144, 1, r, 100, i)); track.add(makeEvent(176, 1, 127, 0, i)); track.add(makeEvent(128, 1, r, 100, i + 2)); } // end loop sequencer.setSequence(seq); sequencer.start(); sequencer.setTempoInBPM(120); } catch (Exception ex) {ex.printStackTrace();} }
public void go() { try { Sequencer sequencer = MidiSystem.getSequencer(); sequencer.open(); int[] eventsIWant = {127}; sequencer.addControllerEventListener(this, eventsIWant); Sequence seq = new Sequence(Sequence.PPQ, 4); Track track = seq.createTrack(); for (int i = 5; i < 60; i+= 4) { track.add(makeEvent(144, 1, i, 100, i)); track.add(makeEvent(176, 1, 127, 0, i)); track.add(makeEvent(128, 1, i, 100, i + 2)); } // end loop sequencer.setSequence(seq); sequencer.setTempoInBPM(220); sequencer.start(); } catch (Exception ex) {ex.printStackTrace();} }
public static void main(String[] args) { try { // make and open a sequencer Sequencer sequencer = MidiSystem.getSequencer(); sequencer.open(); // make a sequence and a track Sequence seq = new Sequence(Sequence.PPQ, 4); Track track = seq.createTrack(); // make a bunch of events to make the notes keep going up // (from piano note 5 to piano note 61) for (int i = 5; i < 61; i+=4) { track.add(makeEvent(144, 1, i, 100, i)); track.add(makeEvent(128, 1, i, 100, i + 2)); } // end loop sequencer.setSequence(seq); sequencer.setTempoInBPM(220); sequencer.start(); } catch (Exception ex) {ex.printStackTrace();} }
/** * Creates a sequence based on the resolution defined in ticksPerQuarterNote * */ public void createSequence() { if (sequence == null) { try { if (ticksPerQuarterNote == 0) { ticksPerQuarterNote = FrinikaConfig.TICKS_PER_QUARTER; } sequence = new FrinikaSequence(Sequence.PPQ, ticksPerQuarterNote, 1); sequencer.setSequence(sequence); } catch (InvalidMidiDataException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
/** * Find the first tempo meta message and return the tempo value * @return */ public static float findFirstTempo(Sequence sequence) throws Exception { for(Track track : sequence.getTracks()) { for(int n=0;n<track.size();n++) { MidiEvent event = track.get(n); if(event.getMessage() instanceof MetaMessage && ((MetaMessage)event.getMessage()).getType()==0x51) { float tempo = new TempoMessage((MetaMessage)event.getMessage()).getBpm(); System.out.println("Found tempomessage "+tempo+" bpm"); return tempo; } } } throw new Exception("No tempo message found"); }
/** * Takes into account both muting and solo. */ public boolean isTrackActive(int track) { Sequence song = sequencer.getSequence(); if (song == null) return true; if (sequencer.getTrackSolo(track)) return true; for (int i = song.getTracks().length - 1; i >= 0; --i) { if (i != track && sequencer.getTrackSolo(i)) return false; } return !sequencer.getTrackMute(track); }
NoteLengthPopup(JButton button, Vector <Snapable> clients, Sequence sequence) { this.button = button; this.sequence=sequence; this.clients=clients; updateButton(button, clients, sequence); int i = 0; for (String str : notes) { JMenuItem item = new JMenuItem(str, icons[i++]); item.addActionListener(this); add(item); } }
private SequenceInfo(AbcExporter abcExporter, boolean useLotroInstruments) throws InvalidMidiDataException, AbcConversionException { AbcMetadataSource metadata = abcExporter.getMetadataSource(); this.fileName = metadata.getSongTitle() + ".abc"; this.composer = metadata.getComposer(); this.title = metadata.getSongTitle(); Pair<List<ExportTrackInfo>, Sequence> result = abcExporter.exportToPreview(useLotroInstruments); sequence = result.second; sequenceCache = new SequenceDataCache(sequence); primaryTempoMPQ = sequenceCache.getPrimaryTempoMPQ(); List<TrackInfo> trackInfoList = new ArrayList<TrackInfo>(result.first.size()); for (ExportTrackInfo i : result.first) { trackInfoList.add(new TrackInfo(this, i.trackNumber, i.part.getTitle(), i.part.getInstrument(), abcExporter .getTimingInfo().getMeter(), abcExporter.getKeySignature(), i.noteEvents)); } this.trackInfoList = Collections.unmodifiableList(trackInfoList); }
/** * Starts playing the MIDI file using the sequencer. * * @param sequence the MIDI sequence. */ public void startPlay(Sequence sequence) { try { sequencer.setSequence(sequence); } catch (InvalidMidiDataException e) { throw SpongeUtils.wrapException(e); } sequencer.start(); }