simpleadvancedsheet music
[continue/next section][MAIN/Introduction][table of contents]

Sound and Music in C128 Mode - continued


TABLE OF CONTENTS

7.3 ADVANCED SOUND AND MUSIC IN C128 MODE

7.3.1 A brief background: The Characteristics of Sound
Figure 7-1. Sine Wave.
Figure 7-2. Frequencies and harmonics.
Figure 7-3. Sound Waveform Types.
7.3.2 Making Music on the Commodore 128
7.3.2.1 The ENVELOPE Statement
Figure 7-4. ADSR Phases.
Figure 7-5. Default Parameters for ENVELOPE Statement.
7.3.2.2 The TEMPO Statement
7.3.2.3 The PLAY Statement
Figure 7-6. Sound Synthesizer Control Characters.
7.3.2.4 The SID Filter
Figure 7-7. Low-pass Filter.
Figure 7-8. High-pass Filter.
Figure 7-9. Band-pass Filter.
7.3.2.5 The FILTER Statement
7.3.3 Tying your Music Program Together
7.3.4 Advanced FilteringFigure 7-10. Notch Reject Filter.

7.3 ADVANCED SOUND AND MUSIC IN C128 MODE

7.3.1 A brief background: The Characteristics of Sound

Every sound you hear is actually a sound wave traveling through the air. Like any wave, a sound (sine) wave can be represented graphically and mathematically (see Figure 7-1).

Figure 7-1. Sine Wave.

[ASCII IMAGE]

The sound wave moves (oscillates) at a particular rate (frequency) which determines the overall pitch (the highness or lowness of the sound).

The sound is also made up of harmonics, which are accompanying multiples of the the overall frequency of the sound or note. The combination of these harmonic sound waves give the note its qualities, called timbre. Figure 7-2 shows the relationship of basic sound frequencies and harmonics.

Figure 7-2. Frequencies and harmonics.

[FIGURE IS MISSING]

The timbre of a musical tone (i.e. the way a tone sounds) is determined by the tone's waveform. The Commodore 128 can generate four types of waveforms: triangle, sawtooth, variable pulse and noise. See Figure 7-3 for a graphic representation of these four waveforms.

Figure 7-3. Sound Waveform Types.

NOTE for Lynx user: Follow [this link] to see the three waveforms in ASCII art format.


TRIANGLE


SAWTOOTH


VARIABLE PULSE


NOISE

7.3.2 Making Music on the Commodore 128

7.3.2.1 The ENVELOPE Statement

The volume of a sound changes throughout the duration of the note, from when you first hear it until it is no longer audible. These volume qualities are referred to as Attack, Decay, Sustain and Release (ADSR). Attack is the rate at which a musical note reaches its peak volume. Decay is the rate at which a musical not decreases from its peak volume to its midranged (sustain) level. Sustain is the level at which a musical note is played at its midranged volume. Release is the rate at which a musical note decreases from its sustain level to zero volume. The ENVELOPE generator controls the ADSR parameters of sound. See Figure 7-4 for a graphical representation of ADSR. The Commodore 128 can change each ADSR parameter to 16 different rates. This gives you absolute flexibility over the envelope generator and the resulting properties of the volume when the sound is originated.

Figure 7-4. ADSR Phases.

[ASCII IMAGE]

One of the most powerful Commodore 128 sound statements - the one that controls the ADSR and waveform - is the ENVELOPE statement. The ENVELOPE statement sets the different controls in the synthesizer chip which makes each sound unique. The ENVELOPE gives you the power to manipulate the SID synthesizer. With ENVELOPE, you can select particular ADSR settings and choose a waveform for you own music and sound effects. The format for the ENVELOPE statement is as follows:

ENVELOPE e[,a[,d[,s[,r[,wf[,pw]]]]]]

Here's what the letters mean:

e
envelope number (0-9).
a
attack rate (0-15)
d
decay rate (0-15)
s
sustain level (0-15)
r
release rate (0-15)
wf
waveform
  • 0 = triangle
  • 1 = sawtooth
  • 2 = pulse (square)
  • 3 = noise
  • 4 = ring modulation
pw
pulse width (0-4095)

Here are the definitions of the parameters not previously defined:

Envelope
The properties of a musical note specified by the waveform and the attack, decay, sustain and release setting of the note. For example, the envelope for a guitar not has a different ADSR and waveform than a flute.
Waveform
The type of sound wave created by the combination of accompanying musical harmonics of a tone. The accompanying harmonic sound waves are multiples of, and are based on the overall frequency of the tone. The qualities of the tone generated by each waveform are recognizably different from one another and are represented graphically in Figure 7-3.
Pulse width
The length of time between notes, generated by pulse waveform.

