termux / termux-packages

A package build system for Termux.
https://termux.dev
Other
12.62k stars 2.91k forks source link

[Bug]: Python has no access to audio devices #20570

Open VladKolerts opened 2 weeks ago

VladKolerts commented 2 weeks ago

Problem description

Sox's rec and play working good, but from python modules can`t access to devices. I'm attempt to use sounddevice and pyaudio. Devices list is empty.

import sounddevice as sd

sd.get_portaudio_version()
# (1246976, 'PortAudio V19.7.0-devel, revision 147dd722548358763a8b649b3e4b41dfffbcfbb6')
sd.query_devices()
# <empty responce>
import pyaudio
p = pyaudio.PyAudio()
p.get_device_count()
# 0
import speech_recognition as sr
sr.Microphone.list_microphone_names()
# []

What steps will reproduce the bug?

pkg update pkg upgrade pkg i termux-api pkg i termux-api pkg i portaudio (19.07.00-1) pkg i pulseaudio (17.0-1) pkg i sox (14.4.2-25) pkg i python (3.11.9-2) pip -V pip 24.0 from /data/data/com.termux/files/usr/lib/python3.11/site-packages/pip (python 3.11 pip install --upgrade setuptools wheel setuptools-70.0.0-py3-none-any.whl wheel-0.43.0-py3-none-any.whl

pip install sounddevice pyaudio sounddevice-0.4.7-py3-none-any.whl PyAudio-0.2.14.tar.gz pip list

Package     Version
----------- -------
cffi        1.16.0
pip         24.0
PyAudio     0.2.14
pycparser   2.22
setuptools  70.0.0
sounddevice 0.4.7
wheel       0.43.0

<restarting termux> pulsesound --start pactl load-module module-sles-source 17 pactl list sources short

1       OpenSL_ES_sink.monitor  module-sles-sink.c      s16le 2ch 44100Hz       IDLE
2       OpenSL_ES_source        module-sles-source.c    s16le 2ch 44100Hz       IDLE

rec 'test.wav'

Input File     : 'default' (pulseaudio)
Channels       : 2
Sample Rate    : 48000
Precision      : 16-bit
Sample Encoding: 16-bit Signed Integer PCM

In:0.00% 00:00:03.41 [00:00:00.00] Out:160k  [======|====  ] Hd:0.0 Clip:7 

play 'test.wav'

test.wav:

 File Size: 999k      Bit Rate: 1.54M
  Encoding: Signed PCM    
  Channels: 2 @ 16-bit   
Samplerate: 48000Hz      
Replaygain: off         
  Duration: 00:00:05.21  

In:18.0% 00:00:00.94 [00:00:04.27] Out:45.1k [  -===|==-   ]        Clip:0 

<voice recorded and played>

python

>>> import sounddevice as sd
>>> sd.query_devices()

>>> sd.query_devices(-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/sounddevice.py", line 569, in query_devices
    raise PortAudioError(f'Error querying device {device}')
sounddevice.PortAudioError: Error querying device -1
>>> sd.query_devices(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/sounddevice.py", line 569, in query_devices
    raise PortAudioError(f'Error querying device {device}')
sounddevice.PortAudioError: Error querying device 2
>>> sd.query_devices(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/sounddevice.py", line 569, in query_devices
    raise PortAudioError(f'Error querying device {device}')
sounddevice.PortAudioError: Error querying device 1

What is the expected behavior?

No response

System information

tried at two device (android 9 and 13), termux 0.118:

Termux Variables:
TERMUX_API_VERSION=0.50.0
TERMUX_APK_RELEASE=GITHUB
TERMUX_APP_PACKAGE_MANAGER=apt
TERMUX_APP_PID=3519
TERMUX_IS_DEBUGGABLE_BUILD=1
TERMUX_MAIN_PACKAGE_FORMAT=debian
TERMUX_VERSION=0.118.0
TERMUX__USER_ID=0
Packages CPU architecture:
aarch64
Subscribed repositories:
# sources.list
deb http://mirror.mephi.ru/termux/termux-main stable main
Updatable packages:
All packages up to date
termux-tools version:
1.42.4
Android version:
9
Kernel build information:
Linux localhost 4.4.156-OniiChanKernel-R3+ #2 SMP PREEMPT Thu Sep 5 02:57:49 UTC 2019 armv8l Android
Device manufacturer:
Xiaomi
Device model:
Redmi Note 7
LD Variables:
LD_LIBRARY_PATH=
LD_PRELOAD=/data/data/com.termux/files/usr/lib/libtermux-exec.so
Installed termux plugins:
com.termux.api versionCode:50
Termux Variables:
TERMUX_API_VERSION=0.50.0
TERMUX_APK_RELEASE=GITHUB
TERMUX_APP_PACKAGE_MANAGER=apt
TERMUX_APP_PID=13496
TERMUX_IS_DEBUGGABLE_BUILD=1
TERMUX_MAIN_PACKAGE_FORMAT=debian
TERMUX_VERSION=0.118.0
TERMUX__USER_ID=0
Packages CPU architecture:
aarch64
Subscribed repositories:
# sources.list
deb http://mirror.mephi.ru/termux/termux-main stable main
# x11-repo (sources.list.d/x11.list)
deb http://mirror.mephi.ru/termux/termux-x11 x11 main
# root-repo (sources.list.d/root.list)
deb http://mirror.mephi.ru/termux/termux-root root stable
Updatable packages:
All packages up to date
termux-tools version:
1.42.4
Android version:
13
Kernel build information:
Linux localhost 4.14.328-perf-@b28c10f6-dirty #1 SMP PREEMPT Mon Feb 26 14:50:07 CET 2024 aarch64 Android
Device manufacturer:
Xiaomi
Device model:
M2007J20CG
LD Variables:
LD_LIBRARY_PATH=
LD_PRELOAD=/data/data/com.termux/files/usr/lib/libtermux-exec.so
Installed termux plugins:
com.termux.api versionCode:50
sylirre commented 2 weeks ago

Install https://f-droid.org/packages/com.termux.api/

Grant microphone permission to Termux:API app in device settings.

Try to load module sles source.

VladKolerts commented 2 weeks ago

Install https://f-droid.org/packages/com.termux.api/

Grant microphone permission to Termux:API app in device settings.

Try to load module sles source.

Already done. You can see this steps at "What steps will reproduce the bug?" Without this, sox's rec and play will also not work.

sylirre commented 2 weeks ago

Isn't portaudio using ALSA?

ALSA does not work on Termux.

VladKolerts commented 2 weeks ago

Isn't portaudio using ALSA?

ALSA does not work on Termux.

no, ALSA don't installed. I describe the entire installation process in the block "What steps will reproduce the bug?"

Sound works in termux command line. My problem is sound devices are not visible from Python

sylirre commented 2 weeks ago

no, ALSA don't installed

That's not what I meant.

Sound works in termux command line.

Because command line tools made to work with Pulseaudio only. They won't attempt to query devices using ALSA.

My problem is sound devices are not visible from Python

Check what audio backend used by your libraries, especially portaudio which is a dependency for sounddevice and normally uses ALSA.

The issue you have is not a bug but a misconfiguration on your side.

Basically everything that does not use Pulseaudio or native OpenSLES won't be able to work with sound.