Difference between revisions of "SOUND"

From BeebWiki
Jump to: navigation, search
m (Internal sound system)
m (Internal sound system)
 
(10 intermediate revisions by the same user not shown)
Line 5: Line 5:
 
|+ SOUND
 
|+ SOUND
 
| Availability
 
| Availability
| colspan="2" | Present in all original versions of BBC BASIC.
+
| colspan="2" | Present in all versions of BBC BASIC.
 
|- style="vertical-align:top"
 
|- style="vertical-align:top"
 
| [[BASIC metasyntax|Syntax]]
 
| [[BASIC metasyntax|Syntax]]
Line 38: Line 38:
 
|}
 
|}
  
== Implementation ==
+
== Specification ==
 
=== Internal sound system ===
 
=== Internal sound system ===
 
<code>SOUND &0xxx,volume,pitch,duration</code>
 
<code>SOUND &0xxx,volume,pitch,duration</code>
  
<code>SOUND &1xxx,volume,pitch,duration</code>
+
<code>SOUND &1xxx,ignored,ignored,ignored</code>
  
 
The channel number encodes &HSFC where:
 
The channel number encodes &HSFC where:
 
* H: Hold 0 or 1
 
* H: Hold 0 or 1
 
* S: Sync 0 to 15, 0 to 3 usually implemented
 
* S: Sync 0 to 15, 0 to 3 usually implemented
* F: Flush 0 or non-0 (usually 0 or 1)
+
* F: Flush 0 or 1, 2-15 reserved
 
* C: Channel 0 to 15, 0 to 3 usually implemented
 
* C: Channel 0 to 15, 0 to 3 usually implemented
  
Line 55: Line 55:
 
channel as a polyphonic note. Channel 0 is conventionally a 'noise' channel.
 
channel as a polyphonic note. Channel 0 is conventionally a 'noise' channel.
  
The volume, pitch and duration are 8-bit numbers with bit 8 and higher ignored.
+
Channel parameter
 +
-----------------
 +
SOUND &xxxC,vol,pitch,dur    play a note on channel C
 +
 +
SOUND &xx0x,vol,pitch,dur    don't flush channel
 +
SOUND &xx1x,vol,pitch,dur    flush channel before playing note
 +
SOUND &xx2x,vol,pitch,dur \
 +
        to                } future expansion
 +
SOUND &xxFx,vol,pitch,dur /
 +
 +
SOUND &xSxx,vol,pitch,dur    synchronise with S other channels
 +
 +
SOUND &0xxx,vol,pitch,dur    play specified note
 +
SOUND &1xxx,ign,ign,dur      continue the current envelope note on the channel
 +
 +
Volume parameter
 +
----------------
 +
SOUND chn,-32768,pitch,dur \
 +
        to                  } future expansion
 +
SOUND chn,-16  ,pitch,dur /
 +
SOUND chn,-15  ,pitch,dur \
 +
        to                  } play a note with volume 1 (quiet) to 15 (loudest)
 +
SOUND chn, -1  ,pitch,dur /
 +
SOUND chn, 0    ,pitch,dur    stop note (zero volume)
 +
SOUND chn, 1    ,pitch,dur \
 +
        to                  } play a note with an envelope
 +
SOUND chn,16    ,pitch,dur /
 +
SOUND chn,17    ,pitch,dur \
 +
        to                  } future expansion
 +
SOUND chn,32767 ,pitch,dur /
 +
 +
Pitch parameter
 +
---------------
 +
SOUND chn,vol,&0000,dur \
 +
        to              } 8-bit pitch &xx00 to &xxFF
 +
SOUND chn,vol,&00FF,dur /
 +
SOUND chn,vol,&0100,dur \
 +
        to              } 8-bit pitch &xx00 to &xxFF (see notes)
 +
SOUND chn,vol,&03FF,dur /
 +
SOUND chn,vol,&x400,dur \  RISC OS extension:
 +
        to              } 15-bit pitch &4000+&ORRR, O=octave, RRR=residual, &4000=middle C (expansion)
 +
SOUND chn,vol,&7FFF,dur /
 +
SOUND chn,vol,&8000,dur \  RISC OS extension:
 +
        to              } 15-bit pitch change &C000+delta (expansion)
 +
SOUND chn,vol,&FFFF,dur /
 +
 +
