Making Music with the Basic Stamp



If music be the food of love; bring me a piezo-electric transducer.



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 A#-4, which has a required frequency of 466.164 Hz. We cannot produce this frequency exactly, but can use Note Number 102 or 103 to obtain 460.829 Hz and 479.157 Hz respectively. But which Note Number is closest to that which we want ?

In purely 'closest to the required frequency', Note Number 102 is out by -5.335 Hz, and Note Number 103 is out by +12.993 Hz, so Note Number 102 is the closest.

The frequency gap between A-4 and A#-4 is however less than the gap between A#-4 and B-4; which are gaps of 26.164 Hz and 27.719 Hz. Is the frequency error of 5.335 Hz out of 26.164 Hz better than the frequency error of 12.993 Hz out of 27.719 Hz ?

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)

Octave C-2 to B-2 C-3 to B-3 C-4 to B-4 C-5 to B-5 C-6 to B-6
C
C#
D
D#
E
F
F#
G
G#
A
A#
B
65.406
69.296
73.416
77.782
82.407
87.307
92.499
97.999
103.826
110.000
116.541
123.471
130.813
138.591
146.832
155.563
164.814
174.614
184.997
195.998
207.652
220.000
233.082
246.942
261.626
277.183
293.665
311.127
329.628
349.228
369.994
391.995
415.305
440.000
466.164
493.883
523.251
554.365
587.330
622.254
659.255
698.456
739.989
783.991
830.609
880.000
932.328
987.767
1046.502
1108.730
1174.659
1244.508
1318.510
1396.913
1479.978
1567.982
1661.219
1760.000
1864.655
1975.533
 

Closest Basic Stamp Note Number

Octave C-2 to B-2 C-3 to B-3 C-4 to B-4 C-5 to B-5 C-6 to B-6
C
C#
D
D#
E
F
F#
G
G#
A
A#
B







5
12
19
25
31
36
41
46
51
55
59
63
67
70
73
76
79
82
85
87
89
92
94
96
97
99
101
102
104
105
106
108
109
110
111
112
113
114
114
115
116
117
117
118
118
119
120
120
120
121
121
122
122
 

Basic Stamp Note Number Frequency (Hz)

Octave C-2 to B-2 C-3 to B-3 C-4 to B-4 C-5 to B-5 C-6 to B-6
C
C#
D
D#
E
F
F#
G
G#
A
A#
B







97.838
103.734
110.387
116.809
124.023
130.753
138.255
146.671
156.177
164.718
174.246
184.945
197.044
207.211
218.484
231.054
245.158
261.097
279.252
292.826
307.787
333.333
352.858
374.813
386.847
413.394
443.853
460.829
499.002
520.562
544.070
598.086
629.327
664.011
702.741
746.269
795.545
851.789
851.789
916.590
992.063
1081.081
1081.081
1187.648
1187.648
1317.523
1479.290
1479.290
1479.290
1686.341
1686.341
1960.784
1960.784
 

Note Number Raw Frequency Error (Hz)

Octave C-2 to B-2 C-3 to B-3 C-4 to B-4 C-5 to B-5 C-6 to B-6
C
C#
D
D#
E
F
F#
G
G#
A
A#
B







 -0.161
 -0.092
0.387
0.268
0.552
 -0.060
 -0.336
 -0.162
0.613
 -0.096
 -0.368
 -0.052
1.047
 -0.441
 -1.516
 -2.028
 -1.784
 -0.529
2.069
 -0.839
 -3.340
 -5.268
3.630
 -6.490
 -5.148
 -1.911
3.853
 -5.334
5.119
 -2.689
 -10.296
 -17.529
7.073
4.755
4.284
6.280
11.554
 -35.064
 -28.211
 -15.737
4.297
 -54.439
 -27.649
12.989
 -56.859
 -0.987
 -79.390
 -0.688
 -88.692
25.122
 -73.659
-178.314
 -14.749
 

Note Number Raw Frequency Error (%)

Octave C-2 to B-2 C-3 to B-3 C-4 to B-4 C-5 to B-5 C-6 to B-6
C
C#
D
D#
E
F
F#
G
G#
A
A#
B







 -0.164
 -0.088
0.352
0.230
0.447
 -0.046
 -0.243
 -0.110
0.394
 -0.058
 -0.211
 -0.028
0.534
 -0.213
 -0.689
 -0.870
 -0.722
 -0.202
0.746
 -0.286
 -1.074
 -1.598
1.039
 -1.754
 -1.313
 -0.460
0.876
 -1.144
1.036
 -0.514
 -1.857
 -2.985
1.137
0.721
0.613
0.849
1.474
 -4.222
 -3.206
 -1.688
0.435
 -5.202
 -2.494
1.106
 -4.569
 -0.075
 -5.683
 -0.046
 -5.656
1.512
 -4.185
 -9.563
 -0.747
 

Note Number Off Key Error (%)

Octave C-2 to B-2 C-3 to B-3 C-4 to B-4 C-5 to B-5 C-6 to B-6
C
C#
D
D#
E
F
F#
G
G#
A
A#
B







 -2.690
 -1.464
6.297
4.020
8.137
 -0.761
 -3.918
 -1.819
7.101
 -0.973
 -3.421
 -0.468
9.866
 -3.451
 -10.387
 -12.766
 -10.831
 -3.288
14.355
 -4.584
 -15.293
 -21.184
21.183
 -22.780
 -18.091
 -7.181
17.268
 -16.138
21.109
 -7.955
 -23.799
 -33.418
23.632
13.806
11.502
16.648
32.951
 -41.518
 -35.028
 -22.110
7.893
 -46.662
 -29.547
22.845
 -43.450
 -1.243
 -48.869
 -0.775
 -48.751
34.106
 -41.309
 -61.659
 -11.155

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 Timing

A 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 "4/4 time", which means four beats to the bar, four quarter-notes ( crotchets ) to a bar. There are many other possibilities ( in particular 3/4 time ), but we will concentrate on 4/4 time, as the first number in an x/y time signature is simply the number of beats in a bar, for purposes of defining loudness accentuation and so on, which the Basic Stamp has no control over.

Tempo, the speed at which a tune is played, is measured in terms of 'beats per minute', that indicates ( in x/4 time ), how many quarter-notes there are in a minute minute.

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 7/8th's of that time, and 3/4 of that time if Staccato.

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 7/8th's and be silent for 1/8th of a notes total length.

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.





Associated Articles

  Build Your Own Basic Stamp
  The PICAXE Processors



Sites to Visit

  Parallax Inc
  The Equal Tempered Scale



Site Navigation

  Home Page
  What's New
  Search
  Add Bookmark
  Have Your Say
  Guestbook




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