Joined: Nov 05, 2018
|Posted: Mon Nov 05, 2018 10:28 am Post subject:
Unexpected behavior with Impulse UGen
|Hello! I hope someone can help me. I am new to ChucK and to these forums.
There is something I want to do which would require that I work with the Impulse UGen rather than a standard oscillator. I was experimenting with it to make sure I understand how to use it at a basic level. So I wrote a simple program to generate a sine wave with it, calculating each sample one at a time in an infinite loop using a sine function. This worked as expected. I tried to then modulate the frequency of this sine wave using another sine function and got unexpected results. Here is my code. See what you think.
|//Why does this program unexpectedly generate a tone that oscillates in pitch with ever increasing
//amplitude when it seemingly should vary in pitch consistently at a 1Hz rate with only a 2Hz range
//around 440Hz? Here, pitch should never go above 441Hz and never below 439Hz.
//declare theta variable for use in sine functions, starting it at zero.
0 => theta;
//declare a frequency variable for use in our sine function to set the pitch of the sound wave.
440 => freQ;
//Connect an Impulse UGen to the dac.
Impulse z => dac;
0.5 => z.gain;
while( true )
//The following line is intended to compute a frequency value for use in main sine function.
//This should generate an oscillating frequency value centered on 440 and varying between
//439 and 441, since a sine function only outputs a value between -1 and 1.
440 + Math.sin(theta) => freQ;
//Now we use another sine function to set the value of our Impulse UGen. This generates a
//simple sine tone. If, instead of 'freQ', we use 440, we will get a standard 440Hz sine wave.
Math.sin(freQ * theta) => z.next;
//Advance time one sample each loop cycle.
samp => now;
//For each loop cycle, increase the value of theta for use in our sine functions by an
//increment of 1 second divided by sample rate of 44100 and multiplied by 2*pi. This is
//so that our freQ value will correspond to standard wave cycles per second.
theta + 0.000142475857 => theta;
//I can't seem to figure out why this program generates the behavior that it does. Please help!!