{"id":3664,"date":"2018-10-07T14:06:38","date_gmt":"2018-10-07T20:06:38","guid":{"rendered":"http:\/\/benincosa.com\/?p=3664"},"modified":"2018-10-07T22:55:42","modified_gmt":"2018-10-08T04:55:42","slug":"bluetooth-speaker-and-microphone-on-raspberry-pi","status":"publish","type":"post","link":"https:\/\/benincosa.com\/?p=3664","title":{"rendered":"Bluetooth Speaker and Microphone on Raspberry Pi"},"content":{"rendered":"<p>If you want your own Jarvis from Ironman the first step is to have a computer that can listen to what you say and then talk back to you.\u00a0 The software for that is the hard part but with some AI and RNNs we can get some pretty good functionality.\u00a0 That comes later, so let&#8217;s first start up with the microphone and audio.\u00a0 I am using a Raspberry Pi 3 and will now set these peripherals up.<\/p>\n<h2>Microphone<\/h2>\n<p>I bought a basic <a href=\"https:\/\/www.amazon.com\/gp\/product\/B00IR8R7WQ\/ref=oh_aui_detailpage_o02_s00?ie=UTF8&amp;psc=1\" target=\"_blank\" rel=\"noopener noreferrer\">USB microphone<\/a> that cost me an outrageous $4.50.\u00a0 After plugging it into the Raspberry Pi, we can test and use it.\u00a0 First we see that it is there with:<\/p>\n<pre class=\"lang:sh decode:true\">pi@pi3:~ $ arecord -l\r\n**** List of CAPTURE Hardware Devices ****\r\ncard 1: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio]\r\n  Subdevices: 1\/1\r\n  Subdevice #0: subdevice #0<\/pre>\n<p>To record something we will use this command to record 3 seconds of audio and take the input from the -D device plughw:1,0<\/p>\n<pre class=\"lang:sh decode:true\">arecord -D plughw:1,0 -d 3 test.wav<\/pre>\n<p>This will output the test.wav file.\u00a0 This file can now be played back when we setup our bluetooth speaker.<\/p>\n<h2>Bluetooth Speaker<\/h2>\n<p>My <a href=\"https:\/\/www.amazon.com\/gp\/product\/B010OYASRG\/ref=oh_aui_detailpage_o03_s00?ie=UTF8&amp;psc=1\" target=\"_blank\" rel=\"noopener noreferrer\">bluetooth speaker was a pretty cool model I bought<\/a> for tunes while working out.\u00a0 Turning it on and making sure its not connected to anything else I logged into the Raspberry Pi and entered the command:<\/p>\n<pre class=\"lang:sh decode:true \">sudo bluetoothctl\r\n<\/pre>\n<p>I then entered the &#8216;scan on&#8217; command (but didn&#8217;t have to) and a bunch of devices showed up.\u00a0 I picked the one I wanted and got its mac address:<\/p>\n<pre class=\"lang:sh decode:true \">32:D9:46:EE:D3:AF<\/pre>\n<p>I then ran the commands:<\/p>\n<pre class=\"lang:sh decode:true\">pair 32:D9:46:EE:D3:AF\r\nconnect 32:D9:46:EE:D3:AF\r\nexit<\/pre>\n<p>The speaker then showed connected.\u00a0 Now we know its there let&#8217;s try to set the system to use it. Running the command:<\/p>\n<pre class=\"lang:sh decode:true\">alsamixer -D bluealsa<\/pre>\n<p>We get:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-medium wp-image-3665\" src=\"http:\/\/benincosa.com\/wp-content\/uploads\/2018\/10\/Screen-Shot-2018-10-07-at-12.25.24-PM-300x238.png\" alt=\"\" width=\"300\" height=\"238\" srcset=\"https:\/\/benincosa.com\/wp-content\/uploads\/2018\/10\/Screen-Shot-2018-10-07-at-12.25.24-PM-300x238.png 300w, https:\/\/benincosa.com\/wp-content\/uploads\/2018\/10\/Screen-Shot-2018-10-07-at-12.25.24-PM-768x608.png 768w, https:\/\/benincosa.com\/wp-content\/uploads\/2018\/10\/Screen-Shot-2018-10-07-at-12.25.24-PM-1024x811.png 1024w, https:\/\/benincosa.com\/wp-content\/uploads\/2018\/10\/Screen-Shot-2018-10-07-at-12.25.24-PM.png 1134w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>This is great because it shows the speaker is connected and ready to work.\u00a0 Now we can play the wav file through this bluetooth speaker.\u00a0 The default audio of the Raspberry Pi is set to play out of the analog jack.\u00a0 So if we run:<\/p>\n<pre class=\"lang:sh decode:true \">aplay test.wav<\/pre>\n<p>It doesn&#8217;t play anything.\u00a0 Changing the command we can specify the device to play through:<\/p>\n<pre class=\"lang:sh decode:true \">aplay -D bluealsa:HCI=hci0,DEV=32:D9:46:EE:D3:AF,PROFILE=a2dp test.wav<\/pre>\n<p>Here the mac address shown below is the mac address of the bluetooth device.\u00a0 It plays but it is really staticy, so the recording isn&#8217;t may favorite.\u00a0 To determine if its the microphone or the audio we simply play another file:<\/p>\n<pre class=\"lang:sh decode:true\">aplay -D bluealsa:HCI=hci0,DEV=32:D9:46:EE:D3:AF,PROFILE=a2dp voice\/foo.wav<\/pre>\n<p>We can simplify this device configuration by adding it to the \/usr\/share\/alsa\/alsa.conf.d\/20-bluealsa.conf.\u00a0 We add the following at the end:<\/p>\n<pre class=\"lang:sh decode:true \">pcm.oontz {\r\n        type plug\r\n        slave.pcm {\r\n                type bluealsa\r\n                device \"32:D9:46:EE:D3:AF\"\r\n                profile \"a2dp\"\r\n        }\r\n        hint {\r\n                show on\r\n                description \"OONTZ Angle 3 3AF\"\r\n        }\r\n}<\/pre>\n<p>In the above I&#8217;ve just named my device &#8220;oontz&#8221; and put in my own MAC address of the device as well as a description.\u00a0 Everything else should be the same for your setup.\u00a0 Then we can run:<\/p>\n<pre class=\"lang:sh decode:true \">aplay -D oontz voice\/foo.wav<\/pre>\n<p>Now, to make this the default we can add a similar entry to the ~\/.asoundrc file.\u00a0 Mine looks like this:<\/p>\n<pre class=\"lang:sh decode:true\">pcm.!default {\r\n\ttype plug\r\n\tslave.pcm {\r\n\t\ttype bluealsa\r\n\t\tdevice \"32:D9:46:EE:D3:AF\"\r\n\t\tprofile \"a2dp\"\r\n\t}\r\n\r\n}<\/pre>\n<p>It is similar to the previous entry, but we have named it pcm.!default.\u00a0 Now when we play the wav file it will use this as the default:<\/p>\n<pre class=\"lang:sh decode:true\">aplay voice\/foo.wav\r\nmplayer voice\/foo.wav<\/pre>\n<p>Cool.\u00a0 Default sound setup for bluetooth.\u00a0 Persistent reconnect will be the next topic!<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you want your own Jarvis from Ironman the first step is to have a computer that can listen to what you say and then talk back to you.\u00a0 The software for that is the hard part but with some AI and RNNs we can get some pretty good functionality.\u00a0 That comes later, so let&#8217;s&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[920,868],"tags":[870,871,869],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts\/3664"}],"collection":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3664"}],"version-history":[{"count":4,"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts\/3664\/revisions"}],"predecessor-version":[{"id":3669,"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts\/3664\/revisions\/3669"}],"wp:attachment":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}