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.

213 lines
6.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_BUFFERED_PROCESSOR(ClassName, variable) \
  61. void ClassName ## Init() { \
  62. variable.Init(); \
  63. } \
  64. void ClassName ## FillBuffer() { \
  65. variable.FillBuffer(&input_buffer_, &output_buffer_); \
  66. } \
  67. void ClassName ## Configure(uint16_t* p, ControlMode control_mode) { \
  68. variable.Configure(p, control_mode); \
  69. } \
  70. ClassName variable;
  71. #define DECLARE_UNBUFFERED_PROCESSOR(ClassName, variable) \
  72. void ClassName ## Init() { \
  73. variable.Init(); \
  74. } \
  75. int16_t ClassName ## ProcessSingleSample(uint8_t control) { \
  76. return variable.ProcessSingleSample(control); \
  77. } \
  78. void ClassName ## Configure(uint16_t* p, ControlMode control_mode) { \
  79. variable.Configure(p, control_mode); \
  80. } \
  81. ClassName variable;
  82. class Processors {
  83. public:
  84. Processors() { }
  85. ~Processors() { }
  86. void Init(uint8_t index);
  87. typedef void (Processors::*InitFn)();
  88. typedef int16_t (Processors::*ProcessSingleSampleFn)(uint8_t);
  89. typedef void (Processors::*FillBufferFn)();
  90. typedef void (Processors::*ConfigureFn)(uint16_t*, ControlMode);
  91. struct ProcessorCallbacks {
  92. InitFn init_fn;
  93. ProcessSingleSampleFn process_single_sample;
  94. FillBufferFn fill_buffer;
  95. ConfigureFn configure;
  96. };
  97. inline void set_control_mode(ControlMode control_mode) {
  98. control_mode_ = control_mode;
  99. Configure();
  100. }
  101. inline void set_parameter(uint8_t index, uint16_t parameter) {
  102. parameter_[index] = parameter;
  103. Configure();
  104. }
  105. inline void CopyParameters(uint16_t* parameters, uint16_t size) {
  106. std::copy(&parameters[0], &parameters[size], &parameter_[0]);
  107. }
  108. inline void set_function(ProcessorFunction function) {
  109. function_ = function;
  110. lfo_.set_sync(function == PROCESSOR_FUNCTION_TAP_LFO);
  111. callbacks_ = callbacks_table_[function];
  112. if (function != PROCESSOR_FUNCTION_TAP_LFO) {
  113. (this->*callbacks_.init_fn)();
  114. }
  115. Configure();
  116. }
  117. inline ProcessorFunction function() const { return function_; }
  118. inline int16_t Process(uint8_t control) {
  119. if (callbacks_.process_single_sample) {
  120. return (this->*callbacks_.process_single_sample)(control);
  121. } else {
  122. input_buffer_.Overwrite(control);
  123. return output_buffer_.ImmediateRead();
  124. }
  125. }
  126. inline bool Buffer() {
  127. if (callbacks_.fill_buffer) {
  128. if (output_buffer_.writable() < kBlockSize) {
  129. return false;
  130. } else {
  131. (this->*callbacks_.fill_buffer)();
  132. return true;
  133. }
  134. } else {
  135. return true;
  136. }
  137. }
  138. inline const NumberStation& number_station() const { return number_station_; }
  139. private:
  140. void Configure() {
  141. if (function_ == PROCESSOR_FUNCTION_SNARE_DRUM ||
  142. function_ == PROCESSOR_FUNCTION_HIGH_HAT) {
  143. uint16_t tone_parameter = control_mode_ == CONTROL_MODE_FULL
  144. ? parameter_[1] : parameter_[0];
  145. uint16_t snappy_parameter = control_mode_ == CONTROL_MODE_FULL
  146. ? parameter_[2] : parameter_[1];
  147. if (tone_parameter >= 65000 && snappy_parameter >= 65000) {
  148. if (function_ != PROCESSOR_FUNCTION_HIGH_HAT) {
  149. set_function(PROCESSOR_FUNCTION_HIGH_HAT);
  150. }
  151. } else if (tone_parameter <= 64500 || snappy_parameter <= 64500) {
  152. if (function_ != PROCESSOR_FUNCTION_SNARE_DRUM) {
  153. set_function(PROCESSOR_FUNCTION_SNARE_DRUM);
  154. }
  155. }
  156. }
  157. (this->*callbacks_.configure)(&parameter_[0], control_mode_);
  158. }
  159. InputBuffer input_buffer_;
  160. OutputBuffer output_buffer_;
  161. ControlMode control_mode_;
  162. ProcessorFunction function_;
  163. uint16_t parameter_[4];
  164. ProcessorCallbacks callbacks_;
  165. static const ProcessorCallbacks callbacks_table_[PROCESSOR_FUNCTION_LAST];
  166. DECLARE_UNBUFFERED_PROCESSOR(MultistageEnvelope, envelope_);
  167. DECLARE_BUFFERED_PROCESSOR(Lfo, lfo_);
  168. DECLARE_UNBUFFERED_PROCESSOR(BassDrum, bass_drum_);
  169. DECLARE_UNBUFFERED_PROCESSOR(SnareDrum, snare_drum_);
  170. DECLARE_UNBUFFERED_PROCESSOR(HighHat, high_hat_);
  171. DECLARE_BUFFERED_PROCESSOR(FmDrum, fm_drum_);
  172. DECLARE_BUFFERED_PROCESSOR(PulseShaper, pulse_shaper_);
  173. DECLARE_BUFFERED_PROCESSOR(PulseRandomizer, pulse_randomizer_);
  174. DECLARE_UNBUFFERED_PROCESSOR(BouncingBall, bouncing_ball_);
  175. DECLARE_UNBUFFERED_PROCESSOR(MiniSequencer, mini_sequencer_);
  176. DECLARE_BUFFERED_PROCESSOR(NumberStation, number_station_);
  177. DISALLOW_COPY_AND_ASSIGN(Processors);
  178. };
  179. extern Processors processors[2];
  180. } // namespace peaks
  181. #endif // PEAKS_PROCESSORS_H_