Now you can realize the power of the ENVELOPE statement. It controls most of the musical qualities of the notes being played by the sound synthesizer. The Commodore 128 has 10 predefined envelopes for 10 different musical instruments. In using the predefined envelopes you do not have to specify the ADSR parameters, waveform and pulse width settings - this is already done for you. All you have to do is specify the envelope number. The rest of the parameters are chose automatically by the Commodore 128. Here are the preselected envelopes for different types of musical instruments:

Figure 7-5. Default Parameters for ENVELOPE Statement
Envelope                                               Wave-
Number    Instrument  Attack  Decay  Sustain  Release  form   Width

0         Piano       0       9      0        0        2      1536
1         Accordion   12      0      12       0        1
2         Calliope    0       0      15       0        0
3         Drum        0       5      5        0        3
4         Flute       9       4      4        0        0
5         Guitar      0       9      2        1        1
6         Harpsicord  0       9      0        0        2      512
7         Organ       0       9      9        0        2      2048
8         Trumpet     8       9      4        1        2      512
9         Xylophone   0       9      0        0        0

Now that you have a little background on the ENVELOPE statement, begin another example by entering this statement into your Commodore 128:

10 ENVELOPE 0, 5, 9, 2, 2, 2, 1700

This ENVELOPE statement redefines the default piano envelope (0) to the following: Attack = 5, Decay = 9, Sustain=2, Release=2, waveform remains the same (2) and pulse width of the variable pulse waveform is now 1700. The piano envelope will not take on these properties until it is selected by a PLAY statement, which you will learn later in this section.

The next step in programming music is setting the volume of the sound chip as follows:

20 VOL 8

The VOL statement sets the volume of the sound chip between 0 and 15, where 15 is the maximum and 0 is off (no volume).

7.3.2.2 The TEMPO Statement

The next step in Commodore 128 music programming is controlling the tempo, or speed of your tune. The TEMPO statement does this for you. Here's the format:

TEMPO n
where n is a digit between 0 and 255 (and 255 is the fastest tempo). If you do not specify the TEMPO statement in your program, the Commodore 128 automatically sets the tempo to 8. Add this statement to your musical example program:
30 TEMP 10
7.3.2.3 The PLAY Statement

Now it's time to learn how to play the notes in your song. You already know how the PRINT statement works. You play the notes in your tune the same way as PRINTing a text string to the screen, except you use the PLAY statement in place of PRINT. PRINT outputs text, PLAY outputs musical notes.

Here's the general format for the play statement:

PLAY "string of synthesizer control
characters and musical notes"

The total number of characters (including the musical notes and synthesizer control characters) that can be put into a PLAY command is 255. However, since this exceeds the maximum number of characters (160) allowed for a single program line in BASIC 7.0, you have to concatenate (that is, add together) at least two strings to reach this length. You can avoid the need to concatenate strings by making sure your PLAY commands do not exceed 160 characters, i.e. one program line length. (This is equivalent with four screen lines in 40-column mode, and two screen lines in 80-column mode.) By doing this, you will produce PLAY command strings that are easier to understand and use.

To play musical notes, enclose the letter of the note you want to play within quote. For example, here's how to play the musical scale (also known as do-re-mi-fa-sol-la-si):

40 PLAY "CDEFGAB"

This plays the notes C, D, E, F, G, A and B in the piano envelope, which is envelope 0. After each time you RUN this example program your are creating, hold down the {run/stop} key and press the {restore} key to reset the synthesizer chip.

You have the option of specifying the duration of the note by preceding it in quotes with one of the following notations:

W
Whole note
H
Half note
Q
Quarter note
I
Eighth note
S
Sixteenth note

The default setting, if the duration is not specified, is the Whole (W) notes.

You can PLAY a rest (no sound) by including the following in the PLAY string:

R
Rest

You can instruct the computer to wait until all voices currently playing reach the end of a measure by including the following in quotes:

M
Wait for end of measure

The Commodore 128 also has synthesizer control characters you can include in a PLAY string. This gives you absolute control over each note and allows you to change synthesizer controls within a string of notes. Follow the control character with a number in the allowable range for that character. The control characters and the range of numbers are shown in Figure 7-6. The {n} following the control character refers to the number you select from the specified range.

