Contents / Reference section / BASIC keywords / Previous letter / Next letter / Index


S

SAVE

Purpose

To save a program that is in the computer's memory onto cassette or disc. The program must be given a name - usually called its filename. The filename can have up to 10 letters and numbers in a cassette system, must start with a letter, and cannot contain spaces or punctuation marks.

Examples

SAVE "FRED"

SAVE A$

Description

A command which saves the current program area - that is the area between the address given in the variables PAGE and TOP.

Syntax

SAVE <string>

Associated keywords

LOAD, CHAIN

SGN

Purpose

This determines whether a number is positive, zero or negative. The function returns

-1 for negative number
0 for zero
+1 for positive number.

Examples

100 X=SGN(Y)

230 result=SGN(difference)

Description

A function returning -1 for an argument which is negative, +1 for a positive argument and zero for an argument equal to zero.

Syntax

<num-var>=SGN(<numeric>)

Associated keywords

ABS

SIN sine

Purpose

This calculates the sine of an angle. The angle must be expressed in radians rather than degrees - but you can convert from degrees to radians using the function RAD.

Examples

120 Y=SIN(RAD(45))

2340 value=SIN (1.56)

Description

A function giving the sine of its argument. The argument must be in radians.

Syntax

<num-var>=SIN(<numeric>)

Associated keywords

COS, TAN, ACS, ASN, ATN, DEG, RAD

Demonstration program

To draw a sine wave on the screen

10 MODE 4

20 FOR X=0 TO 1280 STEP 4

30 DRAW X,500+500*SIN(X/50)

40 NEXT X

SOUND

Purpose

This statement is used to make the computer generate sounds using the internal loudspeaker. The sound generator is capable of making four sounds at once. Each of the four sound channels can generate one note. The keyword SOUND must be followed by four numbers which specify

For example:

SOUND 1,-15,53,20

will play a note on sound channel 1, with a loudness of -15 (maximum volume). A pitch value of 53 gives middle C and a duration of 20 will make the note last for 1 second.

SOUND C,A,P,D

The channel number (C) can be 0,1,2, or 3. Channel 0 is a special channel that can produce various noises, whereas channels 1, 2 and 3 are used to produce single notes. Other values of C (the channel number) produce special effects which are explained further on.

The amplitude or loudness (A) can have any whole number value between -15 and 4. Values -15 to 0 produce notes of fixed loudness throughout the whole note. A value of -15 is the loudest, -7 is half volume and 0 produces silence. Values of 1 to 4 enable the amplitude to be controlled while the note is playing. When you play a note on the piano the sound gradually fades away. Effects like this are selected by using one of the 4 user-defined envelopes which are selected by setting A to be 1,2,3 or 4. Envelopes are explained on pages 182 and 244.

The pitch (P) is used to set the pitch or frequency of the note. The pitch can have any value between 0 and 255. The note A above middle C is selected with a value of 89. The table on page 181 shows the value of P needed to produce a particular note. You will see that to go up an octave P is increased by 48 and to go up a perfect 5th P must be increased by 28.

Increasing the value of P by one will increase the note produced by a quarter of a semi-tone.

To play the chord of C major which consists of the notes C, E and G for 2 seconds you could enter

100 SOUND 1,-15,53,40

110 SOUND 2,-15,69,40

120 SOUND 3,-15,81,40

Whereas to play a number of notes in succession you would enter

100 SOUND l,-15,97,10

110 SOUND 1,-15,105,10

120 SOUND 1,-15,89,10

130 SOUND 1,-15,41,10

140 SOUND 1,-15,69,20

which plays a well-known film theme.

The duration (D) can have any value between -1 and 254. Values in the range 0 to 254 give a note duration of that number of twentieths of a second. Thus if D=40 the note will last for 2 seconds. Setting D=-1 means that the note will continue to sound until you actually take steps to stop it. You can either press the ESCAPE key or stop it by sending another note, to the same channel, which has "Flush Control" set to 1 - see page 353 later in this section.

