3 """ A simple demo using aubio and pyaudio to play beats in real time
5 Note you will need to have pyaudio installed: `pip install pyaudio`.
8 ./demo_tapthebeat.py ~/Music/track1.ogg
10 When compiled with ffmpeg/libav, you should be able to open remote streams. For
11 instance using youtube-dl (`pip install youtube-dl`):
13 ./demo_tapthebeat.py `youtube-dl -xg https://youtu.be/zZbM9n9j3_g`
23 win_s = 1024 # fft size
24 hop_s = win_s // 2 # hop size
26 # parse command line arguments
28 print("Usage: %s <filename> [samplerate]" % sys.argv[0])
31 filename = sys.argv[1]
34 if len( sys.argv ) > 2: samplerate = int(sys.argv[2])
37 a_source = aubio.source(filename, samplerate, hop_s)
38 samplerate = a_source.samplerate
40 # create aubio tempo detection
41 a_tempo = aubio.tempo("default", win_s, hop_s, samplerate)
43 # create a simple click sound
44 click = 0.7 * np.sin(2. * np.pi * np.arange(hop_s) / hop_s * samplerate / 3000.)
47 def pyaudio_callback(in_data, frame_count, time_info, status):
48 samples, read = a_source()
49 is_beat = a_tempo(samples)
52 #print ('tick') # avoid print in audio callback
53 audiobuf = samples.tobytes()
55 return (audiobuf, pyaudio.paComplete)
56 return (audiobuf, pyaudio.paContinue)
58 # create pyaudio stream with frames_per_buffer=hop_s and format=paFloat32
60 pyaudio_format = pyaudio.paFloat32
61 frames_per_buffer = hop_s
63 stream = p.open(format=pyaudio_format, channels=n_channels, rate=samplerate,
64 output=True, frames_per_buffer=frames_per_buffer,
65 stream_callback=pyaudio_callback)
67 # start pyaudio stream
70 # wait for stream to finish
71 while stream.is_active():