|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionWhile I was looking around on PInvoke.net[^] for a particular API, I noticed a function called What I decided to do was to create a program that uses the 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 BeepFirstly, the 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 [DllImport("kernel32.dll")]
private static extern bool Beep(uint dwFreq, uint dwDuration);
Representation of a SongThe 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. <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 The 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 InterfaceFor 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 InformationClass 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
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||