Skip to content Skip to navigation

Connexions

You are here: Home » Content » Building a music tuner program.

Navigation

Content Actions

  • Download module PDF
  • Add to ...
    Add the module to:
    • My Favorites
    • A lens
    • An external social bookmarking service
    • My Favorites (What is 'My Favorites'?)
      'My Favorites' is a special kind of lens which you can use to bookmark modules and collections directly in Connexions. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need a Connexions account to use 'My Favorites'.
    • A lens (What is a lens?)

      Definition of a lens

      Lenses

      A lens is a custom view of Connexions content. You can think of it as a fancy kind of list that will let you see Connexions through the eyes of organizations and people you trust.

      What is in a lens?

      Lens makers point to Connexions materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

      Who can create a lens?

      Any individual Connexions member, a community, or a respected organization.

    • External bookmarks
  • E-mail the author

Recently Viewed

This feature requires Javascript to be enabled.

Building a music tuner program.

Module by: S Jagannathan

Most of us listen to music on our computers. Some of us even compose music using existing software. But we can program a computer to produce sounds too. Some of these sounds will be musical, others will form the range of sounds we can hear as human beings. But why do this? This is the age of the Hubble Space Telescope sending us stunning false color images of galaxies billions of light years away. What is the point of looking at the rings of Saturn thru a small telescope in the back garden? Only the simple pleasure of seeing Saturn with our own eyes! Similarly, by programming a computer to produce even simple sounds, we can intimately understand how sound works. This will in turn let us deeply appreciate the music we listen to and maybe even give us the courage to compose our own music!

So let us begin with some definitions. What is sound? Sound is our brain's response to vibration. If there is no vibration, there is no sound. Fortunately, many objects vibrate. If you tap an object, it will start vibrating by going up and down, up and down, up and down and so on until it runs out of energy. An up and down motion is one cycle of vibration or simply one vibration. The number of times it vibrates every second is the frequency of vibration. The distance it goes up or down as it vibrates is the amplitude of vibration.

When we talk of programming a computer to produce sound, we are actually talking about controlling a loudspeaker via a computer program to produce sound. The loudspeaker is a carefully designed vibrator. If we send a +1, it flexes to maximum in one direction (say outwards). If we send a -1, it flexes to maximum in the opposite direction (say inwards). We can also send a number between -1 and +1 and the computer will set the loudspeaker to an appropriate intermediate position. How often can we set the loudspeaker position? Prepare to be amazed. When playing music from your CD, your computer is setting the loudspeaker position 44100 times a second! 48000 and 96000 times a second are also possible. Let us assume we can set the loudspeaker position 48000 times a second. If we send a 1 followed by -1 we have sent a cycle of vibration. The loudspeaker vibrates once. We can send 24000 such cycles in 1 second. If we did, the loudspeaker will vibrate 24000 times a second! But we wont hear a thing because we can hear only upto 20000 vibrations per second! The lowest we can hear is 20 vibrations per second. The lowest a loudspeaker can vibrate is 1 vibration per second (send 24000 +1s and 24000 -1s every second). Thus by programming a computer, we can generate the entire range of sounds we can hear and beyond.

A bookmark of a web page contains its URL address and an easy to remember name for recalling it later. We bookmark a web page because we find it interesting. A musical note is a bookmark too. It points to a sound we find interesting among all possible sounds. This bookmark names the sound with a letter (A to G) padded with a number (eg. C4) and contains its frequency of vibration. For example, the note C4 (informally known as middle C as it is found in the middle of a Piano keyboard) has a frequency of 261.626 cycles per second.

Let us try to produce middle C on our loudspeaker. Once again we assume we can set the loudspeaker position 48000 times a second. This means we can send a cycle of middle C by setting the loudspeaker position 48000 / 261.626 = 183.4680 times. Since we represent a cycle with just +1s and -1s, we first set the loudspeaker to +1 91.734 times and then set it to -1 91.734 times because 183.4680 / 2 = 91.734. If we do this continuously, we hear middle C! You maybe thinking I can set the loudspeaker 91 or 92 times but how do I set it 91.734 times? Well, you cant. So is it ok to ignore the 0.734 and always set it 91 times? Or how about neatly rounding 0.734 to 1 and always set it 92 times? Actually, it is better to be more accurate than either because your ears can distinguish two sounds that are just half a cycle apart! We will fool the ear more fairly. We start by setting the loudspeaker to +1 91 times and store the original deficit of 0.734 somewhere. When we get ready to set it to -1 91 times, we notice that the new deficit of 0.734 added to the old deficit of 0.734 gives 1.468 (ie) an extra opportunity to set the loudspeaker position. Why not use it now? So we send 92 -1s instead of 91 -1s. We deduct this used opportunity from the deficit and leave it at 0.468. Rinse. Lather. Repeat. Table 1 below shows this for the first four cycles.

Cycle Number Number sent Amount sent Deficit
1 +1 91 0.734
1 -1 92 0.468 = 0.734 + 0.734 - 1
2 +1 92 0.202 = 0.468 + 0.734 - 1
2 -1 91 0.936 = 0.202 + 0.734
3 +1 92 0.670 = 0.936 + 0.734 - 1
3 -1 92 0.404 = 0.670 + 0.734 - 1
4 +1 92 0.138 = 0.404 + 0.734 - 1
4 -1 91 0.872 = 0.138 + 0.734

To be continued soon.

Comments, questions, feedback, criticisms?

Send feedback