USB Audio on Raspberry Pi


Mike AE4ML
 

I have tried this program USRPAudio.py on a Raspberry Pi.
I'm using a Sound Blaster USB  sound card. The audio works and sounds good  with Test files.
but when I run the USRPAudio.py it just riddled with errors. This is a fresh install of Stretch 11 18 .
Fresh install of DVSWITCH.


Mike AE4ML
 

same results using python 2.7 or 3.5 .

I see several failures on this line. src/hostapi/alsa/pa_linux_alsa.c


Steve N4IRS
 

Audio device setup?

 

Sent from Mail for Windows 10

 

From: Mike AE4ML
Sent: Thursday, March 7, 2019 10:29 AM
To: main@DVSwitch.groups.io
Subject: Re: [DVSwitch] USB Audio on Raspberry Pi

 

same results using python 2.7 or 3.5 .

I see several failures on this line. src/hostapi/alsa/pa_linux_alsa.c

 


Mike AE4ML
 

in the process of trouble shooting I have installed this and it didn't help

sudo apt-get install python-pyaudio python3-pyaudio sox
pip install pyaudio


root@raspberrypi:~# cat .asoundrcfile
pcm.!default {
  type asym
   playback.pcm {
     type plug
     slave.pcm "hw:1,0"
   }
   capture.pcm {
     type plug
     slave.pcm "hw:1,0"
   }
}
pcm.convert {
         type plug;
         slave {
               pcm default;
               rate 44000;
         }
}


Mike AE4ML
 
Edited

I pulled out a new SD card , I haven't done anything with as far as audio goes.
I have the dvswitch  programs installed.
tested aplay -l and arec -l and set the correct setting in /etc/asound.conf
tested playing a wav file through the usb sound card
Both of these worked

aplay test.wav
and
speaker-test -D sysdefault:CARD=1       

When I ran USRPAudio.py I go t one error and that was for pyaudio.
So , I loaded python-pyaudio  and python3-pyaudio
now the screen is riddled with the error I reported earlier.


Apostolos Kefalas <sv1ljj@...>
 

Hello Mike,


IOError: [Errno -9997] Invalid sample rate
it seems to me that the usb audio device, can not work at the 8Khz
sampling rate.

Maybe try another usb audio dongle.


Apostolos




On Fri, 2019-03-08 at 13:28 -0800, Mike AE4ML wrote:
[Edited Message Follows]
I pulled out a new SD card , I haven't done anything with as far as
audio goes. 
I have the dvswitch  programs installed. 
tested aplay -l and arec -l and set the correct setting in
/etc/asound.conf
tested playing a wav file through the usb sound card
Both of these worked

aplay test.wav
and 
speaker-test -D sysdefault:CARD=1        

When I ran USRPAudio.py I go t one error and that was for pyaudio. 
So , I loaded python-pyaudio  and python3-pyaudio
now the screen is riddled with the error I reported earlier. 
--
... if you get into RF you’ll spend your days staring at test equipment
wondering why nothing works.


Apostolos Kefalas <sv1ljj@...>
 

Hello Mike,

I think the "rate" should be 44100 and not 44000

If that doesn't work,

replace "hw:1,0" with "plughw:1,0" and try again.


73
Apostolos - SV1LJJ

On Thu, 2019-03-07 at 16:08 -0800, Mike AE4ML wrote:
in the process of trouble shooting I have installed this and it
didn't help

sudo apt-get install python-pyaudio python3-pyaudio sox
pip install pyaudio


root@raspberrypi:~# cat .asoundrcfile 
pcm.!default {
  type asym
   playback.pcm {
     type plug
     slave.pcm "hw:1,0"
   }
   capture.pcm {
     type plug
     slave.pcm "hw:1,0"
   }
}
pcm.convert {
         type plug;
         slave {
               pcm default;
               rate 44000;
         }
}
--
... if you get into RF you’ll spend your days staring at test equipment
wondering why nothing works.


Mike AE4ML
 

Apostolos,
Progress,
I replaced "hw:1,0" with "plughw:1,0"
This is the new output
pi@raspberrypi:~ $ ./USRPAudio.py
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'defaults.bluealsa.device'
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:4996:(snd_config_expand) Args evaluate error: No such file or directory
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM bluealsa
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'defaults.bluealsa.device'
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:4996:(snd_config_expand) Args evaluate error: No such file or directory
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM bluealsa
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
Start audio thread


