Karlo
Karlo

Reputation: 1708

How to include durations in an instrument definition in Csound

Using the function oscil, I define an oscillator bank with given frequencies and amplitudes:

instr 1
    a1 oscil .3, 110
        outs a1,a1
    a2 oscil .2, 220
        outs a2,a2
    a3 oscil .1, 330
        outs a3,a3
endin

I know that I can set the duration in the orchestra section. But how can I give different durations to the different oscillations? Can I do this in the instrument definition? Because I want to be able to call the instrument (3 oscillators) with one line in the orchestra:

;instr start duration
i 1 0 ;duration of oscils defined under instr 1
e

Upvotes: 0

Views: 95

Answers (2)

Luis Vieira Damiani
Luis Vieira Damiani

Reputation: 319

Something that comes to mind is to apply different envelopes to each sinusoid you create inside an instrument:

0dbfs = 1

instr 1
kFirstEnvelope line 0, p3, 1
kSecondEnvelope line 0.5, p3, 0.5
kThirdEnvelope line 1, p3, 0
aFirstSine oscili 1, 440
aSecondSine oscili 1, 660
aThirdSine oscili 1, 880
aMix balance aFirstSine * kFirstEnvelope + aSecondSine * kSecondEnvelope + aThirdSine * kThirdEnvelope, a(0.15)
outs aMix, aMix
endin

You could then call instr 1 from the score with a single line of code, and you would probably want to come up with more interesting envelopes than the ones above.

i 1 0 10

However, if you are doing additive synthesis, a more elegant approach would be to trigger multiple score events from a separate instrument using event_i within a until loop.

instr 2
seed 0
iNoteIndex = 0
iNoteCount = 30
until iNoteIndex == iNoteCount do
iRandomStart = random(0, p3)
iRandomDuration = random(1.2, 0.5 * p3)
event_i "i", 3, iRandomStart, iRandomDuration
iNoteIndex += 1
enduntil
endin

instr 3
iAttack = .2
iDecay = .2
iSustain = .4
iRelease = 0.6
aSineWave oscili 0.1, random(200, 4000)
kEnvelope adsr iAttack, iDecay, iSustain, iRelease
outs aSineWave * kEnvelope
endin

You can then call instr 2 from the score, and that will take care of calling instr 3.

i 2 0 10

Cheers

Upvotes: 1

Steven Yi
Steven Yi

Reputation: 166

Opcode instances within an instrument instance all share the same processing context (i.e., p3/duration). There are a few different strategies one could use to get different durations here:

  1. Use multiple instrument instances have one oscillator per instrument. This is probably the most flexible, but most verbose.
  2. Use some form of envelope and multiply that with the output of each oscillator. For example:

    instr 1
      p3 = 4
      a1 oscil .3, 110
      aenv1 linseg 1, 3, 1, 0.01, 0, 0.99, 0 
      a1 *= aenv1
      outs a1,a1
      ...
    endin
    

In #2, the duration is set by the instrument. The linseg is used as an envelope and the durations written in. One could then use multiple linseg/oscil pairs and hand write the durations for each part in.

Upvotes: 2

Related Questions