Introduction

While I was looking around on PInvoke.net[^] for a particular API, I noticed a function called Beep, and I had a sudden wave of nostalgia about when I first started programming on a ZX Spectrum back in 1983.

What I decided to do was to create a program that uses the Beep API function and plays music in a similar way to the ZX Spectrum. However, due to the power of the .NET Framework, I decided to have some more fun with XML Serialization at the same time.

The aim of this article is just for fun, which explains the bad jokes throughout. I'm not trying to explain XML Serialization (although you might pick up some ideas if you are new to it) or the Beep function, especially as there is not very much to it.

Beep

Firstly, the Beep API function will not play beautiful music on Windows 95, 98 or ME, it will just beep monotonously for a preset amount of time or, if there is a sound card, it will play the default sound. That's it. ¡Nada más! If you are running any of these operating systems, the parameters are ignored, and this article won't be of any use, and can safely be printed out, put through a shredder, and used as bedding for your pet hampster.

The function won't beep in the background as it operates synchronously, just like my old ZX Spectrum. So, if you call it and set the duration to 30,000 milliseconds then you will have to wait for half a minute for the application to respond again.

It will output onto the PC Speaker a range of frequencies from 37Hz to 32767Hz, so you might be able to write an application for calling your pet dog by using some of the upper frequencies. Unfortunately, my pet dog died almost 3 years ago, so I cannot test this hypothesis. He was almost 15 at the time and had been deaf for quite a while, so I suppose even if he was still alive now, I wouldn't have been able to test that.

The Beep method is not currently available in .NET, however it is supposed to be available with .NET 2.0. In the mean time, to use the API function, a little bit of P/Invoking is required.

Representation of a Song

The sample application is quite simple. It opens an XML file that contains a song. The schema is quite simple, and it should be quite easy for anyone with a rudimentary knowledge of music to create a file with a song in it.

The Song element contains an attribute defining the Tempo in beats per minute. A quarter note is equal to one beat.

The Song contains a Notes element, which in turn contains each of the individual Note elements. Each Note defines the length and pitch, although the pitch is actually calculated by using the Pitch and Octave attributes.

An interesting thing about musical notes is that for each octave increase, the frequency doubles. This means that the enumeration that holds all the frequencies only needs to do so for one octave. In this case, the frequencies for octave 7 are used because then the program can divide to get to the correct frequency, and there is no strained notes due to rounding errors being multiplied up.

The User Interface

For the purposes of an easy demonstration, I have put together a simple user interface in order to allow you to create your own songs. You can use the tool bar buttons or the menu to create your masterpieces.

The File menu is the standard open/save/exit combination. The program takes files with a .song extension. Actually, it will accept anything so long as the contents match the expected XML schema.

The Note menu allows you to define each note, and the sequence of notes.

The last menu, the Play menu, permits you to play the sound. Remember that it will come from the PC Speaker and that you won't be able to interact with the application while it is running.

At the bottom of the window is a slider that adjusts so you can set the tempo of the song. The range is 30 beats per minute on the left to 180 beats per minute on the right.

Finally, in the centre of the window is the list of notes that are played. In the first column is the duration, and the second column contains the pitch.

There are shortcut keys to help you operate the application faster and these can be found on the menus.

More Information

Class documentation is available in the source zip. This was generated using NDoc[^] 1.3 beta 1.

Some sample song files are available in the demo zip. These include the scale of C Major and Ode to Joy.

For more details about the elements of this project, the following links may be useful:

Is there any chance that i can play them in the windows media player or any external audio device after saving the file. or is there any chance of saving ".SONG" format to any .wav or .mp3 format...if it is how?

"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."
--Charles Babbage (1791-1871)My: Website | Blog

You beat me to it. . . I was doing a Google search to find out what the ranges were so I could have some fun and I found this. It's OK though; if I build a bigger, better application for making music I'll be sure to make it available. By the way, I'm probably going to steal all your code.

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
"The ultimate metric that I would like to propose for user friendliness is quite simple: if this system was a person, how long would it take before you punched it in the nose?"
- Tom Carey

Unfortunately this won't work with the .NET Compact Framework because there is no support for the beep function, nor in the Pocket PC SDK either. The closest I found was to use the sndPlaySound API function that you would have to invoke from your application.

In Glorious Gadgets by John Kennedy, Microsoft Corporation (October 28, 2002)[^] he states:
"If you remember, the key to using the .NET Compact Framework on the Pocket PC is the use of the Visual C#, or Visual Basic .NET, languages. These languages produce managed code and provide access to the .NET Compact Framework classes that look after everything from file manipulation to XML Web services. However, they don't provide support for everything. For example, making your Pocket PC go beep.

"Using the eMbedded Visual Tool C++ language, you would make the beep noise by calling the API sndPlaySound. This API takes a WAV filename and an integer for various options, and triggers the sound playback. Unfortunately, there is no .NET Compact Framework class provided for that, and so instead, it's necessary to use a technical called platform invoke."

richie k wrote:Can you only play one note at a time?

Back to the full .NET Framework, yes, you can only play one note at a time. This is a very simple function.

richie k wrote:If not, what is the best way to play, as an example, the notes C, D, E at the same time?

I guess one way (not necessarily the best way) would be to create a wave file in memory and you would have to calculate the appropriate wave patterns (which is simple high school maths). The link to Michigan Tech in the article will give you details of the frequencies you need.

I hope this is some help to you.

"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar