MIDI: Convert BPM to FPS delta time? - midi

Given a BPM (beats per minute) MIDI delta time (leftmost bit off) with speed of 192:
0x00C0
I want to convert it to a FPS/TPM (frames per second / ticks per minute) delta time (leftmost bit on), but it should be the same (or the most accurate) speed value if you know what I mean.
For more info about MIDI Delta Time please take a look at Midi File Format under Header Chunk -> Time Division.
I am looking for a formula that will convert between these two deltatime types.

If you're talking about 0x00C0 being the time division field, what you're referring to is not 192 beats per minute, but rather 192 ticks per beat, quite a different beast. BPM is specified indirectly via "Set Tempo" events, given in microseconds per beat (with the lamentably ubiquitous 120 BPM being assumed to begin with). The trickiness of time division with this format is that the length of a tick will grow and shrink depending on the tempo changes in the song.
Let's say the time division you want to convert to has F as the frames per second (24, 25, 29.97, or 30) and G as the ticks per frame (note: it's not ticks per minute!). Further, let's assume that the current tempo in microseconds per beat is p. Then the formula to convert a given duration in ticksold to ticksnew (the unit analysis really helps!) is:
y = x ticksold * (1/192) beat/ticksold * p μsec/beat * (1/106) sec/μsec * F frames/sec * G ticksnew/frame
= ((x * p * F * G)/(192*106)) ticksnew

Related

How do I make a clock second hand move with 4 jumps/ticks per second?

EDIT: Needed help to get exactly 3 ticks per second, but that's not possible because of fractions. Double checking the real life clock I realized it actually moves 4 ticks per second, I'm thinking a quarter second is even and should be able to be exact. Updated the question since I can't get that to work either:
I'm building a clock that has three options of how the second hand will move. I've successfully made the seconds move 6° per second (1 "tick" per second) and one option with a seamless sweep (0.006° per millisecond). But I can't get my formula to work for my third option: having the second hand move exactly 4 times per second, i.e. 1.5° every quarter of a second).
This is the line of code for the second hand (I use CGAffiateTransform later, hence the radians):
let quarterSecond = round(millisecond * 4.0 / 1000.0)
let tickingSeconds = (((1.5 * π / 180) * quarterSecond) + ((6.0 * π / 180) * second))
This does what I want but I don't like the round as it makes the movement inexact. The whole timer interval is set to 0.001 and I have a hard time believing that you can't make the second hand move precisely 4 "ticks" per second without doing some uneven jerks every now and then.
Any ideas?
What you intuitively think as ticking is how often your expression changes value.
Here, your entire expression is constant except for second. Which means that your expression can only change when second change. If it is an integer, it can only change every second.
You need a variable that changes thrice a second, you can approximate that using millisecond and a truncation (to remove the fractional part):
let thrice_a_second = (millisecond * 3.0 / 1000.0).round()
Only then you can use that expression to compute the new position, knowing that it increases by 1 thrice a second.

Operating system- Time Slice

Suppose a multiprogramming operating system allocated time slices of 10 milliseconds and the machine executed an average of five instructions per nanosecond.
How many instructions could be executed in a single time slice?
please help me, how to do this.
This sort of question is about cancelling units out after finding the respective ratios.
There are 1,000,000 nanoseconds (ns) per 1 millisecond (ms) so we can write the ratio as (1,000,000ns / 1ms).
There are 5 instructions (ins) per 1 nanosecond (ns) so we can write the ratio as (5ins / 1ns).
We know that the program runs for 10ms.
Then we can write the equation like so such that the units cancel out:
instructions = (10ms) * (1,000,000ns/1ms) * (5ins/1ns)
instructions = (10 * 1,000,000ns)/1 * (5ins/1ns) -- milliseconds cancel
instructions = (10,000,000 * 5ins)/1 -- nanoseconds cancel
instructions = 50,000,000ins -- left with instructions
We can reason that it is at least the 'right kind' of ratio setup - even if the ratios are wrong or whatnot - because units we are left with instructions, which is matches the type of unit expected in the answer.
In the above I started with the 1,000,000ns/1ms ratio, but it could also have done 1,000,000,000ns/1,000ms (= 1 second / 1 second) and ended with the same result.

What is the time taken by Quicksort?

If the minimum time taken by quicksort algorithm to sort 1000 elements is 100 seconds, what will be the minimum time taken by it to sort 100 elements?
The correct answer is that we don't know. The O(N log N) behaviour only describes the highest order part of the time dependency.
If we assume that the implementation we are looking at follows time = k * N * log N (that is, we assume that there are no lower order parts), then the answer would be:
100 * 100 / 1000 * log 100 / log 1000 = 20/3 or approx 6.7 seconds
average time complexity of quick sort is O(n log n) (also best case). if it takes 100 sec for 1000 elements, and will take 6.7 sec for 100 elements.

How to show GPS coordinates in a specific format?

I am trying to show the location coordinates in specific format like 42° 51’36.712032" N, 112° 25’ 45.069804" W (example of geographical coordinates). How can i implement this.
Thanks in advance.
From Wikipedia:
Each degree of longitude is sub-divided into 60 minutes, each of which is divided into 60 seconds. A longitude is thus specified in sexagesimal notation as 23° 27′ 30" E. For higher precision, the seconds are specified with a decimal fraction.
Thus, take the fractional part of your coordinate and multiply it by 3600 (60×60) to get the total number of seconds; to convert that to the minutes/seconds that you want, take the total seconds mod 60 to get the actual seconds, subtract that number from the fractional part you got earlier, and then divide the result by 60 to get the number of minutes.

Converting MIDI ticks to actual playback seconds

I want to know how to convert MIDI ticks to actual playback seconds.
For example, if the MIDI PPQ (Pulses per quarter note) is 1120, how would I convert it into real world playback seconds?
The formula is 60000 / (BPM * PPQ) (milliseconds).
Where BPM is the tempo of the track (Beats Per Minute).
(i.e. a 120 BPM track would have a MIDI time of (60000 / (120 * 192)) or 2.604 ms for 1 tick.
If you don't know the BPM then you'll have to determine that first. MIDI times are entirely dependent on the track tempo.
You need two pieces of information:
PPQ (pulses per quarter note), which is defined in the header of a midi file, once.
Tempo (in microseconds per quarter note), which is defined by "Set Tempo" meta events and can change during the musical piece.
Ticks can be converted to playback seconds as follows:
ticks_per_quarter = <PPQ from the header>
µs_per_quarter = <Tempo in latest Set Tempo event>
µs_per_tick = µs_per_quarter / ticks_per_quarter
seconds_per_tick = µs_per_tick / 1.000.000
seconds = ticks * seconds_per_tick
Note that PPQ is also called "division" or "ticks per quarter note" in the document linked above.
Note that Tempo is commonly represented in BPM (a frequency) but raw MIDI represents it in µs per quarter (a period).