DEV Community

Vicente G. Reyes
Vicente G. Reyes

Posted on

Getting the tempo of a song using python

Hey! Long time no post here on DEV. I've been dealing with my mental health issue for a while. I was diagnosed with Bipolar-mixed with Depression and Anxiety a while back after a village hacking group played with me(they're still on the run but are currently under surveillance).

Anyway, here's a quick post to check on how to display the tempo of an audio using librosa. You can check the ipython notebook in google colab if you wish to.

I'm using our song We are Monsters. Check it out if you like it.

You can follow the installation process in their documentation or follow this article 😉

Install and import librosa

python -m pip install librosa
import librosa
Enter fullscreen mode Exit fullscreen mode

Upload and load the audio file in the notebook as a waveform in y and store the sample rate as sr

audio_file = librosa.load('We Are Monsters.wav')
y, sr = audio_file
Enter fullscreen mode Exit fullscreen mode

Then load the tempo tracker to get the estimated tempo of the track

tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr)
print('Estimated tempo: {:.2f} beats per minute'.format(tempo))
Enter fullscreen mode Exit fullscreen mode

Estimated tempo: 161.50 beats per minute

We then convert the beat frames into timings and get the estimated tempo of the track

beat_times = librosa.frames_to_time(beat_frames, sr=sr)
beat_times
Enter fullscreen mode Exit fullscreen mode

Output:

