17: SOUND AND MUSIC The Amiga's sound system is capable of generating stereo sound effects which would have been unheard of just a few years ago. The results are impressive even through your TV speaker, but when you connect your Amiga to a Hi-Fi, the sounds can actually shake your room! As you would expect from AMOS, we've come a long way since the humbe BEEP command. In fact, we've provided everything you need to incorporate mind-blowing sound effects in your own games. All the AMOS sound commands are performed independently of your Basic programs. So your soundtracks can be played continuously, without affecting the quality of the game-play in the slightest. Samples may be created using any of the available sampling cartridges and can be replayed with a simple SAMPLAY instruction. Each sample can be output in a variety of speeds, and may be looped repeatedly. It's even possible to play a sample as a musical note. Music can be converted over from a separate package such as SONIX, SOUNDTRACKER or GMC. The AMOS Music system is intelligent and will automatically stop when a sound is played through the current channel, thus allowing you to effortlessly combine both samples and music in the same sound channel, without the risk of unwanted interference effects. Each song can incorporate up to 256 separate instruments; the only limit to the number of songs is the amount of available memory. In order to keep the memory overhead down to an absolute minimum, all tunes are built up of a number of separate patterns. Once a pattern has been created, it can be accessed anywhere in your music using just a couple of bytes. By defining just a few key patterns, you can therefore create dozens of different tues without running short of memory. The best thing about the AMOS music system however, is that it's expandable. The entire source code is supplied on the data disc for you to examine or change. So you won't be left out in the cold by any future developments on the Amiga's music scene. Simple sound effects ==================== We'll start off with a run down of the built-in sound effects included in AMOS Basic. These are the AMOS equivalent to the Amiga Basic BEEP command. BOOM (generate a noise sounding like an explosion) BOOM Kapow! You're dead! Use BOOM to add the appropriate stereo sound effect in your games. Traditionally this type of "White Noise" as been extremely difficult on the Amiga, but AMOS uses a clever interrupt system to create a realistic explosion effect. Example: Boom : Print "You're DEAD!" SHOOT (create a noise like a gun firing) SHOOT This command generates a simple gunshot effect. Like BOOM, SHOOT does not halt your program in any way. So if you're firing several successive shots, you may wish to add a small delay using WAIT. Shoot : Wait 6 : Shoot : Print "You're DEAD!" BELL (simple bell sound) 234 BELL [f] BELL produces a pure tone with frequency f. f sets the pitch of the note, from 1 (very deep) to 96 (very high). Sound channels ============== The Amiga's hardware can effortlessly play up to four different sounds simultaneously. This allows you to add attractive harmonics to your sound effects. Each sound can be output through one of four VOICES numbered from 0 to 3. You can think of these voices as a separate instruments which can independently play their own sequence of notes, samples or music. All four voices are internally combined to generate the final sound you hear through your speaker system. The AMOS sound instructions will happily play your sounds using any arrangement of voices you like. All AMOS sound commands use a standard way of entering your voice settings. Each voice is assigned a particular bit in a VOICE parameter like so: Bit 0 -> Voice 0 Bit 1 -> Voice 1 Bit 2 -> Voice 2 Bit 3 -> Voice 3 To activate the required voices, set the appropriate bits to 1. Here's a list of common values to make things a little easier Value Voice used Effect ----- ---------- ------ 15 0,1,2,3 Uses all four voices 9 0,3 These voices are combined together and output to the left speaker. 8 3 6 2,4 Played through the RIGHT speaker. 4 2 2 1 1 0 In order to do justice to the resulting sound effects, you'll almost certainly need to connect your Amiga to a Hi-Fi system of some sort. Most TVs are just not capable of reproducing the full range of sounds which can be generated by the Amiga's amazing hardware. VOLUME (change the sound colume) 235 VOLUME [v,] intensity VOLUME changes the volume of the sounds which are to be played through one or more sound channels. "intensity" refers to the loudness of this sound. It can normally range from 0 (silent) to 63 (maximum). As a default, the volume is set to the same intensity for all four of the available voices. THe new volume will be used for all future sound effects, including music. The v parameter lets you change the volume of each voice independently. v now indicates which combination of voices are to be regulated. This second option is only used by the sound effects. It has no affect on any music you're playing. The voices are selected using a bit amp in the standard format, with each bit representing state of a single sound channel. If the bit is set to 1, then the volume of this voice will be changed, otherwise it will be unaffected. Examples: Volume %0001,63 : Boom : Wait 100 Volume %1110,10 : Boom : Wait 50 Play 40,0 : Wait 30 Volume 50 : Play 40,0 Sampled sound ============= If you had to generate all the sound effects you need, directly inside your computer, you would be faced with an impossible task. In practive, it's often much easier to take a real sound from an external source, such as a tape recorder, and convert it into a list of numbers which can be held in your computer's memory. Eacn number represents the volume of a particular sample of the sound. By rapidly playing these values back through the Amiga's sound chips, you can recreate a realistic impression of the original sound. This technique forms the basis of the sampled sound effects found in most modern computer games. If you want to create your own samples, you'll be forced to buy a separate piece of hardware known as a SAMPLER CARTRIDGE. Although these cartridges are fun, they're certainly not essentia. AMOS Basic is perfectly capable of playing any existing sound sample, without the need for any expensive add-ons. Currently there are hunderds of sound effects available from the public domain (PD), covering the vast majority of the effects you'll need in your games. We've even included a selection of useful samples on the AMOS data disc for you to experiment with. SAM PLAY (play a sound sample from the AMOS sample bank) SAM PLAY s SAM PLAY v,s SAM PLAY v,s,f The SAMP PLAY instruction plays a sampled sound straight through your loudspeaker system. All samples are normally stored in memory bank number 5, but this may be 236 freely changed using the SAM BANK command. s is the number of the sample bank which is to be played. There's no limit of the samples you can store in a bank other than the available memory. If you want to use your own samples with this instruction, you'll need to incorporate them into an AMOS memory bank. Full details can be found towards the end of this section. v is a bit-map containing a list of voices your sample will use. As usual, there's one bit for each possible voice. To play your samples through the required voice, simply set the relevant bit to 1. f holds the playback speed of your sample, measured in hertz. This specifies the number of samples which are to be played each second. Typical sample speeds range from 4000, for noises such as explosions, to 10000 for recognisable speech effects. By changing the playback rate, you can freely adjust the pitch of your sound over a large range. So a single sample can be used to generate dozens of different sounds. Example: Load "AMOS_DATA:Samples/Sample_Demo.abk" For S=1 To 11 Locate 0,0 : ? "Playing sample ";S Sam Play S Locate 0,24 :Centre "" :Wait Key :Cline Next S Wait Key Sam Play 1,11 : Wait 5 : Sam Play 2,11 Wait key Sam Play 1,1,2000 Wait Key Sam Play 1,1,15000 A further demonstration of this command can be found in EXAMPLE 17.1 SAM BANK (change the current bank) SAM BANK n Assigns a new memory bank to be used for your samples. All future SAM PLAY instructions will now take their sounds directly from this bank. It's possible to exploit this feature to hold several complete sets of samples alongide each other. You can then between these samples at any time, with just a simple call to the SAM BANK. SAM RAW (play a sample from memory) SAM RAW voice,address,length,frequency Plays a raw sample stored anywhere in the Amiga's memory. "voice" is a 237 bit-pattern in standard format which specifies the list of voices your sample is to use. Each bit in the pattern selects a single channel to be played (see sound channels). "address" holds the address of your sample. Normally, this will refer to the inside of an existing AMOS memory bank. "length" contains the length of the sample you wish to play. "frequency" indicates the sample speed to be used for the playback (in samples per second or Hz). This may be very different to the rate at which the sample was originally recorded. SAM RAW lets you play standard Amiga samples straight through your loudspeaker, without the need to create a special memory bank (see Creating a sample bank). It's now your responsibility to manage your samples in memory, and enter the sample parameters by hand. SAM RAW is great for browsing through files from your disc collection. Use BLOAD to hold a file in a bank and then use SAM RAW to play the data. With luck you should come across some interesting sounds. Examples: Reserve As Work 10,55000 Bload "Samples/Samples.abk",start(10) Sam Raw 15,start(10,length(10),10000 SAM LOOP (repeat a sample) SAMP LOOP ON/OFF The SAM LOOP directive informs AMOS Basic that all subsequent samples are to be repeated continuously. Examples: Load "AMOS_DATA:Samples/Sampledemo.abk" Sam Loop On For S=1 To 11 Locate 0,0 : Print "Playing sample ";S Sam Play S Locate 0,24 : Centre "":Wait Key :Cline Next S Sam Loop Off This looping effect can be deactivated with a simple call to the SAM LOOP OFF command. Creating a sample bank ====================== If you're indenting to play your own samples using SAM PLAY, you'll first need to load them into a memory bank. This can be achieved with the SAMMAKER program supplied on the AMOS data disc. On start-up, SAMMAKER presents you with a standard AMOS file selector. Enter the filename of the first sample to be stored in your new bank, and press RETURN. If AMOS can't find the sampling rate, you'll be asked to enter it directly. It doesn't really matter if you make a mistake at this point, as you can safely replay your samples at any speed you like. After a short delay, you'll be prompted for the next sample to be installed into the bank. When you've reached the end of your samples, type SAVE at the file selector to save your samples onto the disc. You'll be automatically prompted for the destination filename of your new bank. This can now be entered into AMOS Basic using the LOAD command like so: Load "Sample.abk" Load "Sample.abk",6 : Rem Loads sample into bank #6. 238 Music ===== The AMOS music system allows you to easily add an attractive backing track to your games. Music can be created from a variety of sources, including GMC, SOUNTRACKER or SONIX. In order to convert these musics into the special AMOS format, you'll need to use one of the translation programs included on the AMOS data disc. GMC music should have been saved using the SAVE DATA icon, as this copies both the music and the instrument definitions into a single large data file. MUSIC (play a piece of music) MUSIC n The AMOS MUSIC command starts a piece of music from the music bank (#3). This music will be played independently of your Basic program, without affecting it in the slightest. Normally, it's possible to store several complete arrangements in the same bank. Each composition is assigned its own individual music number. The only exception to this rule is music created by GMC, which only allows you to place one song in the bank at a time. Example: Load "MUSIC/Musicdemo.abk" Music 1 The AMOS music system is intelligent, and will automatically suspend your music for the duration of any subsequent sound effects on the current channel. When the sound has finished, your tune will be restarted from its previous position. Up to three separate tunes can be started at a time. Each new music command stops the current song, and pushes its status onto a stack. Once the song has concluded, the old music will commence from where it left off. MUSIC STOP (stop a single section of music) MUSIC STOP Halts the current piece of music. If another music is active, it will be restarted immediately. MUSIC OFF (turn off all music) MUSIC OFF THe MUSIC OFF command deactivates your music completely. In order to restart it, you'll need to execute your original series of MUSIC instructions again from scratch. TEMPO (change the speed of a sample of music) 239 TEMPO s TEMP modifies the speed of any tune which is currently being played with the MUSIC command. s is the new speed, and can range from 1 (very slow) to 100 (very fast). Not all instruments are capable of playing at this maximum speed, however. The practical limit is closer to 50. For a demonstration, place the AMOS data disc into the current drive and type: Load "AMOS_DATA:Music/Musicdemo.abk" Music 1 Tempo 35 Tempo 5 Note that music created with GMC often contains labels which set the tempo directly inside the arrangement. These labels will override the tempo settings within AMOS Basic. So it's not advisable to use them in your own music. MVOLUME (set the volume of a piece of music) MVOLUME n Changes the volume of the entire piece of music to intensity n (0-63). VOICE (activate one or more voices of a piece of music) VOICE mask Activates one or more voices of the music independently. Usually each voice will contain its own separate melody which will combined through your speakers to generate the eventual music. "mask" is a bit mask in the normal AMOS format which specifies which voices you wish to play. Each bit represents the state of one voice in the music. If it's set to 1, the voice will be played, otherwise it will be totally unused. Load "AMOS_DATA:Music/Musicdemo.abk" Music 1 For V=0 To 15 Locate 0,0 : Print "Voice ";V Voice V Wait 100 Next V Direct Voice %0001 : Rem Activate voice 0 240 Voice %0010 : Rem 1 Voice %1001 : Rem 3 and 0 Voice %1111 : Rem 4 =VUMETER (volume meter) s=VUMETER(v) The VUMETER function tests voice v and returns the volume of the current note which is being played by your music. s is an intensity value between 0 and 63. v is the number of a single voice to be checked (0-3). Using this function, you can make your sprites dance to a piece of music! Load EXAMPLE 17.2 for a demonstration. Note there's also an AMAL version of this intruction which allows you to create realtime VU meters using interrupts. See the section on the VU command for more information. Playing a note ============== PLAY (play a note) PLAY [voice,] pitch,delay Plays a single note through the loudspeaker of your TV or Hi-Fi. "pitch" sets the tone of this sound, ranging from 0 (low) to 96 (high). Rather than just being an arbitrary number, each pitch is associated with one of the notes (A,B,C,D,E,F,G). This can be seen from the following table. Octave -------------------------------- 0 1 2 3 4 5 6 7 Note Pitch ---- -------------------------------- C 1 13 25 37 49 61 73 85 C# 2 14 26 38 50 62 74 86 D 3 15 27 39 51 63 75 87 D# 4 16 28 40 52 64 76 88 E 5 17 29 41 53 65 77 89 F 6 18 30 42 54 66 78 90 F# 7 19 31 43 55 67 79 91 G 8 20 32 44 56 68 80 92 G# 9 21 33 45 57 69 81 93 A 10 22 34 46 58 70 82 94 A# 11 23 35 47 59 71 83 95 B 12 24 36 48 60 72 84 96 It should be apparent that the notes go up in a cycle of 12. This cycle 241 is known as an octave. The optional voice parameter allows you to play your notes through any combination of the Amiga's four voices. As usutal it's a bit-map in the format: Bit Voice --- ----- 0 0 Setting a bit to a value of 1 plays the 1 1 relevant voice. "delay" sets the length 2 2 of the pause between the play command and 3 3 the next Basic instruction. This allows you to play each note before preceding the next one. A delay of zero starts a note and immediately jumps to the next Basic instruction. By playing several notes after another, you can easily generate some attractive harmonic effects. Examples: Play 1,40,0 : Play 2,50,0 Wait Key Play 1,40,15 : Play 2,50,15 Do T=Rnd(96) : V=Rnd(15) : Play V,T,3 Loop PLAY is not limited to pure notes incidentally. It's also possible to assign complex waveforms to the sound generator using the powerful WAVE and NOISE commands. Waveforms and envelopes ======================= SET WAVE (define a waveform) SET WAVE wave,shape$ The SET WAVE instruction provides you with the ability to define your very own instruments for use with the AMOS Basic PLAY instruction. The sound of yur instrument depends on the shape of a waveform held in the Amiga's memory. This forms a template which is repeated to produce your final note. "wave" is the number of the waveform you wish to define. Allowable wave numbers start from 2 onwards. That's because waves zero and 1 are already installed. Wave zero holds a random noise pattern for producing explosion effects. Wave one is a smooth sine wave and generates the pure tones used by the standard PLAY instruction. The shapes of your waveform are set using a list of 256 numbers which are entered using the SHAPE$ parameter. Now look at the uppest diagram in the AMOS4.PIC (file included with this manual packet). < picture AMOS4.PIC, the uppest diagram > 242 Each number represents the intensity of an individual section of the waveform. This is equivalent to the height of just one point in the diagram. Possible values for intensity range from -128 to 127. Since AMOS strings are only capable of holding *positive* numbers (0-255), you'll need to convert your negative values into a special internal format before use. The required value can be calculated by simply adding 256 to the negative numbers in your list. Here's a program which demonstrates how the triangular wave in the previous diagram could be created in AMOS Basic S$="" For I=-128 To 127 X=I : If X<0 Then Add X,256 S$=S$+Chr$(X) Next I Set Wave 2,S$ Before playing your waveform you have to tell AMOS Basic which channels are to be assigned to your wave. This can be achieved using the WAVE command. Add the following line to the previous routine Wave 2 To 15 : For S=10 To 60 : Play S,10 : Next S The Best way to reproduce the effect of a real instrument is to combine several SINE waves together. An example of one of these sine waves can be seen in the picture AMOS4.PIC: < picture AMOS4.PIC, the diagram in the middle > 243 Adding several of these waves together, with different sizes and separate starting points, produces waves in the following pattern: < picture AMOS4.PIC, the lowest diagram > This generates the smooth harmonics needed for your notes. Here's an example: SHAPE$="" : Degree For S=0 To 255 V=Int((Sin(S)/2+SIN(S*2+45)/4)*128)+127 SHAPE$=SHAPE$+Chr$(V) Next S Set Wave 2,SHAPE$ : Wave 2 to 15 For N=10 to 60 : Play N,10 : Next N WAVE (assign a wave to one or more sound channels) WAVE w To v WAVE assigns wave number w to one or more sound channels. v contains a bit-map in the standard format. If a bit in the pattern is set to 1 then the approrpriate voices are used by PLAY, otherwise they will be 244 completely unaffected. As a default, wave zero is reserved for the NOISE channel, and wave one contains a sine wave. Here are some examples: Wave 0 To %0001 Play 1,40,0 Wave 0 To %1100 Play 20,10 Wave 1 To %1111 Play 60,0 NOISE (assign a noise wave to a channel) NOISE TO voices Applies a white noise effect (wave 0) to the selected voices. Load EXAMPLE 17.3 for a demonstration. "voices" is a standard bit pattern. The first four bits represent the four possible voices, starting from zero. NOISE is equivalent to the command: Wave 0 To voices Examples: Noise To 15 Play 60,0 Play 30,0 DEL WAVE (delete a wave) DEL WAVE n Deletes a wave which has previously been defined using SET WAVE. n is the number of the wave, and starts at 2. It's impossible to delete the built-in NOISE ans SINE waves using this instruction. After the wave has been erased, all voices will be reset to the standard SINE wave (default). SAMPLE (assign a sample to a wave) SAMPLE n TO voices This is the most powerful cersion of all the wave commands. It assigns a sample stored in the sample bank to the current wave. Play will now take an instrument straight from the sample bank. Load "Samples/sample1.abk" 245 Sample 1 To 15 For I=20 To 50 Play I,50 Next I As usual "voices" allows you to select a range of voices to be set by the instruction. It's a standard bit-map; Bit 0 -> Voice 0 etc... Note!: The range of notes that a sample can be played with, depends on its original recording rate. If a note is too high, AMOS may not be able to play it at all. The acceptable range varies from a sample to sample, but it's usually between 10 and 50. SET ENVEL (create a volume envelope) SET ENVEL wave,phase TO duration,volume The SET ENVEL command smoothly changes the volume of a note while it's being played. In the real world, sounds don't just sprint into existence fully formed. They tend to evolve over a period of time, according to a pattern known as the volume envelope. The shape of this envelope varies depending on the type of instrument you are playing. A typical example of one of these envelopes is shown in the picture AMOS5.PIC. < picture AMOS5.PIC > The sound is split up into four phases: Attack decay, sustain and release. AMOS Basic allows you to define your envelopes using up to 246 seven separate steps. Each step represents a steady change in the volume of the current note. "wave" is a number of the waveform which will be affected by this instruction. It's possible to use any waveform you like for this purpose, including the built-in NOISE and SINE generators. "phase" holds the number of the particular phase which is to be defined, ranging from 0 to 6. "duration" specifies the length of the current step in units of a 50th of a second. This determines the apparent speed of the volume change to be generated in this phase. "volume" specifies the volume which is to be reached by the end of this phase. Allowable volume levels range from 0-63. It's important to understand that this volume is relative to the intensity you've previously st with the VOLUME command. So even if the note is quiet, the shape of the envelope will be perfectly reserved. Now for some examples: Set Envel 1,0 To 200,63 : Rem Sets the 1st step. Play 40,0 As you can hear, the volume of your sound starts from zero, and increases to a maximum intensity during the length of the note. Now let's try defining something a little more complicated. Set Envel 1,0 To 15,60 Play 40,0 : Wait Key Set Envel 1,1 To 1,50 Play 40,0 : Wait Key Set Envel 1,2 To 10,50 Play 40,0 : Wait Key Set Envel 1,3 To 50,0 Play 40,0 Finally, here's an example of a NOISE envelope: Noise To 15 Set envel 0,0 To 1000,30 Play 40,0 Wait Key Music Off Don't confuse waves and envelopes. A wave sets the frequency components of your notes, whereas an envelope simply changes their volume according to a set pattern. Speech ====== Your Amiga is supplied with a powerful speech synthesizer program which can be found on the standard Workbench disc. With the help of this routine, your AMOS programs can be made to speak. Speech is especially userful in education, as many yound people will respond far better to the spoken word than to boring text. One word of caution though. Since the narrator package is independent of AMOS Basic, we can't attest to its absolute reliability. You're unlikely to encounter any serious problems, but it's well worth treating it with a littlle care. SAY (speak a phrase) 247 SAY t$[,mode] The SAY command is incredibly easy to use. Enter your text in normal English, concluding your phrase with a punctuation mark such as full stop. SAY will now translate your words into an internal format and speak them directly through your loudspeaker. Example: Say "AMOS Basic can really speak" The first time you use this instruction, the narrator.device will automatically be loaded from disc. So it's vital to ensure that an appropriate disc is placed in the current drive before using this system, as otherwise you may get an Intuition style requester box. "mode" toggeles between two separate speech modes. As a default, your program will wait for the duration of the speech, and any music or sound effects will be temporarily suspended. Setting "mode" to a value of one activates multitasking system which allows you to output your speech whilst AMOS is executing your program. Inevitably, this will slow down your basic routines Considerably. To return your speech back to normal, set mode to zero. If the narrator system cannot understand what you are attempting to speak you won't get an error messagel, but the command will be automatically aborted. Also note that the narrator can occasionally get slightly confused with very short sentences. Sometimes the remainder of the previous phrase is tagged to the end of the current voice. The problem can be solved by simply adding a list of spaces to the end of your text. These will wipe out the unwanted speech data. SET TALK (set speech effects) SET TALK sex,mode,pitch,rate This allows you to change the type of voice which will be used by the SAY command. "sex" chooses between a male (0) or female (1). In all honesty, it's not a particularly realistic rendition. Better effects can be created by simply increasing the frequency of the voice using the pitch parameter. "mode" adds a strange rhythmic pattern to the voice. This can be activated by setting "mode" to a value of 1. "pitch" changes the frequency of the voice, from 65 to 320. "rate" specifies the speed, measured in the words per minute (40-400). 248 Any of the above parameters can be omitted if required. Providing you keep the commas in their normal positions, you can change any set of options independently. Filter effects ============== LED (activate a high pass filter/ change power led) LED ON/OFF The LED command has two completely separate actions. Not only does it toggle the POWER led on your Amiga's console (in KickStart versions 1.3 just makes the led a little darker), but it also controls a special high pass filter. The filter changes the way high frequency sounds are treated by the system. Normally, these sounds are filtered out so as to avoid the risk of unwanted distorion effects. Unfortunately, this robs many percussion instruments of their timbre. By turning off the filter, you can recapture the essential quality of many instruments.