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

AudioStream.h 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2017 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #ifndef AudioStream_h
  31. #define AudioStream_h
  32. #ifndef __ASSEMBLER__
  33. #include <stdio.h> // for NULL
  34. #include <string.h> // for memcpy
  35. #endif
  36. // AUDIO_BLOCK_SAMPLES determines how many samples the audio library processes
  37. // per update. It may be reduced to achieve lower latency response to events,
  38. // at the expense of higher interrupt and DMA setup overhead.
  39. //
  40. // Less than 32 may not work with some input & output objects. Multiples of 16
  41. // should be used, since some synthesis objects generate 16 samples per loop.
  42. //
  43. // Some parts of the audio library may have hard-coded dependency on 128 samples.
  44. // Please report these on the forum with reproducible test cases.
  45. #ifndef AUDIO_BLOCK_SAMPLES
  46. #define AUDIO_BLOCK_SAMPLES 128
  47. #endif
  48. #ifndef AUDIO_SAMPLE_RATE_EXACT
  49. #define AUDIO_SAMPLE_RATE_EXACT 44100.0f
  50. #endif
  51. #define AUDIO_SAMPLE_RATE AUDIO_SAMPLE_RATE_EXACT
  52. #ifndef __ASSEMBLER__
  53. class AudioStream;
  54. class AudioConnection;
  55. typedef struct audio_block_struct {
  56. uint8_t ref_count;
  57. uint8_t reserved1;
  58. uint16_t memory_pool_index;
  59. int16_t data[AUDIO_BLOCK_SAMPLES];
  60. } audio_block_t;
  61. class AudioConnection
  62. {
  63. public:
  64. AudioConnection(AudioStream &source, AudioStream &destination) :
  65. src(source), dst(destination), src_index(0), dest_index(0),
  66. next_dest(NULL)
  67. { isConnected = false;
  68. connect(); }
  69. AudioConnection(AudioStream &source, unsigned char sourceOutput,
  70. AudioStream &destination, unsigned char destinationInput) :
  71. src(source), dst(destination),
  72. src_index(sourceOutput), dest_index(destinationInput),
  73. next_dest(NULL)
  74. { isConnected = false;
  75. connect(); }
  76. friend class AudioStream;
  77. ~AudioConnection() {
  78. disconnect();
  79. }
  80. void disconnect(void);
  81. void connect(void);
  82. protected:
  83. AudioStream &src;
  84. AudioStream &dst;
  85. unsigned char src_index;
  86. unsigned char dest_index;
  87. AudioConnection *next_dest;
  88. bool isConnected;
  89. };
  90. #define AudioMemory(num) ({ \
  91. static DMAMEM audio_block_t data[num]; \
  92. AudioStream::initialize_memory(data, num); \
  93. })
  94. #define CYCLE_COUNTER_APPROX_PERCENT(n) (((n) + (F_CPU_ACTUAL / 32 / AUDIO_SAMPLE_RATE * AUDIO_BLOCK_SAMPLES / 100)) / (F_CPU_ACTUAL / 16 / AUDIO_SAMPLE_RATE * AUDIO_BLOCK_SAMPLES / 100))
  95. #define AudioProcessorUsage() (CYCLE_COUNTER_APPROX_PERCENT(AudioStream::cpu_cycles_total))
  96. #define AudioProcessorUsageMax() (CYCLE_COUNTER_APPROX_PERCENT(AudioStream::cpu_cycles_total_max))
  97. #define AudioProcessorUsageMaxReset() (AudioStream::cpu_cycles_total_max = AudioStream::cpu_cycles_total)
  98. #define AudioMemoryUsage() (AudioStream::memory_used)
  99. #define AudioMemoryUsageMax() (AudioStream::memory_used_max)
  100. #define AudioMemoryUsageMaxReset() (AudioStream::memory_used_max = AudioStream::memory_used)
  101. class AudioStream
  102. {
  103. public:
  104. AudioStream(unsigned char ninput, audio_block_t **iqueue) :
  105. num_inputs(ninput), inputQueue(iqueue) {
  106. active = false;
  107. destination_list = NULL;
  108. for (int i=0; i < num_inputs; i++) {
  109. inputQueue[i] = NULL;
  110. }
  111. // add to a simple list, for update_all
  112. // TODO: replace with a proper data flow analysis in update_all
  113. if (first_update == NULL) {
  114. first_update = this;
  115. } else {
  116. AudioStream *p;
  117. for (p=first_update; p->next_update; p = p->next_update) ;
  118. p->next_update = this;
  119. }
  120. next_update = NULL;
  121. cpu_cycles = 0;
  122. cpu_cycles_max = 0;
  123. numConnections = 0;
  124. }
  125. static void initialize_memory(audio_block_t *data, unsigned int num);
  126. int processorUsage(void) { return CYCLE_COUNTER_APPROX_PERCENT(cpu_cycles); }
  127. int processorUsageMax(void) { return CYCLE_COUNTER_APPROX_PERCENT(cpu_cycles_max); }
  128. void processorUsageMaxReset(void) { cpu_cycles_max = cpu_cycles; }
  129. bool isActive(void) { return active; }
  130. uint16_t cpu_cycles;
  131. uint16_t cpu_cycles_max;
  132. static uint16_t cpu_cycles_total;
  133. static uint16_t cpu_cycles_total_max;
  134. static uint16_t memory_used;
  135. static uint16_t memory_used_max;
  136. protected:
  137. bool active;
  138. unsigned char num_inputs;
  139. static audio_block_t * allocate(void);
  140. static void release(audio_block_t * block);
  141. void transmit(audio_block_t *block, unsigned char index = 0);
  142. audio_block_t * receiveReadOnly(unsigned int index = 0);
  143. audio_block_t * receiveWritable(unsigned int index = 0);
  144. static bool update_setup(void);
  145. static void update_stop(void);
  146. static void update_all(void) { NVIC_SET_PENDING(IRQ_SOFTWARE); }
  147. friend void software_isr(void);
  148. friend class AudioConnection;
  149. uint8_t numConnections;
  150. private:
  151. AudioConnection *destination_list;
  152. audio_block_t **inputQueue;
  153. static bool update_scheduled;
  154. virtual void update(void) = 0;
  155. static AudioStream *first_update; // for update_all
  156. AudioStream *next_update; // for update_all
  157. static audio_block_t *memory_pool;
  158. static uint32_t memory_pool_available_mask[];
  159. static uint16_t memory_pool_first_mask;
  160. };
  161. #endif
  162. #endif