	SECTION ExampleCode,CODE

_LVOOldOpenLibrary equ -408
_LVOCloseLibrary   equ -414
_LVODelay	   equ -198

;these are the functions in Audio.asm
	XREF	PlayNote,TransposePeriod,RateToPeriod,PeriodToRate,StopChan
	XREF	SetupAudio,FreeAudio

	XDEF	_main
_main
;---Get Exec
M1	movea.l	$00000004,a6
	move.l	a6,_SysBase
;---Open DOS library
M2	lea	DOSName,a1
	jsr	_LVOOldOpenLibrary(a6)
	move.l	d0,_DOSBase
;---Setup the audio hardware
M3	jsr	SetupAudio
;---Let's turn our sample rate into a period in nanoseconds so that
;   we can use TransposePeriod to play this wave at various musical
;   pitches.
	;NOTE: Our sampling rate for this wave is 18Khz. The sampling rate is
	;found in the VHDR chunk of any loaded 8SVX sample.
M4	move.l	#18000,d1
	jsr	RateToPeriod
	move.l	d0,d7		;save the period
;---Play the 8SVX sample with the oneShot and repeat portions at orig pitch
M5	move.l	d7,d1
	moveq	#0,d0		;steps = 0 (ie original pitch)
	jsr	TransposePeriod
	move.w	d0,d4		;amiga period
	beq.s	M6		;oops! out of range for DMA
	moveq	#0,d0		;play it back on channel #0
	moveq	#0,d1
	move.w	#512,d1		;size of oneShot (ie 512 bytes)
	lea	Wave1shot,a0	;address of oneShot
	moveq	#0,d2
	move.w	#256,d2		;size of repeat
	lea	Wave1loop,a1	;address of repeat
	moveq	#64,d3		;volume = MAX
	jsr	PlayNote
	;---Delay for a few secs, then turn the note off by stopping that chan.
	;   Because this sample has a looping portion, we need to eventually
	;   stop this note's playback, either by playing another note on this
	;   channel, or calling StopChan
M6	movea.l	_DOSBase,a6
	moveq	#127,d1
	jsr	_LVODelay(a6)
M7	moveq	#0,d0
	jsr	StopChan
	movea.l	_DOSBase,a6
	moveq	#75,d1
	jsr	_LVODelay(a6)
;---Play the 8SVX sample with only the oneShot. Note that because
;   there is no looping portion, our "block done" interrupt automatically
;   stops the channel at the end of the oneShot. Therefore, we don't
;   have to stop the channel ourselves like we did when we played that
;   note with the looping portion.
;   Note that this oneShot portion is very small (only 512 bytes) so we're
;   not going to hear much; just a quick beep and it's done.
M8	moveq	#1,d0		;play it back on channel #1
	moveq	#0,d1
	move.w	#512,d1		;size of oneShot (ie 256 bytes)
	lea	Wave2shot,a0	;address of oneShot
	moveq	#0,d2		;size of repeat
	move.l	d2,a1		;address of repeat (NONE)
	moveq	#64,d3		;volume = MAX
	;note: use the same amiga period
	jsr	PlayNote
;---Delay to give it a chance to finish. There's no repeat portion so it stops itself
M9	movea.l	_DOSBase,a6
	moveq	#50,d1
	jsr	_LVODelay(a6)
;---Now, let's play a major triad. This consists of the original pitch, plus
;   a pitch that is 4 half steps above orig pitch, plus a pitch that is 3
;   half steps above that. We'll call PlayNote 3 times, starting a note on
;   each of 3 amiga chans
M10	moveq	#0,d0		;play it back on channel #0
	moveq	#0,d1
	move.w	#512,d1		;size of oneShot (ie 512 bytes)
	lea	Wave1shot,a0	;address of oneShot
	moveq	#0,d2
	move.w	#256,d2		;size of repeat
	lea	Wave1loop,a1	;address of repeat
	moveq	#64,d3		;volume = MAX
	;note: use orig amiga period
	jsr	PlayNote	
