Make your own free website on Tripod.com

SECTION 7

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

7. Sound and Music in C128 Mode


TABLE OF CONTENTS

7.1 INTRODUCTION

7.2THE SOUND STATEMENT

7.2.1 Writing a SOUND Program
7.2.2 Random Sounds

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.4 CODING A SONG FROM SHEET MUSIC

Figure 7-11. Musical Staff.
Figure 7-12. Part of Bach's Invention 13.
Figure 7-13. Synchronizing Notes for Two Voices.

7.1 INTRODUCTION

The Commodore 128 has one of the most sophisticated built-in sound synthesizers available in a microcomputer. The synthesizer, called Sound Interface Device (SID), is a chip capable of producing three independent voices (sounds) simultaneously. Each of the voices can be played in one of four types of sounds, called waveforms. The SID chip also has programmable Attack, Decay, Sustain and Release (ADSR) parameters. These parameters define the quality of a sound. In addition, the synthesizer has a filter you can use to choose certain sounds. In this section you will learn how to control these parameters to produce almost any kind of sound.

To make it easy for you to select and manipulate the many capabilities of the SID chip, Commodore has develloped new and powerful BASIC music statements.

Here are the new sound and music statements available on the Commodore 128:

  • SOUND
  • ENVELOPE
  • VOL
  • TEMPO
  • PLAY
  • FILTER

This section explains these sound statements, one at a time, in the process constructing a sample musical program. When you are finished with this section, you will know the ingredients that go into a musical program. You'll be able to expand on the example and write programs that play intricate musical compositions. Eventually, you'll be able to program your own musical scores, make your own sound effects and play works of the great classical masters such as Beethoven and contemporary artists like the Beatles. You can even add computer-generated music to your graphics programs to create your own "videos."

7.2 THE SOUND STATEMENT

The SOUND statement is designed primarily for quick and easy sound effects in your programs. You will learn a more intricate way of playing complete musical arragements with the other sound statements later in this section.

The format for the SOUND statement is as follows:

SOUND vc, freq, dur [, dir [, min [, sv [, wf [, pw]]]]]

Here's what the parameters mean:

VC
Select voice 1, 2 or 3
FREQ
Set the frequency level of sound (0-65535)
DUR
Set duration of the sound (in sixtieths of a second)
DIR
Set the direction in which the sound is incremented/decremented:
  • 0 = Increment the frequency upward
  • 1 = Decrement the frequency downward
  • 2 = Oscillate the frequency up and down
MIN
Select the minimum frequency (0-65535) if the sweep (DIR) is specified
SV
Choose the step value for the sweep (0-65535)
WF
Select the waveform (0-3):
  • 0 = Triangle
  • 1 = Sawtooth
  • 2 = Variable Pulse (square)
  • 3 = White Noise
PW
Set the pulse width, the width of the variable pulse waveform

Note that the DIR, MIN, SV and PW parameters are optional.

The first parameter (VC) in the SOUND statement selects which voice will be played. The second parameter (FREQ) determines the frequency of the sound, which ranges from 0 through 65535. The third setting (DUR) is measured in 60ths of a second. If you want to play a sound for one second, set the duration to 60, since 60 times 1/60 equals 1. To play the sound for two seconds, specify the duration to be 120. To play the sound ten seconds, make the duration 600, and so on.

The fourth parameter (DIR) selects the direction in which the frequency or the sound is incremented or decremented. This is referred to as the sweep. The fifth setting (MIN) sets the minimum frequency where the sweep begins. The sixth parameter (SV) is the step value of the sweep. It is similar to the step value in a FOR... NEXT loop. If the DIR, MIN and SV values are specified in the SOUND command, the sound is played first at the original level specified by the freq parameter. Then the synthesizer sweeps through and plays each level of the entire range of frequency values starting at the min frequency. The sweep is incremented or decremented by the step value (SV) and the frequency is played at the new level.

The seventh parameter (WF) in the SOUND command selects the waveform for the sound. (Waveforms are explained in detail in paragraph titled, "Advanced Sound and Music in C128 Mode.")

The final setting in the SOUND command determines the width of the pulse waveform if it is selected as the waveform parameter. (See the discussion in paragraph 7.3, "Advanced Sound and Music in C128 Mode" discussion for an illustration of the pulse waveform.)

7.2.1 Writing a SOUND Program

Now it's time to write your first SOUND program. Here's an example of the SOUND statement:

10 VOL 5
20 SOUND 1,4096,60

RUN this program. The Commodore 128 plays a short beep. You must set the volume before you can play the sound statement, so line 10 sets the VOLume of the sound chip. Line 20 plays voice 1 at a frequency of 4096 for a duration of 1 second (60 times 1/60). Change the frequency with this statement:

