Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gesellkammer
Copy link
Contributor

No description provided.

Copy link
Member

@tjingboem tjingboem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it is more clear when 2 separate examples are used on this manual page. Combining it to 1 long example makes a lot less clear as to what is doing what.
I'd like to propose the following example- it will be a separate example number 2.
I changed a few things.
Can you check please if all works as expected?

<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

instr turnoff
  iid = p4
  prints "\nturning off id %d\n", iid
  turnoff(iid)
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
  
  ; At the very first cycle schedule a turnoff using the handle number,
  ; not the fractional p1. Notice that this operation needs to be done
  ; at performance time to have a valid handle
  if timeinstk() == 1 then
    ksynth = isynth
    schedulek "turnoff", 3, 0, ksynth
  endif
endin
</CsInstruments>
<CsScore>

i "sineoscil" 1 1 1000    ; start at 1 second
i "master" 0 10
e
</CsScore>
</CsoundSynthesizer>

@gesellkammer
Copy link
Contributor Author

gesellkammer commented Jul 31, 2022

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
Copy link
Member

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
Copy link

csounder commented Aug 1, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants