Click here to Skip to main content
15,881,852 members
Articles / Programming Languages / C#
Article

A Simple Beep - Reminiscing about ZX Spectrum audio

Rate me:
Please Sign up or sign in to vote.
4.57/5 (19 votes)
21 May 2004CPOL4 min read 117.9K   1.9K   25   21
Driving the PC Speaker from .NET.

Sample Image - SongBuilder.png

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.

C#
[DllImport("kernel32.dll")]
private static extern bool Beep(uint dwFreq, uint dwDuration);

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.

An example of a file with the the Scale of C Major.

XML
<Song Tempo="60">
  <Notes>
    <Note Duration="Quarter" Pitch="C" Octave="4" />
    <Note Duration="Quarter" Pitch="D" Octave="4" />
    <Note Duration="Quarter" Pitch="E" Octave="4" />
    <Note Duration="Quarter" Pitch="F" Octave="4" />
    <Note Duration="Quarter" Pitch="G" Octave="4" />
    <Note Duration="Quarter" Pitch="A" Octave="4" />
    <Note Duration="Quarter" Pitch="B" Octave="4" />
    <Note Duration="Quarter" Pitch="C" Octave="5" />
  </Notes>
</Song>

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:

History

  • Version 1.0: A small bit of fun.
  • Version 1.01: Some bug fixes and NDoc help file included.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
Scotland Scotland
Have been a Code Project MVP 5 years running and was Microsoft C# MVP 4 years running, MBCS, MIAP and a whole bunch of other stuff. Now I just help run Scottish Developers which is a user group with software development events in Edinburgh, Glasgow and Dundee and I have also started an open source project to help with Password Validation

Main topics I blog about:
* Parallelization in .NET
* Code Quality
* Data Security

Comments and Discussions

 
GeneralMy vote of 5 Pin
Zimriel16-Dec-12 13:25
Zimriel16-Dec-12 13:25 
QuestionTRS-80 next? Pin
Zimriel11-Dec-12 16:08
Zimriel11-Dec-12 16:08 
Generalnot playing in external player like windows media player Pin
sunil15018728-Mar-10 21:11
sunil15018728-Mar-10 21:11 
Generalanable to play saved file out side the application even though we change its format to mp3/.wav.. [modified] Pin
sunil15018726-Mar-10 0:36
sunil15018726-Mar-10 0:36 
GeneralRe: anable to play saved file out side the application even though we change its format to mp3/.wav.. Pin
Colin Angus Mackay26-Mar-10 3:28
Colin Angus Mackay26-Mar-10 3:28 
GeneralRe: anable to play saved file out side the application even though we change its format to mp3/.wav.. Pin
sunil15018728-Mar-10 19:04
sunil15018728-Mar-10 19:04 
GeneralTest Complete Pin
Kyudos8-Sep-06 5:44
Kyudos8-Sep-06 5:44 
GeneralRe: Test Complete Pin
Kyudos8-Sep-06 5:45
Kyudos8-Sep-06 5:45 
QuestionVolume control? Pin
mikker_1231-Aug-06 16:15
mikker_1231-Aug-06 16:15 
Anyone has idea how to control beeper volume? Throught BIOS?

Tnx in advance
QuestionStop ? Pin
Tommi G12-Jun-06 1:55
Tommi G12-Jun-06 1:55 
AnswerRe: Stop ? Pin
Colin Angus Mackay12-Jun-06 8:15
Colin Angus Mackay12-Jun-06 8:15 
General[Message Deleted] Pin
zurbulu8-May-06 2:22
zurbulu8-May-06 2:22 
GeneralRe: Twins? Pin
Colin Angus Mackay8-May-06 2:37
Colin Angus Mackay8-May-06 2:37 
GeneralAwwwww Pin
Michael Starr19-Oct-04 11:27
Michael Starr19-Oct-04 11:27 
GeneralRe: Awwwww Pin
Colin Angus Mackay19-Oct-04 11:34
Colin Angus Mackay19-Oct-04 11:34 
Generalbrings back the memories... Pin
Jeremy Falcon1-Jun-04 16:10
professionalJeremy Falcon1-Jun-04 16:10 
General.net compact framework Pin
richie k28-May-04 0:09
richie k28-May-04 0:09 
GeneralRe: .net compact framework Pin
Colin Angus Mackay28-May-04 1:22
Colin Angus Mackay28-May-04 1:22 
GeneralRe: What amazes me... Pin
Colin Angus Mackay22-May-04 10:44
Colin Angus Mackay22-May-04 10:44 
GeneralLooks like.... Pin
peterchen22-May-04 1:17
peterchen22-May-04 1:17 
GeneralRe: Looks like.... Pin
Colin Angus Mackay22-May-04 2:32
Colin Angus Mackay22-May-04 2:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.