Storing parameters in .asoundrc does not apply - audio

I have a microphone and I can record voice like so:
arecord test.wav -r 22050 -f S32_LE -V mono
So far so good. However, when a third party software that I'm using (which I think uses ffmpeg internally), wants to use the microphone it fails, since it does not use the above described parameters.
I tried now to move those parameters into my .asoundrc so that they will be picked up by alsa.
The Alsa Wiki tells me that the plug plugin can be used in order to specify the rate and the format:
A more complex tool for conversion is the pcm type plug. the syntax
is:
type plug # Format adjusted PCM
slave STR # Slave name (see pcm_slave)
# or
slave { # Slave definition
pcm STR # Slave PCM name
# or
pcm { } # Slave PCM definition
[format STR] # Slave format (default nearest) or "unchanged"
[channels INT] # Slave channels (default nearest) or "unchanged"
[rate INT] # Slave rate (default nearest) or "unchanged"
}
route_policy STR # route policy for automatic ttable generation
# STR can be 'default', 'average', 'copy', 'duplicate'
# average: result is average of input channels
# copy: only first channels are copied to destination
# duplicate: duplicate first set of channels
# default: copy policy, except for mono capture - sum
ttable { # Transfer table (bidimensional compound of
# cchannels * schannels numbers)
CCHANNEL {
SCHANNEL REAL # route value (0.0 ... 1.0)
}
}
So I setup a plugin like so:
pcm.!default {
type asym
playback.pcm {
type plug
slave.pcm "softvol"
}
capture.pcm {
type plug
slave {
pcm "plughw:0" # this works
rate 22050 # does not apply
format S32_LE # does not apply
}
}
}
The pcm "plughw:0" gets picked up however, the rate and the format fall back to 8 Bit and 8000Hz. Also I have to specify -V Mono so that I can record:
arecord test.wav -V mono
Recording WAVE 'test.wav' : Unsigned 8 bit, Rate 8000 Hz, Mono
############+ |
So unfortunately none of the required parameters can be added to my settings.
Update
I tried to record using ffmpeg in an attempt to mimic what :
ffmpeg -f alsa -ac 1 -i plughw:0 output.wav -ar 22050 -f S32_LE
The recording starts but no audio is recorded and the rate and format are mixed up again:
aplay output2.wav
Playing WAVE 'output2.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono
Update with asym plugin
I did try an asymmetric approach as suggested.
pcm.my_mic {
type hw
card 0
channels 1
format S32_LE
}
pcm.duplex {
type asym
playback.pcm "dmixer"
capture.pcm "my_mic"
}
pcm.!default {
type plug
slave.pcm "duplex"
}
However, when I enter arecord test.wav I'm asked for the format despite the fact that it's configured in the .asoundrc.
When I do enter a format I get the following error:
arecord -f S32_LE
Recording WAVE 'stdin' : Signed 32 bit Little Endian, Rate 8000 Hz, Mono
ALSA lib pcm_params.c:2162:(snd1_pcm_hw_refine_slave) Slave PCM not usable
arecord: set_params:1270: Broken configuration for this PCM: no configurations available.
Update
With some ALSA magic by my friend Y.W. I was able to get arecord to work without parameters, however, it's still somehow using inferior settings when recording audio by default.
The key here it seems was to map the mono signal of my I2s card to a stereo signal.
This is the .asoundrc content:
# This section makes a reference to your I2S hardware, adjust the card name
# to what is shown in arecord -l after card x: before the name in []
# You may have to adjust channel count also but stick with default first
pcm.dmic_hw {
type hw
card raspisound
channels 2
format S32_LE
rate 48000
}
# This is the software volume control, it links to the hardware above and after
# saving the .asoundrc file you can type alsamixer, press F6 to select
# your I2S mic then F4 to set the recording volume and arrow up and down to adjust the volume
# After adjusting the volume - go for 50 percent at first, you can do something like
# arecord -D dmic_sv -c2 -r 48000 -f S32_LE -t wav -V mono -v myfile.wav
pcm.dmic_sv {
type softvol
slave.pcm dmic_hw
control {
name "Boost Capture Volume"
card raspisound
}
min_dB -3.0
max_dB 30.0
}
# This plugin converts the mono input to stereo.
pcm.mono2stereo {
type route
slave.pcm dmic_sv
slave.channels 2
# ttable.input_channel.output_channel volume_gain
ttable.0.0 1
ttable.0.1 1
}
# The "plug" plugin converts channels, rate and format on request.
# In our case it converts the 32 format to whatever the application request.
pcm.convert {
type plug
slave {
pcm mono2stereo
}
}
pcm.convertplayback {
type plug
slave {
pcm "hw:0"
}
}
# Default capture and playback devices
pcm.!default {
type asym
capture.pcm convert
playback.pcm convertplayback
}

