;sound player

nolist

;live debug ?
debug_sound	EQU 0

IF debug_sound
	write direct -1,-1,#C7
ELSE
	write "bin/sound.bin"
ENDIF

org #4000
limit #7000
snd_buffer	EQU #7000

read "globals.asm"

;header
db #C7
ds 15,0

;interface

jp player_run
jp player_start
jp player_stop
jp player_skip
jp player_set_volume
jp player_SFX_Start
jp player_SFX_Stop
jp player_SFX_StopAll
jp player_SFX_Play
jp player_SFX_SetChanForce
jp Player_SFX_SetForcedChannel
jp player_pause
jp player_unpause


.player_set_volume

	ld E,A
	jp PLY_SetFadeValue
	;ret


;start music but skip patterns
;
;	A = music , BC = patterns to skip

.player_skip

	push BC
	push AF


	call player_stop

	ld HL,PLY_SendRegisters	;modification to out no sound !
	ld A,(HL)
	ld (PLY_SendRegistersRestore),A
	ld (HL),#C9


	;start music
	pop AF	;get track
	call player_uncompress
	
	ld DE,snd_buffer
	call PLY_Init


	;do the skip
	pop BC	;get count to skip

.player_skip_loop
	push BC
	call PLY_Play
	pop BC
	dec BC
	ld A,C
	or B
	jr nz,player_skip_loop

	PLY_SendRegistersRestore	EQU $+1
	ld A,0
	ld (PLY_SendRegisters),A


	ld A,1		;player is now running
	ld (player_state),A
	ret
	

;stop music
;
.player_stop
	xor A
	ld (player_state),A
	jp PLY_Stop
	;ret

;pause music
;
.player_pause
;save volume
	ld A,(PLY_Channel1_FadeValue + 1)
	ld (player_volume),A
;stop interrupt player
	xor A
	ld (player_state),A
;set no volume
	ld E,16
	call PLY_SetFadeValue
	call PLY_Play	;play one time to be sure to stop sound
	ret

.player_unpause
	player_volume equ $+1
	ld E,0
	call PLY_SetFadeValue
	ld A,1
	ld (player_state),A
	ret

;start player
; music = A
.player_start
	push AF
	xor A
	ld (player_state),A
	;call PLY_Stop
	
	pop AF
	call player_uncompress
	
	;ld DE,snd_buffer
	call PLY_Init

	ld A,1
	ld (player_state),A
	ret	

;play music
.player_run
	ld A,(player_state)
	or A
	jr z,player_run_end

	push DE
	push IX
	push IY
	ex AF,AF'
	push AF
	exx
	push BC
	push HL
	push DE


	call PLY_Play

	pop DE
	pop HL
	pop BC
	exx
	pop AF
	ex AF,AF'
	pop IY
	pop IX
	pop DE

.player_run_end
	
	ret

player_state	db 0

;uncompress music
; A = music , #FF = sfx music (no music but sfx)
;#FE = buffer decompression address in already in HL
.player_uncompress
	cp #FF
	jr z,player_uncompress_nomusic

	cp #FE	
	jr z,player_uncompress_notintable

	ld L,A
	ld H,0
	add HL,HL
	ld DE,music_table
	add HL,DE
	ld E,(HL)
	inc HL
	ld D,(HL)
	ex DE,HL
.player_uncompress_notintable
	ld DE,snd_buffer
	call deexo
	ld DE,snd_buffer
	ret
.player_uncompress_nomusic
	ld DE,(current_sfx_bank)
	ret

read "sfx_code.asm"
	

read "deexo.asm"
read "arkostrackerplayer.asm"

.current_sfx_bank
	dw sfx_bank
.music_table
	dw level1	;0
	dw level2	;1
	dw level3	;2
	dw level4	;3
	dw level5	;4
	dw level6	;5
	dw level7	;6
	dw level8	;7
	dw boss		;8
	dw continue	;9
	dw gameover	;10
	dw win		;11
	dw high		;12
	dw die		;13
	dw title ;it is not really but boss 0 music
	;dw start

;musics
.level1	;0
	incbin "snd/rtype-1.raw"
.level2	;1
	incbin "snd/rtype-2.raw"
.level3 ;2
	incbin "snd/rtype-3.raw"
.level4	;3
	incbin "snd/rtype-4.raw"
.level5	;4
	incbin "snd/rtype-5.raw"
.level6	;5
	incbin "snd/rtype-6.raw"
.level7	;6
	incbin "snd/rtype-7.raw"
.level8	;7
	incbin "snd/rtype-8.raw"
.boss	;8
	incbin "snd/rtypebos.raw"
.continue	;9
	incbin "snd/rtypecon.raw"
.gameover	;10
	incbin "snd/rtypegov.raw"
.title		;11
	;incbin "snd/rtypetit.raw"
	incbin "snd/rtype2wi.raw"
.win		;12
	incbin "snd/rtypewin.raw"
.high		;13
	incbin "snd/rtypehig.raw"
.die		;14
	incbin "snd/rtypepaf.raw"
;.start		;15
;	incbin "snd/rtype-go.raw"

codesize	EQU $

PRINT  "main size     : 0x$codesize bytes (max 0x67CB)"

	ds #67CB-$
.sfx_bank
	
	incbin "snd/sfx.bin"

list
;/EOF
