* TO_GSND:   GEM program to convert .SPL and .SAM samples to a format
* that GEM_SOUND thinks is a SoundWizard .SMP file.  NOTE:  The output
* file is NOT really a SoundWizard file but close enough to fool
* GEM_SOUND.
*
* The program creates a file with the proper header and then copies
* the sound sample to it.  If the input file is unsigned (.SPL) it
* will convert the samples to signed.  The output file will be named
* the same as the input file except it will have a .SMP extension.
*
* The program does NOT resample the input file.  GEM_SOUND expects to
* see a 12.5 KHz sample.  TO_GSND needs 12.5 KHz input samples to produce
* 12.5 KHz output samples.  SoundLab does an excellent job of resampling.
*
* This program is Public Domain.  Hack away to your heart's content!
* I only ask that the whole archive (TO_GSND.ZIP) be distributed.
*
*	Mike Allen, Las Cruces, NM  (MIKE-ALLEN on GEnie.)
*		11/20/92

*	version 1.01
*
* Well, found a problem already.  Seems like my Abacus book on ST
* internals was wrong about the Malloc call.  It returns a 0 if
* the memory is not allocated not a long negative.  My DevPackST
* book had it right.  Makes sense - this is consistant with the
* 'C' malloc call.  Anyway, the way the program was if I tried
* to convert a file bigger than I had room for, the program would
* lock up the ST seriously.  Fixed that and moved the Malloc call
* to before I opened the input file - why open it if I don't have
* enough memory to read it?
*
*	sma	11/21/92

*	version 1.02
*
* added loop to stay in the program for multiple file conversions.
* Terry hasn't asked for it yet, but I bet he will.
*
*	sma	11/22/92

* 	version 1.03
*
* changed so that path is not reinitialized each time.  Suggestion
* from Terry.
*
*	sma	11/22/92

*	Version 1.04
*
* Final (I HOPE!) changes - remembers input and output paths separately
* in case you wish to keep separate paths for input and output samples.
*
*	sma	11/23/92

*	Version 1.05
*
* One last change.  Since I can exit from the sound selection, why
* have the "More" alert?  Terry suggests I remove it - good idea.
* Also found a problem restoring the stack after the mfree call.
*
* Added support for aes91 (fsel_exinput) call.
*
* Added delete source file option.  (another Terry suggestion)
*
*	sma	12/10/92

	opt	a+,o+

	include	utils\initial.s
	

	graf_mouse	#0

	form_alert	#1,#title_text

	clr.w	-(sp)			find current path
	pea	in_path
	move.w	#$47,-(sp)
	trap	#1
	addq.l	#8,sp
	movea.l	#in_path,a0
	bsr	str_end
	move.b	#'\',(a0)+		add a \ to the string
	move.b	#0,(a0)+
	movea.l	#in_path,a0		move the default path to out path	
	movea.l	#out_path,a1
copy_out
	move.b	(a0)+,(a1)+
	bne	copy_out

	cmpi.w	#$140,global	see if aes version supports fsel_exi
	blt.s	again		nope, use fsel_input w/lgs option
	clr.b	aes91
	lea	lgs_in2,a0	concatenate the lgs strings
	move.b	#' ',-1(a0)
	lea	lgsout2,a0
	move.b	#' ',-1(a0)

again	form_alert	#3,#alert_text	Use an alert to chose between
*					input file formats or abort	

	cmpi	#3,d0			Abort selected? (Default)
	beq	done			Yep, clean up and quit

	st	sam			Assume a .SPL format
	move.l	#spl_pic,pick
	cmpi	#2,d0			.SAM format?
	bne	go			no, charge ahead
	clr.b	sam			yep, change assumptions
	move.l	#sam_pic,pick

go	movea.l	#in_path,a0		find the end of the in_path		
	bsr	str_end
	bsr	slash			back up to the slash
	movea.l	pick,a2			move the selected input file
add_sel	move.b	(a2)+,(a0)+		type to the end of the paths
	bne	add_sel

* NOTE:  I modified GEMMACRO.S and AESLIB.S to handle the two extra
* parameters that are passed in fsel_input for Charles. F. Johnson's
* excellent shareware product, Little Green Selector.  They pass two title
* lines to LGS.  If you have not modified the above two files, simply
* omit the last two parameters in the fsel_input call.
* 
* To be honest, I don't know what will happen if you pass the 2 extra
* parameters to fsel_input if you haven't modified the above two files,
* but I suspect nothing.  Note that passing the two extra parameters
* has no effect on the normal file selector if you aren't using LGS.

	tst.b	aes91
	beq	f_in1
	fsel_input	#in_path,#in_pick,#lgs_in1,#lgs_in2
	bra	f_in2
f_in1	fsel_exinput	#in_path,#in_pick,#lgs_in1
f_in2	tst.w	d0	error?
	beq	oops	yep
	tst.w	int_out+2	cancel?
	beq	done	yep, quit
	tst.b	in_pick	file picked?
	beq	oops	oh, no!

	movea.l	#in_path,a0	find the end of the new path
	bsr	str_end
	bsr	slash
	move.l	a0,a1		save in_path end
	movea.l	#out_path,a0	same for out_path
	bsr	str_end
	bsr	slash
	
	movea.l	#in_pick,a2	add on file name to paths
