The application works as intended on windows 10 but crashes in linux. I am trying to record audio through my microphone in a flask application using pyaudio(Python 3). I am trying it in Ubuntu 20.04.
The error is as follows:
ALSA lib pcm_dsnoop.c:641:(snd_pcm_dsnoop_open) unable to open slave
ALSA lib pcm_dmix.c:1089:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pulse.c:242:(pulse_connect) PulseAudio: Unable to connect: Connection refused
ALSA lib pulse.c:242:(pulse_connect) PulseAudio: Unable to connect: Connection refused
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
ALSA lib pcm_dsnoop.c:641:(snd_pcm_dsnoop_open) unable to open slave
ALSA lib pcm_dmix.c:1089:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm_dmix.c:1089:(snd_pcm_dmix_open) unable to open slave
This is the code I am using to record audio, the logic is not wrong and all the variables are correctly assigned, this is just a snippet of the code so some variables might seem vague.
p = pyaudio.PyAudio()
stream = p.open(format=sample_format,
channels=channels,
rate=fs,
frames_per_buffer=chunk,
input=True)
frames = [] # Initialize array to store frames
for i in range(0, int(fs / chunk * seconds)):
if( fee =="T"):
data = stream.read(chunk)
frames.append(data)
else:
break
# Stop and close the stream
stream.stop_stream()
stream.close()
# Terminate the PortAudio interface
p.terminate()
print('Finished recording')
# Save the recorded data as a WAV file
wf = wave.open(filename, 'wb')
wf.setnchannels(channels)
wf.setsampwidth(p.get_sample_size(sample_format))
wf.setframerate(fs)
wf.writeframes(b''.join(frames))
wf.close()
The default microphone device is probably not the one you think it is. In order to find the correct microphone device (if there is one) you can use this:
def get_device_index(p):
device_index = None
for i in range(p.get_device_count()):
devinfo = p.get_device_info_by_index(i)
for keyword in ["mic","input"]:
if keyword in devinfo["name"].lower():
print( "Found an input: device %d - %s"%(i, devinfo["name"]) )
device_index = i
return device_index
if device_index is None:
print( "No preferred input found; using default input device." )
return device_index
Then in order to use this device index:
device_index = get_device_index(p)
stream = p.open(format = sample_format,
channels = channels,
rate = fs,
input = True,
input_device_index = device_index,
frames_per_buffer = chunk)
Taken from here.
Related
I have written this tts code and downloaded all neccersarry packages on my system. It uses openai to be able and have converstations with me and works perfectly on my windows computer. However, when I run the code on my raspberry pie it comes up with errors, I have solved some of the errors but there are still alot I still dont know how to fix it.
Here Is My Code
import openai
import pyttsx3
import speech_recognition as sr
from api_key import API_KEY
from FaceRecTest import speak
openai.api_key = API_KEY
engine = pyttsx3.init()
r = sr.Recognizer()
mic = sr.Microphone(device_index=1)
voice = engine.getProperty('voices')
engine.setProperty('voice', voice[1].id)
engine.setProperty('rate', 180)
conversation = ""
user_name = "Zach"
bot_name = "Timothy"
while True:
with mic as source:
print("\nlistening...")
r.adjust_for_ambient_noise(source, duration=0.2)
audio = r.listen(source)
print("no longer listening.\n")
try:
user_input = r.recognize_google(audio)
except:
continue
if user_input.lower() == "exit" or user_input.lower() == "goodbye":
speak("Goodbye, Have a Good day!")
break
prompt = user_name + ": " + user_input + "\n" + bot_name+ ": "
conversation += prompt
response = openai.Completion.create(engine='text-davinci-003', prompt=conversation, max_tokens=100)
response_str = response["choices"][0]["text"].replace("\n", "")
response_str = response_str.split(user_name + ": ", 1)[0].split(bot_name + ": ", 1)[0]
conversation += response_str + "\n"
print(response_str)
speak(response_str)
Here Is the Errors I am Getting
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_a52.c:823:(_snd_pcm_a52_open) a52 is only for playback
ALSA lib conf.c:5057:(parse_args) Unknown parameter AES0
ALSA lib conf.c:5217:(snd_config_expand) Parse arguments error: No such file or directory
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM iec958:{AES0 0x6 AES1 0x82 AES2 0x0 AES3 0x2 CARD 0}
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_a52.c:823:(_snd_pcm_a52_open) a52 is only for playback
ALSA lib conf.c:5057:(parse_args) Unknown parameter AES0
ALSA lib conf.c:5217:(snd_config_expand) Parse arguments error: No such file or directory
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM iec958:{AES0 0x6 AES1 0x82 AES2 0x0 AES3 0x2 CARD 0}
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
listening...
no longer listening.
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_a52.c:823:(_snd_pcm_a52_open) a52 is only for playback
ALSA lib conf.c:5057:(parse_args) Unknown parameter AES0
ALSA lib conf.c:5217:(snd_config_expand) Parse arguments error: No such file or directory
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM iec958:{AES0 0x6 AES1 0x82 AES2 0x0 AES3 0x2 CARD 0}
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
I used OpenAi to see if it can give a breakdown of what is wrong and this was the response:
"These messages are error messages from the Advanced Linux Sound Architecture (ALSA) library. They indicate that there were issues with trying to open certain sound devices or with certain sound card configurations. The specific issues mentioned in these messages, such as "Unknown field port" and "Invalid type for card," suggest that there may be problems with the configuration of the sound card or with the way the application is trying to access it. To resolve these issues, you may need to check the configuration of your sound card and ensure that the application is set up to use the correct device and settings. Additionally, you may need to update the ALSA driver or install additional software to support the sound card."
I updated all my ALSA drivers, and I am not sure what could be wrong with my sound card.If anybody can help me I will be so grateful, this is really stressing me out. Thank you.
I am trying to capture from a streaming IP cam with username and password and port like so:vs = cv2.VideoCapture('http://user:password#ipadress:port/video.cgi?.mjpg')
but when I run the script I get this error in the cmd output:
[ERROR:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap.cpp (116) cv::VideoCapture::open VIDEOIO(CV_IMAGES): raised OpenCV exception:
OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\videoio\src\cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): http://user:password#ipadress:port/video.cgi?.mjpg in function 'cv::icvExtractPattern'
things I tried:
-installing FFmpeg and directing it to the environment path
-trying different ports
-switching between HTTP and RTSP
obviously, I'm new. thank you.
I'm trying to interface a Raspberry Pi 3B to a Bluetooth ELM327 OBD scanner but I'm having some troubles. I've connected the raspberry to the OBD Scanner by running the following commands from the terminal:
bluetoothctl
power on
default-agent
pair <scanner MAC address>
trust <scanner MAC address>
sudo rfcomm bind rfcomm0 <scanner MAC address>
I've then proceeded to run the following python script to check the connection status
import obd
obd.logger.setLevel(obd.logging.DEBUG)
connection = obd.OBD("/dev/rfcomm0")
print("Connection status: ")
print(connection.status())
connection.close()
Then I've returned this,
[obd.obd] ======================= python-OBD (v0.7.1) =======================
[obd.obd] Explicit port defined
[obd.elm327] Initializing ELM327: PORT=/dev/rfcomm0 BAUD=auto PROTOCOL=auto
[obd.elm327] [Errno 16] could not open port /dev/rfcomm0: [Errno 16] Device or resource busy: '/dev/rfcomm0'
[obd.obd] Closing connection
[obd.obd] Cannot load commands: No connection to car
[obd.obd] ===================================================================
Connection status:
Not Connected
I don't know how to solve this this, and I hope you can help me out.
I think this was a problem because you chose the wrong port, you can scan the ports with this code. The code is captured from this repo.
import obd
ports = obd.scan_serial()
print(ports)
I'm trying to connect to my bluetooth devices using python, but I fail on very first step.
Most tutorial I found have code similar to this to connect.
I use MAC address from hciconfig - I guess this is mac of my only adapter.
import bluetooth
for port in range(1, 11):
try:
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.connect(('00:1A:7D:DA:71:11', port))
print("Connected")
s.close()
except OSError as err:
print(f"Error connecting to {port}", err)
If run this above it will fail with „Device or resource busy”
If I use s.connect('') it fails with „Device or resource busy”.
I can use this adapter through bluetoothctl and other managers.
I have multiple devices working on it. So it is detected correctly by OS.
What is wrong here. How can I connect to my adapter using Python?
I writing app in python that uses pocketsphinx and pjsip
I use pocketsphinx keyword search to wakeup the app and pjsip as phone
the problem is when I try call the "call" function and after that returning to listen to the keyword and then I get an error message from pjsip that he cannot open the sound device:
pjsua_aud.c ..Opening sound device (speaker + mic) PCM#16000/1/20ms
alsa_dev.c ...ALSA lib pcm_hw.c:1557:(snd_pcm_hw_open) open '/dev/snd/pcmC0D0c' failed (-16): Device or resource busy
pjsua_aud.c ..Unable to open sound device: Unknown error from audio driver (PJMEDIA_EAUD_SYSERR) [status=420002]
operation=make_call(), error=Unknown error from audio driver (PJMEDIA_EAUD_SYSERR)
this error only happens when i try to return to the keyword function that uses alsaaudio
so my question will be how I can open the same audio device with pjsip and alsaaudio?