30 SOUND 1,8192,60

Notice line 30 plays a higher tone than line 20. This shows the direct relationship between the frequency setting and the actual frequency of the sound. As you increase the frequency setting, the Commodore 128 increases the pitch of the tone. Now try this statement:

40 SOUND 1,0,60

This shows that a FREQ value of 0 plays the lowest frequency (which is so low it is inaudible). A FREQ value of 65535 plays the highest possible frequency.

Now try placing the sound statement within a FOR... NEXT loop. This allows you to play the complete range of frequencies within the loop. Add these statements to your program:

50 FOR I=1 TO 65535 STEP 100
60 SOUND 1,I,1
70 NEXT

This program segment plays the variable pulse waveform in the range of frequencies from 1 to 65535, in increments of 100, from the lowest frequency to the highest. If you don't specify the waveform, the computer selects the default value of waveform 2, the variable pulse waveform.

Now change the waveform with the following program line (60) and try the program again:

60 SOUND 1,I,1,0,0,0,0,0

Now the program plays voice 1, using the triangle waveform, for the range of frequency between 1 and 65535 in increments of 100. This sounds like a typical sound effect in popular arcade games. Try waveform 1, the sawtooth waveform, and see how it sounds with this line:

60 SOUND 1,I,1,0,0,0,1,0

The sawtooth waveform sounds similar to the triangle waveform though it has less buzz. Finally, try the white noise waveform (3). Substitute line 60 for this line:

60 SOUND 1,I,1,0,0,0,3,0

Now the program loop plays the white noise generator for the entire range of frequencies. As the frequency increases in the loop the pitch increases and sounds like a rocket taking off.

Notice that so far we have not specified all of the parameters in the SOUND statement. Take line 60 for example:

60 SOUND 1,I,1,0,0,0,3,0

The three zeros following 1,I,1 pertain to the sweep parameters within the SOUND statement. Since none of the parameters is specified, the SOUND does not sweep. Add this line to your program:

     100 SOUND 1, 49152, 240, 1, 0, 100, 1, 0
               ^    ^     ^   ^  ^   ^   ^  ^
               |    |     |   |  |   |   |  |
Voice ---------+    |     |   |  |   |   |  |
Frequency ----------+     |   |  |   |   |  |
Duration -----------------+   |  |   |   |  |
Sweep Direction --------------+  |   |   |  |
Minimum Sweep Frequency ---------+   |   |  |
Step Value for Sweep ----------------+   |  |
Waveform --------------------------------+  |
Pulse Width for variable width              |
   waveform --------------------------------+

