Skip to content

Commit

Permalink
add device index to client
Browse files Browse the repository at this point in the history
  • Loading branch information
ezavesky committed Sep 3, 2019
1 parent ab82a2e commit 66f5cdc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Recognized: delta echo foxtrot
```
λ py client.py -h
usage: client.py [-h] [-s SERVER] [-a AGGRESSIVENESS] [--nospinner]
[-w SAVEWAV]
[-w SAVEWAV] [-d DEVICE] [-v]
Streams raw audio data from microphone with VAD to server via WebSocket
Expand All @@ -124,7 +124,13 @@ optional arguments:
speech, 3 the most aggressive. Default: 3
--nospinner Disable spinner
-w SAVEWAV, --savewav SAVEWAV
Save .wav files of utterences to given directory
Save .wav files of utterences to given directory.
Example for current directory: -w .
-d DEVICE, --device DEVICE
Set audio device for input, according to system. The
default utilizes system-specified recording device.
-v, --verbose Print debugging info
```

## Contributions
Expand Down
13 changes: 8 additions & 5 deletions client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Audio(object):
CHANNELS = 1
BLOCKS_PER_SECOND = 50

def __init__(self, callback=None, buffer_s=0, flush_queue=True):
def __init__(self, callback=None, buffer_s=0, flush_queue=True, device_index=None):
def proxy_callback(in_data, frame_count, time_info, status):
callback(in_data)
return (None, pyaudio.paContinue)
Expand All @@ -38,6 +38,7 @@ def proxy_callback(in_data, frame_count, time_info, status):
channels=self.CHANNELS,
rate=self.sample_rate,
input=True,
input_device_index=device_index,
frames_per_buffer=self.block_size,
stream_callback=proxy_callback)
self.stream.start_stream()
Expand Down Expand Up @@ -89,8 +90,8 @@ def write_wav(self, filename, data):
class VADAudio(Audio):
"""Filter & segment audio with voice activity detection."""

def __init__(self, aggressiveness=3):
super().__init__()
def __init__(self, aggressiveness=3, device_index=None):
super().__init__(device_index=device_index)
self.vad = webrtcvad.Vad(aggressiveness)

def vad_collector_simple(self, pre_padding_ms, blocks=None):
Expand Down Expand Up @@ -225,7 +226,7 @@ def main():
# TODO: compress?
print_output("Connecting to '%s'..." % websocket.url)

vad_audio = VADAudio(aggressiveness=ARGS.aggressiveness)
vad_audio = VADAudio(aggressiveness=ARGS.aggressiveness, device_index=ARGS.device)
print_output("Listening (ctrl-C to exit)...")
audio_consumer_thread = threading.Thread(target=lambda: audio_consumer(vad_audio, websocket))
audio_consumer_thread.start()
Expand All @@ -246,7 +247,7 @@ def consumer(self, blocks):
else:
print('.', end='', flush=True)
length_ms = 0
VADAudio(consumer)
VADAudio(consumer, device_index=ARGS.device)
elif 1:
VADAudio.test_vad(3)

Expand All @@ -261,6 +262,8 @@ def consumer(self, blocks):
help="Disable spinner")
parser.add_argument('-w', '--savewav',
help="Save .wav files of utterences to given directory. Example for current directory: -w .")
parser.add_argument('-d', '--device', type=int, default=None,
help="Set audio device for input, according to system. The default utilizes system-specified recording device.")
parser.add_argument('-v', '--verbose', action='store_true',
help="Print debugging info")
ARGS = parser.parse_args()
Expand Down

0 comments on commit 66f5cdc

Please sign in to comment.