8-bit pitches are 48 units per octave, with middle C=101 (see notes)
 +
15-bit pitches are &1000 units per octave, with middle C=&4000
 +
 +
Duration parameter
 +
------------------
 +
SOUND chn,vol,pitch,-32768 \
 +
        to                  } future expansion
 +
SOUND chn,vol,pitch,-2    /
 +
SOUND chn,vol,pitch,-1        play note forever (until flushed)
 +
SOUND chn,vol,pitch,0      \
 +
        to                  } play for specified 1/20th second
 +
SOUND chn,vol,pitch,32767  /
  
 
=== Watford Speech ===
 
=== Watford Speech ===
Line 68: Line 125:
 
<code>SOUND &FExx,command,note,velocity</code>
 
<code>SOUND &FExx,command,note,velocity</code>
  
MIDI commands can be sent with SOUND &FEFF and SOUND &FEFE which are usually coded as
+
MIDI commands can be sent with SOUND &FEFF and SOUND &FEFE which are usually
SOUND -257 and SOUND -258.
+
coded as SOUND -257 and SOUND -258.
  
 
* SOUND -257,Command,Note,Velocity - Send to current channel (channel=&FEFF)
 
* SOUND -257,Command,Note,Velocity - Send to current channel (channel=&FEFF)
 
* SOUND -258,Command,Note,Velocity - Send raw MIDI command (channel=&FEFE)
 
* SOUND -258,Command,Note,Velocity - Send raw MIDI command (channel=&FEFE)
* SOUND -257 or -258,Control<0,num,num - MIDI system control
+
* SOUND -257 or -258,Command<0,num,num - MIDI system control
  
If Command is &0000-&007F it is a MIDI data byte and sent stright to the MIDI system unchanged.
+
If Command is &0000-&007F it is a MIDI data byte and sent straight to the
This allows extended sequences to be sent, such as MIDI System Exclusives.
+
MIDI system unchanged. This allows extended sequences to be sent, such as
 +
MIDI System Exclusives.
  
If Command is &0080-&00FF it is a MIDI command byte. Command bytes &80-&EF are channel-specific commands. SOUND -258 uses them as supplied, SOUND -257 sends them to the current MIDI channel by adding the current channel to the bottom nybble. So, if the current MIDI channel is 4, SOUND -257,&80... will send to MIDI channel 4, and SOUND -257,&8F should send to MIDI channel 3.
+
If Command is &0080-&00FF it is a MIDI command byte. Command bytes &80-&EF
Some implementations do not correctly round the channel number within the nybble in the second
+
are channel-specific commands. SOUND -258 uses them as supplied, SOUND -257
example.
+
sends them to the current MIDI channel by adding the current channel to the
 +
bottom nybble. So, if the current MIDI channel is 4, SOUND -257,&80... will
 +
send to MIDI channel 4, and SOUND -257,&8F should send to MIDI channel 3.
 +
Some implementations do not correctly round the channel number within the
 +
nybble in the second example.
  
Note and Velocity are 7-bit numbers with bit 7 and higher ignored, other than MIDI command &Ex
+
Note and Velocity are 7-bit numbers with bit 7 and higher ignored, other
which can specify a 14-bit Note number.
+
than MIDI command &Ex which can specify a 14-bit Note number.
 +
 
 +
SOUND -257,&0000,ignore,ignore  \
 +
          to                      } send data byte
 +
SOUND -257,&007F,ignore,ignore  /
 +
SOUND -257,&0080,param1,param2  \
 +
          to                      } send command and 0, 1 or 2 data bytes
 +
SOUND -257,&00FF,param1,param2  /
 +
SOUND -257,&0100,param1,param2  \
 +
          to                      } reserved for expansion
 +
SOUND -257,&7FFF,param1,param2  /
 +
SOUND -257,-32768,param1,param2  \
 +
          to                      } reserved for expansion
 +
SOUND -257,-2  ,param1, param2  /
 +
SOUND -257,-1  ,channel,param2    set MIDI channel
 +
SOUND -257,-1  ,0      ,param2    silence all channels
 +
 +
SOUND -258: as SOUND -257 to specified channel
 +
 +
SOUND -259 (&FEFD)              \
 +
          to                      } reserved for expansion
 +
SOUND -512 (&FE00)              /
  
 
=== Speech system ===
 
