import pyaudio import wave import librosa import os chunk = 1024 # Record in chunks of 1024 samples sample_format = pyaudio.paInt32 # 16 bits per sample channels = 1 fs = 44100 # Record at 44100 samples per second seconds = 10 filenamelive = "live.wav" filename = "" bpm = 0 while filename == "": archive = open("name.txt","r") filename = archive.readline() archive.close() if filename == "end": archive = open("name.txt","w") archive.write("") archive.close os.system("rm *mp3") os.system("rm *m4a") os.system("rm *wav") break; if filename == "record": p = pyaudio.PyAudio() print('Recording') stream = p.open(format=sample_format, channels=channels, rate=fs, frames_per_buffer=chunk, input=True) frames = [] # Initialize array to store frames for i in range(0, int(fs / chunk * seconds)): data = stream.read(chunk) frames.append(data) stream.stop_stream() stream.close() p.terminate() print('Finished recording') wf = wave.open(filenamelive, 'wb') wf.setnchannels(channels) wf.setsampwidth(p.get_sample_size(sample_format)) wf.setframerate(fs) wf.writeframes(b''.join(frames)) wf.close() filename ="live.wav" if filename != "" and filename != "end": y, sr = librosa.load(filename) bpm, beatframes = librosa.beat.beat_track(y=y, sr=sr) print(bpm) if bpm != 0: bpm = round(bpm) archive2 = open("bpm.txt","w") archive2.write(str(bpm)) archive = open("name.txt","w") archive.write("") archive.close() archive2.close() bpm = 0 filename = ""