Guest User

mozzi fm synth

a guest
Feb 14th, 2025
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 4.76 KB | Source Code | 0 0
  1. /* Jack's FM Synth
  2.  
  3.   based on the Knob_Lightlevel_FMSynth_x2 example from the mozzi examples
  4.   -adding two new effects
  5.   -a volume knob
  6.   -a screen that reads the carrier frequency and the modulation rate
  7. */
  8.  
  9.  
  10. #define MOZZI_ANALOG_READ_RESOLUTION 10 // code below assumes readings to be in the classic 10-bit (0-1023) range
  11. #include <Mozzi.h>
  12. #include <Oscil.h> // oscillator
  13. #include <tables/cos2048_int8.h> // table for Oscils to play
  14. #include <tables/cos512_int8.h> // for filter
  15. #include <Smooth.h>
  16. #include <AutoMap.h> // maps unpredictable inputs to a range
  17. #include <ResonantFilter.h> //resonsnant filter for fx 1
  18.  
  19.  
  20. // desired carrier frequency max and min, for AutoMap
  21. const int MIN_CARRIER_FREQ = 22;
  22. const int MAX_CARRIER_FREQ = 440;
  23.  
  24. // desired intensity max and min, for AutoMap, note they're inverted for reverse dynamics
  25. const int MIN_INTENSITY = 700;
  26. const int MAX_INTENSITY = 10;
  27.  
  28. // desired mod speed max and min, for AutoMap, note they're inverted for reverse dynamics
  29. const int MIN_MOD_SPEED = 10000;
  30. const int MAX_MOD_SPEED = 1;
  31.  
  32. // max and min filter cutoff
  33. const int MIN_CUTOFF_FREQ = 50;
  34. const int MAX_CUTOFF_FREQ = 6000;
  35.  
  36. AutoMap kMapCarrierFreq(0,1023,MIN_CARRIER_FREQ,MAX_CARRIER_FREQ);
  37. AutoMap kMapIntensity(0,1023,MIN_INTENSITY,MAX_INTENSITY);
  38. AutoMap kMapModSpeed(0,1023,MIN_MOD_SPEED,MAX_MOD_SPEED);
  39. AutoMap kMapCutoffFreq(0,1023,MIN_CUTOFF_FREQ,MAX_CUTOFF_FREQ);
  40.  
  41. const int KNOB_PIN = 0; // set the input for the knob to analog pin 0
  42. const int LDR1_PIN=1; // set the analog input for fm_intensity to pin 1
  43. const int LDR2_PIN=2; // set the analog input for mod rate to pin 2
  44. const int FILTER_PIN = 3; //pin for filter knob
  45.  
  46. Oscil<COS2048_NUM_CELLS, MOZZI_AUDIO_RATE> aCarrier(COS2048_DATA);
  47. Oscil<COS2048_NUM_CELLS, MOZZI_AUDIO_RATE> aModulator(COS2048_DATA);
  48. Oscil<COS2048_NUM_CELLS, MOZZI_CONTROL_RATE> kIntensityMod(COS2048_DATA);
  49. Oscil<COS512_NUM_CELLS, MOZZI_CONTROL_RATE> kLowPass(COS512_DATA);
  50.  
  51. int mod_ratio = 5; // brightness (harmonics)
  52. long fm_intensity; // carries control info from updateControl to updateAudio
  53.  
  54. // smoothing for intensity to remove clicks on transitions
  55. float smoothness = 0.95f;
  56. Smooth <long> aSmoothIntensity(smoothness);
  57.  
  58. LowPassFilter lpf; //low pass filter object
  59.  
  60. uint8_t resonance = 200; //LPF resonance value
  61.  
  62. void setup(){
  63.   //Serial.begin(9600); // for Teensy 3.1, beware printout can cause glitches
  64.   Serial.begin(115200); // set up the Serial output so we can look at the piezo values // set up the Serial output so we can look at the light level
  65.   startMozzi(); // :))
  66. }
  67.  
  68.  
  69. void updateControl(){
  70.   // read the knob
  71.   int knob_value = mozziAnalogRead(KNOB_PIN); // value is 0-1023
  72.  
  73.   // map the knob to carrier frequency
  74.   int carrier_freq = kMapCarrierFreq(knob_value);
  75.  
  76.   //calculate the modulation frequency to stay in ratio
  77.   int mod_freq = carrier_freq * mod_ratio;
  78.  
  79.   //set the filter freqency
  80.   int filter_knob_val = mozziAnalogRead(FILTER_PIN);
  81.   int cutoff_freq = kMapCutoffFreq(filter_knob_val);
  82.  
  83.   // set the FM oscillator frequencies
  84.   aCarrier.setFreq(carrier_freq);
  85.   aModulator.setFreq(mod_freq);
  86.  
  87.   // read the light dependent resistor on the width Analog input pin
  88.   int LDR1_value= mozziAnalogRead(LDR1_PIN); // value is 0-1023
  89.   // print the value to the Serial monitor for debugging
  90.   Serial.print("LDR1 = ");
  91.   Serial.print(LDR1_value);
  92.   Serial.print("\t"); // prints a tab
  93.  
  94.   int LDR1_calibrated = kMapIntensity(LDR1_value);
  95.   Serial.print("LDR1_calibrated = ");
  96.   Serial.print(LDR1_calibrated);
  97.   Serial.print("\t"); // prints a tab
  98.  
  99.  // calculate the fm_intensity
  100.   fm_intensity = ((long)LDR1_calibrated * (kIntensityMod.next()+128))>>8; // shift back to range after 8 bit multiply
  101.   Serial.print("fm_intensity = ");
  102.   Serial.print(fm_intensity);
  103.   Serial.print("\t"); // prints a tab
  104.  
  105.   // read the light dependent resistor on the speed Analog input pin
  106.   int LDR2_value= mozziAnalogRead(LDR2_PIN); // value is 0-1023
  107.   Serial.print("LDR2 = ");
  108.   Serial.print(LDR2_value);
  109.   Serial.print("\t"); // prints a tab
  110.  
  111.   // use a float here for low frequencies
  112.   float mod_speed = (float)kMapModSpeed(LDR2_value)/1000;
  113.   Serial.print("   mod_speed = ");
  114.   Serial.print(mod_speed);
  115.   kIntensityMod.setFreq(mod_speed);
  116.   Serial.print("\t"); // prints a tab
  117.  
  118.   // set the filter cutoff frequency and resonance
  119.   lpf.setCutoffFreqAndResonance(cutoff_freq, resonance);
  120.  
  121.   Serial.print("  cutoff freq = ");
  122.   Serial.print(cutoff_freq);
  123.  
  124.   Serial.println(); // finally, print a carraige return for the next line of debugging info
  125. }
  126.  
  127.  
  128. AudioOutput updateAudio(){
  129.   long modulation = aSmoothIntensity.next(fm_intensity) * aModulator.next();
  130.  
  131.   //return wave;
  132.   return MonoOutput::from8Bit(aCarrier.phMod(modulation));
  133.  
  134. }
  135.  
  136.  
  137. void loop(){
  138.   audioHook();
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment