chaosdorf / spacebell

Chaosdorf hackspace doorbell service using mqtt and PulseAudio
MIT License
0 stars 0 forks source link

How does PulseAudio sample configuration work? #8

Open msteinhoff opened 4 years ago

msteinhoff commented 4 years ago

The project uses the PulseAudio play_sample() API function to play a doorbell sound but does not explain how those samples are managed in the PulseAudio server.

msteinhoff commented 4 years ago

After a quick search for "pulseaudio samples" I found this:

The official name of this functionality seems to be sample cache:

The sample cache is an in-memory storage for short named batches of samples that may be uploaded to the server once and then played multiple times. It is usually used for event sounds.

But these documents do not contain information on how to control the sample cache.

After more digging in man pages, it looks like one can use pactl or pacmd (not sure what the difference is) with the following sub-commands to manage the sample cache:

SAMPLE CACHE
       list-samples
              Lists the contents of the sample cache.

       play-sample name sink-index|sink-name
              Play a sample cache entry to a sink.

       remove-sample name
              Remove an entry from the sample cache.

       load-sample name filename
              Load an audio file to the sample cache.

       load-sample-lazy name filename
              Create  a  new  entry  in the sample cache, but don't load the sample immediately. The sample is loaded
              only when it is first used. After a certain idle time it is freed again.

       load-sample-dir-lazy path
              Load all entries in the specified directory into the sample cache as lazy  entries.  A  shell  globbing
              expression (e.g. *.wav) may be appended to the path of the directory to add.

So I assume that you can upload audio files from your local disk to the PulseAudio server, even via the network.

msteinhoff commented 4 years ago

So I assume that you can upload audio files from your local disk to the PulseAudio server, even via the network.

This looks like an interactive mode and the sample cache is in-memory only. How are samples stored permanently in the PulseAudio server? I assume that there is some kind of startup script on the PulseAudio server which adds a list of sample files from the local disk.

When I think more about this, it might make sense to extend this project with management and synchronization of doorbell sounds. E.g. spacebell maintains a list of all available doorbell sounds, on startup it first checks if all expected sounds are available in the PulseAudio server and uploads them if necessary. (this also assumes that we have more than one doorbell sound, which is not yet implemented here).

This would serve two purposes:

YtvwlD commented 4 years ago

This way, we can even preserve the old logic with different sounds and the current one being randomly selected.