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.

447 Zeilen
11KB

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