Cabbage Logo
Back to Cabbage Site

Why does this code create silence?

Hi,

I’ve written this and I cannot figure out for the life of me why it’s outputting an empty WAV file. What am I doing wrong?

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

Hi @Jair-Rohm_Parker_Wel and welcome to the forum!
I haven’t tested your code but I think you miss this line:

0dbfs = 1

Otherwise your samples are scaled in the range 0-32768 and you won’t hear basically anything.

Btw to better format the code edit your post, select all the code and click this:

image

Cheers!

1 Like

I think the main problem is that you can’t use cpsmidinn in the score - this is an orchestra-only opcode. Because of this, p5 is received by instr 1 as zero. When I change this to a number like 440, I get sound with your code!

Thank you so much, Ian!

With all due respect, I think both previous answers were somewhat correct however:
it would appear what you want is for the frequency to be cpsmidinn(60) as you indicate in the score. Note also that cpsmidinn(60) would be middle C which is 261.63 Hz, not 60 Hz as you indicate in the score comment. And that you would like the amplitude to be p4 which you set as 0.2.

However, with vco2 the first parameter is amplitude, the second is frequency:
aSound vco2 amplitude, frequency

It would appear you’re trying to place the frequency where the amplitude should be and vice versa. So you should use:
aSound vco2 p4, cpsmidinn(p5)

And also include 0dbfs = 1 as Giovanni suggested.

<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 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 p4, cpsmidinn(60)
  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 60 ; Instrument 1, start time 0, duration 180s, amplitude 0.2, frequency 60Hz
</CsScore>
</CsoundSynthesizer>

I am slightly confused about the intent of your schedule line.

Thank you very much for this answer. The code does produce sound now. The point of “schedule” was to create a series of pitches.