M11	move.l	d7,d1
	moveq	#4,d0		;steps = +4
	jsr	TransposePeriod
	move.w	d0,d4		;amiga period
	beq.s	M12		;oops! out of range for DMA
	moveq	#1,d0		;play it back on channel #1
	moveq	#0,d1
	move.w	#512,d1		;size of oneShot (ie 512 bytes)
	lea	Wave1shot,a0	;address of oneShot
	moveq	#0,d2
	move.w	#256,d2		;size of repeat
	lea	Wave1loop,a1	;address of repeat
	moveq	#64,d3		;volume = MAX
	jsr	PlayNote
M12	move.l	d7,d1
	moveq	#4+3,d0		;steps = +4+3
	jsr	TransposePeriod
	move.w	d0,d4		;amiga period
	beq.s	M13		;oops! out of range for DMA
	moveq	#2,d0		;play it back on channel #2
	moveq	#0,d1
	move.w	#512,d1		;size of oneShot (ie 512 bytes)
	lea	Wave1shot,a0	;address of oneShot
	moveq	#0,d2
	move.w	#256,d2		;size of repeat
	lea	Wave1loop,a1	;address of repeat
	moveq	#64,d3		;volume = MAX
	jsr	PlayNote
M13	movea.l	_DOSBase,a6
	moveq	#127,d1
	jsr	_LVODelay(a6)
;---Stop those 3 chans
M14	moveq	#0,d0
	jsr	StopChan
	moveq	#1,d0
	jsr	StopChan
	moveq	#2,d0
	jsr	StopChan
;---Free the hardware
M15	jsr	FreeAudio
;---Close DOS and exit
M16	movea.l	_DOSBase,a1
	movea.l	_SysBase,a6
	jsr	_LVOCloseLibrary(a6)
	moveq	#0,d0
	rts

	SECTION ExampleData,DATA

_SysBase	dc.l	0
_DOSBase	dc.l	0
DOSName		dc.b	'dos.library',0

	SECTION Audio,DATA,CHIP	;put this data in chip mem

