Created
October 15, 2012 00:04
-
-
Save jakevsrobots/3890223 to your computer and use it in GitHub Desktop.
Play back raw data from a file, using pitch tracking from microphone input as an index.
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
""" Control playback of raw data using pitch tracking from input. """ | |
import pyaudio | |
import wave | |
import analyse | |
import numpy | |
import sys | |
import random | |
p = pyaudio.PyAudio() | |
chunk_size = 1024 | |
f = open(sys.argv[1], 'rb') | |
output_stream = p.open(format = pyaudio.paInt16, | |
channels = 2, | |
rate = 22050, | |
output = True) | |
input_stream = p.open( | |
format = pyaudio.paInt16, | |
channels = 1, | |
rate = 44100, | |
input_device_index = 0, | |
input = True) | |
# read data | |
all_data = f.read() | |
def get_chunk(rawsamps): | |
samps = numpy.fromstring(rawsamps, dtype=numpy.int16) | |
pitch = analyse.detect_pitch(samps) | |
min_pitch = 200.0 | |
max_pitch = 700.0 | |
if pitch and pitch >= min_pitch and pitch <= max_pitch: | |
mapped_pitch = (pitch - min_pitch) / (max_pitch - min_pitch) | |
start_point = int(len(all_data) * mapped_pitch) + random.randint(-40,40) | |
return all_data[start_point:start_point+chunk_size] | |
return '' | |
while True: | |
try: | |
data = get_chunk(input_stream.read(chunk_size)) | |
except IOError: | |
continue | |
sys.stdout.write(data) | |
output_stream.write(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment