How to use Audio Injector at 12 kHz sample rate?

Moderator: flatmax

Post Reply
Peter
Posts: 9
Joined: Sat May 27, 2017 7:23 am

How to use Audio Injector at 12 kHz sample rate?

Post by Peter » Tue Jun 06, 2017 7:16 am

I've finally managed to get audio across a LAN, using two Audio Injectors HATs attached to two RAspberry Pi3s. This may seem trivial to many, but was an accomplishment for me (especially when debugging is cumbersome). The audio application works fine, but at 48 kHz sampling rate. For the initial tests that is fine, but my goal is 12 kHz sample rate (and sample depth compression - ulaw -).

I purchased the Audio Injector as it is specified to handle sample rates as low as 8 and 12 kHz. Changing the sample rate in Pyaudio seemed the way to accomplish this. 8 kHz worked like a charm, but is just to low for my application: I require 12 kHz (or 16 kHz). However, both the sample rate settings of 16 and 12 kHz failed to work and produce the following errors:

Traceback (most recent call last):
File "/home/pi/SDR_Remote/lan_voicechat-master/server.py", line 33, in <module>
frames_per_buffer=CHUNK)
File "/usr/lib/python2.7/dist-packages/pyaudio.py", line 747, in open
stream = Stream(self, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/pyaudio.py", line 442, in __init__
self._stream = pa.open(**arguments)
IOError: [Errno Invalid sample rate] -9997

I changed the default settings in the asoundrc, but that did not have any effect at all. It all seems to be related to ALSA, but I am really a newbie when it comes to all this LINUX editing work. :? . Any solutions/comments are greatly appreciated!

BR,

Peter

flatmax
Posts: 609
Joined: Sat Jul 23, 2016 11:39 pm

Re: How to use Audio Injector at 12 kHz sample rate?

Post by flatmax » Thu Jun 08, 2017 12:51 pm

Hi there, 12kHz is a little non standard, but you can still make it work !

Can you please change the alsa device to plug:dmix , for example a test would be :
aplay -Dplug:dmix audio.at.12kHz.wav

dmix is the ALSA plugin which does sample rate conversion (as well as other conversions). If you only want sample rate conversion, then you should use a rate plugin, probably by specifying it in your asoundrc.

thanks
Matt
Check out our audiophile quality crossovers : https://bit.ly/2kb1nzZ
Please review the Zero sound card on Amazon USA : https://www.amazon.com/dp/B075V1VNDD
---
Check out our new forum on github : https://github.com/Audio-Injector

Peter
Posts: 9
Joined: Sat May 27, 2017 7:23 am

Re: How to use Audio Injector at 12 kHz sample rate?

Post by Peter » Sat Jun 10, 2017 8:54 am

Hi Matt,

Thanks for the suggestion, but meanwhile I got the AudioInjector running a 12 kHz (and below) since I switched using pyalsaaudio. The reason to change the Python Module was that PyAudio gave numerous under and overrun errors. Since using the pyalsaaudio module I made quite some progress. But Mr. Murphy is always around: Now Audio glitches appear, but well, using PyAudio there was no audio at all!
BR

Peter

flatmax
Posts: 609
Joined: Sat Jul 23, 2016 11:39 pm

Re: How to use Audio Injector at 12 kHz sample rate?

Post by flatmax » Sat Jun 10, 2017 9:20 pm

Hi,

I think it is likely that when you request 12 kHz operation the python library you are using opens dmix by default ? Either that or it thinks it is opening a 12 kHz stream, but it is either 8 kHz or 16 kHz. If you look at the machine driver for the AI card here you will see the rate constraints.

Does the sound seem sped up or slowed down from what you expect ?

Matt
Check out our audiophile quality crossovers : https://bit.ly/2kb1nzZ
Please review the Zero sound card on Amazon USA : https://www.amazon.com/dp/B075V1VNDD
---
Check out our new forum on github : https://github.com/Audio-Injector

Peter
Posts: 9
Joined: Sat May 27, 2017 7:23 am

Re: How to use Audio Injector at 12 kHz sample rate?

Post by Peter » Tue Jun 13, 2017 6:28 am

Hi Matt,

Thanks for your reply. Indeed, the AudioInjector does not seem capable of running at 12 kHz sampling rate. Not sure were I got this figure from. What the relation is between my setting in pyalsaaudio and the AudioInjector has become more obscure today. I installed a small programm which monitors the data speed over my ethernet socket, but the figures I get are awkard to say the least. My goal is to transmit streaming audio between 2 Raspberry Pi 3's using uLaw at a limited audio bandwidth of 3 kHz, with as little latency as possible (preferably < 100 ms).

The present config settings of pyalsaaudio are:
CHANNELS = 1
INFORMAT = alsaaudio.PCM_FORMAT_S16_LE *)
RATE = 16000
FRAMESIZE = 256

*) Pyalsaaudio can be set to ulaw, but I noticed that the data rate seems twice as high as using the ulaw compression from audioop, hence all tests have been performed as such.

Changing the sample rate from 8000, 16000, 32000 and 48000 Hz gave very strange transfer rates:
SAMPLING RATE Socket data speed
8000: 9 kByte/s (which would seem logical, as uLaw compresses to 8 bit. Hence 8000 * 1 Byte would be 8 kB/s)
16000: 9 kByte/s. Exactly the same rate as using 8000 Hz sampling rate!
32000: 70 kByte/s ???
48000: 107 kByte/s ??

What the actual sampling rate of the audio injector is, I can't tell, but these figures don't make sense to me, as all the other settings have stayed the same, only the sampling rates were changed.

