
*	8to16 an interpolating converter 8-bit 2 16-bit samples
*	version 1.3
*	by IMMORTAL Systems

*	USAGE:	8to16 [-q] <filename>
; $VER: 8to16 v1.3

*	definitions:

cIffHead = "FORM"
c8svxHead = "8SVX"
cVHDRHead = "VHDR"
ctbufmax = 128
cSizeOffset = 124
cBodyID="BODY"

cRaw16Size = 8
cIffHeadSize = $48
cMaudHeader = mMaudEnd-mMaudHeader

	XREF _AnotherLI

* PhxASS special directivec

	INCDIR "include:"
	INCLUDE "phxmacros.i" 
	INCLUDE "lib/dos.i"
	INCLUDE "lib/exec.i"
	


* code section

		section PRG,code

* start is here :))))))))))))))))


		move.l 4.w,a6
		lea cDosName,a1
		moveq #0,d0
		jsr (OpenLibrary,a6)
		tst.l d0
		beq LNoDos
		move.l d0,mDos
		move.l d0,a6


* load params...
		lea mTbuf,a0
		move.l a0,d1
		move.l #ctbufmax,d2
		moveq #0,d3
		jsr (ReadItem,a6)
		tst.l d0
		beq LNothing
		cmp.b #$2d,mTbuf  ;"-"
		bne .somethingelse
		jsr sParameters
		lea mTbuf,a0
		move.l a0,d1
		move.l #ctbufmax,d2
		moveq #0,d3
		jsr (ReadItem,a6)
		tst.l d0
		beq LNothing

* filename is in mTubf NOW!

.somethingelse	

* advert
		lea tMain,a0
		jsr sPRINT

		lea mTbuf,a0
		move.l a0,d1
		move.l #1005,d2
		jsr (Open,a6)
		tst.l d0
		bne .found
		lea tNOTFOUND,a0
		jsr sPRINT
		jmp LErr1
.found		move.l d0,mFH
		move.l d0,d1
		lea mEXBUF,a0
		move.l a0,d2
		jsr (ExamineFH,a6)
		tst.l d0
		beq LErr2
		
;allocate memory

		lea mEXBUF,a0
		add.l #cSizeOffset,a0
		move.l (a0),d0
		move.l d0,mSourceSize
		moveq #0,d1
		move.l 4.w,a6
		jsr (AllocVec,a6)
		tst.l d0
		bne .memory
		lea tNOMEM,a0
		jsr sPRINT
		jmp LErr2

;load file
.memory		move.l d0,mSource
		move.l d0,d2
		move.l mSourceSize,d3
		move.l mFH,d1
		move.l mDos,a6
		jsr (Read,a6)

;examine fileformat

		move.l mSource,a0
		cmp.l #cIffHead,(a0)+
		bne LRaw
		add.l #4,a0
		cmp.l #c8svxHead,(a0)+
		bne LUnknownIFF
		cmp.l #cVHDRHead,(a0)
		bne LUnknownIFF

* IFF 8SVX FILE DETECTED..................

		move.l mSource,a0
		move.l mSource,a1
		add.l mSourceSize,a1
.bodyscan	move.l (a0)+,d0
		cmp.l #cBodyID,d0
		beq .bodyfound
		cmp.l a0,a1
		bne .bodyscan
		bra LUnknownIFF
.bodyfound	move.l (a0)+,mSampleSize
		move.l a0,mSample
		lea tIFFOK,a0
		jsr sPRINT
		jmp LProcess

* RAW FILE DETECTED

LRaw		lea tRAWOK,a0
		jsr sPRINT
		move.l mSource,a0
		move.l a0,mSample
		move.l mSourceSize,d0
		move.l d0,mSampleSize
		jmp LProcess

* BAD IFF

LUnknownIFF	lea tBADIFF,a0
		jsr sPRINT
		jmp LErr3

* Process IT !!!!!!

LProcess	move.l mSampleSize,d0
		bne .notempty

; sample has length=0...leaving...

		lea tEMPTY,a0
		jsr sPRINT
		jmp LErr3

.notempty	move.l mSampleSize,d0
                asl.l #1,d0
		move.l mUseKirk,d7
		bne .nokirk

* KIRK's
		asl.l #1,d0
		asl.l #1,d0
		push.l a0
		lea tKIRK,a0
		jsr sPRINT
		pop.l a0
		bra .nodown


.nokirk		move.l mDown,d7
		bne .nodown
		asl.l #1,d0
		push.l a0
		lea tDOWN,a0
		jsr sPRINT
		pop.l a0
	
.nodown		move.l d0,mDestSampleSize	
		move.l mUseMaud,d7
		bne .raw16
