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.

97 lines
3.0KB

  1. // Copyright 2012 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. // A waveshaper adding waveform impurities, seeded by the serial number of
  28. // the MCU.
  29. #ifndef BRAIDS_SIGNATURE_WAVESHAPER_H_
  30. #define BRAIDS_SIGNATURE_WAVESHAPER_H_
  31. #include "stmlib/stmlib.h"
  32. #include "stmlib/utils/dsp.h"
  33. #include "braids/resources.h"
  34. namespace braids {
  35. class SignatureWaveshaper {
  36. public:
  37. SignatureWaveshaper() { }
  38. ~SignatureWaveshaper() { }
  39. inline void Init(uint32_t seed) {
  40. int32_t skew = seed & 15;
  41. seed >>= 4;
  42. int32_t sigmoid_strength = seed & 31;
  43. seed >>= 5;
  44. int32_t bumplets_frequency = seed & 3;
  45. seed >>= 2;
  46. bumplets_frequency += 3;
  47. int32_t bumplets_width = seed & 7;
  48. seed >>= 3;
  49. bumplets_width += 1;
  50. bumplets_width <<= 7;
  51. bumplets_width *= bumplets_width;
  52. for (int i = 0; i < 256; ++i) {
  53. int16_t x = (i - 128) << 8;
  54. int16_t x_skew = i * i - 32768;
  55. x = stmlib::Mix(x, x_skew, skew << 11);
  56. int16_t sigmoid = x * (8192 + (sigmoid_strength << 10)) / \
  57. (8192 + (sigmoid_strength * std::abs((int32_t) x) >> 5));
  58. int16_t bumplets = wav_sine[(i * bumplets_frequency) & 255];
  59. uint16_t bumplet_gain = x * x / (bumplets_width) + 16;
  60. bumplet_gain = 32768 * 128 / (128 + bumplet_gain);
  61. transfer_[i] = stmlib::Mix(sigmoid, bumplets, bumplet_gain);
  62. }
  63. transfer_[256] = transfer_[255];
  64. }
  65. inline int32_t transfer(uint16_t i) {
  66. return transfer_[i];
  67. }
  68. inline int32_t Transform(int16_t sample) {
  69. uint16_t i = sample + 32768;
  70. int32_t a = transfer_[i >> 8];
  71. int32_t b = transfer_[(i >> 8) + 1];
  72. return a + ((b - a) * (i & 0xff) >> 8);
  73. }
  74. private:
  75. int32_t transfer_[257];
  76. DISALLOW_COPY_AND_ASSIGN(SignatureWaveshaper);
  77. };
  78. } // namespace braids
  79. #endif // BRAIDS_VCO_JITTER_SOURCE_H_