*****************************************************************
*                                                               *
*       maestix.library-Demo:   Realtime FX                     *
*                                                               *
*****************************************************************
*
*       Programmed by           Richard Körber
*       Date                    1995-06-26
*
*****************************************************************
*  This  demonstration  shows  the  realtime effect feature of  *
*  the  maestix library.   It allocates the  Maestro soundcard  *
*  and switches to realtime effect.                             *
*****************************************************************

		INCDIR  "include:"
		INCLUDE exec.i                  ; Library call macros
		INCLUDE intuition.i
		INCLUDE graphics.i
		INCLUDE dos.i
		INCLUDE maestix.i
		INCLUDE libraries/maestix.i     ;Reference includes
		INCLUDE intuition/intuition.i
		INCLUDE dos/dostags.i
		INCLUDE exec/ports.i


BUFFMS          EQU     333     ;<- Buffer duration in milliseconds
VOLUME          EQU     180     ;<- Echo starting volume (0..256)
DECAY           EQU     256     ;<- Decay of each echo loop (0..256)

RATE		EQU	44100	;<- Sampling rate


		SECTION text,CODE

BUFFSIZE        EQU     ((BUFFMS*2*RATE)/1000/2)*2 ;do not change!

*---------------------------------------------------------------*
*       == START OF PROGRAM ==                                  *
*                                                               *
start   ;-- Open all libraries -----------------;
		lea     maestname(PC),a1        ;maestix
		moveq   #39,d0                  ; V39+
		exec    OpenLibrary             ; open
		move.l  d0,maestbase            ; store base
		beq     error1                  ; not found!
		lea     intuiname(PC),a1        ;intuition
		moveq   #36,d0                  ; V36+
		exec    OpenLibrary             ; open
		move.l  d0,intuibase            ; store base
		beq     error2
	;-- Allocate Maestro -------------------;
		sub.l   a0,a0                   ;no tags
		maest   AllocMaestro
		move.l  d0,maestro              ;^Maestro base
		beq     error3
	;-- Set Modus --------------------------;
		move.l  maestro(PC),a0          ;^Maestro base
		lea     modustags(PC),a1        ;^Modus tags
		maest   SetMaestro              ;set it now
		move.l  maestro(PC),a0          ;^Maestro base
		lea     realtags(PC),a1
		maest   StartRealtime
	;-- Open a window ----------------------;
		sub.l   a0,a0                   ;no newwindow struct
		lea     windowtags(PC),a1       ; but loads of tags
		intui   OpenWindowTagList
		move.l  d0,window
		beq     error4
	;-- Wait for reply (main loop) ---------;
.mainloop       move.l  window(PC),a0           ;window message?
		move.l  wd_UserPort(a0),a0      ; ^message port
		exec    WaitPort
.nextmsg        move.l  window(PC),a0           ;window message?
		move.l  wd_UserPort(a0),a0      ; ^message port
		exec    GetMsg                  ; try to get it...
		tst.l   d0                      ; got one?
		beq.b   .mainloop               ; nope: wait for next msg
	;---- got a window event ---------------;
		move.l  d0,a0                   ;^IDCMP-message -> a0
		cmp.l   #IDCMP_CLOSEWINDOW,im_Class(a0) ;close window?
		bne.b   .nextmsg                ; no -> look for next msg
	;-- Exit program -----------------------;
exit            move.l  window(PC),a0           ;Close output window
		intui   CloseWindow
error4          move.l  maestro(PC),a0          ;Stop realtime
		maest   StopRealtime
		move.l  maestro(PC),a0          ;Set maestro free
		maest   FreeMaestro
error3          move.l  intuibase(PC),a1        ;close intuition library
		exec    CloseLibrary
error2          move.l  maestbase(PC),a1        ;close maestix library
		exec    CloseLibrary
error1          moveq   #0,d0                   ;Reply 0
		rts                             ;back to CLI

*---------------------------------------------------------------*
*       == DATA SECTION ==                                      *
*                                                               *
maestbase       dc.l    0                       ;^Maestix Lib Base
intuibase       dc.l    0                       ;^Intuition Lib Base
maestro         dc.l    0                       ;^Maestro Base
window          dc.l    0                       ;^Window structure

modustags       dc.l    MTAG_Output,OUTPUT_FIFO  ;Input -> Output
		dc.l    MTAG_Input,INPUT_STD     ;Custom input
		dc.l    MTAG_CopyProh,CPROH_OFF  ;No copy protection
		dc.l    MTAG_Emphasis,EMPH_INPUT ;Emphasis see input
		dc.l    MTAG_Source,SRC_INPUT    ;Source see input
		dc.l    MTAG_Rate,RATE_INPUT     ;Rate see input
		dc.l    TAG_DONE

realtags        dc.l    MTAG_Effect,11  ;Echo the signal
		dc.l    MTAG_A0,torus	;^to torus structure
		dc.l    MTAG_D2,VOLUME	;entry volume
		dc.l    MTAG_D3,DECAY	;decay volume
		dc.l    TAG_DONE

torus           dc.l    buff_l,buff_r,BUFFSIZE,0 ;ptr to buffers and size
	

windowtags      dc.l    WA_IDCMP,IDCMP_CLOSEWINDOW ;<- New window's tags
		dc.l    WA_Title,.title
		dc.l    WA_InnerWidth,150
		dc.l    WA_InnerHeight,0
		dc.l    WA_DragBar,-1
		dc.l    WA_DepthGadget,-1
		dc.l    WA_CloseGadget,-1
		dc.l    WA_Activate,-1
		dc.l    WA_RMBTrap,-1
		dc.l    TAG_DONE
.title          dc.b    "Realtime FX",0
		even

maestname       dc.b    "maestix.library",0     ;Maestix name
intuiname       dc.b    "intuition.library",0   ;Intuition name
		even


		SECTION buffer,BSS

buff_l          ds.b    BUFFSIZE		;left torus
buff_r          ds.b    BUFFSIZE		;right torus


*---------------------------------------------------------------*
*       == END ==                                               *
*                                                               *
		END
