Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

audio_device selection in Livespeech #23

Open
saibharani opened this issue May 23, 2017 · 53 comments
Open

audio_device selection in Livespeech #23

saibharani opened this issue May 23, 2017 · 53 comments

Comments

@saibharani
Copy link

saibharani commented May 23, 2017

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.

@slowrunner
Copy link

@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?

@bkravi-os-iot
Copy link

@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

@slowrunner
Copy link

slowrunner commented Aug 26, 2019

@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

@Ozer0
Copy link

Ozer0 commented Nov 19, 2019

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
Copy link

Ozer0 commented Dec 10, 2019 via email

@JDSNX
Copy link

JDSNX commented Dec 10, 2019

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
Copy link

JDSNX commented Dec 10, 2019

By the way, I am running this in Raspberry Pi

@Ozer0
Copy link

Ozer0 commented Dec 10, 2019

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
Copy link

Ozer0 commented Dec 10, 2019

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 (:

@JDSNX
Copy link

JDSNX commented Dec 10, 2019

How can I choose the ALSA?

I cannot find this when i configure the sphinxbase

I am really confused now :<

@Ozer0
Copy link

Ozer0 commented Dec 10, 2019

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

@JDSNX
Copy link

JDSNX commented Dec 11, 2019

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.

@Ozer0
Copy link

Ozer0 commented Dec 11, 2019

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.

@JDSNX
Copy link

JDSNX commented Dec 11, 2019

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?

@Ozer0
Copy link

Ozer0 commented Dec 11, 2019

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

@JDSNX
Copy link

JDSNX commented Dec 12, 2019

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
Copy link

JDSNX commented Dec 12, 2019

I fixed it using sudo ldconfig.

@Ozer0
Copy link

Ozer0 commented Dec 12, 2019

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

@JDSNX
Copy link

JDSNX commented Dec 12, 2019

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

@Ozer0
Copy link

Ozer0 commented Dec 12, 2019

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)

@JDSNX
Copy link

JDSNX commented Dec 12, 2019

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.

@Ozer0
Copy link

Ozer0 commented Dec 12, 2019

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

@JDSNX
Copy link

JDSNX commented Dec 13, 2019

Why I cannot import LiveSpeech?

@Ozer0
Copy link

Ozer0 commented Dec 13, 2019

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
Copy link

Ozer0 commented Dec 13, 2019

First try to uninstall pocketsphinx python with
pip uninstall pocketsphinx

@JDSNX
Copy link

JDSNX commented Dec 13, 2019

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

image

@JDSNX
Copy link

JDSNX commented Dec 13, 2019

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.

@Ozer0
Copy link

Ozer0 commented Dec 14, 2019

Hey man, did you solve it?

@JDSNX
Copy link

JDSNX commented Dec 14, 2019

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
Copy link

JDSNX commented Dec 14, 2019

What parameters you put in the LiveSpeech?

@Ozer0
Copy link

Ozer0 commented Dec 14, 2019

See my first reply on this post

@JDSNX
Copy link

JDSNX commented Dec 14, 2019

I got it now sir! Thank you so much! 👍 really appreciate your help!

@Ozer0
Copy link

Ozer0 commented Dec 14, 2019

Very happy to hear you finally can use it :D
You're welcome, I'm here to help people with the things i learned in so many forums like this.

And not a Sir, I'm just 22 haha
Cheers from Mexico City 🇲🇽

@JDSNX
Copy link

JDSNX commented Dec 16, 2019

Hi! Im back again! :D
I just wanna ask if it is possible in LiveSpeech to print the words when I stop speaking? Because when I have a certain sentence it will break. For example I will say "HI HOW ARE YOU" It will output like this
HI HOW
ARE YOU

Is there a way to catch it?

@Ozer0
Copy link

Ozer0 commented Dec 16, 2019

Do you want to do something with the words you say?
Like a command?

@JDSNX
Copy link

JDSNX commented Dec 17, 2019

Yes. It cannot catch all of my sentence because when I try to use it, it will automatically read what I say. Is there way that the livespeech could run when im done talking?

@JDSNX
Copy link

JDSNX commented Dec 17, 2019

I have a custom language model and dictionary already.

@Ozer0
Copy link

Ozer0 commented Dec 17, 2019

Ok, you have to use your .lm and .dict files in the LiveSpeech's parameters function. Then you need to compare the string with the words you say
Here is a simple example

#Creating a command function
def command(word):
    if 'THE WORDS YOU WANT' in word:
        #do what you want, example
        print("Hello")
    else:
        print("Repeat it please")

for phrase in speech:
    word = str(phrase)
    #calling the commands function
    command(word)


@Ozer0
Copy link

Ozer0 commented Dec 17, 2019

20191216_231542

@Ozer0
Copy link

Ozer0 commented Dec 17, 2019

20191216_231559

@Ozer0
Copy link

Ozer0 commented Dec 17, 2019

20191216_232550

@JDSNX
Copy link

JDSNX commented Dec 17, 2019

