ZX Forum #04
19 ноября 1997 |
|
world of sound Spectrum - Chapter 4.4: Programming sound effects - music programming.
4.4. Programming Music Programming music codes - a thankless task. It is better to do with any musical editor. But the editor can not satisfy all Your needs, so that the master programming code is still desirable. The principle is the same - consistent read data and the procedure is called play. It is necessary to provide for termination of execution (often for these purposes is controlled by pressing any key). For example: 1415. 10 START LD HL, 60000; HL = address of data 20 NEXT LD E, (HL); DE = 30 INC HL; length 40 LD D, (HL); Tel 50 INC HL; sequence 60 LD A, D; DE 1970 CP 255 = 80 JR NZ, CONT; 65535 90 LD A, E;? 100 CP 255, if so, 110 JR Z, START; a melody first 120 CONT LD A, D; DE = 130 OR E; 0? 140 JR NZ, BEEP; if not, go to BEEP 150 LD E, (HL); DE = 160 INC HL; dlitel170 LD D, (HL); sequence 180 INC HL; pause 190 PAUSE HALT; interrupt latency 200 DEC DE; DE = DE-1 210 LD A, D; DE = 220 OR E; 0? 230 JR NZ, PAUSE; if not then loop 240 JR NEXT; the transition to the processing of the trace. Data 250 BEEP LD C, (HL); BC = 260 INC HL; chas270 LD B, (HL); to280 INC HL; that 290 PUSH HL; conservation HL 300 PUSH BC; put BC on the stack 310 POP HL; put the value from the stack in HL 320 CALL 949, a subroutine call play 330 CALL 654, control keyboard 340 POP HL; recovery HL 350 LD A, E; A = E 360 CP 255, if pressed, 370 RET NZ; then return to BASIC 380 JR NEXT; the transition to the processing of the trace. Data 2 When you call this subroutine from the machine code, note that interrupts must be enabled! Otherwise, if data from the first will be a pause, the computer hangs. Before using this rather ugly routines necessary to create for her an array of data (this is the most fun). In this case, it should located at address 60000 (# EA60), but You can easily change its location, changing the number in the first row of this subroutine. Data for each note should be as follows: first two bytes of length (First - the younger), then two bytes of the frequency. If, instead of the duration to put two zero, then the next pair of bytes will be regarded as the length of the pause. To mark the end of the melody, insert the data instead of the regular length of two bytes 255. Pause duration is measured in the fiftieth of a second, and what is measured duration of a note - do not know, probably, No one, though to calculate the required value is still possible (see Chapter 4). Now for the drawbacks, which, honestly speaking, more than enough. Two of them were already mentioned above: the difficulty of drawing melodies and strange enough measurement duration of notes. In addition, the duration frequency-dependent. For example, if you enter the following data: 0,1,100,0,0,1,0,5,255, 255, then you hear two different notes of the same length as expected. Another drawback - the opportunity to abort execution only in the interval between notes. However, it can easily be corrected: 1415. 10 START LD HL, 60000; HL = address of data 20 NEXT LD E, (HL); DE = 30 INC HL; length 40 LD D, (HL); Tel 50 INC HL; sequence 60 LD A, D; DE 1970 CP 255 = 80 JR NZ, CONT; 65535 90 LD A, E;? 100 CP 255, if so, 110 JR Z, START; a melody first 120 CONT LD A, D; DE = 130 OR E; 0? 140 JR NZ, BEEP; if not, go to BEEP 150 LD E, (HL); DE = 160 INC HL; dlitel170 LD D, (HL); sequence 180 INC HL; pause 190 PAUSE XOR A; A = 0 (control across the keyboard tours) 200 IN A, (254); poll Keys 210 CPL; inverting A 220 AND 31; dumping excess bit 230 RET NZ; if a key is pressed, the WHO Gates 240 HALT; interrupt latency 250 DEC DE; DE = DE-1 260 LD A, D; DE = 270 OR E; 0? 280 JR NZ, PAUSE; if not, then the cycle 290 JR NEXT; the transition to the processing of the trace. Data 300 BEEP LD C, (HL); BC = 310 INC HL; chas320 LD B, (HL); to330 INC HL; that 340 PUSH HL; conservation HL 350 PUSH BC; put BC on the stack 360 POP HL; put the value from the stack in HL 370 CALL PLAY; subroutine call play 380 POP HL; recovery HL 390 RET NZ; then return to BASIC 400 JR NEXT; the transition to the processing of the trace. Data 410 PLAY DI; ban interrupt 420 LD A, (23624); A = 430 SRL A; color 440 SRL A; bor450 SRL A; Duras 460 LOOP1 XOR 16; inverting bits D4 470 OUT (254), A; output to port A 254 480 LD C, A; preservation A 490 PUSH HL; conservation HL 500 LOOP2 XOR A; A = 0 (control across the keyboard tours) 510 IN A, (254); poll Keys 520 CPL; inverting A 530 AND 31; dumping excess bit 540 JR Z, CONT2; if no key is pressed, the continue 550 POP HL; off the value of the stack 560 EI; permission to interrupt 570 RET; Returns 580 CONT2 DEC HL; HL = HL-1 590 LD A, H; HL = 600 OR L; 0? 610 JR NZ, LOOP2; if not, then the cycle 620 POP HL; recovery HL 630 DEC DE; DE = DE-1 640 LD A, D; DE = 650 OR E; 0? 660 LD A, C; A recovery 670 JR NZ, LOOP1; if DE <> 0 then loop 680 EI; permission to interrupt 690 RET; Returns 2 Note that the same Data will be played in different ways this and the previous routines. And finally, the most obvious drawback - the music, which is obtained by These routines are too primitive. On assembler can do anything and more serious. I hope that convinced you not to try program music directly to the codes and instead buy a good music editor. All routines listed in this chapter can be used to create effects. It's enough to place notes encode the individual level effect. It is desirable that each step was as can be shorter, then the effect will be heard smoothly.
Other articles:
Similar articles:
В этот день... 21 November