* 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

* 	Version 2.00  (aka TO_GSNDV)
*
* This version is functionally no different than version 1.05.  What
* it is is my having fun with the capabilities of the DMA sound,
* experiments with the cookie jar and turning GEMSOUND off and on
* from within a program.  The 'V' in the title means that I have
* added Voice prompts to the alert boxes and file selectors.  It will
* only do that on a machine with DMA sound and is a memory hog, but it
* was fun to do.  Yep, those dulcet tones giving the prompts are mine.
* 8^}
*
*	sma	2/6/93

*	Version 2.01
*
* Oops - found a little error.  I was so used to using GEMSOUND,
* that I forgot to check without GEMSOUND.  Discovered that
* the voice prompts couldn't be turned off if GEMSOUND wasn't in
* the system.  Easy fix.
*
*	sma	2/9/93



	opt	a+,o+

	include	utils\initial.s


	graf_mouse	#0

	move.l	#"_SND",d7		look for sound cookie
	bsr	cookie
	tst.l	d7
	beq	title
	btst.l	#1,d6			check for DMA sound
	beq	title
	clr.b	dma
	move.l	#"GSND",d7		check for gemsound
	bsr	cookie
	tst.l	d7
	beq	voc_title
	clr.b	gsnd			if gemsound, turn it off
	move.l	d6,gsnd_ptr
	movea.l	d6,a0
	move.w	4(a0),ruhe
	move.w	#1,4(a0)
voc_title
	move.l	#voice1,d7	find the length of the file
	bsr	find_len
	tst.l	d0
	bne	oops

	move.l	length,d0	allocate some memory
	bsr	get_mem
	tst.l	d0
	beq	oops

	move.l	d0,m_ptr	read the voice fil\e
	move.l	#voice1,d0
	move.l	length,d6
	move.l	m_ptr,d7
	bsr	read_file
	tst.l	d0
	bmi	oops

	move.l	m_ptr,d5	play the voice file
	move.l	m_ptr,d6
	add.l	length,d6
	subq.l	#1,d6
	move.w	#$80,d7		mono,6.25 KHz
	bsr	sound

title	form_alert	#1,#title_text

	tst.b	dma
	bne	path
	move.l	m_ptr,d0	free up the memory
	bsr	free_mem
	tst.l	d0
	bmi	oops

	move.l	#voice2,d7	find the length of the file
	bsr	find_len
	tst.l	d0
	bne	oops

	move.l	length,d0	allocate some memory
	bsr	get_mem
	tst.l	d0
	beq	oops

	move.l	d0,m_ptr	read the voice file
	move.l	#voice2,d0
	move.l	length,d6
	move.l	m_ptr,d7
	bsr	read_file
	tst.l	d0
	bmi	oops

	move.l	m_ptr,d5	play the voice file
	move.l	m_ptr,d6
	add.l	length,d6
	subq.l	#1,d6
	move.w	#$00,d7		stereo,6.25 KHz
	bsr	sound

	form_alert	#1,#voice_text

	cmpi	#1,d0
	beq	sound_on
	bsr	restore_gsnd	aw, he doesn't like my voice!

sound_on
	move.l	m_ptr,d0	free up the memory
	bsr	free_mem
	tst.l	d0
	bmi	oops
	tst.b	dma		use the voices?
	bne	path		nope

	move.l	#voice3,d7	find the length of the file
	bsr	find_len
	tst.l	d0
	bne	oops

	move.l	length,d0	allocate some memory
	move.l	d0,vce3len	save length for later
	bsr	get_mem
	tst.l	d0
	beq	oops

	move.l	d0,vce3ptr	read the voice file
	move.l	#voice3,d0
	move.l	vce3len,d6
	move.l	vce3ptr,d7
	bsr	read_file
	tst.l	d0
	bmi	oops

	move.l	#voice4,d7	find the length of the file
	bsr	find_len
	tst.l	d0
	bne	oops

	move.l	length,d0	allocate some memory
	move.l	d0,vce4len	save length for later
	bsr	get_mem
	tst.l	d0
	beq	oops

	move.l	d0,vce4ptr	read the voice file
	move.l	#voice4,d0
	move.l	vce4len,d6
	move.l	vce4ptr,d7
	bsr	read_file
	tst.l	d0
	bmi	oops

	move.l	#voice5,d7	find the length of the file
	bsr	find_len
	tst.l	d0
	bne	oops

	move.l	length,d0	allocate some memory
	move.l	d0,vce5len	save length for later
	bsr	get_mem
	tst.l	d0
	beq	oops

	move.l	d0,vce5ptr	read the voice file
	move.l	#voice5,d0
	move.l	vce5len,d6
	move.l	vce5ptr,d7
	bsr	read_file
	tst.l	d0
	bmi	oops

	move.l	#voice6,d7	find the length of the file
	bsr	find_len
	tst.l	d0
	bne	oops

	move.l	length,d0	allocate some memory
	move.l	d0,vce6len	save length for later
	bsr	get_mem
	tst.l	d0
	beq	oops

	move.l	d0,vce6ptr	read the voice file
	move.l	#voice6,d0
	move.l	vce6len,d6
	move.l	vce6ptr,d7
	bsr	read_file
	tst.l	d0
	bmi	oops


path	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_exinput
	blt	again		nope
	clr.b	aes91
	lea	lgs_in2,a0	concatenate the lgs strings
	move.b	#' ',-1(a0)
	lea	lgsout2,a0
	move.b	#' ',-1(a0)

again	tst.b	dma		using voice prompts?
	bne	again1		nope.
	move.l	vce3ptr,d5	play the voice file
	move.l	vce3ptr,d6
	add.l	vce3len,d6
	subq.l	#1,d6
	move.w	#$00,d7		stereo,6.25 KHz
	bsr	sound


