Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

effect_delay_ext.h 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #ifndef effect_delay_ext_h_
  27. #define effect_delay_ext_h_
  28. #include "Arduino.h"
  29. #include "AudioStream.h"
  30. #include "spi_interrupt.h"
  31. enum AudioEffectDelayMemoryType_t {
  32. AUDIO_MEMORY_23LC1024 = 0,
  33. AUDIO_MEMORY_MEMORYBOARD = 1,
  34. AUDIO_MEMORY_UNDEFINED = 2
  35. };
  36. class AudioEffectDelayExternal : public AudioStream
  37. {
  38. public:
  39. AudioEffectDelayExternal() : AudioStream(1, inputQueueArray) {
  40. initialize(AUDIO_MEMORY_23LC1024, 65536);
  41. }
  42. AudioEffectDelayExternal(AudioEffectDelayMemoryType_t type, float milliseconds=1e6)
  43. : AudioStream(1, inputQueueArray) {
  44. uint32_t n = (milliseconds*(AUDIO_SAMPLE_RATE_EXACT/1000.0f))+0.5f;
  45. initialize(type, n);
  46. }
  47. void delay(uint8_t channel, float milliseconds) {
  48. if (channel >= 8 || memory_type >= AUDIO_MEMORY_UNDEFINED) return;
  49. if (milliseconds < 0.0) milliseconds = 0.0;
  50. uint32_t n = (milliseconds*(AUDIO_SAMPLE_RATE_EXACT/1000.0f))+0.5f;
  51. n += AUDIO_BLOCK_SAMPLES;
  52. if (n > memory_length - AUDIO_BLOCK_SAMPLES)
  53. n = memory_length - AUDIO_BLOCK_SAMPLES;
  54. delay_length[channel] = n;
  55. uint8_t mask = activemask;
  56. if (activemask == 0) AudioStartUsingSPI();
  57. activemask = mask | (1<<channel);
  58. }
  59. void disable(uint8_t channel) {
  60. if (channel >= 8) return;
  61. uint8_t mask = activemask & ~(1<<channel);
  62. activemask = mask;
  63. if (mask == 0) AudioStopUsingSPI();
  64. }
  65. virtual void update(void);
  66. private:
  67. void initialize(AudioEffectDelayMemoryType_t type, uint32_t samples);
  68. void read(uint32_t address, uint32_t count, int16_t *data);
  69. void write(uint32_t address, uint32_t count, const int16_t *data);
  70. void zero(uint32_t address, uint32_t count) {
  71. write(address, count, NULL);
  72. }
  73. uint32_t memory_begin; // the first address in the memory we're using
  74. uint32_t memory_length; // the amount of memory we're using
  75. uint32_t head_offset; // head index (incoming) data into external memory
  76. uint32_t delay_length[8]; // # of sample delay for each channel (128 = no delay)
  77. uint8_t activemask; // which output channels are active
  78. uint8_t memory_type; // 0=23LC1024, 1=Frank's Memoryboard
  79. static uint32_t allocated[2];
  80. audio_block_t *inputQueueArray[1];
  81. };
  82. #endif