Skip to content

Multichannel pcm [v3] - #1257

Closed
ghost wants to merge 8 commits into
rpi-4.1.yfrom
unknown repository
Closed

ghost wants to merge 8 commits into
rpi-4.1.yfrom
unknown repository

Conversation

@ghost

@ghost ghost commented Jan 13, 2016

Copy link
Copy Markdown

Same as #1166, but uses the audioserv 3 API for sending the channel map. It's also rebased to the current main branch.

I'm not entirely sure whether the channel map arrives correctly. Common layouts seems to work as expected with my receiver, but then my receiver might not even interpret the channel layout at all. I don't know.

wm4 added 8 commits January 13, 2016 19:41
Allow everything the videocore accepts.
This is required at least for SPDIF. If the bitrate goes above,
videocore will either resample the audio or corrupt it due to
underruns. Supposedly the hardware isn't designed to output
higher rates, but it can still resample it down to supported
rates.

Some code is based on ac97_pcm.c.
Add audioserv 3 extensions. The changes were taken from the paste
linked here:

#1166 (comment)
Report all layouts supported by the HDMI protocol to userspace.
Make the videocore set the correct layout according to the
userspace request.

Some code taken from patch_hdmi.c. In particular, the HDMI channel
layout table was copied without changes - with the idea in mind that
hopefully it can be shared one day. Or at least updating it will be
simpler.

In my tests, everything appears to work, except when outputting
FL FR RL RR. Then my receiver outputs RL on both the RL and RR
speakers, while RR is never heard.
I don't think the ALSA framework provides any kind of automatic
synchronization within the control callbacks. We most likely need
to ensure this manually, so add locking around all access to shared
mutable data. In particular, bcm2835_audio_set_ctls() should
probably always be called under our own audio lock.
Pad the unused channels with NA. This means userspace needs to write
additional, silent padding channels, which is not ideal, but better
than noise.

Works around noise at the following channel counts: 3, 5, 6, 7
Sending more than 2 channels to videocore while outputting to analogue
mysteriously outputs heavy artifacts. So just paint it over with a
hack: if analogue is explicitly selected as destination, do not
reporting support for anything other than stereo.

I'm not sure how to deal with the auto case (destination 0). There's
probably way to retrieve this and even to listen to plug events, but
I didn't find one yet, and it's probably not worth the trouble. Just
don't use this setting, I guess. Unless you like noise.

Changing the setting while an audio stream is active also doesn't
work properly. We could probably interrupt running streams by
returning ENODEV or using kernel hotplug stuff (maybe), but that
also doesn't seem worth the trouble.
It appears the GPU only sends us a message all 10ms to update
the playback progress. Other than this, the playback position
(what SNDRV_PCM_IOCTL_DELAY will return) is not updated at all.
Userspace will see jitter up to 10ms in the audio position.

Make this a bit nicer for userspace by interpolating the
position using the CPU clock.

I'm not sure if setting snd_pcm_runtime.delay is the right
approach for this. Or if there is maybe an already existing
mechanism for position interpolation in the ALSA core.

I only set SNDRV_PCM_INFO_BATCH because this appears to remove
at least one situation snd_pcm_runtime.delay is used, so I have
to worry less in which place I have to update this field, or
how it interacts with the rest of ALSA.

In the future, it might be nice to use VC_AUDIO_MSG_TYPE_LATENCY.
One problem is that it requires sending a videocore message, and
waiting for a reply, which could make the implementation much
harder due to locking and synchronization requirements.
@ghost ghost mentioned this pull request Jan 13, 2016
popcornmix added a commit to raspberrypi/firmware that referenced this pull request Jan 19, 2016
popcornmix added a commit to Hexxeh/rpi-firmware that referenced this pull request Jan 19, 2016
@popcornmix

Copy link
Copy Markdown
Collaborator

As this PR has some potential to break things, I've added the commits to 4.4 branch first.
I've rebuilt kernel, and you can get it with sudo BRANCH=next rpi-update
We'll see if we get any reports for success/failure from that.

What's the best way of testing multichannel output?

@ghost

ghost commented Jan 19, 2016

Copy link
Copy Markdown
Author

What's the best way of testing multichannel output?

Not sure. Other than programs which use the channel map API, I don't think ALSA has a test program or anything.

@popcornmix

