I'm writing a "play sound" program on Reapberry Pi with ALSA.
I called snd_pcm_writei every 1280 samples.
I wish to add a small LED, and let it "lighter while the sound is louder, and darker while the sound is quieter."
My plane is, if there's a callback every short time period(ex: 100ms), and I can get the instantaneous volume in the callback, I can control the LED in it.
In android, there's AudioTrack.setPositionNotificationPeriod. However, I've no idea how to do this under Linux with ALSA.
Could anyone give me some advise?
The playback function looks like this:
// nLeftFrameSize: Total sample number.
// hDevice: Play device handle (initialized beforehand).
// lpbyBuffer: Sample buffer.
while(nLeftFrameSize > 0){
nRes = (int)snd_pcm_writei(( snd_pcm_t*)hDevice, lpbyBuffer, 1280);
nLeftFrameSize -= 1280;
}
I've tried calculate RMS in the while loop before the snd_pcm_writei(), set the LED brightness, and sleep, to make sure the LED can light up while these 1280 samples are playing. But this cause the sound discontinuous.
So I'd create another thread for the LED control, and I can sleep in that thread without bothering the playback.