I thought I would post this in case anyone else needed help in making something like this work. A little back story first.
I put together an eight-pot Pro Micro (atmega32u4) synth to play with the Mozzi library. By default Mozzi outputs pwm audio on pin 9. So that is the way I built it. Mozzi is very powerful and at times (for us synth noobs) rather complex. So I went looking for an easier synth type. There are a number of variations of a granular synth based off on Peter Knight's code. So I thought I'd try that... but. It does not work on the atmega32u4, only the 328, like the Nano or Uno. The problem is the 32u4 does not have a Timer/Counter2, where the PWM is generated. Also the 328 has Timer/Counter2 tied to pin 3.
It turns out that the 32u4 has a Timer/Counter4 that can be tied to pin 9, so this is the results of my poking and reading and smacking the side of my head. But it does work. I now have a synth board that I can use with Mozzi and the different granular synth examples.
You can add the defines and code inside of a "if defined(AVR_ATmega32U4) ...... #else" or else just replace the setups for ATMega328 and ATmega1280.
Setup for PWM port and output pin
#elif defined(__AVR_ATmega32U4__)
//
// On the Arduino Leonardo/Pro Micro
// Output is on pin 9
//
// status LED defines
#define LED_PIN 17
#define LED_PORT PORTD
#define LED_BIT 5
// PWM defines
#define PWM_PIN 9
#define PWM_VALUE OCR4B
#define PWM_INTERRUPT TIMER4_OVF_vect
#else
Turn on PWM audio
// included in code sketch for ATMega32u4
TCCR4A = _BV(COM4B0) | _BV(PWM4B);
TCCR4B = _BV(CS40);
TCCR4C = _BV(COM4B0S);
TCCR4D = _BV(WGM40);
TIMSK4 = _BV(TOIE4);
Full 32u4 Timer/Counter4 register setup, in case I missed something above
Timer/Counter4 setup for granular output pin 9 in ATMega32u4 (Leonardo/Pro Micro)
// TCCR4A=(0<<COM4A1) | (0<<COM4A0) | (0<<COM4B1) | (1<<COM4B0) | (0<<FOC4A) | (0<<FOC4B) | (0<<PWM4A) | (1<<PWM4B);
// COM4B0 set = In Phase and Freq Correct PWM Mode on #OC4B (pin 9). Cleared on match when up-counting. Set when down-counting.
// PWM4B set = Enables PWM mode based on comparator OCR4B
// TCCR4B=(0<<PWM4X) | (0<<PSR4) | (0<<DTPS41) | (0<<DTPS40) | (0<<CS43) | (0<<CS42) | (0<<CS41) | (1<<CS40);
// CS40 set = Timer/Clock4 source to PCK/CK which is no prescaling
// TCCR4C=(0<<COM4A1S) | (0<<COM4A0S) | (0<<COM4B1S) | (1<<COM4B0S) | (0<<COM4D1) | (0<<COM4D0) | (0<<FOC4D) | (0<<PWM4D);
// COM4B0S set = Weird "shadow bit" can change TCCR4A(COM4B0), set it just to be sure
// TCCR4D=(0<<FPIE4) | (0<<FPEN4) | (0<<FPNC4) | (0<<FPES4) | (0<<FPAC4) | (0<<FPF4) | (0<<WGM41) | (1<<WGM40);
// WGM40 set = Phase and Frequency Correct PWM
// TIMSK4=(0<<OCIE4D) | (0<<OCIE4A) | (0<<OCIE4B) | (1<<TOIE4);
// TOIE4 set = Enable interrupt on Timer/Counter4 Overflow
Hope it maybe of some help.
dsfraser