Line 100 starts the sweep frequency at 49152 and decrements the sweep by 100 in the downward direction, until it reaches the minimum sweep frequency at 0. Voice 1, using the sawtooth waveform (#1), plays each SOUND for four seconds (240 * 1/60 s). Line 100 sounds like a bomb dropping, as in may "shoot 'em up" arcade games.

Now try changing some of the parameters in line 100. For instance, change the direction of the sweep to 2 (oscilate); change the minimum frequency of the sweep to 32768; and increase the step value to 3000. Your new SOUND command looks like this:

110 SOUND 1,49152,240,2,32768,3000,1

Line 110 makes a siren sound as though the police were right on your tail. For a more pleasant sound, try this:

110 SOUND 1,65535,250,0,32768,3000,2,2600

This should remind you of a popular space-age TV show, when the space crew unleashed their futuristic weapons on the unsuspecting aliens.

Until now, you have been programming only one voice. You can produce interesting sound effects with the SOUND statement using up to three voices. Experiment and create a program which utilizes all three voices.

Here's a sample program that will help you understand how to program the Commodore 128 synthesizer chip. The program, when RUN, asks for each parameter, and then plays the sound. Here's the program listing. Type it into your computer and RUN it.

10 SCNCLR:PRINT "           SOUND PLAYER":PRINT:PRINT:PRINT
20 PRINT "     INPUT SOUND PARAMETERS TO PLAY":PRINT:PRINT
30 INPUT "VOICE (1-3)";V
40 INPUT "FREQUENCY (0-65535)";F
50 INPUT "DURATION (0-32767)";D
60 INPUT "DO YOU WANT TO SPECIFY OPTIONAL PARAMETERS Y/N";B$:PRINT
70 IF B$="N" THEN 130
80 INPUT "SWEEP DIRECTION (0=UP,1=DOWN,2=OSCILL)";DIR
90 INPUT "MINIMUM SWEEP FREQUENCY (0-65535)";M
100 INPUT "SWEEP STEP VALUE (0-32767)";S
110 INPUT "WAVEFORM (0=TRI,1=SAW,2=VAR PUL,3=NOISE)";W
120 IF W=2 THEN INPUT "PULSE WIDTH (0-4096)";P
130 SOUND V,F,D,DIR,M,S,W,P
140 INPUT"DO YOU WANT TO HEAR THE SOUND AGAIN Y/N";A$
150 IF A$="Y" THEN 130
160 RUN

Here's a quick explanation of the program. Lines 10 and 20 PRINT the introductory messages on the screen. Lines 30 through 50 INPUT the voice, frequency and duration parameters. Line 60 asks if you want to enter the optional SOUND parameters, such as the sweep settings and waveform. If you don't want to specify these parameters, press the {n} and then the {return} key and the program jumps to line 130 and plays the sound. I you do want to specify the optional SOUND settings, press the {y} and then the {return} key and the program continues with line 80. Lines 80 through 110 specify the sweep direction, minimum sweep frequency, sweep step value and waveform. Line 120 INPUTs the pulse width of the variable pulse waveform only if waveform 2 (variable pulse) is selected. Line 130 plays the SOUND according to the parameters that you specified earlier in the program.

Line 140 asks if you want to hear the SOUND again. If you do, press the {y} and then the {return} key. If you did, program control is returned to line 130 and the program plays the SOUND again. If you press any other key as the {y} key, the program continues with line 160. Line 160 reruns the program. To stop the Sound Player program, press the {run/stop} and {restore} keys at the same time.

7.2.2 Random Sounds

The following program generates random sounds using the RND function. Each SOUND parameter is calculated randomly. Type the program into your computer and SAVE it and RUN it. This program illustrates how many thousands of sounds you can produce by specifying various combinations of the SOUND parameters.

10 PRINT "VC  FRQ  DIR  MIN   SV   WF   PW ":VOL 5
20 PRINT "---------------------------------"
30 V=INT (RND (1)*3)+1                  :REM VOICE
40 F=INT (RND (1)*65536)                :REM FREQUENCY
50 D=INT (RND (1)*240)                  :REM DURATION
60 DIR=INT (RND(1)*3)                   :REM STEP DIRECTION
70 M=INT (RND (1)*65536)                :REM MINIMUM FREQUENCY
80 S=INT (RND (1)*32678)                :REM STEP VALUE
90 W=INT (RND (1)*4)                    :REM WAVEFORM
100 P=INT (RND (1)*4096)                :REM PULSE WIDTH
110 PRINT V;F;DIR;M;S;W;P:PRINT:PRINT   :REM DISPLAY VALUE
120 SOUND V,F,D,DIR,M,S,W,P             :REM PLAY SOUND
130 SLEEP 4                             :REM WAIT A BIT
140 SOUND V,0,0,DIR,0,0,W,P             :REM SWITCH SOUND OFF
150 GOTO 10

Lines 10 and 20 PRINT parameter column headings and the underline. Lines 30 through 100 calculate each SOUND parameter within its specific range. For example, line 30 calculates the voice number as follows:

30 V = INT(RND(1)*3)+1

The notation RND(1) specifies the seed value of the random number. The seed is the base number generated by the computer. The 1 tells the computer to generate a new seed each time the command is encountered. Since the Commodore 128 has three voices, the notation * 3 tells the computer to generate a random number within the range 0 through 3. Notice however there is no voice 0, so the + 1 tells the computer to generate a random number such that 1 <= Number < 4. The procedure for generating a random number in a specific range is to multiply the given random number times the maximum value of the parameter (in this case, 3). If the minimum value of the parameter is greater than zero, add to the random number a value that will specify the minimum value of the range of numbers you want to generate (in this case, 1). For instance, line 40 generates a random number such that 0 <= Number < 65535. Since the minimum value is zero in this case, you do not need to add a value to the generated random number.

Line 110 PRINTs the values of the parameters. Line 120 plays the SOUND specified by the random numbers generated in lines 30 through 100. Line 130 delays the program for 4 seconds while the sound is playing. Line 140 turns off the SOUND (after the 4 seconds delay). All sounds generated by this program are all turned off after 4 seconds with line 140. Finally, line 150 returns control to line 10, and the process is repeated until you press the {run/stop} and {restore} keys at the same time.

So far you have experimented with sample programs using only the SOUND statement. Although you can use the SOUND statement to play musical scores, it is best suited for quick and easy sound effects like the ones in the dogfight program. The Commodore 128 has other statements designed specifically for song playing. The following paragraphs describe the advanced sound and music statements that enable you to play complex musical scores and arrangements with your Commodore 128 synthesizer.

[continue with next part]

[top of document]

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