Sound with CALL LOAD
TI BASIC and Extended BASIC use the subprogram CALL SOUND to generate sounds.
It is also possible -and potentially more powerful- to use CALL LOAD.
For example, using either TI Basic with Mini Memory plugged in, or Extended BASIC with memory expansion:
10 CALL INIT 20 S=-31744 30 CALL LOAD(S,137,"",S,63,"",S,145,"",S,228,"",S,250) 40 GOTO 40
Notice the sound does not terminate. We have to turn it off, either by using CALL LOAD to set Tone 1 and Noise volume to zero... or use a negative CALL SOUND:
CALL SOUND(-1,110,0)
Now for the theory.
For sound we use memory address decimal -31744
To use a tone, two bytes are passed. To use a noise or to set volume, one byte is passed. Each byte has to be looked at as 8 bits, as follows:
NOISE: The 8 bits are as followsBIT: | 1 | 2 3 4 | 5 | 6 | 7 8 |
Means: | always 1 | operation | always 0 |type | shift rate |
TONES: FIRST BYTE
BIT: | 1 | 2 3 4 | 5 6 7 8 |
Means: | always 1 | operation | Frequency - 4 LSB |
TONES: SECOND BYTE
BIT: | 1 | 2 | 3 4 5 6 7 8 |
Means | always 0 | Always 0 | Frequency - 6 M.S.B. |
VOLUME:
BIT: | 1 | 2 3 4 | 5 6 7 8 |
Means: | always 1 | Operation | Attenuation 4 M.S.B. |
M S B means Most Significant Bits L S B means Least Significant Bits
In BASIC sound is used with CALL SOUND in the format CALL SOUND(time,freq1,vol1,freq2,vol2,freq3,vol3,noise type,value)
OPERATION: Operation is used for Noise, Tone first byte, and volume.
The values placed in these three bits determine what you are doing and to which of the three tone generators:
Value of the three bits defining Operation: 000...Frequency of tone 1 001..Volume of tone one 110...Noise generator 010...Frequency of tone 2 011..Volume of tone two 111...Noise volume 100...Frequency of tone 3 101..Volume of tone three
FREQUENCY: The actual frequency is held in ten bits, which is split between the two tone bytes, and is determined as follows:
what frequency do you want? Say 110 Hz (or 110 cycles per second!): We find out how many times the frequency will go into 111860.8 (Strange number, but thats how the computer works!) Thus: 111860.8 / 110 = 1017 (use the closest integer) Now, turn this into a binary number: 1111111001 (ten values) and split it into the 6 left most bits and the four right most bits: 6 MSB = 111111 and 4 LSB = 1001 And these values go into the tone bytes as shown on previous page.
NOISE: Noise Type: O = periodic noise, 1 = white noise
Shift Rate: If set at 11, sound varies with frequency of tone 3.
VOLUME: SIXTEEN levels are available from full volume (0000) to silence (1111)
The sound varies by 2db between levels.
TIME: Notice: no time input! To switch off a tone, load the relevant generator with a zero volume. Actual timing is external: you have to do it!
WORKED EXAMPLE: TONE ONE SET AT 110 Hz. 2db volume. with white noise. shift rate 00. vol 20db:
Tone one: Byte 1: 1..0..0..0..1..0..0..1 (1001=4LSB as shown above) Tone One. byte 2: 0..0..1..1..1..1..1..1 1111111=6 MSB as above) Tone One. volume: — 1..0..0..1..0..0..0..1 NOISE type: 1..1..1..0..0..1..0..0 Noise volume: 1..1..1..1..1..0..1..0
To use CALL LOAD we have to transfer these 8 bit bytes into a decimal value:
the right most bit has a value of one if set. the 2nd from the right has a value of 2. then 4.8.16,32.64 and 128.
From which we can work out: 10001001=137 00111111=63 10010001=145 11100100=228 11111010=250
USING EXTENDED BASIC PLUS 32k RAM or MINI MEMORY·ON ITS OWN: 10 CALL INIT 20 S=-31744 30 CALL LOAD(S,137,"",S,63,"",S,145,"",S,228,"",S,250) 40 GOTO 40
To end the sound, set Tone 1 and Noise volume to zero... or use a negative call sound...CALL SOUND(-1,110,0)
NOTE:
1. You can change the frequency while the tone is still sounding. It takes two bytes so it is not as smooth as it could be...
2. You can change the volume while the sound is still present... and it is faster than using several negative CALL SOUNDS, making it possible to do some envelope shaping .... as we shall see in the first of our example programs:
SAMPLE ONE: A sort of bell sound:
100 CALL INIT 110 S=-31744 120 CALL LOAD(S,137,"",S,63,"",S,171,"",S,26) 130 FOR A=0 TO 15 140 CALL LOAD(S,144+A,"",S,176+A) 150 NEXT A 160 CALL LOAD(S,159,"",S,191) 170 GOTO 120
Line 160 turns the sound off. What happens if you omit it?
Let the program run for a long time. What happens when 'garbage collection' takes place?
Try an experiment: Change line 130 to: 130 FOR A=1 to 14 STEP 2 Notice the change?
Remember: there are two bytes for frequency and there is no sound until you give a volume!