As was mentioned earlier in this section, channel number 0 produces "noises" rather than notes and the value of P in the statement

SOUND 0,A,P,D

has a different effect from that described for channels 1, 2 and 3. Here is a summary of the effects of different values of P on the noise channel:

P / Effect

0 / High frequency periodic noise

1 / Medium frequency periodic noise

2 / Low frequency periodic noise

3 / Periodic noise of frequency determined by the pitch setting of channel 1

4 / High frequency "white" noise

5 / Medium frequcy "white" noise

6 / Low frequency "white" noise

7 / Noise of frequency determined (continuously) by the pitch setting of channel 1

Values of P between 0 and 3 produce a rather rasping, harsh note. With P set to 4 the noise is not unlike that produced by a radio when it is not tuned to a station - sort of "shssh" effect. P=6 sounds like the interference found on bad telephone call.

When P is set to 3 or 7 then the frequency of the noise is controlled by the pitch setting of sound channel number 1. If the pitch of channel 1 is changed while channel 0 is generating noise then the pitch of the noise will also change. The program below generates a noise on channel 0 and varies the pitch of the noise by changing the pitch of channel 1. Notice that the amplitude of channel one is very low (-1) so you will hardly hear it - but you will hear the noise on channel 0.

100 SOUND 0,-15,7,150

110 FOR P= 100 TO 250

120 SOUND 1,-1,P,1

130 NEXT P

Notice that we have not yet described how sounds can be affected by a superimposed envelope. An envelope can affect both the pitch and amplitude of a note as it is playing. Thus the statement

SOUND 1,-15,255,255

merely plays a continuous loud note, whereas

ENVELOPE 1,1,-26,-36,-45,255, 255,255,127,0,0,-127,126,0

SOUND 1,1,255,255

produces a complex sound controlled largely by the envelope.

See the keyword ENVELOPE for more details.

As mentioned briefly at the start of the description of the SOUND statement, the channel number, C can be given values other than 0, 1, 2 and 3. You need not understand exactly why the following works to use it!

For C one can write a four figure hexadecimal number to achieve certain effects - for example:

SOUND &1213,-15,53,40

The first parameter in the above example has the value &1213. The ampersand (&) indicates to the computer that the number is to be treated as a hexadecimal number. The four figures which follow the ampersand each control one feature. In this new expanded form the SOUND statement looks like

SOUND &HSFC,A,P,D

and the functions H,S,F and C will be explained in turn. In essence these numbers enable one to synchronize notes so that one can play chords effectively.

The first number (H) can have the value 0 or 1. If H= 1 then instead of playing a new note on the selected channel, the previous note on that channel is allowed to continue. If a note were gently dying away then it might be abruptly terminated when its time was up. Setting H=1 allows the note to continue instead of playing a new note. If H=1 then the note defined by the rest of the SOUND statement is ignored.

The second number (S) is used to synchronize the playing of a number of notes. If S= 0 then the notes are played as soon as the last note on the selected channel has completed. (There is a slight simplification here; "completed" means "has reached the start of the release phase".) The user is referred to the keyword ENVELOPE for relevant detail.

A non-zero value of S indicates to the computer that this note is not to be played until you have a corresponding note on another channel ready to be played. A value of S=1 implies that there is one other note in the group. S=2 implies two other notes (i.e. a total of 3). If a note was sent to channel one with S set to 1 then it would not be played until a note was ready on another channel which also had S set to 1. For example:

110 SOUND &101,-15,50,200

110 SOUND 2,-15,200,100

120 SOUND &102,-15,100,200

When this program is run the note at line 100 will not play until channel 2 is free. Line 110 sounds a note immediately on channel 2 - and for 5 seconds (duration 100). When that note has completed then both the notes from lines 100 and 120 will sound together.

