# HG changeset patch # User Mathieu Durand # Date 1541051504 14400 # Node ID d4eec2afbe01637d3e20ebe3482883c9ce1b2d2d # Parent d942a78cb816e78c316a259a4bc5a7a85b0d092d Select pyaudio audio device from command line * Additional cmdline argument for pyaudio * Update command line args and README for PyAudio * README typo diff -r d942a78cb816 -r d4eec2afbe01 README.md --- a/README.md Sat Oct 06 12:02:01 2018 +0200 +++ b/README.md Thu Nov 01 01:51:44 2018 -0400 @@ -86,6 +86,13 @@ rec -q -t raw -r 16000 -c 1 -b 16 -e signed - | auditok -i - +### PyAudio + +When capturing input with PyAudio, you may need to adjust the device index with -I if multiple input devices are available. Use `lsusb -t` to get the list of usb devices, or use `arecord -l` if you're using a non-usb input device. If you don't know what index to use, just try `0`, `1`, `2` and so on, outputting the audio using `-E` (echo) until you hear the sound. + +You may also get an error `[Errno -9981] Input overflowed` from PyAudio. If that's the case, you need a bigger frame buffer. +Use `-F` with 2048 or 4096 (the default is 1024). + ### Play back detections auditok -E diff -r d942a78cb816 -r d4eec2afbe01 auditok/cmdline.py --- a/auditok/cmdline.py Sat Oct 06 12:02:01 2018 +0200 +++ b/auditok/cmdline.py Thu Nov 01 01:51:44 2018 -0400 @@ -576,6 +576,8 @@ group.add_option("-r", "--rate", dest="sampling_rate", help="Sampling rate of audio data [default: %default]", type=int, default=16000, metavar="INT") group.add_option("-c", "--channels", dest="channels", help="Number of channels of audio data [default: %default]", type=int, default=1, metavar="INT") group.add_option("-w", "--width", dest="sample_width", help="Number of bytes per audio sample [default: %default]", type=int, default=2, metavar="INT") + group.add_option("-I", "--input-device-index", dest="input_device_index", help="Audio device index [default: %default] - only when using PyAudio", type=int, default=None, metavar="INT") + group.add_option("-F", "--audio-frame-per-buffer", dest="frame_per_buffer", help="Audio frame per buffer [default: %default] - only when using PyAudio", type=int, default=1024, metavar="INT") parser.add_option_group(group) group = OptionGroup(parser, "[Do something with detections]", "Use these options to print, play or plot detections.") @@ -609,7 +611,9 @@ try: asource = PyAudioSource(sampling_rate = opts.sampling_rate, sample_width = opts.sample_width, - channels = opts.channels) + channels = opts.channels, + frames_per_buffer = opts.frame_per_buffer, + input_device_index = opts.input_device_index) except Exception: sys.stderr.write("Cannot read data from audio device!\n") sys.stderr.write("You should either install pyaudio or read data from STDIN\n") diff -r d942a78cb816 -r d4eec2afbe01 auditok/io.py --- a/auditok/io.py Sat Oct 06 12:02:01 2018 +0200 +++ b/auditok/io.py Thu Nov 01 01:51:44 2018 -0400 @@ -351,10 +351,12 @@ def __init__(self, sampling_rate=DEFAULT_SAMPLE_RATE, sample_width=DEFAULT_SAMPLE_WIDTH, channels=DEFAULT_NB_CHANNELS, - frames_per_buffer=1024): + frames_per_buffer=1024, + input_device_index=None): AudioSource.__init__(self, sampling_rate, sample_width, channels) self._chunk_size = frames_per_buffer + self.input_device_index = input_device_index import pyaudio self._pyaudio_object = pyaudio.PyAudio() @@ -370,6 +372,7 @@ rate=self.sampling_rate, input=True, output=False, + input_device_index=self.input_device_index, frames_per_buffer=self._chunk_size) def close(self):