77 readVariableLengthQuantity
88} from "../utils/byte_functions.js" ;
99import { arrayToHexString , consoleColors , formatTitle } from '../utils/other.js'
10+ import { SpessaSynthGroupCollapsed , SpessaSynthGroupEnd , SpessaSynthInfo } from '../utils/loggin.js'
1011
1112/**
1213 * midi_loader.js
@@ -19,7 +20,7 @@ export class MIDI{
1920 * @param fileName {string} optional, replaces the decoded title if empty
2021 */
2122 constructor ( arrayBuffer , fileName = "" ) {
22- console . groupCollapsed ( `%cParsing MIDI File...` , consoleColors . info ) ;
23+ SpessaSynthGroupCollapsed ( `%cParsing MIDI File...` , consoleColors . info ) ;
2324
2425 const fileByteArray = new ShiftableByteArray ( arrayBuffer ) ;
2526 const headerChunk = this . readMIDIChunk ( fileByteArray ) ;
@@ -233,14 +234,14 @@ export class MIDI{
233234 {
234235 const decoded = decoder . decode ( messageData . slice ( 7 , messageData . length - 3 ) ) + "\n" ;
235236 this . copyright += decoded ;
236- console . info ( `%cDecoded Roland SC message! %c${ decoded } ` ,
237+ SpessaSynthInfo ( `%cDecoded Roland SC message! %c${ decoded } ` ,
237238 consoleColors . recognized ,
238239 consoleColors . value )
239240 }
240241 }
241242 }
242243 this . tracks . push ( track ) ;
243- console . info ( `%cParsed %c${ this . tracks . length } %c / %c${ this . tracksAmount } ` ,
244+ SpessaSynthInfo ( `%cParsed %c${ this . tracks . length } %c / %c${ this . tracksAmount } ` ,
244245 consoleColors . info ,
245246 consoleColors . value ,
246247 consoleColors . info ,
@@ -260,10 +261,10 @@ export class MIDI{
260261 }
261262 this . firstNoteOn = Math . min ( ...firstNoteOns ) ;
262263
263- console . info ( `%cMIDI file parsed. Total tick time: %c${ this . lastVoiceEventTick } ` ,
264+ SpessaSynthInfo ( `%cMIDI file parsed. Total tick time: %c${ this . lastVoiceEventTick } ` ,
264265 consoleColors . info ,
265266 consoleColors . recognized ) ;
266- console . groupEnd ( ) ;
267+ SpessaSynthGroupEnd ( ) ;
267268
268269 if ( loopStart !== null && loopEnd === null )
269270 {
@@ -327,6 +328,12 @@ export class MIDI{
327328
328329 // reverse the tempo changes
329330 this . tempoChanges . reverse ( ) ;
331+
332+ /**
333+ * The total playback time, in seconds
334+ * @type {number }
335+ */
336+ this . duration = this . _ticksToSeconds ( this . lastVoiceEventTick ) ;
330337 }
331338
332339 /**
@@ -347,4 +354,24 @@ export class MIDI{
347354 fileByteArray . currentIndex += chunk . size ;
348355 return chunk ;
349356 }
357+
358+
359+ /**
360+ * Coverts ticks to time in seconds
361+ * @param ticks {number}
362+ * @returns {number }
363+ * @private
364+ */
365+ _ticksToSeconds ( ticks )
366+ {
367+ if ( ticks <= 0 ) {
368+ return 0 ;
369+ }
370+
371+ // find the last tempo change that has occured
372+ let tempo = this . tempoChanges . find ( v => v . ticks < ticks ) ;
373+
374+ let timeSinceLastTempo = ticks - tempo . ticks ;
375+ return this . _ticksToSeconds ( ticks - timeSinceLastTempo ) + ( timeSinceLastTempo * 60 ) / ( tempo . tempo * this . timeDivision ) ;
376+ }
350377}
0 commit comments