您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

play_sd_wav.cpp 15KB

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