Custom pulse wave with dynamic width

Hi friends,

I am trying to emulate certain behaviour of the Game Boy sound processor.

I need to have a pulse wave that changes its width every cycle, but I can’t seem to make it work. Here’s what I have so far:

	var duty, freq, period;

	freq = 440;
	period = 1/freq;
	// Duty cycle to be iterated every period.
	duty =, 0, Dseq([ 0.12, 0.25, 0.50, 0.75 ], inf));
	// Now I create from scratch my pulse wave with DemandEnvGen using the duty values in the duration.
		level: Dseq([ 0, 0, 1, 1 ], inf), 
		dur: Dseq([ period*duty, 0, period*(1-duty), 0 ], inf)
}.plot(8/440, minval: -1.5, maxval: 1.5)

As you can see in the plot, it does create a pulse wave, but it doesn’t seem to iterate correctly through the duty values (at all). Plus, it sounds pretty bad if you play it.

Am I doing something wrong? Perhaps 1/freq is just too fast? Can you help me? :upside_down_face::hot_face:

Thanks a lot!


This simpler approach may be more reliable than trying to coordinate Duty and DemandEnvGen:

	var freq = 440;
	var dur = freq.reciprocal;
	var width = Dwhite(0.1, 0.5, inf);
	var widthx2 = Dstutter(2, width);[widthx2, 1 - widthx2] * dur, inf), 0, Dseq([1, -1], inf));


Thank you @jamshark70. :rocket: