<Sound Dev> Support for linux alsa PCM devices

Pavel Hofman pavhofman at gmail.com
Sun Jun 16 08:46:16 UTC 2019


Hi,

Linux alsa sound subsystem allows defining custom PCM devices, apart
of the card-based devices (hw:CARD:UI.DEVICE_ID). These devices are
very flexible and allow lots of useful and important features. An
example where java support for PCM devices is very needed is
https://www.diyaudio.com/forums/equipment-and-tools/338888-mu-0404-linux-issues-rew-96-khz-3.html#post5824351
.

Java enumerates the main "default" device
https://github.com/openjdk/jdk/blob/master/src/java.desktop/linux/native/libjsound/PLATFORM_API_LinuxOS_ALSA_PCMUtils.c#L58
and then only devices corresponding to hardware cards
https://github.com/openjdk/jdk/blob/master/src/java.desktop/linux/native/libjsound/PLATFORM_API_LinuxOS_ALSA_PCMUtils.c#L95
. The JDK code does mapping between java deviceID and alsa string
device name.

There is a hard-coded constant ALSA_DEFAULT_DEVICE_ID which
corresponds to string "default" for snd_pcm_open. Alsa devices
corresponding to cards use IDs starting from 1
https://github.com/openjdk/jdk/blob/6bab0f539fba8fb441697846347597b4a0ade428/src/java.desktop/linux/native/libjsound/PLATFORM_API_LinuxOS_ALSA_CommonUtils.c#L87

It would be possible to start the encoded card IDs e.g. from 11 and
keep the 10 lower IDs reserved for the alsa PCM devices, just like the
first value (0) is reserved for the fixed string "default". PCM
devices to enumerate could be specified by an environment variable as
a comma separated list of strings. E.g. wine does it this way using a
register entry https://wiki.winehq.org/Sound#ALSA . The number of
devices enumerated would be limited to 10, all other items in the
variable would be ignored.

The alsa java sound module is already partly configured via
environment variables  -
https://github.com/openjdk/jdk/blob/6bab0f539fba8fb441697846347597b4a0ade428/src/java.desktop/linux/native/libjsound/PLATFORM_API_LinuxOS_ALSA_CommonUtils.h#L52
.

The module would hold static array of strings with names of the
additional PCM devices, initialized in the method iteratePCMDevices()
https://github.com/openjdk/jdk11u/blob/737d8437886ad97c6ed21a25b9911c10b3886f61/src/java.desktop/linux/native/libjsound/PLATFORM_API_LinuxOS_ALSA_PCMUtils.c#L42
. The array would map the PCM device strings:  pcm_devices[deviceID
-1] if deviceID were between 1 and 10.

This addition would greatly enhance capabilities of java sound apps on linux.

If you agree with the proposal, I can start working on a patch.

Best regards,

Pavel Hofman.


More information about the sound-dev mailing list