sample.s (1711B)
1 .include "global.s" 2 3 ;; BANKED: checked 4 .title "Sound sample player" 5 .module Sample 6 7 .area _BASE 8 .AUD3WAVERAM = 0xff30 9 10_play_sample:: 11 push bc 12 lda hl,4(sp) 13 ld a,(hl+) 14 ld d,(hl) 15 ld e,a 16 17 lda hl,6(sp) 18 ld a,(hl+) 19 ld b,(hl) 20 ld c,a 21 22 ld h,d 23 ld l,e 24 25 call .play_sample 26 pop bc 27 ret 28 29; Playback raw sound sample with length BC from HL at 8192Hz rate. 30; BC defines the length of the sample in samples/32 or bytes/16. 31; The format of the data is unsigned 4-bit samples, 32; 2 samples per byte, upper 4-bits played before lower 4 bits. 33; 34; Adaption for GBDK by Lars Malmborg. 35; Original code by Jeff Frohwein. 36 37.play_sample:: 38 ld a,#0x84 39 ldh (.NR52),a ;enable sound 3 40 41 ld a,#0 42 ldh (.NR30),a 43 ldh (.NR51),a 44 45 ld a,#0x77 46 ldh (.NR50),a ;select speakers 47 ld a,#0xff 48 ldh (.NR51),a ;enable sound 3 49 50 ld a,#0x80 51 ldh (.NR31),a ;sound length 52 ld a,#0x20 53 ldh (.NR32),a ;sound level high 54 55 ld a,#0x00 56 ldh (.NR33),a ;sound freq low 57 58.samp2: 59 ld de,#.AUD3WAVERAM ;12 60 push bc ;16 61 ld b,#16 ;16 62 63 xor a 64 ldh (.NR30),a 65.samp3: 66 ld a,(hl+) ;8 67 ld (de),a ;8 68 inc de ;8 69 dec b ;4 70 jr nz,.samp3 ;12 71 72 ld a,#0x80 73 ldh (.NR30),a 74 75 ld a,#0x87 ; (256hz) 76 ldh (.NR34),a 77 78 ld bc,#558 ;delay routine 79.samp4: 80 dec bc ;8 81 ld a,b ;4 82 or c ;4 83 jr nz,.samp4 ;12 84 85 ld a,#0 ;more delay 86 ld a,#0 87 ld a,#0 88 89 pop bc ;12 90 dec bc ;8 91 ld a,b ;4 92 or c ;4 93 jr nz,.samp2 ;12 94 95 ld a,#0xbb 96 ldh (.NR51),a ;disable sound 3 97 ret