Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AudioStream.cpp 9.0KB

5 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. #include <Arduino.h>
  31. #include "AudioStream.h"
  32. #if defined(__IMXRT1052__)
  33. #define MAX_AUDIO_MEMORY 229376
  34. #elif defined(__IMXRT1062__)
  35. #define MAX_AUDIO_MEMORY 229376
  36. #endif
  37. #define NUM_MASKS (((MAX_AUDIO_MEMORY / AUDIO_BLOCK_SAMPLES / 2) + 31) / 32)
  38. audio_block_t * AudioStream::memory_pool;
  39. uint32_t AudioStream::memory_pool_available_mask[NUM_MASKS];
  40. uint16_t AudioStream::memory_pool_first_mask;
  41. uint16_t AudioStream::cpu_cycles_total = 0;
  42. uint16_t AudioStream::cpu_cycles_total_max = 0;
  43. uint16_t AudioStream::memory_used = 0;
  44. uint16_t AudioStream::memory_used_max = 0;
  45. void software_isr(void);
  46. // Set up the pool of audio data blocks
  47. // placing them all onto the free list
  48. __attribute__((section(".progmem")))
  49. void AudioStream::initialize_memory(audio_block_t *data, unsigned int num)
  50. {
  51. unsigned int i;
  52. unsigned int maxnum = MAX_AUDIO_MEMORY / AUDIO_BLOCK_SAMPLES / 2;
  53. //Serial.println("AudioStream initialize_memory");
  54. //delay(10);
  55. if (num > maxnum) num = maxnum;
  56. __disable_irq();
  57. memory_pool = data;
  58. memory_pool_first_mask = 0;
  59. for (i=0; i < NUM_MASKS; i++) {
  60. memory_pool_available_mask[i] = 0;
  61. }
  62. for (i=0; i < num; i++) {
  63. memory_pool_available_mask[i >> 5] |= (1 << (i & 0x1F));
  64. }
  65. for (i=0; i < num; i++) {
  66. data[i].memory_pool_index = i;
  67. }
  68. __enable_irq();
  69. }
  70. // Allocate 1 audio data block. If successful
  71. // the caller is the only owner of this new block
  72. audio_block_t * AudioStream::allocate(void)
  73. {
  74. uint32_t n, index, avail;
  75. uint32_t *p, *end;
  76. audio_block_t *block;
  77. uint32_t used;
  78. p = memory_pool_available_mask;
  79. end = p + NUM_MASKS;
  80. __disable_irq();
  81. index = memory_pool_first_mask;
  82. p += index;
  83. while (1) {
  84. if (p >= end) {
  85. __enable_irq();
  86. //Serial.println("alloc:null");
  87. return NULL;
  88. }
  89. avail = *p;
  90. if (avail) break;
  91. index++;
  92. p++;
  93. }
  94. n = __builtin_clz(avail);
  95. avail &= ~(0x80000000 >> n);
  96. *p = avail;
  97. if (!avail) index++;
  98. memory_pool_first_mask = index;
  99. used = memory_used + 1;
  100. memory_used = used;
  101. __enable_irq();
  102. index = p - memory_pool_available_mask;
  103. block = memory_pool + ((index << 5) + (31 - n));
  104. block->ref_count = 1;
  105. if (used > memory_used_max) memory_used_max = used;
  106. //Serial.print("alloc:");
  107. //Serial.println((uint32_t)block, HEX);
  108. return block;
  109. }
  110. // Release ownership of a data block. If no
  111. // other streams have ownership, the block is
  112. // returned to the free pool
  113. void AudioStream::release(audio_block_t *block)
  114. {
  115. //if (block == NULL) return;
  116. uint32_t mask = (0x80000000 >> (31 - (block->memory_pool_index & 0x1F)));
  117. uint32_t index = block->memory_pool_index >> 5;
  118. __disable_irq();
  119. if (block->ref_count > 1) {
  120. block->ref_count--;
  121. } else {
  122. //Serial.print("reles:");
  123. //Serial.println((uint32_t)block, HEX);
  124. memory_pool_available_mask[index] |= mask;
  125. if (index < memory_pool_first_mask) memory_pool_first_mask = index;
  126. memory_used--;
  127. }
  128. __enable_irq();
  129. }
  130. // Transmit an audio data block
  131. // to all streams that connect to an output. The block
  132. // becomes owned by all the recepients, but also is still
  133. // owned by this object. Normally, a block must be released
  134. // by the caller after it's transmitted. This allows the
  135. // caller to transmit to same block to more than 1 output,
  136. // and then release it once after all transmit calls.
  137. void AudioStream::transmit(audio_block_t *block, unsigned char index)
  138. {
  139. for (AudioConnection *c = destination_list; c != NULL; c = c->next_dest) {
  140. if (c->src_index == index) {
  141. if (c->dst.inputQueue[c->dest_index] == NULL) {
  142. c->dst.inputQueue[c->dest_index] = block;
  143. block->ref_count++;
  144. }
  145. }
  146. }
  147. }
  148. // Receive block from an input. The block's data
  149. // may be shared with other streams, so it must not be written
  150. audio_block_t * AudioStream::receiveReadOnly(unsigned int index)
  151. {
  152. audio_block_t *in;
  153. if (index >= num_inputs) return NULL;
  154. in = inputQueue[index];
  155. inputQueue[index] = NULL;
  156. return in;
  157. }
  158. // Receive block from an input. The block will not
  159. // be shared, so its contents may be changed.
  160. audio_block_t * AudioStream::receiveWritable(unsigned int index)
  161. {
  162. audio_block_t *in, *p;
  163. if (index >= num_inputs) return NULL;
  164. in = inputQueue[index];
  165. inputQueue[index] = NULL;
  166. if (in && in->ref_count > 1) {
  167. p = allocate();
  168. if (p) memcpy(p->data, in->data, sizeof(p->data));
  169. in->ref_count--;
  170. in = p;
  171. }
  172. return in;
  173. }
  174. void AudioConnection::connect(void)
  175. {
  176. AudioConnection *p;
  177. if (isConnected) return;
  178. if (dest_index > dst.num_inputs) return;
  179. __disable_irq();
  180. p = src.destination_list;
  181. if (p == NULL) {
  182. src.destination_list = this;
  183. } else {
  184. while (p->next_dest) {
  185. if (&p->src == &this->src && &p->dst == &this->dst
  186. && p->src_index == this->src_index && p->dest_index == this->dest_index) {
  187. //Source and destination already connected through another connection, abort
  188. __enable_irq();
  189. return;
  190. }
  191. p = p->next_dest;
  192. }
  193. p->next_dest = this;
  194. }
  195. this->next_dest = NULL;
  196. src.numConnections++;
  197. src.active = true;
  198. dst.numConnections++;
  199. dst.active = true;
  200. isConnected = true;
  201. __enable_irq();
  202. }
  203. void AudioConnection::disconnect(void)
  204. {
  205. AudioConnection *p;
  206. if (!isConnected) return;
  207. if (dest_index > dst.num_inputs) return;
  208. __disable_irq();
  209. // Remove destination from source list
  210. p = src.destination_list;
  211. if (p == NULL) {
  212. //>>> PAH re-enable the IRQ
  213. __enable_irq();
  214. return;
  215. } else if (p == this) {
  216. if (p->next_dest) {
  217. src.destination_list = next_dest;
  218. } else {
  219. src.destination_list = NULL;
  220. }
  221. } else {
  222. while (p) {
  223. if (p == this) {
  224. if (p->next_dest) {
  225. p = next_dest;
  226. break;
  227. } else {
  228. p = NULL;
  229. break;
  230. }
  231. }
  232. p = p->next_dest;
  233. }
  234. }
  235. //>>> PAH release the audio buffer properly
  236. //Remove possible pending src block from destination
  237. if(dst.inputQueue[dest_index] != NULL) {
  238. AudioStream::release(dst.inputQueue[dest_index]);
  239. // release() re-enables the IRQ. Need it to be disabled a little longer
  240. __disable_irq();
  241. dst.inputQueue[dest_index] = NULL;
  242. }
  243. //Check if the disconnected AudioStream objects should still be active
  244. src.numConnections--;
  245. if (src.numConnections == 0) {
  246. src.active = false;
  247. }
  248. dst.numConnections--;
  249. if (dst.numConnections == 0) {
  250. dst.active = false;
  251. }
  252. isConnected = false;
  253. __enable_irq();
  254. }
  255. // When an object has taken responsibility for calling update_all()
  256. // at each block interval (approx 2.9ms), this variable is set to
  257. // true. Objects that are capable of calling update_all(), typically
  258. // input and output based on interrupts, must check this variable in
  259. // their constructors.
  260. bool AudioStream::update_scheduled = false;
  261. bool AudioStream::update_setup(void)
  262. {
  263. if (update_scheduled) return false;
  264. attachInterruptVector(IRQ_SOFTWARE, software_isr);
  265. NVIC_SET_PRIORITY(IRQ_SOFTWARE, 208); // 255 = lowest priority
  266. NVIC_ENABLE_IRQ(IRQ_SOFTWARE);
  267. update_scheduled = true;
  268. return true;
  269. }
  270. void AudioStream::update_stop(void)
  271. {
  272. NVIC_DISABLE_IRQ(IRQ_SOFTWARE);
  273. update_scheduled = false;
  274. }
  275. AudioStream * AudioStream::first_update = NULL;
  276. void software_isr(void) // AudioStream::update_all()
  277. {
  278. AudioStream *p;
  279. uint32_t totalcycles = ARM_DWT_CYCCNT;
  280. //digitalWriteFast(2, HIGH);
  281. for (p = AudioStream::first_update; p; p = p->next_update) {
  282. if (p->active) {
  283. uint32_t cycles = ARM_DWT_CYCCNT;
  284. p->update();
  285. // TODO: traverse inputQueueArray and release
  286. // any input blocks that weren't consumed?
  287. cycles = (ARM_DWT_CYCCNT - cycles) >> 4;
  288. p->cpu_cycles = cycles;
  289. if (cycles > p->cpu_cycles_max) p->cpu_cycles_max = cycles;
  290. }
  291. }
  292. //digitalWriteFast(2, LOW);
  293. totalcycles = (ARM_DWT_CYCCNT - totalcycles) >> 4;;
  294. AudioStream::cpu_cycles_total = totalcycles;
  295. if (totalcycles > AudioStream::cpu_cycles_total_max)
  296. AudioStream::cpu_cycles_total_max = totalcycles;
  297. asm("DSB");
  298. }