• Aucun résultat trouvé

isters, there are two locations used for pulse width selection in '—'

each voice. Voice l's pulse width registers, for instance, are at addresses 54274 and 54275. However, there are only four bits available in the second register, so the numbers stored in these locations define a binary number between 0 and 4095. Just as in the pitch registers, the number represented by the two reg isters is equal to the lower value plus the upper value mul tiplied by 256. For example, if the lower register contains a 0, and the upper register an 8, the value would be:

0 + (8 x 256) = 2048

This number signifies the proportion of the wave that will be high. The above value would be exactly 50 percent (2048/

4096 = .50), creating a square waveform. Decreasing or increas ing the number represented by the values in these registers will decrease or increase the proportion of the wave that is high and thus change the sound produced. If you used the following statement, for instance, the wave would be high only 25 percent of the time.

POKE 54274,0:POKE 54275,4

A sustain loop is usually added to a sound routine so that it plays longer. You can force a note to play indefinitely by simply not turning the gate bit off, or by turning the gate bit off manually, perhaps with a function key defined else where in the program. But the easiest way to set the length of the note's sustain level is to use a FOR-NEXT loop as a delay.

This loop should be inserted between the time you set the \ j waveform (and thus turn on the gate bit) and the time when

you turn the gate bit off. If you were using the pulse wave

form, and had already set the other sound parameters earlier j j in the routine, it could look like this:

POKE 54276,65 Set pulse waveform and turn on gate bit ™

FOR T=0 TO 1000:NEXT Lj

Delay loop to play the sound for one second

