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.

102 lines
3.2KB

  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. // Create random repetitions of a pulse.
  28. #ifndef PEAKS_PULSE_PROCESSOR_PULSE_RANDOMIZER_H_
  29. #define PEAKS_PULSE_PROCESSOR_PULSE_RANDOMIZER_H_
  30. #include "stmlib/stmlib.h"
  31. #include "stmlib/utils/ring_buffer.h"
  32. #include "peaks/gate_processor.h"
  33. namespace peaks {
  34. struct TriggerPulse {
  35. uint32_t delay_counter;
  36. };
  37. static const uint8_t kTriggerPulseBufferSize = 32;
  38. class PulseRandomizer {
  39. public:
  40. PulseRandomizer() { }
  41. ~PulseRandomizer() { }
  42. void Init();
  43. void FillBuffer(InputBuffer* input_buffer, OutputBuffer* output_buffer);
  44. void Configure(uint16_t* parameter, ControlMode control_mode) {
  45. if (control_mode == CONTROL_MODE_HALF) {
  46. set_acceptance_probability(65535);
  47. set_repetition_probability(parameter[0]);
  48. set_delay_average(parameter[1]);
  49. set_delay_randomness(0);
  50. } else {
  51. set_acceptance_probability(parameter[0]);
  52. set_repetition_probability(parameter[1]);
  53. set_delay_average(parameter[2]);
  54. set_delay_randomness(parameter[3]);
  55. }
  56. }
  57. inline void set_repetition_probability(uint16_t repetition_probability) {
  58. repetition_probability_ = repetition_probability;
  59. }
  60. inline void set_acceptance_probability(uint16_t acceptance_probability) {
  61. acceptance_probability_ = acceptance_probability;
  62. }
  63. inline void set_delay_average(uint16_t delay_average) {
  64. delay_average_ = delay_average >> 1;
  65. }
  66. inline void set_delay_randomness(uint16_t delay_randomness) {
  67. delay_randomness_ = delay_randomness;
  68. }
  69. private:
  70. uint16_t delay() const;
  71. uint16_t repetition_probability_;
  72. uint16_t acceptance_probability_;
  73. uint16_t delay_average_;
  74. uint16_t delay_randomness_;
  75. uint16_t num_pulses_;
  76. uint16_t retrig_counter_;
  77. uint16_t delay_counter_[kTriggerPulseBufferSize];
  78. DISALLOW_COPY_AND_ASSIGN(PulseRandomizer);
  79. };
  80. } // namespace peaks
  81. #endif // PEAKS_PULSE_PROCESSOR_PULSE_RANDOMIZER_H_