10

As a child I've played one DOS game called Electro Body.

The game did something amazing - it played back PCM samples through the PC speaker. Not the crappy square beeps - it played real sound effects!

It was super quiet in comparison to the usual beeps that the PC speaker makes, but it was a completely new quality of sound. I never heard anything like that before or after that game.

I wonder - if there is a way in GNU/Linux to play arbitrary PCM sound streams through the PC speaker apart from just "beeps" that the beep command makes? Can I play WAV or Ogg files through that?

Apart from the fact that it'd be cool to make some sophisticated noises, one could probably use this as an analogue voltage control output - for whatever crazy DIY project.

unfa
  • 1,745
  • You can access the speaker via /dev/input/eventX on Linux, and in principle it should be possible to write a program which renders arbitrary PCM sound this way, e.g. providing a Pulseaudio or ALSA interface, but I don't know of any existing one. – dirkt Mar 24 '17 at 13:51
  • 2
    Hm, there's actually a snd-pcsp kernel module which claims to do this, but I couldn't get any recognizable sound out of my speaker this way. – dirkt Mar 24 '17 at 14:05
  • PWM was used to make PCM sounds on the PC speaker similar to what Class D amplifiers do today. It's a really neat innovation considering PC speakers aren't using a full speaker driver. – Sawtaytoes Aug 17 '21 at 02:52

1 Answers1

11

I don't have a system to test it on, but it appears that ALSA can provide mapping of output to the PC speaker. FYI, there are many pages out there that say this is a bad idea because the driver is intended as a "toy" and not for general use (it will burn a lot of CPU cycles), but that said, this should work:

# Load the PC speaker driver
sudo modprobe snd-pcsp
# Reload ALSA to find the new driver
sudo alsa force-reload
# You should now see pcsp (pcspeaker) as an ALSA output option
sudo aplay -l

Select the sound card as your output and have fun!

Sources:

http://wiki.archlinux.org/index.php/PC_speaker#ALSA

http://wiki.archlinux.org/index.php/Advanced_Linux_Sound_Architecture#Set_the_default_sound_card

http://www.linuxquestions.org/questions/slackware-14/how-do-you-use-snd-pcsp-in-slackware-14-1-a-4175534306/

Stephen Kitt
  • 434,908
  • 1
    'alsa force-reload' isn't needed.

    I did 'modprobe snd-pcsp' and after that 'aplay -l' returned a new device: 'card 4: pcsp [pcsp], device 0: pcspeaker [pcsp] Subdevices: 1/1 Subdevice #0: subdevice #0'

    – unfa Apr 03 '17 at 09:33
  • 2
    I've finally tested it today with an ASUS PRIME B350M-A motherboard.

    I was able to play sound through JACK, by creating an output port with alsa_out -j PCSP -d hw:pcsp -r 37286 -c1. It refused to run with any other sampling rate, and kept suggesting this one.

    As soon I as started this, I've heard noises that reminded me of frying bacon and electrical arcs. Subtle, but scary.

    I've hesitated to test if it'll play a sound, but I tried it - it works, but it's very quiet and there's this constant random clicking noise (a bit like a dusty vinyl record).

    Using aplay -d hw:pcsp failed.

    – unfa May 29 '17 at 19:57
  • When I do sudo modprobe snd-pcsp, I get an error saying that the module doesn't exist. – Trevor Jex May 24 '20 at 20:42
  • Trevor, you may need to install the module (or compile it from scratch). How to do that will depend on your linux distribution. – Bill Denney May 25 '20 at 21:09
  • 1
    I followed this and I can play sound using aplay -D plughw:CARD=pcsp sound.ogg but the sound comes out as noise with no discernible pitch or melody. – Yan King Yin Aug 23 '20 at 06:51