1

I'm trying to create a beeping sound to be played on my speakers from the shell.

I can easily accomplish this using aplay, but, it takes a second or so before it actually plays.

I need to have it practically instant, as I'm using it in a (headless) shell script to play a sound on a keypress. Using aplay the beep comes way too late.

I'm thinking to write something directly to a /dev sound device or something, but haven't been successful with this yet.

I'm using a Odroid-W (Raspberry Pi-clone), and have heard that, at least on the Pi, the audio jack is actually wired to two PWM GPIO pins (40, 45), writing to these pins directly however yields only silence.

It doesn't need to be a fancy sound, any buzz or beep will do.

(if all fails then I'll probably wire a GPIO pin to a makeshift mixer and connect that to the amp.)

tripleee
  • 7,699
svenema
  • 469
  • 2
  • 6
  • 17
  • 1
    There is the BEL character which should produce a beep assuming that terminal feature is enabled and connected to some sound producing something... – thrig Apr 17 '17 at 18:36
  • I think this is maybe supposed to have one of those "PC speakers" wired up. It did not output sound to the speakers for me..

    https://en.wikipedia.org/wiki/Bell_character

    – svenema Apr 17 '17 at 19:17
  • Any nontrivial audio will also depend on your platform. The aplay I suppose suggests Linux with ALSA? – tripleee Apr 17 '17 at 19:23
  • If you have latencies of a second, something in your audio setup is definitely wrong. Look at your logs for errors. The RaspPi uses the PWM (pulse-width modulation) output to produce sound, so yes, writing to the GPIO pins instead of using the PWM registers will produce nothing. The kernel driver will write to the PWM registers for you, and should do this fast enough, under normal cirumstances. – dirkt Apr 17 '17 at 19:26
  • @tripleee: Yes, Debian Jesse – svenema Apr 17 '17 at 19:41
  • @dirkt: The GPIO pins 40,45 are supposed to be the audio pins. I was expecting that when I "write" directly to these pins (using WiringPi) I would bypass the kernel. I noticed that after doing so I cannot play anything anymore and have to reboot before audio works again, so I'm guessing that I'm using the correct pins.

    about the latency.. I feel that the startup time for the aplay utility is the issue here, not really audio latency.. feel free to correct me here ;-)

    – svenema Apr 17 '17 at 19:41
  • Possibly related, cross-site: http://stackoverflow.com/questions/24507970/linux-audio-application-wanted-low-latency-playback-and-stop – tripleee Apr 18 '17 at 06:12
  • See also https://www.alsa-project.org/main/index.php/Low_latency_howto but probably overkill here. – tripleee Apr 18 '17 at 06:12
  • And finally see https://unix.stackexchange.com/questions/7067/do-the-play-utility-in-linux-really-have-a-delay-to-stop-its-process-with-r where the answer basically points to Pulseaudio instead. Do you have that available? – tripleee Apr 18 '17 at 06:15
  • Wouldn't an internal PC Speaker work in your case? If it will then here is your answer: https://unix.stackexchange.com/questions/1974/how-do-i-make-my-pc-speaker-beep – YoMismo Apr 18 '17 at 06:49

1 Answers1

0

As latency is important for the "feel" of the application (audible user feedback) I have decided to produce it by connecting an active buzzer (the active part here eliminates the need of a PWM output) to one of the gpio pins, this produces sound instantly. GPIO pins can be accessed directly using Drogons WiringPi command line utility. Downside is that you don't have control over the sound itself, but the beep that is produced is good enough for now.

To improve/control sound, one could look into a software PWM and a passive buzzer or speaker.

More information on WiringPi: http://wiringpi.com/

What's an active buzzer?: https://electronics.stackexchange.com/questions/224374/active-vs-passive-buzzer

svenema
  • 469
  • 2
  • 6
  • 17