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.

AudioStream.cpp 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 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 "AudioStream.h"
  31. audio_block_t * AudioStream::memory_pool;
  32. uint8_t AudioStream::memory_pool_size = 0;
  33. uint32_t AudioStream::memory_pool_available_mask;
  34. uint16_t AudioStream::cpu_cycles_total = 0;
  35. uint16_t AudioStream::cpu_cycles_total_max = 0;
  36. uint8_t AudioStream::memory_used = 0;
  37. uint8_t AudioStream::memory_used_max = 0;
  38. // Set up the pool of audio data blocks
  39. // placing them all onto the free list
  40. void AudioStream::initialize_memory(audio_block_t *data, unsigned int num)
  41. {
  42. //Serial.println("AudioStream initialize_memory");
  43. memory_pool = data;
  44. if (num > 31) num = 31;
  45. memory_pool_size = num;
  46. memory_pool_available_mask = 0xFFFFFFFF;
  47. for (unsigned int i=0; i < num; i++) {
  48. data[i].memory_pool_index = i;
  49. }
  50. }
  51. // Allocate 1 audio data block. If successful
  52. // the caller is the only owner of this new block
  53. audio_block_t * AudioStream::allocate(void)
  54. {
  55. uint32_t n, avail;
  56. audio_block_t *block;
  57. uint8_t used;
  58. __disable_irq();
  59. avail = memory_pool_available_mask;
  60. n = __builtin_clz(avail);
  61. if (n >= memory_pool_size) {
  62. __enable_irq();
  63. return NULL;
  64. }
  65. memory_pool_available_mask = avail & ~(0x80000000 >> n);
  66. used = memory_used + 1;
  67. memory_used = used;
  68. __enable_irq();
  69. block = memory_pool + n;
  70. block->ref_count = 1;
  71. if (used > memory_used_max) memory_used_max = used;
  72. return block;
  73. }
  74. // Release ownership of a data block. If no
  75. // other streams have ownership, the block is
  76. // returned to the free pool
  77. void AudioStream::release(audio_block_t *block)
  78. {
  79. uint32_t mask = (0x80000000 >> block->memory_pool_index);
  80. __disable_irq();
  81. if (block->ref_count > 1) {
  82. block->ref_count--;
  83. } else {
  84. memory_pool_available_mask |= mask;
  85. memory_used--;
  86. }
  87. __enable_irq();
  88. }
  89. // Transmit an audio data block
  90. // to all streams that connect to an output. The block
  91. // becomes owned by all the recepients, but also is still
  92. // owned by this object. Normally, a block is released
  93. // after it's transmitted.
  94. void AudioStream::transmit(audio_block_t *block, unsigned char index)
  95. {
  96. for (AudioConnection *c = destination_list; c != NULL; c = c->next_dest) {
  97. if (c->src_index == index) {
  98. if (c->dst.inputQueue[c->dest_index] == NULL) {
  99. c->dst.inputQueue[c->dest_index] = block;
  100. block->ref_count++;
  101. }
  102. }
  103. }
  104. }
  105. // Receive block from an input. The block's data
  106. // may be shared with other streams, so it must not be written
  107. audio_block_t * AudioStream::receiveReadOnly(unsigned int index)
  108. {
  109. audio_block_t *in;
  110. if (index >= num_inputs) return NULL;
  111. in = inputQueue[index];
  112. inputQueue[index] = NULL;
  113. return in;
  114. }
  115. // Receive block from an input. The block will not
  116. // be shared, so its contents may be changed.
  117. audio_block_t * AudioStream::receiveWritable(unsigned int index)
  118. {
  119. audio_block_t *in, *p;
  120. if (index >= num_inputs) return NULL;
  121. in = inputQueue[index];
  122. inputQueue[index] = NULL;
  123. if (in && in->ref_count > 1) {
  124. p = allocate();
  125. if (p) memcpy(p->data, in->data, sizeof(p->data));
  126. in->ref_count--;
  127. in = p;
  128. }
  129. return in;
  130. }
  131. void AudioConnection::connect(void)
  132. {
  133. AudioConnection *p;
  134. if (dest_index > dst.num_inputs) return;
  135. __disable_irq();
  136. p = src.destination_list;
  137. if (p == NULL) {
  138. src.destination_list = this;
  139. } else {
  140. while (p->next_dest) p = p->next_dest;
  141. p->next_dest = this;
  142. }
  143. src.active = true;
  144. dst.active = true;
  145. __enable_irq();
  146. }
  147. // When an object has taken responsibility for calling update_all()
  148. // at each block interval (approx 2.9ms), this variable is set to
  149. // true. Objects that are capable of calling update_all(), typically
  150. // input and output based on interrupts, must check this variable in
  151. // their constructors.
  152. bool AudioStream::update_scheduled = false;
  153. bool AudioStream::update_setup(void)
  154. {
  155. if (update_scheduled) return false;
  156. NVIC_SET_PRIORITY(IRQ_SOFTWARE, 0xFF); // 0xFF = lowest priority
  157. NVIC_ENABLE_IRQ(IRQ_SOFTWARE);
  158. update_scheduled = true;
  159. return true;
  160. }
  161. void AudioStream::update_stop(void)
  162. {
  163. NVIC_DISABLE_IRQ(IRQ_SOFTWARE);
  164. update_scheduled = false;
  165. }
  166. AudioStream * AudioStream::first_update = NULL;
  167. void software_isr(void) // AudioStream::update_all()
  168. {
  169. AudioStream *p;
  170. ARM_DEMCR |= ARM_DEMCR_TRCENA;
  171. ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA;
  172. uint32_t totalcycles = ARM_DWT_CYCCNT;
  173. //digitalWriteFast(2, HIGH);
  174. for (p = AudioStream::first_update; p; p = p->next_update) {
  175. if (p->active) {
  176. uint32_t cycles = ARM_DWT_CYCCNT;
  177. p->update();
  178. // TODO: traverse inputQueueArray and release
  179. // any input blocks that weren't consumed?
  180. cycles = (ARM_DWT_CYCCNT - cycles) >> 4;
  181. p->cpu_cycles = cycles;
  182. if (cycles > p->cpu_cycles_max) p->cpu_cycles_max = cycles;
  183. }
  184. }
  185. //digitalWriteFast(2, LOW);
  186. totalcycles = (ARM_DWT_CYCCNT - totalcycles) >> 4;;
  187. AudioStream::cpu_cycles_total = totalcycles;
  188. if (totalcycles > AudioStream::cpu_cycles_total_max)
  189. AudioStream::cpu_cycles_total_max = totalcycles;
  190. }