Created
November 10, 2019 07:15
-
-
Save lyleaf/8a7b701fd285b42814e2997e1430c702 to your computer and use it in GitHub Desktop.
stream_beat.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""PyAudio Example: Play a wave file (callback version).""" | |
import pyaudio | |
import wave | |
import time | |
import sys | |
import librosa | |
import numpy as np | |
import scipy | |
import matplotlib.pyplot as plt | |
if len(sys.argv) < 2: | |
print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0]) | |
sys.exit(-1) | |
wf = wave.open(sys.argv[1], 'rb') | |
# instantiate PyAudio (1) | |
p = pyaudio.PyAudio() | |
sr = wf.getframerate() | |
print('width : {}'.format(wf.getsampwidth())) | |
#CHUNK=2048 | |
CHUNK=2*sr | |
# sample tempo of the whole file | |
#y, sr = librosa.load(sys.argv[1]) | |
#tempo = librosa.beat.tempo(y=y, sr=sr) | |
#print(tempo/2.0) | |
#sys.exit(0) | |
#nchannels = wf.getnchannels() | |
sampwidth = wf.getsampwidth() | |
doto = [] | |
# define callback (2) | |
def callback(in_data, frame_count, time_info, status): | |
print("callback") | |
global sr, doto | |
#int_data = np.fromstring(in_data,dtype=np.int16) | |
#y = int_data.astype(float) / 32768.0 | |
y = librosa.util.buf_to_float(in_data, n_bytes=sampwidth, dtype=np.float16) | |
print("callback2") | |
print(len(y)) | |
print(y) | |
doto.append(y) | |
# How to get max and min values of the wave data ? | |
#onset_env = librosa.onset.onset_strength(y, sr=sr) | |
#prior = scipy.stats.uniform(30, 300) | |
#prior_lognorm = scipy.stats.lognorm(loc=np.log(120), scale=120, s=1) | |
#tempo = librosa.beat.tempo(y=y, sr=sr) | |
print("callback25") | |
tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr) | |
print("callback3") | |
#tempo = librosa.beat.tempo(y=y, sr=sr, onset_envelope=onset_env) | |
#tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr) | |
#print(beat_frames) | |
print('Estimated tempo: {} beats per minute'.format(tempo/2.0)) | |
if len(doto) > 5: | |
ret_status = pyaudio.paComplete | |
else: | |
ret_status = pyaudio.paContinue | |
return (None, ret_status) | |
# open stream using callback (3) | |
#stream = p.open(format=p.get_format_from_width(wf.getsampwidth()), | |
stream = p.open(format=pyaudio.paInt16, | |
channels=1, | |
#channels=wf.getnchannels(), | |
rate=wf.getframerate(), | |
input = True, | |
frames_per_buffer=CHUNK, | |
stream_callback=callback) | |
# start the stream (4) | |
stream.start_stream() | |
# wait for stream to finish (5) | |
while stream.is_active(): | |
time.sleep(0.2) | |
print('to') | |
for y in doto: | |
plt.plot(y) | |
plt.show() | |
print('toi') | |
# stop stream (6) | |
stream.stop_stream() | |
stream.close() | |
wf.close() | |
# close PyAudio (7) | |
p.terminate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment