Skip to content
Yoshihiro Yamazaki edited this page Nov 27, 2026 · 1 revision

Home > 利用方

利用方

配列のキーは 設計、メソッドの一覧は 機能 にあります。

例の test.mld は、MFi2 (データタイプ 2.1)、1 トラックのファイルです。テンポ (タイムベース 48、テンポ 120)、音色、音量に続いて、C・D・E の 4 分音符 (音の強さつき) と End of Track が入っています。 composer で入れた場合は require_once 'IO/MFi.php'; の代わりに require 'vendor/autoload.php'; と書きます。

読む

ヘッダとデータインフォメーションを表示する例です。

require_once 'IO/MFi.php';

$mfi = new IO_MFi();
$mfi->parse(file_get_contents('test.mld'));

$h = $mfi->headers;
echo "Type:{$h['DataTypeMajor']}.{$h['DataTypeMinor']} Tracks:{$h['NumberOfTrack']}\n";
foreach ($mfi->datainfos as $info) {
    echo $info['Identifer'].": ".$info['Data']."\n";
}
Type:2.1 Tracks:1
titl: test
sorc: 0
note: 1

ノートを並べる

ノートメッセージの時刻 (秒)、ボイス、音名、音の長さを表示する例です。 時刻は、テンポのメッセージ (FF Cx) の下位 4 ビットのタイムベースとテンポの値から、1 Δタイム = (60 / テンポ) / タイムベース 秒で計算します。

require_once 'IO/MFi.php';

$mfi = new IO_MFi();
$mfi->parse(file_get_contents('test.mld'));

// timebase of the tempo message (FF Cx), indexed by the low 4 bits of x
$timebaseTable = array(6, 12, 24, 48, 96, 192, 384, null, 15, 30, 60, 120, 240, 480, 960, null);
$timebase = 48;
$tempo = 120;  // used until the first tempo message
foreach ($mfi->tracks as $track) {
    $time = 0;
    foreach ($track['Events'] as $event) {
        $time += $event['DeltaTime'];
        if ($event['StatusInfo'] == 0xFF) {  // extended status
            if (($event['StatusInfo2'] & 0xF0) == 0xC0) {  // tempo
                $timebase = $timebaseTable[$event['StatusInfo2'] & 0x0F];
                $tempo = $event['Data'];
            }
            continue;
        }
        $sec = $time * 60 / $tempo / $timebase;
        $voice = $event['StatusInfo'] >> 6;
        $note = $mfi->noteTable[$event['StatusInfo'] & 0x3F];
        printf("%.2fs voice:%d note:%s length:%d", $sec, $voice, $note, $event['Data']);
        if (isset($event['Data2'])) {  // MFi2: velocity and octave shift
            printf(" velocity:%d octave:%d", $event['Data2'] >> 2, $event['Data2'] & 3);
        }
        echo "\n";
    }
}
0.00s voice:0 note:C length:48 velocity:63 octave:0
0.50s voice:0 note:D length:48 velocity:48 octave:0
1.00s voice:0 note:E length:48 velocity:32 octave:1

エラーを扱う

壊れたデータでは parse() が例外を投げます。IO_Bit_Exception も Exception の子クラスなので、まとめて受けられます。 例の broken.mld は test.mld の先頭 40 バイトです。

require_once 'IO/MFi.php';

$mfi = new IO_MFi();
try {
    $mfi->parse(file_get_contents('broken.mld'));
} catch (Exception $e) {  // IO_Bit_Exception too
    echo get_class($e).": ".$e->getMessage()."\n";
}
Exception: FileLength(66) is not real FileLength(40) - 8

ダンプする

dump() はプロパティの中身を表示します。sample/mfidump.php はこれを呼ぶだけのスクリプトです。

% php vendor/yoya/io_mfi/sample/mfidump.php test.mld
Header Part:
  Identifier: melo
  FileLength: 66
  OffsetToTrack: 28
  DataTypeMajor: 2 (music data MFi2)
  DataTypeMinor: 1 (whole)
  NumberOfTrack: 1
Data Information Part:
  [0] titl (len:4) Data: test
  [1] sorc (len:1) Data: 0
  [2] note (len:2) Data: 1
Track Part
  [0]
    Identifer: trac
    Length: 28
    Event:
      [0]  DeltaTime: 0  StatusInfo: 255  195  Data: 120
      [1]  DeltaTime: 0  StatusInfo: 255  224  Data: 0
      [2]  DeltaTime: 0  StatusInfo: 255  226  Data: 63
      [3]  DeltaTime: 0  StatusInfo: 27  Data: 48 252
             Voice: 0  Note: 27(C)  Length:48
             Velocity: 63  OctaveShift: 0
      [4]  DeltaTime: 48  StatusInfo: 29  Data: 48 192
             Voice: 0  Note: 29(D)  Length:48
             Velocity: 48  OctaveShift: 0
      [5]  DeltaTime: 48  StatusInfo: 31  Data: 48 129
             Voice: 0  Note: 31(E)  Length:48
             Velocity: 32  OctaveShift: 1
      [6]  DeltaTime: 48  StatusInfo: 255  223  Data: 0

ファイルを指定しないと使い方を、読めないデータではエラーのメッセージを STDERR に出し、終了コード 1 で終わります。

% php vendor/yoya/io_mfi/sample/mfidump.php broken.mld
FileLength(66) is not real FileLength(40) - 8

Clone this wiki locally