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.

177 lines
5.8KB

  1. // Copyright 2013 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. //
  25. // -----------------------------------------------------------------------------
  26. //
  27. // This is the common entry points for all types of modulation sources!
  28. #ifndef PEAKS_PROCESSORS_H_
  29. #define PEAKS_PROCESSORS_H_
  30. #include "stmlib/stmlib.h"
  31. #include <algorithm>
  32. #include "peaks/drums/bass_drum.h"
  33. #include "peaks/drums/fm_drum.h"
  34. #include "peaks/drums/snare_drum.h"
  35. #include "peaks/drums/high_hat.h"
  36. #include "peaks/modulations/bouncing_ball.h"
  37. #include "peaks/modulations/lfo.h"
  38. #include "peaks/modulations/mini_sequencer.h"
  39. #include "peaks/modulations/multistage_envelope.h"
  40. #include "peaks/number_station/number_station.h"
  41. #include "peaks/pulse_processor/pulse_shaper.h"
  42. #include "peaks/pulse_processor/pulse_randomizer.h"
  43. #include "peaks/gate_processor.h"
  44. namespace peaks {
  45. enum ProcessorFunction {
  46. PROCESSOR_FUNCTION_ENVELOPE,
  47. PROCESSOR_FUNCTION_LFO,
  48. PROCESSOR_FUNCTION_TAP_LFO,
  49. PROCESSOR_FUNCTION_BASS_DRUM,
  50. PROCESSOR_FUNCTION_SNARE_DRUM,
  51. PROCESSOR_FUNCTION_HIGH_HAT,
  52. PROCESSOR_FUNCTION_FM_DRUM,
  53. PROCESSOR_FUNCTION_PULSE_SHAPER,
  54. PROCESSOR_FUNCTION_PULSE_RANDOMIZER,
  55. PROCESSOR_FUNCTION_BOUNCING_BALL,
  56. PROCESSOR_FUNCTION_MINI_SEQUENCER,
  57. PROCESSOR_FUNCTION_NUMBER_STATION,
  58. PROCESSOR_FUNCTION_LAST
  59. };
  60. #define DECLARE_PROCESSOR(ClassName, variable) \
  61. void ClassName ## Init() { \
  62. variable.Init(); \
  63. } \
  64. void ClassName ## Process(const GateFlags* gate_flags, int16_t* out, size_t size) { \
  65. variable.Process(gate_flags, out, size); \
  66. } \
  67. void ClassName ## Configure(uint16_t* p, ControlMode control_mode) { \
  68. variable.Configure(p, control_mode); \
  69. } \
  70. ClassName variable;
  71. class Processors {
  72. public:
  73. Processors() { }
  74. ~Processors() { }
  75. void Init(uint8_t index);
  76. typedef void (Processors::*InitFn)();
  77. typedef void (Processors::*ProcessFn)(const GateFlags*, int16_t*, size_t);
  78. typedef void (Processors::*ConfigureFn)(uint16_t*, ControlMode);
  79. struct ProcessorCallbacks {
  80. InitFn init_fn;
  81. ProcessFn process_fn;
  82. ConfigureFn configure_fn;
  83. };
  84. inline void set_control_mode(ControlMode control_mode) {
  85. control_mode_ = control_mode;
  86. Configure();
  87. }
  88. inline void set_parameter(uint8_t index, uint16_t parameter) {
  89. parameter_[index] = parameter;
  90. Configure();
  91. }
  92. inline void CopyParameters(uint16_t* parameters, uint16_t size) {
  93. std::copy(&parameters[0], &parameters[size], &parameter_[0]);
  94. }
  95. inline void set_function(ProcessorFunction function) {
  96. function_ = function;
  97. lfo_.set_sync(function == PROCESSOR_FUNCTION_TAP_LFO);
  98. callbacks_ = callbacks_table_[function];
  99. if (function != PROCESSOR_FUNCTION_TAP_LFO) {
  100. (this->*callbacks_.init_fn)();
  101. }
  102. Configure();
  103. }
  104. inline ProcessorFunction function() const { return function_; }
  105. inline void Process(const GateFlags* gate_flags, int16_t* output, size_t size) {
  106. (this->*callbacks_.process_fn)(gate_flags, output, size);
  107. }
  108. inline const NumberStation& number_station() const { return number_station_; }
  109. private:
  110. void Configure() {
  111. if (function_ == PROCESSOR_FUNCTION_SNARE_DRUM ||
  112. function_ == PROCESSOR_FUNCTION_HIGH_HAT) {
  113. uint16_t tone_parameter = control_mode_ == CONTROL_MODE_FULL
  114. ? parameter_[1] : parameter_[0];
  115. uint16_t snappy_parameter = control_mode_ == CONTROL_MODE_FULL
  116. ? parameter_[2] : parameter_[1];
  117. if (tone_parameter >= 65000 && snappy_parameter >= 65000) {
  118. if (function_ != PROCESSOR_FUNCTION_HIGH_HAT) {
  119. set_function(PROCESSOR_FUNCTION_HIGH_HAT);
  120. }
  121. } else if (tone_parameter <= 64500 || snappy_parameter <= 64500) {
  122. if (function_ != PROCESSOR_FUNCTION_SNARE_DRUM) {
  123. set_function(PROCESSOR_FUNCTION_SNARE_DRUM);
  124. }
  125. }
  126. }
  127. (this->*callbacks_.configure_fn)(&parameter_[0], control_mode_);
  128. }
  129. ControlMode control_mode_;
  130. ProcessorFunction function_;
  131. uint16_t parameter_[4];
  132. ProcessorCallbacks callbacks_;
  133. static const ProcessorCallbacks callbacks_table_[PROCESSOR_FUNCTION_LAST];
  134. DECLARE_PROCESSOR(MultistageEnvelope, envelope_);
  135. DECLARE_PROCESSOR(Lfo, lfo_);
  136. DECLARE_PROCESSOR(BassDrum, bass_drum_);
  137. DECLARE_PROCESSOR(SnareDrum, snare_drum_);
  138. DECLARE_PROCESSOR(HighHat, high_hat_);
  139. DECLARE_PROCESSOR(FmDrum, fm_drum_);
  140. DECLARE_PROCESSOR(PulseShaper, pulse_shaper_);
  141. DECLARE_PROCESSOR(PulseRandomizer, pulse_randomizer_);
  142. DECLARE_PROCESSOR(BouncingBall, bouncing_ball_);
  143. DECLARE_PROCESSOR(MiniSequencer, mini_sequencer_);
  144. DECLARE_PROCESSOR(NumberStation, number_station_);
  145. DISALLOW_COPY_AND_ASSIGN(Processors);
  146. };
  147. extern Processors processors[2];
  148. } // namespace peaks
  149. #endif // PEAKS_PROCESSORS_H_