Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional cmdline argument for pyaudio #17

Merged
merged 3 commits into from
Nov 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 -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
Expand Down
6 changes: 5 additions & 1 deletion auditok/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("-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.")
Expand Down Expand Up @@ -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")
Expand Down
5 changes: 4 additions & 1 deletion auditok/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=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()
Expand All @@ -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):
Expand Down