Mike AE4ML
 

It works but it doesn't I read the other write up on USB to DMR. .
I have tried three different USB audio devices. all have the same results. If I stop and restart the python script it works again


Apostolos Kefalas <sv1ljj@...>
 

Hello Mike,

I do not see any problem!!! This kind of output is usual. As far as I
can see "The audio thread is started", so you should be OK.

Check out with alsamixer if something is muted.


73 Apostolos

On Sat, 2019-03-09 at 05:48 -0800, Mike AE4ML wrote:
Apostolos, 
Progress, 
I replaced "hw:1,0" with "plughw:1,0" 
This is the new output
pi@raspberrypi:~ $ ./USRPAudio.py 
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM
cards.pcm.rear
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM
cards.pcm.center_lfe
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM
cards.pcm.side
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM
cards.pcm.hdmi
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM
cards.pcm.hdmi
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM
cards.pcm.modem
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM
cards.pcm.modem
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM
cards.pcm.phoneline
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM
cards.pcm.phoneline
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition
'defaults.bluealsa.device'
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer
returned error: No such file or directory
ALSA lib conf.c:4996:(snd_config_expand) Args evaluate error: No such
file or directory
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM bluealsa
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition
'defaults.bluealsa.device'
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer
returned error: No such file or directory
ALSA lib conf.c:4996:(snd_config_expand) Args evaluate error: No such
file or directory
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM bluealsa
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
Start audio thread
--
... if you get into RF you’ll spend your days staring at test equipment
wondering why nothing works.


Heiko DL1BZ
 
Edited

Look in the python script, you see the sampling rate is 8000 (=8khz). I had the same problem with a cheap CM108 based usb soundcard, which only support 44,1 or 48khz. A "better" soundcard I used worked from 6khz-48 Khz. As I understand, for correct transcoding to dmr the sampling rate of soundcard need exact 8hkz (=8000), other values don't work. If the soundcard don't support this rate, you cannot use it with pyaudio in USRPAudio.py . For test you can (but only for output) use the buildin sound-output of a raspberry pi, whichs works too with the python-script.

For select an other sounddevice as the default, you need to extend the script:
def rxAudioStream():
...
stream = p.open(format=FORMAT,
                    channels = CHANNELS,
                    rate = RATE,
                    output = True,
                    frames_per_buffer = CHUNK,
                    output_device_index=0,
                    )

def txAudioStream():
...
stream = p.open(format=FORMAT,
                    channels = CHANNELS,
                    rate = RATE,
                    input = True,
                    frames_per_buffer = CHUNK,
                    input_device_index=2,
                    )    

For index info of sounddevices you can use:
$ python -m sounddevice
pi@PISHACK:~ $ python -m sounddevice
  0 bcm2835 ALSA: - (hw:0,0), ALSA (0 in, 2 out)
  1 Loopback: PCM (hw:1,0), ALSA (32 in, 32 out)
  2 Loopback: PCM (hw:1,1), ALSA (32 in, 1 out)
  3 sysdefault, ALSA (0 in, 128 out)
* 4 default, ALSA (32 in, 32 out)
pi@PISHACK:~ $

but you need to install the python-module sounddevice for do that.

73 Heiko, DL1BZ


Mike AE4ML
 

Heiko,
Sorry to say that idea didn't work. I was never able to get the script to work with the raspberry pi onboard audio at all. Only tracebacks and errors from the script.

Is there a list of known working USB audio  devices ?


sounddevice only installed as a python3 failed on install for python

pi@raspberrypi:~ $ python3 -m sounddevice
< 0 bcm2835 ALSA: - (hw:0,0), ALSA (0 in, 2 out)
  1 bcm2835 ALSA: IEC958/HDMI (hw:0,1), ALSA (0 in, 2 out)


pi@raspberrypi:~ $ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
  Subdevices: 7/7
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
card 0: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
  Subdevices: 1/1
  Subdevice #0: subdevice #0


 Traceback (most recent call last):
                                                                                                                                     File "USRPAudio.py", line 106, in txAudioStream
      frames_per_buffer = CHUNK,
                                 
