Last active
August 29, 2015 14:20
-
-
Save dy-dx/a4c644e0fbf6cc33280c to your computer and use it in GitHub Desktop.
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 sys, os, glob | |
import readline | |
import pyaudio | |
import wave | |
from pydub import AudioSegment | |
from pydub.utils import get_player_name, make_chunks | |
CHUNK = 1024 | |
p = pyaudio.PyAudio() | |
def playwav(filename, device_index): | |
wf = wave.open(filename, 'rb') | |
# open stream | |
stream = p.open(output_device_index=device_index, | |
format=p.get_format_from_width(wf.getsampwidth()), | |
channels=wf.getnchannels(), | |
rate=wf.getframerate(), | |
output=True | |
) | |
# read data | |
data = wf.readframes(CHUNK) | |
# play stream | |
while data != '': | |
stream.write(data) | |
data = wf.readframes(CHUNK) | |
# stop stream | |
stream.stop_stream() | |
stream.close() | |
def playquieter(filename, device_index): | |
song = AudioSegment.from_wav(filename) | |
seg = song + 3 | |
# open stream | |
stream = p.open(output_device_index=device_index, | |
format=p.get_format_from_width(seg.sample_width), | |
channels=seg.channels, | |
rate=seg.frame_rate, | |
output=True | |
) | |
# play half-second chunks | |
for chunk in make_chunks(seg, 500): | |
stream.write(chunk._data) | |
# stop stream | |
stream.stop_stream() | |
stream.close() | |
def fixed(rawString): | |
inArray = rawString.split(' ') | |
if (len(inArray) == 1): inArray.append(1) | |
return (inArray[0], inArray[1]) | |
# Determine available output devices | |
devices = [p.get_device_info_by_index(i) for i in range(0, p.get_device_count())] | |
output_devices = [d for d in devices if d['maxOutputChannels'] > 0] | |
default_device_index = p.get_default_output_device_info()['index'] | |
# Ask user to select output device | |
print("Choose an output device.") | |
for d in output_devices: | |
if d['index'] == default_device_index: | |
print("\t%(index)d: %(name)s (default)" % d) | |
else: | |
print("\t%(index)d: %(name)s" % d) | |
output_device_index = str(input("(press enter for default): ")) | |
# Choose default if user enters nothing | |
if output_device_index == '': | |
output_device_index = default_device_index | |
else: | |
output_device_index = int(output_device_index) | |
# Print filenames | |
print("\nAvailable Sounds:") | |
os.chdir(r"/Users/chris/projects/playwavs/") | |
filenames = [file[:-4] for file in glob.glob("*.wav")] | |
for filename in filenames: | |
print(filename) | |
# Main | |
print("q to quit") | |
counter = 1 | |
while (counter == 1): | |
x = str(input("Usage: 'File_Name #': ")) | |
tupX = fixed(x) | |
if tupX[0] in ("q", "quit"): | |
counter = 0 | |
else: | |
for count in range(0, int(tupX[1])): | |
if tupX[0] in filenames: | |
playquieter('%s.wav' % tupX[0], output_device_index) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment