{"id":35,"date":"2013-10-21T03:50:08","date_gmt":"2013-10-21T03:50:08","guid":{"rendered":"http:\/\/robot.laylamah.com\/?p=35"},"modified":"2013-10-21T04:34:33","modified_gmt":"2013-10-21T04:34:33","slug":"pocketsphinx-on-raspberry-pi","status":"publish","type":"post","link":"http:\/\/robot.laylamah.com\/?p=35","title":{"rendered":"PocketSphinx on Raspberry Pi"},"content":{"rendered":"<p>I decided to try to install PocketSphinx for Raspberry Pi for offline speech-to-text processing (until now, I&#8217;ve been using the Google Translate APIs for speech-to-text).<\/p>\n<p>The short version is that the out-of-the-box experience isn&#8217;t very good. It can recognize &#8220;Hello&#8221; pretty reliably; but, not much else! I&#8217;m hopeful that matters will improve with a custom dictionary.<\/p>\n<h3>Anyway, here&#8217;s a HOWTO for getting it up-and-running on the RPi:<\/h3>\n<h5>Pre-Installation:<\/h5>\n<p>The latest source for pocketsphinx can be obtained <strong><a href=\"http:\/\/sourceforge.net\/projects\/cmusphinx\/files\/\" target=\"_blank\">here<\/a><\/strong>.<\/p>\n<p>You&#8217;ll need to download both <strong>sphinxbase<\/strong> and <strong>pocketsphinx<\/strong>.<\/p>\n<p>You&#8217;ll also need to ensure that ALSA is installed. There&#8217;s a pretty good primer about how to accomplish this <strong><a href=\"http:\/\/elinux.org\/RPi_Text_to_Speech_(Speech_Synthesis)\" target=\"_blank\">here<\/a><\/strong>.<\/p>\n<p>The <em>short version<\/em> is that you must, at the minimum, install <strong>alsa-utils<\/strong>:<\/p>\n<pre>sudo apt-get install alsa-utils<\/pre>\n<p>You&#8217;ll also need <strong>bison<\/strong> and <strong>libasound2-dev<\/strong>.<\/p>\n<pre>sudo apt-get install bison\r\nsudo apt-get install libasound2-dev<\/pre>\n<blockquote><p><span style=\"line-height: 1.5rem;\">Note that the Raspberry Pi lacks any hardware microphone input, so you&#8217;ll have to install your own microphone. I am using the Logitech C920 webcam for this purpose.<\/span><\/p><\/blockquote>\n<p>After you&#8217;ve accomplished all of this, you should be able to install pocketsphinx.<\/p>\n<h5>Installation:<\/h5>\n<p>Install <strong>sphinxbase<\/strong>:<\/p>\n<pre>gzip -d sphinxbase-0.8.tar.gz\r\ntar -xvf sphinxbase-0.8.tar\r\ncd sphinxbase-0.8\r\n.\/configure\r\nmake\r\nsudo make install<\/pre>\n<p>Install <strong>pocketsphinx<\/strong>:<\/p>\n<pre>gzip -d pocketsphinx-0.8.tar.gz\r\ntar -xvf pocketsphinx-0.8.tar\r\ncd pocketsphinx-0.8\r\n.\/configure\r\nmake\r\nsudo make install<\/pre>\n<h5>Running PocketSphinx:<\/h5>\n<p>Test pocketsphinx:<\/p>\n<pre>.\/src\/programs\/pocketsphinx_continuous -adcdev plughw:1,0 -nfft 2048 -samprate 48000<\/pre>\n<h5>Troubleshooting:<\/h5>\n<p>Note, if you receive an error such as the following:<\/p>\n<pre><strong>Error opening audio device plughw:1,0 for capture: Connection refused<\/strong>\r\nMixer load failed: Invalid argument FATAL_ERROR: \"continuous.c\", line\r\n246: Failed to open audio device<\/pre>\n<p>You likely have pulseaudio installed, which is causing sphinxbase to attempt to use pulse instead of alsa.<\/p>\n<p><span style=\"line-height: 1.5rem;\">The workaround (<\/span><em style=\"line-height: 1.5rem;\">if you indeed wish to use ALSA<\/em><span style=\"line-height: 1.5rem;\">) is to remove pulse, and then follow the steps above to re-install sphinxbase.<\/span><\/p>\n<pre>sudo apt-get remove pulseaudio -y\r\nsudo aptitude purge pulseaudio -y\r\nsudo mv \/usr\/include\/pulse\/pulseaudio.h \/usr\/include\/pulse\/pulseaudio.h.old<\/pre>\n<p><span style=\"line-height: 1.5rem;\">If you&#8217;ve done everything correctly, running .\/configure on sphinxbase <\/span><strong style=\"line-height: 1.5rem;\">should<\/strong><span style=\"line-height: 1.5rem;\"> give you output that looks like the following:<\/span><a style=\"line-height: 1.5rem;\" href=\"http:\/\/robot.laylamah.com\/wp-content\/uploads\/2013\/10\/configure_sphinxbase_correct.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-42\" alt=\"configure_sphinxbase_correct\" src=\"http:\/\/robot.laylamah.com\/wp-content\/uploads\/2013\/10\/configure_sphinxbase_correct.png\" width=\"666\" height=\"1133\" srcset=\"http:\/\/robot.laylamah.com\/wp-content\/uploads\/2013\/10\/configure_sphinxbase_correct.png 666w, http:\/\/robot.laylamah.com\/wp-content\/uploads\/2013\/10\/configure_sphinxbase_correct-176x300.png 176w\" sizes=\"auto, (max-width: 666px) 100vw, 666px\" \/><\/a><\/p>\n<h5>Addendum:<\/h5>\n<p>I primarily posted this because I ran into the pulseaudio issue, and there were no good resources on the subject (most people just recommended installing and configuring pulseaudio, and allowing sphinxbase to use that instead of alsa). I wanted to avoid the overhead and keep things simple, so I dug deeper. After reading through the application and library source code (<em>&lt;3 open source!<\/em>), making some sandbox code to attempt to reproduce the error, etc., I finally figured out how to get sphinxbase to use alsa as intended.<\/p>\n<p>I am hopeful that this post may save someone else the trouble I went through!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I decided to try to install PocketSphinx for Raspberry Pi for offline speech-to-text processing (until now, I&#8217;ve been using the Google Translate APIs for speech-to-text). The short version is that the out-of-the-box experience isn&#8217;t very good. It can recognize &#8220;Hello&#8221; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,14],"tags":[18,17,15,19,20,21,11,16],"class_list":["post-35","post","type-post","status-publish","format-standard","hentry","category-software","category-speech-to-text-software","tag-alsa","tag-cmu","tag-pocketsphinx","tag-pulse","tag-pulseaudio","tag-raspberry-pi","tag-speech-to-text","tag-sphinx"],"_links":{"self":[{"href":"http:\/\/robot.laylamah.com\/index.php?rest_route=\/wp\/v2\/posts\/35","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/robot.laylamah.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/robot.laylamah.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/robot.laylamah.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/robot.laylamah.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=35"}],"version-history":[{"count":22,"href":"http:\/\/robot.laylamah.com\/index.php?rest_route=\/wp\/v2\/posts\/35\/revisions"}],"predecessor-version":[{"id":59,"href":"http:\/\/robot.laylamah.com\/index.php?rest_route=\/wp\/v2\/posts\/35\/revisions\/59"}],"wp:attachment":[{"href":"http:\/\/robot.laylamah.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=35"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/robot.laylamah.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=35"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/robot.laylamah.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}