=== Speech system ===
 
<code>SOUND &FFxx,command,0,0</code>
 
<code>SOUND &FFxx,command,0,0</code>
The channel number indicates what to do with the command, the command is an 8-bit number with bit 8 and higher ignored.
+
The channel number indicates what to do with the command.
  
* SOUND &FFFx,number,0,0 - speak from PHROM '''15-x''' with word '''number'''. From a coding view this is usually written as SOUND -1,n,0,0 to SOUND -16,n,0,0.
+
* SOUND &FFFx,number,0,0 - speak from PHROM ''15-x'' with word ''number''.<br> This is usually written as SOUND -1,n,0,0 to SOUND -16,n,0,0.
* SOUND &FFBx,address,0,0 - speak from PHROM '''15-x''' from the absolute '''address'''.
+
* SOUND &FFBx,address,0,0 - speak from PHROM ''15-x'' from the absolute ''address''.
* SOUND &FF60,command,0,0 - start sending speech using phoneme '''command''.
+
* SOUND &FF60,command,0,0 - start sending speech using phoneme ''command''.
* SOUND &FF00,command,0,0 - continue sending speech using phoneme '''command''.
+
* SOUND &FF00,command,0,0 - continue sending speech using phoneme ''command''.
  
 
SOUND &FF01-&FF7F actually send the bottom byte as the Speech command to use.
 
SOUND &FF01-&FF7F actually send the bottom byte as the Speech command to use.
 
=== Summary ===
 
SOUND Channels
 
--------------
 
%000H SSSS FFFF CCCC  Internal sound system
 
%0010 0000 xxxx xxxx  Watford Electronics Speech
 
%1111 1110 1111 111x  MIDI control
 
%1111 1111 xxxx xxxx  Speech
 
 
Sound system
 
------------
 
SOUND %000hssss:xxxfcccc, %xxxxxxxx:xxxvvvvv, %xxxxxxxx:pppppppp, %xxxxxxxx:dddddddd - Make sound
 
 
Watford Speech
 
--------------
 
SOUND %00100000:xxxxxxxx, %xxxxxxxx:wwwwwwww, %xxxxxxxx:llllllll, %xxxxxxxx:xxxxxxxx - Speak word
 
 
MIDI
 
----
 
SOUND %11111110:xxxxcccc, %0xxxxxxx:0ddddddd, %xxxxxxxx:xxxxxxxx, %xxxxxxxx:xxxxxxxx - MIDI data
 
SOUND %11111110:xxxxcccc, %0xxxxxxx:10dddddd, %xxxxxxxx:xnnnnnnn, %xxxxxxxx:xvvvvvvv - MIDI note
 
SOUND %11111110:xxxxcccc, %0xxxxxxx:110ddddd, %xxxxxxxx:xnnnnnnn, %xxxxxxxx:xxxxxxxx - MIDI change
 
SOUND %11111110:xxxxcccc, %0xxxxxxx:1110dddd, %xxvvvvvv:vvvvvvvv, %xxxxxxxx:x0000000 - MIDI bend
 
SOUND %11111110:xxxxcccc, %0xxxxxxx:1110dddd, %xx000000:0vvvvvvv, %xxxxxxxx:xvvvvvvv - MIDI bend
 
SOUND %11111110:xxxxcccc, %0xxxxxxx:1111dddd, %xxxxxxxx:xxxxxxxx, %xxxxxxxx:xxxxxxxx - MIDI system
 
SOUND %11111110:1111111c, %1xxxxxxx:1111dddd, %xxxxxxxx:nnnnnnnn, %xxxxxxxx:xxxxxxxx - MIDI control
 
                                    \--\/--/
 
                                  MIDI command if b15=0 and b7=0
 
Speech
 
------
 
SOUND %11111111:11xxrrrr, %xxxaaaaa:aaaaaaaa, %xxxxxxxx:xxxxxxxx, %xxxxxxxx:xxxxxxxx - Speak word
 
SOUND %11111111:10xxrrrr, %xxaaaaaa:aaaaaaaa, %xxxxxxxx:xxxxxxxx, %xxxxxxxx:xxxxxxxx - Speak from address
 
SOUND %11111111:0cccxxxx, %dddddddd:dddddddd, %xxxxxxxx:xxxxxxxx, %xxxxxxxx:xxxxxxxx - Speak data word
 
