You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.1KB

  1. // Copyright 2012 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (olivier@mutable-instruments.net)
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. //
  16. // -----------------------------------------------------------------------------
  17. //
  18. // Sampled oscillator.
  19. #ifndef EDGES_DIGITAL_OSCILLATOR_H_
  20. #define EDGES_DIGITAL_OSCILLATOR_H_
  21. #include "avrlibx/avrlibx.h"
  22. namespace edges {
  23. using namespace avrlibx;
  24. enum OscillatorShape {
  25. OSC_TRIANGLE,
  26. OSC_NES_TRIANGLE,
  27. OSC_PITCHED_NOISE,
  28. OSC_NES_NOISE_LONG,
  29. OSC_NES_NOISE_SHORT,
  30. OSC_SINE,
  31. };
  32. class DigitalOscillator {
  33. public:
  34. typedef void (DigitalOscillator::*RenderFn)();
  35. DigitalOscillator() { }
  36. ~DigitalOscillator() { }
  37. void Init() {
  38. UpdatePitch(60 << 7, OSC_TRIANGLE);
  39. Gate(true);
  40. rng_state_ = 1;
  41. }
  42. void UpdatePitch(int16_t pitch, OscillatorShape shape) {
  43. pitch_ = pitch;
  44. shape_ = shape;
  45. }
  46. void Gate(bool gate) {
  47. gate_ = gate;
  48. }
  49. void Render();
  50. inline void set_cv_pw(uint8_t pw) {
  51. cv_pw_ = pw;
  52. }
  53. private:
  54. void ComputePhaseIncrement();
  55. void RenderSilence();
  56. void RenderSine();
  57. void RenderBandlimitedTriangle();
  58. void RenderNoiseNES();
  59. void RenderNoise();
  60. OscillatorShape shape_;
  61. int16_t pitch_;
  62. bool gate_;
  63. uint8_t note_;
  64. uint24_t phase_;
  65. uint24_t phase_increment_;
  66. uint16_t sample_;
  67. uint16_t rng_state_;
  68. uint16_t aux_phase_;
  69. uint8_t cv_pw_;
  70. static const RenderFn fn_table_[];
  71. DISALLOW_COPY_AND_ASSIGN(DigitalOscillator);
  72. };
  73. } // namespace edges
  74. #endif // EDGES_DIGITAL_OSCILLATOR_H_