POKE 54276,64 Turn off gate bit {J

18 _

LJ

1

SID: The Sound Interface Device

Keep in mind that sustain refers only to a volume level, not to a function of time, as the other elements of a sound's ADSR do. That's why you need something like a delay loop in your routines; it's the simplest way to control the time of the sus tain part of a note.

Turning the gate bit off is the last thing you'll do in most of your sound routines. Once the note has played—

having gone through its attack, decay, sustain, and release steps—you need to turn the gate bit off by POKEing the waveform control register with a value one less than when you selected the waveform and turned the gate bit on. If you earlier POKEd location 54276 with 65 (to turn on the pulse waveform generator and enable the gate bit), now you'd POKE the same location with 64. This will turn off the gate bit, ending the sustain and bringing on the release section of the note's sound envelope. As soon as the release finishes, the sound is completed.

Once you understand the process you need to go through to create sounds with the SID chip, you're ready to produce your first note. Here's a routine that clears the sound control registers; sets the volume, ADSR, frequency, and waveform for the note; enables the gate bit; keeps the note playing; and turns the gate bit off. It's simple, but it's a start.

Program 1-1 • Simple Sound

For mistake-proof program entry, be sure to read "Automatic Proofreader," Appendix G 10 REM CLEAR SOUND REGISTERS :rem 251 20 FOR R=54272 TO 54296:POKER,0:NEXT :rem 25

30 REM - TURN ON VOLUME - :rem 95

40 POKE 54296,15 :rem 47

50 REM - INITIALIZE SPECIAL REGISTERS - :rem 78 60 POKE 54277,0:POKE 54278,240:POKE 54275,8 :rem 3 70 REM - POKE TONE VALUES INTO VOICE 1 :rem 142 80 POKE 54272,48: POKE 54273,28 :rem 55 90 REM - ENABLE TONE REGISTER - :rem 233

100 POKE 54276, 65 :rem 95

110 REM - PLAY TONE FOR 1 SECOND - :rem 16

120 FOR R=0 TO 1000: NEXT :rem 22

130 REM - TURN SOUND REGISTER OFF - :rem 228

140 POKE 54276,64 :rem 98

150 REM - TURN VOLUME OFF - :rem 208

160 POKE 54296,0 :rem 44

19

1

SID: The Sound Interface Device

Let's go through this short routine line by line to see how the sound is produced. Line 20 clears the 24 sound control reg isters, using a FOR-NEXT loop to POKE each register with 0.

The volume is turned on in line 40 and is set to its maximum value, 15. Line 60 initializes the ADSR registers, as well as the pulse width register. Attack and decay are both set to 0, indicating the fastest possible rate, by POKEing location 54277 with 0. By POKEing 240 into location 54278, you're signaling the computer to use a high sustain level, and a low release rate. The sound will continue after the decay at a high vol ume, but will end quickly. Because you're using the pulse waveform in this routine, you also need to set the pulse width register (location 54275) for voice 1. Frequency values are POKEd into the address for voice 1, locations 54272 and 54273 in line 80. By looking at Table 1-3, you can see that these values will create an A note in the fifth octave. Line 100 turns on the gate bit (by setting bit 0 to 1) and selects the pulse waveform (by setting bit 6). The value POKEd into this location is the total of those two bits' values (1 + 64). See Table 1-1 for the bits to set for these registers. A short delay loop keeps the note playing in line 120, and then the gate bit is disabled in line 140. The volume register is turned off in line 160 by POKEing a 0 value into it.

Don't be discouraged if some of this is confusing to you.

Although it may seem complicated, you'll soon see how it all fits together. But it's nice to hear a created sound, even if you're not sure how it all was done. It's relatively simple to modify various registers to change the sound. For instance, by changing the values in the upper and/or lower pitch registers (54272 and 54273), you can change the tone played. Program 1-2 does this.

Program 1-2. Modifying Upper/Lower Tones

For mistake-proof program entry, be sure to read "Automatic Proofreader," Appendix C 10 REM CLEAR SOUND REGISTERS :rem 251 20 FOR R=54272 TO 54296:POKER,0:NEXT :rem 25

30 REM - TURN ON VOLUME - :rem 95

40 POKE 54296,15 :rem 47

50 REM - INITIALIZE SPECIAL REGISTERS - :rem 78 60 POKE 54275,8:POKE 54277,0:POKE 54278,240 :rem 3 70 REM - GET TONE FROM TONE TABLE - :rem 141 75 READ L,H: IF L=999 THEN RESTORE: GOTO 75

:rem 119 20

1

SID: The Sound Interface Device

n n H

77 REM - POKE TONE VALUES INTO VOICE 1 :rem 149 80 POKE 54272,L: POKE 54273,H :rem 245 90 REM - ENABLE TONE REGISTER - :rem 233

100 POKE 54276, 65 :rem 95

110 REM - PLAY TONE FOR 1/8 SECOND - :rem 119

120 FOR R=0 TO 125: NEXT :rem 237

130 REM - TURN SOUND REGISTER OFF - :rem 228

140 POKE 54276,64 :rem 98

150 REM - TURN VOLUME OFF - :rem 208

160 GOTO 75 :rem 60

165 POKE 54296,0 :rem 49

170 REM - TONE TABLE - :rem 116

180 DATA 195,16,194,17,208,18,238,19,30,21,95,22,1 80,23,29,25,155,26,48,28,221 :rem 15 190 DATA 29,164,31,134,33,164,31,221,29,48,28,155, 26,29,25,180,23,95,22,30,21 :rem 204 195 DATA 238,19,208,18,194,17,999,999 :rem 83 This program uses a number of DATA statements contain ing the tone values for a musical scale. Each time a tone is to be played, a pair of values is READ from the DATA state ments. These are the values used to POKE into the registers at locations 54272 and 54273. Notice that there are an even number of values in the DATA statements beginning in line 180. If you group the numbers in pairs, you will see that the first is the low pitch value and the second is the high pitch value. If you modify this routine by entering data of your own, make sure that there is an even number of values, and that the low pitch value precedes the high pitch value. Line 80 actually places these values in the registers. When all the numbers have been READ, the routine in line 75 RESTORES the DATA and starts again. To stop this routine, press the RUN/STOP and RESTORE keys at the same time.