78 lines · 2.0 KB · Python
RawDownload
#! /usr/bin/env python """ A simple demo using aubio and pyaudio to play beats in real time Note you will need to have pyaudio installed: `pip install pyaudio`. Examples:    ./demo_tapthebeat.py ~/Music/track1.ogg When compiled with ffmpeg/libav, you should be able to open remote streams. Forinstance using youtube-dl (`pip install youtube-dl`):     ./demo_tapthebeat.py `youtube-dl -xg https://youtu.be/zZbM9n9j3_g` """ import sysimport timeimport pyaudioimport aubioimport numpy as np win_s = 1024                # fft sizehop_s = win_s // 2          # hop size # parse command line argumentsif len(sys.argv) < 2:    print("Usage: %s <filename> [samplerate]" % sys.argv[0])    sys.exit(1) filename = sys.argv[1] samplerate = 0if len( sys.argv ) > 2: samplerate = int(sys.argv[2]) # create aubio sourcea_source = aubio.source(filename, samplerate, hop_s)samplerate = a_source.samplerate # create aubio tempo detectiona_tempo = aubio.tempo("default", win_s, hop_s, samplerate) # create a simple click soundclick = 0.7 * np.sin(2. * np.pi * np.arange(hop_s) / hop_s * samplerate / 3000.) # pyaudio callbackdef pyaudio_callback(_in_data, _frame_count, _time_info, _status):    samples, read = a_source()    is_beat = a_tempo(samples)    if is_beat:        samples += click        #print ('tick') # avoid print in audio callback    audiobuf = samples.tobytes()    if read < hop_s:        return (audiobuf, pyaudio.paComplete)    return (audiobuf, pyaudio.paContinue) # create pyaudio stream with frames_per_buffer=hop_s and format=paFloat32p = pyaudio.PyAudio()pyaudio_format = pyaudio.paFloat32frames_per_buffer = hop_sn_channels = 1stream = p.open(format=pyaudio_format, channels=n_channels, rate=samplerate,        output=True, frames_per_buffer=frames_per_buffer,        stream_callback=pyaudio_callback) # start pyaudio streamstream.start_stream() # wait for stream to finishwhile stream.is_active():    time.sleep(0.1) # stop pyaudio streamstream.stop_stream()stream.close()# close pyaudiop.terminate()

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t