From fa4683d893c889ce0fc47a972617d33f19d6e662 Mon Sep 17 00:00:00 2001 From: Brian Whitman Date: Fri, 27 Jan 2017 13:34:22 -0500 Subject: [PATCH] cleaned up a bit, more info on the HID library --- README.md | 8 ++++---- listen_and_get_position.py | 14 ++++++-------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 0969b5d..f0ff908 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # respeaker-xmos-hid -Python & C hidapi examples for the "Respeaker" microphone array to show angle & VAD information +Python & C hidapi examples for the "Respeaker" microphone array to show angle & voice activity (VAD) information. -For both, you need to install hidapi +For both, you need to install [hidapi.](https://github.com/signal11/hidapi) -For Python, you need this [hidapi wrapper](https://pypi.python.org/pypi/hid). +For Python, you need this [hidapi wrapper](https://pypi.python.org/pypi/hidapi). (Note: there are two python modules called hid, this is the one you get by pip install hidapi. The other probably works too but has different calling conventions.) -The Python example records 10 seconds to a wave file and prints out VAD and voice angle information in real time to the screen. +The Python example records 10 seconds to a wave file called output.wav and prints out VAD and voice angle information in real time to the screen. Just plug the mic array directly over USB to your computer and run the script. ``` carry:respeaker-xmos-hid bwhitman$ python listen_and_get_position.py diff --git a/listen_and_get_position.py b/listen_and_get_position.py index fc447f7..c590a43 100644 --- a/listen_and_get_position.py +++ b/listen_and_get_position.py @@ -1,4 +1,5 @@ -import wave, os, pyaudio, time, hid +import wave, os, pyaudio, time +import hid # https://pypi.python.org/pypi/hidapi aka https://github.com/trezor/cython-hidapi _pyaudio_instance = pyaudio.PyAudio() _wav = None @@ -6,12 +7,9 @@ RESPEAKER_PRODUCT_ID = 0x07 RECORD_SR = 16000 -# Set up the HID driver, you have to open it by path so we look it up first by VID and PID -_dev = hid.device(vendor_id = RESPEAKER_VENDOR_ID, product_id = RESPEAKER_PRODUCT_ID) -for x in hid.enumerate(): - if x['vendor_id'] == RESPEAKER_VENDOR_ID and x['product_id'] == RESPEAKER_PRODUCT_ID: - _dev.open_path(x['path']) - +# Set up the HID driver +_dev = hid.device() +_dev.open(RESPEAKER_VENDOR_ID, RESPEAKER_PRODUCT_ID) # Write data to a register, return how many bytes were written def write_register(register, data): @@ -106,7 +104,7 @@ def main(): # And test writing a register, the LED -- I turn them off, saves ~120 mA write_register(0, [0, 0, 0, 0]) # mode, b, g, r -- mode 0 is "all off" # Now record audio to a wav file for 10 seconds and print VAD and angle to screen - record() + record(length_seconds = 10) if __name__ == '__main__': main()