again1	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	dma		using voice prompts?
	bne	getin		nope.
	move.l	vce4ptr,d5	play the voice file
	move.l	vce4ptr,d6
	add.l	vce4len,d6
	subq.l	#1,d6
	move.w	#$80,d7		mono,6.25 KHz
	bsr	sound

getin	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

	move.l	#in_path,d7	find the input file length
	bsr	find_len
	tst.l	d0
	bne	oops
	move.l	length,len1

* this section moved and corrected in version 1.01
	
	move.l	len1,d0		malloc memory for file read
	bsr	get_mem
	tst.l	d0
	beq	oops		malloc error I missed the first time.
	move.l	d0,m_ptr
	
	move.l	#in_path,d0
	move.l	m_ptr,d7	read file
	move.l	len1,d6
	bsr	read_file
	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	len1,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	dma		using voice prompts?
	bne	getpath		nope.
	move.l	vce5ptr,d5	play the voice file
	move.l	vce5ptr,d6
	add.l	vce5len,d6
	subq.l	#1,d6
	move.w	#$80,d7		mono,6.25 KHz
	bsr	sound

getpath	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	len1,-(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,d0	free up the malloced memory
	bsr	free_mem
	tst.l	d0
	bne	oops

	tst.b	dma		using voice prompts?
	bne	kill_it		nope.
	move.l	vce6ptr,d5	play the voice file
	move.l	vce6ptr,d6
	add.l	vce6len,d6
	subq.l	#1,d6
	move.w	#$00,d7		stereo,6.25 KHz
	bsr	sound

kill_it	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	tst.b	dma	using voice prompts?
	bne	no_dma
	bsr	restore_gsnd

	move.l	vce3ptr,d0	free up the malloced memory
	bsr	free_mem
	tst.l	d0
	bne	no_dma

	move.l	vce4ptr,d0	free up the malloced memory
	bsr	free_mem
	tst.l	d0
	bne	no_dma

	move.l	vce5ptr,d0	free up the malloced memory
	bsr	free_mem
	tst.l	d0
	bne	no_dma

	move.l	vce6ptr,d0	free up the malloced memory
	bsr	free_mem

no_dma	v_clsvwk
	appl_exit
	clr.w	-(sp)
	move.w	#$4c,-(sp)
	trap	#1

restore_gsnd
	tst.b	dma		using voice prompts?
	bne	done1		no need to restore, never used.
	st	dma		restoring gemsound, so don't use dma
	tst.b	gsnd		was gemsound there?
	bne	done1		nope, no restore
	movea.l	gsnd_ptr,a0
	move.w	ruhe,4(a0)	restore gemsound
done1	rts

get_mem	move.l	d0,-(sp)	malloc memory for file read
	move.w	#$48,-(sp)
	trap	#1
	addq.l	#6,sp
	rts

free_mem
	move.l	d0,-(sp)
	move.w	#$49,-(sp)
	trap	#1
	addq.l	#6,sp
	rts

find_len
	pea	dta			use my own dta
	move.w	#$1a,-(sp)
	trap	#1
	addq.l	#6,sp

	clr.w	-(sp)
	move.l	d7,-(sp)
	move.w	#$4e,-(sp)
	trap	#1
	addq.l	#8,sp
	rts

read_file
*	read the file.	Pointer to file path and name in d0
*			memory pointer from malloc in d7
*			file size in d6
*			returns error status in d0

	clr.w	-(sp)	open the file read-only
	move.l	d0,-(sp)	input file name
	move.w	#$3d,-(sp)
	trap	#1
	addq.l	#8,sp
	tst.l	d0
	bmi	r_oops	file didn't open ok.
	move.w	d0,handle

	move.l	d7,-(sp)	memory ptr
	move.l	d6,-(sp)	file size
	move.w	handle,-(sp)	
	move.w	#$3f,-(sp)
	trap	#1
	lea	12(sp),sp
	tst.l	d0
	bmi	r_oops
	
	move.w	handle,-(sp)	close the file
	move.w	#$3e,-(sp)
	trap	#1
	addq.l	#4,sp
r_oops	rts

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]"
	dc.b	"[TO_GSND Version 2.01  "
	dc.b	"|       Vocal"
	dc.b	"|      GEMSOUND"
	dc.b	"|  sample converter|"
	dc.b	"][Continue!]",0
voice_text
	dc.b	"[2]"
	dc.b	"[| | "
	dc.b	"|Voice Prompts "
	dc.b	"][On|Off]",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
len1	dc.l	0
dma	dc.b	$ff
gsnd	dc.b	$ff
voice1	dc.b	"VOICE\TITLE.VCE",0
voice2	dc.b	"VOICE\PROMPTS.VCE",0
voice3	dc.b	"VOICE\FILETYPE.VCE",0
voice4	dc.b	"VOICE\INFILE.VCE",0
voice5	dc.b	"VOICE\PATH.VCE",0
voice6	dc.b	"VOICE\DELETE.VCE",0

	SECTION	BSS

in_pick	ds.b	14
in_path	ds.b	128
out_path	ds.b	128
gsnd_ptr	ds.l	1
ruhe	ds.w	1
sam	ds.w	1
pick	ds.l	1
handle	ds.w	1
m_ptr	ds.l	1
dta	ds.b	26
length	ds.b	18
vce3ptr	ds.l	1
vce3len	ds.l	1
vce4ptr	ds.l	1
vce4len	ds.l	1
vce5ptr	ds.l	1
vce5len	ds.l	1
vce6ptr	ds.l	1
vce6len	ds.l	1
	ds.l	$80
bss_end

	include utils\cookie.s
	include utils\sound.s
	include	aeslib.s
	include	vdilib.s

