You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

609 satır
16KB

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