By specifying the slave PCM device as plughw:0, you have created a chain of two plug plugins. This means that the application can use whatever sample format it pleases, the upper plugin will convert it to 22 kHz with 32 bits, and the lower plugin will convert it to any format the hardware supports.
To force the hardware to use the specified format, use it directly as slave, with hw:0.
You cannot prevent programs like arecord from using a different format; that is the whole purpose of the plug plugin. To check what format is actually used by the hardware, add the -v parameter to the arecord call.

Related

Can I force arecord to record with a certain sampling rate if my sound card allows it?

I am using arecord to record audio using a USB microphone that allows recording up to 384k sampling rate. With Audacity I can easily record with other lower sampling rates (48k, 44.1k ..) but when I use:
arecord -vD hw:4,0 -f S16_LE -d 120 -r 48000 -c 1 sample.wav
It displays a warning message:
Warning: rate is not accurate (requested = 48000Hz, got = 384000Hz)
And records with 384000Hz.
I can't get to fix this.

.asoundrc ALSA configuration for webcam

I'm an ALSA beginner in the process of setting up my .asoundrc to record audio from my webcam.
Here is what I did to determine that my hardware is actually working:
I'm able to output sound using the following basic .asoundrc
defaults.pcm.!card Generic
defaults.pcm.!device 0
defaults.pcm.!ctl Generic
And I successfully recorded a test from my webcam with the following command
arecord --device=hw:0,0 --duration=5 --format=S16_LE /tmp/test.wav
Then, using a rudimentary knowledge of ALSA configuration, I wrote the following .asoundrc
pcm.!default {
type asym
playback.pcm {
type hw
card Generic
device 0
}
capture.pcm {
type hw
card 0
device 0
}
}
However, this results in no sound output or recording functionality. Please help me understand why this is not working.
output of aplay -l
output of arecord -l

amixer controls won't get anything on my development board

I am trying to set volume on the minilinux system with amixer controls in development board,but it won't get anything.
~ # amixer
~ #
~ # amixer controls
~ #
I have tried to create a plugin by create this file in "/etc/asound.conf"as this How_to_use_softvol_to_control_the_master_volumesaid ,by type in this
pcm.softvol {
type softvol
slave {
pcm "hw:0,0"
}
control {
name "softctl"
card 0
}
}
but when testing it ,give me this error:
/etc # speaker-test -D softvol -c 2 &
/etc #
speaker-test 1.1.5
Playback device is softvol
Stream parameters are 48000Hz, S16_LE, 2 channels
Using 16 octaves of pink noise
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM softvol
Playback open error: -2,No such file or directory
when I changed the asound.conf to something like this:
pcm.softvol {
type hw
card 0
device 0
}
then exac " speaker-test -D softvol -c 2 & " the headphone have that pink noise.
This is really weird that I can play and record excpet control the volume. New to this,no idea what to do next.

How to set default microphone on Raspberry Pi 3B with Raspbian Stretch?

