hendriks73 / tempo-cnn

Framework for estimating temporal properties of music tracks.
GNU Affero General Public License v3.0
89 stars 12 forks source link

Real time tempo detection #20

Open marestudio2004 opened 11 months ago

marestudio2004 commented 11 months ago

Hi, can I apply your library in realtime tempo detection? So signal will be recorded in frames and send in model somthing like in Local tempo estimation... Do you have any recommendation, frame length...and is this possible at all or does the complete audio file have to be loaded? This is my code which does not work: `from tempocnn.classifier import TempoClassifier from tempocnn.feature import read_features import pyaudio import numpy as np from librosa import util

model_name = 'cnn' classifier = TempoClassifier(model_name)

chunk = 2048 sample_format = pyaudio.paInt16 channels = 1 fs = 11025 # frames per channel seconds = 50 p=pyaudio.PyAudio() print("Recording ...") stream = p.open(format = sample_format, channels = channels, rate = fs, frames_per_buffer = chunk, input = True)

for i in range(0, int(fs/chunk * seconds)): data = stream.read(chunk) data_pcm = util.buf_to_float(data, dtype=np.float32) features = read_features(data_pcm, frames=256, hop_length=32)

estimate local tempi, this returns tempo classes, i.e., a distribution

local_tempo_classes = classifier.estimate(features)
 # find argmax per frame and convert class index to BPM value
max_predictions = np.argmax(local_tempo_classes, axis=1)
local_tempi = classifier.to_bpm(max_predictions)
print(f"Estimated local tempo classes: {local_tempi}")

stream.stop_stream() stream.close() p.terminate() print("... Ending Recording") `

marestudio2004 commented 11 months ago

I change the function feature.py, line 34 is under the comment and I add y as audio data and sr as sample rate:

`# y, sr = librosa.load(file, sr=11025) # marko

y = file # marko
sr = 11025 # marko

`