From 37d5b11c8bdcb3c2e62caf175dc1664f5cdef0f2 Mon Sep 17 00:00:00 2001 From: Mathieu Durand Date: Sat, 20 Oct 2018 17:35:20 -0400 Subject: [PATCH 1/3] Additional cmdline argument for pyaudio --- README.md | 7 +++++++ auditok/cmdline.py | 6 +++++- auditok/io.py | 5 ++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cc18680..f2fd8e6 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,13 @@ According to this table, the previous command can be run as: 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 -A 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 --git a/auditok/cmdline.py b/auditok/cmdline.py index 365a967..3ee827f 100755 --- a/auditok/cmdline.py +++ b/auditok/cmdline.py @@ -576,6 +576,8 @@ def main(argv=None): 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("-A", "--audio-device", dest="input_device_index", help="Audio device index", type=int, default=0, metavar="INT") + group.add_option("-F", "--audio-frame-per-buffer", dest="frame_per_buffer", help="Audio frame per buffer", 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 @@ def main(argv=None): 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 --git a/auditok/io.py b/auditok/io.py index cd9ac8c..1f9343b 100644 --- a/auditok/io.py +++ b/auditok/io.py @@ -351,10 +351,12 @@ class PyAudioSource(AudioSource): 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=0): 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 @@ def open(self): rate=self.sampling_rate, input=True, output=False, + input_device_index=self.input_device_index, frames_per_buffer=self._chunk_size) def close(self): From f29453b09de3a089993fa916733c64eef6f58c5a Mon Sep 17 00:00:00 2001 From: Mathieu Durand Date: Wed, 31 Oct 2018 20:55:00 -0400 Subject: [PATCH 2/3] Update command line args and README for PyAudio --- README.md | 2 +- auditok/cmdline.py | 4 ++-- auditok/io.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f2fd8e6..03ea1c0 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ According to this table, the previous command can be run as: ### PyAudio -When capturing input with PyAudio, you may need to adjust the device index with -A 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. +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). diff --git a/auditok/cmdline.py b/auditok/cmdline.py index 3ee827f..24c6fb2 100755 --- a/auditok/cmdline.py +++ b/auditok/cmdline.py @@ -576,8 +576,8 @@ def main(argv=None): 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("-A", "--audio-device", dest="input_device_index", help="Audio device index", type=int, default=0, metavar="INT") - group.add_option("-F", "--audio-frame-per-buffer", dest="frame_per_buffer", help="Audio frame per buffer", type=int, default=1024, metavar="INT") + group.add_option("-I", "--input-device-index", dest="input_device_index", help="Audio device index [default: %default] - sonly 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.") diff --git a/auditok/io.py b/auditok/io.py index 1f9343b..772147f 100644 --- a/auditok/io.py +++ b/auditok/io.py @@ -352,7 +352,7 @@ def __init__(self, sampling_rate=DEFAULT_SAMPLE_RATE, sample_width=DEFAULT_SAMPLE_WIDTH, channels=DEFAULT_NB_CHANNELS, frames_per_buffer=1024, - input_device_index=0): + input_device_index=None): AudioSource.__init__(self, sampling_rate, sample_width, channels) self._chunk_size = frames_per_buffer From 52c714a95d374e5aaf3ce35959de16b22103d2b4 Mon Sep 17 00:00:00 2001 From: Mathieu Durand Date: Wed, 31 Oct 2018 21:06:06 -0400 Subject: [PATCH 3/3] README typo --- auditok/cmdline.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/auditok/cmdline.py b/auditok/cmdline.py index 24c6fb2..0499c18 100755 --- a/auditok/cmdline.py +++ b/auditok/cmdline.py @@ -576,7 +576,7 @@ def main(argv=None): 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] - sonly when using PyAudio", type=int, default=None, 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)