Copy link
Copy Markdown
Collaborator

So what have you tested with?
mpv I guess? Should that handle multichannel alsa output if you play a multichannel file?
Any other apps you've tested with?

@ghost

ghost commented Jan 19, 2016

Copy link
Copy Markdown
Author

I tested only with mpv. I also did some tests with some ALSA tools, but since they don't use the channel mapping API, these are pretty worthless.

@ghost

ghost commented Jan 19, 2016

Copy link
Copy Markdown
Author

PS: it's possible that other applications have problems with this due to the strange padding configuration, but on the other hand it shouldn't be worse than say HDMI over the nvidia binary driver. Possible that the NA channel entries cause issues to other programs, though.

@keylimesoda

Copy link
Copy Markdown

I'm evangelizing this change with the DIYAudio and Volumio communities, pointing them to this thread.

@wm4, would it be simple to add step-by-step instructions for users wanting to validate this fix? Looks like it's a kernal update and then run mpv?

@ghost

ghost commented Jan 28, 2016

Copy link
Copy Markdown
Author

Hm, that's kind of hard. I think I've tested enough with mpv; anything that can use the channel map API should be able to make is of it. My main "worry" is that the channel maps include NA channels (which is supposed to make the application insert padding), and I'm not sure how widely understood this is.

@ghost

ghost commented Jan 28, 2016

Copy link
Copy Markdown
Author

PS: not sure if I made it explicit yet, but this also requires raspbian with updated alsa-lib. Not sure what's raspbian's state - my current installation is an old one, and it was indeed too old.

@clivem

clivem commented Jan 30, 2016

Copy link
Copy Markdown

@wm4 Can you also comment on why, with these changes having been applied to 4.4.y, it is not possible to save/restore via alsactl. eg.

$ sudo alsactl store 0
alsactl: get_control:256: Cannot read control '3,0,0,Playback Channel Map,0': No such device

@ghost

ghost commented Jan 30, 2016

Copy link
Copy Markdown
Author

It appears to access the channel map control even though no playback is active, which is not supported by the changes.

@clivem

clivem commented Jan 30, 2016

Copy link
Copy Markdown

Hmmm. Just about every distribution is storing and re-storing at shutdown/boot. Built-in ALSA device is typically card 0, and if "alsactl store" errors on the first device before it even gets to the cards whose mixer settings I do really want stored, then those settings for the cards I do want stored. eg. mute/volume level, do not survive a reboot...... These changes need to be backed out while things like this are thought about, IMHO!!!!!!

@eblancog

Copy link
Copy Markdown

Here's a link to some files for testing multichannel output:

https://www2.iis.fraunhofer.de/AAC/multichannel.html

You can either download the files or stream them via RTMP. I've done some testing streaming them to omxplayer, which supports multichannel via openMAX, and everything works fine in my 5.1 surround sound setup (channel identification/assigment, etc).

Hope it help with testing. I'm following this PR with utmost interest. :)

@ghost

ghost commented Jan 31, 2016

Copy link
Copy Markdown
Author

The OpenMAX codepath is completely different, and is not affected by this PR at all. So comparing ALSA results with OMX would be interesting.

(I also once thought about implementing this with an ALSA plugin that uses OMX, but discarded the idea.)

@Migs3

Migs3 commented Feb 4, 2016

Copy link
Copy Markdown

I'd like to test this, but I tried using sudo BRANCH=next rpi-update on my Raspbian Jessie R-Pi2, and it killed the sound card completely. No sound card with aplay -l and mixer gives the message that Gstreamer can't find any audio devices.

I got it back, of course, by reverting to 4.1.y (or just using sudo rpi-update)

Is there something I'm missing?

@ghost

ghost commented Feb 4, 2016

Copy link
Copy Markdown
Author

What kind of sound card is it? I suppose I could try if this happens with an USB dongle.

@Migs3

Migs3 commented Feb 4, 2016

Copy link
Copy Markdown

It's the built in sound on the Raspberry Pi 2, I'm connecting via HDMI.

@pelwell

pelwell commented Feb 4, 2016

Copy link
Copy Markdown
Contributor

On 4.4 and above you need to add dtparam=audio=yes to your config.txt. This also works on earlier kernels. What has changed is that loading the module from /etc/modules or simply modprobe'ing it no longer works.

