User-Defined Opcode Database

BufRec1

Records in a mono buffer (function table)

Download UDO File

Description

Records in a mono buffer (function table), with optional start point, end point, and wrap (= loop record).
The example below has different tests for ensuring that BufRec works as expected. See the example for the UDO BufCt for another example with live recording.

Syntax

kfin BufRec1 ain, ift, krec, kstart, kend, kwrap

Initialization

ift - function table for recording

Performance

ain - audio signal to record
krec - 1 for recording, 0 (or any other number) stops recording
kstart - begin of recording into the function table in seconds
kend - end of recording into the function table in seconds
kwrap - if 1, recording wraps between kend and the beginning of the buffer (see th examples below for instr 4); if 0 (or any other number), record stops at kend
kfin - 1 if record has finished

Code

  opcode BufRec1, k, aikkkk
ain, ift, krec, kstart, kend, kwrap xin
		setksmps	1
kendsmps	=		kend*sr ;end point in samples
kendsmps	=		(kendsmps == 0 || kendsmps > ftlen(ift) ? ftlen(ift) : kendsmps)
kfinished	=		0
knew		changed	krec ;1 if record just started
 if krec == 1 then
  if knew == 1 then
kndx		=		kstart * sr - 1 ;first index to write minus one
  endif
  if kndx >= kendsmps-1 && kwrap == 1 then
kndx		=		-1
  endif
  if kndx < kendsmps-1 then
kndx		=		kndx + 1
andx		=		kndx
		tabw		ain, andx, ift
  else
kfinished	=		1
  endif
 endif
 		xout		kfinished
  endop

Examples

<CsoundSynthesizer>
<CsOptions>
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 32
nchnls = 1
0dbfs = 1

giBuf1		ftgen		1, 0, -(1*sr), 2, 0 ;1 second
giBuf2		ftgen		2, 0, -(2*sr), 2, 0 ;2 seconds
giBuf3		ftgen		3, 0, -(3*sr), 2, 0 ;3 seconds
giBuf5		ftgen		5, 0, -(5*sr), 2, 0 ;5 seconds
giBuf10	ftgen		10, 0, -(10*sr), 2, 0 ;10 seconds

      
  opcode BufRec1, k, aikkkk
ain, ift, krec, kstart, kend, kwrap xin
		setksmps	1
kendsmps	=		kend*sr ;end point in samples
kendsmps	=		(kendsmps == 0 || kendsmps > ftlen(ift) ? ftlen(ift) : kendsmps)
kfinished	=		0
knew		changed	krec ;1 if record just started
 if krec == 1 then
  if knew == 1 then
kndx		=		kstart * sr - 1 ;first index to write minus one
  endif
  if kndx >= kendsmps-1 && kwrap == 1 then
kndx		=		-1
  endif
  if kndx < kendsmps-1 then
kndx		=		kndx + 1
andx		=		kndx
		tabw		ain, andx, ift
  else
kfinished	=		1
  endif
 endif
 		xout		kfinished
  endop

  opcode TableToSF, 0, iSkjoj
;writes a function table to an audio file (default: wav 24 bit) if ktrig=1
ift, Soutname, ktrig, iformat, istart, iend xin; start (default = 0) and end (default = last sample) in seconds
istrtsmps =         istart*sr; start to write in samples 
iendsmps  =         (iend == -1 ? ftlen(ift) : iend*sr); end to write in samples
 if iformat == -1 then
iformat   =         18; default: wav 24 bit (for other options see fout manual page)
 endif
 if ktrig == 1 then; make sure that trigger sends "1" just for one k-cycle
kcnt      init      istrtsmps; set the counter to 0 at start
loop:
kcnt      =         kcnt+ksmps; next value (e.g. 10 if ksmps=10)
andx      interp    kcnt-1; build audio index (e.g. from 0 to 9)
asig      tab       andx, ift; read the table values as audio signal
          fout      Soutname, iformat, asig; write asig to a file
 if kcnt <= iendsmps-ksmps kgoto loop; go back as long there is something to do
 endif 
  endop  



instr 1 ;first test: writing a signal from -1 to 1 into the buffer
ain		linseg		-1, p3, 1
kfin		BufRec1	ain, p3, 1, 0, 0, 0
krel		release
 if krel == 1 then
Sfile		sprintf	"Buffer%dFirstTest.wav", p3 
 		TableToSF	p3, Sfile, 1
 endif
endin


instr 2 ;second test: writing a sine into the buffer and playing it back
ain		oscils		1, 400, 0
kfin		BufRec1	ain, p3, 1, 0, 0, 0
krel		release
Sfile		sprintf	"Buffer%dSecondTest.wav", p3 
 		TableToSF	p3, Sfile, krel
endin

instr 3 ;third test: writing from-to
ain		oscils		1, 400, 0
kfin		BufRec1	ain, p3, 1, p4, p5, 0 ;ain, ift, krec, kstart, kend, kwrap
krel		release
Sfile		sprintf	"Buffer%dThirdTest.wav", p3 
 		TableToSF	p3, Sfile, krel
endin

instr 4 ;third test: writing with wrap
ain		oscils		1, 400, 0
kfin		BufRec1	ain, p4, 1, p5, p6, 1 ;ain, ift, krec, kstart, kend, kwrap
krel		release
Sfile		sprintf	"Buffer%dFourthTest.wav", p4
 		TableToSF	p4, Sfile, krel
endin

instr 5 ;recording live (count 1, 2, ...)
ain		inch		1
kfin		BufRec1	ain, 10, 1, 5, 0, 1
krel		release
Sfile		sprintf	"Buffer%dFifthTest.wav", 10
 		TableToSF	10, Sfile, krel
endin

instr 6 ;checking kfin
ain		inch		1
;records for 2 seconds and stops recording at the end of the buffer as no wrap required:
kfin		BufRec1	ain, 2, 1, 0, 0, 0 
		printk2	kfin ;prints '1' after 2 seconds
endin


instr 10 ;play one buffer
aout		poscil3 	1, 1/(ftlen(10)/sr), 10, 0
		out		aout
endin


</CsInstruments>
<CsScore>
;;first test: writing -1 to 1 line in soundfiles "BufferNFirstTest.wav"
;i 1 0 1
;i 1 0 2
;i 1 0 3
;i 1 0 5
;i 1 0 10
;;second test: writing a sine into buffers and playing it back
;i 2 0 1
;i 2 0 2
;i 2 0 3
;i 2 0 5
;i 2 0 10
;i 10 1 1
;i 10 3 2
;i 10 6 3
;i 10 10 5
;i 10 16 10
;;third test: writing from-to
;i 3 0 1 0 .3
;i 3 0 2 1 2
;i 3 0 3 1 2
;i 3 0 5 4.9 5
;;fourth test: writing with wrap
;i 4 0 .5 1 .6 .9 
;i 4 0 1.5 2 1 0
;i 4 0 .5 3 2.6 2.9 
;i 4 0 .5 5 .6 .9 
;i 4 0 9 10 5 0
;;fifth test: live recording from 5 starting with wrap (count 1 - 2 - 3 ... to hear the effect)
i 5 0 10
i 10 10 10
;;checking kfin
;i 6 0 5
</CsScore>
</CsoundSynthesizer>

Credits

joachim heintz 2010


Previous Home Next
KeyStay   BufPlay1