Why does this code produce silence?

I’ve written this and I cannot for the life of me figure out why it creates an empty WAV file. Please help.

Thanks!

<CsoundSynthesizer>
<CsOptions>
;-odac ; Disable real-time audio output
-o output.wav ; Output to a WAV file
</CsOptions>
<CsInstruments>
sr = 44100 ; Sample rate
ksmps = 32 ; Control rate
nchnls = 2 ; Number of output channels

instr 1
  ; Set up global variables
  kEnv madsr 0.2, 0.2, 0.4, 0.6
  aLFO oscil 1, 0.2, 1
  aPanLFO oscil 0.5, 0.1, 1
  aPanLFO = aPanLFO * 2 - 1

  ; Create the main sound using a resonant lowpass filter
  aSound vco2 p5, 80
  aSound butterlp aSound, 800
  aSound = aSound * kEnv

  ; Apply panning
  aLeft = aSound * (1 - aPanLFO)
  aRight = aSound * aPanLFO

  ; Output the sound
  outs aLeft, aRight

  ; Schedule the next note
  if p3 + p4 < 180 then
    schedule 1, 0, p3 + p4, p4, p5 * aLFO
  endif
endin
</CsInstruments>
<CsScore>
; Define the function table
f1 0 16384 10 1

; Play the piece
i 1 0 180 0.2 cpsmidinn(60) ; Instrument 1, start time 0, duration 180s, amplitude 0.2, frequency 60Hz
</CsScore>
</CsoundSynthesizer>

Oh :smiley:
I gave you an answer on the Cabbage forum already,
but try adding this line:

0dbfs = 1

Thank you so much!

I also left you a note in the Cabbage forum. You’re using cpsmidinn(60) p5 as amplitude instead of frequency.

However it should be:
aSound vco2 amplitude, frequency

so:
aSound vco2 p4, cpsmidinn(p5)

and in the score use 60 where you have written cpsmidinn(60).

I put a better explanation there and an example.

Hi,

Thank you so much for your help with this. I’m getting sound now. Unfortunately, I’m only getting one oscillating pitch. It seems that my “schedule” opcode isn’t doing anything. I think it’s because p5 isn’t changing? All “p5 * aLFO”does is periodically modulate the filter cutoff. I tried:
; Generate a random pitch in the key of C major
kPitch randi 1, 7, int(p4) kPitch = cpsmidinn(kPitch + 48) ; Offset by 48 to get C major key

That didn’t do anything. It just threw errors.

Thanks!

Jair-Rôhm

You’re on the right track but possibly missing a few things.

First, a major scale has semi-tone and tone intervals. Adding a random value of an int between 0 - 7 will create only 7 semi-tones, including sharps and flats.

To stay in C major you need to add specific intervals to midi note 60. For ex. adding 2 semi-tones to cpsmidinn(60) would give you D (midi note 62), adding 5 would be an F note. The easiest way to do that is likely with an ftable (or array). Another way to do this would be to store the midi note numbers in an ftable.

It can be tricky to schedule these type of events within a single instr. as when the instr is called in the score and also called with a schedule things like envelopes and variables can get confused - for example the initial score event will hold the envelope for p3 and also for each schedule event.

It may be easier to instead use one instr to trigger (schedule) another. Here is an example. One trig instr adds the values of ftable2, the second trig instr uses midi notes directly instead of adding them to p5 (60).

Also, in your schedule, p5 is not modulating filter cutoff as p5 is never applied to the filter.

If you have any questions feel free to ask.

Scott

<CsoundSynthesizer>
<CsOptions>
;-odac ; Disable real-time audio output
 -o output.wav ; Output to a WAV file
</CsOptions>
<CsInstruments>
sr = 44100 ; Sample rate
ksmps = 32 ; Control rate
nchnls = 2 ; Number of output channels
0dbfs  = 1

instr trigger
  kTrig  metro 1 ; trigger instr 1 once per second
  kRand  = int(trandom(kTrig, 0, 8)) ; create random number every time metro creates a trigger
  kPitch table kRand, 1 ; randomly choose index from ftable 1 to create a new note
  schedkwhen kTrig, 0, 0, 1, 0, .4, p4, p5 + kPitch
endin

instr trigger2
  kTrig  metro 1 ; trigger instr 1 once per second
  kRand  = int(trandom(kTrig, 0, 8)) ; create random pitch 
  kPitch table kRand, 2
  schedkwhen kTrig, 0, 0, 1, 0, .4, p4, kPitch
endin

instr 1
  ; Create the main sound using a resonant lowpass filter
  aEnv   madsr 0.2, 0.2, 0.4, 0.6
  aSound vco2 p4, cpsmidinn(p5)
  aSound butterlp aSound, 800
  aSound = aSound * aEnv

  ; Apply panning
  iPan   random 0, 1
  aLeft  = aSound * (1 - iPan)
  aRight = aSound * iPan

  ; Output the sound
  outs aLeft, aRight
endin

</CsInstruments>
<CsScore>
; Define the function table
f1 0 8 -2 0 2 4 5 7 9 11 12 ; notes to add to midinn 60
f2 0 8 -2 60 62 64 65 67 69 71 72 ; midi notes (one octave of C major) 
; Play the piece
i "trigger"   0 16 0.2 60 ; Instrument 1, start time 0, duration 180s, amplitude 0.2, frequency 60Hz
i "trigger2" 17 16 0.2
</CsScore>
</CsoundSynthesizer>
1 Like