Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

play_sd_wav.cpp 14KB

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