Figure 7-6. Sound Synthesizer Control Characters.
Control                              Default
Character    Description    Range    Setting
----------------------------------------------------
V n          Voice          1-3      1
O n          Octave         0-6      4
T n          Envelope       0-9      0
U n          Volume         0-15     9
X n          Filter         0=off    0
                            1=on

Although the SID chip can process these control characters in any order, for best results, place the control characters in your string in the order that they appear in Figure 7-6.

You don't absolutely have to specify any of the control characters, but you should to maximize the power from your synthesizer. The Commodore 128 automatically sets the synthesizer controls to the default settings in Figure 7-6. If you don't assign special control characters, the SID chip can PLAY only one envelope, one voice and one octave without any FILTERing. Specify the control characters to exercise the most control over the notes within your PLAY string.

If you specify an ENVELOPE statement and select your own settings instead of using the default parameters from Figure 7-5, the envelope control character number in your PLAY string must match the number in your ENVELOPE statement in order to assume the parameters you assigned. You don't have the specify the ENVELOPE statement at all if you just want to PLAY the default settings from Figure 7-6. In this case, simply select an envelope number with the (T) control character in the PLAY statement.

Here's an example of the PLAY statement using the SID chip control characters within a string. Add this line to your program and notice the difference between this statement and the PLAY statement in line 40.

50 PLAY "M V2 O5 T7 U5 X0 C D E F G A B"

This statement PLAYs the same notes as in line 40, but voice 2 is selected, the notes are played one octave higher (5) than line 40, the volume setting is turned down to 5 and the FILTER is specified as off. For now, leave the filter off. When you learn about FILTERing in the next section, you can come back and turn the filter on to see how it affects the notes being played. Notice line 50 selects a new instrument, the organ envelope, with the T7 control character. Now your program PLAYs two different instruments in two of the independent voices. Add this statement to PLAY the third voice:

60 PLAY "M V3 O6 U7 T6 X0 C D E F G A B"

Here's how line 60 controls the synthesizer. The V3 selects the third voice, O6 places voice 3 one octave higher (6) than voice two, T6 selects the harpsichord envelope, U7 sets the volume to 7 and X0 leaves the filter off for all three voices. Now your program plays three voices, each one octave higher thant the other, in three seperate instruments, piano, organ and harpsichord.

So far, your PLAY statements only played whole notes. Add notes of different duration by placing control characters in you PLAY string as follows:

70 PLAY "MV2O6T0U7X0HCDQEFIGASB"

Line 70 PLAYs voice 2 in octave 6 at volume level 7 with the redefined piano envelope (0) on and filter turned off. This statement PLAYs the note C and D as half notes, E and F as quarter notes, G and A as eighth notes and B as a sixteenth note. Notice the difference between the piano envelope in line 40 and the redefined piano envelope in line 70. Line 40 actually sounds more like a piano than line 70.

You can PLAY sharp, flat and dotted notes by preceding the notes within quotes with the following characters:

#
Sharp (half a tone higher).
$
Flat (half a tone lower).
.
Dotted (half a duration longer).

A dotted note plays one-and-a-half times the duration of a note that is not dotted.

Now try adding sharp, flat and dotted notes with this statement:

80 PLAY "MV1O4T4U8X0.HCDQ#EFI$GA.S#B"

Line 80 PLAYs voice 1 in octave 4 at volume level 8 with the flute envelope turned on and the filter turned off. It also PLAYs C and D as dotted half notes, E and F as sharp quarter notes, G and A as flat eighth notes and B as a sharp dotted sixteenth note. You can add rests (R) at any place within your PLAY string.

Up until now your statement examples have left the filter off within the sound synthesizer and have not realized the true power behind it. Now that you have digested most of the sound and music statements and the SID control characters, move on to the next section to learn how to enhance your musical quality with the FILTER statement.

7.3.2.4 The SID Filter

Once you have selected the ENVELOPE, ADSR, VOLume and TEMPO, use the FILTER to perfect your synthesized sounds. In your program, the FILTER statement will precede the PLAY statement. First you should become comfortable with generating the sound and worry about FILTERing last. Since the SID chip has only one filter, it applies to all three voices. Your computerized tunes will play without FILTERing, but to take full advantage of your music synthesizer, use the FILTER statement to increase the sharpness and quality of the sound.