So far I have not found any guides on how to set the default USB microphone for Stretch and so I've followed all of the guides for Wheezy and Jessie and they do not work.
Typing in the command arecord sound.wav does not record anything from the microphone. However, if I were to type in this command it would work arecord -f cd -D hw:1,0 -d 10 sound.wav.
Here is what appears when I enter in arecord -l
**** List of CAPTURE Hardware Devices ****
card 1: Headset [Plantronics Headset], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0
I've edited this file /usr/share/alsa/alsa.conf and set the values to this
defaults.ctl.card 1
defaults.pcm.card 1
I've also edited the /etc/asoundrc file and changed it to this:
pcm.!default {
type asym
playback.pcm {
type plug
slave.pcm "hw:0,0"
}
capture.pcm {
type plug
slave.pcm "hw:1,0"
}
}
ctl.!default {
type hw
card 0
}
and this:
pcm.!default {
type hw
card 1
}
ctl.!default {
type hw
card 1
}
and it still does not work.
I've been having headaches with this one for about a day, but I finally made it work.
Your .asoundrc should actually look like this:
pcm.!default {
type asym
playback.pcm "plughw:0"
capture.pcm "plughw:1"
}
ctl.!default {
type hw
card 1
}
Otherwise your settings are ok, but I think this command is wrong:
arecord sound.wav
The proper command for testing the mic (or at least the one that works for me) is this:
arecord -D plughw:1,0 --duration=3 test.wav && aplay test.wav
This is setup for a 3 seconds recording and after recording stops it automatically plays back to you (assuming your audio also works).
This part plughw:1,0 points to your mic, which I see is set to card 1.
I you have created this file /etc/modprobe.d/alsa-base.conf during your tests please delete it and reboot your pi.
sudo rm -f /etc/modprobe.d/alsa-base.conf
Also if you have tried this for the AlexaPi project, make sure the service is stopped before you try your mic and audio.
sudo systemctl stop AlexaPi.service

Recording each channel using ALSA API

I'm using a playstation eye, plugged into a raspberry pi. I have the raspberry pi recognising the built in microphone array of the PSEye and I can sample input levels using
arecord -vv /dev/null -r 16000 -f S16_LE -c 4 -D iec958:CARD=CameraB409241,DEV=0 /dev/null < /dev/null
Now obviously this shows the levels for all 4 channels being summed together. What I wish to do is record each channel separately. Is this possible using the ALSA API?
I've looked through this http://www.alsa-project.org/alsa-doc/alsa-lib/pcm.html
And took a look at this http://www.linuxjournal.com/article/6735?page=0,2
But neither seemed to what I needed. This is also similar to sampling both channels of a stereo mic over ALSA as well I suppose. Eventually I want to be able to sample the dB from each microphone at a specific point in time.
To read into a separate buffer for each channel, replace SND_PCM_ACCESS_RW_INTERLEAVED with SND_PCM_ACCESS_RW_NONINTERLEAVED, and replace snd_pcm_readi with snd_pcm_readn.
If you want to record each channel to independent audio files, then you can use this application :
https://github.com/flatmax/gtkiostream/blob/master/applications/ALSACaptureSplitter.C
It needs to be compiled, which is described here :
https://lists.audioinjector.net/pipermail/people/2020-March/000028.html
To use the application specify the base file name and the extension, for example :
ALSACaptureSplitter /tmp/test wav
It can record to many different audio file formats.
You can use the options to change the device, specify the recording time, channel count, etc. Here is the help printed out by the application :
./applications/ALSACaptureSplitter -h
ALSACaptureSplitter : An application to capture input and save to
independent files.
Usage:
ALSACaptureSplitter [options] outFileNamePrefix ext
e.g. ALSACaptureSplitter [options] /tmp/out wav
-D : The name of the device : (-D hw:0)
-c : The number of channels to open, if the available number is less, then it is reduced to the available : (-c 2)
-t : The duration to sample for : (-t 2.1)
-r : The sample rate to use in Hz : (-r 48000)
AUDIO FILE FORMATS:The known output file extensions (output file formats) are the following :
8svx aif aifc aiff aiffc al amb amr-nb amr-wb anb au avr awb caf cdda cdr cvs cvsd cvu dat dvms f32 f4 f64 f8 fap flac fssd gsm gsrt hcom htk ima ircam la lpc lpc10 lu mat mat4 mat5 maud mp2 mp3 nist ogg paf prc pvf raw s1 s16 s2 s24 s3 s32 s4 s8 sb sd2 sds sf sl sln smp snd sndfile sndr sndt sou sox sph sw txw u1 u16 u2 u24 u3 u32 u4 u8 ub ul uw vms voc vorbis vox w64 wav wavpcm wv wve xa xi

Resources