;maud header
		add.l #cMaudHeader,d0
		bra .noraw16

.raw16		add.l #cRaw16Size,d0
.noraw16	move.l d0,mDestSize
		moveq #0,d1
		move.l 4.w,a6
		jsr (AllocVec,a6)
		tst.l d0
		bne .havemem
		lea tNOMEM,a0
		jsr sPRINT
		jmp LErr3
.havemem

* copy header
		move.l d0,mDest
		move.l d0,a0
		move.l d0,a1
		add.l mDestSize,a1
		move.l a1,mDestOver
		
;maud test
		move.l  mUseMaud,d7
		bne .raw16h
;maud saving here
		
		move.l mDestSampleSize,d0
		move.l d0,mMaudRawSize
		push.l d0
		lsr.l #1,d0
		move.l d0,mMaudSamples
		pop.l d0
		add.l #56,d0
		move.l d0,mMaudSize
		lea mMaudHeader,a4
		lea mMaudEnd,a5
.maudloop	move.w (a4)+,(a0)+
		cmp.l a5,a4
		bne .maudloop
		bra .noraw16h


.raw16h		move.l mRaw16Head,(a0)+
		move.l mRaw16Head+4,(a0)+

.noraw16h
* store

		move.l a0,mDestSample
		move.l a0,a1

* completed: closing original file.....it will be DESTROYED!!!!
		store
		move.l mDos,a6
		move.l mFH,d1
		jsr (Close,a6)
		recall

* init pointers for interpolation process
*  a1=target.....DONE
*  a0,source

		move.l mSample,a0

*  a2 = a0 limit
		
		move.l a0,a2
		add.l mSampleSize,a2

* test kirk

		move.l mUseKirk,d7
		bne NormalLI		

* using kirk...


		jsr _AnotherLI
		jmp LIDONE



* init done, starting process........
; d0=buffer a3=adr of last compared d1=count
; d2=last d5=interpolated words

NormalLI
		moveq #0,d5		
.loop		moveq #0,d1
		move.l a0,a3
		move.b (a0)+,d2

.loop1		inc.l d1
		move.l mDown,d7
		bne .nodown2
		inc.l d1
.nodown2	cmp.l a2,a0
		beq LDone
		move.l a0,a3
		move.b (a0)+,d7	
		cmp.b d2,d7
		beq .loop1

		
		asl.w #8,d2
		move.b (a3),d0
		asl.w #8,d0
		cmp.l #1,d1
		bne .interpolateit
;write it
		move.w d2,(a1)+
		move.l a3,a0
		jmp .loop

;interpolation here

.interpolateit

;compute step first
;= d3
		move.w d0,d3
		sub.w d2,d3
		and.l #$0000FFFF,d3
		ext.l d3
		divs.w d1,d3
		and.l #$FFFF,d3

;d3.w = interpolation step

;now write it to destination buffer !
;d4=write counter

		moveq #0,d4
		move.w d2,d0
.loop5		move.w d0,(a1)+
		add.w d3,d0
		inc.l d4
		cmp.l d1,d4
		bne .loop5
;done
		add.l d1,d5
		dec.l d5
		move.l a3,a0
		jmp .loop
				
;write rest of data		
LDone		moveq #0,d4
		asl.l #8,d2
.loop6		move.w d2,(a1)+
		inc.l d4
		cmp.l d1,d4
		bne .loop6
;done

LIDONE

		move.l  mUseMaud,d7
		bne .writeraw
		
		lea tIDONEMAUD,a0
		jsr sPRINT
		bra .nowraw

.writeraw	lea tIDONERAW,a0
		jsr sPRINT

;write to file
.nowraw		lea mTbuf,a0
		move.l a0,d1
		move.l #1006,d2
		move.l mDos,a6
		jsr (Open,a6)
		tst.l d0
		bne .opened
		lea tCANNOTOPEN,a0
		jsr sPRINT
		jmp LExit
.opened		move.l mDest,d2
		move.l mDestSize,d3
		move.l d0,d1
		move.l d0,mFH
		jsr (Write,a6)
		move.l mFH,d1
		jsr (Close,a6)

;file written


		lea tEFFIC,a0
		jsr sPRINT

;compute efficiency --> d5

		and.l #$0000FFFF,d5
		mulu.w #10,d5
		move.l mSampleSize,d6
		and.l #$0000FFFF,d6
		divu.w d6,d5
		and.l #$FFFF,d5
		push.l d7
		move.l mDown,d7
		bne .nodown2
		lsr.w #1,d5
