Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

451 lines
11KB

  1. #include "Audio.h"
  2. #include "arm_math.h"
  3. #include "utility/dspinst.h"
  4. #define STATE_DIRECT_8BIT_MONO 0 // playing mono at native sample rate
  5. #define STATE_DIRECT_8BIT_STEREO 1 // playing stereo at native sample rate
  6. #define STATE_DIRECT_16BIT_MONO 2 // playing mono at native sample rate
  7. #define STATE_DIRECT_16BIT_STEREO 3 // playing stereo at native sample rate
  8. #define STATE_CONVERT_8BIT_MONO 4 // playing mono, converting sample rate
  9. #define STATE_CONVERT_8BIT_STEREO 5 // playing stereo, converting sample rate
  10. #define STATE_CONVERT_16BIT_MONO 6 // playing mono, converting sample rate
  11. #define STATE_CONVERT_16BIT_STEREO 7 // playing stereo, converting sample rate
  12. #define STATE_PARSE1 8 // looking for 20 byte ID header
  13. #define STATE_PARSE2 9 // looking for 16 byte format header
  14. #define STATE_PARSE3 10 // looking for 8 byte data header
  15. #define STATE_PARSE4 11 // ignoring unknown chunk
  16. #define STATE_STOP 12
  17. void AudioPlaySDcardWAV::begin(void)
  18. {
  19. state = STATE_STOP;
  20. state_play = STATE_STOP;
  21. data_length = 0;
  22. if (block_left) {
  23. release(block_left);
  24. block_left = NULL;
  25. }
  26. if (block_right) {
  27. release(block_right);
  28. block_right = NULL;
  29. }
  30. }
  31. bool AudioPlaySDcardWAV::play(const char *filename)
  32. {
  33. stop();
  34. wavfile = SD.open(filename);
  35. if (!wavfile) return false;
  36. buffer_remaining = 0;
  37. state_play = STATE_STOP;
  38. data_length = 0;
  39. state = STATE_PARSE1;
  40. return true;
  41. }
  42. void AudioPlaySDcardWAV::stop(void)
  43. {
  44. __disable_irq();
  45. if (state != STATE_STOP) {
  46. audio_block_t *b1 = block_left;
  47. block_left = NULL;
  48. audio_block_t *b2 = block_right;
  49. block_right = NULL;
  50. state = STATE_STOP;
  51. __enable_irq();
  52. if (b1) release(b1);
  53. if (b2) release(b2);
  54. wavfile.close();
  55. } else {
  56. __enable_irq();
  57. }
  58. }
  59. bool AudioPlaySDcardWAV::start(void)
  60. {
  61. __disable_irq();
  62. if (state == STATE_STOP) {
  63. if (state_play == STATE_STOP) {
  64. __enable_irq();
  65. return false;
  66. }
  67. state = state_play;
  68. }
  69. __enable_irq();
  70. return true;
  71. }
  72. void AudioPlaySDcardWAV::update(void)
  73. {
  74. // only update if we're playing
  75. if (state == STATE_STOP) return;
  76. // allocate the audio blocks to transmit
  77. block_left = allocate();
  78. if (block_left == NULL) return;
  79. if (state < 8 && (state & 1) == 1) {
  80. // if we're playing stereo, allocate another
  81. // block for the right channel output
  82. block_right = allocate();
  83. if (block_right == NULL) {
  84. release(block_left);
  85. return;
  86. }
  87. } else {
  88. // if we're playing mono or just parsing
  89. // the WAV file header, no right-side block
  90. block_right = NULL;
  91. }
  92. block_offset = 0;
  93. //Serial.println("update");
  94. // is there buffered data?
  95. if (buffer_remaining > 0) {
  96. // we have buffered data
  97. if (consume()) return; // it was enough to transmit audio
  98. }
  99. // we only get to this point when buffer[512] is empty
  100. if (state != STATE_STOP && wavfile.available()) {
  101. // we can read more data from the file...
  102. buffer_remaining = wavfile.read(buffer, 512);
  103. if (consume()) {
  104. // good, it resulted in audio transmit
  105. return;
  106. } else {
  107. // not good, no audio was transmitted
  108. buffer_remaining = 0;
  109. if (block_left) {
  110. release(block_left);
  111. block_left = NULL;
  112. }
  113. if (block_right) {
  114. release(block_right);
  115. block_right = NULL;
  116. }
  117. // if we're still playing, well, there's going to
  118. // be a gap in output, but we can't keep burning
  119. // time trying to read more data. Hopefully things
  120. // will go better next time?
  121. if (state != STATE_STOP) return;
  122. }
  123. }
  124. // end of file reached or other reason to stop
  125. wavfile.close();
  126. if (block_left) {
  127. release(block_left);
  128. block_left = NULL;
  129. }
  130. if (block_right) {
  131. release(block_right);
  132. block_right = NULL;
  133. }
  134. state_play = STATE_STOP;
  135. state = STATE_STOP;
  136. }
  137. // https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
  138. // Consume already buffered data. Returns true if audio transmitted.
  139. bool AudioPlaySDcardWAV::consume(void)
  140. {
  141. uint32_t len, size;
  142. uint8_t lsb, msb;
  143. const uint8_t *p;
  144. size = buffer_remaining;
  145. p = buffer + 512 - size;
  146. start:
  147. if (size == 0) return false;
  148. //Serial.print("AudioPlaySDcardWAV write, size = ");
  149. //Serial.print(size);
  150. //Serial.print(", data_length = ");
  151. //Serial.print(data_length);
  152. //Serial.print(", state = ");
  153. //Serial.println(state);
  154. switch (state) {
  155. // parse wav file header, is this really a .wav file?
  156. case STATE_PARSE1:
  157. len = 20 - data_length;
  158. if (size < len) len = size;
  159. memcpy((uint8_t *)header + data_length, p, len);
  160. data_length += len;
  161. if (data_length < 20) return false;
  162. // parse the header...
  163. if (header[0] == 0x46464952 && header[2] == 0x45564157
  164. && header[3] == 0x20746D66 && header[4] == 16) {
  165. //Serial.println("header ok");
  166. state = STATE_PARSE2;
  167. p += len;
  168. size -= len;
  169. data_length = 0;
  170. goto start;
  171. }
  172. //Serial.println("unknown WAV header");
  173. break;
  174. // check & extract key audio parameters
  175. case STATE_PARSE2:
  176. len = 16 - data_length;
  177. if (size < len) len = size;
  178. memcpy((uint8_t *)header + data_length, p, len);
  179. data_length += len;
  180. if (data_length < 16) return false;
  181. if (parse_format()) {
  182. //Serial.println("audio format ok");
  183. p += len;
  184. size -= len;
  185. data_length = 0;
  186. state = STATE_PARSE3;
  187. goto start;
  188. }
  189. //Serial.println("unknown audio format");
  190. break;
  191. // find the data chunk
  192. case STATE_PARSE3:
  193. len = 8 - data_length;
  194. if (size < len) len = size;
  195. memcpy((uint8_t *)header + data_length, p, len);
  196. data_length += len;
  197. if (data_length < 8) return false;
  198. //Serial.print("chunk id = ");
  199. //Serial.print(header[0], HEX);
  200. //Serial.print(", length = ");
  201. //Serial.println(header[1]);
  202. p += len;
  203. size -= len;
  204. data_length = header[1];
  205. if (header[0] == 0x61746164) {
  206. //Serial.println("found data chunk");
  207. // TODO: verify offset in file is an even number
  208. // as required by WAV format. abort if odd. Code
  209. // below will depend upon this and fail if not even.
  210. leftover_bytes = 0;
  211. state = state_play;
  212. if (state & 1) {
  213. // if we're going to start stereo
  214. // better allocate another output block
  215. block_right = allocate();
  216. if (!block_right) return false;
  217. }
  218. } else {
  219. state = STATE_PARSE4;
  220. }
  221. goto start;
  222. // ignore any extra unknown chunks (title & artist info)
  223. case STATE_PARSE4:
  224. if (size < data_length) {
  225. data_length -= size;
  226. return false;
  227. }
  228. p += data_length;
  229. size -= data_length;
  230. data_length = 0;
  231. state = STATE_PARSE3;
  232. goto start;
  233. // playing mono at native sample rate
  234. case STATE_DIRECT_8BIT_MONO:
  235. return false;
  236. // playing stereo at native sample rate
  237. case STATE_DIRECT_8BIT_STEREO:
  238. return false;
  239. // playing mono at native sample rate
  240. case STATE_DIRECT_16BIT_MONO:
  241. if (size > data_length) size = data_length;
  242. data_length -= size;
  243. while (1) {
  244. lsb = *p++;
  245. msb = *p++;
  246. size -= 2;
  247. block_left->data[block_offset++] = (msb << 8) | lsb;
  248. if (block_offset >= AUDIO_BLOCK_SAMPLES) {
  249. transmit(block_left, 0);
  250. transmit(block_left, 1);
  251. //Serial1.print('%');
  252. //delayMicroseconds(90);
  253. release(block_left);
  254. block_left = NULL;
  255. data_length += size;
  256. buffer_remaining = size;
  257. if (block_right) release(block_right);
  258. return true;
  259. }
  260. if (size == 0) {
  261. if (data_length == 0) break;
  262. return false;
  263. }
  264. }
  265. // end of file reached
  266. if (block_offset > 0) {
  267. // TODO: fill remainder of last block with zero and transmit
  268. }
  269. state = STATE_STOP;
  270. return false;
  271. // playing stereo at native sample rate
  272. case STATE_DIRECT_16BIT_STEREO:
  273. if (size > data_length) size = data_length;
  274. data_length -= size;
  275. if (leftover_bytes) {
  276. block_left->data[block_offset] = header[0];
  277. goto right16;
  278. }
  279. while (1) {
  280. lsb = *p++;
  281. msb = *p++;
  282. size -= 2;
  283. if (size == 0) {
  284. if (data_length == 0) break;
  285. header[0] = (msb << 8) | lsb;
  286. leftover_bytes = 2;
  287. return false;
  288. }
  289. block_left->data[block_offset] = (msb << 8) | lsb;
  290. right16:
  291. lsb = *p++;
  292. msb = *p++;
  293. size -= 2;
  294. block_right->data[block_offset++] = (msb << 8) | lsb;
  295. if (block_offset >= AUDIO_BLOCK_SAMPLES) {
  296. transmit(block_left, 0);
  297. release(block_left);
  298. block_left = NULL;
  299. transmit(block_right, 1);
  300. release(block_right);
  301. block_right = NULL;
  302. data_length += size;
  303. buffer_remaining = size;
  304. return true;
  305. }
  306. if (size == 0) {
  307. if (data_length == 0) break;
  308. leftover_bytes = 0;
  309. return false;
  310. }
  311. }
  312. // end of file reached
  313. if (block_offset > 0) {
  314. // TODO: fill remainder of last block with zero and transmit
  315. }
  316. state = STATE_STOP;
  317. return false;
  318. // playing mono, converting sample rate
  319. case STATE_CONVERT_8BIT_MONO :
  320. return false;
  321. // playing stereo, converting sample rate
  322. case STATE_CONVERT_8BIT_STEREO:
  323. return false;
  324. // playing mono, converting sample rate
  325. case STATE_CONVERT_16BIT_MONO:
  326. return false;
  327. // playing stereo, converting sample rate
  328. case STATE_CONVERT_16BIT_STEREO:
  329. return false;
  330. // ignore any extra data after playing
  331. // or anything following any error
  332. case STATE_STOP:
  333. return false;
  334. // this is not supposed to happen!
  335. //default:
  336. //Serial.println("AudioPlaySDcardWAV, unknown state");
  337. }
  338. state_play = STATE_STOP;
  339. state = STATE_STOP;
  340. return false;
  341. }
  342. /*
  343. 00000000 52494646 66EA6903 57415645 666D7420 RIFFf.i.WAVEfmt
  344. 00000010 10000000 01000200 44AC0000 10B10200 ........D.......
  345. 00000020 04001000 4C495354 3A000000 494E464F ....LIST:...INFO
  346. 00000030 494E414D 14000000 49205761 6E742054 INAM....I Want T
  347. 00000040 6F20436F 6D65204F 76657200 49415254 o Come Over.IART
  348. 00000050 12000000 4D656C69 73736120 45746865 ....Melissa Ethe
  349. 00000060 72696467 65006461 746100EA 69030100 ridge.data..i...
  350. 00000070 FEFF0300 FCFF0400 FDFF0200 0000FEFF ................
  351. 00000080 0300FDFF 0200FFFF 00000100 FEFF0300 ................
  352. 00000090 FDFF0300 FDFF0200 FFFF0100 0000FFFF ................
  353. */
  354. // SD library on Teensy3 at 96 MHz
  355. // 256 byte chunks, speed is 443272 bytes/sec
  356. // 512 byte chunks, speed is 468023 bytes/sec
  357. bool AudioPlaySDcardWAV::parse_format(void)
  358. {
  359. uint8_t num = 0;
  360. uint16_t format;
  361. uint16_t channels;
  362. uint32_t rate;
  363. uint16_t bits;
  364. format = header[0];
  365. //Serial.print(" format = ");
  366. //Serial.println(format);
  367. if (format != 1) return false;
  368. channels = header[0] >> 16;
  369. //Serial.print(" channels = ");
  370. //Serial.println(channels);
  371. if (channels == 1) {
  372. } else if (channels == 2) {
  373. num = 1;
  374. } else {
  375. return false;
  376. }
  377. bits = header[3] >> 16;
  378. //Serial.print(" bits = ");
  379. //Serial.println(bits);
  380. if (bits == 8) {
  381. } else if (bits == 16) {
  382. num |= 2;
  383. } else {
  384. return false;
  385. }
  386. rate = header[1];
  387. //Serial.print(" rate = ");
  388. //Serial.println(rate);
  389. if (rate == AUDIO_SAMPLE_RATE) {
  390. } else if (rate >= 8000 && rate <= 48000) {
  391. num |= 4;
  392. } else {
  393. return false;
  394. }
  395. // we're not checking the byte rate and block align fields
  396. // if they're not the expected values, all we could do is
  397. // return false. Do any real wav files have unexpected
  398. // values in these other fields?
  399. state_play = num;
  400. return true;
  401. }