Path: blob/master/src/java.desktop/share/classes/javax/sound/midi/Sequencer.java
41159 views
/*1* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.sound.midi;2627import java.io.IOException;28import java.io.InputStream;2930/**31* A hardware or software device that plays back a MIDI32* {@link Sequence sequence} is known as a <em>sequencer</em>. A MIDI sequence33* contains lists of time-stamped MIDI data, such as might be read from a34* standard MIDI file. Most sequencers also provide functions for creating and35* editing sequences.36* <p>37* The {@code Sequencer} interface includes methods for the following basic MIDI38* sequencer operations:39* <ul>40* <li>obtaining a sequence from MIDI file data41* <li>starting and stopping playback42* <li>moving to an arbitrary position in the sequence43* <li>changing the tempo (speed) of playback44* <li>synchronizing playback to an internal clock or to received MIDI45* messages46* <li>controlling the timing of another device47* </ul>48* In addition, the following operations are supported, either directly, or49* indirectly through objects that the {@code Sequencer} has access to:50* <ul>51* <li>editing the data by adding or deleting individual MIDI events or entire52* tracks53* <li>muting or soloing individual tracks in the sequence54* <li>notifying listener objects about any meta-events or control-change55* events encountered while playing back the sequence56* </ul>57*58* @author Kara Kytle59* @author Florian Bomers60* @see SyncMode61* @see #addMetaEventListener62* @see ControllerEventListener63* @see Receiver64* @see Transmitter65* @see MidiDevice66*/67public interface Sequencer extends MidiDevice {6869/**70* A value indicating that looping should continue indefinitely rather than71* complete after a specific number of loops.72*73* @see #setLoopCount74* @since 1.575*/76int LOOP_CONTINUOUSLY = -1;7778/**79* Sets the current sequence on which the sequencer operates.80* <p>81* This method can be called even if the {@code Sequencer} is closed.82*83* @param sequence the sequence to be loaded84* @throws InvalidMidiDataException if the sequence contains invalid MIDI85* data, or is not supported86*/87void setSequence(Sequence sequence) throws InvalidMidiDataException;8889/**90* Sets the current sequence on which the sequencer operates. The stream91* must point to MIDI file data.92* <p>93* This method can be called even if the {@code Sequencer} is closed.94*95* @param stream stream containing MIDI file data96* @throws IOException if an I/O exception occurs during reading of the97* stream98* @throws InvalidMidiDataException if invalid data is encountered in the99* stream, or the stream is not supported100*/101void setSequence(InputStream stream)102throws IOException, InvalidMidiDataException;103104/**105* Obtains the sequence on which the Sequencer is currently operating.106* <p>107* This method can be called even if the {@code Sequencer} is closed.108*109* @return the current sequence, or {@code null} if no sequence is currently110* set111*/112Sequence getSequence();113114/**115* Starts playback of the MIDI data in the currently loaded sequence.116* Playback will begin from the current position. If the playback position117* reaches the loop end point, and the loop count is greater than 0,118* playback will resume at the loop start point for the number of119* repetitions set with {@code setLoopCount}. After that, or if the loop120* count is 0, playback will continue to play to the end of the sequence.121* <p>122* The implementation ensures that the synthesizer is brought to a123* consistent state when jumping to the loop start point by sending124* appropriate controllers, pitch bend, and program change events.125*126* @throws IllegalStateException if the {@code Sequencer} is closed127* @see #setLoopStartPoint128* @see #setLoopEndPoint129* @see #setLoopCount130* @see #stop131*/132void start();133134/**135* Stops recording, if active, and playback of the currently loaded136* sequence, if any.137*138* @throws IllegalStateException if the {@code Sequencer} is closed139* @see #start140* @see #isRunning141*/142void stop();143144/**145* Indicates whether the Sequencer is currently running. The default is146* {@code false}. The Sequencer starts running when either{@link #start} or147* {@link #startRecording} is called. {@code isRunning} then returns148* {@code true} until playback of the sequence completes or {@link #stop} is149* called.150*151* @return {@code true} if the Sequencer is running, otherwise {@code false}152*/153boolean isRunning();154155/**156* Starts recording and playback of MIDI data. Data is recorded to all157* enabled tracks, on the channel(s) for which they were enabled. Recording158* begins at the current position of the sequencer. Any events already in159* the track are overwritten for the duration of the recording session.160* Events from the currently loaded sequence, if any, are delivered to the161* sequencer's transmitter(s) along with messages received during recording.162* <p>163* Note that tracks are not by default enabled for recording. In order to164* record MIDI data, at least one track must be specifically enabled for165* recording.166*167* @throws IllegalStateException if the {@code Sequencer} is closed168* @see #recordEnable169* @see #recordDisable170*/171void startRecording();172173/**174* Stops recording, if active. Playback of the current sequence continues.175*176* @throws IllegalStateException if the {@code Sequencer} is closed177* @see #startRecording178* @see #isRecording179*/180void stopRecording();181182/**183* Indicates whether the Sequencer is currently recording. The default is184* {@code false}. The Sequencer begins recording when185* {@link #startRecording} is called, and then returns {@code true} until186* {@link #stop} or {@link #stopRecording} is called.187*188* @return {@code true} if the Sequencer is recording, otherwise189* {@code false}190*/191boolean isRecording();192193/**194* Prepares the specified track for recording events received on a195* particular channel. Once enabled, a track will receive events when196* recording is active.197*198* @param track the track to which events will be recorded199* @param channel the channel on which events will be received. If -1 is200* specified for the channel value, the track will receive data from201* all channels.202* @throws IllegalArgumentException thrown if the track is not part of the203* current sequence204*/205void recordEnable(Track track, int channel);206207/**208* Disables recording to the specified track. Events will no longer be209* recorded into this track.210*211* @param track the track to disable for recording, or {@code null} to212* disable recording for all tracks213*/214void recordDisable(Track track);215216/**217* Obtains the current tempo, expressed in beats per minute. The actual218* tempo of playback is the product of the returned value and the tempo219* factor.220*221* @return the current tempo in beats per minute222* @see #getTempoFactor223* @see #setTempoInBPM(float)224* @see #getTempoInMPQ225*/226float getTempoInBPM();227228/**229* Sets the tempo in beats per minute. The actual tempo of playback is the230* product of the specified value and the tempo factor.231*232* @param bpm desired new tempo in beats per minute233* @see #getTempoFactor234* @see #setTempoInMPQ(float)235* @see #getTempoInBPM236*/237void setTempoInBPM(float bpm);238239/**240* Obtains the current tempo, expressed in microseconds per quarter note.241* The actual tempo of playback is the product of the returned value and the242* tempo factor.243*244* @return the current tempo in microseconds per quarter note245* @see #getTempoFactor246* @see #setTempoInMPQ(float)247* @see #getTempoInBPM248*/249float getTempoInMPQ();250251/**252* Sets the tempo in microseconds per quarter note. The actual tempo of253* playback is the product of the specified value and the tempo factor.254*255* @param mpq desired new tempo in microseconds per quarter note256* @see #getTempoFactor257* @see #setTempoInBPM(float)258* @see #getTempoInMPQ259*/260void setTempoInMPQ(float mpq);261262/**263* Scales the sequencer's actual playback tempo by the factor provided. The264* default is 1.0. A value of 1.0 represents the natural rate (the tempo265* specified in the sequence), 2.0 means twice as fast, etc. The tempo266* factor does not affect the values returned by {@link #getTempoInMPQ} and267* {@link #getTempoInBPM}. Those values indicate the tempo prior to scaling.268* <p>269* Note that the tempo factor cannot be adjusted when external270* synchronization is used. In that situation, {@code setTempoFactor} always271* sets the tempo factor to 1.0.272*273* @param factor the requested tempo scalar274* @see #getTempoFactor275*/276void setTempoFactor(float factor);277278/**279* Returns the current tempo factor for the sequencer. The default is 1.0.280*281* @return tempo factor282* @see #setTempoFactor(float)283*/284float getTempoFactor();285286/**287* Obtains the length of the current sequence, expressed in MIDI ticks, or 0288* if no sequence is set.289*290* @return length of the sequence in ticks291*/292long getTickLength();293294/**295* Obtains the current position in the sequence, expressed in MIDI ticks.296* (The duration of a tick in seconds is determined both by the tempo and by297* the timing resolution stored in the {@link Sequence}.)298*299* @return current tick300* @see #setTickPosition301*/302long getTickPosition();303304/**305* Sets the current sequencer position in MIDI ticks.306*307* @param tick the desired tick position308* @see #getTickPosition309*/310void setTickPosition(long tick);311312/**313* Obtains the length of the current sequence, expressed in microseconds, or314* 0 if no sequence is set.315*316* @return length of the sequence in microseconds317*/318long getMicrosecondLength();319320/**321* Obtains the current position in the sequence, expressed in microseconds.322*323* @return the current position in microseconds324* @see #setMicrosecondPosition325*/326@Override327long getMicrosecondPosition();328329/**330* Sets the current position in the sequence, expressed in microseconds.331*332* @param microseconds desired position in microseconds333* @see #getMicrosecondPosition334*/335void setMicrosecondPosition(long microseconds);336337/**338* Sets the source of timing information used by this sequencer. The339* sequencer synchronizes to the master, which is the internal clock, MIDI340* clock, or MIDI time code, depending on the value of {@code sync}. The341* {@code sync} argument must be one of the supported modes, as returned by342* {@link #getMasterSyncModes}.343*344* @param sync the desired master synchronization mode345* @see SyncMode#INTERNAL_CLOCK346* @see SyncMode#MIDI_SYNC347* @see SyncMode#MIDI_TIME_CODE348* @see #getMasterSyncMode349*/350void setMasterSyncMode(SyncMode sync);351352/**353* Obtains the current master synchronization mode for this sequencer.354*355* @return the current master synchronization mode356* @see #setMasterSyncMode(SyncMode)357* @see #getMasterSyncModes358*/359SyncMode getMasterSyncMode();360361/**362* Obtains the set of master synchronization modes supported by this363* sequencer.364*365* @return the available master synchronization modes366* @see SyncMode#INTERNAL_CLOCK367* @see SyncMode#MIDI_SYNC368* @see SyncMode#MIDI_TIME_CODE369* @see #getMasterSyncMode370* @see #setMasterSyncMode(SyncMode)371*/372SyncMode[] getMasterSyncModes();373374/**375* Sets the slave synchronization mode for the sequencer. This indicates the376* type of timing information sent by the sequencer to its receiver. The377* {@code sync} argument must be one of the supported modes, as returned by378* {@link #getSlaveSyncModes}.379*380* @param sync the desired slave synchronization mode381* @see SyncMode#MIDI_SYNC382* @see SyncMode#MIDI_TIME_CODE383* @see SyncMode#NO_SYNC384* @see #getSlaveSyncModes385*/386void setSlaveSyncMode(SyncMode sync);387388/**389* Obtains the current slave synchronization mode for this sequencer.390*391* @return the current slave synchronization mode392* @see #setSlaveSyncMode(SyncMode)393* @see #getSlaveSyncModes394*/395SyncMode getSlaveSyncMode();396397/**398* Obtains the set of slave synchronization modes supported by the399* sequencer.400*401* @return the available slave synchronization modes402* @see SyncMode#MIDI_SYNC403* @see SyncMode#MIDI_TIME_CODE404* @see SyncMode#NO_SYNC405*/406SyncMode[] getSlaveSyncModes();407408/**409* Sets the mute state for a track. This method may fail for a number of410* reasons. For example, the track number specified may not be valid for the411* current sequence, or the sequencer may not support this functionality. An412* application which needs to verify whether this operation succeeded should413* follow this call with a call to {@link #getTrackMute}.414*415* @param track the track number. Tracks in the current sequence are416* numbered from 0 to the number of tracks in the sequence minus 1.417* @param mute the new mute state for the track. {@code true} implies the418* track should be muted, {@code false} implies the track should be419* unmuted.420* @see #getSequence421*/422void setTrackMute(int track, boolean mute);423424/**425* Obtains the current mute state for a track. The default mute state for426* all tracks which have not been muted is false. In any case where the427* specified track has not been muted, this method should return false. This428* applies if the sequencer does not support muting of tracks, and if the429* specified track index is not valid.430*431* @param track the track number. Tracks in the current sequence are432* numbered from 0 to the number of tracks in the sequence minus 1.433* @return {@code true} if muted, {@code false} if not434*/435boolean getTrackMute(int track);436437/**438* Sets the solo state for a track. If {@code solo} is {@code true} only439* this track and other solo'd tracks will sound. If {@code solo} is440* {@code false} then only other solo'd tracks will sound, unless no tracks441* are solo'd in which case all un-muted tracks will sound.442* <p>443* This method may fail for a number of reasons. For example, the track444* number specified may not be valid for the current sequence, or the445* sequencer may not support this functionality. An application which needs446* to verify whether this operation succeeded should follow this call with a447* call to {@link #getTrackSolo}.448*449* @param track the track number. Tracks in the current sequence are450* numbered from 0 to the number of tracks in the sequence minus 1.451* @param solo the new solo state for the track. {@code true} implies the452* track should be solo'd, {@code false} implies the track should453* not be solo'd.454* @see #getSequence455*/456void setTrackSolo(int track, boolean solo);457458/**459* Obtains the current solo state for a track. The default mute state for460* all tracks which have not been solo'd is false. In any case where the461* specified track has not been solo'd, this method should return false.462* This applies if the sequencer does not support soloing of tracks, and if463* the specified track index is not valid.464*465* @param track the track number. Tracks in the current sequence are466* numbered from 0 to the number of tracks in the sequence minus 1.467* @return {@code true} if solo'd, {@code false} if not468*/469boolean getTrackSolo(int track);470471/**472* Registers a meta-event listener to receive notification whenever a473* meta-event is encountered in the sequence and processed by the sequencer.474* This method can fail if, for instance,this class of sequencer does not475* support meta-event notification.476*477* @param listener listener to add478* @return {@code true} if the listener was successfully added, otherwise479* {@code false}480* @see #removeMetaEventListener481* @see MetaEventListener482* @see MetaMessage483*/484boolean addMetaEventListener(MetaEventListener listener);485486/**487* Removes the specified meta-event listener from this sequencer's list of488* registered listeners, if in fact the listener is registered.489*490* @param listener the meta-event listener to remove491* @see #addMetaEventListener492*/493void removeMetaEventListener(MetaEventListener listener);494495/**496* Registers a controller event listener to receive notification whenever497* the sequencer processes a control-change event of the requested type or498* types. The types are specified by the {@code controllers} argument, which499* should contain an array of MIDI controller numbers. (Each number should500* be between 0 and 127, inclusive. See the MIDI 1.0 Specification for the501* numbers that correspond to various types of controllers.)502* <p>503* The returned array contains the MIDI controller numbers for which the504* listener will now receive events. Some sequencers might not support505* controller event notification, in which case the array has a length of 0.506* Other sequencers might support notification for some controllers but not507* all. This method may be invoked repeatedly. Each time, the returned array508* indicates all the controllers that the listener will be notified about,509* not only the controllers requested in that particular invocation.510*511* @param listener the controller event listener to add to the list of512* registered listeners513* @param controllers the MIDI controller numbers for which change514* notification is requested515* @return the numbers of all the MIDI controllers whose changes will now be516* reported to the specified listener517* @see #removeControllerEventListener518* @see ControllerEventListener519*/520int[] addControllerEventListener(ControllerEventListener listener,521int[] controllers);522523/**524* Removes a controller event listener's interest in one or more types of525* controller event. The {@code controllers} argument is an array of MIDI526* numbers corresponding to the controllers for which the listener should no527* longer receive change notifications. To completely remove this listener528* from the list of registered listeners, pass in {@code null} for529* {@code controllers}. The returned array contains the MIDI controller530* numbers for which the listener will now receive events. The array has a531* length of 0 if the listener will not receive change notifications for any532* controllers.533*534* @param listener old listener535* @param controllers the MIDI controller numbers for which change536* notification should be cancelled, or {@code null} to cancel for537* all controllers538* @return the numbers of all the MIDI controllers whose changes will now be539* reported to the specified listener540* @see #addControllerEventListener541*/542int[] removeControllerEventListener(ControllerEventListener listener,543int[] controllers);544545/**546* Sets the first MIDI tick that will be played in the loop. If the loop547* count is greater than 0, playback will jump to this point when reaching548* the loop end point.549* <p>550* A value of 0 for the starting point means the beginning of the loaded551* sequence. The starting point must be lower than or equal to the ending552* point, and it must fall within the size of the loaded sequence.553* <p>554* A sequencer's loop start point defaults to start of the sequence.555*556* @param tick the loop's starting position, in MIDI ticks (zero-based)557* @throws IllegalArgumentException if the requested loop start point cannot558* be set, usually because it falls outside the sequence's duration559* or because the start point is after the end point560* @see #setLoopEndPoint561* @see #setLoopCount562* @see #getLoopStartPoint563* @see #start564* @since 1.5565*/566void setLoopStartPoint(long tick);567568/**569* Obtains the start position of the loop, in MIDI ticks.570*571* @return the start position of the loop, in MIDI ticks (zero-based)572* @see #setLoopStartPoint573* @since 1.5574*/575long getLoopStartPoint();576577/**578* Sets the last MIDI tick that will be played in the loop. If the loop579* count is 0, the loop end point has no effect and playback continues to580* play when reaching the loop end point.581* <p>582* A value of -1 for the ending point indicates the last tick of the583* sequence. Otherwise, the ending point must be greater than or equal to584* the starting point, and it must fall within the size of the loaded585* sequence.586* <p>587* A sequencer's loop end point defaults to -1, meaning the end of the588* sequence.589*590* @param tick the loop's ending position, in MIDI ticks (zero-based), or591* -1 to indicate the final tick592* @throws IllegalArgumentException if the requested loop point cannot be593* set, usually because it falls outside the sequence's duration or594* because the ending point is before the starting point595* @see #setLoopStartPoint596* @see #setLoopCount597* @see #getLoopEndPoint598* @see #start599* @since 1.5600*/601void setLoopEndPoint(long tick);602603/**604* Obtains the end position of the loop, in MIDI ticks.605*606* @return the end position of the loop, in MIDI ticks (zero-based), or -1607* to indicate the end of the sequence608* @see #setLoopEndPoint609* @since 1.5610*/611long getLoopEndPoint();612613/**614* Sets the number of repetitions of the loop for playback. When the615* playback position reaches the loop end point, it will loop back to the616* loop start point {@code count} times, after which playback will continue617* to play to the end of the sequence.618* <p>619* If the current position when this method is invoked is greater than the620* loop end point, playback continues to the end of the sequence without621* looping, unless the loop end point is changed subsequently.622* <p>623* A {@code count} value of 0 disables looping: playback will continue at624* the loop end point, and it will not loop back to the loop start point.625* This is a sequencer's default.626* <p>627* If playback is stopped during looping, the current loop status is628* cleared; subsequent start requests are not affected by an interrupted629* loop operation.630*631* @param count the number of times playback should loop back from the632* loop's end position to the loop's start position, or633* {@link #LOOP_CONTINUOUSLY} to indicate that looping should634* continue until interrupted635* @throws IllegalArgumentException if {@code count} is negative and not636* equal to {@link #LOOP_CONTINUOUSLY}637* @see #setLoopStartPoint638* @see #setLoopEndPoint639* @see #getLoopCount640* @see #start641* @since 1.5642*/643void setLoopCount(int count);644645/**646* Obtains the number of repetitions for playback.647*648* @return the number of loops after which playback plays to the end of the649* sequence650* @see #setLoopCount651* @see #start652* @since 1.5653*/654int getLoopCount();655656/**657* A {@code SyncMode} object represents one of the ways in which a MIDI658* sequencer's notion of time can be synchronized with a master or slave659* device. If the sequencer is being synchronized to a master, the sequencer660* revises its current time in response to messages from the master. If the661* sequencer has a slave, the sequencer similarly sends messages to control662* the slave's timing.663* <p>664* There are three predefined modes that specify possible masters for a665* sequencer: {@code INTERNAL_CLOCK}, {@code MIDI_SYNC}, and666* {@code MIDI_TIME_CODE}. The latter two work if the sequencer receives667* MIDI messages from another device. In these two modes, the sequencer's668* time gets reset based on system real-time timing clock messages or MIDI669* time code (MTC) messages, respectively. These two modes can also be used670* as slave modes, in which case the sequencer sends the corresponding types671* of MIDI messages to its receiver (whether or not the sequencer is also672* receiving them from a master). A fourth mode, {@code NO_SYNC}, is used to673* indicate that the sequencer should not control its receiver's timing.674*675* @see Sequencer#setMasterSyncMode(SyncMode)676* @see Sequencer#setSlaveSyncMode(SyncMode)677*/678class SyncMode {679680/**681* Synchronization mode name.682*/683private final String name;684685/**686* Constructs a synchronization mode.687*688* @param name name of the synchronization mode689*/690protected SyncMode(String name) {691692this.name = name;693}694695/**696* Indicates whether the specified object is equal to this697* synchronization mode, returning {@code true} if the objects are the698* same.699*700* @param obj the reference object with which to compare701* @return {@code true} if the specified object is equal to this702* synchronization mode; {@code false} otherwise703*/704@Override705public final boolean equals(Object obj) {706707return super.equals(obj);708}709710/**711* Returns a hash code value for this synchronization mode.712*713* @return a hash code value for this synchronization mode714*/715@Override716public final int hashCode() {717718return super.hashCode();719}720721/**722* Returns mode's name as the string representation of the723* synchronization mode.724*725* @return a string representation of the synchronization mode726*/727@Override728public final String toString() {729return name;730}731732/**733* A master synchronization mode that makes the sequencer get its timing734* information from its internal clock. This is not a legal slave sync735* mode.736*/737public static final SyncMode INTERNAL_CLOCK = new SyncMode("Internal Clock");738739/**740* A master or slave synchronization mode that specifies the use of MIDI741* clock messages. If this mode is used as the master sync mode, the742* sequencer gets its timing information from system real-time MIDI743* clock messages. This mode only applies as the master sync mode for744* sequencers that are also MIDI receivers. If this is the slave sync745* mode, the sequencer sends system real-time MIDI clock messages to its746* receiver. MIDI clock messages are sent at a rate of 24 per quarter747* note.748*/749public static final SyncMode MIDI_SYNC = new SyncMode("MIDI Sync");750751/**752* A master or slave synchronization mode that specifies the use of MIDI753* Time Code. If this mode is used as the master sync mode, the754* sequencer gets its timing information from MIDI Time Code messages.755* This mode only applies as the master sync mode to sequencers that are756* also MIDI receivers. If this mode is used as the slave sync mode, the757* sequencer sends MIDI Time Code messages to its receiver. (See the758* MIDI 1.0 Detailed Specification for a description of MIDI Time Code.)759*/760public static final SyncMode MIDI_TIME_CODE = new SyncMode("MIDI Time Code");761762/**763* A slave synchronization mode indicating that no timing information764* should be sent to the receiver. This is not a legal master sync mode.765*/766public static final SyncMode NO_SYNC = new SyncMode("No Timing");767768}769}770771772