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.

91 lines
3.7KB

  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 "effect_bitcrusher.h"
  30. void AudioEffectBitcrusher::update(const audio_block_t* inputBlock, audio_block_t* outputBlock) {
  31. uint32_t i;
  32. uint32_t sampleSquidge, sampleSqueeze; //squidge is bitdepth, squeeze is for samplerate
  33. if (!inputBlock || !outputBlock) {
  34. return;
  35. }
  36. if (crushBits == 16 && sampleStep <= 1) {
  37. // nothing to do. Output is sent through clean, then exit the function
  38. audio_block_t::copyBlock(inputBlock, outputBlock);
  39. }
  40. if (sampleStep <= 1) { //no sample rate mods, just crush the bitdepth.
  41. for (i = 0; i < AUDIO_BLOCK_SAMPLES; i++) {
  42. // shift bits right to cut off fine detail sampleSquidge is a
  43. // uint32 so sign extension will not occur, fills with zeroes.
  44. sampleSquidge = inputBlock->data[i] >> (16 - crushBits);
  45. // shift bits left again to regain the volume level.
  46. // fills with zeroes.
  47. outputBlock->data[i] = sampleSquidge << (16 - crushBits);
  48. }
  49. }
  50. else if (crushBits == 16) { //bitcrusher not being used, samplerate mods only.
  51. i = 0;
  52. while (i < AUDIO_BLOCK_SAMPLES) {
  53. // save the root sample. this will pick up a root
  54. // sample every _sampleStep_ samples.
  55. sampleSqueeze = inputBlock->data[i];
  56. for (int j = 0; j < sampleStep && i < AUDIO_BLOCK_SAMPLES; j++) {
  57. // for each repeated sample, paste in the current
  58. // root sample, then move onto the next step.
  59. outputBlock->data[i] = sampleSqueeze;
  60. i++;
  61. }
  62. }
  63. }
  64. else { //both being used. crush those bits and mash those samples.
  65. i = 0;
  66. while (i < AUDIO_BLOCK_SAMPLES) {
  67. // save the root sample. this will pick up a root sample
  68. // every _sampleStep_ samples.
  69. sampleSqueeze = inputBlock->data[i];
  70. for (int j = 0; j < sampleStep && i < AUDIO_BLOCK_SAMPLES; j++) {
  71. // shift bits right to cut off fine detail sampleSquidge
  72. // is a uint32 so sign extension will not occur, fills
  73. // with zeroes.
  74. sampleSquidge = sampleSqueeze >> (16 - crushBits);
  75. // shift bits left again to regain the volume level.
  76. // fills with zeroes. paste into buffer sample +
  77. // sampleStep offset.
  78. outputBlock->data[i] = sampleSquidge << (16 - crushBits);
  79. i++;
  80. }
  81. }
  82. }
  83. }