bass - MIDI tick to millisecond? -
i realize there many questions here concerning converting midi ticks milliseconds (ex: how convert midi timeline actual timeline should played, midi ticks actual playback seconds !!! ( midi music), midi timestamp in seconds) , have looked @ them all, tried implement suggestions, still not getting it.
(did mention little "math phobic")
can me work practical example? using bass lib un4seen. have data need - don't trust calculations.
bass methods
tick
// position of midi stream uint64_t tick = bass_channelgetposition(midifilestream, bass_pos_midi_tick)
ppqn
//the pulses per quarter note (or ticks per beat) value of midi stream. float ppqn; bass_channelgetattribute(handle, bass_attrib_midi_ppqn, &ppqn);
tempo
//tempo in microseconds per quarter note. uint32_t tempo = bass_midi_streamgetevent( midifilestream, -1, midi_event_tempo);
my attempt @ calculating ms value tick:
float currentmilliseconds = tick * tempo / (ppqn * 1000);
the value appears correct don't have confidence in since not quite understanding formula.
printf("tick %llu\n",tick); printf("ppqn %f\n",ppqn); printf("tempo %u\n",tempo); printf("currentmilliseconds %f \n", currentmilliseconds);
example output:
tick 479 ppqn 24.000000 tempo 599999 currentmilliseconds 11974.980469
update
my confusion continues based on blog post think have code right – @ least output seems accurate. conversely, answer provided @strikeskids below yields different results. maybe have order of operations problem in there?
float kmillisecondsperquarternote = tempo / 1000.0f; float kmillisecondspertick = kmillisecondsperquarternote / ppqn; float deltatimeinmilliseconds = tick * kmillisecondspertick; printf("deltatimeinmilliseconds %f \n", deltatimeinmilliseconds);
.
float currentmillis = tick * 60000.0f / ppqn / tempo; printf("currentmillis %f \n", currentmillis);
output:
deltatimeinmilliseconds 11049.982422 currentmillis 1.841670
tempo in beats per minute. because want getting time, should have in denominator of fraction.
currenttime = currenttick * (beats / tick) * (minutes / beat) * (millis / minute)
millis = tick * (1/ppqn) * (1/tempo) * (1000*60)
to use integer arithmetic efficiently do
currentmillis = tick * 60000 / ppqn / tempo
wiki
Comments
Post a Comment