@Migs3

Migs3 commented Feb 4, 2016

Copy link
Copy Markdown

Ah! There it is, Now I'll do some testing later when I get home. Thanks!

@jrubinstein

Copy link
Copy Markdown

@just ran the sudo BRANCH=next rpi-update script on my rpi2, and now I don't have audio.
I'm testing using the files from Richard Taylor:
http://faculty.tru.ca/rtaylor/rt-plugins/chan_labels_6.wav
http://rtaylor.sites.tru.ca/2013/06/25/digital-crossovereq-with-open-source-software-howto

where's the config.txt file live? Find shows nothing.

Nevermind, found it :)

New problem: I'm still only getting 2 channels out from my hdmi :(

@volkand22

Copy link
Copy Markdown

I think i discovered an issue: No sound output to channels 7 and 8 on hdmi (tested with speaker-test and ecasound)
Kernel is 4.14.31, compiled with channels_max = 8 and rate_max = 96000

@jrubinstein

jrubinstein commented Apr 2, 2018 via email

Copy link
Copy Markdown

@volkand22

Copy link
Copy Markdown

I did a clean install of Raspbian, still no luck with 7.1 surround. My Receiver (Denon 1911) recognizes only 5.1. With Windows and Equalizer APO the 7.1 is working. I tried with and without your Alsa-config-file.
Are you using a Pi3 too or still a Pi2?

@jrubinstein

jrubinstein commented Apr 9, 2018 via email

Copy link
Copy Markdown

@volkand22

Copy link
Copy Markdown

Thank you, that would be very much appreciated. The building took one night at the Pi3.

@jrubinstein

Copy link
Copy Markdown

So i'm trying to test with the pi3 today and when I pulled down the kernel, there is not a /sound/arm/bcm2835-pcm.c file any more. Perhaps that is the problem. The file I'm seeing that may be relevant is /sound/atmel/ac97c.c

I will try building a new kernel after changing that file.
... the kernel will not build on volumio. Will have to do some other stuff, so it may be awhile before I can test fully. :/

@volkand22

Copy link
Copy Markdown

I patched the file drivers/staging/vc04_services/bcm2835-audio/bcm2835-pcm.c, then speaker-test with 8 channels was possible.

@jrubinstein

jrubinstein commented Apr 14, 2018 via email

Copy link
Copy Markdown

@volkand22

Copy link
Copy Markdown

No, its not working because the Receiver does not recognise 7.1 from the PI, only 5.1 is working.

@jrubinstein

jrubinstein commented Apr 14, 2018 via email

Copy link
Copy Markdown

@volkand22

Copy link
Copy Markdown

Here are the Kernel-Building commands for easy cut & paste:

KERNEL=kernel7 && make bcm2709_defconfig && make -j4 zImage modules dtbs && sudo make modules_install && sudo cp arch/arm/boot/dts/.dtb /boot/ && sudo cp arch/arm/boot/dts/overlays/.dtb* /boot/overlays/ && sudo cp arch/arm/boot/dts/overlays/README /boot/overlays/ && sudo cp arch/arm/boot/zImage /boot/$KERNEL.img

@volkand22

Copy link
Copy Markdown

I tried your asoundrc, it did not work. I modified only the crossover frequency, maybe there are other changes necessary?

@jrubinstein

jrubinstein commented Apr 14, 2018 via email

Copy link
Copy Markdown

@volkand22

Copy link
Copy Markdown

no, did not changed the t.table.

Here is the result of the speaker-test with 8 channels, with or without the asoundrc nothing changed, 0, 1, 3, 4, 5 are working, 6 and 7 did not work, receiver screen displays 5.1

0 Front L
1 Front R
2 (sub, no amplifier, test not possible)
3 Center
4 Surround L
5 Surround R
6 no output
7 no output

@jrubinstein

Copy link
Copy Markdown

@volkand22 can you do a pastebin of your asroundrc file?

@volkand22

Copy link
Copy Markdown

Here is the asoundrc:
asoundrc.txt

@jrubinstein

Copy link
Copy Markdown

