;global func to save a bit memory
;important ! add to stack BC (previous state) and AF (current bank)
;F , AF' and BC corrupted
.SND_EnterSndBank
	ld (SND_EnterSndBank_saveHL),HL
	pop HL	;ret address
	push BC
	ex AF,AF'
	ld A,(#4000)
	push AF
	ex AF,AF'
	
	ld BC,#7FC7
	out (C),C
	
	push HL		;add ret address
SND_EnterSndBank_saveHL	EQU $+1
	ld HL,0
	ret
	
;exit sound bank , allways call it with a JP !
.SND_ExitSndBank
	pop AF
	ld B,#7F
	out (C),A
	pop BC
	ret
	

;set forced channel
.SND_SfxSetForcedChannel
	push BC
	ld (SND_SfxSetForcedChannel_param),A
	ld A,(#4000)
	push AF
	
SND_SfxSetForcedChannel_param	EQU $+1
	ld A,0
	
	ld BC,#7FC7
	out (C),C
	
	call SFX_SetForcedChannel
	
	pop AF
	ld B,#7F
	out (C),A
	pop BC
	ret

;set SFX chan mode
.SND_SfxChanMode
	call SND_EnterSndBank
	
	ld BC,#7FC7
	out (C),C
	
	call SFX_SetChanMode
	
	jp SND_ExitSndBank
	
;set Sound Mode
; A = - 0 = full - 1 = music only - 2 = SFX only
.SND_SetMode
	ld (SND_MusicMode),A
	or A
	jr z,SND_SetMode0
	dec A
	jr z,SND_SetMode1
;SFX only ,no music only SFX on 3 chans	
	xor A
	call SND_SfxChanMode
	xor A
	jr SND_SetSfxState
;music only	
.SND_SetMode1
	ld A,#C9
	jr SND_SetSfxState
;music + SFX on 1 channel
.SND_SetMode0
	ld A,1
	call SND_SfxChanMode
	xor A
.SND_SetSfxState
	ld (SND_SfxPlay),A
	ret
.SND_MusicMode
	db 0

;start SFX mode
.SND_SfxStart
	call SND_EnterSndBank
	
	ld BC,#7FC7
	out (C),C

	call SFX_Start
	
	jp SND_ExitSndBank
	
;stop all SFX channel
.SND_SfxStopAll
	call SND_EnterSndBank

	call SFX_StopAll
	
	jp SND_ExitSndBank
	
;play SFX in A
.SND_SfxPlay

	nop		;let it there , no SFX = #C9
	push HL
	push DE
	push BC
	push IX
	push IY
	
	ex AF,AF'
	push AF
	ex AF,AF'

	ld B,A
	ld A,(#4000)
	push AF
	ld A,B

	
	ld BC,#7FC7
	out (C),C

	call SFX_Play
	
	pop AF
	ld B,#7F
	out (C),A
	
	ex AF,AF'
	pop AF
	ex AF,AF'
	
	pop IY
	pop IX
	pop BC
	pop DE
	pop HL
	
	ret
	

.MUSIC_SetVolume
	call SND_EnterSndBank

	call SND_Volume
	
	jp SND_ExitSndBank
	
;sound interface
.MUSIC_Skip
	ld (MUSIC_Skip_Param),BC

	call SND_EnterSndBank
	
;check if we are in music mode	
	ld C,A
	ld A,(SND_MusicMode)
	cp 2
	jr c,MUSIC_SkipNotSFXmode
	ld C,#FF	;SFX only mode !
.MUSIC_SkipNotSFXmode
	ld A,C

MUSIC_Skip_Param	EQU $+1	
	ld BC,0

	call SND_Skip
	
	jp SND_ExitSndBank

.MUSIC_Play

	call SND_EnterSndBank

;check if we are in music mode	
	ld C,A
	ld A,(SND_MusicMode)
	cp 2
	jr c,MUSIC_PlayNotSFXmode
	ld C,#FF	;SFX only mode !
.MUSIC_PlayNotSFXmode
	ld A,C
	
	call SND_Player
	
	jp SND_ExitSndBank
	
.MUSIC_Stop
	call SND_EnterSndBank

	call SND_Stop
	
	jp SND_ExitSndBank
	
.MUSIC_PlayerEntry
	call SND_EnterSndBank
	
	call SND_Run
	
	jp SND_ExitSndBank
	
.MUSIC_Pause
	call SND_EnterSndBank
	
	call SND_Pause
	
	jp SND_ExitSndBank
	
.MUSIC_UnPause
	call SND_EnterSndBank
	
	call SND_UnPause
	
	jp SND_ExitSndBank