MSynth.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #ifndef MSYNTH
  2. #define MSYNTH
  3. /* ********************************
  4. * MSynth
  5. * ********************************
  6. *
  7. * MSynth is a modular-synth library for Arduino.
  8. *
  9. * Author:
  10. * Akshay Cadambi
  11. * Graduate student in Media Arts and Technology
  12. * UC Santa Barbara
  13. *
  14. * */
  15. #include "TimerOne.h"
  16. #include "ugens.h"
  17. #include "wiring_private.h"
  18. #define AUDIO_OUTPUT_REGISTER OCR1A
  19. using namespace ugen;
  20. // This function will defined by the user in the Ardunino Sketch
  21. sample audioLoop(void);
  22. inline void audioISR(void)
  23. {
  24. /*
  25. * Writes audio to port.
  26. * This is the default audio-rate ISR and is a 'hidden' alias for audioLoop()
  27. * Set within MSynthMaster.
  28. *
  29. * TODO: Add a global DC_OFFSET for easier amplitude modulation
  30. * */
  31. AUDIO_OUTPUT_REGISTER = audioLoop();
  32. }
  33. class MSynthMaster
  34. {
  35. /*
  36. * Used to automate the ISR and provide an interface between the sketch
  37. * and the backend.
  38. * */
  39. public:
  40. MSynthMaster(void)
  41. {
  42. _ISRfrequency = SystemSR;
  43. }
  44. MSynthMaster(long int SR)
  45. {
  46. // TODO: Find a way to integrate this into everything
  47. // This doesn't really affect the oscillators yet.
  48. _ISRfrequency = SR;
  49. }
  50. void init(void)
  51. {
  52. long int ISRPeriod_inMicroseconds = 1000000 / (double) _ISRfrequency ;
  53. pinMode(9, OUTPUT);
  54. Timer1.initialize( ISRPeriod_inMicroseconds );
  55. Timer1.attachInterrupt(audioISR);
  56. sbi(TCCR1A, COM1A1); // Associates Pin 9 with the Timer1 Output
  57. }
  58. long int _ISRfrequency; // Audio Rate
  59. } MSynth;
  60. #endif // MSYNTH