Created
May 24, 2013 18:44
-
-
Save nils-werner/5645657 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
import numpy as np | |
import pyaudio | |
import scipy.io.wavfile as wav | |
import random | |
import time | |
import gc | |
from numpy import zeros, mat, matrix, sqrt, sin, arange, pi, exp, cos, array, dot, max, abs, int16, eye | |
signal = np.ndarray(shape=(0,2)) | |
fs,A = wav.read("A.wav") # reading | |
fs,Bm = wav.read("Bm.wav") # reading | |
fs,D = wav.read("D.wav") # reading | |
fs,G = wav.read("G.wav") # reading | |
sigs = A, Bm, D, G | |
speed = 33000; | |
signal = np.vstack(( signal, sigs[random.randint(0,3)] )); | |
readposition = 0; | |
writeposition = 0; | |
def callback(in_data, frame_count, time_info, status): | |
global signal | |
global readposition | |
data = (0.00001 * signal[readposition:readposition+frame_count]).astype(np.float32).tostring() | |
readposition = readposition+frame_count; | |
return (data, pyaudio.paContinue) | |
p = pyaudio.PyAudio() | |
stream = p.open(format=pyaudio.paFloat32, | |
channels=2, | |
rate=fs, | |
output=True, | |
stream_callback=callback) | |
while stream.is_active(): | |
if(len(signal) - writeposition < 6*fs): | |
print("Allocating") | |
signal = np.vstack(( signal, np.zeros((8*fs,2)) )); | |
if(writeposition - readposition < 2*fs): | |
print("Recording") | |
sample = sigs[random.randint(0,3)] | |
for j in range(0, 4): | |
offset = writeposition + j*speed; | |
if j == 0: | |
signal[offset:offset+len(sample)] += sample | |
else: | |
signal[offset:offset+len(sample)] += sample/2 | |
writeposition += (j+1)*speed | |
if(readposition > 5*fs): | |
print("Freeing") | |
signal = signal[5*fs:]; | |
readposition -= 5*fs; | |
writeposition -= 5*fs; | |
time.sleep(0.2) | |
gc.collect() | |
print("Stream finished") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment