|
|||||||
Play it Again, CamCameron Christie, who has contributed several routines to the REVMEDIA library, now looks at some little used R/BASIC statements and a possible application for them. I've been running R/BASIC programming courses for some time now, and the most frequent question arising is not about system subroutines or WINDOW_COMMON%; instead I get "Is R/BASIC any good for writing games?". Unfortunately, the short answer is: No. The main differences between games and 'ordinary' programs (such as reports) are usually graphics and sound. Since ARev uses a character graphics screen there's not much we can do about fast and fancy graphics. VIDEO.RW can be used to achieve some rudimentary animation (if you're more adventurous, try dabbling with VSPACE!), but the bottom line is that R/BASIC is pretty much useless unless it's used to call C or Assembler routines. Sound, however, is a more realistic proposition altogether. Using standard R/BASIC, the only sound available is the short, monotone beep, achieved by printing CHAR(7). With just a little knowledge of the PC's internal workings, some musical theory, and a few lesser-used R/BASIC statements, your AREV system could soon play anything from Sibelius to Springsteen. A routine called PLAY, which mimics the functionality of the GW-BASIC PLAY statement, will be available on the next REVMEDIA diskette. In anticipation of this, the theory behind the program is as follows. The timer chip within the PC (the 8253-5) generates clock ticks based on a 1.193 MHz base frequency. To produce an audible tone, the chip's behaviour can be manipulated using the R/BASIC OUT statement - see the Technical Reference Manual for more details. An initialising value of 182 is sent to port 67, followed by the low & high order bytes which are sent to port 66. (FIG. 1). A general warning applies to use of the OUT statement: if your PC is not fully IBM compatible, or you send bits to the wrong address, then unpredictable and nasty side effects may occur - be careful! Now we must go directly to the speaker and activate it for the duration of the tone we have established. (FIG 2) The speaker is controlled by the programmable peripheral interface and is accessed via port 97. If the first two bytes at this location are on then the sound will be heard ; if they are off the speaker will be deactivated. To manipulate the bits within a byte we use here the BITOR and BITAND functions, along with OUT and its opposite number, INP - again, see the Technical Reference Manual. (Those among you who like to use obscure code might like to try X = - BITNOT(X) instead of X += 1; it's three times slower, but you can't fault its opacity!) Finally, a quick word about FIG 3, which calculates the frequency required for a given musical note. An octave is actually defined as a twelve note scale (including semitones) where the first note of an octave has a frequency twice that of the corresponding note in the previous octave. Therefore, given the frequency of one note, we can work out the frequency of any other note by knowing the interval between them. An alternative method would be to actually hold an array of all the required frequencies in the program; this is actually much more efficient! For more details on sound generation, see The Peter Norton Programmer's Guide to the IBM PC (Microsoft Press) FIGURE 1.0001 * For a given FREQUENCY in Hz. 0002 VALUE = 1193180/FREQUENCY 0003 LOW = INT(MOD(VALUE,256)) 0004 HIGH = INT(VALUE/256) 0005 OUT 67,182 0006 OUT 66,LOW 0007 OUT 66,HIGH FIGURE 2.0001 * For a given DURATION in seconds. 0002 * Set first 2 bits at speaker address to begin sound. 0003 0004 LAST.SETTING = INP(97) 0005 NEW.SETTING = BITOR(LAST.SETTING,03) 0006 OUT 97,NEW.SETTING 0007 * Timing Control Loop - DURATION is set elsewhere in the program. 0008 * See REVMEDIA passim for documentation on the DOSTIME subroutine. 0009 DOSTIME(START) 0010 LOOP 0011 DOSTIME(FINISH) 0012 UNTIL (FINISH-START) > DURATION 0013 REPEAT 0014 * Reset first 2 bits at speaker address to end sound. 0015 NEW.SETTING = BITAND(LAST.SETTING,252) 0016 OUT 97,NEW.SETTING FIGURE 3.0001 * For a given NOTE and OCTAVE (derived, e.g, from a passed parameter), 0002 * calculate the FREQUENCY used by FIG 1. 0003 NOTES = "C,Cs,Db,D,Ds,Eb,E,F,Fs,Gb,G,Gs,Ab,A, NOTES := "As,Bb,B" POSITIONS = "1,2,2,3,4,4,5,6,7,7,8,9,9,10,11,11,12" CONVERT "," TO @FM IN NOTES CONVERT "," TO @FM IN POSITIONS * Note A4 is defined as the root note, frequency = 440.00 Hz. BASE.FREQUENCY = 440 * Set A4 note position within scale. (4 full octaves of 12, plus 10). BASE.POS = 58 LOCATE NOTE IN NOTES USING @FM SETTING POS THEN POSITION = POSITIONS<POS> + OCTAVE*12 OFFSET = POSITION - BASE.POS FREQUENCY = (BASE.FREQUENCY * (2**(0FFSET/12))) END" (Volume 2, Issue 5, Pages 4,5) |
|||||||
| |||||||