In the first paragraph of this section (The Characteristics of Sound) we defined a sound as a sound wave traveling (oscilating) through the air at a particular rate. The rate at which a sound oscillates is called the wave's frequency. Recall that a sound wave is made up of an overall frequency and accompanying harmonics, which are multiples of the overall frequency. See Figure 7-2. The accompanying harmonics give the sound its timbre, the qualities of the sound which are determined by the waveform. The filter within the SID chip gives you the ability to accent and eliminate the harmonics of a waveform and change its timbre.

The SID chip filters sounds in three ways: low-pass, high-pass and bandpass filtering. These filters are additive, meaning you can use more than one filter at a time. This is discussed in the next section. Low-pass filters out frequencies above a certain level you specify, called the cutoff frequency. The cutoff frequency is the dividing line that marks the boundery of which frequency level will be played and which will not. In low-pass filtering, the SID chip plays all frequencies below the cutoff frequency and filters out the frequencies above it. As the name implies, the low frequencies are allowed to pass through the filter and high ones are not. The low-pass filter produces full, solid sounds. See Figure 7-7.

Figure 7-7. Low-pass Filter.

[ASCII IMAGE]

Conversly, the high-pass filter allows all frequencies above the cutoff frequency to pass through the chip. All the ones below it are filtered out. See Figure 7-8. The high-pass filter produces tinny, hollow sounds.

Figure 7-8. High-pass Filter.

[ASCII IMAGE] The band-pass filter allows a range of frequencies partially above and below the cutoff frequency to pass through the SID chip. All other frequen- cies above and below the band surrounding the cutoff frequency are filtered out. See Figure 7-9.

Figure 7-9. Band-pass Filter.

[ASCII IMAGE]

7.3.2.5 The FILTER Statement

The FILTER statement specifies the cutoff frequency, the type of filter being used and the resonance. The resonance is the peaking effect of the sound wave frequency as it approaches the cutoff frequency. The resonance determines the sharpness and clearness of a sound: the higher the resonance, the sharper the sound.

This is the format of the FILTER statement:

FILTER cf, lp, bp, hp, res

Here's what the parameters mean:

cf
Cutoff frequency (0 - 2047)
lp
Low-pass filter (0=off, 1=on)
bp
Band-pass filter (0=off, 1=on)
hp
High-pass filter (0=off, 1=on)
res
Resonance (0 - 15)

You can specify the cutoff frequency to be any value between 0 and 2047. Turn on the low-pass filter by specifying a 1 as the second parameter in the FILTER statement. Turn on the band-pass filter by specifying a 1 as the third parameter and enable the high-pass filter with a 1 in the fourth parameter position. Turn off any of the three filters by placing a 0 in the respective position of the filter you want to disable. You can enable or disable one, two or all three of the filters at the same time.

Now that you have some background on the FILTER statement, add this line to your sound program, but do not RUN the program yet:

45 FILTER 1200, 1, 0, 0, 10

Line 45 sets the cutoff frequency at 1200, turns on the low-pass filter, disables the high-pass and band-pass filters and assigns a 10 to the resonance level. Now go back and turn the filter on in your PLAY statements by changing all the X0 control characters to X1. Reset the sound chip by pressing the {run/stop} and {restore} keys and RUN your sound program again. Notice the difference between the way the notes sound and how they sounded without the filter. Change line 45 to:

45 FILTER 1200, 0, 1, 0, 10

The new line 45 turns off the low-pass filter and enables the band-pass filter. Press {run/stop} and {restore} and RUN your sound program again. Notice the difference between the low-pass and band-pass filters. Change line 45 again to:

45 FILTER 1200, 0, 0, 1, 10

Reset the sound chip and RUN your example program again. Notice the difference between the high-pass filter and the low-pass and band-pass filters. Experiment with different cutoff frequencies, resonance levels and filters to perfect the music and sound in you own programs. 7.3.3 Tying your Music Program Together

Your first musical program is complete. Now you can program your favorite songs. Let's tie all the components together. Here's the program listing. Don't be alarmed, this is the same program you built in this section except the print statements are added so you know which program lines are being played.