@volkand22 you are just using my asoundrc? That should work, you'll know it works because different speakers will produce different frequency ranges. Is that happening? If not, then you need to update asound.conf or alsa.conf (see: http://www.alsa-project.org/main/index.php/Asoundrc#The_.asoundrc_file_format)
You are only seeing 5.1 on your receiver? When you changed the bcm2836 file before compiling the kernel, did you change it to max_channels = 8? If you put in 7.1 or something, that wouldn't work.

@volkand22

Copy link
Copy Markdown

Yes, i have the kernel compiled with 8 channels and i still see only 5.1 on the receiver, that is the problem. I tried with and without the asoundrc-file. If it is working on your Pi3 now, please could you provide the 3 mentioned config files? I looked into the alsa.conf but i have no idea what to do.

@mikebrady

Copy link
Copy Markdown
Contributor

@wm4 -- I'm about to submit a patch to do interpolation based on your patch. Would you like to have your name associated with it? (Sorry, can't find an email for you anywhere.)

@lategoodbye

Copy link
Copy Markdown
Contributor

@mikebrady Please do not wait too long for a feedback here. You can still reference on this pull request or single commit.

@kpelckmans

Copy link
Copy Markdown

It seems that the first commit would already solve multichannel in alsa: a569b82

This comment was already given on feb 26 2016:

Okay, @wm4 I pulled a clean version of the kernel and edited only the channels =2 to channels =8 and max rate to 96000, and it works with no other mods or channel mapping change. I think your PR is messing with the channel mapping and Alsa doesn't like it.

Could someone just merge this commit in the stable branch?

@lost-carrier

Copy link
Copy Markdown

+1

1 similar comment
@Sethur

Sethur commented Apr 27, 2019

Copy link
Copy Markdown

+1

@JamesH65

JamesH65 commented Aug 5, 2019

Copy link
Copy Markdown
Contributor

@popcornmix Any thoughts on what we should do with this PR?

@popcornmix

Copy link
Copy Markdown
Collaborator

@wm4 has stated he is no longer interested in this PR.
We have actually cherry-picked parts of it (the interpolation commit is now upstream, and the increase to 8 channels and higher samplerates and some of the locking improvements).

The channel mapping interface is the main part missing but it is not something easy to control through alsa, so I haven't been able to test it (I think mvp may use this api, but not certain).

I think we can close this. If we want to better support channel mapping there may be commits of interest here (but the alsa driver has been upstreamed in the meantime and these commits are touch to apply given the changes).

@popcornmix popcornmix closed this Aug 5, 2019
@danielhjames

Copy link
Copy Markdown

Hi @popcornmix thanks for merging the .channels_max = 8 patch in 56c0724 😃

AdUki pushed a commit to AdUki/rpi-sound-tester that referenced this pull request Sep 21, 2026
The firmware driver passes only a channel count and the firmware picks
the speaker layout from it (the per-stream channel map was never merged,
raspberrypi/linux#1257), so only counts with one unambiguous layout are
offered. The slot order is ALSA's, as measured on a Pi 3; it is not yet
confirmed against a real multichannel sink.

Surround is limited to 48 kHz: the Pi carries 8 HDMI channels only up to
48 kHz (raspberrypi/linux#3933).

Signed-off-by: Simon Mikuda <simon.mikuda@streamunlimited.com>
AdUki pushed a commit to AdUki/rpi-sound-tester that referenced this pull request Sep 21, 2026
…onsole

The firmware driver passes only a channel count and the firmware picks the
speaker layout from it (the per-stream channel map was never merged,
raspberrypi/linux#1257), so only counts with one unambiguous layout are
offered. The slot order is ALSA's, as measured on a Pi 3; it is not yet
confirmed against a real multichannel sink. Surround is limited to 48 kHz:
the Pi carries 8 HDMI channels only up to 48 kHz (raspberrypi/linux#3933).

HdmiOutput becomes SocOutput so HDMI and the jack share one thread,
converter and latency servo. The jack is stereo by construction: a stored
layout wider than the sink reads as stereo, so nothing can make the engine
write past its two-slot block.

The device picker lists ALSA's playback devices, leaving out the Octo,
which the engine holds open.

Signed-off-by: Simon Mikuda <simon.mikuda@streamunlimited.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Waiting for external input Waiting for a comment from the originator of the issue, or a collaborator. Waiting for internal comment Waiting for comment from a member of the Raspberry Pi engineering team

Projects

None yet

Development

Successfully merging this pull request may close these issues.