package midi
This is one of O1’s sound packages (the other being o1.sound.sampled). This package provides
a simple interface to a part of the more generic MIDI API. In particular, it lets students
play MIDI music by writing notes a String
and passing them to a function. That function,
play, has an alias in the top-level package o1, so it’s
accessible to students simply via import o1._
.
Here is a summary of the notation used in the musical String
s that you pass to
play and some other functions in this package:
"cdefgah"
plays seven notes at the default tempo of 120. (N.B. the seventh note ish
, notb
.)"CDEFGAH"
plays them louder."CDEFGAH/240"
plays them at a double tempo of 240."CD E"
has a pause between the second and third note."CD-E---"
has a longer second note and a longer still third note."C.D.E"
produces a staccato-like effect on the first two notes (playing them shorter followed by a pause).">CDE<<<CDE"
plays three notes in a higher octave then shifts three octaves down before playing them again."C7D3E"
plays the c in Octave #7, the d in Octave #3, and the e in the default Octave #5."CbDBE#7F"
has a c-flat, a d-flat, an e-sharp in Octave #7, and a natural f. b and B are equivalent."C♭D♭E♯7F♮"
is a fancy-pants way of writing the same thing."CDE[13]CDE"
plays three notes using the default Instrument #1, then again using Instrument #13."(CEG)(DF#A)(EG#H)---"
plays three chords, the last of which is longer."CDE&<<[28]efg&[110] F"
simultaneously plays the three parts separated by&
s."P:CDE"
uses the MIDI percussion channel: each "note"represents a different percussion instrument.
"C|D||||E"
means the same as ̀"cde"
: the|
s don't do anything, but you can use them to mark bars or whatever.
For a numbered list of the instruments, see the General MIDI Sound Set; the Instrument
object contains the same
list as Scala constants.
- Alphabetic
- By Inheritance
- midi
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Type Members
- sealed trait Accidental extends Product with Serializable
An adjustment to the frequency of a Pitch: a Flat, a Sharp, or a Natural.
- case class Chord(notes: Seq[MusicElem]) extends MusicElem with Product with Serializable
Each instance of this class represents a combination of simultaneous Notes or other MusicElems.
- case class Instrument(number: Int) extends MusicElem with Product with Serializable
A MusicElem that sets the instrument for the upcoming notes.
A MusicElem that sets the instrument for the upcoming notes. The 128 main instruments of the standard MIDI list are supported. The companion object of this class provides constants that match those instruments.
- number
the number of a MIDI instrument between 0 and 127, inclusive
- type MidiUnavailableException = javax.sound.midi.MidiUnavailableException
An error type that signals the underlying Java MIDI toolkit is unavailable.
- case class Music(tempoSetting: Option[Int], voices: Seq[Voice]) extends Product with Serializable
Represents a piece of music that may consist of multiple Voices, which in turn consist of MusicElems such as notes.
Represents a piece of music that may consist of multiple Voices, which in turn consist of MusicElems such as notes.
Instead of using the constructor, you can use the companion object to construct a
Music
object from a string: e.g.,Music("cdefg&>gfedc")
.- tempoSetting
a tempo setting (in beats per minute) for the music, if there is one
- voices
the voices that, played simultaneously, make up the piece of music; no more than MaxVoices plus a possible percussion track
- trait MusicElem extends AnyRef
A
MusicElem
is an element describes an aspect of MIDI music: a note, a chord, a pause, or a meta directive such as the change of an instrument.A
MusicElem
is an element describes an aspect of MIDI music: a note, a chord, a pause, or a meta directive such as the change of an instrument.MusicElem
s put together make a Voice; voices put together compose a piece of Music.Each
MusicEleme
has alength
that represents how long it takes to play it. The duration of the shortest audible sound playable witho1.sound.midi
is one; other durations are linearly relative to that.- See also
the subtypes Note, Chord, Pause, OctaveShift, Instrument
- case class Note(pitch: Pitch, length: Int, isStaccato: Boolean) extends MusicElem with Product with Serializable
Each instance of this class represents a single musical note at a set pitch.
Each instance of this class represents a single musical note at a set pitch.
- pitch
the pitch of the note
- length
the duration of the note: how long it takes to play it
- isStaccato
whether the note should be played staccato so that sound is shorter than its duration
- case class OctaveShift(shift: Int) extends MusicElem with OccursInChord with Product with Serializable
Each instance of this class is a directive that instructs the subsequent notes to play one octave higher or lower.
Each instance of this class is a directive that instructs the subsequent notes to play one octave higher or lower.
- shift
the direction of the shift: +1 means one octave higher, -1 one lower
- case class Pitch(name: Char, accidental: Accidental, octave: Option[Int]) extends OccursInChord with Product with Serializable
A
Pitch
is a frequency for a note to be played at.A
Pitch
is a frequency for a note to be played at.- name
the name of the corresponding note, which provides the base frequency; one of "cdefgah" or their upper-case equivalents
- accidental
an adjustment to the base frequency
- octave
the number of the octave relative to DefaultOctave; e.g., -2 means two octaves lower than the default; passing in
None
has the same effect as passing iuzero
- case class Voice(notes: Seq[MusicElem], isPercussion: Boolean) extends Product with Serializable
Represents a single voice within a piece of Music.
Represents a single voice within a piece of Music. A voice consists of MusicElems, primarily notes, in order.
- notes
the notes (and possible other MusicElems) that compose this voice
- isPercussion
whether the notes should not be interpresed as regular notes but as the special sounds defined for the MIDI standard’s percussion channel
Value Members
- val DefaultOctave: Int
The number (5) of the default octave to play notes from.
- val DefaultTempo: Int
The default tempo (120 beats per minute).
The default tempo (120 beats per minute). This is used by various methods in with this package when playing music, unless otherwise specified.
- val HighVolume: Int
A number (127) that corresponds to the highest possible volume setting available to this MIDI package.
A number (127) that corresponds to the highest possible volume setting available to this MIDI package. (The lowest is zero.)
- val MaxVoices: Int
the maximum number of simultaneous Voices supported by this library.
the maximum number of simultaneous Voices supported by this library. This number doesn’t include the additional percussion track.
- val MediumVolume: Int
A number (80) that corresponds to a “medium” volume on the scale from 0 to 127 available to this MIDI package.
- def parse(musicString: String): Music
Parses the given musical
String
and returns the result as a Music object.Parses the given musical
String
and returns the result as a Music object. TheString
must be formatted as described in the package overview. Throws anIllegalArgumentException
in case theString
was invalid. - def play(music: Music): Unit
Plays the music described by the given Music object on the MIDI synthesizer.
- def play(music: String): Unit
Parses the given musical
String
and plays the music that it describes on the MIDI synthesizer.Parses the given musical
String
and plays the music that it describes on the MIDI synthesizer. TheString
must be formatted as described in the package overview. Prints out an error message in case theString
was invalid. - object Flat extends Accidental with Product with Serializable
Represents the concept of a flat note.
Represents the concept of a flat note. Assigning this accidental to a Pitch makes it slightly lower.
- object Instrument extends Serializable
This object provides a selection of constants that correspond to the 128 main instruments of the standard MIDI list.
This object provides a selection of constants that correspond to the 128 main instruments of the standard MIDI list. They are grouped in nested objects (Piano, Strings, etc.). The object PercussionChannel contains constants that correspond to the special sounds available on the MIDI percussion channel.
- object Music extends Serializable
This companion object of class
Music
just provides a factory method. - object Natural extends Accidental with Product with Serializable
Represents the concept of a natural note (neither Sharp nor Flat.
- object Pause extends MusicElem with Product with Serializable
This object is a
MusicElem
that represents a short pause in the music. - object Sequencer
This object is an interface to the underlying MIDI sequencer.
This object is an interface to the underlying MIDI sequencer. It is a thin Scala wrapper around the sequencer in the Java MIDI API.
- object Sharp extends Accidental with Product with Serializable
Represents the concept of a sharp note.
Represents the concept of a sharp note. Assigning this accidental to a Pitch makes it slightly higher.
- object Synthesizer
This object is an interface to the underlying MIDI synthesizer.
This object is an interface to the underlying MIDI synthesizer. It is a thin Scala wrapper around the synthesizer in the Java MIDI API. Uses the piano sound.