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.

effect_bitcrusher.cpp 3.9KB

пре 9 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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(void)
  31. {
  32. audio_block_t *block;
  33. uint32_t i;
  34. uint32_t sampleSquidge, sampleSqueeze; //squidge is bitdepth, squeeze is for samplerate
  35. if (crushBits == 16 && sampleStep <= 1) {
  36. // nothing to do. Output is sent through clean, then exit the function
  37. block = receiveReadOnly();
  38. if (!block) return;
  39. transmit(block);
  40. release(block);
  41. return;
  42. }
  43. // start of processing functions. Could be more elegant based on external
  44. // functions but left like this to enable code optimisation later.
  45. block = receiveWritable();
  46. if (!block) return;
  47. if (sampleStep <= 1) { //no sample rate mods, just crush the bitdepth.
  48. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  49. // shift bits right to cut off fine detail sampleSquidge is a
  50. // uint32 so sign extension will not occur, fills with zeroes.
  51. sampleSquidge = block->data[i] >> (16-crushBits);
  52. // shift bits left again to regain the volume level.
  53. // fills with zeroes.
  54. block->data[i] = sampleSquidge << (16-crushBits);
  55. }
  56. } else if (crushBits == 16) { //bitcrusher not being used, samplerate mods only.
  57. i=0;
  58. while (i < AUDIO_BLOCK_SAMPLES) {
  59. // save the root sample. this will pick up a root
  60. // sample every _sampleStep_ samples.
  61. sampleSqueeze = block->data[i];
  62. for (int j = 0; j < sampleStep && i < AUDIO_BLOCK_SAMPLES; j++) {
  63. // for each repeated sample, paste in the current
  64. // root sample, then move onto the next step.
  65. block->data[i] = sampleSqueeze;
  66. i++;
  67. }
  68. }
  69. } else { //both being used. crush those bits and mash those samples.
  70. i=0;
  71. while (i < AUDIO_BLOCK_SAMPLES) {
  72. // save the root sample. this will pick up a root sample
  73. // every _sampleStep_ samples.
  74. sampleSqueeze = block->data[i];
  75. for (int j = 0; j < sampleStep && i < AUDIO_BLOCK_SAMPLES; j++) {
  76. // shift bits right to cut off fine detail sampleSquidge
  77. // is a uint32 so sign extension will not occur, fills
  78. // with zeroes.
  79. sampleSquidge = sampleSqueeze >> (16-crushBits);
  80. // shift bits left again to regain the volume level.
  81. // fills with zeroes. paste into buffer sample +
  82. // sampleStep offset.
  83. block->data[i] = sampleSquidge << (16-crushBits);
  84. i++;
  85. }
  86. }
  87. }
  88. transmit(block);
  89. release(block);
  90. }