|
@@ -0,0 +1,73 @@
|
|
|
+#pragma once
|
|
|
+
|
|
|
+namespace Audio
|
|
|
+{
|
|
|
+
|
|
|
+ const float SAMPLE_RATIO = 44100.0f;
|
|
|
+ const float PI_A = 3.14159265359f;
|
|
|
+ const size_t BUFFER_SIZE = 1024;
|
|
|
+
|
|
|
+ enum Shape
|
|
|
+ {
|
|
|
+ SINE
|
|
|
+ };
|
|
|
+
|
|
|
+ class Oscillator
|
|
|
+ {
|
|
|
+
|
|
|
+ protected:
|
|
|
+ float m_phase;
|
|
|
+ float m_phase_stride;
|
|
|
+ float m_frequency;
|
|
|
+ Shape m_shape;
|
|
|
+ float sample_duration;
|
|
|
+
|
|
|
+ float buffer[BUFFER_SIZE] = {0};
|
|
|
+
|
|
|
+ public:
|
|
|
+
|
|
|
+ Oscillator() = default;
|
|
|
+ float frequency() { return m_frequency; }
|
|
|
+ Shape shape() { return m_shape; }
|
|
|
+ /* float phase() { return m_phase; }
|
|
|
+ float phase_stide() { return m_phase_stride; }
|
|
|
+ void phase(float phase) { m_phase = phase; }
|
|
|
+ void phase_stride(float phase_stride) { m_phase_stride = phase_stride; }
|
|
|
+ */
|
|
|
+ void* data() { return (void *)buffer; }
|
|
|
+
|
|
|
+
|
|
|
+ virtual void update() {} ;
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+ class SquareOscillator: public Oscillator
|
|
|
+ {
|
|
|
+
|
|
|
+ public:
|
|
|
+ SquareOscillator(float frequency) {
|
|
|
+ sample_duration = 1 / SAMPLE_RATIO;
|
|
|
+ m_frequency = frequency;
|
|
|
+ m_phase = 0;
|
|
|
+ m_phase_stride = frequency * sample_duration;
|
|
|
+ };
|
|
|
+
|
|
|
+ void update() override;
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+ class SineOscillator: public Oscillator {
|
|
|
+
|
|
|
+ public:
|
|
|
+ SineOscillator(float frequency) {
|
|
|
+ sample_duration = 1 / SAMPLE_RATIO;
|
|
|
+ m_frequency = frequency;
|
|
|
+ m_phase = 0;
|
|
|
+ m_phase_stride = frequency * sample_duration;
|
|
|
+ }
|
|
|
+
|
|
|
+ void update() override;
|
|
|
+
|
|
|
+ };
|
|
|
+};
|
|
|
+
|