@GVisser
After some experiment on my diy-dev board for audio experiments, here is my conclusions:
- Depending on what device you are connected, distortions are present when you are plugged in some equipment with AUDIO-IN. Most probably because it expect 1Vp-p, and you are feeding it with 3.3Vp-p.
Here is recommended audio setup:
Code: Select all
// .-------------------------------------.
// | |
// | STM32FxxxXXxx |
// .---------------------------------|---.
// |G P|
// |N B|
// |D 0-----|R1|------|C2|----------|
// | | --
// | C || P1
// | 1 ||<----------------| OUDIO OUT
// | | --
// .--------------------------------------------|-----------------|------------------| GND
// R1 = 100-500 Ohm
// C1 = 100-500 nF
// C2 = 10 uF
// P1 = 10KOhm potentiometer
Also, distortions may come from a way how author decided to create sound. Base PWM frequency of 31250Hz is, IMHO, just too low for 1-bit DAC.
maybe it's ok for 1 channel, but for four channels (used in sketch), it is noticeably full of "eeeeeks"

Maybe try with higher capacitor in low-pass filter (500nF+), or higher resistor (500 Ohm+).
In my audio project, i used 250KHz base PWM frequency, that's 4uS period, meaning it is 4*72= 288 (bluepill) or 4*84=336 (STM32F401CC) values for width of that pulse. Enough for 3 channels (each channel is 1/3 of maximum of pulse width).
I think author just ported code for STM32, with all the limitation original code have (for arduino uno).
Also, i don't think it doesn't matter is it PWM1 or PWM2 mode. Beside visually in analyzer. Sound is difference in audio signal, it sound the same when you are altering between 99% HIGH - 1% LOW and 100% HIGH - 0% LOW, and 1% HIGH - 99% LOW and 100% LOW - 0% HIGH. It's the difference of 1% that is creating analog AC voltage.
Personally, i'm more into digital electronics then into analog. With digital, i'm dealing only with ones and zeroes
Anyway,
will set correct prescaler and overflow for you, no matter on what speed CPU is (and that's why i like this core: same code, no matter is it STM32F103C or STM32F401CC).