File "/usr/lib/python2.7/dist-packages/pyaudio.py", line 750, in open
                                                                                                          
stream = Stream(self, *args, **kwargs)
                                                                                                                                                   File "/usr/lib/python2.7/dist-packages/pyaudio.py", line 441, in __init__
                                         
Expression 'paInvalidSampleRate' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2048
                                                                                                                                   Expression 'PaAlsaStreamComponent_InitialConfigure( &self->playback, outParams, self->primeBuffers, hwParamsPlayback, &realSr )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2722
                                                                                                                                          Expression 'PaAlsaStream_Configure( stream, inputParameters, outputParameters, sampleRate, framesPerBuffer, &inputLatency, &outputLatency, &hostBufferSizeMode )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2843
                                                                                                                                                                                     self._stream = pa.open(**arguments)
                                     
IOError: [Errno -9996] Invalid input device (no default output device)
                                                                                                           
Unhandled exception in thread started by <function rxAudioStream at 0x76765f70>
         Traceback (most recent call last):
                                            
File "USRPAudio.py", line 42, in rxAudioStream
                                                                                              
output_device_index=0,
                                                                                                                      
File "/usr/lib/python2.7/dist-packages/pyaudio.py", line 750, in open
             
stream = Stream(self, *args, **kwargs)
                                                     
File "/usr/lib/python2.7/dist-packages/pyaudio.py", line 441, in __init__
                                                                                                                                   self._stream = pa.open(**arguments)
                                                                                                                                                                      IOError: [Errno -9997] Invalid sample rate


Mike AE4ML
 

My use for this setup is for monitoring only,  I'm really not interested in transmitting .
I have a radio for that..

Mike


Heiko DL1BZ
 

give me a little time I will be re-check all. I‘m running rasbian-lite (debian stretch) with the last 4.14.98+ kernel.
All what I can say I had a running monitoring with the build-in sound output, and I had deinstall pulseaudio.
I will be back here later...

73 Heiko, DL1BZ


Heiko DL1BZ
 

I re-check all. Here all works with my onboard-sound-device:
root@mmdvmpi:~# aplay -l
**** Liste der Hardware-Geräte (PLAYBACK) ****
Karte 0: ALSA [bcm2835 ALSA], Gerät 0: bcm2835 ALSA [bcm2835 ALSA]
  Sub-Geräte: 7/7
  Sub-Gerät #0: subdevice #0
  Sub-Gerät #1: subdevice #1
  Sub-Gerät #2: subdevice #2
  Sub-Gerät #3: subdevice #3
  Sub-Gerät #4: subdevice #4
  Sub-Gerät #5: subdevice #5
  Sub-Gerät #6: subdevice #6
Karte 0: ALSA [bcm2835 ALSA], Gerät 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
  Sub-Geräte: 1/1
  Sub-Gerät #0: subdevice #0
root@mmdvmpi:~#
Expression 'parameters->channelCount <= maxChans' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 1514
                                                                                                          Start audio thread...
Expression 'ValidateParameters( inputParameters, hostApi, StreamDirection_In )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2818

                                                                                                                                        Unhandled exception in thread started by <function txAudioStream at 0x767804b0>
                           Traceback (most recent call last):
                                                               File "./USRPAudio.py", line 114, in txAudioStream
                                                                                                                    input_device_index=2,
                                                                                                                                           File "/usr/lib/python2.7/dist-packages/pyaudio.py", line 750, in open
                        stream = Stream(self, *args, **kwargs)
                                                                File "/usr/lib/python2.7/dist-packages/pyaudio.py", line 441, in __init__
                                                                                                                                             self._stream = pa.open(**arguments)
                                                                                                                                                                                IOError: [Errno -9998] Invalid number of channels
                                     START:  10.03.19 17:05:57 DL1BZ Slot:2 TG:7

END:    10.03.19 17:05:57 DL1BZ Slot:2 TG:7 BER:0.00% 3.29s

The error message because "Invalid number of channels" you can ignore.
I can hear me if  I sent something in DMR as a monitor.
Maybe you need to check your system - I verify this on 3 different Pi (2x Pi3B+ and 1x Pi3B) - so I need to say it works like I wrote.

73 Heiko, DL1BZ