Created
October 21, 2015 16:27
-
-
Save PseudoSky/fb5012402a7f769a5da0 to your computer and use it in GitHub Desktop.
Python scripts to dub down recorded audio frequency in real time and pipe it back through multiple outputs.
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
import math | |
import pyaudio | |
import sys, time | |
import numpy as np | |
import wave | |
RRATE = 16000 #from scipy import signal | |
WAVE = 15000 | |
sigme = ''.join([chr(int(math.sin(x/((RRATE/WAVE)/math.pi))*127+128)) for x in xrange(RRATE)]) | |
n = -5240 # this is how the pitch should change, positive integers increase the frequency, negative integers decrease it. | |
chunk =2048*2 | |
FORMAT = pyaudio.paInt16 | |
CHANNELS = 1 | |
RATE =44100# 41500 | |
RECORD_SECONDS = 30 | |
swidth = 2 | |
DEVICES=[1,2] | |
p = pyaudio.PyAudio() | |
print p.get_device_count() | |
count = p.get_device_count() | |
devices = [] | |
for i in range(count): | |
devices.append(p.get_device_info_by_index(i)) | |
for i, dev in enumerate(devices): | |
print "%d - %s" % (i, dev['name']) | |
stream = p.open(format = FORMAT, | |
channels = CHANNELS, | |
rate = RATE, | |
input = True, | |
output = True, | |
frames_per_buffer = chunk, | |
output_device_index=2) | |
outp = p.open(format = FORMAT, | |
channels = CHANNELS, | |
rate = RATE, | |
#input = False, | |
output = True, | |
frames_per_buffer = chunk/2, | |
output_device_index=1) | |
print "* recording" | |
start = time.time() | |
while(time.time()-start < RECORD_SECONDS): | |
try: | |
data = stream.read(chunk) | |
data = np.array(wave.struct.unpack("%dh"%(len(data)/swidth), data)) | |
# do real fast Fourier transform | |
data = np.fft.rfft(data) | |
# This does the shifting | |
data2 = [0]*len(data) | |
if n >= 0: | |
data2[n:len(data)] = 2*data[0:(len(data)-n)] | |
data2[0:n] = 2*data[(len(data)-n):len(data)] | |
else: | |
data2[0:(len(data)+n)] = 2*data[-n:len(data)] | |
data2[(len(data)+n):len(data)] = 2*data[0:-n] | |
data = np.array(data2) | |
# Done shifting | |
# inverse transform to get back to temporal data | |
data = np.fft.irfft(data) | |
dataout = np.array(data, dtype='int16') | |
chunkout = wave.struct.pack("%dh"%(len(dataout)), *list(dataout)) #convert back to 16-bit data | |
#print dataout | |
#outp.write(sigme) | |
#outp.write(chunkout) | |
print dataout | |
stream.write(chunkout) | |
except IOError as ex: | |
print ex | |
if ex[1] != pyaudio.paInputOverflowed: | |
print "overflow" | |
#raise | |
print "* done" | |
stream.stop_stream() | |
stream.close() | |
p.terminate() |
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
import time | |
from pyo import * | |
running=False | |
ser = {} | |
global obj | |
global s | |
print pa_list_devices() | |
obj = {} | |
ser = Server() | |
s = Server(sr=44100, nchnls=2, buffersize=1024, duplex=1).boot() | |
def cb(): | |
global fs | |
Trig.play() | |
mic = Input([0,1]) | |
fs.setInput(mic) | |
obj['a'] = SineLoop(freq=20000, feedback=.1, mul=.3) | |
obj["lf1"] =Sine(freq=.03, mul=10) | |
obj["lf2"] = Sine(freq=.05, mul=10) | |
mic = Input([0,1]) | |
fs=FreqShift(mic,shift=-15000,mul=8).out() | |
TrigFunc(mic,cb) | |
def start(f=440): | |
global obj | |
global running | |
obj['left-sin'] = FreqShift(obj['a'], shift=obj['lf1'], mul=.5).out() | |
obj['right-sin'] = FreqShift(obj['a'], shift=SuperSaw(freq=[49,50], detune=obj['lf2'], bal=0.7, mul=0.2), mul=.5).out(1) | |
running=True | |
s.start() | |
while True: | |
try: | |
# outp.write(sigme) | |
if not(serverCreated()): | |
print "Creating Server" | |
# obj=setup() | |
print obj | |
print "ServerCreated: \t",serverCreated() | |
elif running==False: | |
start() | |
time.sleep(1) | |
print "ServerBooted: \t",serverBooted() | |
print "\n\nRunning", | |
except KeyboardInterrupt: | |
print("* Killed Process") | |
quit() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment