Professional Documents
Culture Documents
MUSIC
BOOK
A Guide to Programming
Music and Sound
James Vogel and Nevin B. Scrimshaw
/ s^
James Vogel
and
Nevin B. Scrimshaw
Birkhduser
Vogel, James
The Commodore 64 Music Book.
Bibliography: p.
1. Musical instruments, Electronic-Instruction and study. 2. Commodore 64
(Computer) - Programming. 3. Basic (Computer program language) I. Scrim
shaw, Nevin, 1950-. II. Title.
MT723.V631983 789.9'9 83-15634
ISBN 0-8176-3158-5
ABCDEFGHIJ
ISBN 0-8176-3158-5
Printed in USA
Contents
Foreword ™-vi»
Section I
Introduction: An Overview of Electronic Music Synthesis and the SID Chip. 2
1 Sound and Vibration 3
2 Frequency and Pitch 5
3 Waveforms 7
4 The Envelope Generator -Attack/Decay/Sustain/Release (ADSR) 10
5 Understanding Filters 13
6 Ring Modulation, Synchronization, Timbre Modulation 16
Section II
Introduction to Section II 20
7 Understanding the Power of the POKE Statement 22
8 Programming Pitch Controls 26
9 Advanced Pitch Control Techniques 28
10 Controlling Waveforms 30
11 Programming the ADSR Controls 34
12 Programming Volume Controls 38
13 Programming Note Duration 41
Section HI
Programming Music Using the READ/DATA Format 42
14 READ/DATA Format 43
15 Setting Up the DATA Bank 46
16 Duration Control Using the READ/DATA Format 48
17 The RESTORE Statement 51
18 Using Counters 53
19 Programming A Song For One Voice Using READ/DATA Format 55
20 DATA Manipulation Using Flags, Counter, and RESTORE Statement 58
21 Shortcuts and Abbreviations 63
22 Multiple Voice Programming Using the READ/DATA Format 66
Section IV
Sound Effects, Ring Modulation, Synchronization, Filters and
Frequency Modulation 74
23 Phaser and Siren Sound Effects Using the STEP Command 75
24 The White Noise Effect; Blast Off and Locomotive 79
25 Sound and Color 81
26 Using the Random Number Function to Create Effects 85
27 Ring Modulator and Synchronization Effects 88
28 Programming Filters 92
29 Frequency Modulation 95
Section V
Programming Music Using the Array Format 100
30 Using Subscripted Variables, Simple Arrays, and the DIM Statement 101
31 Using the Two-Dimensional Array Format for One Voice 104
32 Creating Two Characters With One Voice 108
33 Controlling Multiple Voices With Independent Duration 111
34 Real Time Music: Programming An Organ Keyboard 118
Section VI
Sounding A Final Note 126
Bibliography 127
Sid Chip Control Chart 128
Table of Note Values 129
Keyboard ASCII Codes 130
Foreword
Acknowledgements
60 READ A,B,C,D,E,F,G
150 GOTO 60
800 DATA-1,-1,-1,-1,-1,-1,-1
If you are using your TV as a monitor, chances are that you are
also using it for sound. If you want to hear a richer, clearer sound,
you can connect the audio output located on the back of your com
puter to your stereo or to an instrument amplifier. This is easily
accomplished.
2 Frequency and Pitch
20 POKE 54296,15
40 POKE 54276,17
90 GOTO 20
193,50,60
910 END
10 FOR I = 0 TO 7
20 POKE 54296,15
40 POKE 54276,17
60 A = A/2 I I
80 IF TI < TM + 10 THEN 80
90 TM = TI
110 GOTO 40
38539,36376,34334
800 DATA -1
900 RESTORE
920 END
3 Waveforms
Generating a wide range of pitches is not the only job that the three
oscillators perform. They also produce four different waveforms at each
of the desired pitches. Each waveform has its own unique tone quality
or color. We call this tone coloring "timbre," and it is one of the simplest
and most powerful sound controls with which you can work. It is the
difference in timbre or tone coloration that enables you to distinguish
an instrument like the trumpet with its brassy sound from the warm tone
of a violin. Timbre is largely the result of waveshape.
Before we go any further with our discussion of waveforms, perhaps
you would like to hear them. Try typing in this program on your 64.
It's a brief passage from "The Flight of the Bumble Bee" by Rimsky
Korsakoff. The program is designed to play the passage four times. The
first time through, it will demonstrate the Sawtooth waveform; the sec
ond time, the Triangle waveform; the third time, the Pulse waveform;
and the final time through, it will demonstrate the "White Noise"
waveform.
30 POKE 54296,15
60 READH,L
90 POKE 54276,WF(I)
100 FOR T = 1 TO 50 : NEXT
110 GOTO 60
920 GOTO 60
As you can hear, each waveform has its own distinct tone quality.
This is because the shape of the wave governs the number and amplitude
(volume) of the partials, or harmonics, that you hear. If you would like
to explore this further, here is a more detailed explanation of waveforms,
but don't get discouraged if you don't immediately grasp it. Remember,
the SID is doing all this hard work for you. All you will have to do
is type in a few numbers.
Each pitched soundwave vibrates at a fundamental frequency. This
fundamental harmonic, as it is known, is a sine wave. It looks like this
on an oscilloscope:
The fundamental is the actual pitch you hear. In addition to this fun
damental, other sine waves are generated. The second harmonic vibrates
at twice the frequency of the fundamental, while the third vibrates at
three times the frequency. Theoretically, there are an infinite number
of harmonics. The reason why you don't hear them is because each har
monic is progressively quieter.
In the Sawtooth waveform, all the harmonics are present. Each har
monic, however, grows progressively quieter in a ratio that is equal to
the reciprocal of the harmonic number. Thus, the second harmonic is
one-half as loud as the fundamental, while the third harmonic is one-
third as loud. The Sawtooth waveform gets its name from its shape,
which looks like this:
8
The Triangle waveform contains only the odd harmonics in an
amplitude ratio equal to the reciprocal of the square of its number. Thus,
harmonic two would not be present, while harmonic three would be one-
ninth as loud as the fundamental. Similarly, harmonic four would not
be present, while harmonic five would be one-twenty-fifth as loud as
the fundamental. The Triangle waveform looks like this:
The Pulse waveform is a rectangular shape. You can program the width
of the pulse. Each rectangular wave that you program will exhibit a dif
ferent harmonic content. That makes this your most variable sounding
waveform. The Pulse waveform will look something like this, depend
ing on your pulse width:
You can also create a square waveform with this setting. A square wave
will contain only odd harmonics at an amplitude ratio equal to the
reciprocal of the harmonic number. Thus, the third harmonic would be
one-third as loud as the first, while the fifth would be one-fifth as loud.
The final waveform selection is "White Noise," which is a random
generation of all frequencies at equal energy. This waveform is especially
useful for percussive sounds, as well as for a wide variety of sound effects.
As you can see, each waveform generated a different range of har
monics. It is this difference that provides you with a wide variety of
tone colors. Once again, the SID will do all the work in generating these
waveforms. You just tell it which one you'd like. We'll show you how
this is done in the chapter entitled, "Controlling Waveforms."
4 The Envelope Generator
Attack/ Decay/ Sustain/
Release (ADSR)
10
phase, which is the RELEASE. The RELEASE controls how quickly
the volume will drop from the SUSTAIN level back down to zero.
The ADSR is a truly powerful control. It plays a major role in creating
sounds that resemble acoustical instruments, as well as in creating wholly
original sounds. This is because each instrument has its own unique
envelope.
Consider a piano. It has a very explosive ATTACK, rising rapidly
to its peak volume. The sound then drops or DECAYs over a relatively
long period. When you remove your finger from the key, the note is
RELEASEd, and the sound quickly drops to zero.
A stringed instrument, such as a cello, can have several different kinds
of envelopes, depending on how it is played. If it is bowed, the ATTACK
will be rather gradual. According to the dynamics of the particular stroke,
the DECAY can be rather slight, followed by a long SUSTAINing period.
When the bow is removed, the note is RELEASEd and gradually fades
away. If the note is plucked, however, the ATTACK will be quick and
forceful. Then it will DECAY over a long period. This is a similar
envelope to those for all plucked instruments.
Some instruments, such as the organ, have an instantaneous ATTACK
with absolutely no DECAY. Instead, the SUSTAIN holds the note at
peak volume until RELEASE. The RELEASE, like the ATTACK, is
then instantaneous.
There are 16 possible settings for each phase of the envelope, so you
can create just about any configuration you can imagine. We'll show
you how in the chapter, "Programming the ADSR Controls." If you're
curious, try typing in this program. It will demonstrate four separate
and unique envelopes using the same waveforms and pitches.
60 READ H,L,DR
70 IF H<0THEN 900
90 POKE 54276,17
~~ fl
120 GOTO 50
12
5 Understanding Filters
13
sound. This is called Subtractive Synthesis.
To use the low-pass filter, you first choose a cut-off frequency. The
filter will allow all the harmonics below the cut-off to pass unaltered.
All the harmonics above the cut-off will be attenuated (decreased) pro
gressively. This is one of the most commonly used filters because it pro
duces a brassy-sounding timbre for pitched notes.
The high-pass filter is the reverse of the low pass-filter. It allows all
the harmonics above the cut-off frequency to pass unaltered, and at
tenuates all those that fall below. This filter is not especially good for
pitched tones because it filters out the fundamental and most of the lower
harmonics. It is very useful when used with the 'White Nosie" waveform,
producing a wide variety of sounds.
The third filter is the band-pass filter. It allows only a narrow band
of frequencies around the cut-off point. All those above and below the
band will be attenuated.
You can obtain the opposite effect of a band-pass filter by combin
ing a high and a low filter. This is known as a band reject filter. It will
pass all frequencies above and below the cut-off, and reject those at the
cut-off. This is a very specialized filter.
You can further control these filters with a Resonance control. This
control emphasizes the frequencies around the cut-off frequency selected
for the filter.
One final word about the SID filter concerns the rate of attentua-
tion. Each filter is permanently set to attenuate the frequencies at a given
rate. This rate is measured in decibels (dB) per octave. A decibel is a
relative measurement, a ratio between a fixed sound and another sound.
It is not an absolute quantity. It has been determined that the smallest
difference in loudness between two pitches that the human ear can detect
is three dB. It has also been determined that a three dB increase cor
responds to a doubling of the sound's intensity.
The SID's low-pass and high-pass filters are set to attenuate frequencies
at 12 dB per octave. The band-pass filter attenuates frequencies at six dB.
To get a brief demonstration of the power of these filters, try out
this program. It will produce the same tone filtered through a low-pass
filter, a high-pass filter, and finally, a band-pass filter.
14
30 F(0) = 15 : F(l) = 31 : F(2) = 47 : F(3) = 79
140 GOTO 60
15
6 Ring Modulation,
Synchronization,
Timbre Modulation
The SID offers you three additional ways to alter your sound, other
than those discussed in previous chapters.
The first is the Ring Modulator. It's one of the easiest controls to use,
and produces a wide array of crazy and funny sounds. When set, the
Ring Modulator multiplies the the output of two oscillators, producing
the sum and difference of the harmonics. Here is what it sounds like.
20 POKE 54296,15
40 READH,L,DR
70 POKE 54276,21
80 FOR T = 1 TO DR : NEXT
90 POKE 54276,20
95 FOR B = 1 TO 34 : NEXT
100 GOTO 30
16
250 DATA-1,-1,-1
900 RESTORE : I = I + 1 : IF I = 2 THEN POKE 54296,0 : END
910 GOTO 30
10 FOR I = 0 TO 7
20 POKE 54296,15
40 POKE 54276,19
60 A = A/2 I
80 IFTKTM + 10THEN80
90 TM = TI
110 GOTO 40
200 DATA 64814,61176,57743,54502,51443,48556,45830,43258,40830,38539,
36376,34334
800 DATA -1
900 RESTORE
910 NEXT I : FOR T = 1 TO 200 : NEXT : POKE 54296,0
920 END
We'll show you how to set these controls in the chapter called, "Ring
Modulator and Synchronization Effects."
Finally, some of the most dramatic sound colors can be produced
by a special function that allows you to modulate a sound or a series
of sounds of a voice using the output of the oscillator and Envelope
Generator of voice three. This modulation can produce complex sound
effects, after the manner of a Phaser of a Wah-wah pedal. We'll
describe how to do this in Chapter 29, "Frequency Modulation."
17
SECTION II
Introduction to Section II
Programming Sound Controls
Now that you have an overview of the actual process the SID goes
through to produce a sound, you're ready to learn how to program its
controls. Let's jump right in and construct a short sound program.
20
60 closes the gate, which signals the RELEASE cycle of the ADSR and
the end of the note.
OSCI
Amplitude Master Audio
Modulator Volume Out
Waveform
21
7 Understanding the Power
of the POKE Statement
The first number, 100, is the line number of your program. This is
followed by POKE 54277, which instructs the computer to go to con
trol register number 54277. This number is known as an address. It is
the location of a particular byte, in this case, the byte that controls the
ATTACK/DECAY setting for Voice 1.
The SID chip contains 29 of these control registers or bytes. If 29
controls sound like a lot to handle, take heart. The first seven controls
operate Voice 1. The next two sets of seven are duplicates of the first.
They are used to control Voices 2 and 3. So when you know the first
seven, you've already got a handle on 21. The remaining eight bytes con
trol various filter settings and Analog/Digital TA/D converters for in
terfacing the SID with external audio signals. We've provided you with
a handy chart of "SID Chip Control Registers" at the end of this chapter.
22~ ~
After POKEing the address of the desired control, enter a comma,
followed by a number from 0-255 inclusive. This number represents the
value that you are instructing the computer to enter onto the byte. By
entering this value, you are, in effect, directly programming each of the
eight bits contained on the byte. But, "why a number between 0 and
255?" you may ask. The reason is, we are using a programming language
called BASIC.
The purpose of this language is to act as an intermediary between
human language and the computer's language. We use a number system
built around 10, the decimal system. The computer uses a system built
around two, the binary system. We have 10 characters (0-9) with which
to build our numbers, while the computer has two (0,1). The computer
uses the 0 to turn off a bit and uses a 1 to turn on a bit. When we tell
the computer to POKE 54277,75, it will convert our 75 into binary, and
use this number as a code to turn on and off the bits located at the byte
with the address 54277. It would look like this:
m
en
<
| w W
u
w
<
Q Q Q Q
0 1 0 0 1 0 1 1
r- vo o
S s s s
That's because the decimal number 75 is the same as the binary number
01001011. In this manner, our statement POKE 54277,75 turns on
DECAY bits 0, 1, 3 and ATTACK bit 6, giving us a combination of
three DECAY settings and one ATTACK setting. So you see, our number
75 does a lot of work.
It's very easy to learn how to make this conversion, so don't be alarmed.
You don't have to be a math whiz: all you do is add. Follow this simple
procedure, and you'll be able to attain control of your sound settings
in no time.
23
128 64 32 16 8 4 2 1
0 1 0 0 1 0 1 1
m o
•*-»
s n PQ PQ PQ s s
The chart above represents our control register. If you want to turn on
a bit, just use the decimal number above the bit when entering your value.
If you want more than one bit, just add. The computer will do the con
version for you. We used the number 75. That is made up of 64 + 8
+ 2 + 1. If we wanted 76, then our register would look like this:
128 64 32 16 8 4 2 1
0 1 0 0 1 1 0 0
SO •* m o
<-•
a n a oa PQ PQ PQ a
64 + 8 + 4 = 76
So, why 0-255 inclusive? The reason is, you can encode combinations
of these base two place values to obtain any number between 0 and 255
inclusive. Zero is the smallest (0 + 0 + 0 + 0 + 0+.0 + 0 + 0 =
0). Two hundred fifty five is the largest because 1 + 2 + 4 + 8 +
16 + 32 + 64 + 128 = 255. If you count the 0, that gives you 256
possible ways to program the eight bits. This gives you a lot of control
for your sound settings, and it's not that hard to manage.
Here is a chart to aid you in this process. It shows the address number,
and function of each control, and the value located on each bit.
24
SID CHIP CONTROL CHART
BIT no. 7 6 5 4 3 2 1 0
0 54272 LOFQ f 7 f6 f5 f4 f3 f2 f 1 fO
i 54273 HIFQ f 15 f 14 f 13 f 12 f 11 no f9 f8
2 54274 LOPW pw 7 pw 6 pw 5 pw 4 pw 3 pw 2 pw 1 pwO
VOICE 2
" 54283 WAVE FORM NOISE run AM Z\ TEST RMOD SYNC GATE
12 54284 ATK-DEC ATK3 ATK2 ATK 1 ATK0 DEC 3 DEC 2 DEC 1 DEC0
13 54285 SUS-REL SUS3 SUS2 SUS 1 SUSO REL3 REL2 REL 1 RELO
VOICE 3
14 54286 LOFQ f7 f6 f5 f4 f3 f2 f 1 fO
IS 54287 HIFQ f 15 f 14 f 13 f 12 f 11 f 10 f9 f8
16 54288 LOPW pw7 pw6 pw 5 pw 4 pw 3 pw 2 pw 1 pwO
19 54291 ATK-DEC ATK3 ATK2 ATK 1 ATK0 DEC 3 DEC 2 DEC1 DEC0
20 54292 SUS-REL SUS3 SUS2 SUS 1 SUSO REL3 REL2 REL 1 RELO
21 54293 LOFC ///// ///// ///// ///// ///// fc2 fcl fcO
23 54295 RES-FILT RES 3 RES 2 RES 1 RESO FILTEX FILT3 FILT2 FILT1
25 54297 POT 1 7 6 5 4 3 2 1 0
26 54298 POT 2 7 6 5 4 3 • 2 1 0
27 54299 OSC3 07 06 05 04 03 02 01 00
28 54300 ENV3 E7 E6 E5 E4 E3 E2 E 1 E0
25
8 Programming Pitch Controls
26
For now, however, you may want to practice using the Table of Note
Values. Here is a program that will make it easy. It is designed around
an INPUT statement. The program will ask you to enter a Hi Frequen
cy number and a Low Frequency number. Select a pitch from the Table,
and enter the Hi and Low values. The program will automatically enter
these values into Line 100, which POKEs the High and Low Frequency
control registers for Voice 1 with your chosen pitch. You will then hear
your note played by the computer. Try a few.
60 IF A = 0 THEN END
80 POKE 54296,15
90 POKE 54278,240
140 GOTO 10
27
9 Advanced Pitch Control
Techniques
There may be times when you want to program a pitch that does not
appear on the Table of Note Values. To do this, you must use a for
mula. This formula was used to compile the Table.
The first step is to convert the actual frequency of the pitch (FA) to
a frequency number (FQ). The formula for this is
FA = .06097 = FQ.
28
Thus, 7282-(256*28) = 114 LF = 114.
29
10 Controlling Waveforms
Bit No. 7 6 5 4 3 2 1 0
30
portant to set thfe ADSR controls into your program ahead of the
waveform.
Let's return to our original sound program.
10 POKE 54296,15
20 POKE 54277,0 : POKE 54278,128
40 POKE 54276,17
60 POKE 54276,16
As you can see, we gated (turned on) the waveform in Line 40, after
we had programmed the information for the ADSR in Line 20 and the
Pitch in Line 30. Gating a waveform without this information is like
opening your mouth and having no sound come out—you've got to tell
your vocal chords to vibrate a message first. Line 50 sets the duration
of the sound. Line 60 stops the sound (closes the gate) which begins the
release of the envelope.
The Triangle waveform produces a mellow, round sound. When used
with the proper envelope, it is useful for producing sounds similar to
those of a flute, an organ, or a French horn.
The Sawtooth waveform is much brighter and edgier, useful for brass
and reeds.
The White Noise gives you a hissing sound, which can be refined to
produce all kinds of sound effects such as explosions, hand claps, snare
drums and rocket engines. The Pulse waveform is the most varied sound,
because one can alter its harmonic content by controlling the pulse length.
This also makes it the most difficult to program.
31
osc=
128 64 32 16 8 4 2 1
54274 PW7 PW6 PW5 PW4 PW3 PW2 PW1 PWO LPWO
The High Pulse number must be number (0-15), while the Low Pulse
number must be (0-255). The easiest way to understand these settings
is through experimentation. For this reason, we have supplied you with
the following program. It is similar to the program we used for explor
ing the High and Low Frequency numbers.
We'll get you started by telling you that a setting of 0 for the Low
Pulse width number and 8 for the High Pulse width number will create
a square wave.
20 PRINT CHR$(147)
60 IF A = - 1 THEN END
90 POKE 54296,15
160 GOTO 20
32
line containing the POKE statement with the addresses 54276, 54283
or 54290. Just change the waveform control number. Remember to
POKE a High and Low Pulse Width command if you want to use
the Pulse waveform.
If you look at the Sid Chip Control Chart for the waveform control
register you will notice that it also contains bits for the Ring Modula
tion and Synchronization functions. These functions work with the
waveforms. We'll show you how they work in a later chapter devoted
to Ring Modulation and Synchronization.
The Test bit has a special function. When turned on, it resets the
oscillator. You probably won't have to use this function if you follow
one rule. TAKE CARE when adding waveforms. It is possible to add
two waveform settings together. However, if you add a waveform while
the white noise waveform is on, the output of the noise waveform may
lock up. If this happens, the bit must be reset or it will remain silent.
33
11 Programming the ADSR
Controls
The Envelope Generator of the SID is one of the most powerful and
unique controls. Each voice has its own Envelope Generator which is
located on two bytes. The first byte contains the ATTACK/DECAY
function while the second contains the SUSTAIN/RELEASE. If you
examine the SID Chip Control Chart, it will reveal that the address for
Voice 1 ATTACK/DECAY register is 54277. The address for the SUS
TAIN/RELEASE is 54278. For Voice 2, the register numbers are 54284
and 54285. For Voice 3, the registers numbers are 54291 and 54292.
Reg. #54277
#52478
Bit. No. 7 6 5 4 3 2 1 0
ATTACK Settings
34
rapidly a note will rise from zero to its peak volume. A setting of 0 will
result in the volume rising instantaneously to its peak volume. This is
like an organ. As soon as you press the key, you get the maximum sound.
A setting of 240 will give you a very slow and gradual rise to peak
volume, while a setting of 128 will provide an intermediate rise.
The ATTACK cycle begins when the waveform gate is opened.
DECAY Settings
When you have selected your ATTACK and DECAY settings, you
must then add them together and encode the sum as one number. For
example, let's say you have selected a fast ATTACK of 16 and a medium
DECAY of 5. You would then add them together; 164-5 = 31. They
would then be inserted into the program with this statement.
POKE 54277,31
SUSTAIN Settings
RELEASE Settings
" 35
level to zero at a rate defined by the RELEASE setting. These settings
are built around 0, 1,2, 4, 8, again giving you a choice of 0 to 15. A
RELEASE Settings
POKE 54278,137
The real trick to using the ADSR is learning how the ATTACK/
DECAY/SUSTAIN/RELEASE all work together. Try to envision the
envelope you want to create. Think about how the sound goes through
its stages. Then try to create these stages using the settings. Often, drawing
a diagram will be helpful in plotting out your sound. Here is a possible
diagram for an organ-like envelope.
SUSTAIN
ATTACK RELEASE
36
Here is one for a harp.
ATTACK \ RELEASE
Combined with the proper waveform, pitch range and sometimes with
the help of filters, the ADSR plays a big role in the total sound produced.
One final reminder on the ADSR. Always place the settings toward
the beginning of your program before the waveform control settings.
This way, when you open the gate and trigger the ADSR, the computer
already has the envelope in its memory.
If you would like to practice some ADSR settings, you can use the
"Four Envelopes" program in Chapter 4. This will allow you to pro
gram four groups of settings each time you run the program. Line 20
of that program controls the ATTACK/DECAY and Line 30 controls
the SUSTAIN/RELEASE.
By changing the values to the right of the equal sign, you can enter
your new settings into an Array. Try it out.
37
12 Programming Volume
r Controls
Of all the SID's sound controls, the volume is perhaps the easiest
to control. It is located on Register 54296. It shares this byte with the
filter mode.
Reg. #54296
The volume is located on the first four bits. You can therefore pro
gram it with a number 0 to 15, with 15 being the loudest settings and
0 being the lowest. Set the volume at the beginning of your program.
POKE 54296,15
38
13 Programming Note Duration
There are a number of methods that you may employ to control the
duration of a note. The simplest method is to use a time loop, as we
did in our sample sound program at the beginning of this Section. The
time loop is created by a FOR/NEXT statement placed between the com
mands that open and close the waveform gate.
POKE 54276,17
POKE 54276,16
The time loop instructs the computer to open the gate, wait for a time
and then close the gate. You can lengthen the note by increasing the
number within the loop. Thus FOR DR = 1 to 1000 would be twice
as long as our original setting. Similarly, you can shorten the duration
by decreasing the number within the loop. FOR DR = 1 TO 250 would
produce a note with half a duration.
By replacing the second number of the loop with a variable in a
READ statement, and placing the value in a DATA statement, you
can control the length of each individual note. We used this technique
in several of our sample programs. We'll teach you how to use this
method in the chapter devoted to Programming a Song for One Voice.
Another easy method to control the duration of a note is to utilize
the computer's "jiffy clock" which reads an interval timer. The "jiffy
clock" is initialized to zero when the computer is turned on. It then
increments by 1 every l/60th of a second. TI is a reserved system
variable that contains the current value of the "jiffy clock." Start your
program by setting the variable, TM in this case, equal to the Timer
(TI).
8 TM = TI
39
You can place the following statements into your program between
the lines controlling the waveform gate to control the duration of your
note.
70 POKE 54276,17
80 IFTKTM + 10THEN80
90 TM = TI
Line 80 instructs the computer to wait until the computer time (TI) is
10time intervals from where it begins before moving on to the
next line.
Line 90 resets TM to the clock.
40
SECTION III
Programming Music Using the
READ/DATA Format
When programming music, you always want to bear in mind that the
computer is your instrument as well as your composing sheet. The SID
chip is the actual sound generating instrument. The BASIC program
ming language and the assembly language statements provide you with
a number of different methods for encoding your music, and thus, act
as your method of notation. The approach you employ for encoding
your music is largely dependent upon the structure of the selected piece.
Is it short of long? Does it use single or multiple voices? Are there many
repetitions of small phrases, or relatively few repetitions? Is the tempo
slow or fast? Do you want to control the duration of each note for each
voice, or is unison the aim? These are all questions which you should
consider when tailoring a specific music program. The answers to these
questions will often determine your programming format.
One function which your computer/instrument performs very well is
organizing a collection of information, known as DATA, and present
ing it in whatever order you have programmed. This makes it particularly
well suited to developing music, provided that you can convert musical
ideas such as pitch and duration into DATA, and then structure this
DATA into a program that will present it in the proper order at the
proper time.
The BASIC language offers many tools with which to accomplish this
task. Among them are the READ/DATA statement; the use of arrays;
time loops; counters; conditional statements such as IF/THEN; flags
(known as delimiters); and directional statements, such as GOTO. These
are some of the most powerful tools of programming, and you may
already be familiar with them. If you're not, we'll show you how they
work. If you've worked with them before, we'll show you how they are
used in music programming in coordination with the SID's sound
controls.
42 "
14 READ/DATA Format
10 POKE 54296,15
40 POKE 54276,17
60 POKE 54276,16
40 POKE 54276,17
43
60 POKE 54276,16
70 GOTO 25
Let's look more carefully at the loop that is set up between Lines
25-70. As you can see, we have placed the READ statement in Line
25. This begins a loop which will cycle between Line 25 and Line 70
and then instructs the computer to return to Line 25. When placed
in a program loop such as the one used here, the first two pieces of
information will be READ by the computer in the order in which they
appear in the DATA bank, which begins in Line 100. The computer
will then assign these two pieces of information the variable names
chosen in the READ statement. In this case, H and L will be assigned
the values 16 and 196, respectively. The computer will then check to
see if the value of H is less than 0. If it were, the computer would
44
end the program. In this case, the value is 16, so the computer will
pass to the next line.
In Line 30, the values of H and L are POKEd into the appropriate
registers, thus defining the pitch C-4 as found in the Table of Note
Values. The computer then passes on to Lines 40-60, which instruct
it to play the note for the proper duration, and then to close the gate.
Having satisfied these commands, the computer passes on to Line 70,
where it receives the instruction to go back to Line 25 and READ the
next two pieces of information. This time through the loop, the com
puter will pluck the next two pieces of information from Line 100,
thus giving H and L the respective values 18 and 209, which are the
frequency numbers for D. This information will be processed through
the loop by the same procedure as before. Eventually, all the notes
for a C scale will be READ and played. Finally, on the ninth pass
of the loop, the computer will read the values -1, - 1 for H and L.
H will thus be less than (<) 0, so the program will end.
FLAGS
45
15 Setting Up the DATA Bank
There are a few simple rules that should be followed when setting up
a DATA bank. The DATA bank is usually placed at the end of your
program.
The first step is to select a line number. In our case—the C scale pro
gram that appears in the previous chapter-the DATA bank begins with
Line 100. This is followed by the statement DATA. At this point, you
can enter the values you wish to be READ. Each value must be followed
by a comma. In this way, the computer can differentiate each piece of
information.
A comma should not be placed after the final value entered on a DATA
line. If you place a comma after the final value of a line, it will throw
the computer out of sync and produce an ERROR statement. To see
and hear the effects of this mistake, go to Line 30 of your program,
and place a comma after the last value, which is 30. Then, press RETURN
to enter the change. Now, RUN the program. Aside from the strange
tones following the fifth note, you will also note ? ILLEGAL QUANTITY
ERROR IN 30 printed at the bottom of your screen.
The Commodore 64 can handle up to two full lines of values for any
single DATA entry. It will automatically continue entering values on
the second line if you overload the first. If you overload the second line,
it will continue onto a third. However, all values entered on the third
line will not be stored in the DATA bank. They will be entered as a
new line, with the number of that line being the same as the next value
in the DATA (which is the first value on the new line). This can be very
damaging to your program if you already have a line with that number.
For this reason, you should take care, when entering your DATA, not
to enter more than two lines for any single line number. If you need
46~
more space, type the next line number, followed by DATA, and con
tinue entering your values.
Another rule that should be followed when compiling the DATA bank
is always to include the same number of DATA values as asked for in
the READ statement. For instance, our statement asks the computer
to READ H and L. We must therefore include two minus ones (— 1,
-1) at the end of our DATA. Otherwise, the computer will be looking
for a value that is nonexistent, and will print an OUT OF DATA ERROR
statement.
All the DATA values in the bank are treated as a continuous list, and
are READ left to right, from the lowest line number to the highest.
47
16 Duration Control Using the
READ DATA Format
10 POKE 54296,15
40 POKE 54276,17
50 FOR T = 1 TO DR : NEXT
60 POKE 54276,16
70 GOTO 25
As you can see, our program is very similar. We have amended Line
25 by adding an extra variable called DR for duration.
We have also amended Line 50 and replaced the 900 with the variable
DR, so that it now reads
FOR T = 1 TO DR
48
You will also notice that we have added another - 1 to the flag. This
is to compensate for the third value added to the READ statement.
In this manner, we have replaced a constant duration value with
a variable. This variable is encoded as every third value in the DATA
statement. Thus, each time through the loop, the computer will place
the new value of DR into the time loop. Since time is a proportional
concept, we can create quarter notes, eighth notes, sixteenth notes,
etc. by adjusting the length of the duration number. If 200 is used
for a sixteenth note, then 400 would be an eighth note, 800 would
be a quarter note, and so on. That's because a sixteenth note is half
the duration of an eighth note while an eighth note is half the dura
tion of a quarter note.
A similar program using the "Jiffy Clock" to control duration can
be created using this format.
7 TM = TI
10 POKE 54296,15
40 POKE 54276,17
50 IFTKTM + DRTHEN50
55 TM = TI
60 POKE 54276,16
70 GOTO 25
100 DATA 16,195,25,18,209,25,21,31,45,22,96,15,25,30,15
This program replaces the time loop with the "Jiffy Clock." This is
accomplished by adding Line 7, TM = TI. This tells the computer
to set the variable TM equal to the value of TI. TI is the BASIC
numeric function which reads the current value of the interval times.
Each 1/60 of a second, the interval times will increase by 1. By set
ting TM equal to TI, the computer will also increment TM by 1 as
it increments TI.
In Line 50 we add the conditional statement
50 IFTKTM + DRTHEN50
to replace our time loop. Now, the computer will add the value READ
49
as DR to the current value of TM, and remain on Line 50 until the
interval timer. TI catches up to the new value of TM. When TI is
no longer less than TM, the computer will go to Line 55
55 TM = TI
which will once again set TM equal to the current value of the timer.
By placing our timer between Line 40, which turns on the note, and
Line 60, which turns off the note, we can effectively control the dura
tion of the pitch.
Once again, we use proportional values to create notes of different
length. If 15 is a sixteenth note, then 30 is an eighth note and 60 is
a quarter note.
50
17 The RESTORE Statement
7 TM = TI
10 POKE 54296,15
40 POKE 54276,17
50 IFTKTM + DRTHEN50
55 TM = TI
60 POKE 54276,16
70 GOTO 25
100 DATA 16,195,30,18,209,30,21,31,45,22,96,15,25,30,15
The computer will ignore this statement until H is less than 0. When
H is less than 0, it will reset the internal pointer to the first element
on the DATA list, go back to the beginning of Line 25, and READ
the DATA once again. This creates our loop.
This is useful for certain sound effects, but for playing music, we
need more control of the DATA than just a repetitious loop. We can
gain some of this control by using the RESTORE statement in coor
dination with counters. We will discuss the use of counters in the next
chapter.
52
18 Using Counters
Counters are used to keep track of how many times the computer
has performed a particular task. When used in coordination with con
ditional statements, they become useful tools for the manipulation of
DATA. If we add a counter to our last program, we can then use it
with a conditional statement to play the scale as many times as we wish,
and then to break out of the loop. The following example will play the
scale three times and then END.
10 POKE 54296,15
40 POKE 54276,17
50 IFTKTM + DRTHEN50
55 TM = TI
60 POKE 54276,16
70 GOTO 25
100 DATA 16,195,30,18,209,30,21,31,45,22,96,15,25,30,15
500 C = C+l
510 IF C = 3 THEN POKE 54276,0 : END
520 RESTORE : GOTO 25
53
In this program, we have again changed Line 25:
500 C = C+1
Now the computer resets the DATA pointer to the first element on
the DATA list, and returns to Line 25 to start the process over. This
time through the loop, when it is directed to Line 500, it will again
increment the counter by 1. Thus, C = C+ 1 is converted to 1 = 1
+ 1. The C which was 1 is now 2. Once again the computer will pass
by the conditional statement in Line 510 and RESTORE the DATA
for another pass through the loop.
Finally, on the third pass, the counter will be bumped up to 3. The
conditional statement IF C = 3 will be fulfilled, and the computer
will execute the commands to turn off the volume and end the program.
You will find counters very useful in a variety of situations. We'll
show you some of these uses in later programs. For now, however,
let's move on and program a piece of music.
54
19 Programming A Song
For One Voice Using
READ/DATA Format
Let's start our music programming with a song familiar to all. We've
chosen "Oh, Suzannah," played on synthesized banjo.
Here is the song in standard musical notation.
OH, SUZANNAH
70 POKE 54276,33
80 FOR T = 1 TO DR : NEXT
55
90 POKE54276,32
95 FOR B = 1 TO 34 : NEXT
100 GOTO 40
910 restores the DATA and sends the computer back to Line 40 to restart
the song.
75 = sixteenth note
56
150 = eighth note
225 = dotted eighth note
300 = quarter note
450 = dotted quarter note
For the last note, we used a duration value of 900, simply because
it sounds better. It's all right to break out of a strict numerical inter
pretation of the time value. This will make your music sound more
"human" and less "machine-like." As you become more familiar with
programming music, it's a good idea to experiment with all your
variables. This will give your music a more "personal" touch.
Another little twist we added to this program was a second time
loop in Line 95. Not only does this have the effect of slowing the en
tire program down, it also puts a space between the notes. Line 95
adds only a thirty-second note to each duration which is not that
noticeable. What it really does is separate the notes to make the ar
ticulation crisper, enhancing the overall banjo effect. When you have
to describe a sound completely on the computer, many things that
just happen when someone plays an instrument have to be worked
into the computer description of each instrument. The numbers 1 to
34 were chosen through experimentation: 50 was too slow, 25 was too
fast.
Finally, we used our flags, counter and RESTORE statement to
manipulate the DATA. In this case, all we wanted was to repeat the
entire DATA list and END. It is possible, however, to use these tools
for more complex manipulation of DATA. To see how, take a look
at the next chapter.
57
20 DATA Manipulation
Using Flags, Counter,
and RESTORE Statement
It is probably dawning on you just about now that the main drawback
in music programming is the quantity of numbers involved in setting
up a DATA bank. There are, however, many tricks you can use to cut
down on the number of notes you have to encode.
In the READ/DATA format, there is some flexibility for ac
complishing this with the proper use of flags, counters, and the
RESTORE statement. The chief disadvantage of this particular format
is that you can only return to the beginning of the DATA list when you
use the RESTORE statement. Still, it offers much in the way of a direct
approach to programming while you are just starting out. Later on, we'll
introduce you to the ARRAY format, which is more flexible in its ability
to manipulate DATA, but a little harder to understand. The concepts
that you will learn here, however, will be applicable to ARRAYS.
The following program will serve to illustrate some of the possible
techniques you can use to manipulate DATA. It is a beautiful melody
by Robert Schumann from Opus 15, "Scenes From Childhood." Type
it in on your Commodore, sit back, and take a listen. This program
runs about two minutes.
58
5 REM ♦♦♦ SCHUMANN ***
110 DATA-1,-1,-1
1010 IF I = 3 THEN 40
1020 IF I = 5 THEN 40
As you can see, this program utilizes two counters: one in Line 35
and one in Line 1000. It also uses three flags: the first in Line 100,
the second in Line 130, and the third in 170. In addition, we use the
RESTORE statement three times.
To understand what all this is doing, let's take a look at the musical
notation for this piece.
59
ABOUT STRANGE LANDS AND PEOPLE
& By Robert Schumann
(1) (3) m (4) . (5; m (6)
4e
ir r TJ r u J u J
(21), (22)
The numbers represent the measure number. The * represents the place
ment of a flag.
This song contains 54 notes. That would require encoding 162 pieces
of information, were we to encode the high and low frequency numbers
and the duration of each note. In addition, if we wanted to play it
twice, that would mean 324 entries. Obviously, this is undesirable.
Looking carefully at the piece we can see that some of the music is
used more than once. Using our counters and flags in coordination
with the RESTORE statement, we can get this number down to about
100. If that still sounds to you like a lot of numbers, you are right.
Turning the flow of electrons into real music is not the simplest of
matters —lots of numbers are required, no matter what method you
use. The payback comes from all the listening pleasure you and your
friends will derive from the work. That doesn't mean, though, that
you shouldn't try your hardest to cut down the number of entries you
"have to" make.
We set up two counters in this program. The first counter was set
up in Line 35.
This counter keeps track of how many times the piece is played. The
first time through J = 1; the second time, J = 2. After completing
the second pass, we go back to the counter a third time, and thus trig
ger the end of the song.
We set up a second counter at Line 1000. This counter (I) works
with the flags as set up in Line 40:
60
This counter has been initialized at 0 in Line 35. We use this counter
to keep track of our flags.
The next step is to eliminate as many repetitious phrases as possible,
always keeping in mind that once we use the RESTORE statement,
we will be back at the beginning of our DATA list. Studying the musical
notation, we see that measures 3 and 4 are identical to measures 1
and 2. Also, measures 17-22 are identical to measures 3-8. With this
knowledge, we can eliminate 20 notes, or 60 DATA entries. DATA
Lines 100-110 contain the information for measures 1 and 2, follow
ed by a flag - 1, - 1, - 1. When the computer sees the flag, it goes
to Line 1000. The counter (I) is now 1, so the computer follows the
command for 1. It RESTORES the DATA, and goes back to Line
40, where it will re-READ the list. In this manner, we have eliminated
the necessity of encoding measures 3 and 4.
The next time the computer arrives at the flag in Line 110, it is again
directed to Line 1000. This time, the counter (I) is 2, so the computer
follows the command for 2 in Line 1005. As you see, the DATA is
not restored this time through. This means that the computer will con
tinue reading the DATA at the point it last left off (Line 120). In this
manner, we can place a flag in the DATA and use it several times,
having it perform a different function each time.
The computer arrives at its next flag in Line 130, which is the end
of measure 8 in the musical notation. The counter is moved up to 3,
and the computer is again directed to go back to Line 40 and to con
tinue READing DATA for measure 9 (Line 140).
In DATA Line 170 the next flag is registered, bumping the counter
up to 4. We are now at measure 17 of the music. Here is our chance
to eliminate encoding measures 17-22. We do this by RESTOREing
the DATA and returning to the beginning of our list. Keep in mind
that we wish to duplicate measures 3-8, and not measures 1 and 2.
This is accomplished when we arrive once again at our flag in Line
110. This time, the counter moves to 5, and the computer receives the
instruction to keep READing. It does so until it reencounters the flag
in Line 130. At this point, we are at the end of measure 22 in the music.
Our counter is now at 6. We have played the song once through,
and it's time to tell the computer to play it again. We accomplish this
by redirecting it, back to Line 35. At Line 35, our counter (J), which
keeps track of the number of times we play the song, is bumped up
to 2. Our counter (I) is reset to 0, and we begin the whole process over.
When we arrive at Line 1030 for the second time, the computer is
directed to go to Line 35. The counter (J) is moved up to 3. The volume
61
is turned off, and the program ENDs.
If you are having any problems understanding these new concepts,
take heart, and above all, don't give up. It often takes a while to ab
sorb a new idea completely. If you need a break, take one, and let
the ideas roll around for a while. If you'd like, skip ahead to some
of the sound effects, and have a few laughs.
62
21 Shortcuts and Abbreviations
When you are typing in your programs, you will probably want to
save as much time as possible. For this reason, we are going to introduce
a few time-saving conventions.
The fifst convention is the use of abbreviations. The Commodore 64
allows you to abbreviate many key words in the BASIC programming
language. A complete list may be found in Appendix D of the Com
modore User's Guide. An abbreviation is usually accomplished by typing
the first letter of the command followed by a shifted second letter. Thus,
POKE is abbreviated with a P and a shifted O. The shifted letter will
appear on the screen as the graphics symbol located on the right of the
front face of the letter key. A shifted O would therefore appear as an
inverted L.
The advantage of the abbreviation is that it saves time and space. When
the program is listed, the abbreviated command will appear in its full
spelling. The abbreviation for DATA, for example, D and shifted A,
will appear as the full word DATA when the program is listed. You
must take care to leave space at the end of your second DATA line if
you use the abbreviated form. The space must be enough to accommodate
the fully written commands. Otherwise, you might lose a few elements
in your list.
Other abbreviations you may find useful are RE (shifted) S for
RESTORE; and L (shifted) I for LIST.
The second time-saving convention you may find helpful is the use
of initialization. Start your program by assigning the number 54272 to
a variable such as M. The number 54272 is the number of the first register
in the sound chip. Thus, if M = 54272, then M + 1 would equal 54273.
Similarly, M + 24 would equal 54296. In this way, you can cut down
63
on the length of your POKE addresses. If you look at your SID Chip
Control chart, you will see that we have provided you with a complete
list of abbreviated numbers in the first column. Use the chart as follows:
begin your program with an initialization.
M = 54272
Then, if you want to POKE a specific address, look for the number
on the chart.
For example, POKE M + 11, 17 would POKE the waveform con
trol register for Voice II with the triangle waveform. Similarly, POKE
M + 24, 15 would set the volume to its maximum setting.
We will be using this convention often in the following chapters.
After a while, you won't have to look at the chart, since you will
remember the code.
One last time-saving tip: make a master program for each music
format and keep it on file. Then, when you want to program a piecg,
just load it in and fill in whatever variables for waveform, ADSR,
etc. you wish. Then, it's a simple matter of entering your new DATA
and whatever commands are necessary for its manipulation.
Why not try out these techniques on the next program? It's for a
simple bass line.
30 M = 54272
40 POKE M +24,15
:END
80 POKE M +4,33
90 FOR Q = 1 TO DR : NEXT
120 GOTO60
64
500 C = C + 1 : IF C = 1 THEN RESTORE : GOTO 60
510 IFC = 2THEN60
520 IF C = 3 THEN RESTORE : GOTO 60
530 IF C = 4 THEN RESTORE : C = 0 : GOTO 55
65
22 Multiple Voice Programming
Using the READ/DATA
Format
Now that you are familiar with some of the music programming tech
niques for one voice, it's a simple matter to get all three working for
you. Here is a program that will demonstrate the 64's ability to pro
duce music with three part harmony. For this program, we use the
READ/DATA format combined with the "Jiffy Clock." You will notice
that we have provided a separate DATA line for each of the song's thirty-
six notes. Although this is certainly not necessary, it does provide for
a certain amount of clarity. Another advantage of this method is that,
should you enter a wrong value in your DATA, it is a simple matter
to locate the error.
66
180 READH1,L1,H2,L2,H3,L3
190 POKE M+ 1,H1 : POKE M,L1 : POKE M + 4,33
200 POKE M + 8,H2 : POKE M + 7.L2 : POKE M + 11,33
210 POKE M+ 15.H3 : POKE M + 14,L3 : POKE M+ 18,17
220 T = T+X
67
630 DATA 15,28,49,0,0,0,0
680 DATA -1
700 RESTORE
68
With the ability to create harmony through the use of multiple voices,
your music will take on a new richness and color that was not present
with just one voice.
You will find the method used in this program useful for many types
of musical statements. Its one major drawback, however, is that it
does not provide for independent control of the duration for each voice.
You must, therefore, construct your programs with this in mind. All
these voices must play for the same duration simultaneously. In other
words, Voice 1 cannot play a quarter note at the same point in the
program that Voices 2 and 3 are producing eighth notes. You can,
however, create the illusion of truly independent voices by turning dif
ferent voices on and off at different times during the program. This
is done quite simply, by entering zeros for the high and low frequency
numbers of the voices you wish to remain silent. The effect will be
that of independent movement.
We have provided you with a more sophisticated program (in Sec
tion V) that will enable you to achieve true independent control of
multiple voices. If you are curious, skip ahead and take a look.
Here is another program which is similar to the one you just typed in.
10 PRINT " > > > > MUSIC ARRANGED BY JIM VOGEL < < < < "
100 M = 54272
150 T = TI
170 READX:IFX<0THEN900
180 READH1,L1,H2,L2,H3,L3
220 T = T+X
69
310 DATA 10,17,195,0,0,8,225
70
710 DATA 20,0,0,0,0,7,12
720 DATA 30,0,0,0,0,4,180
730 DATA -1
910 RESTORE
The major difference between this and the previous program lies
in Lines 190-210.
As you can see, we have now made these lines into a conditional
command. If the computer reads a zero value in HI, it immediately
goes on to line 200. This is because the computer treats the zero in
the statement as no value. If HI does have a value, however, the com
puter will execute the remainder of the line before passing onto line 200.
You may wish to save the first section of this program — up to Line
230—and use it as a template for your own future programs.
71
SECTION IV
Sound Effects, Ring Modulation,
Synchronization, Filters,
and Frequency Modulation
One of the truly entertaining aspects of the SID is its ability to create
sound effects. This is one area of sound generation which can really call
into play many of the 64's unique capabilities. You can, for instance,
incorporate the 64's ability to generate random numbers into a sound
effects program, as we've done in the program "Insect Speech." You
can also utilize the 64's STEP function to create a program that will
"scoop" pitch, producing glissando and siren-like sounds. All sorts of
interesting effects may be created through the use of the SID's Ring
Modulator and Synchronization bits. In addition, the sometimes unruly-
sounding waveforms can be molded into a more sophisticated sound
through the use of the three filters and resonator bits of the SID. Finally,
you can utilize these effects as sub-routines, or incorporate them into
the main body of video programs to add another dimension to your com
puter art or games.
Let's move ahead and learn how all this can be done.
74
23 Phaser and Siren Sound
Effects Using the STEP
Command
will increment by one each time through the loop. With the addition
of the STEP statement, we can increment the counter by whatever value
we choose. For example:
20 M = 54272
30 POKE M + 24,15
60 POKEM + 1,X
70 POKE M + 4,17
You will notice that in creating this effect, we changed a few rules.
We did not, as has been our custom, utilize both the high and low fre
quency numbers. That is because we were not interested in creating a
specific pitch, such as A-440, which requires two numbers. You can use
the high frequency register alone to produce pitched notes if you are
not concerned with exact interval relationships. Using the low frequen
cy register alone, however, is not advisable because it produces very lit
tle sound.
You will also notice that, while we turned on the waveform and opened
the gate, we never closed it with a 16. This choice produces a constant
on sound of the program. This is also why our SUSTAIN/RELEASE
setting is set at 240, maximum SUSTAIN and no RELEASE. Since the
RELEASE cycle of the ADSR is triggered by the closing of the gate,
no setting we might have used would ever have had a chance to func
tion. So, even though there are many notes produced, the effect is one
of continuous sound getting higher and higher, which is repeated twice.
The most important point to grasp, however, is our use of the STEP
command. Each value of X that was obtained from the loop was POKEd
into the low frequency register: the first time a value of 4, then 6, then
8, etc. This is the element of the program responsible for the gradual
rise in pitch.
76
We can also invert the STEP command by using negative numbers.
For example:
will loop ten times, creating the values 50,45,40,35, etc. for the variable
T. We used this technique in the following program.
20 M = 54272
30 POKE M + 24,15
60 POKEM + 1,X
70 POKE M +4,17
210 GOTO 50
This program is identical to the one for our first effect, with one altera
tion, in Line 50:
20 M = 54272
30 POKE M +24,6
40 POKEM + 5,136: POKE M +6,240
60 POKEM + 1,X
70 POKE M + 4,33
100 POKEM+1,Z
77
200 C = C+l :IFC = 3 THEN 500
210 GOTO 50
Still another effect that may be obtained through the use of the STEP
statement is the "Phaser" effect.
30 POKE M +24,15
50 POKE M +4,17
60 FOR K = 1 TO 15
70 FOR Z = 250 TO 0 STEP - 20
80 POKEM + 1,Z
90 NEXT Z: NEXT K
78
24 The White Noise Effect;
Blast Off and Locomotive
The easiest effect to produce on the 64 is the White Noise effect. Simply
POKE 129 into the waveform register. You can use the White Noise
waveform for explosions, bombs, gunfire, and all sorts of havoc.
Here is just one example of its many uses-it dramatizes well the power
of the White Noise. It's called "Blast Off." Once again, we utilize the
Nick's Sealer Program; only this time we make two simple changes. In
Line 40, we changed the triangle waveform of 17 to a white noise
waveform of 129. In Line 80 we cut down the timer from TM+10 to
TM + 2. The result is quite startling. Our eight-octave chromatic scale
turns into a rocket blasting off and finally trailing away into space! (Note:
when used in a program, % means integer.)
20 POKE 54296,15
60 A = A/2 t I
70 L°7o = A/256 : H% = A-(256*L°fo)
80 IF TI<TM + 2 THEN 80
90 TM = TI
100 POKE 54273,L<7o : POKE 54272,H°7o
"79
110 GOTO 40
36376,34334
800 DATA -1
900 RESTORE
920 END
100 M = 54272
120 FOR J = 1 TO 40
190 NEXTJ
For this effect, we used a nested loop. Line 20 creates a loop that
makes the entire effect play 40 times while Line 170 controls the dura
tion for each chugging sound. This chugging sound is created by us
ing a low ATTACK number (Line 140) with no SUSTAIN, combined
with the noise waveform.
White Noise effects can vary greatly when filtered through a high
pass filter. Well show you how to set these filters in Chapter 28, "Pro
gramming Filters."
80
25 Sound and Color
One of the most common audio effects wafting out of the halls of
video arcades sounds like this:
20 M = 54272
30 POKE M+ 24,15
60 POKEM+1,X
70 POKE M +4,33
210 GOTO 50
500 POKE M +24,0: END
8l
problem, we suggest you use the Jiffy Clock. Using the clock allows
you to go off and check conditional statements and then return to
check the clock.
Here is one way that you might insert this sound program into a
game. Our intention here is merely to show you the technique of in
tegrating sound with simple graphics commands.
30 POKE M +24,15
40 POKE M +5,136: POKE M + 6,240
50 FOR X = 5 TO 255 STEP 20
60 POKEM+1,X
70 POKE M + 4,33
Our program will clear the screen, play the effect, change the back
ground color to red (Line 205) and then to black (Line 206), repeat
the process four times, and then print out a message before ending.
Here is a program that will produce sixteen color bars, one for each
of the 64's colors, across your display screen. After each color bar,
a tone will be produced. Make sure to place the end quote and
semicolon properly in the second display line of Line 60. Otherwise,
the graphics will not run correctly. The reversed field R in Line 60
is produced by pressing the control key and 9 simultaneously.
82
50 PRINT
70 POKE 54296,15
130 NEXTW
As you can see from the REM statement, the sound commands begin
on Line 70 and continue onto Line 120. They should, by this time,
be quite familiar.
You can diversify this program by changing your sound program
ming. This is accomplished by expanding your READ statement to
include values to be POKEd into the high and low frequency number
registers.
40 READA,B,C,
50 PRINT
70 POKE54296,15
130 NEXT
83
160 DATA 149,63,75,150,67,15
40 READ A
to read:
40 READA,B,C.
84
26 Using the Random Number
Function to Create Effects
30 POKE M +24,15
40 POKE M +5,16 : POKE M +6,255
50 FOR Z = 1 TO 5
60 POKE M + 1 ,RND(Z)*50 + 54
70 POKE M + 4,33
80 FOR K = 1 TO 4 : NEXT K
90 POKE M + 4,32
100 NEXTZ
200 C = C+1:IFC = 25 THEN 500
210 GOTO 50
500 POKE M + 24,0: END
85
50 FOR Z = 1 TO 5
60 POKE M + 1 ,RND(Z)*50+ 54
30 POKE M +24,15
60 POKE M + 1 ,RND(Z)*225 + 20
70 POKE M +4,17
80 FOR K = 1 TO 4 : NEXT K
90 POKE M +4,32
100 NEXTZ
86 ~~
Line 70 is to open the waveform gate and turn on the triangle
waveform. In Line 90, we close the gate, but change the waveform
to 32. That means that the RELEASE cycle, which was set at its max
imum, will have a sawtooth value. Try this trick on some of your music
programs if you get tired of the way they're sounding.
87
27 Ring Modulator
and Synchronization Effects
With the SlD's Ring Modulator, you can produce astounding sound
effects. When set, the Ring Mod bit takes the output of two oscillators
and produces tones that are the sum and difference of their output. To
understand how to set the Ring Mod bit, you have to take a look at
the waveform registers for each voice.
Decimal
Control 128 64 32 16 8 4 2 1
Number
Function
1
Bit
7 6 5 4 3 2 1 0
Number
As you can see, the Ring Modulator is located on Bit 2, with a decimal
control number of 4. To utilize the function, you must add the Ring
Mod to the triangle waveform. Thus, the statement POKE 54276,21 in
structs the computer to use the Ring Mod function for Voice 1.
16 = triangle waveform
4 = Ring Mod
+ 1 = opens gate
21
What we have done here is to POKE values into the high frequency
number registers of both Voice 1 and Voice 3. No further settings are
required for Voice 3. It is not necessary to turn on Voice 3 waveforms
or ADSR.
To hear what the Ring Mod can do, let's insert these commands
into a program we used early on, called "Nick's Sealer."
1 REM *** RING MOD EFFECT *** BY J.C. VOGEL & NICK
10 FORI = 0TO7
20 POKE 54296,15
40 POKE 54276,21
60 A = A/2 t I
80 IFTKTM + 2THEN80
90 TM = TI
110 GOTO 40
36376,34334
800 DATA -1
900 RESTORE
920 END
Originally, this program played a chromatic scale for the entire range
of the 64. Now, with just a few changes, we get a remarkably different-
sounding program. We changed Line 40 to use the Ring Modulators.
It now reads:
40 POKE 54276,21
" " 89
In addition, we changed Line 100 to mix the output of oscillators 3
and 1. It now reads:
POKE 54283,21
POKE 54290,21
You will notice that no other settings are used for Voice 3 other than
its high frequency number. This is all the 64 needs when it is using
Ring Modulation. The ADSR or other setting for Voice 3 will have
no effect. The same rules apply for mixing the other voice.
SYNC FUNCTION
16 = triangle waveform
4 = Sync function
+ 1 = gate
19
To hear what the Sync can do, let's alter another one of our pro-
90
grams. We've chosen "Jazz Bass Line," which appears at the end of
Chapter 21.
10 REM *** SYNC BASS LINE ***
40 POKE M + 24,15
60 READH,L,DR:IFH<0THEN500
70 POKE M +15,H: POKE M+1,L
80 POKEM + 4,19
90 FOR Q = 1 TO DR : NEXT
The only changes occur in Line 70, where we mix in the output of
the Voice 3 high register; in Line 80, where we activate the Sync; and
in Line 110, where we turn it off.
It should be noted that the Sync function works better when the
lower pitch is in the mixed-in oscillator—as, in this case, Voice 3. That
is why we have placed Voice 3 first. If you examine the DATA, you
will see that the first variable is always lower. Other than this, the
same rules for mixing the voices that we used for the Ring Modulator
can be used for the Sync function. To utilize the sync for Voice 2,
POKE 54283,19 and mix it with the output of Voice 1 as follows:
9l
28 Programming Filters
Using the SID's filters is not so much a sound "effect" as the way
of "affecting" your sound. If you have not, as yet, read Chapter 5,
"Understanding Filters," we suggest you go back and do so at this time.
As stated earlier, the SID offers you three filters, called low-pass, high-
pass and band-pass. A band-reject filter may be produced by adding
the high-pass and low-pass filters together. You can also add resonance
to each filter setting.
The filter controls are located on registers 54293-54296.
Decimal
Control 128 64 32 16 8 4 2 1
Number
54293 FC10 FC9 FC8 FC7 FC6 FC5 FC4 FC3 HIFC#
54295 RES3 RES2 RES1 RESO FLTX FLT3 FLT2 FLT1 RES/FLT VOICE #
Voice
54296 HP BP LP VOL3 VOL2 VOL1 VOLO FLT&VOL
3 OFF
Bit # 7 6 5 4 3 2 1 0
92
To utilize the filters, follow these steps:
1. Choose the voices you wish to filter. The control for voice selection is
located at register 54295. For Voice 1, POKE 54295,1; for Voice 2, POKE
54295,2; for Voice 3, POKE 54295,4. For a combination, such as Voices
1 and 3, just add the decimal control numbers: POKE 54295,5. Enter
this setting onto a program line near the beginning of the program,
preferably before the ADSR and waveform settings. The FILTEX set
ting is for external audio input signals.
2. Choose your filter mode. The control is located on register 54296, the
same as your volume control. The low-pass has a 16 control number,
band-pass a 32, and high-pass a 64. Add the filter setting to the volume.
Thus, a low-pass filter with a maximum volume setting would be pro
grammed with POKE 54296,31. Enter this into your program where you
usually enter the volume.
15= volume
+ 16= low-pass filter
31= setting
93
20 POKE 54277,0 : POKE 54278,128
25 READ H,L : IF H < 0 THEN END
30 POKE 54273,H : POKE 54272,L
40 POKE 54276,33
70 GOTO 25
100 DATA 16,195,18,209,21,31,22,96,25,30
All of our settings for the filters are accomplished on Lines 10 and 15.
10 sets the filter for Voice 1 with moderate resonance: the low cutoff
number is 0; the high cutoff number is 100.
15 sets the low-pass filter and the volume at 15.
To hear what this program would sound like without the filter,
simply change the setting for register 54295 from 129 to 0. This also
demonstrates that, unless you specify which voice you wish to filter,
all your other filter settings have no effect on the program.
Using the programmable filter is a fairly subtle procedure, as there
are a number of settings which you must control. The cutoff frequency
number is particularly significant in controlling the output of your
sound. The approximate cutoff frequencies range from 30 Hz to 12
KHz. Hz stands for Hertz, which are the same as cycles per second.
Again, the higher the number is, the higher the frequency.
To better understand this, look at our sample filter program. We
use a low-pass filter which will pass the frequencies below the cutoff
number, and attenuate those above. We use a setting of 54293,0 :
54294,100. If we raise our cutoff number to 54293,0 : 54294,240, our
cutoff sound will be closer to the original unfiltered sound. That is
because we are raising the cutoff point-thus allowing more of the
original harmonics to pass unfiltered.
If we were using the high-pass filter, the output result would be the
opposite, since the high-pass filter passes those harmonics above cutoff
while attenuating those below.
The best way to "get hold of the filter controls is to put aside an
hour and experiment with different settings and different filters.
94
29 Frequency Modulation
128 = 3 OFF
+ 15 = volume
143
95
To see how this works, type in this program:
100 M = 54272
150 N = 5728
200 NEXT
If you are unclear about the formula used in Line 180, there is an
explanation in Chapter 9, "Advanced Pitch Control Techniques."
To experience the true range of this effect, try changing the variables.
We suggest you start with Line 120. Change the waveform selection
to sawtooth, then pulse, and then white noise, to hear the various forms
96
of modulation. Don't forget to add a pulse width number when you
use the pulse waveform.
After you've experimented with the waveforms, try changing the
low frequency number for Voice 3, also in Line 120. Another number
you can change is in Line 150. To obtain a new number, consult the
Table of Note Values, or make one up.
Finally, you can change a number in Line 170. This is the number
on the far right. It is currently set at 100. A change here has a dramatic
effect. Try replacing the 100 with a 2 to hear what we mean. Frequency
modulation is one of those effects that is particularly well-suited for
a computer.
Another form of modulation can be accomplished using a similar
method, but with the output of the top eight bits of the Voice 3
envelope generator. For this effect, use register 54300. You can mix
the output of this register with the filter frequency output of another
oscillator to create a "phasing" effect. Your one rule here is that the
gate for the third voice must be open. A waveform setting is not
necessary.
100 M = 54272
160 N = 5728
170 FQ = N + PEEK(M + 28)* 10
180 HF = INT(FQ/256): LF = FQ-HF*256
190 POKE M,LF : POKE M + 1 ,HF
200 NEXT
97
145 opens the gate for Voice 1.
150 begins the loop.
160 establishes the frequency number.
170 adds the frequency number to the value read in register 54300
multiplied by 10.
180 converts the FQ number to high and low FQ numbers
190 sets the high and low FQ registers for Voice 1.
200 instructs the computer to go back and do it again.
210 turns off the volume.
98
SECTION V
Programming Music Using
the Array Format
Too
30 Using Subscripted Variables,
Simple Arrays,
& The DIM Statement
30 POKE 54296,15
60 READ H,L
90 POKE 54276,WF(I)
110 GOTO 60
920 GOTO 60
101
We create our array in Line 20. It consists of four parts, or elements,
called subscripted variables. They are WF(0), WF (1), WF(2), and
WF(3). As you can see, part of the name, WF, remains the same, but
the part in parentheses is variable. This part is known as the subscript.
When the computer encounters a subscripted variable such as WF (0),
it recognizes it as an array, and immediately sets aside, or "dimen
sions" a portion of memory to hold eleven elements. This means that,
even though we only used four elements in our array, enough memory
was automatically reserved potentially to hold WF(4)-WF(10). If we
had wished to use more than eleven elements, we would have had to
use another array-devising procedure, which we will describe shortly.
That procedure utilizes the BASIC DIM Statement.
When creating an array with subscripted variables, the first
characters in the name must be a letter. The second optional characters
may be a numeral or letter. Thus, WF(0), W3(0), and W(0) are all
possible choices, while 3F(0), 33(0), 3(0) are syntactically illegal.
The other important task accomplished in Line 20 was the assign
ment of information to be held in the array. Here we used a direct
method by simply stating WF(0) = 33 : WF(l) = 17 etc. Now the com
puter places into its memory the information that it stores 33 as the
first element on its WF list, 17 as the second element etc.
On Line 90 of the program, we tell the computer to utilize this infor
mation each time it goes through the loop established between Lines
60-110.
90 POKE 54276,WF(I)
95 PRINT WF(I)
and try running it again. You should now see the waveform number
of each note appear on the screen right after the note begins.
To demonstrate that the array is stored in memory, or to verify that
a particular element in the array is correct, try this little trick. Without
using a program line, simply type in PRINT WF(2) and hit RETURN.
The computer should respond with a 65 printed on the screen.
102
USING THE READ/DATA STATEMENT
TO LOAD THE ARRAY
Another method to load an array is to READ in a DATA list. The
difference between the DATA/READ into an array and a regular
DATA list is that the DATA is now stored in memory, and each ele
ment of the list has been given a name. To demonstrate this quickly,
try this short program.
10 FOR I = 0TO3
20 READWF(I)
30 NEXT
40 PRINT WF(0),WF(1),WF(2),WF(3)
50 DATA 17,33,65,129
After you run the program, which has no sound, type PRINT WF(2)
and hit RETURN. The computer should respond with a 65, showing
that the number 65 has been stored in an array under the name WF(2).
We'll use this technique to load most of the arrays we use in music
programming.
103
31 Using the Two-Dimensional
Array Format for One
Voice
10 DIMV(20,2)
15 M = 54272 :Z = 0: C = 0
104
85 REM *** PLAY THE MUSIC ***
90 Z = 0
210 DATA-1,-1,-1
1010 IF C = 1 THEN Z = 2
1012 IF C = 2 THEN Z = 4
1014 IF C = 3 THEN Z = 0
1016 IF C = 4 THEN POKE M + 24,0 : END
1020 GOTO 100
z () i :2
0 16 195 1i
1 18 209 ji
2 21 31 :i
1
20
r \
105
This is known as a two-dimensional array. The statement DIM V
(20,2) instructs the computer to create a two-dimensional array, twenty-
one rows by three columns. The 2 in DIM V (20,2) represents the three
columns of information loaded off the DATA. The first column, 0,
holds the HF number, while the second column, 1, holds the LF
number. The third column, 2, holds the duration number. The 20 in
the statement represents the note number or row number that has been
loaded into the array using the counter Z in Line 60.
The second part of the program (Lines 90-150) uses the informa
tion stored in the array to play the music.
The third part of the program controls what information will be
used in the play section.
Now, here is the line-by-line explanation.
106
140 closes the gate (ends the note); increments the counter Z.
150 loops back to Line 100 and plays the next note.
200 DATA for FQ numbers and duration.
210 DATA flag.
1000 increments the counter C.
1010 instructs the computer, IF C = 1, THEN set the note counter Z to
equal 2 (which is the third note stored in the array).
1012 instructs, IF C = 2, THEN set the note counter Z to equal 4 (which
is the fifth note stored in the array).
1014 instructs, IF C = 3, THEN set the note counter Z to equal 4 (which
is the first note stored in the array).
1016 instructs, IF C = 4, THEN turn off the volume and END the
program.
1020 instructs the computer to go to 100 and play the next note.
If you find the concept of using a zero as the first element in the array
and a one as the second element too much of a "double think," then
initialize the Z to be equal to 1 in Line 15 and Line 90. If you do this,
then also increase the Z's in Lines 1010, 1012 and 1014.
We hope that you are now understanding the array concept. If not,
don't worry about it. You'll catch on, just give it time. You may also
consult one of the many texts on BASIC programming that are available
for more examples. You can still use the programs we've provided as
a guide. Later on, you can fashion your own program to fit your needs.
107
32 Creating Two Characters
With One Voice
10 DIM V(200,2)
15 WF(0) = 16 : WF(l) = 64
20 M = 54272 : C = 0
108
25 FOR S = 54272 TO 54296 : POKE S,0 : NEXT
30 POKE M + 2,25: POKE M +3,8
35 POKE M +24,15
40 POKE M + 6,255
50 READH,L,DR
80 Z = 0
90 TM = TI
150 GOTO 85
200 DATA 67,15,28,59,190,7,56,99,7,50,60,7,44,193,7,-1,-1,-1
1011 IF C = 3 THEN Z = 12 :1 = 1
1013 IF C = 5 THEN Z = 0
1015 IF C = 6 THEN Z = 6 :1 = 1 : POKE M + 5,8:POKE M + 6,15
1016 IF C = 7 THEN Z = 17 :1 = 1
109
30 sets the low and high pulse width numbers.
35 sets the volume.
40 sets the ADSR for the first voice.
50 reads the first three elements from the DATA list.
55 stores the information in the two-dimensional array and increments
the note counter Z.
60 signals the end of the array loading process.
70 instructs the computer to loop back and read the next 3 elements of
note information from the DATA list.
75 sets up the counter J to play the song twice and then end.
80 resets Z = 0 for the play portion of the program.
85 checks for flags.
90 sets TM = TI, the "Jiffy Clock."
100 POKEs the high and low frequency number registers.
110 turns on the waveform and opens the gate by adding one to the
waveform number WF(I).
120 checks the clock and plays the note for the duration value.
130 closes the gate using only the waveform number WF(I).
200-230 DATA and flags * note 0,0,0- flag to signal the end of the READ
section (see Line 60).
1000-1017 manipulates the DATA in the note array by resetting the note
counter Z; changes the waveform WF(I); changes ADSR when
necessary. It is the changes in waveform WF(I) and envelope (ADSR)
that change the character of the voice.
1040 instructs the computer to go back to the play section.
Now let's put all of our programming knowledge together and create
a program that will control multiple voices with independent duration
for each voice.
no
33 Controlling Multiple Voices
with Independent Duration
This program will allow you to translate sheet music or your own com
positions into a format that can be executed by the computer. Your one
limitation will be the number of voices available at any given time-
which, of course, is three.
To cut down on the length of the DATA list, we will be using a tech
nique of encoding our pitch outlined in Chapter 9, "Advanced Pitch Con
trol Techniques." With this approach, you do not have to enter the high
and low frequency numbers. Instead, you will enter a single number
known as the frequency number. This number may also be obtained
from the Table of Note Values.
In this program, three arrays are dimensioned to hold the informa
tion of the song. One will hold the high frequency numbers of all three
voices. A second will hold the low frequency numbers, and a third will
hold the duration information.
During the READ portion of the program, the frequency numbers
will be READ from the DATA list. Each number will then undergo a
mathematical operation built into the program. This operation will break
down the number into a high frequency and a low frequency number.
This information is then loaded into the proper array. An explanation
of the formula may be found in Chapter 9.
The second number READ from the DATA statement will be the dura
tion number. This number will have an entirely new meaning for you,
and is the secret to controlling the voices independently.
Here is how it works. The playing portion of the program is built
around a short time loop. All three voices will be processed through the
in
same loop simultaneously. The length of one pass through the loop
represents a sixteenth note, two passes an eighth note, etc. The loop time
may be as long or short as suits the music's tempo. The number that
is encoded as the duration represents the number of passes through the
loop.
Since the duration of a note now means the number of passes through
the loop, when the duration is read from the DATA list, it must load
the array with the gated waveform number and pitch controls for each
pass through the loop. On the final pass, it must load a closed gated
waveform to turn off the note.
What this means is that a quarter note A-440 would be represented
in the DATA as 7217,4. After the note has been READ, a series of opera
tions would be performed upon the numbers, resulting in the loading
of three arrays with the following information:
28 49 17
28 49 17
28 49 17
28 49 16
The program is designed so that all the information for Voice 1 is read
in first. Then Voice 2 is read in, and finally Voice 3. The operation is
quite complex. As a result, there is often a 30 second silence before you
hear the music.
The playing portion of the program is the same as our previous pro
gram. The only real difference is that you must use three counters in
stead of one, to keep track of the three arrays.
The BASIC programming language does not operate with the same
speed as machine language programming. The more conditional
statements it must check, therefore, the slower the program will run.
Often, the first note duration of a song played for the first time will
be slightly out of sync. This is because the computer is encountering
the counters and conditional statements for the first time. You can com
pensate for this by programming a 0,1 as the first piece of DATA for
each voice. Then the set-up time will not interfere with the music.
Well talk more about this program further along. For now, type it
in and sit back. The program plays continuously. If you want to end
it, simply hit the RUN/STOP and RESTORE keys.
This program demonstrates three independent voices. The first two
play separate patterns that cycle over and over, while the third plays
Tl2
the melody.
110 1 = 0
120 READ N,DR
200 NEXTK
400 PI = 0 : P2 = 0 : P3 = 0
113
450 PI = Pl + 1 : P2 = P2+1 : P3 = P3 + 1
455 IF D(P1,0) = 0 THEN 2000
114
150 sets the integer portion of HF (high frequency) equal to the frequen
cy number (N) divided by 256; and the integer portion of LF (low
frequency) equal to the frequency number (N) minus the HF number
times 256.
160 instructs the computer, IF the duration number is 1, THEN store the
high and low FQ number in the proper array and store the gated
waveform number in the duration array; increment the note counter
I; go to 120 for the next note.
170 establishes a FOR/NEXT loop that stores notes with durations greater
than 1; fills the arrays with the high and low FQ numbers and a gated
waveform duration for all but the last pass through the time loop of
the note. On each pass, the note counter I is incremented by 1.
180 stores the last beat in the array with the same high and low FQ
numbers, but with a closed gated waveform. This line turns off the
note and begins the RELEASE cycle.
190 increments the note counter I for the last beat stored in Line 180, and
instructs the computer to go back to 120 to read the next note.
200 instructs the computer to go back and READ in the DATA for the
next voice.
250 REM: Sound settings.
300-320 ADSR for three voices.
330 sets the volume. (Optional filter settings should be placed here if used.)
399 REM: Play the music portion of the program.
400 initializes the note counters for Voice 1,2, & 3 to 0.
410-430 POKEs the high and low frequency register for all three voices.
440 POKEs the waveform control registers of all three voices with the
numbers stored in the duration array.
445 establishes time loop for 16th note; plays the note.
450 increments the three counters.
455 checks the next duration for Voice 1; instructs the computer, IF the
value stored in the array is 0, to THEN go to Line 200.
460 checks the next duration for Voice 2; instructs the computer, IF the
value is 0, to THEN go to Line 2500.
465 checks the next duration for Voice 3; instructs the computer, IF the
value is 0, to THEN go to Line 3000.
470 instructs the computer to go back to 412 and play the next 1/16
measure (the next beat).
NOTE—The 0 in the DATA is the last duration value for each voice.
fl5
In the READ portion of the program, this acts as a flag to signal the
end of the READ cycle for each voice. In the play portion, it acts as
a flag to signal an exit from the program. This enables us to manipulate
the order of notes for each voice.
600 REMDATA.
610-640 Voice 1 DATA.
700-710 Voice 2 DATA.
800-820 Voice 3 DATA.
NOTE-The first two pieces of DATA for all three voices are 0,1. This
is so that distortion of the first note, as described earlier, does not result.
2000 manipulates the array for Voice 1; instructs the computer to return
to 460 to check the next voice.
2500 manipulates the array for Voice 3; instructs the computer to return
to 410 to play the next note.
LIST 170
Then, have the computer print out the current value of each variable.
PRINT I
I = 501
This means that you have more elements than can be accommodated
by your array DIM H(500,2). This means, somewhere along the line,
the computer did not reset I to 0 when it was supposed to do so. Did
you place the zeros at the end of the DATA? Were the values properly
entered in the DATA? Did you leave one out? Did you put a period
__
between elements instead of a comma? etc.
Do this with each variable. In this way, you can track down the bug.
Be sure the save the program, even if it does not work properly. Then
you can update the program. There is always the possibility of a crash.
This occurs when the computer is frozen and you can't break out. The
only option is to turn off the computer. If you've saved your work, you're
safe. If not-well, there goes an hour.
Programming multiple voices with independent duration is no easy
trick. It takes time and patience. Use our program as a model, and make
up your own music. Or try and translate some sheet music, if you prefer.
When you've become proficient, try to tailor your own program.
117
34 Real Time Music:
Programming
An Organ Keyboard
Up until now, we have been playing "computer time" music. That is,
we write a program and have the computer play it for us. Your Com
modore 64 has the ability, however, to play "real time" music. You press
a key, and the computer responds with a note. At the time of this writing,
Commodore has announced that it will be releasing a "piano keyboard"
which can be hooked up to your 64. This event, however, is still months
away. The next best thing is to write a program to turn the keyboard
of the computer into an instrument. There have been a few excellent
examples of this type of program in recent computer magazines. There
is also a wonderful program for a keyboard in the appendix of your
Commodore User's Guide. Though a little difficult to understand, it's
well worth the time to type it into your 64 and to save it on disk or tape.
We'd like to add our keyboard program to the growing list, and we'll
show you how you can write one of your own. The beauty of this pro
cess is that you can assign different keys all kinds of functions.
Our program is for a "Chromatic Chord Organ." With it, you have
control of all three voices, a row of keys for each; a two-octave range
for each voice; and a choice of two waveforms. The program allows
you to hear permutations of a three-note chord over the two-octave range.
In addition, you can use two voices as a drone and a third voice as a
melody. This is because the voices remain ON for as long as you wish.
The function keys are used to turn off the sound for each voice in
dependently or all together. To end the program, you press the space bar.
Us
Here is the program. The graphic display at the beginning is purely
optional. We'll show you how the program works after you've played
some "real time" music.
410 Z = 54272
420 P = 0
fl9
500 POKE Z + 5,0 : POKE Z + 6,240
570 IF M = 136 THEN POKE Z + 1,0 : POKE Z,0 : POKE Z + 8,0 : POKE
GOTO 540
GOTO 540
670 1 = 0
680 IF M = A(I) THEN POKE Z + 1 ,D(I,P): POKE Z,D(I,P +1): GOTO 540
690 IF M = B(I) THEN POKE Z + 8,D(I,P): POKE Z + 7,D(I,P +1): GOTO 540
540
710 1 = 1+1
28,49
1010 DATA 29,223,31,165,33,135
1020 DATA 33,135,35,134,37,162,39,223,42,62,44,193,47,107,50,60,53,57,56,
99
120
HOW TO BUILD AN ORGAN KEYBOARD
The GET command instructs the computer to read each key that you
type. The character is then stored in the keyboard buffer. The GET state
ment usually takes this form:
When this line is encountered, the computer stays on the line cycling back
and forth, waiting for you to press a key. When the key is pressed, the com
puter executes whatever commands are linked to that specific key. Usually
it is necessary to have a command to return to the GET A$ line, so that
new information can be processed.
The 64 uses the ASC code to evaluate the character received in the GET
statement. A table of ASC codes for the 64 may be found in the Appen
dices of both the Users' Guide and the Programmer's Reference Guide.
Each key on your computer has an ASC code, which is a number that
specifically relates to that key. An ASC code number is programmed as
ASC(X) where X equals the code number of the desired key. If you check
your table, you will see that ASC(32) means the space bar, while ASC(66)
means the letter A, and so on.
With this knowledge, you can understand the concept that lies behind
this program.
PROGRAM OVERVIEW
This organ program is put together in modules. The first module (Lines
100-280) contains the graphics, which are optional. The next section (Lines
430480) creates arrays to hold the frequency numbers for two full octaves.
In addition, it establishes three more arrays to hold the ASC codes for the
three rows of keys we will be using. All this information is stored in the
DATA bank.
The next module is for permanent sound setting (Lines 490-530).
The following section contains the GET command and a number of con
ditional statements to evaluate the character received (Lines 540-730).
The final module is the "play" portion of the program. It POKEs the
appropriate sound registers with values stored in the arrays (Lines 490-530
and Lines 560-700).
For a more in-depth look, follow this line-by-line program description.
NOTE—All three rows are set to play the same chromatic scale. If the se
cond key of the second row is pressed, the second note of the second voice
is played.
730 instructs the computer, after checking all the conditionals, go back and
GET the next note.
122
900 ends the program.
1000-1010 DATA for low octave high and low FQ numbers.
1020-1030 DATA for upper octave high and low FQ numbers.
1040 DATA ASC codes for top row.
1050 DATA ASC codes for second row.
1060 DATA ASC codes for third row.
The organ takes a bit of time to get used to. Since most people are used
to the major scale and the organ produces the chromatic scale, the notes
do not work the way many people would expect. But figuring out the notes
can be an interesting investigation. This organ offers much opportunity
for unique chordal structures.
Also try the Drone effect. Set up a two-note drone in the lower octave,
using the top two rows. Then, switch to the upper octave, using the Func
tion 8 key. Use the third row for your melody lines. It will remain in the
upper octave. As long as you do not press a key in the top two rows, they
will continue droning in the lower octave.
You can play some beautiful, slow, meditative music using this set-up.
If you want to change the pitches of the keys, you can do so by altering
the DATA in Lines 1000-1030. Different octaves may be selected, or,-if
you prefer, a diatonic, minor or modal scale.
If you alter the DATA in Lines 1000-1030 to the list below, your
chromatic chord organ will be converted into an F Lydian modal organ.
If you use an F and C drone in the top two rows, you have the tuning to
play a NORTH INDIAN CLASSICAL RAGA called YAMAN
KALYAN.
The drone is obtained by pressing («-) on the top row and (T) on the
second row.
Try making up your own organ. When you do, make sure to program
26 notes, no more, no less. That's what it takes for the program to run
properly.
123
SECTION VI
Sounding A Final Note
126
Bibliography
Brook, Barry S. Editor. Musicology and the Computer and Musicology 1966-2000:
A Practical Problem, American Musicological Society/G.N.Y.C., City University
Press: 1965.
Commodore 64 Programmer's Reference Guide, Commodore Business Machines;
Howard W. Sams and Co., Inc.; first edition, third printing: 1983.
Commodore 64 User's Guide, Commodore Business Machines; Howard W. Sams
and Co., Inc.; second edition: 1982.
Howe, Hubert S., Jr., Electronic Music Synthesis, W.W. Norton, New York and
London: 1975.
Lincoln, Harry B., Editor, The Computer and Music, Cornell University Press, Ithaca
and London: 1970.
Mandell, Steven L., BASIC, second edition; Academic Press, Harcourt Brace
Jovanovich; New York: 1982.
The Synthesizer, second edition; Roland Corporation: 1979.
Trythall, Gilbert, Principles and Practice of Electronic Music, Grosset and Dunlap,
New York: 1973.
Von Foerster, Heinz and Beauchamp, James W., Editors, Music By Computers, John
Wiley and Sons, Inc., New York: 1969.
127
SID CHIP CONTROL CHART
BIT no. 7 6 5 4 3 2 1 0
0 54272 LOFQ f 7 f6 f5 f4 f3 f2 f 1 fO
i 54273 HI FQ f 15 f 14 f 13 f 12 f 11 f 10 f9 f8
5 54277 ATK-DEC ATK 3 ATK2 ATK 1 ATK0 DEC 3 DEC 2 DEC1 DEC0
6 54278 SUS-REL SUS3 SUS2 SUS 1 SUSO REL3 REL2 REL 1 REL0
VOICE 2
7 54279 LOFQ f7 f6 f5 f4 f3 f2 f 1 f 1
8 54280 HIFQ f 15 f 14 f 13 f 12 f 11 f 10 f9 f8
VOICE 3
14 54286 LOFQ f7 f6 f5 f4 f3 f2 f 1 fO
IS 54287 HIFQ f 15 f 14 f 13 f 12 f 11 f 10 f9 f8
16 54288 LOPW pw 7 pw 6 pw 5 pw 4 pw3 pw2 pw 1 pwO
17 54289 HIPW ///// ///// ///// ///// pw 11 pw 10 pw9 pw8
18 54290 WAVE FORM NOISE run AM z\ TEST RMOD SYNC GATE
19 54291 , ATK-DEC ATK3 ATK2 ATK 1 ATK0 DEC 3 DEC 2 DEC1 DEC0
20 54292 SUS-REL SUS3 SUS2 SUS1 SUSO REL3 REL2 REL 1 REL0
128
TABLE OF NOTE VALUES
OCTAVE 0 OCTAVE 4
OCTAVE 1 OCTAVE 5
OCTAVE 2 OCTAVE 6
OCTAVE 3 OCTAVE 7
129
O
1 2 3 4 CLR INST
5 6 7 8 9 0 + £ HOME DEL Fl 133
95 49 50 51 52 53 54 55 56 57 48 43 45 92 147 148 F2 137
Q W E R T Y U *
CTRL O P @ t
F3 134
81 87 69 82 84 89 85 73 RESTORE
79 80 64 42 94
F4 138
RUN SHIFT A S D F G H J K L RETURN
STOP F5 135
LOCK 65 83 6i\ 70 71 72 74 75 76 58 59 61 13 F6 139
Z X C V B N M
SHIFT / CRSR CRSR F7 136
90 88 6*3r 86 66 SHIFT
78 77 44 46 47 17 29 F8 140
k
■ 7
"a £..$
«
Are you interested in electronic music? Do you dream of playing
"Rhapsody in Blue" ... without a piano? You can ... with THE
COMMODORE 64 MUSIC BOOK. It will teach you to play your
Commodore 64 as a musical instrument, and you'll pick up profes
sional programming skills along the way.
ISBN 0-8176-3158-5
ISBN 3-7643-3158-5 $14.95