Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

141 lines
4.5KB

  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
  3. *
  4. * Development of this audio library was funded by PJRC.COM, LLC by sales of
  5. * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
  6. * open source software by purchasing Teensy or other PJRC products.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice, development funding notice, and this permission
  16. * notice shall be included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "effect_delay_ext.h"
  27. void AudioEffectDelayExternal::update(void)
  28. {
  29. audio_block_t *block;
  30. uint32_t n, channel, read_offset;
  31. // grab incoming data and put it into the memory
  32. block = receiveReadOnly();
  33. if (memory_type >= AUDIO_MEMORY_UNDEFINED) {
  34. // ignore input and do nothing is undefined memory type
  35. release(block);
  36. return;
  37. }
  38. if (block) {
  39. if (head_offset + AUDIO_BLOCK_SAMPLES <= memory_length) {
  40. // a single write is enough
  41. write(head_offset, AUDIO_BLOCK_SAMPLES, block->data);
  42. head_offset += AUDIO_BLOCK_SAMPLES;
  43. } else {
  44. // write wraps across end-of-memory
  45. n = memory_length - head_offset;
  46. write(head_offset, n, block->data);
  47. head_offset = AUDIO_BLOCK_SAMPLES - n;
  48. write(0, head_offset, block->data + n);
  49. }
  50. release(block);
  51. } else {
  52. // if no input, store zeros, so later playback will
  53. // not be random garbage previously stored in memory
  54. if (head_offset + AUDIO_BLOCK_SAMPLES <= memory_length) {
  55. zero(head_offset, AUDIO_BLOCK_SAMPLES);
  56. head_offset += AUDIO_BLOCK_SAMPLES;
  57. } else {
  58. n = memory_length - head_offset;
  59. zero(head_offset, n);
  60. head_offset = AUDIO_BLOCK_SAMPLES - n;
  61. zero(0, head_offset);
  62. }
  63. }
  64. // transmit the delayed outputs
  65. for (channel = 0; channel < 8; channel++) {
  66. if (!(activemask & (1<<channel))) continue;
  67. block = allocate();
  68. if (!block) continue;
  69. // compute the delayed location where we read
  70. if (delay_length[channel] <= head_offset) {
  71. read_offset = head_offset - delay_length[channel];
  72. } else {
  73. read_offset = memory_length + head_offset - delay_length[channel];
  74. }
  75. if (read_offset + AUDIO_BLOCK_SAMPLES <= memory_length) {
  76. // a single read will do it
  77. read(read_offset, AUDIO_BLOCK_SAMPLES, block->data);
  78. } else {
  79. // read wraps across end-of-memory
  80. n = memory_length - read_offset;
  81. read(read_offset, n, block->data);
  82. read(0, AUDIO_BLOCK_SAMPLES - n, block->data + n);
  83. }
  84. transmit(block, channel);
  85. release(block);
  86. }
  87. }
  88. uint32_t AudioEffectDelayExternal::allocated[2] = {0, 0};
  89. void AudioEffectDelayExternal::initialize(AudioEffectDelayMemoryType_t type, uint32_t samples)
  90. {
  91. uint32_t memsize, avail;
  92. activemask = 0;
  93. head_offset = 0;
  94. memory_type = type;
  95. if (type == AUDIO_MEMORY_23LC1024) {
  96. //memsize = 65536;
  97. memsize = 8000;
  98. } else if (type == AUDIO_MEMORY_MEMORYBOARD) {
  99. memsize = 393216;
  100. } else {
  101. return;
  102. }
  103. avail = memsize - allocated[type];
  104. if (avail < AUDIO_BLOCK_SAMPLES*2+1) {
  105. memory_type = AUDIO_MEMORY_UNDEFINED;
  106. return;
  107. }
  108. if (samples > avail) samples = avail;
  109. memory_begin = allocated[type];
  110. allocated[type] += samples;
  111. memory_length = samples;
  112. zero(0, memory_length);
  113. }
  114. static int16_t testmem[8000];
  115. void AudioEffectDelayExternal::read(uint32_t offset, uint32_t count, int16_t *data)
  116. {
  117. while (count) { *data++ = testmem[memory_begin + offset++]; count--; }
  118. }
  119. void AudioEffectDelayExternal::write(uint32_t offset, uint32_t count, const int16_t *data)
  120. {
  121. while (count) { testmem[memory_begin + offset++] = *data++; count--; }
  122. }
  123. void AudioEffectDelayExternal::zero(uint32_t offset, uint32_t count)
  124. {
  125. while (count) { testmem[memory_begin + offset++] = 0; count--; }
  126. }