PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

effect_bitcrusher.cpp 3.9KB

3 лет назад
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, Jonathan Payne (jon@jonnypayne.com)
  3. * Based on Effect_Fade by Paul Stoffregen
  4. * Also samplerate reduction based on Pete Brown's bitcrusher here:
  5. * http://10rem.net/blog/2013/01/13/a-simple-bitcrusher-and-sample-rate-reducer-in-cplusplus-for-a-windows-store-app
  6. *
  7. * Development of this audio library was funded by PJRC.COM, LLC by sales of
  8. * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
  9. * open source software by purchasing Teensy or other PJRC products.
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice, development funding notice, and this permission
  19. * notice shall be included in all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. */
  29. #include <Arduino.h>
  30. #include "effect_bitcrusher.h"
  31. void AudioEffectBitcrusher::update(void)
  32. {
  33. audio_block_t *block;
  34. uint32_t i;
  35. uint32_t sampleSquidge, sampleSqueeze; //squidge is bitdepth, squeeze is for samplerate
  36. if (crushBits == 16 && sampleStep <= 1) {
  37. // nothing to do. Output is sent through clean, then exit the function
  38. block = receiveReadOnly();
  39. if (!block) return;
  40. transmit(block);
  41. release(block);
  42. return;
  43. }
  44. // start of processing functions. Could be more elegant based on external
  45. // functions but left like this to enable code optimisation later.
  46. block = receiveWritable();
  47. if (!block) return;
  48. if (sampleStep <= 1) { //no sample rate mods, just crush the bitdepth.
  49. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  50. // shift bits right to cut off fine detail sampleSquidge is a
  51. // uint32 so sign extension will not occur, fills with zeroes.
  52. sampleSquidge = block->data[i] >> (16-crushBits);
  53. // shift bits left again to regain the volume level.
  54. // fills with zeroes.
  55. block->data[i] = sampleSquidge << (16-crushBits);
  56. }
  57. } else if (crushBits == 16) { //bitcrusher not being used, samplerate mods only.
  58. i=0;
  59. while (i < AUDIO_BLOCK_SAMPLES) {
  60. // save the root sample. this will pick up a root
  61. // sample every _sampleStep_ samples.
  62. sampleSqueeze = block->data[i];
  63. for (int j = 0; j < sampleStep && i < AUDIO_BLOCK_SAMPLES; j++) {
  64. // for each repeated sample, paste in the current
  65. // root sample, then move onto the next step.
  66. block->data[i] = sampleSqueeze;
  67. i++;
  68. }
  69. }
  70. } else { //both being used. crush those bits and mash those samples.
  71. i=0;
  72. while (i < AUDIO_BLOCK_SAMPLES) {
  73. // save the root sample. this will pick up a root sample
  74. // every _sampleStep_ samples.
  75. sampleSqueeze = block->data[i];
  76. for (int j = 0; j < sampleStep && i < AUDIO_BLOCK_SAMPLES; j++) {
  77. // shift bits right to cut off fine detail sampleSquidge
  78. // is a uint32 so sign extension will not occur, fills
  79. // with zeroes.
  80. sampleSquidge = sampleSqueeze >> (16-crushBits);
  81. // shift bits left again to regain the volume level.
  82. // fills with zeroes. paste into buffer sample +
  83. // sampleStep offset.
  84. block->data[i] = sampleSquidge << (16-crushBits);
  85. i++;
  86. }
  87. }
  88. }
  89. transmit(block);
  90. release(block);
  91. }