.nodown2	pop.l d7

	
;print it
.eflop		tst.l d5
		beq .efdone
		push.l d5
		lea tE,a0
		jsr sPRINT
		pop.l d5
		dec.l d5
		jmp .eflop

.efdone		lea tE2,a0
		jsr sPRINT
		



* ALL DONE, EXITING

LExit		move.l 4.w,a6
		move.l mSource,a1
		jsr (FreeVec,a6)
		move.l mDest,a1
		jsr (FreeVec,a6)
		move.l mDos,a1
		jsr (CloseLibrary,a6)
		moveq #0,d0
		rts


* -----------------------------------------------------------



LNothing

* advert
		lea tMain,a0
		jsr sPRINT


		lea tUSAGE,a0
		jsr sPRINT

	
;end

		move.l 4.w,a6
		move.l mDos,a1
		jsr (CloseLibrary,a6)
		moveq #0,d0
		rts

LNoDos		moveq #$2f,d0
		rts
		

LErr3		move.l 4.w,a6
		move.l mSource,a1
		jsr (FreeVec,a6)
LErr2		move.l mDos,a6
		move.l mFH,d1
		jsr (Close,a6)
LErr1		move.l 4.w,a6
		move.l mDos,a1
		jsr (CloseLibrary,a6)
		moveq #20,d0
		rts


* SUBROUTINES ARE HERE

sEOL		lea mLF,a0
sPRINT		store
		cmp.l #0,mQuiet
		beq .shutup
		move.l a0,d1
                              move.l mDos,a6
		jsr (PutStr,a6)
.shutup		recall
		rts

sParameters	lea mTbuf,a0
.loop		inc.l a0
		tst.b (a0)
		beq .endparam
		move.b (a0),d0
		cmp.b #'q',d0
		bne .noq
		move.l #0,mQuiet
.noq		cmp.b #'d',d0
		bne .nod
		move.l #0,mDown
.nod		cmp.b #'M',d0
		bne .nomau
		move.l #0,mUseMaud
.nomau		cmp.b #'k',d0
		bne .loop
		move.l #0,mUseKirk
		bra .loop
.endparam	rts







* data section

		section dt,data



mId		dc.l 0
mTbuf		dcb.b $80 ,0
mQuiet		dc.l 1
mDown		dc.l 1
mUseMaud	dc.l 1
mUseKirk	dc.l 1
mLF		dc.b 10,0
mDos		dc.l 0
cDosName	dc.b "dos.library",0
mFH		dc.l 0
mSource		dc.l 0
mSourceSize	dc.l 0

mSample		dc.l 0
mSampleSize	dc.l 0
mHex		dc.w 0

mEXBUF		dcb.l $40,0

mRaw16Head	dc.b "Raw16Bit"
mDestSize	dc.l 0
mDest		dc.l 0
mDestSample	dc.l 0
mDestSampleSize		dc.l 0
mDestOver	dc.l 0


; maud structure definition

mMaudHeader	dc.b "FORM"
mMaudSize	dc.l 0
mMaud2		dc.b "MAUDMHDR",0,0,0,32
mMaudSamples	dc.l 0
mOtherMaud	dc.l $00100010,$000020ab,$00010000,$00010000,0,0,0
mMaudData	dc.b "MDAT"
mMaudRawSize	dc.l 0

mMaudEnd


tVersion	dc.b "$VER: 8to16 v1.3",0
tMain		dc.b 10,"8to16 v1.3 interpolating sample converter",10,"(c) 1997 IMMORTAL Systems & Jens Kirk",10,0


tUSAGE		dc.b 10,"USAGE:  8to16 [-qdMk] <filename>",10,"-q QUIET -d DOUBLE MODE -M Maud -k Kirk's interpolation",10,10,0
tNOTFOUND	dc.b "File NOT found!",10,10,0
tNOMEM		dc.b "Out of memory!",10,10,0
tIFFOK		dc.b "Processing IFF-8SVX file...",10,10,0
tRAWOK		dc.b "Processing 8-bit RAW file...",10,10,0
tBADIFF		dc.b "Unsupported IFF file!",10,10,0
tEMPTY		dc.b "Sample TOO short!",10,10,0
tIDONERAW	dc.b "Interpolation done,saving RAW16bit...",10,10,0
tIDONEMAUD	dc.b "Interpolation done,saving IFF-MAUD...",10,10,0
tEFFIC		dc.b "Efficiency: [",0
tE		dc.b "*",0
tE2		dc.b "]",10,10,0
tCANNOTOPEN	dc.b "Cannot open file for output!!!",10,10,0
tDOWN		dc.b "Doubling sample rate to increase efficiency...",10,10,0
tKIRK           dc.b "Using KIRK's interpolation method...",10,10,0


