kadamski
Posts: 187
Joined: Fri Jun 08, 2012 10:56 pm

Re: ALSA on Raspbian

Mon Jul 16, 2012 4:13 pm

@lintweaker
Would you be so kind and test if there is the same plughw problem also on USB audio device? This could point me out if it's driver or userspace problem. To do this you would need to compile and run a simple C program i would provide.

lintweaker
Posts: 33
Joined: Mon Jul 09, 2012 6:26 pm

Re: ALSA on Raspbian

Mon Jul 16, 2012 5:40 pm

@kadamski

Just tried with plughw instead of hw, works just fine (using a usb audio UAC 2.0 device)

BTW After making my normal user a member of the audio group I finally can use aplay -L / -l etc :)

kadamski
Posts: 187
Joined: Fri Jun 08, 2012 10:56 pm

Re: ALSA on Raspbian

Mon Jul 16, 2012 5:56 pm

But I need to know if using plughw with PCM that can not be handled by hw itself (on USB card) is going to work. It should as this is what plughw is for but for some reason it is not for RPi buildin sound chip.

bryankemp
Posts: 2
Joined: Tue Jul 17, 2012 1:57 pm

Re: ALSA on Raspbian

Tue Jul 17, 2012 2:17 pm

I also have been working on a MPD implementation on my pi. I have a Fiio E17 Dac that I have configured successfully, and while high quality files 24/96 FLACs do sound good with an occasional crackle, playing lower quality mp3s or even aac produce a huge amount of static. I have seriously spent days trying to find a combination of Alsa configurations that will elimiate the static. One interesting thing I have noticed is the static diminishes if I have another process running on the Pi at the same time. So far the only solution I have it to force also to upsample the lower quality files into 24/96 for playback but the CPU utilization goes through the roof. I would appreciate any thoughts or comments you may have about this.

kadamski
Posts: 187
Joined: Fri Jun 08, 2012 10:56 pm

Re: ALSA on Raspbian

Tue Jul 17, 2012 3:21 pm

I was finally able to test plug ALSA plugin on other soundcard. I was using some creative USB audio card (using snd_usb_audio module) and plughw does work as it should with this card. This means it's a driver problem. Some more investigation is needed.

@bryankemp
You said that you don't have problems with static when you have another process running at the same time. If I understand correctly, this process have to use some significant amount of CPU in order to eliminate static, right? (there are always some processes running on Linux but they are sleeping most of the time). This suggest that it may be related to some power saving options.. Maybe it's something related to CPU/GPU scaling while not on load or things like NO_HZ in kernel. You could try adding "nohz=y" to your cmdline.txt and check if it changes anything.

lintweaker
Posts: 33
Joined: Mon Jul 09, 2012 6:26 pm

Re: ALSA on Raspbian

Tue Jul 17, 2012 4:38 pm

@bryankemp
I can confirm that stressing the CPU (by executing:
time echo "scale=5000; 4*a(1)" | bc -l -q which pegs the cpu for ~ 85%) the sound quality stays constant for normal mp3s. Higher frequency (> 48Hz) files are very much affected, they sound worse then normal (which is not perfect).

dom
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5138
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge

Re: ALSA on Raspbian

Tue Jul 17, 2012 4:57 pm

lintweaker wrote:@bryankemp
I can confirm that stressing the CPU (by executing:
time echo "scale=5000; 4*a(1)" | bc -l -q which pegs the cpu for ~ 85%) the sound quality stays constant for normal mp3s. Higher frequency (> 48Hz) files are very much affected, they sound worse then normal (which is not perfect).
Are we talking HDMI or analogue audio here?

kadamski
Posts: 187
Joined: Fri Jun 08, 2012 10:56 pm

Re: ALSA on Raspbian

Tue Jul 17, 2012 5:21 pm

Sorry to interrupt your thread but i have finally got plug plugin working on internal audio device. To make it working, I had to use mmap emulation ALSA plugin. Here's how I've set it up:

Code: Select all

pcm.mmap0 {
    type mmap_emul;
    slave {
      pcm "hw:0,0";
    }
}

pcm.!default {
  type plug;
  slave {
    pcm mmap0;
  }
}
So it seems that plug plugin needs working mmap in order to work properly. The only problem using mmap_emul is that it make latency worse.

dom
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5138
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge

Re: ALSA on Raspbian

Tue Jul 17, 2012 5:28 pm

kadamski wrote:So it seems that plug plugin needs working mmap in order to work properly. The only problem using mmap_emul is that it make latency worse.
That sounds interesting. So we want to add mmap support to alsa driver:
https://github.com/raspberrypi/linux/bl ... 2835-pcm.c

I know you can add MMAP flags to the snd_pcm_hardware, and you are supposed to do something different with the audio buffers (looking at sound/drivers/dummy.c).
Do you know enough about this to know what is needed?

lintweaker
Posts: 33
Joined: Mon Jul 09, 2012 6:26 pm

Re: ALSA on Raspbian

