
	incdir	include:
	include	exec/memory.i
	include	lvo/exec_lib.i
	include	devices/ahi.i
	include	libraries/ahi_sub.i
	include	libraries/toccata.i
	include	libraries/toccata_lib.i
	include	utility/hooks.i
	include	toccata.i
	include	macros.i

	XDEF	_intAHIsub_Disable
	XDEF	_intAHIsub_Enable
	XDEF	_intAHIsub_Update
	XDEF	_intAHIsub_SetVol
	XDEF	_intAHIsub_SetFreq
	XDEF	_intAHIsub_SetSound
	XDEF	_intAHIsub_SetEffect
	XDEF	_intAHIsub_LoadSound
	XDEF	_intAHIsub_UnloadSound

	XDEF	_RecordFunc

	XDEF	_PlayFuncMono
	XDEF	_PlayFuncStereo

	XREF	_ToccataBase

*******************************************************************************

;in:
* a2	struct AHI_AudioCtrl
_intAHIsub_Disable:
	push	a6
	move.l	4.w,a6
	call	Disable
	pop	a6
	rts

;in:
* a2	struct AHI_AudioCtrl
_intAHIsub_Enable:
	push	a6
	move.l	4.w,a6
	call	Enable
	pop	a6
	rts


* Unused functions

_intAHIsub_SetVol:
_intAHIsub_SetFreq:
_intAHIsub_SetSound:
_intAHIsub_SetEffect:
_intAHIsub_LoadSound:
_intAHIsub_UnloadSound:
	moveq	#AHIS_UNKNOWN,d0
_intAHIsub_Update:
	rts


*******************************************************************************

;in:
* d0	size
* a0	buffer
* a1	AudioCtrl
_RecordFunc:
	pushm	a2/a3
	move.l	a1,a2
	move.l	ahiac_DriverData(a2),a1

* Update dd->t_RecMessage->ahirm_Length:
	move.l	t_RecMessage(a1),a3
	lsr.l	#2,d0				;bytes => samples
	move.l	d0,ahirm_Length(a3)

* Copy sample buffer
	move.l	t_RecBuffer(a1),a3
	lsr.l	#1,d0
	bcs	.1
	subq.l	#1,d0
	bmi	.exit
.loop
	move.l	(a0)+,(a3)+
.1
	move.l	(a0)+,(a3)+
	dbf	d0,.loop

* Call the SamplerFunc Hook
	move.l	t_RecMessage(a1),a1
	move.l	ahiac_SamplerFunc(a2),a0
	move.l	h_Entry(a0),a3
	jsr	(a3)
.exit
	moveq	#TRUE,d0
	popm	a2/a3
	rts

*******************************************************************************

TocSamplesCnt	EQUR	d7
TocBuffer	EQUR	a6

*
* These functions fills the Toccata buffer with the mixed output. Since
* the Toccata buffer is not the same size as the mixing buffer, the mixing
* routine may have to be called several times each interrupt. Or if the mixing
* buffer is larger than the Toccata buffer, it may not be called at all.
*
* The routines are not very well commented, but they should be pretty stright-
* forward once you know what they actually do.
*

;in:
* d0    scratch
* d1    scratch
* a0    scratch
* a1    ato_SoftIntData
* a5    scratch
_PlayFuncMono:
	pushm	d7/a2-a3/a6
	move.l	a1,a2				;a2 is later used as Hook object
	move.l	ahiac_DriverData(a2),a5

* Swap Toccata buffer buffers and get pointer to one of them.
	move.l	t_SampBuffer1(a5),TocBuffer
	move.l	t_SampBuffer2(a5),t_SampBuffer1(a5)
	move.l	TocBuffer,t_SampBuffer2(a5)

* TocSamplesCnt is number of samples left in Toccata buffer to fill.
	move.l	t_TocSamples(a5),TocSamplesCnt

* t_MixSamplesCnt is uncopied samples left in mixing buffer
.loop
	tst.l	t_MixSamplesCnt(a5)
	bne	.fillTocBuffer

* There are no unplayed samples in the mixing buffer. Fill it again!
	move.l	ahiac_PlayerFunc(a2),a0
	suba.l	a1,a1
	move.l	h_Entry(a0),a3
	jsr	(a3)				;Call Player Hook
	move.l	ahiac_MixerFunc(a2),a0
	move.l	t_MixBuffer(a5),a1
	move.l	h_Entry(a0),a3
	jsr	(a3)				;Call Mixer Hook

	move.l	ahiac_BuffSamples(a2),t_MixSamplesCnt(a5)
	move.l	t_MixBuffer(a5),t_MixBufferPtr(a5)

.fillTocBuffer
	move.l	t_MixSamplesCnt(a5),d0
	sub.l	d0,TocSamplesCnt
	bpl	.1
	add.l	TocSamplesCnt,d0
	moveq	#0,TocSamplesCnt
.1
* d0 is now how many samples should be copied before either the Toccata buffer
* is full or the mixing is empty.

	sub.l	d0,t_MixSamplesCnt(a5)

* t_MixBufferPtr is the address (in the mixing buffer) where the next untouched
* sample is located.
	move.l	t_MixBufferPtr(a5),a0
	lsr.l	#1,d0				;Just unrolling...
	bcs	.2
	subq.l	#1,d0
.transferLoop
	move.w	(a0)+,(TocBuffer)+
.2
	move.w	(a0)+,(TocBuffer)+
	dbf	d0,.transferLoop
	move.l	a0,t_MixBufferPtr(a5)		;Update pointer till next time
	tst.l	TocSamplesCnt			;Is Toccata buffer full now?
	bne	.loop				;Nope, loop.

	popm	d7/a2-a3/a6
	rts


_PlayFuncStereo:
	pushm	d7/a2-a3/a6
	move.l	a1,a2
	move.l	ahiac_DriverData(a2),a5

	move.l	t_SampBuffer1(a5),TocBuffer
	move.l	t_SampBuffer2(a5),t_SampBuffer1(a5)
	move.l	TocBuffer,t_SampBuffer2(a5)

	move.l	t_TocSamples(a5),TocSamplesCnt

.loop
	tst.l	t_MixSamplesCnt(a5)
	bne	.fillTocBuffer

	move.l	ahiac_PlayerFunc(a2),a0
	suba.l	a1,a1
	move.l	h_Entry(a0),a3
	jsr	(a3)
	move.l	ahiac_MixerFunc(a2),a0
	move.l	t_MixBuffer(a5),a1
	move.l	h_Entry(a0),a3
	jsr	(a3)

	move.l	ahiac_BuffSamples(a2),t_MixSamplesCnt(a5)
	move.l	t_MixBuffer(a5),t_MixBufferPtr(a5)

.fillTocBuffer
	move.l	t_MixSamplesCnt(a5),d0
	sub.l	d0,TocSamplesCnt
	bpl	.1
	add.l	TocSamplesCnt,d0
	moveq	#0,TocSamplesCnt
.1
	sub.l	d0,t_MixSamplesCnt(a5)
	move.l	t_MixBufferPtr(a5),a0
	lsr.l	#1,d0
	bcs	.2
	subq.l	#1,d0
.transferLoop
	move.l	(a0)+,(TocBuffer)+		; 2 WORDs == left and right data
.2
	move.l	(a0)+,(TocBuffer)+
	dbf	d0,.transferLoop
	move.l	a0,t_MixBufferPtr(a5)
	tst.l	TocSamplesCnt
	bne	.loop

	popm	d7/a2-a3/a6
	rts
