Location: PHPKode > projects > XRNS-PHP > midi_class_v175/documentation.htm
<!-- last changes: 26.04.2005 -->
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<title>PHP MIDI CLASS</title>
h2 {
h3 {
.func {
.sample {
  font-family:"courier new",courier;
<body bgcolor="#FFFFFF">



<a href="#addMsg">addMsg()</a><br>
<a href="#deleteMsg">deleteMsg()</a><br>
<a href="#deleteTrack">deleteTrack()</a><br>
<a href="#downloadMidFile">downloadMidFile()</a><br>
<a href="#getBpm">getBpm()</a><br>
<a href="#getDrumkitList">getDrumkitList()</a><br>
<a href="#getDrumset">getDrumset()</a><br>
<a href="#getInstrumentList">getInstrumentList()</a><br>
<a href="#getMid">getMid()</a><br>
<a href="#getMsg">getMsg()</a><br>
<a href="#getMsgCount">getMsgCount()</a><br>
<a href="#getNoteList">getNoteList()</a><br>
<a href="#getTempo">getTempo()</a><br>
<a href="#getTimebase">getTimebase()</a><br>
<a href="#getTrack">getTrack()</a><br>
<a href="#getTrackCount">getTrackCount()</a><br>
<a href="#getTrackTxt">getTrackTxt()</a><br>
<a href="#getTxt">getTxt()</a><br>
<a href="#getXml">getXml()</a><br>
<a href="#importMid">importMid()</a><br>
<a href="#importTrackTxt">importTrackTxt()</a><br>
<a href="#importTxt">importTxt()</a><br>
<a href="#importXml">importXml()</a><br>
<a href="#insertMsg">insertMsg()</a><br>
<a href="#newTrack">newTrack()</a><br>
<a href="#open">open()</a><br>
<a href="#playMidFile">playMidFile()</a><br>
<a href="#saveMidFile">saveMidFile()</a><br>
<a href="#setBpm">setBpm()</a><br>
<a href="#setTempo">setTempo()</a><br>
<a href="#setTimebase">setTimebase()</a><br>
<a href="#soloTrack">soloTrack()</a><br>
<a href="#transpose">transpose()</a><br>
<a href="#transposeTrack">transposeTrack()</a><br>
<b>RTTL Class (entension/child class)</b><br>
<a href="#getRttl">getRttl()</a><br>
<a href="#importRttl">importRttl()</a><br>


<h3>Binary Input</h3>

<a name="importMid"><span class="func">importMid($smf_path [, $tn])</span></a><br>
imports standard MIDI file (SMF, *.mid) or RMID files (*.rmi) of type 0 or 1. If optional 2. parameter $tn is set, only track number $tn is imported (similar effect as soloTrack(), but much faster, as only one track is parsed).
<span class="sample">$midi->importMid('music/song1.mid');</span>

<h3>Text Input</h3>

<a name="importTxt"><span class="func">importTxt($txt)</span></a><br>
imports MIDI song in <a href="#mf2t">MF2T/T2MF</a> ASCII format (e.g. fetched from database). If a line "TimestampType=Delta" is found at the beginning of a track (next line after "MTrk"), all timestamps of this track are interpreted as relative (delta) values, otherwise as absolute values.
Relative timestamps and the "TimestampType=Delta" are not compatible with the original MF2T/T2MF format!
<span class="sample">$midi->importTxt($txt);</span>

<a name="importTrackTxt"><span class="func">importTrackTxt($txt [, $tn])</span></a><br>
imports MIDI track in <a href="#mf2t">MF2T/T2MF</a> format. If optional track number $tn is specified, track $tn is replaced, otherwise new track is appended. If a line "TimestampType=Delta" is found at the beginning of the track (next line after "MTrk"), all timestamps of this track are interpreted as relative (delta) values, otherwise as absolute values.
Relative timestamps and the "TimestampType=Delta" are not compatible with the original MF2T/T2MF format! 
<span class="sample">$midi->importTrackTxt($txt, 2);</span>

<h3>XML Input</h3>

<a name="importXml"><span class="func">importXML($txt)</span></a><br>
imports MIDI song in <a href="#xml">MIDI XML</a> format
<span class="sample">$midi->importXML($xmlStr);</span>

<h3>Binary Output</h3>

<a name="getMid"><span class="func">getMid()</span></a><br>
returns MIDI song as binary MIDI string (SMF).
<span class="sample">$smf = $midi->getMid();</span>

<a name="saveMidFile"><span class="func">saveMidFile($mid_path)</span></a><br>
saves MIDI song as standard MIDI file (SMF).
<span class="sample">$midi->saveMidFile('/tmp/song1.mid');</span>

<a name="playMidFile"><span class="func">playMidFile($file [,$visible=1,$autostart=1,$loop=1,$plug=''])</a></span><br>
embeds MIDI file in web page. Optional parameters for visibility, autostart and looping (1=yes,0=no). Optional parameter $plug determines preferred plugin, it can be 'bk' (beatnik),
'qt' (QuickTime) or 'wm' (Windows Media Player), if it's omitted or empty string (''), the default player for MIDI files is used (&lt;embed type="audio/midi"...&gt;).
<span class="sample">$midi->playMidFile($file);</span><br>
<span class="sample">$midi->playMidFile($file,0,1,0,'bk');</span>

<a name="downloadMidFile"><span class="func">downloadMidFile($output [,$file])</span></a><br>
if parameter $file is specified, starts download of file $file, otherwise the current midi song is downloaded from memory. $output is the proposed filename for saving.
<span class="sample">$midi->downloadMidFile('song1.mid', '/tmp/~tmp1.mid');</span>

<h3>Text Output</h3>

<a name="getTxt"><span class="func">getTxt([$ttype=0])</span></a><br>
returns MIDI song in <a href="#mf2t">MF2T/T2MF</a> format (e.g. to save to database). Optional Parameter $ttype determines timestamp type, if $ttype==0 (or omitted), the timestamps are absolute values and the result is compatible to the original MF2T/T2MF format, if $ttype==1, timestamps are relative values (not compatible to original MF2T/T2MF format):
As messages are internally stored with absolute timestamps, getTxt() is much faster than getTxt(1);
<span class="sample">$str = $midi->getTxt();</span>

<a name="getTrackTxt"><span class="func">getTrackTxt($tn[,$ttype=0])</span></a><br>
returns track number $tn in <a href="#mf2t">MF2T/T2MF</a> format. For optional parameter $ttype see <a href="#getTxt">getTxt()</a>.
<span class="sample">$str = $midi->getTrackTxt(2,1);</span>

<h3>XML Output</h3>

<a name="getXml"><span class="func">getXml([$ttype=0])</span></a><br>
returns MIDI song as <a href="#xml">MIDI XML</a>. Optional Parameter $ttype determines timestamp type, if $ttype==0 (or omitted), the timestamps are absolute values, if $ttype==1 timestamps are relative (delta) values. The output is valid XML according to the MIDI XML standard v0.9 by <a href="http://www.recordare.com/dtds/midixml.html">MusicXML/Recordare</a>.
As messages are internally stored with absolute timestamps, getXml() is much faster than getXml(1);
<span class="sample">$xmlStr = $midi->getXml();</span>


<a name="getTempo"><span class="func">getTempo()</span></a><br>
returns MIDI tempo value (0 if not set).

<a name="getBpm"><span class="func">getBpm()</span></a><br>
returns tempo as beats per minute (0 if tempo not set).

<a name="getTimebase"><span class="func">getTimebase()</span></a><br>
returns timebase value.

<a name="getTrackCount"><span class="func">getTrackCount()</span></a><br>
returns number of tracks.

<a name="getTrack"><span class="func">getTrack($tn)</span></a><br>
returns track number $tn as array of msg strings in <a href="#mf2t">MF2T/T2MF</a> format.
<span class="sample">$track = $midi->getTrack(2);</span>

<a name="getMsgCount"><span class="func">getMsgCount()</span></a><br>
returns number of messages of track number $tn.
<span class="sample">$msgCnt = $midi->getMsgCount(2);</span>

<a name="getMsg"><span class="func">getMsg($tn,$mn)</span></a><br>
returns message string number $mn of track number $tn. The message string is in <a href="#mf2t">MF2T/T2MF</a> format.
<span class="sample">$msgStr = $midi->getMsg(2, 23);</span>


<a name="open"><span class="func">open([$timebase=480])</span></a><br>
creates(resets) new empty MIDI song for further processing (eg. with newTrack() and addMsg()). Optional parameter $timebase to set its timebase (default = 480).
<span class="sample">$midi->open(240);</span>

<a name="newTrack"><span class="func">newTrack()</span></a><br>
adds new empty track, returns its track number.
<span class="sample">$newTrck = $midi->newTrack();</span>

<a name="addMsg"><span class="func">addMsg($tn, $msgStr[, $ttype=0])</span></a><br>
appends message string $msgStr to end of track $tn. If $ttype==0 or omitted, timestamp is interpreted as absolute value, if $ttype==1, it's interpreted as relative (delta) value. The message string has to be in <a href="#mf2t">MF2T/T2MF</a> format.
addMsg() is faster then insertMsg(), but it does not check for correct position according to time.
<span class="sample">$midi->addMsg(1, "4800 On ch=1 n=66 v=80");</span>

<a name="insertMsg"><span class="func">insertMsg($tn,$msgStr)</span></a><br>
adds message at adequate position of track number $tn (slower than addMsg).<br>
If other messages with same time are found, new message is inserted before
those.  The message string has to be in <a href="#mf2t">MF2T/T2MF</a> format.
<span class="sample">$midi->insertMsg(0, "0 Meta Text \"created by fluxus\"");</span>

<a name="deleteMsg"><span class="func">deleteMsg($tn,$mn)</span></a><br>
deletes message $mn of track $tn.

<a name="setBpm"><span class="func">setBpm($bpm)</span></a><br>
sets tempo corresponding to given beats per minute $bpm.

<a name="setTempo"><span class="func">setTempo($tempo)</span></a><br>
sets tempo by replacing set tempo msg in track 0 (or adding new track 0).

<a name="setTimebase"><span class="func">setTimebase($tb)</span></a><br>
sets timebase

<a name="deleteTrack"><span class="func">deleteTrack($tn)</span></a><br>
deletes track number $tn.

<a name="soloTrack"><span class="func">soloTrack($tn)</span></a><br>
deletes all tracks except track number $tn (and $track 0 which contains tempo info).

<a name="transpose"><span class="func">transpose($dn)</span></a><br>
transposes song by $dn (positive or negative) half tone steps.
<span class="sample">$midi->transpose(12); // 1 octave up</span>

<a name="transposeTrack"><span class="func">transposeTrack($tn, $dn)</span></a><br>
transposes all notes of track number $tn by $dn (positive or negative) half tone steps.
<span class="sample">$midi->transposeTrack(1, -12); // 1 octave down</span>


<a name="getDrumkitList"><span class="func">getDrumkitList()</span></a><br>
returns list of standard gm drum kit names (associative array, key=number, value=name).

<a name="getDrumset"><span class="func">getDrumset()</span></a><br>
returns list of gm drumset instrument names (associative array, key=number, value=name).

<a name="getInstrumentList"><span class="func">getInstrumentList()</span></a><br>
returns list of standard instrument names.

<a name="getNoteList"><span class="func">getNoteList()</span></a><br>
returns list of note names ('C0',...).

<h3>RTTL Extension</h3>

<a name="getRttl"><span class="func">getRttl([$title='', $tn=-1]])</span></a><br>
returns RTTL ringtone as string. If optional parameter $title is specified, this will be the RTTL name (max. 10 characters), if it's "" or omitted, the method looks for a TrkName meta event, and if there is none, name will be "mid2rttl". The optional parameter $tn determines the tracknumber that will be used, if it's <0 or omitted, the first track that contains note events will be used.
<span class="sample">$rttlStr = $rttl->getRttl();</span>

<a name="importRttl"><span class="func">importRttl($rttlStr)</span></a><br>
imports a RTTL ringtone as string (creates a new midi song of type 0).
<span class="sample">$rttl->importRttl($rttlStr);</span>

<h2><a name="mf2t">MF2T/T2MF text format</a></h2>
excerpt of readme.txt in <a href="ftp://ftp.cs.ruu.nl/pub/MIDI/PROGRAMS/MSDOS/mf2t.zip">mf2t.zip</a>
Format of the textfile:

File header:        Mfile &lt;format&gt; &lt;ntrks&gt; &lt;division&gt;
Start of track:     MTrk
End of track:       TrkEnd

Note On:            On &lt;ch&gt; &lt;note&gt; &lt;vol&gt;
Note Off:           Off &lt;ch&gt; &lt;note&gt; &lt;vol&gt;
Poly Pressure:      PoPr[PolyPr] &lt;ch&gt; &lt;note&gt; &lt;val&gt;
Channel Pressure:   ChPr[ChanPr] &lt;ch&gt; &lt;val&gt;
parameter:          Par[Param] &lt;ch&gt; &lt;con&gt; &lt;val&gt;
Pitch bend:         Pb &lt;ch&gt; &lt;val&gt;
Program change:     PrCh[ProgCh] &lt;ch&gt; &lt;prog&gt;
Sysex message:      SysEx &lt;hex&gt;

Sequence nr:        Seqnr &lt;num&gt;
Key signature:      KeySig &lt;num&gt; &lt;manor&gt;
Tempo:              Tempo &lt;num&gt;
Time signature:     TimeSig &lt;num&gt;/&lt;num&gt; &lt;num&gt; &lt;num&gt;
SMPTE event:        SMPTE &lt;num&gt; &lt;num&gt; &lt;num&gt; &lt;num&gt; &lt;num&gt;

Meta text events:   Meta &lt;texttype&gt; &lt;string&gt;
Meta end of track:  Meta TrkEnd
Sequencer specific: SeqSpec &lt;type&gt; &lt;hex&gt;
Misc meta events:   Meta &lt;type&gt; &lt;hex&gt;

The &lt;&gt; have the following meaning:

&lt;ch&gt;        ch=&lt;num&gt;
&lt;note&gt;      n=&lt;noteval&gt;  [note=&lt;noteval&gt;]
&lt;vol&gt;       v=&lt;num&gt; [vol=&lt;num&gt;]
&lt;val&gt;       v=&lt;num&gt; [val=&lt;num&gt;]
&lt;con&gt;       c=&lt;num&gt; [con=&lt;num&gt;]
&lt;prog&gt;      p=&lt;num&gt; [prog=&lt;num&gt;]
&lt;manor&gt;     minor or major
&lt;noteval&gt;   either a &lt;num&gt; or A-G optionally followed by #,
            followed by &lt;num&gt; without intermediate spaces.

&lt;texttype&gt;  Text Copyright SeqName TrkName InstrName Lyric Marker Cue or &lt;type&gt;
&lt;type&gt;      a hex number of the form 0xab
&lt;hex&gt;       a sequence of 2-digit hex numbers (without 0x) separated by space
&lt;string&gt;    a string between double quotes (like "text").

Channel numbers are 1-based, all other numbers are as they appear in the midifile.

&lt;division&gt;  is either a positive number (giving the time resolution in
            clicks per quarter note) or a negative number followed by a positive
            number (giving SMPTE timing).

&lt;format&gt; &lt;ntrks&gt; &lt;num&gt; are decimal numbers.

The &lt;num&gt; in the Pb is the real value (two midibytes combined).
In Tempo it is a long (32 bits) value. Others are in the interval 0-127.
The SysEx sequence contains the leading F0 and the trailing F7.

<h2><a name="xml">MIDI XML format</a></h2>
(for details see Standard MIDI File DTD by <a href="http://www.recordare.com/dtds/midixml.html">Recordare</a>)

<b>Supported elements:</b>

<b>Supported events:</b>
&lt;ProgramChange Channel="..." Number="..."/&gt;
&lt;NoteOn Channel="..." Note="..." Velocity="..."/&gt;
&lt;NoteOff Channel="..." Note="..." Velocity="..."/&gt;
&lt;PolyKeyPressure Channel="..." Note="..." Pressure="..."/&gt;
&lt;ControlChange Channel="..." Control="..." Value="..."/&gt;
&lt;ChannelKeyPressure Channel="..." Pressure="..."/&gt;
&lt;PitchBendChange Channel="..." Value="..."/&gt;
&lt;SequenceNumber Value="..."/&gt;
&lt;MIDIChannelPrefix Value="..."/&gt;
&lt;SetTempo Value="..."/&gt;
&lt;SMPTEOffset TimeCodeType="1" Hour="..." Minute="..." Second="..." Frame="..." FractionalFrame="..."&gt;&lt;/SMPTEOffset&gt;
&lt;TimeSignature Numerator="..." LogDenominator="..." MIDIClocksPerMetronomeClick="..." ThirtySecondsPer24Clocks="..."/&gt;
&lt;KeySignature Fifths="..." Mode="..."/&gt;

<b>Not (yet) supported elements:</b>
&lt;!-- for SMPTE time codes --&gt;

<b>Not (yet) supported events:</b>
&lt;XMFPatchTypePrefix Value="..."/&gt;
&lt;Port Value="..."/&gt;
&lt;OtherMetaEvent Number="..."&gt;...&lt;/OtherMetaEvent&gt;

Return current item: XRNS-PHP