Tue Jul 17, 2012 6:22 pm

dom wrote:
lintweaker wrote:@bryankemp
I can confirm that stressing the CPU (by executing:
time echo "scale=5000; 4*a(1)" | bc -l -q which pegs the cpu for ~ 85%) the sound quality stays constant for normal mp3s. Higher frequency (> 48Hz) files are very much affected, they sound worse then normal (which is not perfect).
Are we talking HDMI or analogue audio here?
I my case its usb-audio so any problems are usb (dwc_otg) related. I have not tried hmdi audio yet, it's on the todo list.

kadamski
Posts: 187
Joined: Fri Jun 08, 2012 10:56 pm

Re: ALSA on Raspbian

Wed Jul 18, 2012 8:32 pm

dom wrote:That sounds interesting. So we want to add mmap support to alsa driver:
https://github.com/raspberrypi/linux/bl ... 2835-pcm.c
Having mmap support would be great not only for plughw but also for things like jackd, dmix etc.
dom wrote: I know you can add MMAP flags to the snd_pcm_hardware, and you are supposed to do something different with the audio buffers (looking at sound/drivers/dummy.c).
Do you know enough about this to know what is needed?
Unfortunately, I don't have experience in writing ALSA drivers. And there is very little documentation about that, and especially about mmap support. It's even hard to find any documentation about using mmap mode from userspace.

But here's how *I think* it's working. First of all, it's good to know how is mmap used from userspace. So in mmap mode, instead of using standard *writei/*writen ALSA functions to write frames to sound device, you should use snd_pcm_mmap_begin(), which will return information about memory area, offset etc, where you can put your frames to. Now you can write your frames in this area and when you finish, you should call snd_pcm_mmap_commit(). The area you are writing to is suposed to be DMA mapped buffer of sound device. If I understand this correctly, ALSA should handle changing pointers and all accounding by itself, at least in common situations (continues memory area for buffer etc).
Now on the driver side - it seems that the implementation would be easy if only hardware buffer could be mapped to the DMA regions that are set by snd_pcm_lib_malloc_pages. It seems for me, that if it's set like this, you don't have to worry about much else. Looking at the current implementation of the driver, it seems that it's not trivial to implement something like this as for now, it does uses custom .copy callback that calls some vchiq functions to push frames to sound device instead of accessing sound buffer directly.

dom
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5138
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge

Re: ALSA on Raspbian

Wed Jul 18, 2012 11:36 pm

kadamski wrote:Now on the driver side - it seems that the implementation would be easy if only hardware buffer could be mapped to the DMA regions that are set by snd_pcm_lib_malloc_pages. It seems for me, that if it's set like this, you don't have to worry about much else. Looking at the current implementation of the driver, it seems that it's not trivial to implement something like this as for now, it does uses custom .copy callback that calls some vchiq functions to push frames to sound device instead of accessing sound buffer directly.
Correct. Audio samples are passed in chunks through a message passing interface (vchiq). The ARM is not able to directly output the audio (on HDMI it needs to be interleaved with the video signal generated by GPU).
Presumably USB sound cards need to do a similar scheme of sending audio samples in blocks to a remote playback device. Can they handle mmap?

kadamski
Posts: 187
Joined: Fri Jun 08, 2012 10:56 pm

Re: ALSA on Raspbian

Thu Jul 19, 2012 7:16 am

Yes, USB cards can handle MMAP. I believe they do so by using some intermediate buffer that is mmaped and then copied in chunks after userspace finishes writing to it. There is not too much documentation about that, only found this to mention it. I believe, current approach is the first case, while we need second case, right ? I believe, it refers to this driver, but it's not an obvious example for me. My main concern is - how do we know when userspace write to this mmaped buffer? We need to know when they called snd_pcm_mmap_commit so we can push data from intermediate buffer using vchiq.

kadamski
Posts: 187
Joined: Fri Jun 08, 2012 10:56 pm

Re: ALSA on Raspbian

Thu Jul 19, 2012 7:49 am

Ok, so this thread is quite helpful to understand what has to be done. It's still not enough for me to know how to do this but at least I now get the context. So there is "ack" callback that is called after read/write operation to the buffer, which should use appl_ptr to know how much data was written. And there is pcm-indirect.h, that has some helper functions. I have to join all this together and do some experiments to see how it's actually working. Unless that's that's enough to be obvious for you how this should be implemented?

dom
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5138
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge

Re: ALSA on Raspbian

Thu Jul 19, 2012 8:49 am

kadamski wrote:I have to join all this together and do some experiments to see how it's actually working. Unless that's that's enough to be obvious for you how this should be implemented?
Please feel free to experiment. Patches would be very welcome.

Efcis
Posts: 26
Joined: Thu Sep 01, 2011 8:26 pm

Re: ALSA on Raspbian

Thu Jul 19, 2012 3:03 pm

Sorry for hijacking this thread, but are your various experiment could also resolve https://github.com/raspberrypi/firmware/issues/62, or is a different topic ?

