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.

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