csound / manual

Csound Reference Manual (English)
Other
46 stars 29 forks source link

add example for nstance to clarify usage together with turnoff #624

Open gesellkammer opened 2 years ago

gesellkammer commented 2 years ago

I would propose:


I would propose:

```csound

<CsoundSynthesizer>
<CsOptions>
; Select audio/midi flags here according to platform
; Audio out  
-odac      
; For Non-realtime ouput leave only the line below:
; -o nstance2.wav -W ;;; for file output any platform
</CsOptions>
<CsInstruments>

sr = 44100
ksmps = 32
nchnls = 2
0dbfs  = 1

; by Eduardo Moguillansky

; nstance can be used to control the lifetime of a launched event
instr sineoscil
ifreq = p4
  a0 poscil 0.5, ifreq
  aenv linsegr 0, 0.1, 1, 0.1, 0
  a0 *= aenv
  outs a0, a0
endin

; an alarm clock; will trigger at `itime`, only once
opcode alarmclock, k, i
  itime xin
  kt = timeinsts()
  kout = (kt >= itime && kt < itime + ksmps/sr) ? 1 : 0
  xout kout
endop

instr master
  ; event with a unique p1. Although isynth is a itime variable, at itime
  ; it has a value of 0!!. Only at performance time it turns valid

  isynth nstance nstrnum("sineoscil")+0.01, 0, -1, 200

  ; Stock the scheduled event after 2 seconds
  if alarmclock(2) == 1 then
    println "turning off instrument with id %d", isynth
    ; notice that we convert isynth to ksynth to call turnoff at
    ; ktime.
    ksynth = isynth
    turnoff ksynth
  endif
endin
</CsInstruments>
<CsScore>

i "sineoscil" 1 3 1000 ; start at 1 second
i "master" 0 10
e
</CsScore>
</CsoundSynthesizer>
tjingboem commented 2 years ago

it is not an easy example for me. I've tried to put it in a more transparent order (for me) , and moved the comments, where possible, to other places so the total overview of the example becomes more transparent. Also i moved the UDO to the instr 0 space.

I like that in this example the handle of nstance is used, the first example on the manual page does not use the handle.

I've come up with this result, please tell me if you think this covers it all:

<CsoundSynthesizer>
<CsOptions>
; Select audio/midi flags here according to platform
; Audio out  
-odac      
; For Non-realtime ouput leave only the line below:
; -o nstance2.wav -W ;;; for file output any platform
</CsOptions>
<CsInstruments>

sr = 44100
ksmps = 32
nchnls = 2
0dbfs  = 1

; example by Eduardo Moguillansky
; nstance can be used to control the lifetime of a launched event

opcode alarmclock, k, i ; an alarm clock; will trigger at `itime`, only once
  itime xin
  kt = timeinsts()
  kout = (kt >= itime && kt < itime + ksmps/sr) ? 1 : 0
  xout kout
endop

instr sineoscil
ifreq = p4
  a0 poscil 0.3, ifreq
  aenv linsegr 0, 0.1, 1, 0.1, 0
  a0 *= aenv
  outs a0, a0
endin

instr master  ; event with a unique p1.
  ; Although isynth is a itime variable, at itime
  ; it has a value of 0!!. Only at performance time it turns valid

  isynth nstance nstrnum("sineoscil")+0.01, 0, -1, 200    ; p3: negative value is used for 'always on'

  if alarmclock(2) == 1 then    ; Stock the scheduled event after 2 seconds
    println "\nturning off instrument with id %d\n", isynth
    ksynth = isynth             ; notice that we convert isynth to ksynth to call turnoff at ktime
    turnoff ksynth
  endif
endin

</CsInstruments>
<CsScore>

i "sineoscil" 1 2 1000 ; start at 1 second
i "master" 0 10
e
</CsScore>
</CsoundSynthesizer>
csounder commented 2 years ago

this version worked better for me - the other didn't seem to work.

If possible, could you have it turn on and off more notes over time?

- Dr.B

Dr. Richard Boulanger

Professor

Electronic Production and Design

Berklee College of Music

Professional Writing & Technology Division

On Mon, Aug 1, 2022 at 7:29 AM Menno Knevel @.***> wrote:

it is not an easy example for me. I've tried to put it in a more transparent order (for me) , and moved the comments, where possible, to other places so the total overview of the example becomes more transparent. Also i moved the UDO to the instr 0 space.

I like that in this example the handle of nstance is used, the first example on the manual page does not use the handle.

I've come up with this result, please tell me if you think this covers it all:

; Select audio/midi flags here according to platform ; Audio out -odac ; For Non-realtime ouput leave only the line below: ; -o nstance2.wav -W ;;; for file output any platform sr = 44100 ksmps = 32 nchnls = 2 0dbfs = 1 ; example by Eduardo Moguillansky ; nstance can be used to control the lifetime of a launched event opcode alarmclock, k, i ; an alarm clock; will trigger at `itime`, only once itime xin kt = timeinsts() kout = (kt >= itime && kt < itime + ksmps/sr) ? 1 : 0 xout kout endop instr sineoscil ifreq = p4 a0 poscil 0.3, ifreq aenv linsegr 0, 0.1, 1, 0.1, 0 a0 *= aenv outs a0, a0 endin instr master ; event with a unique p1. ; Although isynth is a itime variable, at itime ; it has a value of 0!!. Only at performance time it turns valid isynth nstance nstrnum("sineoscil")+0.01, 0, -1, 200 ; p3: negative value is used for 'always on' if alarmclock(2) == 1 then ; Stock the scheduled event after 2 seconds println "\nturning off instrument with id %d\n", isynth ksynth = isynth ; notice that we convert isynth to ksynth to call turnoff at ktime turnoff ksynth endif endin i "sineoscil" 1 2 1000 ; start at 1 second i "master" 0 10 e

— Reply to this email directly, view it on GitHub https://us-west-2.protection.sophos.com?d=github.com&u=aHR0cHM6Ly9naXRodWIuY29tL2Nzb3VuZC9tYW51YWwvcHVsbC82MjQjaXNzdWVjb21tZW50LTEyMDEwNzY5MTI=&i=NWYxNzBkMDNiNTVmZGEwZmIyNjczYmRm&t=RWtzamZRc3Fid1NzakhNYnh0dS9QQ29vN3prYlBET1dSQStMOFVBVVBCZz0=&h=7619d154c25c4afe8bafdbfed2c6e387, or unsubscribe https://us-west-2.protection.sophos.com?d=github.com&u=aHR0cHM6Ly9naXRodWIuY29tL25vdGlmaWNhdGlvbnMvdW5zdWJzY3JpYmUtYXV0aC9BQUxXWUZTV1FISkZYUEpZWFVaRTNWM1ZXNllLRkFOQ05GU001NENaQkQzUQ==&i=NWYxNzBkMDNiNTVmZGEwZmIyNjczYmRm&t=NjE4OFR5UDZsS3lQczc1NzJSelVPLzhYVDFuM01ENlpuYmw0em9xdGdLRT0=&h=7619d154c25c4afe8bafdbfed2c6e387 . You are receiving this because you are subscribed to this thread.Message ID: @.***>