PaulStoffregen / Audio

Teensy Audio Library
http://www.pjrc.com/teensy/td_libs_Audio.html
1.1k stars 404 forks source link

Band limited waveforms #360

Closed MarkTillotson closed 3 years ago

MarkTillotson commented 4 years ago

This version of band limited square and sawtooth waveforms is integrated into AudioSynthWaveform and AudioSynthWaveformModulated.

The new tone types WAVEFORM_BANDLIMIT_SQUARE, WAVEFORM_BANDLIMIT_SAWTOOTH and WAVEFORM_BANDLIMIT_SAWTOOTH_REVERSE are used so side-by-side comparisons with originals can be done.

PaulStoffregen commented 4 years ago

Is this ready to merge?

zirafa commented 3 years ago

Hello, I tested this PR on a Teensy 4.1. I can confirm that the bandlimited waveforms sound much better, thanks for this. The aliasing artifacts are quite audible when modulating non-bandlimited waveforms, but the artifacts disappear when modulating bandlimited waveforms.

Here is a test recording I made for comparison:

- (SINGLE NOTE) reverse sawtooth vs. bandlimited reverse sawtooth
- (SINGLE NOTE) sawtooth vs. reverse sawtooth
- (SINGLE NOTE) squarewave vs. bandlimited squarewave

- (SINGLE NOTE + modulation) reverse sawtooth vs. bandlimited reverse sawtooth
- (SINGLE NOTE + modulation) sawtooth vs. bandlimited sawtooth
- (SINGLE NOTE + modulation) squarewave vs. bandlimited squarewave

- (TWO NOTES + modulation) reverse sawtooth vs. bandlimited reverse sawtooth
- (TWO NOTES + modulation) sawtooth vs. bandlimited sawtooth
- (TWO NOTES + modulation) squarewave vs. bandlimited squarewave

Spectral plot here


Note:

  1. The same test using sine and triangle waveforms don't reveal any artifacts when modulating them, so I'm guessing they are already bandlimited in some way. Does that sound right?
  2. My Teensy would occasionally get into an unstable state when switching between the different waveforms during playback. However, the problem is intermittent so maybe it is just a problem with my code. I'll keep poking at it and see if I can reproduce it.
rvh23 commented 3 years ago

Until I saw this I was about to try and write my own bandlimited oscillator code, which would have been a bit of a task because I'm new to both the Teensy and its Audio library (but not DSP). But I can't find a version of the Audio library that includes these new waveforms. Where should I be looking? Thanks very much, Richard.

zirafa commented 3 years ago

@rvh23 You can grab MarkTillotson's branch referenced in this PR. Replace the original Teensyduino audio library with this one and then you should be able to use the bandlimited waveforms.

rvh23 commented 3 years ago

@rvh23 You can grab MarkTillotson's branch referenced in this PR. Replace the original Teensyduino audio library with this one and then you should be able to use the bandlimited waveforms.

Thanks very much. The new bandlimited synthesis seems to work very nicely with a very large improvement over the non-band-limited methods in terms of suppressing aliasing.

rvh23 commented 3 years ago
1. The same test using sine and triangle waveforms don't reveal any artifacts when modulating them, so I'm guessing they are already bandlimited in some way. Does that sound right?

Triangle harmonics fall off much faster than Saw or Square wavs, so the aliasing introduced by simple non-bandlimited synthesis is less obvious, but you might still hear a little at higher frequncies. With sinusoids, in theory there are no higher harmonics, except that the Audio library uses linear interpolation that may produce minor distortion and conseqeunt aliasing, but iin practice it's likely to be of minimal concern.

I also just looked a the new library code, and it doesn't include a BW-limited version of the triangle wav anyway, presumably for the reason I state above.

PaulStoffregen commented 3 years ago

Merging this in prep for 1.54-beta6....

krenzlin commented 3 years ago

May I ask how the band limited waveforms are generated? My guess is - just skimming through the code - that it does not use the common PolyBLEP or PTR and instead uses a lookup for the transition and somehow scales it to need.

I am just curious what are the advantages of this method.

Also this feature request could be closed with this PR.