This hosting is not for free. You might want to consider disabling AdBlock or other extensions hiding ads. I'd appreciate that! Even if you don't do that, this web is fully functional so don't worry.

Privacy Terms

Internal PC Speaker Music Programming (Linux)

A long time ago on a high school when we had a programming class I got this idea to make a song played my this PC speaker. You know, the speakers are in most cases not present at school computers and the only sound you computer does is the beep sound when you turn it on. I thought that it would be a great idea to use it! So I wrote a simple program in Pascal, then rewrote that in C (since I learned I can do it in C as well) and played it. The teacher had no idea what's going on. Haha yea always trolling...

Anyway, this PC speaker or internal speaker is a simple sound generating device that was mainly used in the old computers for a diagnostic messages, old games and many stuff that needed this sound output without using the modern speakers. For example BIOS uses this PC speaker for example to let you know that your hardware is alright and starting.

This is nothing difficult, just a funny piece of programming that sounds pretty cool, at least to me. In Linux you can use a beep command (apt-get install beep) where you simply give a frequency and duration. The best way is to make a shell script like that with notes.

beep -f 500 -l 250
beep -f 240 -l 500
sleep 0.2
beep -f 350 -l 200
...

The tone frequencies can be found here: https://courses.engr.illinois.edu/ece390/books/labmanual/io-devices-speaker.html

If you want to use C, well let's take a look at the beep command implementation here: http://www.johnath.com/beep/beep.c. I have put a simplified version below.

int fd = open("/dev/console", O_WRONLY);
 
  ioctl(fd, KIOCSOUND, (int)(1193180/500));
  usleep(250000);
  ioctl(fd, KIOCSOUND, 0);
  
  ioctl(fd, KIOCSOUND, (int)(1193180/240));
  usleep(500000);
  ioctl(fd, KIOCSOUND, 0);
  
  usleep(200000);
  
  ioctl(fd, KIOCSOUND, (int)(1193180/350));
  usleep(200000);
  ioctl(fd, KIOCSOUND, 0);
  
...
  close(fd);

Be sure to type sudo modprobe -v pcspkr before and run the compiled program with sudo as well.

Sources: Bash version of Tetris Theme and C template with example (was too lazy to rewrite the whole song).

Oh and here is a demo...

Published:

Keywords: beep,old school,code,software,script,bash
#linux #beep #speakers #oldschool #oldschoolgaming #tetris #programming #bash

You might also be interested in these articles:

Video Encoding (Transcode, Convert, Compress, Codec) Simply Explained

Weird Positions at the Beginning of Karate Kata - Useless Techniques?

Implementing Collision Detection & Physics Engine

Top 10 Beautiful Film and TV Show Soundtracks

How to Really Overcome Common Practical Problems in Meditation

Comments

Write a new comment:

All the comments are reviewed before publishing! Meaningless posts are automatically refused.

corrupted midi file - 3. 2. 2020

You can also use the C code to make microtonal music. Using the scale+note to frequency formula you can create your own scales instead of just 12 notes: https://pages.mtu.edu/~suits/NoteFreqCalcs.html Turns out most pc speakers also have a resonant frequency so some notes will be louder than others, and a lot of pc speakers work by just internally looping while decrementing a value, then when it hits zero it goes back to the given frequency. This produces a sawtooth waveform but my point is that sometimes the modulus of this (in your code it's the magic number 1193180) with the frequency you give it won't always produce very nice sounding results. Technically by spamming a frequency to the speaker you can even make it so that it never is able to reach 0, because before it does it's always interupted by the frequency change request, creating some interesting noises.

Hitokage - 4. 2. 2020

I see, thanks for the comment.