pocketsphinx-python icon indicating copy to clipboard operation
pocketsphinx-python copied to clipboard

audio_device selection in Livespeech

Open saibharani opened this issue 7 years ago • 53 comments

I want to do keyword recognition from a specific mic. Can you tell me how to do audio_device selection. I want to select audio_device with index 2. Thank you.

saibharani avatar May 23 '17 08:05 saibharani

@saibharani You can try speech = LiveSpeech(audio_device='2')

but if you are asking about an ALSA audio_device=2 you may get (like I am getting...):

Error opening audio device 2 for capture: Connection refused Traceback (most recent call last): File "keyword.py", line 16, in <module> speech = LiveSpeech(audio_device='2',verbose=True,sampling_rate=44100,lm=False,keyphrase='forward',kws_threshold='le-20') File "/usr/local/lib/python3.5/dist-packages/pocketsphinx/__init__.py", line 206, in __init__ self.ad = Ad(self.audio_device, self.sampling_rate) File "/usr/local/lib/python3.5/dist-packages/sphinxbase/ad_pulse.py", line 124, in __init__ this = _ad_pulse.new_Ad(audio_device, sampling_rate) RuntimeError: new_Ad returned -1

LiveSpeech() seems to be trying pulseaudio, can it use ALSA device?

Can we use a USB mic that only supports 44.1K and 48K sample_rate?

slowrunner avatar Jun 19 '19 21:06 slowrunner

@slowrunner . may be this late as I joined this platform late.. May be you are not in this need anymore. But can be helpful for others.. By default LiveSpeech will call ad_pulse.py when sys.platform.startswith('linux').. as in your case it is calling ad_pulse. But if you want to change this call to as_alsa.py, simple change below lines in /usr/local/lib/python2.7/dist-packages/sphinxbase/init.py:

import sys

if sys.platform.startswith('win'): from .ad_win32 import * elif sys.platform.startswith('darwin'): from .ad_openal import * elif sys.platform.startswith('linux'): try: ### RAVI commented below and added line to import alsa. Otherwise, it will call pulse not alsa ### #from .ad_pulse import * from .ad_alsa import * except: from .ad_alsa import *

from .sphinxbase import *

Please check above in bold and italic.

Hope this helps

bkravi-os-iot avatar Aug 26 '19 03:08 bkravi-os-iot

@bkravi-os-iot Thank you. Made that change in each sphinxbase/__init__.py, but still not having enough success.

If I use LiveSpeech(audio_device='hw:1,0',sampling_rate=16000...), it finds the capture device but claims:

*** get_model_path(): /usr/local/lib/python3.5/dist-packages/pocketsphinx/model
Available samping rate 11025 is too far from requested 16000

(This may be the HDMI device though, not my USB mic)

which is weird because in a non-livespeech script I use:

p = pyaudio.PyAudio()
samprate = 16000
stream = p.open(format=pyaudio.paInt16, channels=1, rate=int(samprate), input=True, frames_per_buffer=1024)
stream.start_stream() 

and pocketsphinx works just fine.

I tried sampling_rate=11025 but pocketsphinx only has a 16k model and cores:

*** get_model_path(): /usr/local/lib/python3.5/dist-packages/pocketsphinx/model
Segmentation fault

btw: arecord -l

**** List of CAPTURE Hardware Devices ****
card 1: Device [USB Audio Device], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

slowrunner avatar Aug 26 '19 14:08 slowrunner

