Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

508 lines
13KB

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