| Making Music with the Basic Stamp | |
|
Getting a Basic Stamp microcontroller module to play a tune should be a simple
thing to do, given that it has an in-built tone generator, but it's not quite
as simple as it first appears.
The fundamental problem is that the Basic Stamp plays Note Numbers using
its SOUND command, which have a linear relationship to frequency; each
consecutive Note Number is a fixed frequency step above the previous one.
Musical instruments, on the other hand, have a logarithmic frequency
characteristic, which is known as, an equal tempered scale. Here, the frequency
of each semi-tone is "one 12th of root 2" ( 1.059463 ) times greater than the
one before it. Because the "Concert A" frequency has been defined as 440Hz, it is a simple
matter to determine what the frequencies are for the other notes available.
Once particular characteristic you will see from the table of frequencies
given below, is that a note has a frequency which is exactly half of the same
named note in the octave above, and, correspondingly, that note is twice the
frequency of the named note in the octave below.
Converting musical notes to a frequency is simple to do, either using a table
as appears here, or mathematically, but we still need to convert the
frequency to a Note Number, which the Basic Stamp will use to produce a note
of the required frequency.
My DIYSTAMP Compiler for the Basic Stamp
includes a PLAY command which takes QBASIC-style PLAY command strings and
converts a tune into the required Note Numbers ( and adds the correct timing for
each note, depending upon playing style and tempo ) but if you don't wish to
use that compiler, you will have to do the conversion by hand, or write a
program to do the conversion for you. The Basic Stamp Programming Manual 1.9 from
Parallax Inc provides the equations which
show the relationship between frequency and Note Number as below -
The Note Numbers are limited in the range 1 to 127, and can produce a note with a frequency between 94.8 Hz and 10.55 KHz respectively. This means that there are some low and high frequency musical notes which the Basic Stamp cannot produce. The linear Note Number to frequency relationship also means that some musical notes cannot be accurately produced by the Basic Stamp. The following tables show the mappings from musical note frequency to Note Number, the frequency that each Note Number actually produces, and the error between the frequency produced and that which is required. Determining which is the closest Note Number produced frequency to that required is not simple, as musical note frequencies are not linearly spaced, but logarithmic. Consider In purely 'closest to the required frequency', Note Number 102 is out by
The frequency gap between In this example the answer is yes ( 20% against 42% closeness to the next note ), although I would be hard pushed to say that the assessment was based upon a legitimate mathematical proof, given the logarithmic nature of the problem. If anyone wants to provide a more mathematically correct way of resolving the problem, I'll be glad to hear it, although I'm unlikely to understand it ! The tables below show the Note Numbers I have determined fit the musical note frequency required and show the raw frequency error of the note produced against that required, and the error percentage, given both as a raw frequency error, and as the amount of error in terms of where the frequency produced falls bewteen that required and the closest note above or below it. As can be seen, there can be quite a significant error between the frequency produced, and how it falls between that and the other note it is closest to. It would tehrefore seem likely that most tunes will sound off-key when played using a Basic Stamp. Musical Note Frequencies (Hz)
Closest Basic Stamp Note Number
Basic Stamp Note Number Frequency (Hz)
Note Number Raw Frequency Error (Hz)
Note Number Raw Frequency Error (%)
Note Number Off Key Error (%)
This table shows how far away from the required frequency a particular Note Number is, with respect to the musical note which is either above or below the note required. It is a good measure as to how 'off key' a required musical note will sound. Tempo and TimingA tune can sound awful when it is played at the wrong tempo, and this is particularly the case with the Basic Stamp.Many musical compositions in western culture use "Common Time" or
Tempo, the speed at which a tune is played, is measured in terms of 'beats per
minute', that indicates ( in It is therefore easy to work out how long a quarter note should last -
60
length of quarter note (seconds) = -----------
tempo (bpm)
Full notes, half-notes, eigth-notes and so on are all multiples, or halvings of the quarter note time. Notes do not usually sound for the full duration of that time, except when they
are played Legato. Normally a note will sound for This means that a note should be played for its expected time of that period, and silence should make up the rest ( which is an unintended pun for the musically inclined ). So called "dotted notes", should have the time they sound for, and any associated silence increased by one half. The problem with the Basic Stamp is that the time a note can be played for, and likewise the length of any period of silence, can only be specified in multiples of 12mS. With a time value of between 1 and 255, the maximum period is just over 3 seconds, which is plenty long enough for most uses; the problem rests with the resolution of 12mS steps of time. A single quarter-note, played Legatto at 150 BPM, should sound for just under half a second; 400mS. Unfortunately, 400mS is not exactly divisible by 12mS, so we have to use a Note Length ( the number of 12mS periods ) of 33 or 34, giving an actual note length of 396mS or 408mS, an error of 1% and 2% respectively. Unless we deal with this, there will be a change to the tempo at which the tune is produced, and a lengthening or shortening of the tune playing time. This can be minimised by selecting higher and lower Note Length values to make the time a note is played and silent for approach the correct note length, and adjustments can be made to keep complete bars running at the correct rate, but it is very difficult when notes shorter than quarter-notes are played at higher tempos. The problem becomes more pronounced when we are using notes which should
sound for There is no simple, or even automated way to deal with the issue of timing, and producing a decent sounding tune will be an art which needs to be acquired. Many tunes may play well without requiring any tweaking, but many more may not. Basic Stamp is a registered trademark of Parallax Inc. QBASIC is a registered trademark of Microsoft Corporation. DIYSTAMP and DIYSTAMP Compiler are trademarks of the Happy Hippy. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
First published on Wednesday the 4th of September, 2002 at 14:27:03
Last upload was on Thursday the 8th of January, 2004 at 14:07:32 |