Debian Codegraph @

tests/midifiles/test_units.py

raw symbols SCIP
1 # SPDX-FileCopyrightText: 2017 Ole Martin Bjorndalen <[email protected]>
2 #
3 # SPDX-License-Identifier: MIT
4
5 from mido.midifiles.units import bpm2tempo, second2tick, tempo2bpm, tick2second
6
7
8 def test_tempo2bpm():
9 # default tempo (500000 ms per quarter note)
10 assert tempo2bpm(500000) == 120
11 assert tempo2bpm(1000000) == 60
12 # x/4 time signature: 4 beats (quarter notes) per bar
13 # 60 bpm: 1 beat / sec = 1 quarter note / sec: 1 sec / quarter note
14 assert tempo2bpm(1000000, time_signature=(4, 4)) == 60
15 # 120 bpm: 2 beat / sec = 2 quarter note / sec: 0.5 sec / quarter note
16 assert tempo2bpm(500000, time_signature=(4, 4)) == 120
17 assert tempo2bpm(500000, time_signature=(3, 4)) == 120 # 3/4 is the same
18 # x/2 time signature: 2 beats (half notes) per bar
19 # 60 bpm: 1 beat / sec = 2 quarter note / sec: 0.5 sec / quarter note
20 assert tempo2bpm(500000, time_signature=(2, 2)) == 60
21 # 120 bpm: 2 beat / sec = 4 quarter note / sec: 0.25 sec / quarter note
22 assert tempo2bpm(250000, time_signature=(2, 2)) == 120
23 assert tempo2bpm(250000, time_signature=(3, 2)) == 120 # 3/2 is the same
24 # x/8 time signature: 8 beats (eighth notes) per bar
25 # 60 bpm: 1 beat / sec = 0.5 quarter note / sec: 2 sec / quarter note
26 assert tempo2bpm(2000000, time_signature=(8, 8)) == 60
27 # 120 bpm: 2 beat / sec = 1 quarter note / sec: 1 sec / quarter note
28 assert tempo2bpm(1000000, time_signature=(8, 8)) == 120
29 assert tempo2bpm(1000000, time_signature=(8, 8)) == 120 # 6/8 is the same
30
31
32 def test_bpm2tempo():
33 # default tempo (500000 ms per quarter note)
34 assert bpm2tempo(60) == 1000000
35 assert bpm2tempo(120) == 500000
36 # x/4 time signature: 4 beats (quarter notes) per bar
37 # 60 bpm: 1 beat / sec = 1 quarter note / sec: 1 sec / quarter note
38 assert bpm2tempo(60, time_signature=(4, 4)) == 1000000
39 # 120 bpm: 2 beat / sec = 2 quarter note / sec: 0.5 sec / quarter note
40 assert bpm2tempo(120, time_signature=(4, 4)) == 500000
41 assert bpm2tempo(120, time_signature=(3, 4)) == 500000 # 3/4 is the same
42 # x/2 time signature: 2 beats (half notes) per bar
43 # 60 bpm: 1 beat / sec = 2 quarter note / sec: 0.5 sec / quarter note
44 assert bpm2tempo(60, time_signature=(2, 2)) == 500000
45 # 120 bpm: 2 beat / sec = 4 quarter note / sec: 0.25 sec / quarter note
46 assert bpm2tempo(120, time_signature=(2, 2)) == 250000
47 assert bpm2tempo(120, time_signature=(3, 2)) == 250000 # 3/2 is the same
48 # x/8 time signature: 8 beats (eighth notes) per bar
49 # 60 bpm: 1 beat / sec = 0.5 quarter note / sec: 2 sec / quarter note
50 assert bpm2tempo(60, time_signature=(8, 8)) == 2000000
51 # 120 bpm: 2 beat / sec = 1 quarter note / sec: 1 sec / quarter note
52 assert bpm2tempo(120, time_signature=(8, 8)) == 1000000
53 assert bpm2tempo(120, time_signature=(8, 8)) == 1000000 # 6/8 is the same
54
55
56 # TODO: these tests could be improved with better test values such as
57 # edge cases.
58 def test_tick2second():
59 # default tempo (500000 ms per quarter note)
60 assert tick2second(1, ticks_per_beat=100, tempo=500000) == 0.005
61 assert tick2second(2, ticks_per_beat=100, tempo=100000) == 0.002
62
63
64 def test_second2tick():
65 # default tempo (500000 ms per quarter note)
66 assert second2tick(0.001, ticks_per_beat=100, tempo=500000) == 0
67 assert second2tick(0.004, ticks_per_beat=100, tempo=500000) == 1
68 assert second2tick(0.005, ticks_per_beat=100, tempo=500000) == 1
69 assert second2tick(0.0015, ticks_per_beat=100, tempo=100000) == 2
70 assert second2tick(0.0025, ticks_per_beat=100, tempo=100000) == 2