
<CsoundSynthesizer>

<CsOptions>
-idevaudio -odevaudio -b100
</CsOptions>

<CsInstruments>

sr 	= 	44100	
ksmps 	= 	10
nchnls 	= 	2


;FLTK INTERFACE CODE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FLcolor	255, 255, 155, 0, 0, 0	;SET INTERFACE COLOURS
		FLpanel	"Simple Record->Playback Looped", 330,300, 0, 0

iy	=	10

#define RECORD_PLAY_FLTK(COUNT'INSTR_0S)
#
;SWITCHES                                 					ON | OFF | TYPE | WIDTH | HEIGHT | X | Y | OPCODE |     INS    | STARTTIM | IDUR
FLcolor	200,200, 200,255,0,0	;RED
gkRecord$COUNT, gihRecord$COUNT	FLbutton	"@circle",			1,    0,     2,     50,     30,    10, iy,    0,   1+$INSTR_0S,      0,      -1
FLsetTextColor 255, 0, 0, gihRecord$COUNT	;RED TEXT                                                                                            

FLcolor	200, 200, 200,0,255,0	;GREEN                                                                                                               
gkPlay$COUNT, gihPlay$COUNT	FLbutton	"@>",				1,    0,     2,     50,     30,    60, iy,    0,   2+$INSTR_0S,      0,      -1
FLsetTextColor 0, 200, 0, gihPlay$COUNT	;GREEN TEXT

FLcolor	255, 255, 255, 0, 0, 100	;SET INTERFACE COLOURS

;VALUE DISPLAY BOX				WIDTH | HEIGHT | X |  Y
idSpeed$COUNT		FLvalue	" ",     	60,       16,   120, iy+20
;SLIDER			            				MIN  | MAX | EXP | TYPE |    DISP       | WIDTH | HEIGHT | X | Y
gkSpeed$COUNT, ihSpeed$COUNT	FLslider 	"Speed",	-2,     2,    0,    3,   idSpeed$COUNT,    200,    20,    120, iy

FLsetVal_i	1, 	ihSpeed$COUNT
iy	=	iy+50
#

$RECORD_PLAY_FLTK(1'0)
$RECORD_PLAY_FLTK(2'100)
$RECORD_PLAY_FLTK(3'200)
$RECORD_PLAY_FLTK(4'300)
$RECORD_PLAY_FLTK(5'400)
$RECORD_PLAY_FLTK(6'500)

		FLpanel_end	; END OF PANEL CONTENTS

;INSTRUCTIONS AND INFO PANEL
				FLpanel	" ", 500, 140, 342, 0
;TEXT BOXES												TYPE | FONT | SIZE | WIDTH | HEIGHT | X | Y
ih		 	FLbox  	"              Recording To Multiple Tables                   ", 	1,      5,     14,    490,    15,     5,  0
ih		 	FLbox  	"-------------------------------------------------------------", 	1,      5,     14,    490,    15,     5,  20
ih		 	FLbox  	"This example layers 6 record/playback devices which record   ", 	1,      5,     14,    490,    15,     5,  40
ih		 	FLbox  	"and playback  from 6 independent function tables. Playback   ", 	1,      5,     14,    490,    15,     5,  60
ih		 	FLbox  	"and recording are looped. A playback speed control for each  ", 	1,      5,     14,    490,    15,     5,  80
ih		 	FLbox  	"player is added. The code makes use of macros to minimize    ", 	1,      5,     14,    490,    15,     5, 100
ih		 	FLbox  	"code repetition.                                             ", 	1,      5,     14,    490,    15,     5, 120

				FLpanel_end

		FLrun		; RUN THE WIDGET THREAD!

giRecBuf1	ftgen	1, 0, 1048576, -7, 0, 1048576, 0	;AUDIO DATA STORAGE
giRecBuf1	ftgen	2, 0, 1048576, -7, 0, 1048576, 0	;AUDIO DATA STORAGE
giRecBuf1	ftgen	3, 0, 1048576, -7, 0, 1048576, 0	;AUDIO DATA STORAGE
giRecBuf1	ftgen	4, 0, 1048576, -7, 0, 1048576, 0	;AUDIO DATA STORAGE
giRecBuf1	ftgen	5, 0, 1048576, -7, 0, 1048576, 0	;AUDIO DATA STORAGE
giRecBuf1	ftgen	6, 0, 1048576, -7, 0, 1048576, 0	;AUDIO DATA STORAGE

gitablelen	init	1048576
girecdur1	init	0
girecdur2	init	0
girecdur3	init	0
girecdur4	init	0
girecdur5	init	0
girecdur6	init	0

#define	RECORD_PLAY_INSTRS(COUNT'RECORD'PLAY)
#
instr	$RECORD	;RECORD
	if	gkRecord$COUNT=0	then		;IF RECORD BUTTON IS DEACITVATED...
		turnoff				;...TURNOFF THIS INSTRUMENT IMMEDIATELY
	endif					;END OF CONDITIONAL BRANCHING

	ain		inch		1			;READ AUDIO FROM LIVE INPUT CHANNEL 1
	andx		phasor		sr/gitablelen		;CREATE A POINTER FOR WRITING TO TABLE - FREQUENCY OF POINTER IS DEPENDENT UPON TABLE LENGTH AND SAMPLE RATE
	andx		=		andx*gitablelen		;RESCALE POINTER ACCORDING TO LENGTH OF FUNCTION TABLE 
	gkrecdur$COUNT	downsamp	andx			;CREATE A K-RATE GLOBAL VARIABLE THAT WILL BE USED BY THE 'PLAYBACK' INSTRUMENT TO DETERMINE THE LENGTH OF RECORDED DATA			
	;OPCODE		INPUT | INDEX | FUNCTION_TABLE
	tablew		ain,     andx,     $COUNT	;WRITE AUDIO TO AUDIO STORAGE TABLE
endin
		
instr	$PLAY	;PLAYBACK INSTRUMENT
	if	gkPlay$COUNT=0	then		;IF PLAY BUTTON IS DEACTIVATED THEN...
		turnoff				;...TURNOFF THIS INSTRUMENT IMMEDIATELY
	endif					;END OF CONDITIONAL BRANCHING

	andx		phasor		(sr*gkSpeed$COUNT)/i(gkrecdur$COUNT)	;CREATE A POINTER FOR WRITING TO TABLE - FREQUENCY OF POINTER IS DEPENDENT UPON TABLE LENGTH AND SAMPLE RATE
	andx		=		andx*i(gkrecdur$COUNT)	;RESCALE POINTER ACCORDING TO LENGTH OF PREVIOUS AUDIO RECORDING 
	;OUT 	OPCODE 		INDEX | FUNCTION_TABLE
	asig	tablei		andx,      $COUNT		;READ AUDIO FROM AUDIO STORAGE FUNCTION TABLE
	outs	asig, asig					;SEND AUDIO TO OUTPUTS
endin
#

$RECORD_PLAY_INSTRS(1'1'2)
$RECORD_PLAY_INSTRS(2'101'102)
$RECORD_PLAY_INSTRS(3'201'202)
$RECORD_PLAY_INSTRS(4'301'302)
$RECORD_PLAY_INSTRS(5'401'402)
$RECORD_PLAY_INSTRS(6'501'502)

</CsInstruments>

<CsScore>
f 0 3600
</CsScore>

</CsoundSynthesizer>