Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

play_sd_wav.cpp 14KB

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