soundbox/stft-decode.py

34 lines
996 B
Python
Raw Permalink Normal View History

2020-12-20 00:36:12 +00:00
import soundbox
import numpy as np
import scipy.signal as sig
import sys
if len(sys.argv) != 2:
print(f"""Utilisation: {sys.argv[0]} [source]
Détermine les notes jouées dans le fichier [source] en utilisant
la transformation de Fourier à court terme.""")
sys.exit(1)
source_file = sys.argv[1]
# Calcul du STFT
signal = soundbox.load_signal(source_file)
freq, time, vecs = sig.stft(signal, soundbox.samp_rate, nperseg=soundbox.samp_rate * 0.25)
values = np.absolute(vecs) / np.max(np.absolute(vecs))
time_scale = len(signal) / soundbox.samp_rate / values.shape[1]
# Calcul de la fenêtre des fréquences intéressantes
high_enough = np.where(values.max(axis=1) >= 0.01)
left = high_enough[0][0]
right = high_enough[0][-1]
freq = freq[left:right]
values = values[left:right]
# Recherche des pics
for i in range(values.shape[1]):
peaks, _ = sig.find_peaks(values[:, i], height=0.1, distance=10)
print(f'{i * time_scale:.2f} s', list(map(soundbox.freq_note, freq[peaks])))