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.

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