Blog‎ > ‎

Fun with C - IV

posted 23 Jun 2012, 05:51 by olnf Admin
Author: Sudeep Jaiswal

If you studied C in school/college, you probably remember it as a boring subject that mostly went over your head. You probably learnt to perform calculations, sorting numbers and other banal things like finding if an year was leap or not. But did you know that C can be used to play sounds?


Every computer, old and new, desktop or laptop has an internal speaker. That’s how old DOS games like ‘Prince of Persia’ played sounds and music. Nowadays, you’d almost never get to hear it on your machine. But if you were to uninstall your audio driver… The internal speaker, as I understand it, is located somewhere on the motherboard. If you don’t know what the motherboard is, it’s a large green plate inside the CPU to where all connections come and go. If don’t know what the CPU is, I can’t believe how you know where to look for the computer’s ON button. Or maybe you’ve always owned a laptop.

Now, the internal speaker can be made to emit tones of various frequencies; low frequency sounds that chickens can hear or very high frequency sounds that are audible to dogs. Now, the range of frequencies that humans can hear is between 20 Hz and 20 KHz. This varies between humans, and of course, as we get older, there is some hearing loss. Before I continue, a word of warning; I do not condone cruelty to animals… except mosquitoes, houseflies, cockroaches and ants that invade your food. Never mind the technicality.

---


C has a function called ‘sound()’ which lets us make the internal speaker emit a tone of any frequency that we like. It emits the tone for a very short duration of time, even shorter than a beep. To counter that, there is a ‘delay()’ function that lets us extend the duration of the tone to a time we specify. Finally, if we fail to turn off the internal speaker, a harsh, ear-splitting tone continues to beep until we are out of DOS.


Line

Program

1

#include<stdlib.h>

2

#include<dos.h>

3

int main()

4

{

5

      int i,a,b;

6


for(i=1;i<=25;i++)

7

      {

8

            b=random(20);

9

            a=random(b);

10


while(a!=0)

11


{

12


sound(random(1000));

13


delay(random(500));

14


a--;

15


}

16


delay(random(500));

17


}

18


nosound();

19


return 0;

20

}



If you’re not familiar with C or  have not read ‘Fun with C, read it now so that you understand the basics of the program. I will go only into the logic. Inside the ‘for’ loop (line 6), which will run for 25 times, I am assigning a random number into variable ‘b’ (line 8). ‘random()’ is a function that generates a random number. ‘random(20)’ will generate a random number between 0 and 19. In line 9, a random number between 0 and whatever number is in ‘b’ minus one will get stored in variable ‘a’. Let’s assume that ‘a’ holds the value 7 now. Now, a ‘while’ loop (line 10) is being run which will run while the value of ‘a’ is not zero. The operations being performed if the condition is found true are:

  1. Playing a sound of frequency between 0 and 999 Hz
  1. A time delay of 0-499 milliseconds
  1. Decrementing value of ‘a’ by 1

Understand that ‘a--’ (line 14) means ‘a=a-1’. In words; ‘reduce value of ‘a’ by 1 and store it in ‘a’’. So going back to line 10, ‘a’ holding a value 7 would mean the condition is true. A random number between 0 and 999 will be generated and a tone of that frequency will be played. After that, a random number between 0 and 499 will be generated and there will be time delay of that many milliseconds before ‘a’ is decremented to become 6. Since we did not turn the internal speaker off, the tone continues to play during the delay (line 13). Next, the condition will again be checked (line 10), will be found true and the cycle continues until ‘a’ becomes 0, after which the ‘while’ loop will be ignored and line 16 will be executed.

Like before, a random number between 0 and 499 is generated and for that many milliseconds the delay occurs, ‘i’ is then incremented by 1 (‘i++’, line 6) and the condition in the ‘for’ loop is checked again. This was one cycle. This will go on 24 more times until the value of ‘i’ becomes greater than 25. After 25 full cycles, line 18 will be executed. ‘nosound()’, as you may have guessed turns the internal speaker off. It’s not necessary to do that but it’s just good manners, like turning off the electricity while leaving a room.

---


In the download, I have included the ‘exe’ and an ‘mp3’ file; because the ‘exe’ probably won’t run if you’re on a newer computer. The tones may sound random, but actually they are not. The logic I’ve used to generate a seemingly random pattern of tones is constant; the same lines of code are executed every time. You might think the ‘random()’ function would make a difference, that it would generate different numbers every time resulting in different sounding tunes. However, this is not the case. The ‘random()’ function itself uses some logic to generate numbers and that too is the same every time. So no matter how many times you run the ‘exe’, the same tone would get played.

Download: music.zip (201 kb)

Comments