SOUND %11111111:00000000, %dddddddd:dddddddd, %xxxxxxxx:xxxxxxxx, %xxxxxxxx:xxxxxxxx - Speak data word
 
                \--\/--/
 
                    if b7=0, if<>0, sent to Speech as command, normally use &FF60 for Speak External
 
  
 
== Trivia ==
 
== Trivia ==
Line 140: Line 187:
 
   D7: CHAIN    D7: CHAIN
 
   D7: CHAIN    D7: CHAIN
 
   etc.
 
   etc.
 +
 +
== See also ==
 +
See [[OSWORD &07|OSWORD 7]] for implementation
  
 
== References ==
 
== References ==
Line 145: Line 195:
  
 
[[User:Jgharston|Jgharston]] 00:30, 2 January 2008 (UTC)
 
[[User:Jgharston|Jgharston]] 00:30, 2 January 2008 (UTC)
<!--
+
[[User:Jgharston|Jgharston]] ([[User talk:Jgharston|talk]]) 23:58, 7 March 2020 (CET)
    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
 
 
 
    * which sound channel is to be used
 
    * the loudness of the note (or the envelope number)
 
    * the pitch of the note
 
    * how long the note is to last
 
 
 
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>
 
 
 
-->
 

Latest revision as of 03:41, 16 April 2020

SOUND is a BASIC command to make sounds.

SOUND
Availability Present in all versions of BBC BASIC.
Syntax SOUND <numeric>,<numeric>,<numeric>,<numeric>
Token (hex) D4 (command)
Description Generates sounds.

Description

SOUND calls OSWORD &07 to generate sounds. It is most commonly used to generate sounds with the internal sound generator, but it can also be used to create speech or MIDI sounds, as specified by the channel parameter.

Defined channel numbers

&0xxx Sound generator
&1xxx Sound generator
&20xx Watford Electronics Speech system[1]
&FExx MIDI control [2]
&FFxx Speech system [3]

Specification

Internal sound system

SOUND &0xxx,volume,pitch,duration

SOUND &1xxx,ignored,ignored,ignored

The channel number encodes &HSFC where:

  • H: Hold 0 or 1
  • S: Sync 0 to 15, 0 to 3 usually implemented
  • F: Flush 0 or 1, 2-15 reserved
  • C: Channel 0 to 15, 0 to 3 usually implemented

The internal sound system implements 4 channels (1 on the Electron), but extensions can increase the number of supported channels. For example, the Electron Sound Expansion supports four channels, and MIDI redirection can support 16 channels treating each channel as a polyphonic note. Channel 0 is conventionally a 'noise' channel.

Channel parameter
-----------------
SOUND &xxxC,vol,pitch,dur    play a note on channel C

SOUND &xx0x,vol,pitch,dur    don't flush channel
SOUND &xx1x,vol,pitch,dur    flush channel before playing note
SOUND &xx2x,vol,pitch,dur \
        to                 } future expansion
SOUND &xxFx,vol,pitch,dur /

SOUND &xSxx,vol,pitch,dur    synchronise with S other channels

SOUND &0xxx,vol,pitch,dur    play specified note
SOUND &1xxx,ign,ign,dur      continue the current envelope note on the channel

Volume parameter
----------------
SOUND chn,-32768,pitch,dur \
        to                  } future expansion
SOUND chn,-16   ,pitch,dur /
SOUND chn,-15   ,pitch,dur \
        to                  } play a note with volume 1 (quiet) to 15 (loudest)
SOUND chn, -1   ,pitch,dur /
SOUND chn, 0    ,pitch,dur    stop note (zero volume)
SOUND chn, 1    ,pitch,dur \
        to                  } play a note with an envelope
SOUND chn,16    ,pitch,dur /
SOUND chn,17    ,pitch,dur \
        to                  } future expansion
SOUND chn,32767 ,pitch,dur /

Pitch parameter
---------------
SOUND chn,vol,&0000,dur \
        to               } 8-bit pitch &xx00 to &xxFF
SOUND chn,vol,&00FF,dur /
SOUND chn,vol,&0100,dur \
        to               } 8-bit pitch &xx00 to &xxFF (see notes)