;----Here's a simulation of an 8SVX sample that has a oneShot portion and
;    a repeat (looping portion). The label Wave1shot is the audio data for the
;    oneShot portion. The label Wave1loop is the data for the repeated portion.
;    In this example, the data sets are the same, but for most 8SVX, they'll
;    be different (with different lengths).
Wave2shot:	;same as Wave1shot
Wave1shot:	;512 sample points (bytes)
 dc.b -128,-124,-120,-116,-112,-108,-104,-100,-96,-92,-88,-84,-80,-76,-72
 dc.b -68,-64,-60,-56,-52,-48,-44,-40,-36,-32,-28,-24,-20,-16,-12
 dc.b -8,-4,0,4,8,12,16,20,24,28,32,36,40,44,48
 dc.b  52,56,60,64,68,72,76,80,84,88,92,96,100,104,108
 dc.b  112,116,120,124
 dc.b -128,-124,-120,-116,-112,-108,-104,-100,-96,-92,-88,-84,-80,-76,-72
 dc.b -68,-64,-60,-56,-52,-48,-44,-40,-36,-32,-28,-24,-20,-16,-12
 dc.b -8,-4,0,4,8,12,16,20,24,28,32,36,40,44,48
 dc.b  52,56,60,64,68,72,76,80,84,88,92,96,100,104,108
 dc.b  112,116,120,124
 dc.b -128,-124,-120,-116,-112,-108,-104,-100,-96,-92,-88,-84,-80,-76,-72
 dc.b -68,-64,-60,-56,-52,-48,-44,-40,-36,-32,-28,-24,-20,-16,-12
 dc.b -8,-4,0,4,8,12,16,20,24,28,32,36,40,44,48
 dc.b  52,56,60,64,68,72,76,80,84,88,92,96,100,104,108
 dc.b  112,116,120,124
 dc.b -128,-124,-120,-116,-112,-108,-104,-100,-96,-92,-88,-84,-80,-76,-72
 dc.b -68,-64,-60,-56,-52,-48,-44,-40,-36,-32,-28,-24,-20,-16,-12
 dc.b -8,-4,0,4,8,12,16,20,24,28,32,36,40,44,48
 dc.b  52,56,60,64,68,72,76,80,84,88,92,96,100,104,108
 dc.b  112,116,120,124
 dc.b -128,-124,-120,-116,-112,-108,-104,-100,-96,-92,-88,-84,-80,-76,-72
 dc.b -68,-64,-60,-56,-52,-48,-44,-40,-36,-32,-28,-24,-20,-16,-12
 dc.b -8,-4,0,4,8,12,16,20,24,28,32,36,40,44,48
 dc.b  52,56,60,64,68,72,76,80,84,88,92,96,100,104,108
 dc.b  112,116,120,124
 dc.b -128,-124,-120,-116,-112,-108,-104,-100,-96,-92,-88,-84,-80,-76,-72
 dc.b -68,-64,-60,-56,-52,-48,-44,-40,-36,-32,-28,-24,-20,-16,-12
 dc.b -8,-4,0,4,8,12,16,20,24,28,32,36,40,44,48
 dc.b  52,56,60,64,68,72,76,80,84,88,92,96,100,104,108
 dc.b  112,116,120,124
 dc.b -128,-124,-120,-116,-112,-108,-104,-100,-96,-92,-88,-84,-80,-76,-72
 dc.b -68,-64,-60,-56,-52,-48,-44,-40,-36,-32,-28,-24,-20,-16,-12
 dc.b -8,-4,0,4,8,12,16,20,24,28,32,36,40,44,48
 dc.b  52,56,60,64,68,72,76,80,84,88,92,96,100,104,108
 dc.b  112,116,120,124
 dc.b -128,-124,-120,-116,-112,-108,-104,-100,-96,-92,-88,-84,-80,-76,-72
 dc.b -68,-64,-60,-56,-52,-48,-44,-40,-36,-32,-28,-24,-20,-16,-12
 dc.b -8,-4,0,4,8,12,16,20,24,28,32,36,40,44,48
 dc.b  52,56,60,64,68,72,76,80,84,88,92,96,100,104,108
 dc.b  112,116,120,124
Wave1loop	;256 data points
 dc.b -128,-124,-120,-116,-112,-108,-104,-100,-96,-92,-88,-84,-80,-76,-72
 dc.b -68,-64,-60,-56,-52,-48,-44,-40,-36,-32,-28,-24,-20,-16,-12
 dc.b -8,-4,0,4,8,12,16,20,24,28,32,36,40,44,48
 dc.b  52,56,60,64,68,72,76,80,84,88,92,96,100,104,108
 dc.b  112,116,120,124
 dc.b -128,-124,-120,-116,-112,-108,-104,-100,-96,-92,-88,-84,-80,-76,-72
 dc.b -68,-64,-60,-56,-52,-48,-44,-40,-36,-32,-28,-24,-20,-16,-12
 dc.b -8,-4,0,4,8,12,16,20,24,28,32,36,40,44,48
 dc.b  52,56,60,64,68,72,76,80,84,88,92,96,100,104,108
 dc.b  112,116,120,124
 dc.b -128,-124,-120,-116,-112,-108,-104,-100,-96,-92,-88,-84,-80,-76,-72
 dc.b -68,-64,-60,-56,-52,-48,-44,-40,-36,-32,-28,-24,-20,-16,-12
 dc.b -8,-4,0,4,8,12,16,20,24,28,32,36,40,44,48
 dc.b  52,56,60,64,68,72,76,80,84,88,92,96,100,104,108
 dc.b  112,116,120,124
 dc.b -128,-124,-120,-116,-112,-108,-104,-100,-96,-92,-88,-84,-80,-76,-72
 dc.b -68,-64,-60,-56,-52,-48,-44,-40,-36,-32,-28,-24,-20,-16,-12
 dc.b -8,-4,0,4,8,12,16,20,24,28,32,36,40,44,48
 dc.b  52,56,60,64,68,72,76,80,84,88,92,96,100,104,108
 dc.b  112,116,120,124
