Your Spectrum
YS MegaBasic manual - Sound
Home YS MegaBasic Contents

SOUND

YS MegaBasic provides two new ways in which you can produce sound on your Spectrum - the 'PLAY_' command and the interrupt sound generator (ISG). 'PLAY_' takes the form:

  PLAY_n,l,s,d,f

Where: 'n' produces pure notes (n=0) or white noise (n=1); 'l' is the length of each step; 's' is the start frequency; 'd' is the number of steps; and 'f' is the change of frequency after each step. Variables 'd' and 'f' may be repeated more than once if required.

INTERRUPT SOUND GENERATOR

Whereas 'PLAY_' is only a glorified BEEP command, YS MegaBasic provides a new feature, allowing sound to be produced while a program is still running. This is made possible by the interrupt sound generator (ISG). Fifty times a second the normal program flow of the Spectrum is interrupted and a jump is made to a small machine code routine that scans the keyboard. On the MegaSpectrum, as well as scanning the keyboard, the machine code routine has been customised to produce sounds. YS MegaBasic provides a number of commands to handle the ISG and the sound buffer, and these are as follows:
SOFF Disables the ISG - any sound being made ceases immediately.
SON Enables the ISG, so that it will resume whatever it was doing before being disabled.
SREP_n Requires a single numeric expression (n). If the result of the expression is zero, then the data stored in the sound buffer will only be played once - it the result is one, the sound buffer's contents will be repeated forever.
SOUND_ While quite complex, this is the main command used to manipulate the sound buffer. It takes the form:
SOUND_n,a,b,c,d
Where: 'n' clears the sound buffer before replacing it with something new (n=0) or adds sound on to the end of the sound buffer (n=1); 'a' plays a pure sound (a=0) or white noise (a=1); 'b' is the value to be added to the frequency after each step; 'c' is the number of steps in sequence; and 'd' is the number of times the sequence is to be repeated.
18

When the 'PLAY_'command is executed, the ISG is disabled automatically. Therefore, before any sound can be made, you must use 'SON' to re-enable the ISG (note, by the way, that the ISG does not function when the line-editor is operating). The more complex the sounds get, the slower your program will run. Here's a short example program to prove that the ISG really works:

  10 SOUND_0,0,1,20,255
  20 SREP_1
  30 SON
  40 MODE_4: STIPPLE_6: FONT_2
  50 VDU_(128+RND*15)
  60 PAPER RND*7: INK 9
  70 GO TO 50


Lines 10 to 30 set up the sound and lines 40 to 70 go off to perform another task.
19.a