I'm trying to play low sample rates (11025 or 22050) mono raw files with aplay with no success.

kadamski
Posts: 187
Joined: Fri Jun 08, 2012 10:56 pm

Re: ALSA on Raspbian

Thu Jul 19, 2012 6:15 pm

You have problems only when playing using pulseaudio or also without it? Unfortunately, I don't have any knowledge about pulseaudio right now.

Efcis
Posts: 26
Joined: Thu Sep 01, 2011 8:26 pm

Re: ALSA on Raspbian

Thu Jul 19, 2012 6:30 pm

kadamski wrote:You have problems only when playing using pulseaudio or also without it? Unfortunately, I don't have any knowledge about pulseaudio right now.
If you're replying to me (not sure), I don't use pulseaudio at all. I just try to play plain mono binary raw files stored on my SD card (or the stdin pipe) with

Code: Select all

aplay -t raw -r samplingRate -c 1 -f S16_LE audioFile.raw
It only works for file sampled at 44100 and 48000. When using other sampling rates, aplay doesn't complain, but nothing is played (I use the 3.5 jack output), and once done, I have to reboot the raspi to hear another sound again.

kadamski
Posts: 187
Joined: Fri Jun 08, 2012 10:56 pm

Re: ALSA on Raspbian

Thu Jul 19, 2012 6:35 pm

Yes, I was replaying to you. Is it possible to give me access to the not working .wav file so I could experiment with it?

Efcis
Posts: 26
Joined: Thu Sep 01, 2011 8:26 pm

Re: ALSA on Raspbian

Thu Jul 19, 2012 7:00 pm

kadamski wrote:Is it possible to give me access to the not working .wav file so I could experiment with it?
Sure.

Please, check http://f6fvy.free.fr/raspi/

These files are raw files (not wav), 1 channel (mono), signed 16 bit integer for each sample (thus the -f S16_LE switch in the aplay command).

Thanks for your experiments.

kadamski
Posts: 187
Joined: Fri Jun 08, 2012 10:56 pm

Re: ALSA on Raspbian

Thu Jul 19, 2012 8:02 pm

Ok, so I decided to first debug Efcis problems, so far I have found that:
1. I can reproduce the problem using plughw plugin, not yet tried directly using hw.
2. After it fails, ALSA driver hangs on bcm2835_audio_set_ctls_chan function(), waiting on vcos_event_wait(&instance->msg_avail_event), which is defined in drivers/misc/vc04_services/interface/vcos/linuxkernel/vcos_platform.h, I have to look at it..
3. rmmod+insmod again does not solve the problem.

kadamski
Posts: 187
Joined: Fri Jun 08, 2012 10:56 pm

Re: ALSA on Raspbian

Thu Jul 19, 2012 9:11 pm

1. It happens also when using hw:0,0 directly. And also using 2 channels, not only 1.
2. I was trying to figure out what exactly I have to do for it to hang. So I was trying different sample rates and it seems that all the rates are working ok down to some value. Testing at 2 channels, 21361 was still ok. 21360 (and everything below that) failed. For 1 channel, the values has to be doubled - it failed exactly at 42721. It can hang at higher sample rates on underrun situation. Overclocking settings has nothing to do with the value when it starts hanging. The value is always the same, across several boots.
3. Unsuprisingly, it can be reproduced also with mplayer using -srate 21360 (or lower) parameter.
3. When hanging at vcos_event_wait, it accually hangs in down_interruptible(event)
4. When killing aplay while it's hanging, I get this in dmesg

Code: Select all

vcos: [2437]: bcm2835_audio_write:778 bcm2835_audio_write: failed on vchi_bulk_queue_transmit (status=-1)
So it hangs at sending PCM keys to videocore. It looks like it hangs at vcos_event_wait, on down_interruptible(event), so exactly the same place it hangs at later, when trying to play some more files.

More testingbtommorow.

Efcis
Posts: 26
Joined: Thu Sep 01, 2011 8:26 pm

Re: ALSA on Raspbian

Thu Jul 19, 2012 9:41 pm

@kadamski Thanks for your efforts. I must confess I'm kind of lost at this point to help you ;-)
Regards

dom
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5138
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge

Re: ALSA on Raspbian

Fri Jul 20, 2012 12:26 am

@kadamski
Thanks for investigating. If you suspect a problem with videocore side, I can have a look at the weekend.

kadamski
Posts: 187
Joined: Fri Jun 08, 2012 10:56 pm

Re: ALSA on Raspbian

Fri Jul 20, 2012 6:48 am

@Efcis
Don't worry. I'm just posting my findings here so they are not lost. And maybe it will ring a bell for someone reading this, and he would know what is wrong.

@Dom
It's too soon right now, it's all just clues now. I'll let you know when I'm sure what is causing the problem (or when I'm lost and can't do any more investigations ;))

Return to “Raspbian”

Who is online

Users browsing this forum: No registered users and 5 guests