Ya, we have the same code. But I dont know why it will automatically print the phrase even if im not done talking yet.

@Ozer0
Copy link

Ozer0 commented Dec 17, 2019

The dictionary you created is conformed of only words?, or you wrote the wole phrases (two or more words)?
For example, I use a LIGHT ON command. So that's how i wrote the txt file to create the language model.

LIGHT ON
LIGHT OFF
.
.
.

etc

@JDSNX
Copy link

JDSNX commented Dec 18, 2019

What I wrote on my txt file is by word like OFF, ON, OPEN, CLOSE, etc. I cannot get the whole sentence because it will break automatically for example i will say "Please turn on the lights". The sentence will be separated to PLEASE TURN and ON THE LIGHTS. That is my problem as of now. I cannot get the whole phrase

@Ozer0
Copy link

Ozer0 commented Dec 19, 2019

Maybe you should try using the whole phrases in your txt file.
Once you get that, you can use the whole phrase in your if sentence.
I tell you this because the dictionary is different from the language model, the last one is used to determinate what words would be in front of some other word.
You can read the CMUSphinx documentation to understand it.

@ismailryt
Copy link

Hi @Ozer0 ,I hope you want to help me
I already follow every step and all the step in conversation between you and @JDSNX

But i still have a error like this

Error opening audio device plughw:1,0 for capture: Device or resource busy
Traceback (most recent call last):
File "try.py", line 15, in
dic=os.path.join(model_path, '0175.dic')
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_alsa.py", line 122, in init
this = _ad_alsa.new_Ad(audio_device, sampling_rate)
RuntimeError: new_Ad returned -1

As you can see audio device plughw:1,0 is error, i already check using arecord -l, i hope you have any solution, anyway im using respeaker 4-mic array

@ismailryt
Copy link

pi@raspberrypi:~ $ arecord -l
**** List of CAPTURE Hardware Devices ****
card 1: seeed4micvoicec [seeed-4mic-voicecard], device 0: bcm2835-i2s-ac10x-codec0 ac10x-codec.1-003b-0 [bcm2835-i2s-ac10x-codec0 ac10x-codec.1-003b-0]
Subdevices: 0/1
Subdevice #0: subdevice #0

@Ozer0
Copy link

Ozer0 commented Apr 24, 2020

pi@raspberrypi:~ $ arecord -l
**** List of CAPTURE Hardware Devices ****
card 1: seeed4micvoicec [seeed-4mic-voicecard], device 0: bcm2835-i2s-ac10x-codec0 ac10x-codec.1-003b-0 [bcm2835-i2s-ac10x-codec0 ac10x-codec.1-003b-0]
Subdevices: 0/1
Subdevice #0: subdevice #0
Hi there.
Seems like you have the ALSA problem, have you changed the init file?

@ismailryt
Copy link

@Ozer0 Thanks for you reply, actually i already fix that but now i have this error

RuntimeError: new_Decoder returned -1

I know this is happend because path problem, but i dont know how to set right path, i use your code

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-id'),
lm=os.path.join(model_path, '0175.lm'),
dic=os.path.join(model_path, '0175.dic')
)

Can you teach me this part of line " model_path = get_model_path() "
Thanks

@Ozer0
Copy link

Ozer0 commented May 2, 2020

@Ozer0 Thanks for you reply, actually i already fix that but now i have this error

RuntimeError: new_Decoder returned -1

I know this is happend because path problem, but i dont know how to set right path, i use your code

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-id'),
lm=os.path.join(model_path, '0175.lm'),
dic=os.path.join(model_path, '0175.dic')
)

Can you teach me this part of line " model_path = get_model_path() "
Thanks

Hi, you solved the problem?

@ismailryt
Copy link

@Ozer0 actually no error right now, but now my problem is LiveSpeech cant recognize anything.. When i use http://blog.justsophie.com/python-speech-to-text-with-pocketsphinx/ decode method, it can recognize 1 word then input over flowed because my raspberry pi cpu too slow

So my last hope is using LiveSpeech, im wondering why no error but cant recognize anything, when I use pocketsphinx_continuous, it works can record my word and can give me output. Can you help me

@Ozer0
Copy link

Ozer0 commented May 12, 2020

@Ozer0 actually no error right now, but now my problem is LiveSpeech cant recognize anything.. When i use http://blog.justsophie.com/python-speech-to-text-with-pocketsphinx/ decode method, it can recognize 1 word then input over flowed because my raspberry pi cpu too slow

So my last hope is using LiveSpeech, im wondering why no error but cant recognize anything, when I use pocketsphinx_continuous, it works can record my word and can give me output. Can you help me

Well, gotta say I used LiveSpeech in a Raspberry Zero so i don't think the CPU is your problem.
What it could be is you have to use an infinite loop, are you using that on your python code?
You can see part of my code above these answers

@ismailryt
Copy link

@Ozer0 i find the another solution! I use decode method not using LiveSpeech and can do hotword after that give intruction to system. Thanks for this thread and all of you guys, really helped me

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants