-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbeats_detail.py
58 lines (42 loc) · 1.71 KB
/
beats_detail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 19 23:26:39 2019
@author: ongrayyi
"""
import os
import librosa
import numpy as np
import matplotlib.pyplot as plt
import peakutils
from peakutils.plot import plot as pplot
#filename = os.path.expanduser("~/Desktop/2CELLOS_-_Despacito_OFFICIAL_VIDEO-D9LrEXF3USs.wav")
def note_beats(filename):
y, sr = librosa.load(filename)
D = librosa.stft(y)
D_harmonic, D_percussive = librosa.decompose.hpss(D)
y_harmonic = librosa.istft(D_harmonic)
hop_length = 800
tempo, beats = librosa.beat.beat_track(y=y_harmonic, sr=sr, hop_length=hop_length)
onset_env = librosa.onset.onset_strength(y=y_harmonic, sr=sr, aggregate=np.median, hop_length=hop_length)
tempo, beats = librosa.beat.beat_track(onset_envelope=onset_env, sr=sr, hop_length=hop_length)
times = librosa.frames_to_time(np.arange(len(onset_env)), sr=sr, hop_length=hop_length)
indexes = peakutils.peak.indexes(librosa.util.normalize(onset_env), thres=0.15, min_dist=3)
times_peaks = []
#values_peaks = []
for i in indexes:
times_peaks.append(times[i])
#values_peaks.append(onset_env[i])
return times_peaks
"""
plt.figure(figsize=(8, 4))
times = librosa.frames_to_time(np.arange(len(onset_env)), sr=sr, hop_length=hop_length)
plt.plot(times, librosa.util.normalize(onset_env), label='Onset strength')
plt.vlines(times[beats], 0, 1, alpha=0.5, color='r',linestyle='--', label='Beats')
plt.plot(times_peaks, librosa.util.normalize(values_peaks), 'ro')
plt.legend(frameon=True, framealpha=0.75)
# Limit the plot to a 15-second window
plt.xlim(0, 30)
plt.gca().xaxis.set_major_formatter(librosa.display.TimeFormatter())
plt.tight_layout()
"""