SimpleMIDIFile:
Filter:
wslib/Classes (extension) | Undocumented classes

SimpleMIDIFile
ExtensionExtension

simple MIDI file implementation

Description

a MIDI file implementation capable of reading and writing all types of MIDI data (except sysex messages).

First code examples:

SimpleMIDIFile also supports Event Patterns (see Pattern and Pbind). You can use the p method to convert a SimpleMIDIFile into a playable pattern (a Ppar to be exact).

Via the fromPattern method patterns can be turned into midi files as well:

Class Methods

.new

create a new, possibly empty instance.

Arguments:

pathName

.fromPattern

create a new instance from a pattern.

Arguments:

pattern
maxEvents
maxAmp
startTime
inTracks
inTempo
timeSignature

.read

read an existing MIDI file from disk.

Arguments:

pathName

.tempoScale

global tempo scale

Instance Methods

core methods

.pathName

file path of the midi file. defaults to "~/scwork/midi.mid".

.midiEvents

an array containing all MIDI events as arrays. Format:

[ trackNumber, absTime, type, channel, val1, val2 ]

tracknumber
the number of the track in which this event sits. For format type 0 files this is always 0, for type 1 files track 0 is reserved for meta events, so midiEvent tracks start counting at 1.
absTime
the absolute start-time of the event. If timeMode == \ticks the absTime is in ticks (tempo dependant), if timeMode == \seconds it is in seconds (not depending on tempo). In the original midi file format all times are relative to the previous event on a track, but for convenience SimpleMIDIFile converts them to absolute times when reading in.
type
usually a Symbol. Can be one of [\noteOff, \noteOn, \polyTouch, \cc, \program, \afterTouch, \pitchBend]
channel midi channel on which the event plays. Starts at 0 (= midi channel 1)
val1
val2
values for the event. These are usually between 0-127. The contents of these vary per event type:
typeval1val2
noteOnnote numbervelocity
noteOffnote numbervelocity
polyTouchnote numberamount
cccontrol numberamount
programprogram number(no val2)
afterTouchamount(no val2)
pitchBenddepends on pitchBendMode

.metaEvents

an array containing all META events as arrays. Format:

[ trackNumber, absTime, type, [values] / value / string ]

absTime represents the abslute time at which the event takes place. In the Standard MIDI file format time values are always relative to the previous event. For convenience reasons the SimpleMIDIFile class converts these to absolute times when reading a file. The format of last item in the array above depends on the type of the event:

types of which the last item is a string

types of which the last item is a value

types of which the last item is an array of values

types of which the last item something else

there can be unknown types of meta events. They have status byte number instead of a symbol in the 'type' slot of the array

.tempo

a global tempo indicator in bpm. tempo always indicates the first tempo found, ignoring other tempo events in metaEvents. The tempo mapping functionality uses the tempo events instead of this value. Setting the tempo with tempo_ does not add or change tempo events ( a MIDI file can but does not need to contain a tempo event ). Use getTempo to get tempo, if existing, from metaEvents.

Arguments:

newTempo

the new tempo

.timeMode

either 'ticks' or 'seconds', defaults to 'ticks'. This affects all time information contained in the midiEvents and metaEvents. Whenever the timeMode is switched all time data will be converted.

ticks
standard MIDI file format for time data. The duration of one tick depends on the division and tempo at that specific point.
seconds
absolute seconds, not influenced by tempo or division

Arguments:

newTimeMode

the time mode

convert

flag indicating if converting data between time modes

.division

number of 'ticks' per quarter note. This should usually be 1024. Change it only when you experience problems with specific sequencer software (there seem to be some which use different divisions)

.format

can be 0, 1 or 2 (single, multi or pattern oriented)

0
typically all data in first track (0)
1
typically have all global metaData in first track (0) and all midi data in following tracks (1..). A trackName metaEvent in track 0 is usually interpreted as the song name
2
not very common. Works the same as format 1, but is meant for separate patterns in one file, contained in sequences (tracks)

Empty SimpleMIDIFiles need to be inited as format 0 or 1 first, using init0 and init1 methods.

.tracks

number of tracks used. Can be fetched from midiEvents using adjustTracks.

.pitchBendMode

one of int8', 'int16', 'float'. The default is 'int8'.

int8
pitchbend stored as array of 2 int8 values (0-127), as stored in the midifile
int16
pitchbend stored as single int16 value (0-16383)
float
pitchbend stored as floating point values (-1.0 - 1.0)

Arguments:

to
convert

Flag indicating if existing values should be converted.

init

.init0

init as type 0 file

Arguments:

inTempo
timeSignature

.init1

init as type 1 file. Minimum tracks are 2; first track contains metaData.

Arguments:

inTracks
inTempo
timeSignature

read/write

.read

read MIDI file from disk.

.write

write MIDI file to disk.

Arguments:

newFileName

the filename

.writeFile

Arguments:

theFile

.checkWrite

Arguments:

newFileName
overwrite
ask

pattern support

.p

Arguments:

inst
amp
useTempo

.fromPattern

fill with events from a Pattern

Arguments:

pattern
maxEvents
maxAmp
startTime

.generatePatternSeqs

returns sequenceable [note, dur] data in an array.

correction

.adjustTracks

adjust number of tracks according to midi data

.adjustEndOfTrack

Arguments:

track
wait

.correctTempoEvents

Arguments:

removeDuplicates
removeDoubles

.sortMIDIEvents

sort events on time and track

.sortMetaEvents

.getTempo

conversion / processing

.convertNoteOns

Arguments:

noteOffVelo

.convertNoteOffs

.shiftTime

Arguments:

deltaTime

selection

.midiTrackEvents

Arguments:

trackNumber

.midiChannelEvents

