Created
March 20, 2019 05:32
-
-
Save acenturyandabit/a0ba4e7b42bc801f40d21b73082903c8 to your computer and use it in GitHub Desktop.
python smart audio looper
This file contains 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
#!/usr/bin/env python | |
import os | |
import librosa | |
import random | |
import pyaudio | |
import numpy as np | |
import time | |
p = pyaudio.PyAudio() | |
# Open stream with correct settings | |
stream = p.open(format=pyaudio.paFloat32, | |
channels=1, | |
rate=44100, | |
output=True | |
) | |
# open and list directory | |
path = 'corpus/' | |
files = os.listdir(path) | |
for name in files: | |
# load the file, take a 1s snip of it | |
y, sr = librosa.core.load( | |
path+name, offset=random.random()*150, duration=3, sr=44100) | |
# use BPM to isolate a single segment of it | |
tempo, beats = librosa.beat.beat_track(y, sr, units='samples') | |
print("bpm:{0}".format(tempo)) | |
print(beats) | |
# try: | |
y2 = y[beats[1]:beats[2]] | |
y3 = np.tile(y2, 10) | |
# play it | |
# Assuming you have a numpy array called samples | |
data = y3.astype(np.float32).tostring() | |
stream.write(data) | |
# close the file | |
# free the audio | |
print("playyy") | |
# except |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment