Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

play_sd_wav.cpp 16KB

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