;	transposedemo.a -- by Teijo Kinnunen

; This program demonstrates the usage of both the input and output
; routines of midiroutines.a.
; It reads in notes from the MIDI-interface, prints the name of a
; note on the screen, and sends the note out after transposing it
; one octave up.
; (the code could be better optimized, but I think it's not vital)

; Press CTRL-C to exit!!

	XREF	_GetSerial
	XREF	_FreeSerial
	XREF	_AddMIDIData

	XDEF	_sigmask
	XDEF	_maintsk
	XDEF	_inputbuff

	CODE
		movem.l	d1-d7/a0-a6,-(sp)
; ------ Open the dos.library --------------------------------------------
		movea.l	4,a6
		lea	dosname(pc),a1
		moveq	#0,d0
		jsr	-$228(a6)	;OpenLibrary()
		tst.l	d0
		beq.w	exit0
		move.l	d0,dosbase
; ------ Get a signal bit for MIDI input notification --------------------
		moveq	#-1,d0
		jsr	-$14a(a6)	;AllocSignal()
		tst.l	d0
		bmi.w	exit1
		move.l	d0,sigbit
		moveq	#1,d5
		lsl.l	d0,d5
		move.l	d5,_sigmask
		or.l	#1<<12,d5	;add SIGBREAKF_CTRL_C
; ------ Find the address of this task -----------------------------------
		suba.l	a1,a1
		jsr	-$126(a6)	;FindTask()
		move.l	d0,_maintsk
; ------ Get the serial port ---------------------------------------------
		jsr	_GetSerial(pc)
		tst.l	d0
		bne.w	exit2
; ====== MAIN LOOP =======================================================
mainloop	move.l	d5,d0
		jsr	-$13e(a6)	;Wait()
		btst	#12,d0		;SIGBREAK_CTRL_C?
		bne.w	exit3
; make sure that the RBF doesn't disturb us by sending a new note before
; we've finished
		move.l	_inputbuff,_inputbuff2
		move.b	_inputbuff2(pc),d7
		and.b	#$f0,d7
		cmp.b	#$90,d7
		bne.s	nowrite	;if it's Note Off, we don't display its name
		tst.b	_inputbuff2+2 ;if volume == 0, it's Note Off
		beq.s	nowrite
		moveq	#0,d7
		move.b	_inputbuff2+1(pc),d7	;get note number
		divu	#12,d7
		move.w	d7,d6		;d6 = octave number
		swap	d7		;d7.w = note (0 - 11)
		add.w	d6,d6		;* 2
		add.w	d7,d7
		lea	notenames(pc),a0
		lea	printbuff(pc),a1
		move.w	0(a0,d7.w),(a1)+	;note name
		lea	octaves(pc),a0
		move.w	0(a0,d6.w),(a1)+	;octave
; ------ Write the note name ---------------------------------------------
		move.l	a6,-(sp)
		move.l	dosbase(pc),a6
		jsr	-$3c(a6)	;Output()
		move.l	d0,d1
		move.l	#printbuff,d2
		moveq	#5,d3
		jsr	-$30(a6)	;Write()
		move.l	(sp)+,a6
; ------ Transpose and send out the note ---------------------------------
nowrite		add.b	#12,_inputbuff2+1	;transpose octave up
		bmi.s	nosendout		;> 127 => not possible
		lea	_inputbuff2(pc),a0
		moveq	#3,d0
		jsr	_AddMIDIData(pc)
nosendout	bra.w	mainloop
; ========================================================================
; ------ Free the serial port --------------------------------------------
exit3		jsr	_FreeSerial(pc)
; ------ Free the signal bit ---------------------------------------------
exit2		move.l	sigbit(pc),d0
		jsr	-$150(a6)	;FreeSignal()
; ------ Close dos.library -----------------------------------------------
exit1		move.l	dosbase(pc),a1
		movea.l	4,a6
		jsr	-$19e(a6)	;CloseLibrary()
; ------ Exit ------------------------------------------------------------
exit0		movem.l	(sp)+,d1-d7/a0-a6
		moveq	#0,d0
		rts
; ------ Variables -------------------------------------------------------

dosbase		dc.l	0
sigbit		dc.l	0
_maintsk	dc.l	0
_sigmask	dc.l	0
_inputbuff	dc.l	0
_inputbuff2	dc.l	0
notenames	dc.b	'C-C#D-D#E-F-F#G-G#A-A#B-'
octaves		dc.b	'-10 1 2 3 4 5 6 7 8 9 '
printbuff	dc.w	0,0
		dc.b	13
dosname		dc.b	'dos.library',0
	END
