2

I am trying to create a simple terminal music application, lets call it a piano. The application reads keys from the key board, and outputs a sample to a file handle (pointing to a /dev/audio; which doesn't exist unless piped through padsp...but that's a another story). The application works...sort of. It reads the keys each time one is pressed, great. But the outputs occur every second key that is read... unless I call the output subroutine twice in each loop. If called only once I get no notes played for the first, but the second key pressed leads to playing of the notes of both the keys pressed). I am sorry if I haven't made this clear. Can someone explain what is happening, please? This is loop concerned in Attempt 2


ReadMode 'cbreak';
while(1){
    sleep 1/$refreshRate;
        my $key = ReadKey(-1) or next;; 
        last if ( ord($key)==27);   # escape key exits
        playNote( ord($key)%96);    # have to call play note twice 
        playNote( ord($key)%96);    
}
Saif Ahmed
  • 21
  • 2

1 Answers1

2

The sample size ($sps variable) has to be at least 1024 for the sound to play immediately when pressing a key rather than playing both notes when pressing another key. Calling playNote twice works but is a workaround for the problem. The reason is probably because pulseaudio waits until the buffer is full to send the audio to the speakers, and 900 bytes doesn't quite fill the buffer. Pulseaudio probably uses a buffer of size 1024.

Nathan Mills
  • 2,243
  • 2
  • 9
  • 15