您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

122 行
4.9KB

  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,j;
  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. }
  91. #if 0
  92. void AudioEffectBitcrusher::updateCrush(uint8_t xxcrushBits, uint32_t xxsampleRate )
  93. {
  94. __disable_irq();
  95. if (xxcrushBits >= 16) { //too high a bitdepth or specifying full 16 bit range?
  96. crushBits = 16; // this will simply passthrough the crusher clean
  97. } else if(xxcrushBits >= 1) { // anywhere between 1 and 15, send it through to the Squidger.
  98. crushBits = xxcrushBits;
  99. } else {
  100. crushBits =1; // now lets be honest, crushing to 0 bits is going to be a mute command. send a 1 through instead.
  101. }
  102. if (xxsampleRate >=44100) {// maximum sample rate to be 44100
  103. sampleRate = 44100; //thats that
  104. } else if(xxsampleRate >= 1) { // anywhere between 1 and 44100, send it through to the Squeezer.
  105. sampleRate = xxsampleRate;
  106. sampleStep = 44100 / sampleRate; // calcualte the number of samples to duplicate as we reduce the samplerate.
  107. } else {
  108. sampleRate = 1;
  109. sampleStep = 44100 / sampleRate; // calcualte the number of samples to duplicate as we reduce the samplerate.
  110. }
  111. __enable_irq();
  112. }
  113. #endif