Insect in AudioSynthWaveform when WAVEFORM_PULSE selected -> wrong frequency.

neurofun

Well-known member
When selecting WAVEFORM_PULSE in an AudioSynthWaveform object, output frequency is half of what it should be.

error in line 144 of /Audio/synth_waveform.cpp
Code:
        tone_phase += tone_incr;

fix:
Code:
        tone_phase += 2*tone_incr;

The next piece of code demonstrates the bug and that the fix is working when applied.
It cycles rapildy trough 5 waveforms playing at a fixed frequency.
You can clearly hear the WAVEFORM_PULSE drop an octave.
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 = 200;

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

  waveform1.begin(1.0, 440.0, WAVEFORM_SINE);
  waveform1.pulseWidth(1.0);
}

void loop() {
  waveform1.begin(1.0, 440.0, WAVEFORM_SINE);
  delay(ms);
  waveform1.begin(1.0, 440.0, WAVEFORM_SAWTOOTH);
  delay(ms);
  waveform1.begin(1.0, 440.0, WAVEFORM_SQUARE);
  delay(ms);
  waveform1.begin(1.0, 440.0, WAVEFORM_TRIANGLE);
  delay(ms);
  waveform1.begin(1.0, 440.0, WAVEFORM_PULSE);
  delay(ms);
}



There is also an initialisation error in the constructor of AudioSynthWaveform pertaining WAVEFORM_PULSE.
"tone_width" of type uint_32t is initialised with 0.25, a float which sets it to zero = 100% duty cycle = DC = no sound.
comment last line of setup() in test program to see or hear the effect.
I propose to initialise the pulsewidth at 50% duty cycle, which give use a square wave.

error in line 44 of /Audio/synth_waveform.h
Code:
  tone_phase(0), tone_width(0.25), tone_incr(0), tone_type(0),

fix:
Code:
  tone_phase(0), tone_width(0x7fffffffLL), tone_incr(0), tone_type(0),
 
Please ignore and close this thread, working on a fix with a different approach to resolve frequency and phase inconsistencies.
 
Back
Top