Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

play_sd_wav.cpp 14KB

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