4d 03: • , f • . 1 , 41 !RI 47.030 ;0:1
LEARN/ OfAMMING - FOR FU liND THE FUTURE HOW TO ORDER YOUR BINDERS UK and Republic of Ireland: Send £4.95 (inc p & p) (1R£5.95) for each binder to the address below: Marshall Cavendish Services Ltd, Department 980, Newtown Road, Hove, Sussex BN3 7DN Australia See inserts for details, or Vol. 2 No 23 write to INPUT, Times Consultants, PO Box 213, Alexandria, NSW 2015 BASIC PROGRAMMING 50 New Zealand: See inserts for details, or write to INPUT, Gordon and Gotch (NZ) Ltd, PO Box 1595, Wellington YOU HUM IT—I'LL PLAY IT 701 Malta: Binders are available from local newsagents.
How to turn any tune into a computer program
BASIC PROGRAMMING 51
MORE SORTING METHODS
There are four binders each holding 13 issues. For more speed and efficiency
PERIPHERALS BACK NUMBERS Back numbers are supplied at the regular cover price (subject to availability). BULLETIN BOARDS 71.2 UK and Republic of Ireland: 1111 INPUT, Dept AN, Marshall Cavendish Services, Newtown Road, Hove BN3 7DN The computerized information exchange Australia, New Zealand and Malta: Back numbers are available through your local newsagent. GAMES PROGRAMMING 23
ON-SCREEN FLIGHT SIMULATOR 716
Take over the controls at 2,000 metres up COPIES BY POST Our Subscription Department can supply copies to any UK address regularly at £1.00 each. APPLICATIONS 13 For example the cost of 26 issues is £26.00; for any other quantity simply multiply the number of issues required by £1.00. Send your order, with payment to: Subscription Department, Marshall Cavendish Services Ltd, ' DGS, MAPS EA Newtown Road, Hove, Sussex BN3 7DN Please state the title of the publication and the part from which you wish to start. No messing about with paper—plan them on the screen HOW TO PAY: Readers in UK and Republic of Ireland: All cheques or postal orders MACHINE CODE 24 for binders, back numbers and copies by post should be made payable to: Marshall Cavendish Partworks Ltd.
QUERIES: When writing in, please give the make and model of your computer,as well as the Part No., page and line where the program is rejected or where it does not work. We can only answer specific queries– and please do not telephone. Send Special sound effects based on a simple command your queries to INPUT Queries, Marshall Cavendish Partworks Ltd, 58 Old Compton Street, London W IV SPA. INDEX The last part of INPUT, Part 52, will contain a complete, cross-referenced index. For easy access to your growing collection, a cumulative index to the contents INPUT IS SPECIALLY DESIGNED FOR: of each issue is contained on the inside back cover. The SINCLAIR ZX SPECTRUM (16K, 48K,128 and +), COMMODORE 64 and 128, ACORN ELECTRON, BBC B PICTURE CREDITS and B+, and the DRAGON 32 and 64. Front cover, Paul Chave/Ian Stephen. Pages 701, 706, Phil Dobson. Pages 702, 703, 704, 705, 706, Berry Fallon Design. Page 709, Paul Chave/Spectrum. Page In addition, many of the programs and explanations are also 710, Sporting Pictures. Page 712, Will Stephen. Page 715, Kevin O'Brien. Pages suitable for the SINCLAIR ZX81, COMMODORE VIC 20, and 716, 718, Paul Chave/ Ian Stephen. Page 720, 727, Peter Reilly. Page 721, Grant Simon. Page 723, Grant Simon/Ian Stephen. Page 724, Tudor Artists. Pages TANDY COLOUR COMPUTER in 32K with extended BASIC. 728, 730, Jeremy Gower. Programs and text which are specifically for particular machines are indicated by the following symbols: © Marshall Cavendish Limited 1984/5/6 All worldwide rights reserved. SPECTRUM 16K, The contents of this publication including software, codes, listings, 48K,128, and + COMMODORE 64 and 128 graphics, illustrations and text are the exclusive property and copyright of Marshall Cavendish Limited and may not be copied, reproduced, ACORN ELECTRON, transmitted, hired, lent, distributed, stored or modified in any form BBC B and B+ UDRAGON 32 and 64 whatsoever without the prior approval of the Copyright holder. friM: T TANDY TRS80 Published by Marshall Cavendish Partworks Ltd, 58 Old Compton Street, London WIV 5PA, ZX81 VIC 20 COLOUR COMPUTER England. Printed by Artisan Presss, Leicester and Howard Hunt Litho, London. MUSICAL NOTATION A MUSICAL RULER KEY SIGNATURES ADDING RHYTHM PLAYING A TUNE
The real beauty of computer music so cannot play chords. Vertical lines called is that the machine plays it for you. STANDARD MUSICAL NOTATION `bar lines' divide up the music horizontally Find out how to transcribe any Music is usually written on one or two groups into groups of notes called `bars'; each bar has written music into a program that of five horizontal lines, called 'staves' (the the same duration as any other bar, though singular is 'staff). Various note symbols are each one may be internally divided into any remembers your favourite tunes placed on or between the lines of the staves, number of notes, as long as its total their height indicating the pitch, the order duration is the same as other bars. Music has two main elements—pitch and indicating the order of notes in the piece (they rhythm. The first article on music (pages 669 are played from left to right) and the shape of PITCH INFORMATION to 675) dealt mainly with pitch, which the the symbols indicating the notes' durations. The keyboard program in the last article computer provided subject to which keys you Notes which line up vertically are played turned the computers' keyboards into some- pressed, with you adding a touch of rhythm. together to sound as chords, but the programs thing resembling a musical instrument. While This article not only adds a predetermined in this article play only one voice at a time and the programs worked very well, they are rhythm to the music that the computer plays, inevitably limited because of the small num- but also explains how you can turn any piece ber of keys on a computer keyboard compared of sheet music into the DATA that your with the total number of notes you can use— computer needs to play the written tunes. not to mention the difficulty of fingering them. But there is no reason why you should It can be very confusing, and time- in the table and it's probably easier to use these be confined to the same limits when you use consuming, to translate each note on a sheet of for the Spectrum (where the numbers are tunes in your own programs. music into the pitch and duration values for simple) or the Dragon and Tandy (which use You can quite easily convert any piece of your computer, especially if you cannot read letters). The number for the Commodores written music into the numbers that your music. But there is an easy method. and Acorns are more complicated so it's easier computer needs for each of these notes with- If you look at the diagram, you can see that to use the general scale and then convert them out needing very much knowledge of music: pitch values from 1 to 37 are marked beneath in the program. To find out what to mark on just use the 'musical ruler' described later on each note. These are the basis for the numbers each line, compare the numbers on the dia- in this article. But, first, here is a brief you need in your program. gram to the table, and read off your explanation of some of the basics behind You could refer to this diagram whenever computer's equivalent number. written music. you wanted to 'transcribe' a tune, but even that would mean a lot of time spent compar- SHARPS AND FLATS UNDERSTANDING THE NOTES ing your sheet music to the diagram and Unless you convert only pieces of music Fig. 1 shows a typical piece of written music. finding the right note. A much easier method written in C Major, you will at some time This would just play each note available on a is to have a measure of some sort which you need to transcribe sharps and flats—and these keyboard in a rising succession—a 'chroma- can place over any note on a musical staff, and do not have the same pitch as the natural note. tic' scale. just read off its pitch value for your computer. By looking at the key signature, you can see The large symbols at the extreme left of the what, if any, flats or sharps the tune uses (how staves are called 'clefs': the top one is called A PITCH RULE to understand key signatures is explained the 'treble clef and the lower one the 'bass To make this, all you need to do is to mark a later in this article). Any sharps or flats mean clef'. The clefs simply give the note symbols piece of paper with a musical scale. Luckily, you need to change the values on your ruler. on the staves a pitch—without a clef, the pitch this only takes a minute or so, although you Sharps and flats are the black keys on the of any note on a staff is indeterminate. probably need a different scale for each piece drawing of the keyboard, and their pitch Note symbols are placed either between of music, as the size of the staves can vary. numbers are provided. So, for example, the two lines, or squarely over a line. First, place your paper over the staff, and pitch number of the lowest C sharp is 2 (using Notes above or below the staves are related copy onto it the staff lines showing the the general scale of pitch numbers in the table, to the staves by means of leger lines'; so the possible positions of a note. Then all you do is you can see what this is for your micro). This note Middle C lies on a leger line midway write the pitch number for each note against is the same as the pitch number for D flat, and between the two staves, ..nd the three highest the corresponding mark. This ruler can is between 1 and 3, the pitch numbers for C notes in the diagram are on leger lines above trace pitch values directly off the sheet music. and D. the upper staff, at the righthand end. The actual values for each pitch are given But sharps and flats are not shown on the the staff, after the clef. These marks are called Spectrum Commodore Vic Acorn Dragon/ the key signature. (You should note that the General High Low Tandy words 'key' and 'scale' mean the same, so the scale byte byte key of C Major means the same.) 1 — 12 8 97 192 5 C The absence of a key signature indicates 2 — 11 8 225 197 9 C + that the key is C Major, which is a kind of 3 — 10 9 104 200 13 D `default' key, but a key signature of one or 4 — 9 9 247 203 17 D + more sharps or flats means the piece is in a 5 — 8 10 143 206 21 E different key. In a key signature of one sharp, 6 — 7 11 48 208 25 F the sharp sign is at the position at which the 7 —6 11 218 211 29 F+ note F is placed. Its effect is to sharpen all the 8 — 5 12 143 214 33 G Fs, in all octaves, throughout the piece. 9 — 4 13 78 216 37 G + You may remember from the earlier article 10 — 3 14 24 218 41 A that it's necessary to sharpen the Fs to obtain 11 — 2 14 239 220 45 A + the arrangement of musical intervals for B 12 — 1 15 210 222 49 B Major. The key signature of one sharp 13 0 16 195 224 53 C achieves this, and it's known as the key 14 1 17 195 226 57 C + signature of B Major. So if you're transcrib- 15 2 18 209 227 61 D ing a piece that has some sharps or flats just to 16 3 19 239 229 65 D + the right of the clefs, remember to modify all 17 4 21 31 231 69 E the Fs or Cs, or whatever, for the whole piece, 18 5 22 96 232 73 F and use the corrected pitch number. 19 6 23 181 233 77 F + Fig. 4 shows the first few notes of Pop Goes 20 7 25 30 234 81 G the Weasel in the key of F Major, that is with a 21 8 26 156 235 85 G + key signature of one flat. The pitch numbers 22 9 28 49 236 89 A are shown: notice that the fourth note is B flat, 23 10 29 223 237 93 A + with a pitch number of 23 on the general 24 11 31 165 238 97 B scale. This is where your ruler comes in again. 25 12 33 135 239 101 C If your piece of music contains an F sharp, 26 13 35 134 240 105 C + instead of writing the pitch number for F next 27 14 37 162 N/A 109 D to the F mark on your scale, you should write 28 15 39 223 ,, 113 D + the number for F sharp.
29 16 42 62 )3 117 E By doing this for any sharps or flats in the
30 17 44 193 5) 121 F key signature, sharps and flats become just as
31 18 47 107 15 125 F + easy to transcribe as natural notes. 32 19 50 60 ,, 129 G You should be careful, though: sometimes
33 20 53 57 5) 133 G + you may come across accidental sharps, flats 34 21 56 99 ,, 137 A or naturals—these have priority over a key 35 22 59 190 ,, 141 A + signature. 36 23 63 75 ,, 145 B A MATTER OF TIMING 37 24 67 15 31 149 C Once you've worked out the pitch of each note, the next thing is to work out its duration, staves in the diagram to prevent it from altered. A sharp or flat sign used like this is or how long it sounds for. You'll also have to becoming too cluttered. A sharpened note is known as an accidental. Its effect lasts until notated on a staff with its note symbol the end of the bar it's used in, unless its effect preceded by a 'sharp' sign, (which is like the is cancelled before this point. There's a third computer 'hash' character); a flattened note symbol, called a natural, which cancels a appears on the staff preceded by a 'flat' sign, sharp or flat and restores the note to its un- (which is like a lower case t'). sharpened and un-flattened state. Fig. 3 Fig. 2 shows how the first few notes from shows the first few notes of Oh, I Do Like To middle C, including sharps and flats, are Be Beside The Seaside, including an 'E' shown on the upper staff. Notice that while C sharpened by an accidental and naturalized a sharp and D flat share the same pitch number couple of notes later. and the same black key, they appear at different heights on the staff—since one is a C KEY SIGNATURES that has been raised, and the other is a D that Where the scale in which a piece is played has been lowered. contains a sharp or flat note, this note needs to Individual notes in a piece may be shar- be used instead of the natural every time. To pened or flattened, using the appropriate signify that it is always a sharp or flat, the symbols immediately before the notes to be appropriate sign is marked at the beginning of general pitch. To find the actual pitch num- An important point about the way the bers for each computer you can look at the computers operate applies to all the table given earlier in the article. It also shows programs. Suppose one bar contains a single, the relative duration values. long note, and another contains many short There are several points here about the way notes. In the second case the lines that READ the music is written. Firstly, where three and test the DATA will be executed more times `quavers' (the table shows what the notes are than in the first case, and so the speed at called), occur together, as in bars 9 and 11, for which they are played will fall a little because example, their stems have been joined tog- of the extra operating time. Therefore the ether. Small groups of two, three, or four speed of execution of these lines should be as quavers and semi-quavers are often joined high as possible. This is why there is no test like this, to make it easier to read the music. for the end of DATA. But apart from that, the notes are exactly the A convention could be used in which a note down the duration of rest symbols which same—the joins do not change the pitch or negative number, say, signalled the end of the indicate gaps, or periods of silence between duration of the notes. tune; this would require an extra test in these notes. Fig. 5 is a table that shows the note In bar 8, there is a similar join—but this crucial lines and would make the tune slightly symbols and corresponding rest symbols, time it does change the notes a bit. The join uneven. So the program is just allowed to run with their relative durations. It is a very good here is between just two notes and is a curved out of data, when it stops with an error idea to use relative durations for notes and line instead of a straight one. What it means is message, something that is not normally then choose a tempo which defines their that the notes should be joined together to considered good practice. actual durations at a later stage—that way, sound like one, longer, note. The join is Of course, when you use a tune in your own you can quickly and easily experiment to find known as a tie. You can have ties joining any programs, you can use a FOR ... NEXT loop to the tempo which sounds best. You can see number of notes together. To convert them READ the exact number of pieces of DATA, how this works in practise from the program into computer data, you should add up the which would prevent the error occurring. further on in this article. individual durations of the notes to give one Here are the programs to play the tune Both American and English names for the long note. Three Blind Mice: symbols are given: American names are more logical, though they will be less familiar to PROGRAM DATA a British readers. Generally, the stems on the The pitch and duration numbers you've 10 INPUT "ENTER TEMPO (1 — 50)",t: LET notes may point upwards or downwards, worked out for the tune are put into DATA t=t/100 whichever direction looks neater. statements at the end of the program from 20 IF t < 0.001 OR t>0.5 THEN GOTO 10 The two longest rest symbols are little where they are READ and interpreted as the 30 FOR n = 0 TO 1 STEP 0: READ a,b: BEEP rectangular blocks which lie above or below program runs. The DATA for Three Blind Mice a't,b: NEXT n the middle line of the staff. Following a note has been added to the programs in this way. 100 DATA 6,4,6,2,12,0,6,4,6,2,12,0 or a rest by a dot multiplies its duration by There is enough DATA in each statement for 110 DATA 6,7,4,5,2,5,12,4,6,7,4,5,2,5,10,4,2,7 one and a half; a doubly-dotted note has a two bars except on the Dragon and Tandy. 120 DATA 4,12,2,12,2,11,2,9,2,11 duration one-and-three-quarters the duration Some convention of this type is sensible, as it 130 DATA 4,12,2,7,4,7,2,7 of the undotted note. lets you find the DATA for a given bar easily. 140 DATA 2,12,2,12,2,12,2,11,2,9,2,11 The clef at the beginning of a piece of The total duration of each bar should be 150 DATA 4,12,2,7,4,7,2,7 music is followed by the key signature, if one the same, 12 units in the example used. So, if 160 DATA 2,12,2,12,2,12,2,11,2,9,2,11 is present, then by the time signature. This you transcribe a piece of music and the 170 DATA 4,12,2,7,4,7,2,5 consists of two figures placed one above the rhythm doesn't sound right, check that each 180 DATA 6,4,6,2,12,0 other. Essentially, the upper figure shows bar contains the same number of duration how many beats or pulses there are in each units, and correct any errors if you find them. When you type in and RUN this program, you bar, and the lower figure shows the duration The screen-editing facilities of each computer will be asked to INPUT a number between 1 of each of these beats. So 4/4 indicates that allow you to duplicate DATA statements con- and 50 for the tempo. As with the other there are 4 crochet beats per bar. Again, you taining bars that are repeated, which can save programs, the value that you enter here is not need not fully understand the significance of a lot of time. actually the tempo, but the inverse tempo. this if you are just transcribing sheet music What the computer does is to multiply the into DATA tatements for the music programs, TEMPO relative values for duration of each note by although you might like to use it as a guideline The duration values give the relative duration this tempo value—so, a small number gives for choosing the tempo. of the notes, but they don't dictate its tempo, you a fast speed, while a large number gives or absolute speed. When you RUN the you a slow speed. TRANSCRIBING A SIMPLE TUNE program, you will be asked to provide a tempo The reason for using inverse tempo is that You can now begin to transcribe a piece of number which controls this: a small number it makes the programs simpler, and shorter. written music into the numbers or letters that will make the tune play fast, a higher number There is an IF ... THEN check to make sure your computer uses to play the tune—both makes it play slower. In fact, the number you you INPUT a reasonable value, in Line 20. for pitch and duration. INPUT is a sort of inverse tempo, since in Line 30 then actually plays the tune. Inside Fig. 6 shows the rhyme, Three Blind Mice, music a large tempo number means a fast a FOR ... NEXT loop, it READS two pieces of written on a musical staff, along with the speed, not a slow one. An inverse tempo is DATA for each note—the computer's pitch numbers and letters which represent the used here as it is easier to program. value, and a relative duration value. Then, it Line 10 lets you INPUT the tempo value: values of 30-50 are about right. The subrout- ine at 3000 initializes the SID chip, and the one at 4000 sets up arrays HQ% and LQ% for the 37 pitches, and fills them with the values for the high and low bytes which have to be PO KEd into the SID chip. The loop from 100 to 160 is responsible for READing and inter- preting the DATA statements containing the music information at 10000 onwards. Line 100 reads the current pair of pitch and duration values into variables P and D. Line 120 switches on the envelope for voice 1, and POKES the values for the specified pitch into the SID chip (variables SE, EN, SL and SI are used for speed: they are initialized in the subroutine at 3000). Line 110 tests the pitch and bypasses this line if the pitch is 0, BEEPS before READing the next items of DATA. 3050 POKE SI +24,4 signifying the note is a rest. When you use the routine for your own 3060 RETURN Line 130 is a delay loop which gives the tunes, you can change the numbers in the FOR 4000 DIM HQ%(37), LQ%(37) length of the note: the delay depends on the ... NEXT loop so that it READS the correct 4010 TMP=2227:P2=21(1/12) tempo and the duration value. Line 140 number of items of DATA. At the moment, the 4020 FOR 1=1 TO 37 switches the envelope off, and Line 150 is FOR ... NEXT loop has a STEP of 0 so it never 4030 LQ%(I)=TMP-256'INT(TMP/256): another delay loop to give the 'breaks' be- reaches the end of the loop and only stops HQ%(I) =TMP/256 tween notes. The 'off ' time given is a third of when the DATA has run out, with an E out of 4040 TMP=TMP*132 the length of the 'on' time. If the first value in DATA message. 4050 NEXT: RETURN this FOR ... NEXT loop is altered, the ratio of 10000 DATA 17,6,15,6,13,12 `on' to 'off' time can be altered, making the 10002 DATA 17,6,15,6,13,12 notes more separate if the off time is length- 10 INPUT "TEMPO(30-50)";TP 10004 DATA 20,6,18,4,18,2,17,12 ened or flow together more if the off time is 20 GOSUB 3000 10006 DATA 20,6,18,4,18,2,17,10,20,2 shortened. 30 GOSUB 4000 10008 DATA 25,4,25,2,24,2,22,2,24,2,25, The upper limit of both FOR . . . NEXT loops 100 READ P,D 4,20,2,20,4,20,2 should be derived from D'TP to ensure that 110 IF P=0 THEN 130 10010 DATA 25,2,25,2,25,2,24,2,22,2,24, both the 'on' and `off' times are related to the 120 POKE SE,EN: POKE SL,HQ%(P): POKE 2,25,4,20,2,20,2,20,2,20,2 tempo and the duration parameter. The at- SI,LQ%(P) 10012 DATA 25,4,25,2,24,2,22,2,24,2,25, tack, decay, sustain and release values set in 130 FOR DL=1 TO D'TP: NEXT 2,20,2,20,2,20,4,18,2 Lines 3030 and 3040 also affect the 'shape' of 140 POKE SE,EF 10014 DATA 17,6,15,6,13,12 the notes which controls the quality of the 150 FOR DL=1 TO D'TP/3: NEXT sound. 160 GOTO 100 This program uses pitch values from 1 to 37, 3000 SI = 54272 and then changes them into SID chip values CKI 3010 SL=SI +1: SE=SI + 4: EN = 33: in the subroutines starting at Lines 3000 and 10 INPUT "TEMPO(30 —60)"; TP EF =32 4000. You could, if you wished, just use the 20 GOSUB 4000 3020 FOR I =SI TO SI +28: POKE 1,0:NEXT I correct DATA to start with and then miss out 30 RV =36874:K0 = 0:K1=1 3030 POKE SI +5,16'1 +9 the calculation. This is, however, slightly 40 POKE 36878,4 3040 POKE SI +6,1615+ 9 more difficult to work out. 100 READ P,D 5 Note symbol European name American name Rest symbol Relative duration Duration if dotted
CI■ Semibreve Whole note 70- 16 24 J Minim Half note AM_ 8 12 Crotchet Quarter note 4 6 dJ Quaver Eighth note 2 3 iii 47
Semiquaver Sixteenth note 1 1.5 iii produces three different notes, each an octave above or below the others. So the three octaves of the range are each handled by a different VIC sound voice. This may seem awkward, but it enables you to produce better tuning than is otherwise possible. The three pitch numbers in the range 1 to 37 are reduced to numbers in the range 1 to 13, and the correct pitch value is then taken from the array TA% and POKEd into one of the three sound-controlling registers. Lines 120-140 transform the pitch numbers to fall within this range and also select the correct sound-controlling register, whose address is left in the variable RE. (The register for the lowest octave is at 36874, the middle one is at 36875 and the upper one is at 36876). Line 160 contains a delay loop governing the note's `on' time, and Line 180 is a loop controlling its `off' time; the logic is similar to that of the Commodore 64 program above. Line 150 switches the note on by poking the correct value into the chosen VIC sound register, and Line 170 switches it off, after the first delay loop, by setting this register to 0. The variables KO, Kl, RV, RG are used to shorten the time taken to process the main loop, so as to achieve as regular a rhythm as possible.
10 I N PUT"TEM PO (20-30)",TP 20 TP =TP/20 90 ENVELOPE1,1,0,0,0,0,0,0,30, — 2,0, — 5,100,50 95 ENVELOPE2,1,0,0,0,0,0,0, — 127, —127,-127,-127,0,0 100 READ P,D 110 IF P= KO THEN 160 10010 DATA 25,2,25,2,25,2,24,2,22,2,24, 110 IF D*TP > 254 THEN D =254/TP 120 RG = RV 2,25,4,20,2,20,2,20,2,20,2 120 IF P=0 THEN SOUND1,2,0, 130 IF P>13 THEN P=P-12: 10012 DATA 25,4,25,2,24,2,22,2,24,2,25, D'TP:GOTO 100 RG =RG + K1 2,20,2,20,2,20,4,18,2 150 SOUND1,1,49+ P*4,0*TP 140 IF P>13 THEN P=P-12: 10014 DATA 17,6,15,6,13,12 RG =RG + K1 150 POKE RG,TA%(P) As in the Commodore 64 program, the first 160 FOR I= KO TO TrD:NEXT line reads in the tempo value, which should be 170 POKE RG,K0 between 30-60 for the tune given. The 180 FOR I = KO TO TP/3*D:NEXT subroutine at 4000 initializes the array TA% to 190 GOTO 100 hold values to be POKEd into the VIC chip's 4000 DIM TA%(37) sound registers. 4011 FOR 1=1 TO 13: READ The loop from 100 to 190 is responsible V:TA%(I) =255— V: NEXT for READing and interpreting the DATA state- 4020 RETURN ments containing the music information at 4030 DATA 90,85,80,76,72,67,64,60,57, 10000 onwards. Line 100 READS the curren 54,51,48,45 pitch and duration values into variables P an 10000 DATA 17,6,15,6,13,12 D. Line 110 tests for a rest, and bypasse 10002 DATA 17,6,15,6,13,12 Lines 120-150, which handle the VIC chip 10004 DATA 20,6,18,4,18,2,17,12 values, if it detects one. 10006 DATA 20,6,18,4,18,2,17,10,20,2 To obtain the 3 octave range, all three 10008 DATA 25,4,25,2,24,2,22,2,24,2,25, sound voices of the VIC chip are used: the 4,20,2,20,4,20,2 same number POKEd into the three registers , 200 GOTO 100 by the queuing system. The Dragon and Tandy program begins by 10000 DATA 17,6,15,6,13,12 Line 100 reads in the current pitch and letting you INPUT a value which the program 10002 DATA 17,6,15,6,13,12 duration pair. Line 110 tests for a rest. If one uses to determine the speed of the tune, its 10004 DATA 20,6,18,4,18,2,17,12 is called for, it executes a rest of the required tempo. Line 20 checks to see whether the 10006 DATA 20,6,18,4,18,2,17,10,20,2 duration (D times tempo) using the 'null' value you INPUT is less than 1, which is too 10008 DATA 25,4,25,2,24,2,22,2,24,2,25,4, envelope so no sound is made, and returns to small, and if it is, the computer returns to 20,2,20,4,20,2 Line 100. Line 150 plays an ordinary note. Line 10 for you to enter a new value. 10010 DATA 25,2,25,2,25,2,24,2,22,2,24,2, The expression 53 + (P —1)'4 converts the Line 30 READS two variables—one for the 25,4,20,2,20,2,20,2,20,2 pitch numbers 1-37 into the form expected by pitch (the letter) and one for the duration of 10012 DATA 25,4,25,2,24,2,22,2,24,2,25,2, the SOUND command: 1 is converted into 53, the note. To enter sharps add a plus or hash 20,2,20,2,20,4,18,2 2 into 57, 3 into 61, and so on. sign, and for flats a minus sign, after the note. 10014 DATA 17,6,15,6,13,12 If you prefer, you need not perform this Then, in Line 40, the computer plays the calculation on the DATA—by using the con- note, using the PLAY command. The T, in The first line lets you INPUT the tempo; a version table of pitch values earlier in this quotes, indicates that the next string in the value 20 to 30 is about right for the tune given. article, you could use the correct DATA to start line is a number between 1 and 255 (not 155 as Line 90 sets up ENVELOPE number 1, with, instead. indicated in the Dragon manual), which sets which the program uses for playing notes; it the actual tempo of the tune. The value you has an attack segment (attack rate is 30), a MIA INPUT at the beginning of the program is decay segment (decay rate is — 2), a sustain 10 INPUT"TEMPO (1 — 50)1=1";TP inverted, and then converted into a string by segment (where the volume remains steady) 20 IF TP<1 THEN 10 the function STR$. and a final release segment (release rate is 30 READ A$,D The "03" after this in the same line sets the —5). The peak volume is 100 units, and the 40 PLAY"T" + STR$(1NT(1 + 255/ computer to octave 3, while the last part of the sustain volume is 50 units. These values are (TM))) + "03" +A$ line ( + A$) sets the pitch of the note. provided by the last 6 parameters of the 50 GOTO 30 Once the computer has PLAYed the note, it ENVELOPE command. Don't worry if you 1000 DATA E,6,D,6,C,12 goes back to Line 30, to READ the next note's don't understand how the ENVELOPE com- 1010 DATA E,6,D,6,C,12 DATA—unless, of course, the tune is over, in mand works, there'll be more on this in a later 1020 DATA G,6,F,4,F,2,E,12 which case there will be no more DATA, and so article. 1030 DATA G,6, F,4, F,2, E,12 the computer stops with an error message, as Line 95 sets up ENVELOPE 2, which is used 1040 DATA 04C,4,04C,2,B,2,A,2,B,2, explained earlier in the article. when the program is instructed to 'play' a 04C,4,G,2,G,4,G,2 rest. It's a kind of 'null' envelope, which 1050 DATA 04C,2,04C,2,04C,2,B,2,A, doesn't actually let any sound through. The 2,B,2,04C,2,G,2,G,2,G,2,G,2 reason the program uses an ENVELOPE and a 1060 DATA 04C,4,04C,2,B,2,A,2,B,2 SOUND command to play a rest is so that the 04C,2,G,2,G,2,G,4,F,2 timing, whether of notes or of rests, is all 1070 DATA E,12,D,12,C,12 taken care of by the system of queuing notes. The rate at which notes are played is not governed by the execution time of the programs, but by the computer's internal queuing system. If you tried to produce rests with delay loops, the queuing system would handle all the notes and feed them out to the sound chip, but the delay loops would occur out of sequence and would effectively be lost. So the program uses a SOUND command both for notes which sound and for rests and the two kinds of event are then handled similarly Here we look at how you can make small improvements to standard sort routines to increase their speed. And give details of one routine that beats the lot hands down!
All techniques used for sorting data into a the second row, value 72 is identified as the 130 NEXT Z predefined order come to grief because of highest incorrectly placed value, exchanging 140 GOTO 10 time or memory restrictions. Some sort of with 11 by the third row. 3999 REM DELAYED REPLACEMENT SORT compromise has to be established so that the 4000 FOR 1=1 TO AA-1 most efficient sorting method can be used for top bottom 4010 LET K=1 a particular application. No single sorting 67 35 72 19 47 38 11 [86] 4020 FOR J=1+1 TO AA method is ideal for every application. 67 35 [72] 19 47 38 [11] 86 4030 IF A(J)