	include	gemmacro.i
start:
	move.l	4(a7),a3		base page
	move.l	#mystack,a7
	move.l	$c(a3),d0		text len
	add.l	$14(a3),d0		data len
	add.l	$1c(a3),d0		BSS len
	add.l	#$100,d0		basepage
	move.l	d0,-(sp)
	move.l	a3,-(sp)
	clr.w	-(sp)
	move.w	#$4a,-(sp)
	trap	#1			shrink memory
	lea	12(sp),sp

	bsr	setsound

mainloop:
	form_alert	#2,#mainalert
	move.w	int_out,d0		;get the button
	cmpi.w	#2,d0
	beq	exit
loadsound:
	pea	fseltitle
	pea	fselsp			;get the PNT file
	jsr	_fileselector
	add.l	#8,sp
	cmpi.l	#1,d0
	bne	mainloop	;if problem go to start

	pea	soundin
	move.l	#-1,-(sp)
	jsr	_loadfile		;read sound in
	add.l	#8,sp
	jsr	_closefile

	move.l	_loadlength,soundlength
	bsr	convertsound

convertedloop:
	form_alert	#3,#aftercon
	move.w	int_out,d0		;get the button
	cmpi.w	#3,d0
	beq	mainloop
	cmpi.w	#2,d0
	beq	savesound
playsound:
	bsr	startsound
	bra	convertedloop
savesound:
	lea	_myfp,a0
.loop:
	tst.b	(a0)+
	bne	.loop
	subq.l	#4,a0
	move.b	#'8',(a0)+		;change the name
	move.b	#'B',(a0)+
	move.b	#'T',(a0)+
	moveq	#0,d0
	jsr	_openfile

	pea	avrheader
	move.l	#128,-(sp)
	jsr	_writetofile		;write avr header
	add.l	#8,sp


	pea	soundout
	move.l	newsoundlength,-(sp)
	jsr	_writetofile		;write data
	add.l	#8,sp
	jsr	_closefile
	bra	mainloop





fseltitle:	dc.b	'Select 16bit sample',0
fselsp:		dc.b	'16B',0
	even

exit:
	bsr	stopsound
	move.w	#$81,-(sp)		;unlock the sound system
	trap	#14
	addq.l	#2,sp

	clr.w	-(sp)
	trap	#1



mainalert:
	dc.b	'[2][Sound Converter|What would you like to do?]'
	dc.b	'[LOAD|QUIT]',0
	even
aftercon:
	dc.b	'[2][Sound has been converted|What now?]'
	dc.b	'[PLAY IT|SAVE IT|TRASH IT]',0
	even




setsound:
	move.w	#$80,-(sp)		;lock the sound system
	trap	#14
	addq.l	#2,sp

	move.w	#2,-(sp)		;setmode 8bit mono
	move.w	#$84,-(sp)
	trap	#14
	addq.l	#4,sp

	move.w	#0,-(sp)	;rectracks
	move.w	#0,-(sp)	;playtracks
	move.w	#$85,-(sp)	;set tracks
	trap	#14
	addq.l	#6,sp

	move.w	#1,-(sp)	;disable handshaking
	move.w	#0,-(sp)	;prescale
	move.w	#0,-(sp)	;srclk internal 25.175Mhz
	move.w	#8,-(sp)	;dst dac
	move.w	#0,-(sp)	;src dma playback
	move.w	#$8b,-(sp)	;devconnect
	trap	#14
	add.l	#12,sp

	move.w	#1,-(sp)
	move.w	#6,-(sp)	;compatible prescale
	move.w	#$82,-(sp)
	trap	#14
	addq.l	#6,sp

*	move.w	#$0010,mastervol
*	jsr	setvol

nosound:
	rts

setvol:
	move.w	mastervol,-(sp)
	move.w	#0,-(sp)	;ltatten
	move.w	#$82,-(sp)
	trap	#14
	addq.l	#6,sp

	move.w	mastervol,-(sp)
	move.w	#1,-(sp)	;rtatten
	move.w	#$82,-(sp)
	trap	#14
	addq.l	#6,sp
	rts


startsound:
	move.w	#%0000000000000000,-(sp)	;stop the sound
	move.w	#$88,-(sp)
	trap	#14
	addq.l	#4,sp

	move.l	#soundout,d0
	add.l	newsoundlength,d0
	move.l	d0,-(sp)	;endaddress
	pea	soundout	;start address
	move.w	#0,-(sp)	;playback
	move.w	#$83,-(sp)	;setbuffer
	trap	#14
	add.l	#12,sp

	move.w	#%0000000000000001,-(sp)	;play once
	move.w	#$88,-(sp)
	trap	#14
	addq.l	#4,sp
	rts

stopsound:
	move.w	#%0000000000000000,-(sp)	;stop the sound
	move.w	#$88,-(sp)
	trap	#14
	addq.l	#4,sp
	rts
*****************************************************************
*	Convert 16 bit stereo sample to 8 bit mono		*
*	at half the replay rate.				*
*****************************************************************
convertsound:
	move.l	soundlength,d0
	asr.l	#2,d0			;div by 2 gives number of samples
	move.l	d0,newsoundlength

	lea	soundin,a0		;now mono
	move.l	a0,a1
	lea	soundout,a2
	subq.l	#1,d0
*	now halve the sample rate by averaging each two words
csloop2:
	moveq	#0,d1
	moveq	#0,d2
	move.w	(a0),d1		;get the sample
	move.w	4(a0),d2
	ext.l	d1
	ext.l	d2
	add.l	d2,d1			;add right sample word
	divs.l	#2,d1			;average them
	move.w	d1,(a1)+		;write result into same place
	addq.l	#8,a0
	subq.l	#1,d0
	bpl	csloop2

	lea	soundin,a0		;now mono
	lea	soundout,a2
	move.l	newsoundlength,d0
	asr.l	#1,d0
	move.l	d0,newsoundlength
	subq.l	#1,d0
*	now convert to 8 bit
csloop3:
	moveq	#0,d1
	moveq	#0,d2
	move.w	(a0)+,d1		;get the sample
	divs.l	#256,d1			;average them
	move.b	d1,(a2)+		;write result into soundout
	subq.l	#1,d0
	bpl	csloop3
	move.l	newsoundlength,loopend
	rts



	include	'file.s'

	IFEQ	__LK
	include	aeslib.s
	include	vdilib.s
	ENDC

	section data
avrheader:
	dc.b	'2BIT'
	dc.b	'MVPXSMPL'
	dc.w	0		;mono
	dc.w	8		;8bit
	dc.w	-1		;signed
	dc.w	0		;non looping
	dc.w	-1		;no midi note
	dc.b	0		;replay speed byte
	dc.b	00,$30,$04		;12292
newsoundlength:
	dc.l	0
	dc.l	0
loopend:
	dc.l	0
	ds.b	26
	ds.b	64


	section BSS
		ds.l	100
mystack:	ds.l	1
soundlength:	ds.l	1
mastervol:	ds.w	1
soundin:
	ds.b	4000000
soundout:
	ds.b	2000000
