Skip to content

Commit

Permalink
display measure (from time signature)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoya committed Mar 14, 2015
1 parent e40d791 commit 3154106
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
21 changes: 19 additions & 2 deletions IO/MIDI.php
Expand Up @@ -14,6 +14,10 @@ class IO_MIDI {
var $xfkaraoke = null;
var $_mididata = null;
var $_scaleCharactor = null;
var $_measure1 = 4;
var $_measure2 = 4;
var $_timeInMeasure = 0;
var $_measureSeqno = 1;

function parse($mididata) {
$this->_mididata = $mididata;
Expand Down Expand Up @@ -398,7 +402,16 @@ function dump($opts = array()) {
$bitio->hexdump($track['_offset'], 8);
}
foreach ($track['track'] as $idx2 => $chunk) {
echo " [$idx2]:";
if (empty($opts['measure']) === false) {
$deltaTime = $chunk['DeltaTime'];
$this->_timeInMeasure -= $deltaTime; // XXX
if ($this->_timeInMeasure <= 0) {
echo "=== Measure: {$this->_measureSeqno}\n";
$this->_timeInMeasure = $this->header['header']['Division'] * $this->_measure1 * (4 / $this->_measure2);
$this->_measureSeqno++;
}
}
echo " [$idx2]:";
foreach ($chunk as $key => $value) {
switch ($key) {
case 'EventType':
Expand Down Expand Up @@ -444,7 +457,11 @@ function dump($opts = array()) {
echo $value;
break;
case 0x58: // time signature
printf("%x/%x", ord($value{0}), pow( 2, ord($value{1}) ) );
$measure1 = ord($value{0});
$measure2 = pow( 2, ord($value{1}));
echo "$measure1/$measure2";
$this->_measure1 = $measure1;
$this->_measure2 = $measure2;
break;
default:
break;
Expand Down
5 changes: 4 additions & 1 deletion sample/mididump.php
Expand Up @@ -2,7 +2,7 @@

require_once 'IO/MIDI.php';

$options = getopt("f:hv");
$options = getopt("f:hvm");

if ((isset($options['f']) === false) || (is_readable($options['f']) === false)) {
fprintf(STDERR, "Usage: php mididump.php -f <midi_file> [-h]\n");
Expand All @@ -22,5 +22,8 @@
if (isset($options['v'])) {
$opts['verbose'] = true;
}
if (isset($options['m'])) { // 小節
$opts['measure'] = true;
}

$midi->dump($opts);

0 comments on commit 3154106

Please sign in to comment.