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.

544 lines
14KB

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