Arguments:

channel
track

.midiTrackTypeEvents

Arguments:

track
type
channel

.midiTypeEvents

Arguments:

... args

.noteEvents

Arguments:

channel
track

.realNoteOnEvents

Arguments:

channel
track

.noteOffEvents

Arguments:

channel
track

.firstNoteOnTrack

Arguments:

track

.pitchBendEvents

Arguments:

channel
track

.afterTouchEvents

Arguments:

channel
track

.controllerEvents

Arguments:

cc
channel
track

.modulationEvents

Arguments:

channel
track

.breathEvents

Arguments:

channel
track

.volumeEvents

Arguments:

channel
track

.panEvents

Arguments:

channel
track

.expressionEvents

Arguments:

channel
track

.damperEvents

Arguments:

channel
track

.metaTrackEvents

Arguments:

trackNumber

.timeSignatureEvents

.keySignatureEvents

.smpteOffsetEvents

.tempoEvents

.testEndOfTrack

Arguments:

track

converted selection

to different array formats or Classes

.noteSustainEvents

Arguments:

channel
track

returns events in the form:

.midiDeltaEvents

Arguments:

track
type
channel

.midiDeltaCCEvents

Arguments:

track
cc
channel

.timeSignatures

.keySignatures

.trackNames

.instNames

.midiTracks

.metaTracks

.asDicts

.asNoteDicts

Arguments:

channel
track

.envFromType

Arguments:

track
type
channel

.envFromCC

Arguments:

track
cc
channel

extraction of single values

.trackName

Arguments:

track

.instName

Arguments:

track

.length

.smpteOffset

timing methods (converted selection, extraction)

.tempi

.tempoMap

.tempoEnv

.beatAtTime

Arguments:

time

.timeAtBeat

Arguments:

beat

.tempoAtTime

Arguments:

time

.timeSignatureAsArray

.measureAtBeat

Arguments:

beat
measureFormat

.measureAtTime

Arguments:

time
measureFormat

.beatAtMeasure

Arguments:

measure
measureFormat

.timeAtMeasure

Arguments:

measure
measureFormat

addition of events

.addMIDIEvent

Arguments:

event

array, for format, see above.

sort

.addAllMIDIEvents

Arguments:

events

array of arrays, for their format, see above.

sort

.addMIDIEventToTrack

Arguments:

event
track
sort

.addAllMIDIEventsToTrack

Arguments:

events
track
sort

.addMIDITypeEvent

Arguments:

type
channel
args
absTime
track
sort

.addAllMIDITypeEvents

Arguments:

type
channel
args
absTime
track
sort

.addNote

Arguments:

noteNumber
velo
startTime
dur
upVelo
channel
track
sort

.addCC

Arguments:

cc
val
startTime
channel
track

.addMetaEvent

Arguments:

event
sort

.addTrack

Arguments:

name

.addTimeSignature

Arguments:

div
denom
time
sort
removeOld

.addTimeSignatureString

Arguments:

string
time
sort
removeOld

.addTempo

Arguments:

tempo
time
sort
removeOld

.setTrackName

Arguments:

name
track

.setInstName

Arguments:

name
track

.setTempo

Arguments:

newTempo

removal

.removeMetaEvents

Arguments:

type
time
track

.removeTimeSignature

Arguments:

time
doIt

.removeTempo

Arguments:

time
doIt

analysis

.analyzeTypes

.analyzeMetaTypes

.analyzeChannels

Arguments:

type

.analyzeChannel

Arguments:

channel
track

.analyzeUsedChannels

Arguments:

track

.analyzeUsedTracks

.analyzeUsedEvents

.analyzeTracks

Arguments:

type

.analyzeCC

.analyzeCCTracks

.usedChannels

Arguments:

track

.usedTracks

Arguments:

channel

.countMIDIEvents

Arguments:

type
track
channel

.info

additions

.convertTimes

Arguments:

newTimeMode

.getTime

Arguments:

file

.play

Arguments:

clock
protoEvent
quant
inst
amp

.noteOnEvents

Arguments:

channel
track

.sysexEvents

.processChunk

Arguments:

file

.rawMeasureAtBeat

Arguments:

beat

.convertToInt8

Arguments:

val
size

.convertPitchBend

Arguments:

to

.asDeltaChunks

.numbytes

.theTrackNumber

.asChunks

.asMIDIFileChunks

.theChan

.playNotesWithFunction

Arguments:

function
channel
track
clock
doneAction

.curTime

.init

.theCmd

.keySignatures

.ccEvents

Arguments:

cc
channel
track

.renderPattern

Arguments:

pattern
maxEvents
timeSig
numTicks

.convertToVLInteger

Arguments:

dT

.rawMeasureAtTime

Arguments:

time

.trackNames

.asMetaChunks

.asMIDIChunks

.handleMeta

Arguments:

file

.playWithSynth

Arguments:

defName
maxlevel
minlevel
addArgs
addAction
channel
track
target

.plot

Arguments:

notesOnly

.panEvents

Arguments:

channel
track

.expressionEvents

Arguments:

channel
track

.beatAtRawMeasure

Arguments:

rawMeasure

.prAdjustEndOfTrack

Arguments:

track
wait

.tempo

Arguments:

newTempo

.info

.asMetaDeltaChunks

.asPenFunction

Arguments:

bounds
track
type
channel
cc
minVal
maxVal
grid

.timeAtRawMeasure

Arguments:

rawMeasure

.handleRunningStatus

Arguments:

val
file

.getVl

Arguments:

file

.handleMIDI

Arguments:

cmd
file

.handleSysex

Arguments:

file

.firstNote

Arguments:

trackArray

.endOfTrack

Arguments:

track

.asMIDIDeltaChunks