PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pirms 3 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. #include <Arduino.h>
  27. #include "play_sd_wav.h"
  28. #include "spi_interrupt.h"
  29. #define STATE_DIRECT_8BIT_MONO 0 // playing mono at native sample rate
  30. #define STATE_DIRECT_8BIT_STEREO 1 // playing stereo at native sample rate
  31. #define STATE_DIRECT_16BIT_MONO 2 // playing mono at native sample rate
  32. #define STATE_DIRECT_16BIT_STEREO 3 // playing stereo at native sample rate
  33. #define STATE_CONVERT_8BIT_MONO 4 // playing mono, converting sample rate
  34. #define STATE_CONVERT_8BIT_STEREO 5 // playing stereo, converting sample rate
  35. #define STATE_CONVERT_16BIT_MONO 6 // playing mono, converting sample rate
  36. #define STATE_CONVERT_16BIT_STEREO 7 // playing stereo, converting sample rate
  37. #define STATE_PARSE1 8 // looking for 20 byte ID header
  38. #define STATE_PARSE2 9 // looking for 16 byte format header
  39. #define STATE_PARSE3 10 // looking for 8 byte data header
  40. #define STATE_PARSE4 11 // ignoring unknown chunk after "fmt "
  41. #define STATE_PARSE5 12 // ignoring unknown chunk before "fmt "
  42. #define STATE_STOP 13
  43. void AudioPlaySdWav::begin(void)
  44. {
  45. state = STATE_STOP;
  46. state_play = STATE_STOP;
  47. data_length = 0;
  48. if (block_left) {
  49. release(block_left);
  50. block_left = NULL;
  51. }
  52. if (block_right) {
  53. release(block_right);
  54. block_right = NULL;
  55. }
  56. }
  57. bool AudioPlaySdWav::play(const char *filename)
  58. {
  59. stop();
  60. #if defined(HAS_KINETIS_SDHC)
  61. if (!(SIM_SCGC3 & SIM_SCGC3_SDHC)) AudioStartUsingSPI();
  62. #else
  63. AudioStartUsingSPI();
  64. #endif
  65. __disable_irq();
  66. wavfile = SD.open(filename);
  67. __enable_irq();
  68. if (!wavfile) {
  69. #if defined(HAS_KINETIS_SDHC)
  70. if (!(SIM_SCGC3 & SIM_SCGC3_SDHC)) AudioStopUsingSPI();
  71. #else
  72. AudioStopUsingSPI();
  73. #endif
  74. return false;
  75. }
  76. buffer_length = 0;
  77. buffer_offset = 0;
  78. state_play = STATE_STOP;
  79. data_length = 20;
  80. header_offset = 0;
  81. state = STATE_PARSE1;
  82. return true;
  83. }
  84. void AudioPlaySdWav::stop(void)
  85. {
  86. __disable_irq();
  87. if (state != STATE_STOP) {
  88. audio_block_t *b1 = block_left;
  89. block_left = NULL;
  90. audio_block_t *b2 = block_right;
  91. block_right = NULL;
  92. state = STATE_STOP;
  93. __enable_irq();
  94. if (b1) release(b1);
  95. if (b2) release(b2);
  96. wavfile.close();
  97. #if defined(HAS_KINETIS_SDHC)
  98. if (!(SIM_SCGC3 & SIM_SCGC3_SDHC)) AudioStopUsingSPI();
  99. #else
  100. AudioStopUsingSPI();
  101. #endif
  102. } else {
  103. __enable_irq();
  104. }
  105. }
  106. void AudioPlaySdWav::update(void)
  107. {
  108. int32_t n;
  109. // only update if we're playing
  110. if (state == STATE_STOP) return;
  111. // allocate the audio blocks to transmit
  112. block_left = allocate();
  113. if (block_left == NULL) return;
  114. if (state < 8 && (state & 1) == 1) {
  115. // if we're playing stereo, allocate another
  116. // block for the right channel output
  117. block_right = allocate();
  118. if (block_right == NULL) {
  119. release(block_left);
  120. return;
  121. }
  122. } else {
  123. // if we're playing mono or just parsing
  124. // the WAV file header, no right-side block
  125. block_right = NULL;
  126. }
  127. block_offset = 0;
  128. //Serial.println("update");
  129. // is there buffered data?
  130. n = buffer_length - buffer_offset;
  131. if (n > 0) {
  132. // we have buffered data
  133. if (consume(n)) return; // it was enough to transmit audio
  134. }
  135. // we only get to this point when buffer[512] is empty
  136. if (state != STATE_STOP && wavfile.available()) {
  137. // we can read more data from the file...
  138. readagain:
  139. buffer_length = wavfile.read(buffer, 512);
  140. if (buffer_length == 0) goto end;
  141. buffer_offset = 0;
  142. bool parsing = (state >= 8);
  143. bool txok = consume(buffer_length);
  144. if (txok) {
  145. if (state != STATE_STOP) return;
  146. } else {
  147. if (state != STATE_STOP) {
  148. if (parsing && state < 8) goto readagain;
  149. else goto cleanup;
  150. }
  151. }
  152. }
  153. end: // end of file reached or other reason to stop
  154. wavfile.close();
  155. #if defined(HAS_KINETIS_SDHC)
  156. if (!(SIM_SCGC3 & SIM_SCGC3_SDHC)) AudioStopUsingSPI();
  157. #else
  158. AudioStopUsingSPI();
  159. #endif
  160. state_play = STATE_STOP;
  161. state = STATE_STOP;
  162. cleanup:
  163. if (block_left) {
  164. if (block_offset > 0) {
  165. for (uint32_t i=block_offset; i < AUDIO_BLOCK_SAMPLES; i++) {
  166. block_left->data[i] = 0;
  167. }
  168. transmit(block_left, 0);
  169. if (state < 8 && (state & 1) == 0) {
  170. transmit(block_left, 1);
  171. }
  172. }
  173. release(block_left);
  174. block_left = NULL;
  175. }
  176. if (block_right) {
  177. if (block_offset > 0) {
  178. for (uint32_t i=block_offset; i < AUDIO_BLOCK_SAMPLES; i++) {
  179. block_right->data[i] = 0;
  180. }
  181. transmit(block_right, 1);
  182. }
  183. release(block_right);
  184. block_right = NULL;
  185. }
  186. }
  187. // https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
  188. // Consume already buffered data. Returns true if audio transmitted.
  189. bool AudioPlaySdWav::consume(uint32_t size)
  190. {
  191. uint32_t len;
  192. uint8_t lsb, msb;
  193. const uint8_t *p;
  194. p = buffer + buffer_offset;
  195. start:
  196. if (size == 0) return false;
  197. #if 0
  198. Serial.print("AudioPlaySdWav consume, ");
  199. Serial.print("size = ");
  200. Serial.print(size);
  201. Serial.print(", buffer_offset = ");
  202. Serial.print(buffer_offset);
  203. Serial.print(", data_length = ");
  204. Serial.print(data_length);
  205. Serial.print(", space = ");
  206. Serial.print((AUDIO_BLOCK_SAMPLES - block_offset) * 2);
  207. Serial.print(", state = ");
  208. Serial.println(state);
  209. #endif
  210. switch (state) {
  211. // parse wav file header, is this really a .wav file?
  212. case STATE_PARSE1:
  213. len = data_length;
  214. if (size < len) len = size;
  215. memcpy((uint8_t *)header + header_offset, p, len);
  216. header_offset += len;
  217. buffer_offset += len;
  218. data_length -= len;
  219. if (data_length > 0) return false;
  220. // parse the header...
  221. if (header[0] == 0x46464952 && header[2] == 0x45564157) {
  222. //Serial.println("is wav file");
  223. if (header[3] == 0x20746D66) {
  224. // "fmt " header
  225. if (header[4] < 16) {
  226. // WAV "fmt " info must be at least 16 bytes
  227. break;
  228. }
  229. if (header[4] > sizeof(header)) {
  230. // if such .wav files exist, increasing the
  231. // size of header[] should accomodate them...
  232. //Serial.println("WAVEFORMATEXTENSIBLE too long");
  233. break;
  234. }
  235. //Serial.println("header ok");
  236. header_offset = 0;
  237. state = STATE_PARSE2;
  238. } else {
  239. // first chuck is something other than "fmt "
  240. //Serial.print("skipping \"");
  241. //Serial.printf("\" (%08X), ", __builtin_bswap32(header[3]));
  242. //Serial.print(header[4]);
  243. //Serial.println(" bytes");
  244. header_offset = 12;
  245. state = STATE_PARSE5;
  246. }
  247. p += len;
  248. size -= len;
  249. data_length = header[4];
  250. goto start;
  251. }
  252. //Serial.println("unknown WAV header");
  253. break;
  254. // check & extract key audio parameters
  255. case STATE_PARSE2:
  256. len = data_length;
  257. if (size < len) len = size;
  258. memcpy((uint8_t *)header + header_offset, p, len);
  259. header_offset += len;
  260. buffer_offset += len;
  261. data_length -= len;
  262. if (data_length > 0) return false;
  263. if (parse_format()) {
  264. //Serial.println("audio format ok");
  265. p += len;
  266. size -= len;
  267. data_length = 8;
  268. header_offset = 0;
  269. state = STATE_PARSE3;
  270. goto start;
  271. }
  272. //Serial.println("unknown audio format");
  273. break;
  274. // find the data chunk
  275. case STATE_PARSE3: // 10
  276. len = data_length;
  277. if (size < len) len = size;
  278. memcpy((uint8_t *)header + header_offset, p, len);
  279. header_offset += len;
  280. buffer_offset += len;
  281. data_length -= len;
  282. if (data_length > 0) return false;
  283. //Serial.print("chunk id = ");
  284. //Serial.print(header[0], HEX);
  285. //Serial.print(", length = ");
  286. //Serial.println(header[1]);
  287. p += len;
  288. size -= len;
  289. data_length = header[1];
  290. if (header[0] == 0x61746164) {
  291. //Serial.print("wav: found data chunk, len=");
  292. //Serial.println(data_length);
  293. // TODO: verify offset in file is an even number
  294. // as required by WAV format. abort if odd. Code
  295. // below will depend upon this and fail if not even.
  296. leftover_bytes = 0;
  297. state = state_play;
  298. if (state & 1) {
  299. // if we're going to start stereo
  300. // better allocate another output block
  301. block_right = allocate();
  302. if (!block_right) return false;
  303. }
  304. total_length = data_length;
  305. } else {
  306. state = STATE_PARSE4;
  307. }
  308. goto start;
  309. // ignore any extra unknown chunks (title & artist info)
  310. case STATE_PARSE4: // 11
  311. if (size < data_length) {
  312. data_length -= size;
  313. buffer_offset += size;
  314. return false;
  315. }
  316. p += data_length;
  317. size -= data_length;
  318. buffer_offset += data_length;
  319. data_length = 8;
  320. header_offset = 0;
  321. state = STATE_PARSE3;
  322. //Serial.println("consumed unknown chunk");
  323. goto start;
  324. // skip past "junk" data before "fmt " header
  325. case STATE_PARSE5:
  326. len = data_length;
  327. if (size < len) len = size;
  328. buffer_offset += len;
  329. data_length -= len;
  330. if (data_length > 0) return false;
  331. p += len;
  332. size -= len;
  333. data_length = 8;
  334. state = STATE_PARSE1;
  335. goto start;
  336. // playing mono at native sample rate
  337. case STATE_DIRECT_8BIT_MONO:
  338. return false;
  339. // playing stereo at native sample rate
  340. case STATE_DIRECT_8BIT_STEREO:
  341. return false;
  342. // playing mono at native sample rate
  343. case STATE_DIRECT_16BIT_MONO:
  344. if (size > data_length) size = data_length;
  345. data_length -= size;
  346. while (1) {
  347. lsb = *p++;
  348. msb = *p++;
  349. size -= 2;
  350. block_left->data[block_offset++] = (msb << 8) | lsb;
  351. if (block_offset >= AUDIO_BLOCK_SAMPLES) {
  352. transmit(block_left, 0);
  353. transmit(block_left, 1);
  354. release(block_left);
  355. block_left = NULL;
  356. data_length += size;
  357. buffer_offset = p - buffer;
  358. if (block_right) release(block_right);
  359. if (data_length == 0) state = STATE_STOP;
  360. return true;
  361. }
  362. if (size == 0) {
  363. if (data_length == 0) break;
  364. return false;
  365. }
  366. }
  367. //Serial.println("end of file reached");
  368. // end of file reached
  369. if (block_offset > 0) {
  370. // TODO: fill remainder of last block with zero and transmit
  371. }
  372. state = STATE_STOP;
  373. return false;
  374. // playing stereo at native sample rate
  375. case STATE_DIRECT_16BIT_STEREO:
  376. if (size > data_length) size = data_length;
  377. data_length -= size;
  378. if (leftover_bytes) {
  379. block_left->data[block_offset] = header[0];
  380. //PAH fix problem with left+right channels being swapped
  381. leftover_bytes = 0;
  382. goto right16;
  383. }
  384. while (1) {
  385. lsb = *p++;
  386. msb = *p++;
  387. size -= 2;
  388. if (size == 0) {
  389. if (data_length == 0) break;
  390. header[0] = (msb << 8) | lsb;
  391. leftover_bytes = 2;
  392. return false;
  393. }
  394. block_left->data[block_offset] = (msb << 8) | lsb;
  395. right16:
  396. lsb = *p++;
  397. msb = *p++;
  398. size -= 2;
  399. block_right->data[block_offset++] = (msb << 8) | lsb;
  400. if (block_offset >= AUDIO_BLOCK_SAMPLES) {
  401. transmit(block_left, 0);
  402. release(block_left);
  403. block_left = NULL;
  404. transmit(block_right, 1);
  405. release(block_right);
  406. block_right = NULL;
  407. data_length += size;
  408. buffer_offset = p - buffer;
  409. if (data_length == 0) state = STATE_STOP;
  410. return true;
  411. }
  412. if (size == 0) {
  413. if (data_length == 0) break;
  414. leftover_bytes = 0;
  415. return false;
  416. }
  417. }
  418. // end of file reached
  419. if (block_offset > 0) {
  420. // TODO: fill remainder of last block with zero and transmit
  421. }
  422. state = STATE_STOP;
  423. return false;
  424. // playing mono, converting sample rate
  425. case STATE_CONVERT_8BIT_MONO :
  426. return false;
  427. // playing stereo, converting sample rate
  428. case STATE_CONVERT_8BIT_STEREO:
  429. return false;
  430. // playing mono, converting sample rate
  431. case STATE_CONVERT_16BIT_MONO:
  432. return false;
  433. // playing stereo, converting sample rate
  434. case STATE_CONVERT_16BIT_STEREO:
  435. return false;
  436. // ignore any extra data after playing
  437. // or anything following any error
  438. case STATE_STOP:
  439. return false;
  440. // this is not supposed to happen!
  441. //default:
  442. //Serial.println("AudioPlaySdWav, unknown state");
  443. }
  444. state_play = STATE_STOP;
  445. state = STATE_STOP;
  446. return false;
  447. }
  448. /*
  449. 00000000 52494646 66EA6903 57415645 666D7420 RIFFf.i.WAVEfmt
  450. 00000010 10000000 01000200 44AC0000 10B10200 ........D.......
  451. 00000020 04001000 4C495354 3A000000 494E464F ....LIST:...INFO
  452. 00000030 494E414D 14000000 49205761 6E742054 INAM....I Want T
  453. 00000040 6F20436F 6D65204F 76657200 49415254 o Come Over.IART
  454. 00000050 12000000 4D656C69 73736120 45746865 ....Melissa Ethe
  455. 00000060 72696467 65006461 746100EA 69030100 ridge.data..i...
  456. 00000070 FEFF0300 FCFF0400 FDFF0200 0000FEFF ................
  457. 00000080 0300FDFF 0200FFFF 00000100 FEFF0300 ................
  458. 00000090 FDFF0300 FDFF0200 FFFF0100 0000FFFF ................
  459. */
  460. // SD library on Teensy3 at 96 MHz
  461. // 256 byte chunks, speed is 443272 bytes/sec
  462. // 512 byte chunks, speed is 468023 bytes/sec
  463. #define B2M_44100 (uint32_t)((double)4294967296000.0 / AUDIO_SAMPLE_RATE_EXACT) // 97352592
  464. #define B2M_22050 (uint32_t)((double)4294967296000.0 / AUDIO_SAMPLE_RATE_EXACT * 2.0)
  465. #define B2M_11025 (uint32_t)((double)4294967296000.0 / AUDIO_SAMPLE_RATE_EXACT * 4.0)
  466. bool AudioPlaySdWav::parse_format(void)
  467. {
  468. uint8_t num = 0;
  469. uint16_t format;
  470. uint16_t channels;
  471. uint32_t rate, b2m;
  472. uint16_t bits;
  473. format = header[0];
  474. //Serial.print(" format = ");
  475. //Serial.println(format);
  476. if (format != 1) return false;
  477. rate = header[1];
  478. //Serial.print(" rate = ");
  479. //Serial.println(rate);
  480. if (rate == 44100) {
  481. b2m = B2M_44100;
  482. } else if (rate == 22050) {
  483. b2m = B2M_22050;
  484. num |= 4;
  485. } else if (rate == 11025) {
  486. b2m = B2M_11025;
  487. num |= 4;
  488. } else {
  489. return false;
  490. }
  491. channels = header[0] >> 16;
  492. //Serial.print(" channels = ");
  493. //Serial.println(channels);
  494. if (channels == 1) {
  495. } else if (channels == 2) {
  496. b2m >>= 1;
  497. num |= 1;
  498. } else {
  499. return false;
  500. }
  501. bits = header[3] >> 16;
  502. //Serial.print(" bits = ");
  503. //Serial.println(bits);
  504. if (bits == 8) {
  505. } else if (bits == 16) {
  506. b2m >>= 1;
  507. num |= 2;
  508. } else {
  509. return false;
  510. }
  511. bytes2millis = b2m;
  512. //Serial.print(" bytes2millis = ");
  513. //Serial.println(b2m);
  514. // we're not checking the byte rate and block align fields
  515. // if they're not the expected values, all we could do is
  516. // return false. Do any real wav files have unexpected
  517. // values in these other fields?
  518. state_play = num;
  519. return true;
  520. }
  521. bool AudioPlaySdWav::isPlaying(void)
  522. {
  523. uint8_t s = *(volatile uint8_t *)&state;
  524. return (s < 8);
  525. }
  526. uint32_t AudioPlaySdWav::positionMillis(void)
  527. {
  528. uint8_t s = *(volatile uint8_t *)&state;
  529. if (s >= 8) return 0;
  530. uint32_t tlength = *(volatile uint32_t *)&total_length;
  531. uint32_t dlength = *(volatile uint32_t *)&data_length;
  532. uint32_t offset = tlength - dlength;
  533. uint32_t b2m = *(volatile uint32_t *)&bytes2millis;
  534. return ((uint64_t)offset * b2m) >> 32;
  535. }
  536. uint32_t AudioPlaySdWav::lengthMillis(void)
  537. {
  538. uint8_t s = *(volatile uint8_t *)&state;
  539. if (s >= 8) return 0;
  540. uint32_t tlength = *(volatile uint32_t *)&total_length;
  541. uint32_t b2m = *(volatile uint32_t *)&bytes2millis;
  542. return ((uint64_t)tlength * b2m) >> 32;
  543. }