array([ 1.39319728, 1.76471655, 2.15945578, 2.5077551 ,
2.85605442, 3.27401361, 3.66875283, 4.04027211,
4.41179138, 4.78331066, 5.15482993, 5.50312925,
5.87464853, 6.2461678 , 6.59446712, 6.96598639,
7.31428571, 7.68580499, 8.05732426, 8.42884354,
8.80036281, 9.17188209, 9.54340136, 9.91492063,
10.26321995, 10.61151927, 10.98303855, 11.33133787,
11.70285714, 12.07437642, 12.42267574, 12.77097506,
13.14249433, 13.4675737 , 13.81587302, 14.16417234,
14.51247166, 14.86077098, 15.20907029, 15.55736961,
15.92888889, 16.32362812, 16.69514739, 17.06666667,
17.4614059 , 17.83292517, 18.20444444, 18.57596372,
18.94748299, 19.31900227, 19.69052154, 20.06204082,
20.43356009, 20.82829932, 21.22303855, 21.59455782,
21.9660771 , 22.33759637, 22.70911565, 23.05741497,
23.40571429, 23.77723356, 24.14875283, 24.49705215,
24.86857143, 25.21687075, 25.56517007, 25.91346939,
26.28498866, 26.63328798, 26.9815873 , 27.32988662,
27.67818594, 28.02648526, 28.37478458, 28.7230839 ,
29.11782313, 29.4893424 , 29.86086168, 30.23238095,
30.60390023, 30.9754195 , 31.34693878, 31.71845805,
32.08997732, 32.4614966 , 32.83301587, 33.18131519,
33.55283447, 33.9475737 , 34.31909297, 34.69061224,
35.06213152, 35.45687075, 35.82839002, 36.1999093 ,
36.59464853, 36.9661678 , 37.33768707, 37.7324263 ,
38.10394558, 38.47546485, 38.87020408, 39.2185034 ,
39.59002268, 39.96154195, 40.30984127, 40.65814059,
41.00643991, 41.35473923, 41.70303855, 42.05133787,
42.39963719, 42.74793651, 43.09623583, 43.44453515,
43.79283447, 44.14113379, 44.48943311, 44.83773243,
45.18603175, 45.53433107, 45.85941043, 46.20770975,
46.55600907, 46.90430839, 47.25260771, 47.60090703,
47.94920635, 48.29750567, 48.64580499, 48.99410431,
49.34240363, 49.69070295, 50.03900227, 50.38730159,
50.73560091, 51.08390023, 51.4554195 , 51.80371882,
52.15201814, 52.50031746, 52.84861678, 53.1969161 ,
53.54521542, 53.91673469, 54.26503401, 54.63655329,
55.03129252, 55.40281179, 55.79755102, 56.16907029,
56.54058957, 56.9353288 , 57.28362812, 57.65514739,
58.02666667, 58.39818594, 58.76970522, 59.14122449,
59.53596372, 59.93070295, 60.30222222, 60.6737415 ,
61.04526077, 61.41678005, 61.76507937, 62.11337868,
62.48489796, 62.85641723, 63.22793651, 63.62267574,
63.99419501, 64.36571429, 64.73723356, 65.08553288,
65.45705215, 65.82857143, 66.2000907 , 66.57160998,
66.94312925, 67.31464853, 67.66294785, 68.03446712,
68.40598639, 68.77750567, 69.12580499, 69.49732426,
69.86884354, 70.26358277, 70.63510204, 71.00662132,
71.37814059, 71.74965986, 72.09795918, 72.4462585 ,
72.79455782, 73.14285714, 73.49115646, 73.86267574,
74.21097506, 74.55927438, 74.9075737 , 75.25587302,
75.60417234, 75.95247166, 76.30077098, 76.64907029,
76.99736961, 77.34566893, 77.69396825, 78.04226757,
78.39056689, 78.73886621, 79.08716553, 79.43546485,
79.76054422, 80.10884354, 80.45714286, 80.80544218,
81.1537415 , 81.50204082, 81.85034014, 82.19863946,
82.54693878, 82.8952381 , 83.24353741, 83.59183673,
83.94013605, 84.28843537, 84.63673469, 84.98503401,
85.33333333, 85.68163265, 86.02993197, 86.37823129,
86.72653061, 87.12126984, 87.49278912, 87.86430839,
88.23582766, 88.60734694, 88.95564626, 89.30394558,
89.6522449 , 90.00054422, 90.34884354, 90.72036281,
91.06866213, 91.41696145, 91.83492063, 92.25287982,
92.64761905, 93.01913832, 93.3906576 , 93.76217687,
94.13369615, 94.50521542, 94.87673469, 95.24825397,
95.61977324, 95.99129252, 96.38603175, 96.75755102,
97.12907029, 97.50058957, 97.87210884, 98.24362812,
98.63836735, 99.00988662, 99.3814059 , 99.77614512,
100.1476644 , 100.51918367, 100.86748299, 101.23900227,
101.61052154, 102.00526077, 102.37678005, 102.74829932,
103.11981859, 103.46811791, 103.83963719, 104.21115646,
104.58267574, 104.95419501, 105.32571429, 105.69723356,
106.06875283, 106.46349206, 106.83501134, 107.20653061,
107.57804989, 107.92634921, 108.29786848, 108.66938776,
109.06412698, 109.43564626, 109.80716553, 110.17868481,
110.55020408, 110.92172336, 111.29324263, 111.68798186,
112.08272109, 112.45424036, 112.82575964, 113.19727891,
113.56879819, 113.96353741, 114.33505669, 114.70657596,
115.10131519, 115.47283447, 115.84435374, 116.21587302,
116.61061224, 116.98213152, 117.35365079, 117.74839002,
118.1199093 , 118.49142857, 118.86294785, 119.23446712,
119.60598639, 119.97750567, 120.34902494, 120.74376417,
121.11528345, 121.48680272, 121.88154195, 122.27628118,
122.64780045, 122.99609977, 123.36761905, 123.73913832,
124.13387755, 124.50539683, 124.8769161 , 125.22521542,
125.59673469, 125.96825397, 126.33977324, 126.73451247,
127.10603175, 127.50077098, 127.87229025, 128.26702948,
128.63854875, 129.01006803, 129.35836735, 129.75310658,
130.1478458 , 130.51936508, 130.8676644 , 131.23918367,
131.58748299, 131.95900227, 132.33052154, 132.67882086,
133.07356009, 133.46829932, 133.81659864, 134.21133787,
134.6060771 , 134.97759637, 135.34911565, 135.74385488,
136.1385941 , 136.51011338, 136.88163265, 137.25315193,
137.6246712 , 137.99619048, 138.3444898 , 138.71600907,
139.1107483 , 139.50548753, 139.85378685, 140.22530612,
140.57360544, 140.94512472, 141.33986395, 141.71138322,
142.08290249, 142.47764172, 142.82594104, 143.19746032,
143.56897959, 143.94049887, 144.31201814, 144.72997732,
145.1014966 , 145.49623583, 145.91419501, 146.3321542 ,
146.70367347, 147.0984127 , 147.46993197, 147.84145125,
148.21297052, 148.5844898 , 148.97922902, 149.37396825,
149.74548753, 150.14022676, 150.51174603, 150.88326531,
151.25478458, 151.62630385, 151.99782313, 152.3693424 ,
152.74086168, 153.11238095, 153.46068027, 153.83219955,
154.22693878, 154.5752381 , 154.92353741, 155.27183673,
155.64335601, 155.99165533, 156.3631746 , 156.71147392,
157.0829932 , 157.47773243, 157.87247166, 158.26721088,
158.6155102 , 159.01024943, 159.35854875, 159.73006803,
160.1015873 , 160.44988662, 160.84462585, 161.21614512,
161.5876644 , 161.98240363, 162.3539229 , 162.70222222,
163.0737415 , 163.42204082, 163.79356009, 164.16507937,
164.53659864, 164.90811791, 165.25641723, 165.62793651,
165.99945578, 166.3477551 , 166.69605442, 167.04435374,
167.41587302, 167.81061224, 168.18213152, 168.53043084,
168.90195011, 169.27346939, 169.62176871, 169.94684807,
170.29514739, 170.64344671, 170.99174603, 171.36326531,
171.73478458, 172.12952381, 172.50104308, 172.87256236,
173.26730159, 173.63882086, 174.03356009, 174.40507937,
174.79981859, 175.19455782, 175.5660771 , 175.96081633,
176.3323356 , 176.70385488, 177.07537415, 177.49333333,
177.84163265, 178.21315193, 178.60789116, 179.00263039,
179.39736961, 179.76888889, 180.14040816, 180.51192744,
180.88344671, 181.25496599, 181.62648526, 181.99800454,
182.36952381, 182.74104308, 183.13578231, 183.50730159,
183.90204082, 184.27356009, 184.64507937, 185.03981859,
185.41133787, 185.8060771 , 186.17759637, 186.52589569,
186.87419501, 187.22249433, 187.59401361, 187.98875283,
188.36027211, 188.75501134, 189.14975057, 189.5444898 ,
189.93922902, 190.33396825, 190.72870748, 191.10022676,
191.49496599, 191.88970522, 192.28444444, 192.67918367,
193.09714286, 193.44544218, 193.84018141, 194.23492063,
194.62965986, 195.02439909, 195.41913832, 195.7906576 ,
196.18539683, 196.58013605, 196.97487528, 197.36961451,
197.76435374, 198.13587302, 198.50739229, 198.90213152,
199.25043084, 199.62195011, 199.99346939, 200.36498866,
200.73650794, 201.13124717, 201.50276644, 201.87428571,
202.24580499, 202.59410431, 202.96562358, 203.33714286,
203.73188209, 204.08018141, 204.42848073, 204.8 ,
205.17151927, 205.51981859, 205.93777778, 206.30929705,
206.68081633, 207.0523356 , 207.44707483, 207.8185941 ,
208.19011338, 208.56163265, 208.90993197, 209.28145125,
209.65297052, 210.0244898 , 210.39600907, 210.7907483 ,
211.16226757, 211.53378685, 211.88208617, 212.25360544,
212.62512472, 213.01986395, 213.41460317, 213.76290249,
214.15764172, 214.50594104, 214.87746032, 215.24897959,
215.64371882, 216.03845805, 216.40997732, 216.7814966 ,
217.15301587, 217.5477551 , 217.91927438, 218.29079365,
218.66231293, 219.0338322 , 219.38213152, 219.75365079,
220.12517007, 220.47346939, 220.84498866, 221.19328798,
221.56480726, 221.93632653, 222.3078458 , 222.65614512,
223.05088435, 223.42240363, 223.7939229 , 224.16544218,
224.53696145, 224.90848073, 225.28 , 225.62829932,
225.97659864, 226.34811791, 226.69641723, 227.04471655,
227.41623583, 227.76453515, 228.15927438, 228.55401361,
228.94875283, 229.34349206, 229.71501134, 230.08653061,
230.43482993, 230.80634921, 231.17786848, 231.5261678 ,
231.89768707, 232.26920635, 232.61750567, 232.98902494,
233.36054422, 233.73206349, 234.10358277, 234.47510204,
234.82340136, 235.19492063, 235.56643991, 235.93795918,
236.33269841, 236.70421769, 237.07573696, 237.44725624,
237.79555556, 238.16707483, 238.5385941 , 238.91011338,
239.28163265, 239.65315193, 240.00145125])