For comparison I have ran the same sample speeds but now without the ulaw compression:
8000: 17 kByte/s
16000: 17 kByte/s
32000: 135 kByte/s
48000: 201 KByte/s

The ulaw compression seems to work as anticipated, a factor 2 (as sampling depth is 16 bits). But I cannot make head or tails from the resulting socket data speed.

It has become clear to me that the 8 and 16 kHz settings are the same, but not what the real sampling rate is (8000 Hz??). So I am back to square one: How to set the Audio Injector to a different sampling rate. (And addtionally: How to get seamless audio transfer at low sampling rates).

Regarding your question about the sound: It seems normal, no change in spectrum as far as I can tell.

BTW: Were do I change the alsa to dplug = Dmix?

BR,

Peter

flatmax
Posts: 609
Joined: Sat Jul 23, 2016 11:39 pm

Re: How to use Audio Injector at 12 kHz sample rate?

Post by flatmax » Fri Jun 16, 2017 8:37 am

To change alsa to use the dmix plug, you need to use the plug:dmix device.
You can change the sample rate dmix uses like so (from here):

Code: Select all

aplay -D"plug:'dmix:RATE=44100'" test.wav
This sound card supports the following rates natively (in Hz) :

Code: Select all

8000, 16000, 32000, 44100, 48000, 96000, 88200
thanks
Matt
Check out our audiophile quality crossovers : https://bit.ly/2kb1nzZ
Please review the Zero sound card on Amazon USA : https://www.amazon.com/dp/B075V1VNDD
---
Check out our new forum on github : https://github.com/Audio-Injector

Peter
Posts: 9
Joined: Sat May 27, 2017 7:23 am

Re: How to use Audio Injector at 12 kHz sample rate?

Post by Peter » Sun Jun 18, 2017 7:13 pm

Hi Matt,

I tried setting the rates according to the link you send regarding dmixer. As it may also solve the stuttering, as buffers can be set, it looks like a promising solution. See code /etc/asound.conf:

Code: Select all

pcm.AudioInjector {
type hw card 0
}
pcm.!default {
    type plug
    slave.pcm "dmixer"
}

pcm.dmixer {
    type dmix
    ipc_key 1024
    slave {
           pcm "Audio Injector"
           period_time 0
           period_size 1024
           buffer_size 4096
           rate 16000
    }
    bindings {
            0 0
            1 1
    }
}
ctl.dmixer {
    type hw
    card 0
}
However, I must be doing something wrong, as I keep getting the following error, preventing me to continue:

Code: Select all

Traceback (most recent call last):
  File "local_audio_p3_V6.py", line 303, in <module>
    main()
  File "local_audio_p3_V6.py", line 299, in main
    App()
  File "local_audio_p3_V6.py", line 97, in __init__
    player = alsaaudio.PCM(type=alsaaudio.PCM_PLAYBACK)
alsaaudio.ALSAAudioError: No such file or directory [default]
aplay -l gives:

Code: Select all

**** List of PLAYBACK Hardware Devices ****
card 0: audioinjectorpi [audioinjector-pi-soundcard], device 0: AudioInjector audio wm8731-hifi-0 []
  Subdevices: 1/1
  Subdevice #0: subdevice #0
Any clue what I am doing wrong in the asound.conf? (I have copied the complete code according to your link, but that gave a similar error).

BR,

Peter

flatmax
Posts: 609
Joined: Sat Jul 23, 2016 11:39 pm

Re: How to use Audio Injector at 12 kHz sample rate?

Post by flatmax » Wed Jun 21, 2017 9:59 am

OK - looks good.
Have you checked that you can play something using aplay ? Just to see that the bug isn't in the .asoundrc file.

Matt
Check out our audiophile quality crossovers : https://bit.ly/2kb1nzZ
Please review the Zero sound card on Amazon USA : https://www.amazon.com/dp/B075V1VNDD
---
Check out our new forum on github : https://github.com/Audio-Injector

Peter
Posts: 9
Joined: Sat May 27, 2017 7:23 am

Re: How to use Audio Injector at 12 kHz sample rate?

Post by Peter » Thu Jun 22, 2017 5:13 am

Hi Matt,

aplay plays a test wav nicely, so far so good. This is what the /.asoundrc looks like:

Code: Select all

pcm.!default {
type hw
slave.pcm {
        type hw
        ipc_key 2867
        slave {
                pcm "hw:Loopback,0,0"
                rate 16000
                format S16_LE
                channels 1
                period_size 4096
                buffer_size 16384
                }
        }
}

pcm.hubcap {
        type plug
        slave {
                pcm "hw:Loopback,1,0"
                rate 16000
                format S16_LE
        }
}


pcm.!default {
        type hw
        card 0
}

ctl.!default {
        type hw
        card 0
}
I did not realise that in asoundrc a buffer was specified, although this is not dmixer. If asoundrc prevails over asound.conf (?) then why would there still be the audio stuttering at 16 kbps speeds. Should I align these 2 files??

Peter

flatmax
Posts: 609
Joined: Sat Jul 23, 2016 11:39 pm

Re: How to use Audio Injector at 12 kHz sample rate?

Post by flatmax » Thu Jun 22, 2017 9:08 am

Regarding your asoundrc, I am not familiar with using loopback, I am aware people use it with jackd ?

If nothing in the asoundrc is special to you, then you can copy it aside ... it seems to be setting your default to a loopback device.

Matt
Check out our audiophile quality crossovers : https://bit.ly/2kb1nzZ
Please review the Zero sound card on Amazon USA : https://www.amazon.com/dp/B075V1VNDD
---
Check out our new forum on github : https://github.com/Audio-Injector

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests