It seems that your way of generating a wave is a suitable approach for subtractive synthesis. If you want to adjust the PWM of your waveform, you will have to regenerate the samples (or get a pre-calculated waveform stored in memory).
In most cases, you will also need to rebuild the wavetable if the pitch changes. You can read the wave at a higher speed by calculating the increments of the read pointer relative to the fundamental wave, but for this you will need to interpolate between the values ββin the wavetable and may introduce smoothing with more complex waves.
In most cases, of course, it is unlikely that the generated wave will be exactly 2 ^ n samples. Therefore, at the beginning of the processing procedure, copy the samples from the previous wave first before copying the current wave to the output buffer.
You do not want the regeneration process to violate your DSP process procedure, so I would build an updated waveform in a separate memory location and copy it when ready.
The ADSR envelope (subtractive path) should be used as the gain after generating the wave, and not affect the wave.
Hope that helps :)
source share