file	move.b	(a2),(a0)+
	move.b	(a2)+,(a1)+
	bne	file

	pea	dta	find the file length
	move.w	#$1a,-(sp)
	trap	#1
	addq.l	#6,sp
	clr.w	-(sp)
	pea	in_path
	move.w	#$4e,-(sp)
	trap	#1
	addq.l	#8,sp
	tst.l	d0
	bne	oops
	move.l	lange,length

* this section moved and corrected in version 1.01
	
	move.l	length,-(sp)	malloc memory for file read
	move.w	#$48,-(sp)
	trap	#1
	addq.l	#6,sp
	tst.l	d0
	beq	oops	malloc error I missed the first time.
	move.l	d0,m_ptr
	
	clr.w	-(sp)	open the file read-only
	pea	in_path
	move.w	#$3d,-(sp)
	trap	#1
	addq.l	#8,sp
	tst.l	d0
	bmi	oops	file didn't open ok.
	move.w	d0,handle

	move.l	m_ptr,-(sp)	read file
	move.l	length,-(sp)
	move.w	handle,-(sp)	
	move.w	#$3f,-(sp)
	trap	#1
	lea	12(sp),sp
	tst.l	d0
	bmi	oops
	
	move.w	handle,-(sp)	close the file
	move.w	#$3e,-(sp)
	trap	#1
	addq.l	#4,sp
	tst.l	d0
	bmi	oops

	tst.b	sam	if it already unsigned we don't mess with the data
	beq	do_it

	movea.l	m_ptr,a0	toggle the high bit of the samples.
	move.l	length,d0
	subq.l	#1,d0
toggle	bchg.b	#7,(a0)+
	dbf	d0,toggle

do_it	movea.l	#out_path,a0	find the end of the new path
	bsr	str_end

dot	cmpi.b	#'.',-(a0)	back up and look for the dot
	bne	dot
	addq.l	#1,a0

	
	movea.l	#smp_type,a1	change the file type
type	move.b	(a1)+,(a0)+
	bne	type

* get an output file path

	tst.b	aes91
	beq	f_out1
	fsel_input	#out_path,#no_pick,#lgsout1,#lgsout2
	bra	f_out2
f_out1	fsel_exinput	#out_path,#no_pick,#lgsout1
f_out2	tst.w	d0	error?
	beq	oops	yep
	tst.w	int_out+2	cancel?
	beq	done	yep, quit

	clr.w	-(sp)	output file is r/w
	pea	out_path
	move.w	#$3c,-(sp)	create output file
	trap	#1
	addq.l	#8,sp
	tst.l	d0
	bmi	oops
	move.w	d0,handle

	pea	magic	write header info
	move.l	#12,-(sp)
	move.w	handle,-(sp)
	move.w	#$40,-(sp)
	trap	#1
	lea	12(sp),sp
	tst.l	d0
	bmi	oops

	move.l	m_ptr,-(sp)	write the file
	move.l	length,-(sp)
	move.w	handle,-(sp)
	move.w	#$40,-(sp)
	trap	#1
	lea	12(sp),sp
	tst.l	d0
	bmi	oops
	

	move.w	handle,-(sp)	close the file
	move.w	#$3e,-(sp)
	trap	#1
	addq.l	#4,sp
	tst.l	d0
	bmi	oops
	
	move.l	m_ptr,-(sp)	free up the malloced memory
	move.w	#$49,-(sp)
	trap	#1
	addq.l	#6,sp
	tst.l	d0
	bne	oops

	form_alert	#2,#delete_text
	cmpi	#2,d0
	beq	again
	pea	in_path
	move.w	#$41,-(sp)
	trap	#1
	addq.l	#6,sp
	tst.l	d0
	beq	again

oops	neg.w	d0
	form_error	d0
done	v_clsvwk
	appl_exit
	clr.w	-(sp)
	move.w	#$4c,-(sp)
	trap	#1

str_end
	tst.b	(a0)+
	bne	str_end
	subq.l	#1,a0
	rts

slash	cmpi.b	#'\',-(a0)	back up and look for the back-slash
	bne	slash
	addq.l	#1,a0
	rts

	SECTION	DATA
aes91	dc.b	$ff
title_text
	dc.b	"[0][TO_GSND Version 1.05  | |      GEMSOUND|"
	dc.b	"  sample converter| ][Continue!]",0
alert_text
	dc.b	"[2][Select Input File Type| "
	dc.b	"|.SPL = Unsigned|.SAM = Signed]"
	dc.b	"[*.SPL|*.SAM|Done]",0
delete_text
	dc.b	"[2][| |Delete source file?][Yes|No]"
no_pick	dc.b	0
spl_pic	dc.b	"*.SPL",0
sam_pic	dc.b	"*.SAM",0
smp_type	dc.b	"SMP",0
lgs_in1	dc.b	"Select Input",0
lgs_in2	dc.b	"Sound File",0
lgsout1	dc.b	"Select",0
lgsout2	dc.b	"Output Path",0
magic	dc.l	$7E817E81
offset1	dc.w	4
offset2	dc.w	0
length	dc.l	0

	SECTION	BSS

in_pick	ds.b	14
in_path	ds.b	128
out_path	ds.b	128
sam	ds.w	1
pick	ds.l	1
handle	ds.w	1
m_ptr	ds.l	1
dta	ds.b	26
lange	ds.b	18
	ds.l	$80
bss_end

	include	aeslib.s
	include	vdilib.s