The third number (F) can have the value 0 or 1. If it is set to 1 then the sound statement in which it occurs flushes (throws away) any other notes waiting in the queue for a particular channel. It also stops whatever note is being generated on that channel at present. The sound statement in which F=1 then plays its note. Setting F behaves like an "over-ride". For example:

20 SOUND 2,-15,200,100

25 FOR X=1 TO 500:NEXT X

30 SOUND &12,-15,100,200

In the above situation line 20 will start a sound on channel2 but this will be stopped almost immediately by line 30 which will generate a lower and longer note on channel 2. Line 25 just gives a short delay.

Setting F=1 provides an easy way of stopping an everlasting note! Thus SOUND &13,0,0,1 stops the current note on channel 3 and instead plays one at zero loudness and of minimum length. This will stop channel 3 immediately.

The last number (C) is the channel number described earlier.

Description

The sound generator has four separately-controlled synthesis channels. Each can sound at one of 16 amplitudes, including 'off'. The audio output is the sum of the channel outputs. Channels 1 - 3 each generate a squarewave with programmable frequency. Channel 0 can produce noise (unpitched sound of psuedo-random structure) or a pulse waveform. The frequency of the pulsewave or period of the noise can be set to one of the three fixed options, or to the frequency of channel.

The BASIC program generates each sound by initiating one or more 'requests', each of which may take the form of a musical note or a single effect and is directed to a specific channel. If the destination channel is idle when a request requires it, the sound starts playing immediately. If a previous request is still being handled the new one is placed on a queue, where it waits until the current event is over (or past a critical stage - see ENVELOPE). If the queue is full, the program waits. Separate queues are provided for the four channels, each of which can hold up to four requests, not counting the one currently being executed. The program can look at the state of any queue and flush any queue, but cannot find out or alter the state of the current event, except for flushing the whole queue.

The SOUND keyword is followed by four parameters, the first of which consists of 4 hexadecimal digits. Thus

SOUND &HSFC,A,P,D

Range Function

H 0 or 1 Continuation
S 0 to 3 Synchronization
F 0 or 1 Flush
C 0 to 3 Channel number
A -15 to 4 Amplitude or envelope number
P 0 to 255 Pitch
D 1 to 255 Duration

The 'H' parameter allows the previous event on that channel to continue, and if this is 1, the amplitude and pitch parameters of SOUND have no effect. Because the dummy note is queued in the normal way, it can be used to ensure that the release segment of a sound, which occurs after the duration is over and would otherwise be truncated by the next sounding event on the same channel, is allowed to complete.

The 'S' parameter allows requests to be queued separately and then executed at the same instant, for chords and multiple voice effects. The value initially determines the number of other channels that must receive requests with the same value of 's', before the group will play. For example, each note of a three-note chord would be generated by a SOUND with the value of 2 for 's'. The system will read the value of 's' from the first one and then wait for 2 more requests with 2 as the value of 's' before playing the complete chord. Single requests use 0 for 's' so they play as soon as they reach the end of the channel queue.

The parameter 'F' will normally be zero, causing the request to be queued. If it is 1, the channel queue will be flushed first, so the request will sound immediately.

The parameter 'C' determines the number of the sound channel to be used.

The 'A' parameter controls the amplitude of the sound and can be used in two ways. Positive values up to 4 select the envelope (1 to 4) to be used. If the RS423 and cassette output buffers are unused then envelope numbers up to 16 may be defined and used. Zero and negative integers up to -15 directly set the amplitude of the sound, which is then fixed at this value for the duration of the note. -15 corresponds to the loudest, and 0 is 'off'. The 'P' parameter determines the pitch of the note. It can take values from 0 to 255.

The 'D' parameter determines the total duration of sounds who amplitude is determined explicitly by a negative or zero value of 'A' parameter. The duration is given in twentieths of a second. If an envelope has been selected, by a positive value of 'A', then the duration 'D' determines the total of the attack, decay and sustain periods - but not of the release phase.

