├── setup.cfg ├── .gitmodules ├── .gitignore ├── MANIFEST.in ├── appveyor.yml ├── LICENSE ├── tests ├── __init__.py ├── test_audiofile.py ├── test_kws.py ├── test_lattice.py ├── test_fsg.py ├── test_jsgf.py ├── test_lm.py ├── test_phoneme.py ├── test_config.py └── test_decoder.py ├── sphinxbase └── __init__.py ├── swig └── sphinxbase │ ├── ad_alsa.i │ ├── ad_openal.i │ ├── ad_pulse.i │ ├── ad_win32.i │ └── ad_base.i ├── .travis.yml ├── pocketsphinx └── __init__.py ├── README.md └── setup.py /setup.cfg: -------------------------------------------------------------------------------- 1 | [build_ext] 2 | inplace=1 -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "deps/pocketsphinx"] 2 | path = deps/pocketsphinx 3 | url = https://github.com/cmusphinx/pocketsphinx 4 | [submodule "deps/sphinxbase"] 5 | path = deps/sphinxbase 6 | url = https://github.com/cmusphinx/sphinxbase 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | .vscode/ 3 | build/ 4 | dist/ 5 | MANIFEST 6 | __pycache__ 7 | *.pyd 8 | *.egg-info 9 | *.so 10 | *.htk 11 | *.lat 12 | *.pyc 13 | *.log 14 | sphinxbase/ad_win32.py 15 | sphinxbase/ad_openal.py 16 | sphinxbase/ad_pulse.py 17 | sphinxbase/ad_alsa.py 18 | sphinxbase/sphinxbase.py 19 | pocketsphinx/pocketsphinx.py 20 | *_wrap.c 21 | pocketsphinx/data/ 22 | pocketsphinx/model/ 23 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | recursive-include deps/sphinxbase/include *.h 2 | prune deps/sphinxbase/include/wince 3 | recursive-include deps/sphinxbase/src/libsphinxad *.c 4 | recursive-include deps/sphinxbase/src/libsphinxbase *.c *.h 5 | recursive-include deps/pocketsphinx/include *.h 6 | recursive-include deps/pocketsphinx/src/libpocketsphinx *.c *.h 7 | global-include *.i 8 | graft pocketsphinx/model 9 | graft pocketsphinx/data 10 | include README.md LICENSE -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | version: 0.1.15 ({build}) 2 | 3 | environment: 4 | PASSWORD: 5 | secure: KZdEfjKCVSUL334LJP/iPqWKfzhoIm43D4VZwJcEb0k= 6 | 7 | matrix: 8 | - PYTHON: C:\Python27 9 | - PYTHON: C:\Python35 10 | - PYTHON: C:\Python36 11 | - PYTHON: C:\Python27-x64 12 | - PYTHON: C:\Python35-x64 13 | - PYTHON: C:\Python36-x64 14 | 15 | init: 16 | - set VS90COMNTOOLS=%VS140COMNTOOLS% 17 | - set PATH=%PYTHON%;%PYTHON%\Scripts;%PATH% 18 | 19 | install: 20 | - git submodule update --init --recursive 21 | - choco install -y swig --allow-empty-checksums 22 | - python -m pip install --upgrade pip setuptools wheel twine 23 | 24 | build: off 25 | 26 | test_script: 27 | - python setup.py test 28 | 29 | after_test: 30 | - python setup.py bdist_wheel 31 | - python setup.py bdist_wininst 32 | 33 | artifacts: 34 | - path: dist/* 35 | 36 | on_success: 37 | - if %APPVEYOR_REPO_TAG%==true twine upload -u bambucha -p %PASSWORD% dist/* 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 1999-2016 Carnegie Mellon University. All rights 2 | reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions 6 | are met: 7 | 8 | 1. Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 11 | 2. Redistributions in binary form must reproduce the above copyright 12 | notice, this list of conditions and the following disclaimer in 13 | the documentation and/or other materials provided with the 14 | distribution. 15 | 16 | This work was supported in part by funding from the Defense Advanced 17 | Research Projects Agency and the National Science Foundation of the 18 | United States of America, and the CMU Sphinx Speech Consortium. 19 | 20 | THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 21 | ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 24 | NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 1999-2016 Carnegie Mellon University. All rights 2 | # reserved. 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions 6 | # are met: 7 | # 8 | # 1. Redistributions of source code must retain the above copyright 9 | # notice, this list of conditions and the following disclaimer. 10 | # 11 | # 2. Redistributions in binary form must reproduce the above copyright 12 | # notice, this list of conditions and the following disclaimer in 13 | # the documentation and/or other materials provided with the 14 | # distribution. 15 | # 16 | # This work was supported in part by funding from the Defense Advanced 17 | # Research Projects Agency and the National Science Foundation of the 18 | # United States of America, and the CMU Sphinx Speech Consortium. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 21 | # ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 | # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 24 | # NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | -------------------------------------------------------------------------------- /tests/test_audiofile.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 1999-2016 Carnegie Mellon University. All rights 2 | # reserved. 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions 6 | # are met: 7 | # 8 | # 1. Redistributions of source code must retain the above copyright 9 | # notice, this list of conditions and the following disclaimer. 10 | # 11 | # 2. Redistributions in binary form must reproduce the above copyright 12 | # notice, this list of conditions and the following disclaimer in 13 | # the documentation and/or other materials provided with the 14 | # distribution. 15 | # 16 | # This work was supported in part by funding from the Defense Advanced 17 | # Research Projects Agency and the National Science Foundation of the 18 | # United States of America, and the CMU Sphinx Speech Consortium. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 21 | # ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 | # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 24 | # NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | from unittest import TestCase 32 | from pocketsphinx import AudioFile 33 | 34 | 35 | class TestAudioFile(TestCase): 36 | 37 | def test_audiofile(self): 38 | hypothesis = '' 39 | for phrase in AudioFile(): 40 | hypothesis = str(phrase) 41 | self.assertEqual(hypothesis, 'go forward ten meters') 42 | -------------------------------------------------------------------------------- /sphinxbase/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 1999-2016 Carnegie Mellon University. All rights 2 | # reserved. 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions 6 | # are met: 7 | # 8 | # 1. Redistributions of source code must retain the above copyright 9 | # notice, this list of conditions and the following disclaimer. 10 | # 11 | # 2. Redistributions in binary form must reproduce the above copyright 12 | # notice, this list of conditions and the following disclaimer in 13 | # the documentation and/or other materials provided with the 14 | # distribution. 15 | # 16 | # This work was supported in part by funding from the Defense Advanced 17 | # Research Projects Agency and the National Science Foundation of the 18 | # United States of America, and the CMU Sphinx Speech Consortium. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 21 | # ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 | # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 24 | # NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | import sys 33 | 34 | if sys.platform.startswith('win'): 35 | from .ad_win32 import * 36 | elif sys.platform.startswith('darwin'): 37 | from .ad_openal import * 38 | elif sys.platform.startswith('linux'): 39 | try: 40 | from .ad_pulse import * 41 | except: 42 | from .ad_alsa import * 43 | 44 | from .sphinxbase import * 45 | -------------------------------------------------------------------------------- /tests/test_kws.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 1999-2016 Carnegie Mellon University. All rights 2 | # reserved. 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions 6 | # are met: 7 | # 8 | # 1. Redistributions of source code must retain the above copyright 9 | # notice, this list of conditions and the following disclaimer. 10 | # 11 | # 2. Redistributions in binary form must reproduce the above copyright 12 | # notice, this list of conditions and the following disclaimer in 13 | # the documentation and/or other materials provided with the 14 | # distribution. 15 | # 16 | # This work was supported in part by funding from the Defense Advanced 17 | # Research Projects Agency and the National Science Foundation of the 18 | # United States of America, and the CMU Sphinx Speech Consortium. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 21 | # ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 | # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 24 | # NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | from unittest import TestCase 32 | from pocketsphinx import AudioFile 33 | 34 | 35 | class TestKws(TestCase): 36 | 37 | def test_kws(self): 38 | segments = [] 39 | for phrase in AudioFile(lm=False, keyphrase='forward', 40 | kws_threshold=1e+20): 41 | segments = phrase.segments(detailed=True) 42 | self.assertEqual(segments, [('forward', -617, 63, 121)]) 43 | -------------------------------------------------------------------------------- /tests/test_lattice.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 1999-2016 Carnegie Mellon University. All rights 2 | # reserved. 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions 6 | # are met: 7 | # 8 | # 1. Redistributions of source code must retain the above copyright 9 | # notice, this list of conditions and the following disclaimer. 10 | # 11 | # 2. Redistributions in binary form must reproduce the above copyright 12 | # notice, this list of conditions and the following disclaimer in 13 | # the documentation and/or other materials provided with the 14 | # distribution. 15 | # 16 | # This work was supported in part by funding from the Defense Advanced 17 | # Research Projects Agency and the National Science Foundation of the 18 | # United States of America, and the CMU Sphinx Speech Consortium. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 21 | # ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 | # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 24 | # NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | from unittest import TestCase 32 | from pocketsphinx import Pocketsphinx 33 | 34 | 35 | class TestLattice(TestCase): 36 | 37 | def test_lattice(self): 38 | ps = Pocketsphinx() 39 | ps.decode() 40 | 41 | lattice = ps.get_lattice() 42 | self.assertEqual(lattice.write('tests/goforward.lat'), None) 43 | 44 | lattice = ps.get_lattice() 45 | self.assertEqual(lattice.write_htk('tests/goforward.htk'), None) 46 | -------------------------------------------------------------------------------- /tests/test_fsg.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 1999-2016 Carnegie Mellon University. All rights 2 | # reserved. 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions 6 | # are met: 7 | # 8 | # 1. Redistributions of source code must retain the above copyright 9 | # notice, this list of conditions and the following disclaimer. 10 | # 11 | # 2. Redistributions in binary form must reproduce the above copyright 12 | # notice, this list of conditions and the following disclaimer in 13 | # the documentation and/or other materials provided with the 14 | # distribution. 15 | # 16 | # This work was supported in part by funding from the Defense Advanced 17 | # Research Projects Agency and the National Science Foundation of the 18 | # United States of America, and the CMU Sphinx Speech Consortium. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 21 | # ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 | # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 24 | # NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | from unittest import TestCase 32 | from pocketsphinx import LogMath, FsgModel 33 | 34 | 35 | class TestFsg(TestCase): 36 | 37 | def test_fsg_word_add(self): 38 | fm = FsgModel('simple_grammar', LogMath(), 1.0, 10) 39 | fm.word_add('hello') 40 | fm.word_add('world') 41 | self.assertGreaterEqual(fm.word_id('world'), 0) 42 | 43 | def test_fsg_add_silence(self): 44 | fm = FsgModel('simple_grammar', LogMath(), 1.0, 10) 45 | fm.add_silence('', 1, 0.5) 46 | self.assertGreaterEqual(fm.word_id(''), 0) 47 | -------------------------------------------------------------------------------- /tests/test_jsgf.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 1999-2016 Carnegie Mellon University. All rights 2 | # reserved. 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions 6 | # are met: 7 | # 8 | # 1. Redistributions of source code must retain the above copyright 9 | # notice, this list of conditions and the following disclaimer. 10 | # 11 | # 2. Redistributions in binary form must reproduce the above copyright 12 | # notice, this list of conditions and the following disclaimer in 13 | # the documentation and/or other materials provided with the 14 | # distribution. 15 | # 16 | # This work was supported in part by funding from the Defense Advanced 17 | # Research Projects Agency and the National Science Foundation of the 18 | # United States of America, and the CMU Sphinx Speech Consortium. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 21 | # ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 | # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 24 | # NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | from unittest import TestCase 32 | from pocketsphinx import Pocketsphinx, Jsgf 33 | 34 | 35 | class TestJsgf(TestCase): 36 | 37 | def test_jsgf(self): 38 | ps = Pocketsphinx( 39 | lm='deps/pocketsphinx/test/data/turtle.lm.bin', 40 | dic='deps/pocketsphinx/test/data/turtle.dic' 41 | ) 42 | 43 | # Decoding with 'turtle' language model 44 | ps.decode() 45 | self.assertEqual(ps.hypothesis(), 'go forward ten meters') 46 | 47 | # Switch to JSGF grammar 48 | jsgf = Jsgf('deps/pocketsphinx/test/data/goforward.gram') 49 | rule = jsgf.get_rule('goforward.move2') 50 | fsg = jsgf.build_fsg(rule, ps.get_logmath(), 7.5) 51 | ps.set_fsg('goforward', fsg) 52 | ps.set_search('goforward') 53 | 54 | # Decoding with 'goforward' grammar 55 | ps.decode() 56 | self.assertEqual(ps.hypothesis(), 'go forward ten meters') 57 | -------------------------------------------------------------------------------- /swig/sphinxbase/ad_alsa.i: -------------------------------------------------------------------------------- 1 | /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 | /* ==================================================================== 3 | * Copyright (c) 2013 Carnegie Mellon University. All rights 4 | * reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * This work was supported in part by funding from the Defense Advanced 19 | * Research Projects Agency and the National Science Foundation of the 20 | * United States of America, and the CMU Sphinx Speech Consortium. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 23 | * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 24 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 26 | * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * ==================================================================== 35 | * 36 | */ 37 | 38 | %define DOCSTRING 39 | "This documentation was automatically generated using original comments in 40 | Doxygen format. As some C types and data structures cannot be directly mapped 41 | into Python types, some non-trivial type conversion could have place. 42 | Basically a type is replaced with another one that has the closest match, and 43 | sometimes one argument of generated function comprises several arguments of the 44 | original function (usually two). 45 | 46 | Functions having error code as the return value and returning effective 47 | value in one of its arguments are transformed so that the effective value is 48 | returned in a regular fashion and run-time exception is being thrown in case of 49 | negative error code." 50 | %enddef 51 | 52 | %module(docstring=DOCSTRING) ad_alsa 53 | 54 | %feature("autodoc", "1"); 55 | 56 | %include ad_base.i 57 | -------------------------------------------------------------------------------- /swig/sphinxbase/ad_openal.i: -------------------------------------------------------------------------------- 1 | /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 | /* ==================================================================== 3 | * Copyright (c) 2013 Carnegie Mellon University. All rights 4 | * reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * This work was supported in part by funding from the Defense Advanced 19 | * Research Projects Agency and the National Science Foundation of the 20 | * United States of America, and the CMU Sphinx Speech Consortium. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 23 | * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 24 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 26 | * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * ==================================================================== 35 | * 36 | */ 37 | 38 | %define DOCSTRING 39 | "This documentation was automatically generated using original comments in 40 | Doxygen format. As some C types and data structures cannot be directly mapped 41 | into Python types, some non-trivial type conversion could have place. 42 | Basically a type is replaced with another one that has the closest match, and 43 | sometimes one argument of generated function comprises several arguments of the 44 | original function (usually two). 45 | 46 | Functions having error code as the return value and returning effective 47 | value in one of its arguments are transformed so that the effective value is 48 | returned in a regular fashion and run-time exception is being thrown in case of 49 | negative error code." 50 | %enddef 51 | 52 | %module(docstring=DOCSTRING) ad_openal 53 | 54 | %feature("autodoc", "1"); 55 | 56 | %include ad_base.i 57 | -------------------------------------------------------------------------------- /swig/sphinxbase/ad_pulse.i: -------------------------------------------------------------------------------- 1 | /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 | /* ==================================================================== 3 | * Copyright (c) 2013 Carnegie Mellon University. All rights 4 | * reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * This work was supported in part by funding from the Defense Advanced 19 | * Research Projects Agency and the National Science Foundation of the 20 | * United States of America, and the CMU Sphinx Speech Consortium. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 23 | * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 24 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 26 | * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * ==================================================================== 35 | * 36 | */ 37 | 38 | %define DOCSTRING 39 | "This documentation was automatically generated using original comments in 40 | Doxygen format. As some C types and data structures cannot be directly mapped 41 | into Python types, some non-trivial type conversion could have place. 42 | Basically a type is replaced with another one that has the closest match, and 43 | sometimes one argument of generated function comprises several arguments of the 44 | original function (usually two). 45 | 46 | Functions having error code as the return value and returning effective 47 | value in one of its arguments are transformed so that the effective value is 48 | returned in a regular fashion and run-time exception is being thrown in case of 49 | negative error code." 50 | %enddef 51 | 52 | %module(docstring=DOCSTRING) ad_pulse 53 | 54 | %feature("autodoc", "1"); 55 | 56 | %include ad_base.i 57 | -------------------------------------------------------------------------------- /swig/sphinxbase/ad_win32.i: -------------------------------------------------------------------------------- 1 | /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 | /* ==================================================================== 3 | * Copyright (c) 2013 Carnegie Mellon University. All rights 4 | * reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * This work was supported in part by funding from the Defense Advanced 19 | * Research Projects Agency and the National Science Foundation of the 20 | * United States of America, and the CMU Sphinx Speech Consortium. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 23 | * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 24 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 26 | * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * ==================================================================== 35 | * 36 | */ 37 | 38 | %define DOCSTRING 39 | "This documentation was automatically generated using original comments in 40 | Doxygen format. As some C types and data structures cannot be directly mapped 41 | into Python types, some non-trivial type conversion could have place. 42 | Basically a type is replaced with another one that has the closest match, and 43 | sometimes one argument of generated function comprises several arguments of the 44 | original function (usually two). 45 | 46 | Functions having error code as the return value and returning effective 47 | value in one of its arguments are transformed so that the effective value is 48 | returned in a regular fashion and run-time exception is being thrown in case of 49 | negative error code." 50 | %enddef 51 | 52 | %module(docstring=DOCSTRING) ad_win32 53 | 54 | %feature("autodoc", "1"); 55 | 56 | %include ad_base.i 57 | -------------------------------------------------------------------------------- /tests/test_lm.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 1999-2016 Carnegie Mellon University. All rights 2 | # reserved. 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions 6 | # are met: 7 | # 8 | # 1. Redistributions of source code must retain the above copyright 9 | # notice, this list of conditions and the following disclaimer. 10 | # 11 | # 2. Redistributions in binary form must reproduce the above copyright 12 | # notice, this list of conditions and the following disclaimer in 13 | # the documentation and/or other materials provided with the 14 | # distribution. 15 | # 16 | # This work was supported in part by funding from the Defense Advanced 17 | # Research Projects Agency and the National Science Foundation of the 18 | # United States of America, and the CMU Sphinx Speech Consortium. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 21 | # ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 | # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 24 | # NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | from unittest import TestCase 32 | from pocketsphinx import Pocketsphinx, NGramModel 33 | 34 | 35 | class TestLm(TestCase): 36 | 37 | def test_lm(self): 38 | ps = Pocketsphinx( 39 | dic='deps/pocketsphinx/test/data/defective.dic', 40 | mmap=False 41 | ) 42 | 43 | # Decoding with 'defective' dictionary 44 | ps.decode() 45 | self.assertEqual(ps.hypothesis(), '') 46 | 47 | # Switch to 'turtle' language model 48 | turtle_lm = 'deps/pocketsphinx/test/data/turtle.lm.bin' 49 | lm = NGramModel(ps.get_config(), ps.get_logmath(), turtle_lm) 50 | ps.set_lm('turtle', lm) 51 | ps.set_search('turtle') 52 | 53 | # Decoding with 'turtle' language model 54 | ps.decode() 55 | self.assertEqual(ps.hypothesis(), '') 56 | 57 | # The word 'meters' isn't in the loaded dictionary 58 | # Let's add it manually 59 | ps.add_word('foobie', 'F UW B IY', False) 60 | ps.add_word('meters', 'M IY T ER Z', True) 61 | 62 | # Decoding with 'turtle' language model 63 | ps.decode() 64 | self.assertEqual(ps.hypothesis(), 'foobie meters meters') 65 | -------------------------------------------------------------------------------- /tests/test_phoneme.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 1999-2016 Carnegie Mellon University. All rights 2 | # reserved. 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions 6 | # are met: 7 | # 8 | # 1. Redistributions of source code must retain the above copyright 9 | # notice, this list of conditions and the following disclaimer. 10 | # 11 | # 2. Redistributions in binary form must reproduce the above copyright 12 | # notice, this list of conditions and the following disclaimer in 13 | # the documentation and/or other materials provided with the 14 | # distribution. 15 | # 16 | # This work was supported in part by funding from the Defense Advanced 17 | # Research Projects Agency and the National Science Foundation of the 18 | # United States of America, and the CMU Sphinx Speech Consortium. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 21 | # ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 | # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 24 | # NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | from unittest import TestCase 32 | from pocketsphinx import Pocketsphinx 33 | 34 | 35 | class TestPhoneme(TestCase): 36 | 37 | def __init__(self, *args, **kwargs): 38 | self.ps = Pocketsphinx( 39 | lm=False, 40 | dic=False, 41 | allphone='deps/pocketsphinx/model/en-us/en-us-phone.lm.bin', 42 | lw=2.0, 43 | pip=0.3, 44 | beam=1e-200, 45 | pbeam=1e-20, 46 | mmap=False 47 | ) 48 | self.ps.decode() 49 | super(TestPhoneme, self).__init__(*args, **kwargs) 50 | 51 | def test_phoneme_hypothesis(self): 52 | self.assertEqual( 53 | self.ps.hypothesis(), 54 | 'SIL G OW F AO R D T AE N NG IY ZH ER S SIL' 55 | ) 56 | 57 | def test_phoneme_best_phonemes(self): 58 | self.assertEqual(self.ps.segments(), [ 59 | 'SIL', 60 | 'G', 61 | 'OW', 62 | 'F', 63 | 'AO', 64 | 'R', 65 | 'D', 66 | 'T', 67 | 'AE', 68 | 'N', 69 | 'NG', 70 | 'IY', 71 | 'ZH', 72 | 'ER', 73 | 'S', 74 | 'SIL' 75 | ]) 76 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | 3 | env: 4 | global: 5 | secure: kRjum2tZsaQZPnti27bc/ZIjYS1hftXTyuSWdq/6raRignORgbZMUc/RMXBz+ig8kiEVE3SFBZdapECGj+3Vb6Q6Vi4ypv8lR4rtJMTNITSOeM1KHHDCi6xLLGKrOsJ5ndkYHTNxuyIMGZu9Sv3TMcNqHcBChbZKT383OXgrxLA= 6 | 7 | matrix: 8 | include: 9 | - os: osx 10 | osx_image: xcode6.4 11 | language: generic 12 | env: ENABLE_LIBSPHINXAD=1 BUILD_WHEEL=true 13 | - os: osx 14 | osx_image: xcode8 15 | language: generic 16 | env: ENABLE_LIBSPHINXAD=1 BUILD_WHEEL=true 17 | - os: osx 18 | osx_image: xcode8.3 19 | language: generic 20 | env: ENABLE_LIBSPHINXAD=1 BUILD_WHEEL=true 21 | - os: osx 22 | osx_image: xcode9.3 23 | language: generic 24 | env: ENABLE_LIBSPHINXAD=1 BUILD_WHEEL=true 25 | - os: linux 26 | language: python 27 | python: 2.7 28 | env: ENABLE_LIBSPHINXAD=1 29 | - os: linux 30 | language: python 31 | python: 3.5 32 | env: ENABLE_LIBSPHINXAD=1 33 | - os: linux 34 | language: python 35 | python: 3.6 36 | env: ENABLE_LIBSPHINXAD=1 BUILD_SDIST=true 37 | 38 | before_install: 39 | - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update; fi 40 | - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew reinstall swig python python@2; fi 41 | - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then export PATH=$HOME/Library/Python/3.6/bin:$PATH; fi 42 | - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get update -qq; fi 43 | - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get install -y swig libpulse-dev libasound2-dev; fi 44 | 45 | install: 46 | - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then python2 -m pip install --user --upgrade pip setuptools wheel twine; fi 47 | - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then python3 -m pip install --user --upgrade pip setuptools wheel twine; fi 48 | - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then python -m pip install --upgrade pip setuptools wheel twine; fi 49 | 50 | script: 51 | - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then python2 setup.py test; fi 52 | - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then python3 setup.py test; fi 53 | - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then python setup.py test; fi 54 | 55 | before_deploy: 56 | - if [[ "$TRAVIS_OS_NAME" == "osx" && "$BUILD_SDIST" == "true" ]]; then python2 setup.py sdist; fi 57 | - if [[ "$TRAVIS_OS_NAME" == "osx" && "$BUILD_WHEEL" == "true" ]]; then python2 setup.py bdist_wheel; fi 58 | - if [[ "$TRAVIS_OS_NAME" == "osx" && "$BUILD_SDIST" == "true" ]]; then python3 setup.py sdist; fi 59 | - if [[ "$TRAVIS_OS_NAME" == "osx" && "$BUILD_WHEEL" == "true" ]]; then python3 setup.py bdist_wheel; fi 60 | - if [[ "$TRAVIS_OS_NAME" == "linux" && "$BUILD_SDIST" == "true" ]]; then python setup.py sdist; fi 61 | - if [[ "$TRAVIS_OS_NAME" == "linux" && "$BUILD_WHEEL" == "true" ]]; then python setup.py bdist_wheel; fi 62 | 63 | deploy: 64 | provider: script 65 | script: twine upload -u bambucha -p $PASSWORD dist/* 66 | on: 67 | tags: true 68 | branch: master 69 | condition: '"$BUILD_SDIST" == "true" || "$BUILD_WHEEL" == "true"' 70 | skip_cleanup: true 71 | -------------------------------------------------------------------------------- /tests/test_config.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 1999-2016 Carnegie Mellon University. All rights 2 | # reserved. 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions 6 | # are met: 7 | # 8 | # 1. Redistributions of source code must retain the above copyright 9 | # notice, this list of conditions and the following disclaimer. 10 | # 11 | # 2. Redistributions in binary form must reproduce the above copyright 12 | # notice, this list of conditions and the following disclaimer in 13 | # the documentation and/or other materials provided with the 14 | # distribution. 15 | # 16 | # This work was supported in part by funding from the Defense Advanced 17 | # Research Projects Agency and the National Science Foundation of the 18 | # United States of America, and the CMU Sphinx Speech Consortium. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 21 | # ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 | # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 24 | # NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | from unittest import TestCase 32 | from pocketsphinx import DefaultConfig 33 | 34 | 35 | class TestConfig(TestCase): 36 | 37 | def test_config_get_float(self): 38 | config = DefaultConfig() 39 | self.assertEqual(config.get_float('-samprate'), 16000.0) 40 | 41 | def test_config_set_float(self): 42 | config = DefaultConfig() 43 | config.set_float('-samprate', 8000.0) 44 | self.assertEqual(config.get_float('-samprate'), 8000.0) 45 | 46 | def test_config_get_int(self): 47 | config = DefaultConfig() 48 | self.assertEqual(config.get_int('-nfft'), 512) 49 | 50 | def test_config_set_int(self): 51 | config = DefaultConfig() 52 | config.set_int('-nfft', 256) 53 | self.assertEqual(config.get_int('-nfft'), 256) 54 | 55 | def test_config_get_string(self): 56 | config = DefaultConfig() 57 | self.assertEqual(config.get_string('-rawlogdir'), None) 58 | 59 | def test_config_set_string(self): 60 | config = DefaultConfig() 61 | config.set_string('-rawlogdir', '~/pocketsphinx') 62 | self.assertEqual(config.get_string('-rawlogdir'), '~/pocketsphinx') 63 | 64 | def test_config_get_boolean(self): 65 | config = DefaultConfig() 66 | self.assertEqual(config.get_boolean('-backtrace'), False) 67 | 68 | def test_config_set_boolean(self): 69 | config = DefaultConfig() 70 | config.set_boolean('-backtrace', True) 71 | self.assertEqual(config.get_boolean('-backtrace'), True) 72 | -------------------------------------------------------------------------------- /swig/sphinxbase/ad_base.i: -------------------------------------------------------------------------------- 1 | /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 | /* ==================================================================== 3 | * Copyright (c) 2013 Carnegie Mellon University. All rights 4 | * reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * This work was supported in part by funding from the Defense Advanced 19 | * Research Projects Agency and the National Science Foundation of the 20 | * United States of America, and the CMU Sphinx Speech Consortium. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 23 | * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 24 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 26 | * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * ==================================================================== 35 | * 36 | */ 37 | 38 | %include pybuffer.i 39 | %include typemaps.i 40 | 41 | %{ 42 | #include 43 | typedef ad_rec_t Ad; 44 | %} 45 | 46 | typedef struct {} Ad; 47 | 48 | %extend Ad { 49 | Ad(const char *audio_device=NULL, int sampling_rate=16000, int *errcode) { 50 | Ad *ad; 51 | if (audio_device == NULL) 52 | ad = ad_open_sps(sampling_rate); 53 | else 54 | ad = ad_open_dev(audio_device, sampling_rate); 55 | *errcode = ad ? 0 : -1; 56 | return ad; 57 | } 58 | 59 | ~Ad() { 60 | ad_close($self); 61 | } 62 | 63 | Ad *__enter__(int *errcode) { 64 | *errcode = ad_start_rec($self); 65 | return $self; 66 | } 67 | 68 | void __exit__(PyObject *exception_type, PyObject *exception_value, 69 | PyObject *exception_traceback, int *errcode) { 70 | *errcode = ad_stop_rec($self); 71 | } 72 | 73 | int start_recording(int *errcode) { 74 | return *errcode = ad_start_rec($self); 75 | } 76 | 77 | int stop_recording(int *errcode) { 78 | return *errcode = ad_stop_rec($self); 79 | } 80 | 81 | %include 82 | %pybuffer_mutable_binary(char *DATA, size_t SIZE); 83 | int readinto(char *DATA, size_t SIZE, int *errcode) { 84 | return *errcode = ad_read($self, (int16*)DATA, SIZE /= sizeof(int16)); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /tests/test_decoder.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 1999-2016 Carnegie Mellon University. All rights 2 | # reserved. 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions 6 | # are met: 7 | # 8 | # 1. Redistributions of source code must retain the above copyright 9 | # notice, this list of conditions and the following disclaimer. 10 | # 11 | # 2. Redistributions in binary form must reproduce the above copyright 12 | # notice, this list of conditions and the following disclaimer in 13 | # the documentation and/or other materials provided with the 14 | # distribution. 15 | # 16 | # This work was supported in part by funding from the Defense Advanced 17 | # Research Projects Agency and the National Science Foundation of the 18 | # United States of America, and the CMU Sphinx Speech Consortium. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 21 | # ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 | # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 24 | # NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | from unittest import TestCase 32 | from pocketsphinx import Pocketsphinx 33 | 34 | 35 | class TestRawDecoder(TestCase): 36 | 37 | def __init__(self, *args, **kwargs): 38 | self.ps = Pocketsphinx() 39 | self.ps.decode() 40 | super(TestRawDecoder, self).__init__(*args, **kwargs) 41 | 42 | def test_raw_decoder_lookup_word(self): 43 | self.assertEqual(self.ps.lookup_word('hello'), 'HH AH L OW') 44 | self.assertEqual(self.ps.lookup_word('abcdf'), None) 45 | 46 | def test_raw_decoder_hypothesis(self): 47 | self.assertEqual(self.ps.hypothesis(), 'go forward ten meters') 48 | self.assertEqual(self.ps.score(), -7066) 49 | self.assertEqual(self.ps.confidence(), 0.04042641466841839) 50 | 51 | def test_raw_decoder_segments(self): 52 | self.assertEqual(self.ps.segments(), [ 53 | '', '', 'go', 'forward', 'ten', 'meters', '' 54 | ]) 55 | 56 | def test_raw_decoder_best_hypothesis(self): 57 | self.assertEqual(self.ps.best(), [ 58 | ('go forward ten meters', -28034), 59 | ('go for word ten meters', -28570), 60 | ('go forward and majors', -28670), 61 | ('go forward and meters', -28681), 62 | ('go forward and readers', -28685), 63 | ('go forward ten readers', -28688), 64 | ('go forward ten leaders', -28695), 65 | ('go forward can meters', -28695), 66 | ('go forward and leaders', -28706), 67 | ('go for work ten meters', -28722) 68 | ]) 69 | 70 | 71 | class TestCepDecoder(TestCase): 72 | 73 | def test_cep_decoder_hypothesis(self): 74 | ps = Pocketsphinx() 75 | with open('deps/pocketsphinx/test/data/goforward.mfc', 'rb') as f: 76 | with ps.start_utterance(): 77 | f.read(4) 78 | buf = f.read(13780) 79 | ps.process_cep(buf, False, True) 80 | self.assertEqual(ps.hypothesis(), 'go forward ten meters') 81 | self.assertEqual(ps.score(), -7095) 82 | self.assertEqual(ps.probability(), -32715) 83 | -------------------------------------------------------------------------------- /pocketsphinx/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 1999-2016 Carnegie Mellon University. All rights 2 | # reserved. 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions 6 | # are met: 7 | # 8 | # 1. Redistributions of source code must retain the above copyright 9 | # notice, this list of conditions and the following disclaimer. 10 | # 11 | # 2. Redistributions in binary form must reproduce the above copyright 12 | # notice, this list of conditions and the following disclaimer in 13 | # the documentation and/or other materials provided with the 14 | # distribution. 15 | # 16 | # This work was supported in part by funding from the Defense Advanced 17 | # Research Projects Agency and the National Science Foundation of the 18 | # United States of America, and the CMU Sphinx Speech Consortium. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 21 | # ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 | # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 24 | # NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | import os 32 | import sys 33 | import signal 34 | from contextlib import contextmanager 35 | from sphinxbase import * 36 | from .pocketsphinx import * 37 | 38 | 39 | DefaultConfig = Decoder.default_config 40 | 41 | 42 | def get_model_path(): 43 | """ Return path to the model. """ 44 | return os.path.join(os.path.dirname(__file__), 'model') 45 | 46 | 47 | def get_data_path(): 48 | """ Return path to the data. """ 49 | return os.path.join(os.path.dirname(__file__), 'data') 50 | 51 | 52 | class Pocketsphinx(Decoder): 53 | 54 | def __init__(self, **kwargs): 55 | model_path = get_model_path() 56 | data_path = get_data_path() 57 | 58 | self.goforward = os.path.join(data_path, 'goforward.raw') 59 | 60 | if kwargs.get('dic') is not None and kwargs.get('dict') is None: 61 | kwargs['dict'] = kwargs.pop('dic') 62 | 63 | if kwargs.get('hmm') is None: 64 | kwargs['hmm'] = os.path.join(model_path, 'en-us') 65 | 66 | if kwargs.get('lm') is None: 67 | kwargs['lm'] = os.path.join(model_path, 'en-us.lm.bin') 68 | 69 | if kwargs.get('dict') is None: 70 | kwargs['dict'] = os.path.join(model_path, 'cmudict-en-us.dict') 71 | 72 | if kwargs.pop('verbose', False) is False: 73 | if sys.platform.startswith('win'): 74 | kwargs['logfn'] = 'nul' 75 | else: 76 | kwargs['logfn'] = '/dev/null' 77 | 78 | config = DefaultConfig() 79 | 80 | for key, value in kwargs.items(): 81 | if isinstance(value, bool): 82 | config.set_boolean('-{}'.format(key), value) 83 | elif isinstance(value, int): 84 | config.set_int('-{}'.format(key), value) 85 | elif isinstance(value, float): 86 | config.set_float('-{}'.format(key), value) 87 | elif isinstance(value, str): 88 | config.set_string('-{}'.format(key), value) 89 | 90 | super(Pocketsphinx, self).__init__(config) 91 | 92 | def __str__(self): 93 | return self.hypothesis() 94 | 95 | @contextmanager 96 | def start_utterance(self): 97 | self.start_utt() 98 | yield 99 | self.end_utt() 100 | 101 | @contextmanager 102 | def end_utterance(self): 103 | self.end_utt() 104 | yield 105 | self.start_utt() 106 | 107 | def decode(self, audio_file=None, buffer_size=2048, 108 | no_search=False, full_utt=False): 109 | buf = bytearray(buffer_size) 110 | with open(audio_file or self.goforward, 'rb') as f: 111 | with self.start_utterance(): 112 | while f.readinto(buf): 113 | self.process_raw(buf, no_search, full_utt) 114 | return self 115 | 116 | def segments(self, detailed=False): 117 | if detailed: 118 | return [ 119 | (s.word, s.prob, s.start_frame, s.end_frame) 120 | for s in self.seg() 121 | ] 122 | else: 123 | return [s.word for s in self.seg()] 124 | 125 | def hypothesis(self): 126 | hyp = self.hyp() 127 | if hyp: 128 | return hyp.hypstr 129 | else: 130 | return '' 131 | 132 | def probability(self): 133 | hyp = self.hyp() 134 | if hyp: 135 | return hyp.prob 136 | 137 | def score(self): 138 | hyp = self.hyp() 139 | if hyp: 140 | return hyp.best_score 141 | 142 | def best(self, count=10): 143 | return [ 144 | (h.hypstr, h.score) 145 | for h, i in zip(self.nbest(), range(count)) 146 | ] 147 | 148 | def confidence(self): 149 | hyp = self.hyp() 150 | if hyp: 151 | return self.get_logmath().exp(hyp.prob) 152 | 153 | 154 | class AudioFile(Pocketsphinx): 155 | 156 | def __init__(self, **kwargs): 157 | signal.signal(signal.SIGINT, self.stop) 158 | 159 | self.audio_file = kwargs.pop('audio_file', None) 160 | self.buffer_size = kwargs.pop('buffer_size', 2048) 161 | self.no_search = kwargs.pop('no_search', False) 162 | self.full_utt = kwargs.pop('full_utt', False) 163 | 164 | self.keyphrase = kwargs.get('keyphrase') 165 | 166 | self.in_speech = False 167 | self.buf = bytearray(self.buffer_size) 168 | 169 | super(AudioFile, self).__init__(**kwargs) 170 | 171 | self.f = open(self.audio_file or self.goforward, 'rb') 172 | 173 | def __iter__(self): 174 | with self.f: 175 | with self.start_utterance(): 176 | while self.f.readinto(self.buf): 177 | self.process_raw(self.buf, self.no_search, self.full_utt) 178 | if self.keyphrase and self.hyp(): 179 | with self.end_utterance(): 180 | yield self 181 | elif self.in_speech != self.get_in_speech(): 182 | self.in_speech = self.get_in_speech() 183 | if not self.in_speech and self.hyp(): 184 | with self.end_utterance(): 185 | yield self 186 | 187 | def stop(self, *args, **kwargs): 188 | raise StopIteration 189 | 190 | 191 | class LiveSpeech(Pocketsphinx): 192 | 193 | def __init__(self, **kwargs): 194 | signal.signal(signal.SIGINT, self.stop) 195 | 196 | self.audio_device = kwargs.pop('audio_device', None) 197 | self.sampling_rate = kwargs.pop('sampling_rate', 16000) 198 | self.buffer_size = kwargs.pop('buffer_size', 2048) 199 | self.no_search = kwargs.pop('no_search', False) 200 | self.full_utt = kwargs.pop('full_utt', False) 201 | 202 | self.keyphrase = kwargs.get('keyphrase') 203 | 204 | self.in_speech = False 205 | self.buf = bytearray(self.buffer_size) 206 | self.ad = Ad(self.audio_device, self.sampling_rate) 207 | 208 | super(LiveSpeech, self).__init__(**kwargs) 209 | 210 | def __iter__(self): 211 | with self.ad: 212 | with self.start_utterance(): 213 | while self.ad.readinto(self.buf) >= 0: 214 | self.process_raw(self.buf, self.no_search, self.full_utt) 215 | if self.keyphrase and self.hyp(): 216 | with self.end_utterance(): 217 | yield self 218 | elif self.in_speech != self.get_in_speech(): 219 | self.in_speech = self.get_in_speech() 220 | if not self.in_speech and self.hyp(): 221 | with self.end_utterance(): 222 | yield self 223 | 224 | def stop(self, *args, **kwargs): 225 | raise StopIteration 226 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Pocketsphinx Python 2 | 3 | [![Latest Version](https://img.shields.io/pypi/v/pocketsphinx.svg?maxAge=86400)](https://pypi.org/project/pocketsphinx) 4 | [![Development Status](https://img.shields.io/pypi/status/pocketsphinx.svg?maxAge=86400)](https://pypi.org/project/pocketsphinx) 5 | [![Supported Python Versions](https://img.shields.io/pypi/pyversions/pocketsphinx.svg?maxAge=86400)](https://pypi.org/project/pocketsphinx) 6 | [![Travis Build Status](https://travis-ci.org/bambocher/pocketsphinx-python.svg?branch=master)](https://travis-ci.org/bambocher/pocketsphinx-python) 7 | [![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/v2rvnt181dox00jr/branch/master?svg=true)](https://ci.appveyor.com/project/bambocher/pocketsphinx-python/branch/master) 8 | [![License](https://img.shields.io/pypi/l/pocketsphinx.svg?maxAge=86400)](https://pypi.org/project/pocketsphinx) 9 | 10 | > **Warning** 11 | > This repository is no longer maintained. Please use the official python bindings for PocketSphinx: [github](https://github.com/cmusphinx/pocketsphinx), [pypi](https://pypi.org/project/pocketsphinx/). 12 | 13 | Pocketsphinx is a part of the [CMU Sphinx](http://cmusphinx.sourceforge.net) Open Source Toolkit For Speech Recognition. 14 | 15 | This package provides a python interface to CMU [Sphinxbase](https://github.com/cmusphinx/sphinxbase) and [Pocketsphinx](https://github.com/cmusphinx/pocketsphinx) libraries created with [SWIG](http://www.swig.org) and [Setuptools](https://setuptools.readthedocs.io). 16 | 17 | ## Supported platforms 18 | 19 | * Windows 20 | * Linux 21 | * Mac OS X 22 | 23 | ## Installation 24 | 25 | ```shell 26 | # Make sure we have up-to-date versions of pip, setuptools and wheel 27 | python -m pip install --upgrade pip setuptools wheel 28 | pip install --upgrade pocketsphinx 29 | ``` 30 | 31 | More binary distributions for manual installation are available [here](https://pypi.org/project/pocketsphinx/#files). 32 | 33 | ## Usage 34 | 35 | ### LiveSpeech 36 | 37 | It's an iterator class for continuous recognition or keyword search from a microphone. 38 | 39 | ```python 40 | from pocketsphinx import LiveSpeech 41 | for phrase in LiveSpeech(): print(phrase) 42 | ``` 43 | 44 | An example of a keyword search: 45 | 46 | ```python 47 | from pocketsphinx import LiveSpeech 48 | 49 | speech = LiveSpeech(lm=False, keyphrase='forward', kws_threshold=1e-20) 50 | for phrase in speech: 51 | print(phrase.segments(detailed=True)) 52 | ``` 53 | 54 | With your model and dictionary: 55 | 56 | ```python 57 | import os 58 | from pocketsphinx import LiveSpeech, get_model_path 59 | 60 | model_path = get_model_path() 61 | 62 | speech = LiveSpeech( 63 | verbose=False, 64 | sampling_rate=16000, 65 | buffer_size=2048, 66 | no_search=False, 67 | full_utt=False, 68 | hmm=os.path.join(model_path, 'en-us'), 69 | lm=os.path.join(model_path, 'en-us.lm.bin'), 70 | dic=os.path.join(model_path, 'cmudict-en-us.dict') 71 | ) 72 | 73 | for phrase in speech: 74 | print(phrase) 75 | ``` 76 | 77 | ### AudioFile 78 | 79 | It's an iterator class for continuous recognition or keyword search from a file. 80 | 81 | ```python 82 | from pocketsphinx import AudioFile 83 | for phrase in AudioFile(): print(phrase) # => "go forward ten meters" 84 | ``` 85 | 86 | An example of a keyword search: 87 | 88 | ```python 89 | from pocketsphinx import AudioFile 90 | 91 | audio = AudioFile(lm=False, keyphrase='forward', kws_threshold=1e-20) 92 | for phrase in audio: 93 | print(phrase.segments(detailed=True)) # => "[('forward', -617, 63, 121)]" 94 | ``` 95 | 96 | With your model and dictionary: 97 | 98 | ```python 99 | import os 100 | from pocketsphinx import AudioFile, get_model_path, get_data_path 101 | 102 | model_path = get_model_path() 103 | data_path = get_data_path() 104 | 105 | config = { 106 | 'verbose': False, 107 | 'audio_file': os.path.join(data_path, 'goforward.raw'), 108 | 'buffer_size': 2048, 109 | 'no_search': False, 110 | 'full_utt': False, 111 | 'hmm': os.path.join(model_path, 'en-us'), 112 | 'lm': os.path.join(model_path, 'en-us.lm.bin'), 113 | 'dict': os.path.join(model_path, 'cmudict-en-us.dict') 114 | } 115 | 116 | audio = AudioFile(**config) 117 | for phrase in audio: 118 | print(phrase) 119 | ``` 120 | 121 | Convert frame into time coordinates: 122 | 123 | ```python 124 | from pocketsphinx import AudioFile 125 | 126 | # Frames per Second 127 | fps = 100 128 | 129 | for phrase in AudioFile(frate=fps): # frate (default=100) 130 | print('-' * 28) 131 | print('| %5s | %3s | %4s |' % ('start', 'end', 'word')) 132 | print('-' * 28) 133 | for s in phrase.seg(): 134 | print('| %4ss | %4ss | %8s |' % (s.start_frame / fps, s.end_frame / fps, s.word)) 135 | print('-' * 28) 136 | 137 | # ---------------------------- 138 | # | start | end | word | 139 | # ---------------------------- 140 | # | 0.0s | 0.24s | | 141 | # | 0.25s | 0.45s | | 142 | # | 0.46s | 0.63s | go | 143 | # | 0.64s | 1.16s | forward | 144 | # | 1.17s | 1.52s | ten | 145 | # | 1.53s | 2.11s | meters | 146 | # | 2.12s | 2.6s | | 147 | # ---------------------------- 148 | ``` 149 | 150 | ### Pocketsphinx 151 | 152 | It's a simple and flexible proxy class to `pocketsphinx.Decode`. 153 | 154 | ```python 155 | from pocketsphinx import Pocketsphinx 156 | print(Pocketsphinx().decode()) # => "go forward ten meters" 157 | ``` 158 | 159 | A more comprehensive example: 160 | 161 | ```python 162 | from __future__ import print_function 163 | import os 164 | from pocketsphinx import Pocketsphinx, get_model_path, get_data_path 165 | 166 | model_path = get_model_path() 167 | data_path = get_data_path() 168 | 169 | config = { 170 | 'hmm': os.path.join(model_path, 'en-us'), 171 | 'lm': os.path.join(model_path, 'en-us.lm.bin'), 172 | 'dict': os.path.join(model_path, 'cmudict-en-us.dict') 173 | } 174 | 175 | ps = Pocketsphinx(**config) 176 | ps.decode( 177 | audio_file=os.path.join(data_path, 'goforward.raw'), 178 | buffer_size=2048, 179 | no_search=False, 180 | full_utt=False 181 | ) 182 | 183 | print(ps.segments()) # => ['', '', 'go', 'forward', 'ten', 'meters', ''] 184 | print('Detailed segments:', *ps.segments(detailed=True), sep='\n') # => [ 185 | # word, prob, start_frame, end_frame 186 | # ('', 0, 0, 24) 187 | # ('', -3778, 25, 45) 188 | # ('go', -27, 46, 63) 189 | # ('forward', -38, 64, 116) 190 | # ('ten', -14105, 117, 152) 191 | # ('meters', -2152, 153, 211) 192 | # ('', 0, 212, 260) 193 | # ] 194 | 195 | print(ps.hypothesis()) # => go forward ten meters 196 | print(ps.probability()) # => -32079 197 | print(ps.score()) # => -7066 198 | print(ps.confidence()) # => 0.04042641466841839 199 | 200 | print(*ps.best(count=10), sep='\n') # => [ 201 | # ('go forward ten meters', -28034) 202 | # ('go for word ten meters', -28570) 203 | # ('go forward and majors', -28670) 204 | # ('go forward and meters', -28681) 205 | # ('go forward and readers', -28685) 206 | # ('go forward ten readers', -28688) 207 | # ('go forward ten leaders', -28695) 208 | # ('go forward can meters', -28695) 209 | # ('go forward and leaders', -28706) 210 | # ('go for work ten meters', -28722) 211 | # ] 212 | ``` 213 | 214 | ### Default config 215 | 216 | If you don't pass any argument while creating an instance of the Pocketsphinx, AudioFile or LiveSpeech class, it will use next default values: 217 | 218 | ```python 219 | verbose = False 220 | logfn = /dev/null or nul 221 | audio_file = site-packages/pocketsphinx/data/goforward.raw 222 | audio_device = None 223 | sampling_rate = 16000 224 | buffer_size = 2048 225 | no_search = False 226 | full_utt = False 227 | hmm = site-packages/pocketsphinx/model/en-us 228 | lm = site-packages/pocketsphinx/model/en-us.lm.bin 229 | dict = site-packages/pocketsphinx/model/cmudict-en-us.dict 230 | ``` 231 | 232 | Any other option must be passed into the config as is, without using symbol `-`. 233 | 234 | If you want to disable default language model or dictionary, you can change the value of the corresponding options to False: 235 | 236 | ```python 237 | lm = False 238 | dict = False 239 | ``` 240 | 241 | ### Verbose 242 | 243 | Send output to stdout: 244 | 245 | ```python 246 | from pocketsphinx import Pocketsphinx 247 | 248 | ps = Pocketsphinx(verbose=True) 249 | ps.decode() 250 | 251 | print(ps.hypothesis()) 252 | ``` 253 | 254 | Send output to file: 255 | 256 | ```python 257 | from pocketsphinx import Pocketsphinx 258 | 259 | ps = Pocketsphinx(verbose=True, logfn='pocketsphinx.log') 260 | ps.decode() 261 | 262 | print(ps.hypothesis()) 263 | ``` 264 | 265 | ### Compatibility 266 | 267 | Parent classes are still available: 268 | 269 | ```python 270 | import os 271 | from pocketsphinx import DefaultConfig, Decoder, get_model_path, get_data_path 272 | 273 | model_path = get_model_path() 274 | data_path = get_data_path() 275 | 276 | # Create a decoder with a certain model 277 | config = DefaultConfig() 278 | config.set_string('-hmm', os.path.join(model_path, 'en-us')) 279 | config.set_string('-lm', os.path.join(model_path, 'en-us.lm.bin')) 280 | config.set_string('-dict', os.path.join(model_path, 'cmudict-en-us.dict')) 281 | decoder = Decoder(config) 282 | 283 | # Decode streaming data 284 | buf = bytearray(1024) 285 | with open(os.path.join(data_path, 'goforward.raw'), 'rb') as f: 286 | decoder.start_utt() 287 | while f.readinto(buf): 288 | decoder.process_raw(buf, False, False) 289 | decoder.end_utt() 290 | print('Best hypothesis segments:', [seg.word for seg in decoder.seg()]) 291 | ``` 292 | 293 | ## Install development version 294 | 295 | ### Install requirements 296 | 297 | Windows requirements: 298 | 299 | * [Python](https://www.python.org/downloads) 300 | * [Git](http://git-scm.com/downloads) 301 | * [Swig](http://www.swig.org/download.html) 302 | * [Visual Studio Community](https://www.visualstudio.com/ru-ru/downloads/download-visual-studio-vs.aspx) 303 | 304 | Ubuntu requirements: 305 | 306 | ```shell 307 | sudo apt-get install -qq python python-dev python-pip build-essential swig git libpulse-dev libasound2-dev 308 | ``` 309 | 310 | Mac OS X requirements: 311 | 312 | ```shell 313 | brew reinstall swig python 314 | ``` 315 | 316 | ### Install with pip 317 | 318 | ```shell 319 | pip install https://github.com/bambocher/pocketsphinx-python/archive/master.zip 320 | ``` 321 | 322 | ### Install with distutils 323 | 324 | ```shell 325 | git clone --recursive https://github.com/bambocher/pocketsphinx-python 326 | cd pocketsphinx-python 327 | python setup.py install 328 | ``` 329 | 330 | ## Projects using pocketsphinx-python 331 | 332 | * [SpeechRecognition](https://github.com/Uberi/speech_recognition) - Library for performing speech recognition, with support for several engines and APIs, online and offline. 333 | 334 | ## License 335 | 336 | [The BSD License](https://github.com/bambocher/pocketsphinx-python/blob/master/LICENSE) 337 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import os 3 | import sys 4 | from shutil import copy, copytree, ignore_patterns 5 | from glob import glob 6 | 7 | from distutils import log 8 | from distutils.command.build import build as _build 9 | try: 10 | from setuptools import setup, Extension 11 | from setuptools.command.install import install as _install 12 | from setuptools.command.bdist_egg import bdist_egg as _bdist_egg 13 | except ImportError: 14 | from distutils.core import setup 15 | from distutils.extension import Extension 16 | from distutils.command.install import install as _install 17 | from distutils.command.bdist_egg import bdist_egg as _bdist_egg 18 | 19 | if sys.platform.startswith('win'): 20 | from distutils.command.bdist_msi import bdist_msi as _bdist_msi 21 | from distutils.command.bdist_wininst import bdist_wininst as _bdist_wininst 22 | 23 | 24 | def _find_vcvarsall(version): 25 | vsbase = msvc9compiler.VS_BASE % version 26 | productdir = None 27 | 28 | if version != 9.0: 29 | try: 30 | productdir = msvc9compiler.Reg.get_value(r"%s\Setup\VC" % vsbase, 31 | "productdir") 32 | except KeyError: 33 | log.debug("Unable to find productdir in registry") 34 | 35 | if not productdir or not os.path.isdir(productdir): 36 | toolskey = "VS%0.f0COMNTOOLS" % version 37 | toolsdir = os.environ.get(toolskey, None) 38 | 39 | if toolsdir and os.path.isdir(toolsdir): 40 | productdir = os.path.join(toolsdir, os.pardir, os.pardir, "VC") 41 | productdir = os.path.abspath(productdir) 42 | if not os.path.isdir(productdir): 43 | log.debug("%s is not a valid directory" % productdir) 44 | return None 45 | else: 46 | log.debug("Env var %s is not set or invalid" % toolskey) 47 | if not productdir: 48 | log.debug("No productdir found") 49 | return None 50 | vcvarsall = os.path.join(productdir, "vcvarsall.bat") 51 | if os.path.isfile(vcvarsall): 52 | return vcvarsall 53 | log.debug("Unable to find vcvarsall.bat") 54 | return None 55 | 56 | 57 | try: 58 | from distutils import msvc9compiler 59 | msvc9compiler.find_vcvarsall = _find_vcvarsall 60 | except ImportError: 61 | pass 62 | 63 | 64 | extra_compile_args = [] 65 | extra_link_args = [] 66 | 67 | libsphinxbase = ( 68 | glob('deps/sphinxbase/src/libsphinxbase/lm/*.c') + 69 | glob('deps/sphinxbase/src/libsphinxbase/feat/*.c') + 70 | glob('deps/sphinxbase/src/libsphinxbase/util/*.c') + 71 | glob('deps/sphinxbase/src/libsphinxbase/fe/*.c') 72 | ) 73 | 74 | libpocketsphinx = glob('deps/pocketsphinx/src/libpocketsphinx/*.c') 75 | 76 | sb_sources = ( 77 | libsphinxbase + 78 | ['deps/sphinxbase/swig/sphinxbase.i'] 79 | ) 80 | 81 | ps_sources = ( 82 | libsphinxbase + 83 | libpocketsphinx + 84 | ['deps/pocketsphinx/swig/pocketsphinx.i'] 85 | ) 86 | 87 | sb_include_dirs = [ 88 | 'deps/sphinxbase/include', 89 | 'deps/sphinxbase/include/sphinxbase' 90 | ] 91 | 92 | ps_include_dirs = ['deps/pocketsphinx/include'] 93 | 94 | define_macros = [ 95 | ('SPHINXBASE_EXPORTS', None), 96 | ('POCKETSPHINX_EXPORTS', None), 97 | ('SPHINX_DLL', None), 98 | ('HAVE_CONFIG_H', None) 99 | ] 100 | 101 | if sys.platform.startswith('win'): 102 | sb_include_dirs.append('deps/sphinxbase/include/win32') 103 | extra_compile_args.extend([ 104 | '/wd4244', 105 | '/wd4267', 106 | '/wd4197', 107 | '/wd4090', 108 | '/wd4018', 109 | '/wd4311', 110 | '/wd4312', 111 | '/wd4334', 112 | '/wd4477', 113 | '/wd4996' 114 | ]) 115 | extra_link_args.append('/ignore:4197') 116 | elif sys.platform.startswith('darwin'): 117 | sb_include_dirs.append('deps/sphinxbase/include/android') 118 | extra_compile_args.extend([ 119 | '-Wno-macro-redefined', 120 | '-Wno-sign-compare', 121 | '-Wno-logical-op-parentheses' 122 | ]) 123 | elif sys.platform.startswith('linux'): 124 | sb_include_dirs.append('deps/sphinxbase/include/android') 125 | extra_compile_args.extend([ 126 | '-Wno-unused-label', 127 | '-Wno-strict-prototypes', 128 | '-Wno-parentheses', 129 | '-Wno-unused-but-set-variable', 130 | '-Wno-unused-variable', 131 | '-Wno-unused-result', 132 | '-Wno-sign-compare', 133 | '-Wno-misleading-indentation' 134 | ]) 135 | 136 | sb_swig_opts = ( 137 | ['-modern', '-threads'] + 138 | ['-I' + h for h in sb_include_dirs] + 139 | ['-Ideps/sphinxbase/swig'] + 140 | ['-outdir', 'sphinxbase'] 141 | ) 142 | 143 | ps_swig_opts = ( 144 | ['-modern', '-threads'] + 145 | ['-I' + h for h in sb_include_dirs + ps_include_dirs] + 146 | ['-Ideps/sphinxbase/swig'] + 147 | ['-outdir', 'pocketsphinx'] 148 | ) 149 | 150 | if not os.path.exists(os.path.join(os.path.dirname(__file__), 'pocketsphinx/model')): 151 | copytree(os.path.join(os.path.dirname(__file__), 'deps/pocketsphinx/model/en-us'), 152 | os.path.join(os.path.dirname(__file__), 'pocketsphinx/model'), 153 | ignore=ignore_patterns('en-us-phone.lm.bin')) 154 | if not os.path.exists(os.path.join(os.path.dirname(__file__), 'pocketsphinx/data')): 155 | os.makedirs(os.path.join(os.path.dirname(__file__), 'pocketsphinx/data')) 156 | copy(os.path.join(os.path.dirname(__file__), 'deps/pocketsphinx/test/data/goforward.raw'), 157 | os.path.join(os.path.dirname(__file__), 'pocketsphinx/data/goforward.raw')) 158 | 159 | 160 | class build(_build): 161 | def run(self): 162 | self.run_command('build_ext') 163 | return _build.run(self) 164 | 165 | 166 | class install(_install): 167 | def run(self): 168 | self.run_command('build_ext') 169 | return _install.run(self) 170 | 171 | 172 | cmdclass = { 173 | 'build': build, 174 | 'install': install, 175 | } 176 | 177 | 178 | try: 179 | from wheel.bdist_wheel import bdist_wheel as _bdist_wheel 180 | except ImportError: 181 | pass 182 | else: 183 | class bdist_wheel(_bdist_wheel): 184 | def run(self): 185 | self.run_command('build_ext') 186 | return _bdist_wheel.run(self) 187 | 188 | cmdclass['bdist_wheel'] = bdist_wheel 189 | 190 | 191 | if sys.platform.startswith('win'): 192 | class bdist_wininst(_bdist_wininst): 193 | def run(self): 194 | self.run_command('build_ext') 195 | return _bdist_wininst.run(self) 196 | 197 | cmdclass['bdist_wininst'] = bdist_wininst 198 | 199 | 200 | ext_modules = [ 201 | Extension( 202 | name='sphinxbase._sphinxbase', 203 | sources=sb_sources, 204 | swig_opts=sb_swig_opts, 205 | include_dirs=sb_include_dirs, 206 | define_macros=define_macros, 207 | extra_compile_args=extra_compile_args, 208 | extra_link_args=extra_link_args 209 | ), 210 | Extension( 211 | name='pocketsphinx._pocketsphinx', 212 | sources=ps_sources, 213 | swig_opts=ps_swig_opts, 214 | include_dirs=sb_include_dirs + ps_include_dirs, 215 | define_macros=define_macros, 216 | extra_compile_args=extra_compile_args, 217 | extra_link_args=extra_link_args 218 | ) 219 | ] 220 | 221 | if sys.platform.startswith('win'): 222 | ext_modules.append( 223 | Extension( 224 | name='sphinxbase._ad_win32', 225 | sources=['swig/sphinxbase/ad_win32.i', 'deps/sphinxbase/src/libsphinxad/ad_win32.c'], 226 | swig_opts=sb_swig_opts, 227 | include_dirs=sb_include_dirs, 228 | libraries=['winmm'], 229 | define_macros=define_macros, 230 | extra_compile_args=extra_compile_args, 231 | extra_link_args=extra_link_args 232 | ) 233 | ) 234 | elif sys.platform.startswith('darwin'): 235 | sb_include_dirs.append('/System/Library/Frameworks/OpenAL.framework/Versions/A/Headers') 236 | ext_modules.append( 237 | Extension( 238 | name='sphinxbase._ad_openal', 239 | sources=['swig/sphinxbase/ad_openal.i', 'deps/sphinxbase/src/libsphinxad/ad_openal.c'], 240 | swig_opts=sb_swig_opts, 241 | include_dirs=sb_include_dirs, 242 | extra_objects=['/System/Library/Frameworks/OpenAL.framework/Versions/A/OpenAL'], 243 | define_macros=define_macros, 244 | extra_compile_args=extra_compile_args, 245 | extra_link_args=extra_link_args 246 | ) 247 | ) 248 | elif sys.platform.startswith('linux'): 249 | ext_modules.extend([ 250 | Extension( 251 | name='sphinxbase._ad_pulse', 252 | sources=['swig/sphinxbase/ad_pulse.i', 'deps/sphinxbase/src/libsphinxad/ad_pulse.c'], 253 | swig_opts=sb_swig_opts, 254 | include_dirs=sb_include_dirs, 255 | libraries=['pulse', 'pulse-simple'], 256 | define_macros=define_macros, 257 | extra_compile_args=extra_compile_args, 258 | extra_link_args=extra_link_args 259 | ), 260 | Extension( 261 | name='sphinxbase._ad_alsa', 262 | sources=['swig/sphinxbase/ad_alsa.i', 'deps/sphinxbase/src/libsphinxad/ad_alsa.c'], 263 | swig_opts=sb_swig_opts, 264 | include_dirs=sb_include_dirs, 265 | libraries=['asound'], 266 | define_macros=define_macros, 267 | extra_compile_args=extra_compile_args, 268 | extra_link_args=extra_link_args 269 | ) 270 | ]) 271 | 272 | setup( 273 | name='pocketsphinx', 274 | version='0.1.15', 275 | description='Python interface to CMU Sphinxbase and Pocketsphinx libraries', 276 | long_description=open('README.md').read(), 277 | long_description_content_type='text/markdown', 278 | author='Dmitry Prazdnichnov', 279 | author_email='dmitry@prazdnichnov.name', 280 | maintainer='Dmitry Prazdnichnov', 281 | maintainer_email='dmitry@prazdnichnov.name', 282 | url='https://github.com/bambocher/pocketsphinx-python', 283 | download_url='https://pypi.org/project/pocketsphinx/#files', 284 | packages=['sphinxbase', 'pocketsphinx'], 285 | ext_modules=ext_modules, 286 | cmdclass=cmdclass, 287 | classifiers=[ 288 | 'Development Status :: 2 - Pre-Alpha', 289 | 'Operating System :: Microsoft :: Windows', 290 | 'Operating System :: POSIX :: Linux', 291 | 'Operating System :: MacOS', 292 | 'License :: OSI Approved :: BSD License', 293 | 'Programming Language :: Python :: 2', 294 | 'Programming Language :: Python :: 2.7', 295 | 'Programming Language :: Python :: 3', 296 | 'Programming Language :: Python :: 3.5', 297 | 'Programming Language :: Python :: 3.6', 298 | 'Programming Language :: C', 299 | 'Intended Audience :: Developers', 300 | 'Topic :: Software Development :: Libraries :: Python Modules', 301 | 'Topic :: Multimedia :: Sound/Audio :: Speech' 302 | ], 303 | license='BSD', 304 | keywords='sphinxbase pocketsphinx', 305 | test_suite='tests', 306 | include_package_data=True, 307 | zip_safe=False 308 | ) 309 | --------------------------------------------------------------------------------