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

Home > 利用方

利用方

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

例の test.mmf は、CNTI、OPDA (曲名 ST と作曲者 SW のタグ)、Handy Phone Standard のスコアトラック 1 つ (音色・バンクの指定と 2 つの音) の SMAF です。 composer で入れた場合は require_once 'IO/SMAF.php'; の代わりに require 'vendor/autoload.php'; と書きます。

読む

チャンクの一覧、タグ、イベントを表示する例です。Duration を足していくと、トラックの先頭からの時間になります。

require_once 'IO/SMAF.php';

$smaf = new IO_SMAF();
$smaf->parse(file_get_contents('test.mmf'));

echo $smaf->headers['ID'].' '.$smaf->headers['Size'].PHP_EOL;
foreach ($smaf->chunks as $chunk) {
    echo $chunk['ID'].' ('.$chunk['Size'].')'.PHP_EOL;
    if ($chunk['ID'] === 'OPDA') {
        foreach ($chunk['Chunks'] as $dch) {           // Dch*
            foreach ($dch['Tags'] as $tag) {
                echo "  {$tag['ID']}: {$tag['Data']}".PHP_EOL;
            }
        }
    }
    if ($chunk['ID'] === 'MTR') {
        printf("  Format Type:%d Timebase D:0x%02x G:0x%02x\n",
               $chunk['Format Type'], $chunk['Timebase D'], $chunk['Timebase G']);
        foreach ($chunk['Chunks'] as $sub) {
            if ($sub['ID'] !== 'Mtsq') {
                continue;
            }
            $time = 0;
            foreach ($sub['Tracks'] as $ev) {
                $time += $ev['Duration'];
                $e = $ev['event'];
                if ($e['Type'] === 'Note Message') {
                    echo "    t=$time note gate={$e['GetTime']}".PHP_EOL;
                } else {
                    echo "    t=$time ch={$e['Channel']} {$e['Type']} {$e['Value']}".PHP_EOL;
                }
            }
        }
    }
}
MMMD 89
CNTI (5)
OPDA (26)
  ST: Test
  SW: Author
MTR (32)
  Format Type:0 Timebase D:0x02 G:0x02
    t=0 ch=0 Program change 5
    t=0 ch=0 Bank Select 2
    t=0 note gate=48
    t=48 note gate=48

ダンプする

require_once 'IO/SMAF.php';

$smaf = new IO_SMAF();
$smaf->parse(file_get_contents('test.mmf'));
$smaf->dump();

出力はサンプルの smafdump.php と同じです (下)。

読めないファイル

途中で切れたデータなどでは、IO_Bit の IO_Bit_Exception (Exception の子) がそのまま投げられます。次は、OPDA に直接テキストが入った ffmpeg の mmf 出力を読んだ例です。

require_once 'IO/SMAF.php';

$data = file_get_contents('ffmpeg.mmf');
$smaf = new IO_SMAF();
try {
    $smaf->parse($data);
} catch (Exception $e) {
    echo get_class($e).': '.$e->getMessage().PHP_EOL;
}
IO_Bit_Exception: getData: 16 < 8 + 1635149366

サンプル

smafdump.php

構造をダンプします。引数はファイル名だけです。

% php sample/smafdump.php test.mmf
Header:
  ID:MMMD
  Size:89
Body:
  ID:CNTI
  Size:5
  Contents Class:0
  Contents Type:0
  Contents Code Type:0
  Copy Status:0
  Copy Counts:0
  ID:OPDA
  Size:26
  Chunks:
    ID:Dch
    CodeType:0
    Size:18
    Tags:
      ID:ST Size:4 Data:Test 
      ID:SW Size:6 Data:Author 
  ID:MTR
  TrackNumber:0
  Size:32
  Format Type:0
  Sequence Type:0
  Timebase D:2
  Timebase G:2
  Channnel Status:0
  Chunks:
    ID:Mtsq
    Size:18
    Tracks:
      Duration:0 event:{Channel:0 Type:Program change Value:5} 
      Duration:0 event:{Channel:0 Type:Bank Select Value:2} 
      Duration:0 event:{Type:Note Message GetTime:48} 
      Duration:48 event:{Type:Note Message GetTime:48} 

Clone this wiki locally