I got a sollution (works for me) (: I have the same result as you when i use arecord -l, that means the hardware is plughw:1,0 Once you installed and builded pocketsphinx and sphinxbase, You can test it in a terminal with the following comand: pocketsphinx_continuous -inmic yes -adcdev plughw:1,0 If it works, you can go to the next step with the LiveSpeech function with the parameter audio_device='plughw:1,0'

Example

import os
from pocketsphinx import LiveSpeech, get_model_path

model_path = get_model_path()

speech = LiveSpeech(
    audio_device='plughw:1,0',   #<---- Here is
    verbose=False,
    sampling_rate=16000,
    buffer_size=2048,
    no_search=False,
    full_utt=False,
    hmm=os.path.join(model_path, 'en-us'),
    lm=os.path.join(model_path, 'en-us.lm.bin'),
    dic=os.path.join(model_path, 'cmudict-en-us.dict')
)

for phrase in speech:
    print(phrase)

NOTE: I needed to change the init.py as @bkravi-os-iot said. (Thank you)

Ozer0 avatar Nov 19 '19 16:11 Ozer0

Hi (: Yes, what i did was chage the file init.py that is on the sphinxbase directory.

Just get into that directory and type: sudo nano init.py Change the two lines and it's done. I'll put two screenshots to make it easier for you.

El 09/12/2019 6:54 p. m., Judison Bacalso [email protected] escribió:

Hi @Ozer0https://github.com/Ozer0 I was just wondering how did you change the init.py that @bkravi-os-iothttps://github.com/bkravi-os-iot said. because i cannot change it because it cannot be writable. Thanks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/bambocher/pocketsphinx-python/issues/23?email_source=notifications&email_token=AMFRP7XP7G5MUZ5MTIUIRSDQX3SGDA5CNFSM4DMNPF72YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGLHUIA#issuecomment-563509792, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AMFRP7URAOEIIBHT6PUBN63QX3SGDANCNFSM4DMNPF7Q.

Ozer0 avatar Dec 10 '19 01:12 Ozer0

Hi @Ozer0 I have successfully change the init.py. But now I have another problem, take a look at this:

Error opening audio device plughw:1,0 for capture: Invalid argument Traceback (most recent call last): File "/home/pi/Desktop/try.py", line 17, in dic=os.path.join(model_path, 'cmudict-en-us.dict') File "/usr/local/lib/python3.7/dist-packages/pocketsphinx/init.py", line 206, in init self.ad = Ad(self.audio_device, self.sampling_rate) File "/usr/local/lib/python3.7/dist-packages/sphinxbase/ad_pulse.py", line 122, in init this = _ad_pulse.new_Ad(audio_device, sampling_rate) RuntimeError: new_Ad returned -1

I just follow what you said above about the audio_device.

JDSNX avatar Dec 10 '19 02:12 JDSNX

By the way, I am running this in Raspberry Pi

JDSNX avatar Dec 10 '19 02:12 JDSNX

Ok, my init.py looks like this.

import sys

if sys.platform.startswith('win'):
    from .ad_win32 import *
elif sys.platform.startswith('darwin'):
    from .ad_openal import *
elif sys.platform.startswith('linux'):
    try:
        from .ad_alsa import *
    except:
        from .ad_alsa import *

from .sphinxbase import *

I had the same problem. Did you tried with this comand on the console? pocketsphinx_continuous -inmic yes -adcdev plughw:1,0

If there's the problem like "ERROR continuos.c" maybe when you builded sphinxbase the Raspberry choosed pulseaudio instead of ALSA. So, check this link. http://robot.laylamah.com/?p=35

You have to verify Sphinxbase uses ALSA when you type the comand ./configure. This is because Sphinxbase always look first for pulseaudio, but in the documentation they said in embebed systems you should use ALSA.

Even if you didn't installed pulseaudio this could happen (my case), so what I did was look for that pulseaudio.h file that makes that error. It was on a directory called Pulse in the /home/pi So I had to move it to another directory (Desktop) with the comand sudo mv pulse/ /home/pi/Desktop Then I re-configured sphinxbase and realized sphinxbase choosed ALSA.

I think you could look for the file with whis comand on the console whereis pulseaudio

Ozer0 avatar Dec 10 '19 03:12 Ozer0

Working with pocketsphinx python on Raspberry could be difficult because of many errors while installing all dependencies, that's why I'm working on a YouTube tutorial for begginers like me. I'll share the video's link when finished (:

Ozer0 avatar Dec 10 '19 03:12 Ozer0

How can I choose the ALSA?

I cannot find this when i configure the sphinxbase

I am really confused now :<

JDSNX avatar Dec 10 '19 11:12 JDSNX

First of all do the steps to rename or move the "pulseaudio.h" file or if it's the case, uninstall pulseaudio. Then you have to re-configure sphinxbase with ./configure When finish, look for the line that says checking for alsa/aaoundlib.h .. yes

Ozer0 avatar Dec 10 '19 17:12 Ozer0

I already re-configured my sphinxbase and I already saw checking for alsa/aaoundlib.h .. yes. And I also uninstall my pulseaudio.

My problem now is when i check the alsamixer it has an error: image

After I uninstall my pulseaudio this error came up. Been searching for a while for this error but cannot fixed.

JDSNX avatar Dec 11 '19 05:12 JDSNX

Did you only uninstall pulseaudio? What i did was look for the file i told you and moved it to another directory. I don't have installed pulseaudio on my raspberry and i don't have that problem while using alsautils.

Ozer0 avatar Dec 11 '19 06:12 Ozer0

I uninstall pocketsphinx and sphinxbase. Re-configured them all but when I check init.py i doesnt have any code.

Now, when i run this: pocketsphinx_continuous -inmic yes -adcdev plughw:1,0 this is the output: image

image

Can you determine what did I do wrong?

JDSNX avatar Dec 11 '19 09:12 JDSNX

Are you sure you're trying to edit the sphinxbase's init.py? Because pocketsphinx also has an init.py file, don't confuse them. You don't need to uninstall pocketsphinx or sphinxbase, you only have to re-configure them and install again. Try to do that

And about the problem, seems you need to specific the language model. You can try with a comand like this PicsArt_12-11-11 53 59

Try tp add the 3 parameter -hmm is the en-us file -lm = language model (.lm file) -dict = dictionary (.dic file) So go to your model folder and change the name to yours

Ozer0 avatar Dec 11 '19 17:12 Ozer0

Hi sir! I am able to run the continuous now. But the thing is i cannot run this line: pocketsphinx_continuous -inmic yes -adcdev plughw:1,0 I can only run by using this ./src/programs/pocketsphinx_continuous -adcdev plughw:1,0 -nfft 2048 -samprate 48000.

When im using the first one the error is this image

But when I use the second one i need to go to the pocketsphinx folder and run it there. I just followed the steps you linked to me before.

JDSNX avatar Dec 12 '19 03:12 JDSNX

I fixed it using sudo ldconfig.

JDSNX avatar Dec 12 '19 03:12 JDSNX

Happy to hear that. So now you can use a python script with pocketsphinx?

Ozer0 avatar Dec 12 '19 03:12 Ozer0

Yes, I am using the one you gave. Is there a way to catch the string? Or I will need the LiveSpeech?

JDSNX avatar Dec 12 '19 03:12 JDSNX

Yeah, you have to use LiveSpeech and convert the "phrase" in code. See this video, it was helpful for me. https://www.youtube.com/watch?v=f_Pq7GDdxos

Also you can create your own dictionary and language model to reduce the error, it's very helpful. See this page (Step 8) https://medium.com/@ranjanprj/for-some-time-now-i-have-been-thinking-really-hard-to-build-a-diy-study-aid-for-children-which-uses-17ce90e72f43 Once you get the two files, you have to copy to the model folder and next use them on the python script (LiveSpeech parameters)

Ozer0 avatar Dec 12 '19 04:12 Ozer0

I followed the steps of the video you showed me and it has an error about the LiveSpeech image

But the using the pocketpshinx_continuous does not have an error. I even create my own dictionary and language.

JDSNX avatar Dec 12 '19 10:12 JDSNX

Why are you executing your script with ./ ? I don't know if that works but you have errors with python's keywords like "import", you should try with python speech.py

Ozer0 avatar Dec 12 '19 17:12 Ozer0

Why I cannot import LiveSpeech?

JDSNX avatar Dec 13 '19 03:12 JDSNX

That's was my problem too haha Did you successfully installed pocketsphinx?

Here are the steps I followed, you should try with pip3 instead of pip. 20191212_215157

Ozer0 avatar Dec 13 '19 04:12 Ozer0

First try to uninstall pocketsphinx python with pip uninstall pocketsphinx

Ozer0 avatar Dec 13 '19 04:12 Ozer0

Hi! Do you have any idea how to deal with this?

image

JDSNX avatar Dec 13 '19 08:12 JDSNX

I am back with this error.

image

I already change init.py from pulse to alsa. Both 3.7 and 2.7. I just followed the video you linked.

JDSNX avatar Dec 13 '19 08:12 JDSNX

Hey man, did you solve it?

Ozer0 avatar Dec 14 '19 03:12 Ozer0

Nah. I keep getting the error like this. Same error for python 2 and 3.

I changed the init.py but the error still there.

JDSNX avatar Dec 14 '19 03:12 JDSNX

What parameters you put in the LiveSpeech?

JDSNX avatar Dec 14 '19 03:12 JDSNX