SOUND chn,vol,&03FF,dur /
SOUND chn,vol,&x400,dur \  RISC OS extension:
        to               } 15-bit pitch &4000+&ORRR, O=octave, RRR=residual, &4000=middle C (expansion)
SOUND chn,vol,&7FFF,dur /
SOUND chn,vol,&8000,dur \  RISC OS extension:
        to               } 15-bit pitch change &C000+delta (expansion)
SOUND chn,vol,&FFFF,dur /

8-bit pitches are 48 units per octave, with middle C=101 (see notes)
15-bit pitches are &1000 units per octave, with middle C=&4000

Duration parameter
------------------
SOUND chn,vol,pitch,-32768 \
        to                  } future expansion
SOUND chn,vol,pitch,-2     /
SOUND chn,vol,pitch,-1        play note forever (until flushed)
SOUND chn,vol,pitch,0      \
        to                  } play for specified 1/20th second
SOUND chn,vol,pitch,32767  /

Watford Speech

SOUND &20xx,word,library,0

Watford speach commands can be sent with SOUND &2000, which is usually coded as SOUND 8192.

Bit 0-7 of the channel number is ignored.

MIDI control

SOUND &FExx,command,note,velocity

MIDI commands can be sent with SOUND &FEFF and SOUND &FEFE which are usually coded as SOUND -257 and SOUND -258.

  • SOUND -257,Command,Note,Velocity - Send to current channel (channel=&FEFF)
  • SOUND -258,Command,Note,Velocity - Send raw MIDI command (channel=&FEFE)
  • SOUND -257 or -258,Command<0,num,num - MIDI system control

If Command is &0000-&007F it is a MIDI data byte and sent straight to the MIDI system unchanged. This allows extended sequences to be sent, such as MIDI System Exclusives.

If Command is &0080-&00FF it is a MIDI command byte. Command bytes &80-&EF are channel-specific commands. SOUND -258 uses them as supplied, SOUND -257 sends them to the current MIDI channel by adding the current channel to the bottom nybble. So, if the current MIDI channel is 4, SOUND -257,&80... will send to MIDI channel 4, and SOUND -257,&8F should send to MIDI channel 3. Some implementations do not correctly round the channel number within the nybble in the second example.

Note and Velocity are 7-bit numbers with bit 7 and higher ignored, other than MIDI command &Ex which can specify a 14-bit Note number.

SOUND -257,&0000,ignore,ignore   \
         to                       } send data byte
SOUND -257,&007F,ignore,ignore   /
SOUND -257,&0080,param1,param2   \
         to                       } send command and 0, 1 or 2 data bytes
SOUND -257,&00FF,param1,param2   /
SOUND -257,&0100,param1,param2   \
         to                       } reserved for expansion
SOUND -257,&7FFF,param1,param2   /
SOUND -257,-32768,param1,param2  \
         to                       } reserved for expansion
SOUND -257,-2   ,param1, param2  /
SOUND -257,-1   ,channel,param2     set MIDI channel
SOUND -257,-1   ,0      ,param2     silence all channels

SOUND -258: as SOUND -257 to specified channel

SOUND -259 (&FEFD)               \
         to                       } reserved for expansion
SOUND -512 (&FE00)               /

Speech system

SOUND &FFxx,command,0,0 The channel number indicates what to do with the command.

  • SOUND &FFFx,number,0,0 - speak from PHROM 15-x with word number.
    This is usually written as SOUND -1,n,0,0 to SOUND -16,n,0,0.
  • SOUND &FFBx,address,0,0 - speak from PHROM 15-x from the absolute address.
  • SOUND &FF60,command,0,0 - start sending speech using phoneme command.
  • SOUND &FF00,command,0,0 - continue sending speech using phoneme command.

SOUND &FF01-&FF7F actually send the bottom byte as the Speech command to use.

Trivia

In the original development of BBC BASIC, the sound command was BEEP which can be seen from the token position for SOUND, alphabetically with other keywords starting with 'B'[4]:

 Originally:   Became:
 D4: BEEP      D4: SOUND
 D5: BPUT      D5: BPUT
 D6: CALL      D6: CALL
 D7: CHAIN     D7: CHAIN
 etc.

See also

See OSWORD 7 for implementation

References

Jgharston 00:30, 2 January 2008 (UTC) Jgharston (talk) 23:58, 7 March 2020 (CET)