Skip to content

Commit 28a98cc

Browse files
Merge pull request #482 from wikimedia/upstream/midi-duration
MIDI: Fix duration for files without tempo events
2 parents 4bf44f6 + 1b4d86d commit 28a98cc

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

getid3/module.audio.midi.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,15 @@ public function Analyze() {
330330
}
331331
}
332332

333+
// Fallback: If playtime_seconds is still empty/zero, estimate using default tempo
334+
// 120 BPM = 0.5 per quarter note = 480 quarternote ticks
335+
if (empty($info['playtime_seconds']) && !empty($thisfile_midi['totalticks'])) {
336+
$ticksPerQuarterNote = isset($thisfile_midi_raw['ticksperqnote']) ? $thisfile_midi_raw['ticksperqnote'] : 480;
337+
if ($ticksPerQuarterNote > 0) {
338+
$totalQuarterNotes = $thisfile_midi['totalticks'] / $ticksPerQuarterNote;
339+
$info['playtime_seconds'] = $totalQuarterNotes * 0.5;
340+
}
341+
}
333342

334343
if (!empty($info['playtime_seconds'])) {
335344
$info['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];

0 commit comments

Comments
 (0)