Oscillator.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #pragma once
  2. namespace Audio
  3. {
  4. const float SAMPLE_RATIO = 44100.0f;
  5. const float PI_A = 3.14159265359f;
  6. const size_t BUFFER_SIZE = 1024;
  7. enum Shape
  8. {
  9. SINE
  10. };
  11. class Oscillator
  12. {
  13. protected:
  14. float m_phase;
  15. float m_phase_stride;
  16. float m_frequency;
  17. Shape m_shape;
  18. float sample_duration;
  19. float buffer[BUFFER_SIZE] = {0};
  20. public:
  21. Oscillator() = default;
  22. float frequency() { return m_frequency; }
  23. Shape shape() { return m_shape; }
  24. /* float phase() { return m_phase; }
  25. float phase_stide() { return m_phase_stride; }
  26. void phase(float phase) { m_phase = phase; }
  27. void phase_stride(float phase_stride) { m_phase_stride = phase_stride; }
  28. */
  29. void* data() { return (void *)buffer; }
  30. virtual void update() {} ;
  31. };
  32. class SquareOscillator: public Oscillator
  33. {
  34. public:
  35. SquareOscillator(float frequency) {
  36. sample_duration = 1 / SAMPLE_RATIO;
  37. m_frequency = frequency;
  38. m_phase = 0;
  39. m_phase_stride = frequency * sample_duration;
  40. };
  41. void update() override;
  42. };
  43. class SineOscillator: public Oscillator {
  44. public:
  45. SineOscillator(float frequency) {
  46. sample_duration = 1 / SAMPLE_RATIO;
  47. m_frequency = frequency;
  48. m_phase = 0;
  49. m_phase_stride = frequency * sample_duration;
  50. }
  51. void update() override;
  52. };
  53. };