Proposition to change pulseWidth() behaviour in AudioSynthWaveform object.

neurofun

Well-known member
current behaviour of pulseWidth(float n):

n = 0.0 ... 1.0 -> duty cycle = 100% ... 50%

proposed behaviour of pulseWidth(float n):

n = 0.0 ... 1.0 -> duty cycle = 0% ... 100%

I find the current behaviour counter intuitive and limiting. One might argue that you don't hear the difference between 25% an 75% duty cycle and that is true if the AudioSynthWaveform object is used as an audio rate oscillator(generating sounds).
But other applications need 0% -> 100% duty cycle range.
For example if used as a function generator.
Another example would be where WAVEFORM_PULSE is used as a clock generator and is fed into an object that does falling edge detection, modulating the pulseWith would amount to modulating the phase of the clock signal.

If I understand your explanation from another thread, changing the behaviour of pulseWidth(float n) would be an API change, which is not desirable.
So I propose to add an new member, void dutyCycle(float n);

current pulseWidth(float n) code in /Audio/synth_waveform.h:
Code:
  void pulseWidth(float n) {          // 0.0 to 1.0
    if (n < 0) n = 0;
    else if (n > 1.0) n = 1.0;
    tone_width = n * 0x7fffffffLL; 
    // pulse width is stored as the equivalent phase
  }

new proposed dutyCycle(float n) code
Code:
  void dutyCycle(float n) {          // 0.0 to 1.0
    if (n < 0) n = 0;
    else if (n > 1.0) n = 1.0;
    tone_width = (1 - n) * 0xffffffffLL;  //dutycycle 0% -> 100% instead of 100% -> 50% 
    // pulse width is stored as the equivalent phase
  }

below is some code to test the functionality of pulseWitdth().
this is just a pulseWidth sweep from 0.0 to 1.0 over a period of 2 sec, repeated ad infinitum.
Code:
#include <Audio.h>

// GUItool: begin automatically generated code
AudioSynthWaveform       waveform1;      //xy=270,263
AudioOutputI2S           i2s1;           //xy=455,257
AudioOutputAnalogStereo  dacs1;          //xy=456,298
AudioConnection          patchCord1(waveform1, 0, i2s1, 0);
AudioConnection          patchCord2(waveform1, 0, i2s1, 1);
AudioConnection          patchCord3(waveform1, 0, dacs1, 0);
AudioConnection          patchCord4(waveform1, 0, dacs1, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=104,305
// GUItool: end automatically generated code

int ms = 2;

void setup() {
  AudioMemory(20);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.5);

  waveform1.begin(1.0, 440.0, WAVEFORM_PULSE);
}

void loop() {
  for(float i=0; i<1.0; i += 0.001){
    waveform1.pulseWidth(i);
    // waveform1.dutyCycle(i);
    delay(ms);
  }
}
 
Please ignore and close this thread, I think the above assessment of pulseWidth() behaviour is wrong.
At the time I did the measurements, I was not aware the line outputs of the audio adapter board are inverted compared to the dac outputs :(
 
Last edited:
Back
Top