Need Help: Sound recording issue with pyaudio
Problem description
Created a pyaudio script t.py to record sound and export it as wav file. Problem:In termux the file shows that it started recording but strucks at it and does not stop and requires stopping script with ctrl+z while ctrl+c did not workit does not write any recording to wav. Means no recording. The script is made to record 5 seconds of audio
Script content:
import pyaudio
import wave
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "file.wav"
audio = pyaudio.PyAudio()
# start Recording
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
print ("recording...")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print ("finished recording")
# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()
waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
Steps to reproduce
Expected behavior
Recording of 5 seconds audio in wav file Additional information
Tried solution: Installed ubuntu on termux with vnc server x11 working and on it tried to run the script, and following error is obtained: Error log from ubuntu installed on termux is given below
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4693:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4693:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4693:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5181:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM sysdefault
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4693:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4693:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4693:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5181:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM sysdefault
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front
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.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround40
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround41
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround50
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround51
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround71
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
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: Access denied
ALSA lib pulse.c:242:(pulse_connect) PulseAudio: Unable to connect: Access denied
ALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
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 pulse.c:242:(pulse_connect) PulseAudio: Unable to connect: Access denied
ALSA lib pulse.c:242:(pulse_connect) PulseAudio: Unable to connect: Access denied
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4693:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4693:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4693:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5181:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM dmix
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
Traceback (most recent call last):
File "t.py", line 14, in <module>
stream = audio.open(format=FORMAT, channels=CHANNELS,
File "/usr/lib/python3/dist-packages/pyaudio.py", line 750, in open
stream = Stream(self, *args, **kwargs)
File "/usr/lib/python3/dist-packages/pyaudio.py", line 441, in __init__
self._stream = pa.open(**arguments)
OSError: [Errno -9996] Invalid input device (no default output device)
Then tried another solution dbus-launch --exit-with-session pulseaudio --start --daemon as non root also tried dbus-launch --exit-with-session pulseaudio --start --daemon --system --disallow-exit as root user python t.py This time script ran and stopped after 5 seconds and file.wav is also created but it was mute no audio was actually recorded.
And output is
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4693:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4693:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4693:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5181:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM sysdefault
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4693:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4693:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4693:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5181:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM sysdefault
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front
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.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround40
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround41
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround50
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround51
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround71
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
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_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
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 confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4693:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4693:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4693:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5181:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM dmix
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
recording...
finished recording
ls results shown file.wav but it was having no sound.
However on termux script keeps working infinitely and does not stop after 5 seconds and we need to force stop it with ctrl+z, while with ubuntu on termux script rans records and stops and creates output but is muted or no actual sound recorded Termux is been provided with microphone access permission using termux-microphone-record api.
Other solutions tried: sudo jackd -d alsa to run jack server but in ended with following error:
Copyright 2001-2005 Paul Davis and others.
Copyright 2004-2016 Grame.
Copyright 2016-2017 Filipe Coelho.
jackdmp comes with ABSOLUTELY NO WARRANTY
This is free software, and you are welcome to redistribute it
under certain conditions; see the file COPYING for details
JACK semaphore error: semget creation (Function not implemented)
jack_shm_lock_registry fails...
No access to shm registry
Failed to open server```
Termux
Try to launch pulseaudio daemon manually and load module-sles-source.
pulseaudio &
pactl load-module module-sles-source
Ubuntu
No, Pulseaudio won't work here. Termux uses audio drivers compatible with Android OS, specifically ones which use OpenSLES and AAudio. Ubuntu doesn't have them. But even if has, they won't be compatible with Android's Hardware Abstraction Layer.
ALSA won't work either. Most Android devices do not have support for it on kernel level, others which have it will require root in order to access devices exposed through /dev.
Error after trying following commands
dbus-launch --exit-with-session pulseaudio --start --daemon pulseaudio & pactl load-module module-sles-source
Traceback (most recent call last):
File "t.py", line 14, in <module>
stream = audio.open(format=FORMAT, channels=CHANNELS,
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/pyaudio.py", line 750, in open
stream = Stream(self, *args, **kwargs)
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/pyaudio.py", line 441, in __init__
self._stream = pa.open(**arguments)
OSError: [Errno -9998] Invalid number of channels
When tried only pulseaudio & pactl load-module module-sles-source Following error occured
[pulseaudio] source.c: Assertion 'pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_LATENCY, &usec, 0, NULL) == 0' failed at /home/builder/.termux-build/pulseaudio/src/src/pulsecore/source.c:1155, function pa_usec_t pa_source_get_latency(pa_source *)(). Aborting.
Traceback (most recent call last):
File "t.py", line 14, in <module>
stream = audio.open(format=FORMAT, channels=CHANNELS,
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/pyaudio.py", line 750, in open
stream = Stream(self, *args, **kwargs)
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/pyaudio.py", line 441, in __init__
self._stream = pa.open(**arguments)
OSError: [Errno -9996] Invalid input device (no default output device)
[1]+ Aborted pulseaudio
Another thing i observed is that when the script is run in termux it gets into background and that may be reason it strucks at recording... and does not actually record and we need to ctrl+z or pkill to exit. But in ubuntu termux script does not show such behaviour. Please try executing script in your device and comment if it runs properly or not.
I think pyaudio can use portaudio api. I have portaudio working over pulseaudio in my apt repo.
I think pyaudio can use portaudio api. I have portaudio working over pulseaudio in my apt repo.
Script seems to be initialised without any errors and works till print ("recording...") line but after that instead of recording it gets into background and runs infinitely and not recording.
However this problem is only in termux and not in proot
This issue/PR has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Any updates Because i too get this error https://github.com/termux/termux-packages/issues/17402#issue-1796107620
I believe OpenSL ES source module for Pulseaudio is broken on modern Android and needs to be fixed or enhanced.
It is advised to use termux-microphone-record from termux-api package to record audio for the time being.
still not working(
I don't know termux team is busy or what but the issue is still same after 4years with no progress @termux please fix this asap
Please don't ping the entire org for a "is it done yet" question.
Instead of asking to "fix it asap", contribute.
Hmm make sense I can try to fix
I can assure you that openSl Es source modules work fine for PulseAudio
They recorded audio and Pluse Audio play them too
The main issue is PyAudio is not recogniseing PulseAudio Stream for some reason
I think I have go through pyaudio code to figure how it works
Looks like pyaudio uses PortAudio So let's see if PortAudio Recognise Plus Audio
Our portaudio seems to disable all audio backends (neither supported by Termux):
TERMUX_PKG_DEPENDS="libc++"
TERMUX_PKG_EXTRA_CONFIGURE_ARGS="
--enable-cxx
--without-alsa
--without-jack
--without-oss
--without-asihpi
ac_cv_lib_pthread_pthread_create=yes
ac_cv_lib_rt_clock_gettime=yes
"
however its configure script suggests that Pulseaudio isn't supported anyway:
Optional Packages:
--with-PACKAGE[=ARG] use PACKAGE [ARG=yes]
--without-PACKAGE do not use PACKAGE (same as --with-PACKAGE=no)
--with-alsa Enable support for ALSA [autodetect]
--with-jack Enable support for JACK [autodetect]
--with-oss Enable support for OSS [autodetect]
--with-asihpi Enable support for ASIHPI [autodetect]
--with-winapi Select Windows API support
([wmme|directx|asio|wasapi|wdmks][,...]) [wmme]
--with-asiodir ASIO directory [/usr/local/asiosdk2]
--with-dxdir DirectX directory [/usr/local/dx7sdk]
--with-pic[=PKGS] try to use only PIC/non-PIC objects [default=use
both]
--with-gnu-ld assume the C compiler uses GNU ld [default=no]
--with-sysroot=DIR Search for dependent libraries within DIR
(or the compiler's sysroot if not specified).
README also doesn't mention pulse:
### Host API Implementations:
src/hostapi/alsa = Advanced Linux Sound Architecture (ALSA)
src/hostapi/asihpi = AudioScience HPI
src/hostapi/asio = ASIO for Windows and Macintosh
src/hostapi/coreaudio = Macintosh Core Audio for OS X
src/hostapi/dsound = Windows Direct Sound
src/hostapi/jack = JACK Audio Connection Kit
src/hostapi/oss = Unix Open Sound System (OSS)
src/hostapi/wasapi = Windows Vista WASAPI
src/hostapi/wdmks = Windows WDM Kernel Streaming
src/hostapi/wmme = Windows MultiMedia Extensions (MME)
This one may work: https://github.com/its-pointless/termux-packages/commit/70c4016230d87155591d7bb1b951959e06dd44e1
It is portaudio build recipe for old Termux community repo.
Is it works anymore Someone told me it is not maintained anymore.
If it works then many issue such as =>
- Speech Recognition
- PyAudio
- etc And scope to new packages would be opened
Installing its-pointless PortAudio for ARM
-
Update Termux Packages:
pkg update pkg upgrade -
Install wget:
pkg install wget -
Download PortAudio Package:
wget https://its-pointless.github.io/files/24/dists/termux/extras/binary-arm/portaudio_20180920-1_arm.deb -
Install the Package:
dpkg -i portaudio_20180920-1_arm.deb -
Fix Dependencies:
apt --fix-broken install -
Verify Installation:
dpkg -l | grep portaudio
Troubleshooting
If you get this error:
ImportError: dlopen failed: library "libportaudio.so.2" not found: needed by /data/data/com.termux/files/usr/lib/python3.11/site-packages/pyaudio/_portaudio.cpython-311.so in namespace (default)
Follow these steps:
-
Locate
libportaudio.so:find /data/data/com.termux/files/usr/lib -name "libportaudio.so*" -
Create Symbolic Link:
ln -s /data/data/com.termux/files/usr/lib/libportaudio.so /data/data/com.termux/files/usr/lib/libportaudio.so.2
For other cpu types you can follow same instructions just download the .deb file accordingly
Ahh but now there is a new issue PortAudio is recogniseing PulseAudio Stream but not recording it.
So here is a brief debugging report of what is working and what is not
PulseAudio ✅ OpenSL ES source module ✅ PortAudio ❌ PyAudio✅
How this works
OpenSl Module ------- PulseAudio ----- PortAudio ----- PyAudio
What to figure out ??
Why PortAudio is recogniseing PulseAudio Stream but not Recording it