Syntax

SOUND <numeric>,<numeric>,<numeric>,<numeric>

Associated keywords

ENVELOPE, ADVAL

SPC space

Purpose

This statement is used to print multiple spaces on the screen. It can only be used as part of PRINT or INPUT statements. The number in brackets gives the number of spaces to be printed.

Examples

120 PRINT "Name";SPC(6);"Age";SPC(10);"Hours"

4030 INPUT SPC(10),"Value",V

Description

A statement printing a number of spaces on the screen. Up to 255 spaces may be printed.

Syntax

PRINT SPC (<numeric>)

or

INPUT SPC (<numeric>)

Associated keywords

TAB, PRINT, INPUT

SQR square root

Purpose

This statement is used to calculate the square root of a number.

Examples

10 X=SQR(Y)

300 X=(-B+SQR(B^2-4*A*C))/(2*A)

Description

A function returning the square root of its argument. An attempt to calculate the square root of a negative number will produce the error message '-ve root' which is error number 21.

Syntax

<num-var>=SQR(<numeric>)

Associated keywords

None

STEP

Purpose

This is part of the FOR...TO...STEP...NEXT structure.

In the program shown below, STEP indicates the amount that the variable COST is to be increased each time around the loop. In this case the cost is to increase in steps of 5 units.

The step may be positive or negative.

STEP is optional, if omitted a step size of +1 is assumed - see FOR on page 260.

Examples

300 FOR X=100 TO 20 STEP -2.3

Description

Part of the FOR...NEXT construct. STEP is optional.

Syntax

FOR <num-var>=<numeric>TO<numeric> STEP<numeric>

Associated keywords

FOR, TO, NEXT

Demonstration program

230 FOR cost=100 TO 200 STEP 5

250 production=FNtaken(cost)

260 PRINT production,cost

270 NEXT cost

STOP

Purpose

This statement interrupts a program which is running and prints the message

STOP at line XXXX

on the screen; otherwise the effect is identical to END.

STOP may occur as many times as is needed in a program.

Examples

2890 STOP

3080 STOP

Description

Causes execution of the program to cease and a message to be printed out.

Syntax

STOP

Associated keywords

END

STR$ string

Purpose

This string function converts a number into the equivalent string representation. Thus STR$(4.6) would give "4.6".

STR$ is affected by the field width and format constraints imposed by the variable @%. The default format is G9 with B1= 0. See page 70.

The opposite function of converting a string into a number is performed by the function VAL.

Examples

20 A$=STR$(X)

5060 num$=STR$ (size)

Description

A string function which returns the string form of the numeric argument as it would have been printed.

Syntax

<string-var&gt;=STR$(<numeric>)

Associated keywords

VAL, PRINT

STRING$

Purpose

This produces a long string consisting of multiple copies of a shorter string. Thus STRING$(6,"--0") would be

--0--0--0--0--0--0

This function is useful for decorative features. It should be used whenever the user needs to generate a long string from lots of identical short strings.

It is very important, to avoid wasting memory space, that strings are set to their maximum length the first time that they are allocated. This can easily be done by using STRING$. For example to set A$ to contain up to 40 characters one could write

A$=STRING$(40, " ")

A$ can then be set back to empty using A$="" before use.

Examples

400 A$=STRING$(x,pattern$)

560 B4$=STRING$(5,"0+")

PRINT STRING&(10,"hello")

Description

A string function returning multiple concatenations of a string.

Syntax

<string-var>=STRING$(<numeric>,<string>)

Exit: BBC Microcomputer User Guide; Kasoft Typesetting; Archer


The BBC Microcomputer User Guide was written by John Coll and edited by David Allen for the British Broadcasting Corporation.

Optical character recognition and original formatting effort by Mark Usher.

HTML version maintained by: Kade "Archer" Hansson; e-mail: archer@dialix.com.au

Last updated: Monday 12th February 2001