{"id":1251,"date":"2020-11-13T14:30:07","date_gmt":"2020-11-13T06:30:07","guid":{"rendered":"http:\/\/ukonline2000.com\/?p=1251"},"modified":"2020-11-18T18:00:27","modified_gmt":"2020-11-18T10:00:27","slug":"raspi-voice-hat-%e9%85%8d%e7%bd%ae%e4%bd%bf%e7%94%a8%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"http:\/\/ukonline2000.com\/?p=1251","title":{"rendered":"Raspi Voice HAT-\u914d\u7f6e\u4f7f\u7528\u6559\u7a0b"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">\u4ea7\u54c1\u7b80\u4ecb<\/h1>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"723\" src=\"http:\/\/ukonline2000.com\/wp-content\/uploads\/2020\/11\/TIM\u56fe\u724720201112221844-1024x723.png\" alt=\"\" class=\"wp-image-1235\" srcset=\"http:\/\/ukonline2000.com\/wp-content\/uploads\/2020\/11\/TIM\u56fe\u724720201112221844-1024x723.png 1024w, http:\/\/ukonline2000.com\/wp-content\/uploads\/2020\/11\/TIM\u56fe\u724720201112221844-300x212.png 300w, http:\/\/ukonline2000.com\/wp-content\/uploads\/2020\/11\/TIM\u56fe\u724720201112221844-768x542.png 768w, http:\/\/ukonline2000.com\/wp-content\/uploads\/2020\/11\/TIM\u56fe\u724720201112221844-425x300.png 425w, http:\/\/ukonline2000.com\/wp-content\/uploads\/2020\/11\/TIM\u56fe\u724720201112221844.png 1238w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u524d\u8a00<\/h2>\n\n\n\n<p>Raspi Voice HRaspi Voice HAT\u662f\u4e13\u4e3aAI\u548c\u8bed\u97f3\u5e94\u7528\u8bbe\u8ba1\u7684Raspberry Pi\u53cc\u9ea6\u514b\u98ce\u6269\u5c55\u677f\u3002 \u8fd9\u610f\u5473\u7740\u60a8\u53ef\u4ee5\u6784\u5efa\u4e00\u4e2a\u96c6\u6210Amazon Amazona\u8bed\u97f3\u670d\u52a1\uff0cGoogle\u52a9\u624b\uff0c\u767e\u5ea6AI\u7b49\u7684\u529f\u80fd\u66f4\u5f3a\u5927\uff0c\u66f4\u7075\u6d3b\u7684\u8bed\u97f3\u4ea7\u54c1\u3002<\/p>\n\n\n\n<p>\u8be5\u677f\u662f\u57fa\u4e8e\u6811\u8393\u6d3e\u800c\u8bbe\u8ba1\u7684\u97f3\u9891\u6a21\u5757\uff0c\u91c7\u7528WM8960\u4f4e\u529f\u8017\u7acb\u4f53\u58f0\u7f16\u89e3\u7801\u5668\uff0c\u901a\u8fc7I2C\u63a5\u53e3\u63a7\u5236\uff0cI2S\u63a5\u53e3\u4f20\u8f93\u97f3\u9891\u3002 \u7535\u8def\u677f\u4e24\u4fa7\u6709\u4e24\u4e2a\u9ea6\u514b\u98ce\u91c7\u96c6\u58f0\u97f3\uff0c\u8fd8\u63d0\u4f9b12\u4e2aAPA102 RGB LED\u548c1\u4e2a\u677f\u8f7d\u626c\u58f0\u5668\uff0c\u5e76\u63d0\u4f9b1\u4e2a\u7528\u6237\u6309\u94ae\u548c1\u4e2aI2C\u63a5\u53e3\uff0c\u7528\u4e8e\u6269\u5c55\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n\n\n\n<p>\u6b64\u5916\uff0c\u677f\u8f7d3.5mm\u97f3\u9891\u63d2\u5b54\u6216JST 2.0\u626c\u58f0\u5668\u8f93\u51fa\u5747\u53ef\u7528\u4e8e\u97f3\u9891\u8f93\u51fa\uff0c\u53ef\u901a\u8fc7\u5916\u63a5\u8033\u673a\u64ad\u653e\u97f3\u4e50,\u540c\u65f6\u4e5f\u53ef\u901a\u8fc7\u53cc\u901a\u9053\u5587\u53ed\u63a5\u53e3\u5916\u63a5\u5587\u53ed\u64ad\u653e\u3002\u677f\u5b50\u5de6\u53f3\u4e24\u8fb9\u6709\u4e00\u4e2a\u9ad8\u8d28\u91cfMEMS\u7845\u9ea6\u514b\u98ce\uff0c\u53ef\u4ee5\u7acb\u4f53\u58f0\u5f55\u97f3\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4ea7\u54c1\u7279\u6027<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>\u4f9b\u7535\u7535\u538b\uff1a5V<\/li><li>\u903b\u8f91\u7535\u538b\uff1a3.3V<\/li><li>\u97f3\u9891\u7f16\u89e3\u7801\u82af\u7247\uff1aWM8960<\/li><li>\u63a7\u5236\u63a5\u53e3\uff1aI2C<\/li><li>\u97f3\u9891\u63a5\u53e3\uff1aI2S<\/li><li>\u6269\u5c55\u63a5\u53e3\uff1a1x I2C\uff0c1x\u6309\u952e<\/li><li>\u7535\u6e90\u63a5\u53e3\uff1a1xType-C \u63a5\u53e3\uff085V\uff09<\/li><li>LED\u63a5\u53e3\uff1a12\u4e2aAPA102\u53ef\u7f16\u7a0bRGB LED\uff0c\u8fde\u63a5\u5230SPI\u63a5\u53e3<\/li><li>DAC\u4fe1\u566a\u6bd4\uff1a98dB<\/li><li>ADC\u4fe1\u566a\u6bd4\uff1a94dB<\/li><li>\u8033\u673a\u9a71\u52a8\uff1a40mW (16\u03a9@3.3V)<\/li><li>\u626c\u58f0\u5668\u9a71\u52a8\uff1a1W per channel (8\u03a9 BTL)\uff08\u677f\u8f7dmono\u626c\u58f0\u5668\uff09<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u786c\u4ef6\u8d44\u6e90<\/h2>\n\n\n\n<p><br>LP\u3001LN\u5206\u522b\u5bf9\u5e94\u5de6\u626c\u58f0\u5668\u7684\u6b63\u3001\u8d1f\u6781; RP\u3001RN\u5206\u522b\u5bf9\u5e94\u53f3\u626c\u58f0\u5668\u7684\u6b63\u3001\u8d1f\u6781\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\u529f\u80fd\u5f15\u811a<\/td><td>\u6811\u8393\u6d3e\u5f15\u811a\uff08BCM\uff09<\/td><td>\u63cf\u8ff0<\/td><\/tr><tr><td>5V<\/td><td>5V<\/td><td>\u7535\u6e90\u6b63\uff085V\u7535\u6e90\u8f93\u5165\uff09<\/td><\/tr><tr><td>GND<\/td><td>GND<\/td><td>\u7535\u6e90\u5730<\/td><\/tr><tr><td>SDA<\/td><td>P3\/GPIO2<\/td><td>I2C\u6570\u636e\u8f93\u5165<\/td><\/tr><tr><td>SCL<\/td><td>P5\/GPIO3<\/td><td>I2C\u65f6\u949f\u8f93\u5165<\/td><\/tr><tr><td>CLK<\/td><td>P12\/GPIO18<\/td><td>I2S\u4f4d\u65f6\u949f\u8f93\u5165<\/td><\/tr><tr><td>LRCLK<\/td><td>P35\/GPIO19<\/td><td>I2S\u5e27\u65f6\u949f\u8f93\u5165<\/td><\/tr><tr><td>DAC<\/td><td>P40\/GPIO21<\/td><td>I2S\u4e32\u884c\u6570\u636e\u8f93\u51fa<\/td><\/tr><tr><td>ADC<\/td><td>P38\/GPIO20<\/td><td>I2S\u4e32\u884c\u6570\u636e\u8f93\u5165<\/td><\/tr><tr><td>BUTTON<\/td><td>P29\/GPIO5\u6216P31\/GPIO6<\/td><td>\u81ea\u5b9a\u4e49\u6309\u952e<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">\u6811\u8393\u6d3e\u4f7f\u7528<\/h1>\n\n\n\n<p>\u672c\u4ea7\u54c1\u4f8b\u7a0b\u53ea\u9002\u7528\u4e8e\u6811\u8393\u6d3e\u5b98\u65b9\u7cfb\u7edf\uff08Raspbian\uff09<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b89\u88c5\u9a71\u52a8<\/h2>\n\n\n\n<p>\u5982\u679c\u4f60\u662f\u56fd\u5185\u7528\u6237\uff0c\u7528\u5b98\u65b9\u6e90\u53ef\u80fd\u4f1a\u6bd4\u8f83\u6162\uff0c\u6574\u4e2a\u5b89\u88c5\u8fc7\u7a0b\u4e0b\u8f7d\u9700\u8981\u5f88\u957f\u65f6\u95f4\u4e14\u53ef\u80fd\u4f1a\u66f4\u65b0\u5931\u8d25\uff0c\u56e0\u6b64\u53ef\u4ee5\u6362\u6210\u963f\u91cc\u6e90\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/apt\/sources.list<\/code><\/pre>\n\n\n\n<p>\u5c06\u5b98\u65b9\u6e90\u5f00\u5934\u7528#\u6ce8\u91ca\uff0c\u5e76\u6dfb\u52a0\u963f\u91cc\u6e90\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#deb-src http:\/\/archive.raspberrypi.org\/debian\/ stretch main\ndeb http:\/\/mirrors.aliyun.com\/raspbian\/raspbian\/ buster main contrib non-free rpi\ndeb-src http:\/\/mirrors.aliyun.com\/raspbian\/raspbian\/ buster main contrib non-free rpi<\/code><\/pre>\n\n\n\n<p>\u66f4\u65b0\u8f6f\u4ef6\u6e90\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get update\nsudo apt-get upgrade<\/code><\/pre>\n\n\n\n<p>\u68c0\u67e5\u5185\u6838\u7248\u672c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8bf7\u4f7f\u7528\u6811\u8393\u6d3e\u5b98\u65b9\u5185\u6838\u7248\u672c\u4e3a5.4\u4ee5\u4e0a\u7684\u5b98\u65b9\u7cfb\u7edf\uff08Kernel version:5.4\uff09\ngit clone --depth=1 https:\/\/github.com\/respeaker\/seeed-voicecard<\/code><\/pre>\n\n\n\n<p>\u5b89\u88c5WM8960\u9a71\u52a8\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd seeed-voicecard\n#\u9700\u8981\u7b49\u5f85\u4e00\u5b9a\u7684\u65f6\u95f4\nsudo .\/install.sh\nreboot<\/code><\/pre>\n\n\n\n<p>\u91cd\u542f\u540e\u8fd0\u884c\u5982\u4e0b\u547d\u4ee4\u770b\u4e0b\u9a71\u52a8\u662f\u5426\u52a0\u8f7d\u6210\u529f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u68c0\u6d4b\u58f0\u5361<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>\u68c0\u67e5\u64ad\u653e\uff1aaplay -l<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>pi@raspberrypi:~\/seeed-voicecard $ aplay -l\n**** List of PLAYBACK Hardware Devices ****\ncard 0: ALSA &#91;bcm2835 ALSA], device 0: bcm2835 ALSA &#91;bcm2835 ALSA]\n  Subdevices: 8\/8\n  Subdevice #0: subdevice #0\n  Subdevice #1: subdevice #1\n  Subdevice #2: subdevice #2\n  Subdevice #3: subdevice #3\n  Subdevice #4: subdevice #4\n  Subdevice #5: subdevice #5\n  Subdevice #6: subdevice #6\n  Subdevice #7: subdevice #7\ncard 0: ALSA &#91;bcm2835 ALSA], device 1: bcm2835 ALSA &#91;bcm2835 IEC958\/HDMI]\n  Subdevices: 1\/1\n  Subdevice #0: subdevice #0\ncard 1: seeed2micvoicec &#91;seeed-2mic-voicecard], device 0: bcm2835-i2s-wm8960-hifi wm8960-hifi-0 &#91;]\n  Subdevices: 1\/1\n  Subdevice #0: subdevice #0<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>\u68c0\u67e5\u5f55\u97f3\uff1aarecord -l<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>pi@raspberrypi:~\/seeed-voicecard $ arecord -l\n**** List of CAPTURE Hardware Devices ****\ncard 1: seeed2micvoicec &#91;seeed-2mic-voicecard], device 0: bcm2835-i2s-wm8960-hifi wm8960-hifi-0 &#91;]\n  Subdevices: 1\/1\n  Subdevice #0: subdevice #0\npi@raspberrypi:~\/seeed-voicecard $<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5f55\u97f3\u64ad\u653e\u6d4b\u8bd5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u5f55\u64ad\u6d4b\u8bd5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo arecord -f cd -Dhw:1 | aplay -Dhw:1<\/code><\/pre>\n\n\n\n<p>\u7a0b\u5e8f\u8fd0\u884c\u540e\u901a\u8fc7\u8033\u673a\u6216\u8005\u5587\u53ed\u4f1a\u542c\u5230\u9ea6\u514b\u98ce\u91c7\u96c6\u5230\u7684\u58f0\u97f3\uff0c\u6ce8\u610f\u5587\u53ed\u4e0d\u80fd\u5f00\u9760\u8fd1\u9ea6\u514b\u98ce\u5426 \u5219\u4f1a\u5bfc\u81f4\u5171\u632f\u4ea7\u751f\u5578\u53eb\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5f55\u97f3<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo arecord -D hw:1,0 -f S32_LE -r 16000 -c 2 test.wav<\/code><\/pre>\n\n\n\n<p>test.wav\u662f\u5f55\u5236\u751f\u6210\u7684\u6587\u4ef6\u540d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u64ad\u653e<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo aplay -Dhw:1 test.wav<\/code><\/pre>\n\n\n\n<p>\u64ad\u653e\u521a\u521a\u5f55\u5236\u7684\u97f3\u9891<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8c03\u8282\u97f3\u91cf<\/h3>\n\n\n\n<p>\u9ed8\u8ba4\u7684\u97f3\u91cf\u662f\u6bd4\u8f83\u5c0f\u7684<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo alsamixer<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"953\" height=\"843\" src=\"http:\/\/ukonline2000.com\/wp-content\/uploads\/2020\/11\/9689ed182bcc065352877990b7294de.png\" alt=\"\" class=\"wp-image-1252\" srcset=\"http:\/\/ukonline2000.com\/wp-content\/uploads\/2020\/11\/9689ed182bcc065352877990b7294de.png 953w, http:\/\/ukonline2000.com\/wp-content\/uploads\/2020\/11\/9689ed182bcc065352877990b7294de-300x265.png 300w, http:\/\/ukonline2000.com\/wp-content\/uploads\/2020\/11\/9689ed182bcc065352877990b7294de-768x679.png 768w, http:\/\/ukonline2000.com\/wp-content\/uploads\/2020\/11\/9689ed182bcc065352877990b7294de-339x300.png 339w\" sizes=\"auto, (max-width: 953px) 100vw, 953px\" \/><\/figure>\n\n\n\n<p>\u5982\u679c WM8960 \u58f0\u5361\u6ca1\u6709\u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u58f0\u5361\u5219\u9700\u8981\u6309 F6 \u9009\u62e9\u58f0\u5361\u8bbe\u5907\u3002<br><br>\u5b9e\u9645\u4e0a\u53f3\u8fb9\u8fd8\u6709\u5f88\u591a\u53ef\u4ee5\u8c03\u8282\u7684\u9009\u9879\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4f7f\u7528\u677f\u5b50\u4e0a\u7684\u5f69\u8272LED\u706f<\/strong><\/h2>\n\n\n\n<p>\u5b89\u88c5spidev<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo pip install spidev<\/code><\/pre>\n\n\n\n<p>\u4e0b\u8f7d\u7a0b\u5e8f\u5e76\u89e3\u538b<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"http:\/\/ukonline2000.com\/wp-content\/uploads\/2020\/11\/Voice_HAT.zip\">Voice_HAT<\/a><a href=\"http:\/\/ukonline2000.com\/wp-content\/uploads\/2020\/11\/Voice_HAT.zip\" class=\"wp-block-file__button\" download>\u4e0b\u8f7d<\/a><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>cd Voice_HAT\nsudo python pixels.py<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f7f\u7528\u6309\u952e<\/h2>\n\n\n\n<p>\u677f\u4e0a\u7684\u6309\u952e\u9ed8\u8ba4\u4f7f\u7528GPIO6(\u53ef\u901a\u8fc7\u8df3\u7ebf\u66f4\u6539\u4e3aGPIO5). <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo pip install rpi.gpio    \/\/ install RPi.GPIO library\nnano button.py               \/\/ copy the following code in button.py<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\u7f16\u8f91button.py\u6587\u4ef6\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\u5e76\u4fdd\u5b58<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>import RPi.GPIO as GPIO\nimport time\n \nBUTTON = 6\n \nGPIO.setmode(GPIO.BCM)\nGPIO.setup(BUTTON, GPIO.IN)\n \nwhile True:\n    state = GPIO.input(BUTTON)\n    if state:\n        print(\"off\")\n    else:\n        print(\"on\")\n    time.sleep(1)<\/code><\/pre>\n\n\n\n<p>\u8fd0\u884c\u7a0b\u5e8f\u540e\uff0c\u53ef\u4ee5\u4ece\u6253\u5370\u4e2d\u770b\u5230\u6309\u952e\u7684\u72b6\u6001<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pi@raspberrypi:~ $ python button.py\noff\noff\non\non\noff<\/code><\/pre>\n\n\n\n<!--nextpage-->\n\n\n\n<h2 class=\"wp-block-heading\" id=\"extract-voice\">Extract Voice<\/h2>\n\n\n\n<p>We use&nbsp;<a href=\"https:\/\/people.csail.mit.edu\/hubert\/pyaudio\/\">PyAudio python library<\/a>&nbsp;to extract voice.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Step 1, We need to run the following script to get the device index number of 2 Mic pi hat:<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo pip install pyaudio\ncd ~\nnano get_index.py<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>Step 2, copy below code and paste on get_index.py.<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>import pyaudio\n \np = pyaudio.PyAudio()\ninfo = p.get_host_api_info_by_index(0)\nnumdevices = info.get('deviceCount')\n \nfor i in range(0, numdevices):\n        if (p.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0:\n            print \"Input Device id \", i, \" - \", p.get_device_info_by_host_api_device_index(0, i).get('name')\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>Step 3, press Ctrl + X to exit and press Y to save.<\/li><li>Step 4, run &#8216;sudo python get_index.py&#8217; and we will see the device ID as below.<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>Input Device id  2  -  seeed-2mic-voicecard: - (hw:1,0)\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>Step 5, change&nbsp;<code>RESPEAKER_INDEX = 2<\/code>&nbsp;to index number. Run python script record.py to record a speech.<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>import pyaudio\nimport wave\n \nRESPEAKER_RATE = 16000\nRESPEAKER_CHANNELS = 2 \nRESPEAKER_WIDTH = 2\n# run getDeviceInfo.py to get index\nRESPEAKER_INDEX = 2  # refer to input device id\nCHUNK = 1024\nRECORD_SECONDS = 5\nWAVE_OUTPUT_FILENAME = \"output.wav\"\n \np = pyaudio.PyAudio()\n \nstream = p.open(\n            rate=RESPEAKER_RATE,\n            format=p.get_format_from_width(RESPEAKER_WIDTH),\n            channels=RESPEAKER_CHANNELS,\n            input=True,\n            input_device_index=RESPEAKER_INDEX,)\n \nprint(\"* recording\")\n \nframes = &#91;]\n \nfor i in range(0, int(RESPEAKER_RATE \/ CHUNK * RECORD_SECONDS)):\n    data = stream.read(CHUNK)\n    frames.append(data)\n \nprint(\"* done recording\")\n \nstream.stop_stream()\nstream.close()\np.terminate()\n \nwf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')\nwf.setnchannels(RESPEAKER_CHANNELS)\nwf.setsampwidth(p.get_sample_size(p.get_format_from_width(RESPEAKER_WIDTH)))\nwf.setframerate(RESPEAKER_RATE)\nwf.writeframes(b''.join(frames))\nwf.close()\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>Step 6. If you want to extract channel 0 data from 2 channels, please follow below code. For other channel X, please change [0::2] to [X::2].<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>import pyaudio\nimport wave\nimport numpy as np\n \nRESPEAKER_RATE = 16000\nRESPEAKER_CHANNELS = 2\nRESPEAKER_WIDTH = 2\n# run getDeviceInfo.py to get index\nRESPEAKER_INDEX = 2  # refer to input device id\nCHUNK = 1024\nRECORD_SECONDS = 3\nWAVE_OUTPUT_FILENAME = \"output.wav\"\n \np = pyaudio.PyAudio()\n \nstream = p.open(\n            rate=RESPEAKER_RATE,\n            format=p.get_format_from_width(RESPEAKER_WIDTH),\n            channels=RESPEAKER_CHANNELS,\n            input=True,\n            input_device_index=RESPEAKER_INDEX,)\n \nprint(\"* recording\")\n \nframes = &#91;] \n \nfor i in range(0, int(RESPEAKER_RATE \/ CHUNK * RECORD_SECONDS)):\n    data = stream.read(CHUNK)\n    # extract channel 0 data from 2 channels, if you want to extract channel 1, please change to &#91;1::2]\n    a = np.fromstring(data,dtype=np.int16)&#91;0::2]\n    frames.append(a.tostring())\n \nprint(\"* done recording\")\n \nstream.stop_stream()\nstream.close()\np.terminate()\n \nwf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')\nwf.setnchannels(1)\nwf.setsampwidth(p.get_sample_size(p.get_format_from_width(RESPEAKER_WIDTH)))\nwf.setframerate(RESPEAKER_RATE)\nwf.writeframes(b''.join(frames))\nwf.close()\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4ea7\u54c1\u7b80\u4ecb \u524d\u8a00 Raspi Voice HRaspi Voice HAT\u662f\u4e13\u4e3aA &hellip; <a href=\"http:\/\/ukonline2000.com\/?p=1251\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1251","post","type-post","status-publish","format-standard","hentry","category-raspberry-pi"],"_links":{"self":[{"href":"http:\/\/ukonline2000.com\/index.php?rest_route=\/wp\/v2\/posts\/1251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/ukonline2000.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ukonline2000.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ukonline2000.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ukonline2000.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1251"}],"version-history":[{"count":7,"href":"http:\/\/ukonline2000.com\/index.php?rest_route=\/wp\/v2\/posts\/1251\/revisions"}],"predecessor-version":[{"id":1265,"href":"http:\/\/ukonline2000.com\/index.php?rest_route=\/wp\/v2\/posts\/1251\/revisions\/1265"}],"wp:attachment":[{"href":"http:\/\/ukonline2000.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ukonline2000.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1251"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ukonline2000.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}