Conclusion

There's a lot you can do with librosa like Feature Exraction, displaying an audio's spectogram, add effects to songs, check the beat and tempo of a song, and many more. Anyway, I'm still learning this library, any advice or challenge on anything about this library can be very helpful. 👍

Follow me on my new Twitter for more!

Top comments (3)

Collapse
 
michaeltharrington profile image
Michael Tharrington

Hey Vicente! It's awesome seeing a fresh post from ya. Been a long while!

I'm so sorry for all the mess that you've gone through with those hackers. I'll be thinking about you dealing with your diagnosis. I have a history of bipolarism in my family and I know it's really difficult thing to go through.

Anywho, really appreciate ya coming back to post here again and share your learnings with us!

Collapse
 
highcenburg profile image
Vicente G. Reyes • Edited

Appreciate this man! It's a whole group that has a program that can read my thoughts, hear my inner voice, have a remote neural monitor and voice2skull on me. I have been researching on how they can read my thoughts and hear my inner voice but couldn't find one.

If I may ask, how'd you deal with the bipolarism in your family?

Apologies if I haven't checked messages on discord yet! Will reply when I feel better!

Collapse
 
michaeltharrington profile image
Michael Tharrington

Wow, that sounds extreme! I really hope that all that stops for ya.

Dealing with bipolarism in my family kinda differed depending on the situation. Both my grandmother, my uncle, and some of my cousins have been diagnosed. I have always just done my best to be kind and understanding to these folks because I know it's gotta be a tough thing to go through. I know they take medications, but I honestly couldn't tell you what they are. My grandmother (mom's mom) passed many years ago in a car crash, and I know she was diagnosed later in life... I was pretty young when she left us, but I know that she was fighting it. I wish I had some good advice for ya, but I am definitely not a professional and only really know the littlest bit from interacting with others.

On another note, don't ya worry about missing any Discord messages. It's great to speak with ya here.