10 ENVELOPE 0,5,9,2,2,2,1700
15 VOL 8
20 TEMPO 10
25 PRINT "LINE 30"
30 PLAY "CDEFGAB"
35 FILTER 1200,0,0,1,10
40 PRINT "LINE 45 - FILTER OFF"
45 PLAY "V2 O5 T7 U5 X0 CDEFGAB"
50 PRINT "SAME AS LINE 45 - FILTER ON"
55 PLAY "V2 O5 T7 U5 X0 CDEFGAB"
60 PRINT "LINE 65 - FILTER OFF"
65 PLAY "V3 O6 T6 U7 X0 CDEFGAB"
70 PRINT "SAME AS LINE 65 - FILTER ON"
75 PLAY "V3 O6 T6 U7 X1 CDEFGAB"
80 PRINT "LINE 85 - FILTER OFF"
85 PLAY "V2 O6 T0 U7 X0 HCD QEF IGA SB"
90 PRINT "SAME AS LINE 85 - FILTER ON"
95 PLAY "V2 O6 T0 U7 X1 HCD QEF IGA SB"
100 PRINT "LINE 105 - FILTER OFF"
105 PLAY "V1 O4 T4 U8 X0 H.CD Q#EF I$GA S.B"
110 PRINT "SAME AS LINE 105 - FILTER ON"
115 PLAY "V1 O4 T4 U8 X1 H.CD Q#EF I$GA S.B"

Line 10, the ENVELOPE statement, specifies the envelope for the piano (0), which sets the attack to 5, decay to 9, sustain to 2 and release to 2. It also selects the variable pulse waveform (2), with a pulse width of 1700. Line 15 sets the VOLume to 8. Line 20 chooses the TEMPO to be 10.

Line 35 FILTERs the notes that are played in lines 30 to 115. It sets the FILTER cutoff frequency to 1200. In addition, line 35 turns off the low-pass and band-pass filters with the two zeros following the cutoff frequency (1200). The high-pass filter is turned on with the 1 following the two zeros. The resonace is set to 10 by the last parameter in the FILTER statement.

Line 30 PLAYs the notes C, D, E, F, G, A, B in that order. Line 45 PLAYs the same notes as line 30, but it specifies the SID control character U5 as volume level 5, V2 as voice 2 and O5 as octave 5. Remember, the SID control characters allow you to change the synthesizer controls within a string and exercise the most control over the synthesizer. The control character T7 selects the organ envelope. Line 65 specifies the control characters U7 for volume level 7, V3 for voice 3, O6 for octave 6, T6 for the harpsichord envelope and X0 to turn off the filter. Line 65 PLAYs the same notes as line 30 and 45, but in a different volume, voice, octave and instrument envelope.

Line 85 has the same volume, voice, octave and envelope as line 65, and it specifies half notes for the notes C and D, quarter notes for the notes E and F, eighth notes for the notes G and A and a sixteenth note for the B note. Line 105 sets the volume at 8, voice 1, octave 4, flute envelope (4) and turns off the filter. It also specifies the C and D notes as dotted half notes, E and F as sharp quarter notes, G and A as flat eighth notes and B as a dotted sixteenth note. Line 115 is the same as line 105, but with the filter turned on.

7.3.4 Advanced Filtering

Each of the previous FILTERing examples used only one filter at a time. You can combine the SID chip's three filters with each other to achieve different filtering effects. For example, you can enable the low-pass and high-pass filters at the same time to form a notch reject filter. A notch reject filter allows the frequencies below and above the cutoff to pass through the SID chip, while frequencies close to the cutoff frequency are filtered. See Figure 7-10 for a graphical representation of a notch reject filter.

Figure 7-10. Notch Reject Filter.

[ASCII IMAGE]

You can also add either the low-pass or high-pass filter to the band-pass filter to obtain interesting effects. By mixing the band-pass filter with the low-pass filter, you can select the band of frequencies beneath the cutoff frequency and below. The rest are filtered out.

By mixing the band-pass and the high-pass filters, you can select the band of frequencies above the cutoff frequency and higher. All frequencies below the cutoff frequency are filtered out.

Experiment with the different combinations of filters to see all the different types of accents you can place on your musical notes and sound effects. The filters are designed to perfect the sounds created by other components of the SID chip. Once you have created the musical notes or sound effects with the SID chip, go back and add FILTERing to your programs to make them as crisp and clean as possible.

Now you have all the information you need to write your own musical programs in Commodore 128 BASIC. Experiment with the different waveforms, ADSR settings, TEMPOs and FILTERing. Look in a book of sheet music and enter the notes from a musical scale in sequence within a play string. Accent the notes in the string with the SID control characters. You can combine your Commodore 128 Music Synthesizer with C128 mode graphics to make your own videos or "movies", complete with sound tracks.

[continue with next part]

[top of document]

page URL: www.bigfoot.com/~c128page/c128sg/sect-07b.htm
contact: c128page@bigfoot.com