LibDriver WM8978
Loading...
Searching...
No Matches
wav_player.c
Go to the documentation of this file.
1
36
37#include "wav_player.h"
38
47static uint8_t a_wav_write_wav_header(wav_handle_t *handle)
48{
49 uint8_t res;
50 uint8_t buf[44];
51
52 buf[0] = handle->wav.chunk_id[0]; /* R */
53 buf[1] = handle->wav.chunk_id[1]; /* I */
54 buf[2] = handle->wav.chunk_id[2]; /* F */
55 buf[3] = handle->wav.chunk_id[3]; /* F */
56 buf[4] = (handle->wav.chunk_size >> 0) & 0xFF; /* set chunk size */
57 buf[5] = (handle->wav.chunk_size >> 8) & 0xFF; /* set chunk size */
58 buf[6] = (handle->wav.chunk_size >> 16) & 0xFF; /* set chunk size */
59 buf[7] = (handle->wav.chunk_size >> 24) & 0xFF; /* set chunk size */
60 buf[8] = handle->wav.format[0]; /* W */
61 buf[9] = handle->wav.format[1]; /* A */
62 buf[10] = handle->wav.format[2]; /* V */
63 buf[11] = handle->wav.format[3]; /* E */
64 buf[12] = handle->wav.sub_chunk1_id[0]; /* f */
65 buf[13] = handle->wav.sub_chunk1_id[1]; /* m */
66 buf[14] = handle->wav.sub_chunk1_id[2]; /* t */
67 buf[15] = handle->wav.sub_chunk1_id[3]; /* 0x00 */
68 buf[16] = (handle->wav.sub_chunk1_size >> 0) & 0xFF; /* 16 bytes */
69 buf[17] = (handle->wav.sub_chunk1_size >> 8) & 0xFF; /* 16 bytes */
70 buf[18] = (handle->wav.sub_chunk1_size >> 16) & 0xFF; /* 16 bytes */
71 buf[19] = (handle->wav.sub_chunk1_size >> 24) & 0xFF; /* 16 bytes */
72 buf[20] = (handle->wav.audio_format >> 0) & 0xFF; /* pcm */
73 buf[21] = (handle->wav.audio_format >> 8) & 0xFF; /* pcm */
74 buf[22] = (handle->wav.num_channel >> 0) & 0xFF; /* 1 channel */
75 buf[23] = (handle->wav.num_channel >> 8) & 0xFF; /* 1 channel */
76 buf[24] = (handle->wav.sample_rate >> 0) & 0xFF; /* set sample rate */
77 buf[25] = (handle->wav.sample_rate >> 8) & 0xFF; /* set sample rate */
78 buf[26] = (handle->wav.sample_rate >> 16) & 0xFF; /* set sample rate */
79 buf[27] = (handle->wav.sample_rate >> 24) & 0xFF; /* set sample rate */
80 buf[28] = (handle->wav.byte_rate >> 0) & 0xFF; /* double */
81 buf[29] = (handle->wav.byte_rate >> 8) & 0xFF; /* double */
82 buf[30] = (handle->wav.byte_rate >> 16) & 0xFF; /* double */
83 buf[31] = (handle->wav.byte_rate >> 24) & 0xFF; /* double */
84 buf[32] = (handle->wav.block_align >> 0) & 0xFF; /* 2 bytes */
85 buf[33] = (handle->wav.block_align >> 8) & 0xFF; /* 2 bytes */
86 buf[34] = (handle->wav.bit_per_sample >> 0) & 0xFF; /* 16 bits */
87 buf[35] = (handle->wav.bit_per_sample >> 8) & 0xFF; /* 16 bits */
88 buf[36] = handle->wav.sub_chunk2_id[0]; /* d */
89 buf[37] = handle->wav.sub_chunk2_id[1]; /* a */
90 buf[38] = handle->wav.sub_chunk2_id[2]; /* t */
91 buf[39] = handle->wav.sub_chunk2_id[3]; /* a */
92 buf[40] = (handle->wav.sub_chunk2_size >> 0) & 0xFF; /* init 0 */
93 buf[41] = (handle->wav.sub_chunk2_size >> 8) & 0xFF; /* init 0 */
94 buf[42] = (handle->wav.sub_chunk2_size >> 16) & 0xFF; /* init 0 */
95 buf[43] = (handle->wav.sub_chunk2_size >> 24) & 0xFF; /* init 0 */
96 res = handle->audio_write(0, 44, buf); /* write buffer */
97 if (res != 0) /* check result */
98 {
99 handle->debug_print("wav: audio write failed.\n"); /* audio write failed */
100
101 return 1; /* return error */
102 }
103 handle->pos += 44; /* add 44 offset */
104
105 return 0; /* success return 0 */
106}
107
117uint8_t wav_init(wav_handle_t *handle)
118{
119 if (handle == NULL) /* check handle */
120 {
121 return 2; /* return error */
122 }
123 if (handle->debug_print == NULL) /* check debug_print */
124 {
125 return 3; /* return error */
126 }
127 if (handle->iis_init == NULL) /* check iis_init */
128 {
129 handle->debug_print("wav: iis_init is null.\n"); /* iis_init is null */
130
131 return 3; /* return error */
132 }
133 if (handle->iis_deinit == NULL) /* check iis_deinit */
134 {
135 handle->debug_print("wav: iis_deinit is null.\n"); /* iis_deinit is null */
136
137 return 3; /* return error */
138 }
139 if (handle->iis_stop == NULL) /* check iis_stop */
140 {
141 handle->debug_print("wav: iis_stop is null.\n"); /* iis_stop is null */
142
143 return 3; /* return error */
144 }
145 if (handle->iis_pause == NULL) /* check iis_pause */
146 {
147 handle->debug_print("wav: iis_pause is null.\n"); /* iis_pause is null */
148
149 return 3; /* return error */
150 }
151 if (handle->iis_resume == NULL) /* check iis_resume */
152 {
153 handle->debug_print("wav: iis_resume is null.\n"); /* iis_resume is null */
154
155 return 3; /* return error */
156 }
157 if (handle->iis_set_freq == NULL) /* check iis_set_freq */
158 {
159 handle->debug_print("wav: iis_set_freq is null.\n"); /* iis_set_freq is null */
160
161 return 3; /* return error */
162 }
163 if (handle->iis_write == NULL) /* check iis_write */
164 {
165 handle->debug_print("wav: iis_write is null.\n"); /* iis_write is null */
166
167 return 3; /* return error */
168 }
169 if (handle->iis_read == NULL) /* check iis_read */
170 {
171 handle->debug_print("wav: iis_read is null.\n"); /* iis_read is null */
172
173 return 3; /* return error */
174 }
175 if (handle->audio_init == NULL) /* check audio_init */
176 {
177 handle->debug_print("wav: audio_init is null.\n"); /* audio_init is null */
178
179 return 3; /* return error */
180 }
181 if (handle->audio_deinit == NULL) /* check audio_deinit */
182 {
183 handle->debug_print("wav: audio_deinit is null.\n"); /* audio_deinit is null */
184
185 return 3; /* return error */
186 }
187 if (handle->audio_read == NULL) /* check audio_read */
188 {
189 handle->debug_print("wav: audio_read is null.\n"); /* audio_read is null */
190
191 return 3; /* return error */
192 }
193 if (handle->audio_write == NULL) /* check audio_write */
194 {
195 handle->debug_print("wav: audio_write is null.\n"); /* audio_write is null */
196
197 return 3; /* return error */
198 }
199 if (handle->delay_ms == NULL) /* check delay_ms */
200 {
201 handle->debug_print("wav: delay_ms is null.\n"); /* delay_ms is null */
202
203 return 3; /* return error */
204 }
205
206 handle->inited = 1; /* flag inited */
207
208 return 0; /* success return 0 */
209}
210
221uint8_t wav_deinit(wav_handle_t *handle)
222{
223 if (handle == NULL) /* check handle */
224 {
225 return 2; /* return error */
226 }
227 if (handle->inited != 1) /* check handle initialization */
228 {
229 return 3; /* return error */
230 }
231
232 handle->inited = 0; /* flag closed */
233
234 return 0; /* success return 0 */
235}
236
249uint8_t wav_player_init(wav_handle_t *handle, char *path)
250{
251 uint8_t buf[72];
252
253 if (handle == NULL) /* check handle */
254 {
255 return 2; /* return error */
256 }
257 if (handle->inited != 1) /* check handle initialization */
258 {
259 return 3; /* return error */
260 }
261
262 handle->pos = 0; /* init 0 */
263 if (handle->audio_init(0, path, &handle->size) != 0) /* audio init */
264 {
265 handle->debug_print("wav: player failed.\n"); /* player failed */
266
267 return 1; /* return error */
268 }
269 if (handle->audio_read(handle->pos, 72, buf) != 0) /* read header */
270 {
271 handle->debug_print("wav: read failed.\n"); /* read failed */
272
273 return 1; /* return error */
274 }
275 handle->wav.chunk_id[0] = buf[0]; /* set chunk id 0 */
276 handle->wav.chunk_id[1] = buf[1]; /* set chunk id 1 */
277 handle->wav.chunk_id[2] = buf[2]; /* set chunk id 2 */
278 handle->wav.chunk_id[3] = buf[3]; /* set chunk id 3 */
279 handle->wav.chunk_size = buf[4] |
280 ((uint32_t)(buf[5]) << 8) |
281 ((uint32_t)(buf[6]) << 16) |
282 ((uint32_t)(buf[7]) << 24); /* set chunk size */
283 handle->wav.format[0] = buf[8]; /* set format 0 */
284 handle->wav.format[1] = buf[9]; /* set format 1 */
285 handle->wav.format[2] = buf[10]; /* set format 2 */
286 handle->wav.format[3] = buf[11]; /* set format 3 */
287 handle->wav.sub_chunk1_id[0] = buf[12]; /* set sub chunk1 id 0 */
288 handle->wav.sub_chunk1_id[1] = buf[13]; /* set sub chunk1 id 1 */
289 handle->wav.sub_chunk1_id[2] = buf[14]; /* set sub chunk1 id 2 */
290 handle->wav.sub_chunk1_id[3] = buf[15]; /* set sub chunk1 id 3 */
291 handle->wav.sub_chunk1_size = buf[16] |
292 ((uint32_t)(buf[17]) << 8) |
293 ((uint32_t)(buf[18]) << 16) |
294 ((uint32_t)(buf[19]) << 24); /* set sub chunk1 size */
295 handle->wav.audio_format = buf[20] |
296 ((uint16_t)(buf[21]) << 8); /* set audio format */
297 handle->wav.num_channel = buf[22] |
298 ((uint16_t)(buf[23]) << 8); /* set num channel */
299 handle->wav.sample_rate = buf[24] |
300 ((uint32_t)(buf[25]) << 8) |
301 ((uint32_t)(buf[26]) << 16) |
302 ((uint32_t)(buf[27]) << 24); /* set sample rate */
303 handle->wav.byte_rate = buf[28] |
304 ((uint32_t)(buf[29]) << 8) |
305 ((uint32_t)(buf[30]) << 16) |
306 ((uint32_t)(buf[31]) << 24); /* set byte rate */
307 handle->wav.block_align = buf[32] |
308 ((uint16_t)(buf[33]) << 8); /* set block align */
309 handle->wav.bit_per_sample = buf[34] |
310 ((uint16_t)(buf[35]) << 8); /* set bit per sample */
311 if (buf[36] != 'd')
312 {
313 handle->wav.sub_chunk2_id[0] = buf[38]; /* set sub chunk2 id 0 */
314 handle->wav.sub_chunk2_id[1] = buf[39]; /* set sub chunk2 id 1 */
315 handle->wav.sub_chunk2_id[2] = buf[40]; /* set sub chunk2 id 2 */
316 handle->wav.sub_chunk2_id[3] = buf[41]; /* set sub chunk2 id 3 */
317 handle->wav.sub_chunk2_size = buf[42] |
318 ((uint32_t)(buf[43]) << 8) |
319 ((uint32_t)(buf[44]) << 16) |
320 ((uint32_t)(buf[45]) << 24); /* set sub chunk _size */
321 handle->pos += 44; /* add 44 offset */
322 }
323 else
324 {
325 handle->wav.sub_chunk2_id[0] = buf[36]; /* set sub chunk2 id 0 */
326 handle->wav.sub_chunk2_id[1] = buf[37]; /* set sub chunk2 id 1 */
327 handle->wav.sub_chunk2_id[2] = buf[38]; /* set sub chunk2 id 2 */
328 handle->wav.sub_chunk2_id[3] = buf[39]; /* set sub chunk2 id 3 */
329 handle->wav.sub_chunk2_size = buf[40] |
330 ((uint32_t)(buf[41]) << 8) |
331 ((uint32_t)(buf[42]) << 16) |
332 ((uint32_t)(buf[43]) << 24); /* set sub chunk _size */
333 handle->pos += 44; /* add 44 offset */
334 }
335 if (handle->wav.chunk_id[0] != 'R' ||
336 handle->wav.chunk_id[1] != 'I' ||
337 handle->wav.chunk_id[2] != 'F' ||
338 handle->wav.chunk_id[3] != 'F') /* check chunk id */
339 {
340 handle->debug_print("wav: format is invalid.\n"); /* format is invalid */
341
342 return 4; /* return error */
343 }
344 if (handle->wav.format[0] != 'W' ||
345 handle->wav.format[1] != 'A' ||
346 handle->wav.format[2] != 'V' ||
347 handle->wav.format[3] != 'E') /* check format */
348 {
349 handle->debug_print("wav: format is invalid.\n"); /* format is invalid */
350
351 return 4; /* return error */
352 }
353 if (handle->wav.sub_chunk1_id[0] != 'f' ||
354 handle->wav.sub_chunk1_id[1] != 'm' ||
355 handle->wav.sub_chunk1_id[2] != 't' ||
356 handle->wav.sub_chunk1_id[3] != ' ') /* check sub chunk1 id */
357 {
358 handle->debug_print("wav: format is invalid.\n"); /* format is invalid */
359
360 return 4; /* return error */
361 }
362 if (handle->wav.sub_chunk2_id[0] != 'd' ||
363 handle->wav.sub_chunk2_id[1] != 'a' ||
364 handle->wav.sub_chunk2_id[2] != 't' ||
365 handle->wav.sub_chunk2_id[3] != 'a') /* check sub chunk2 id */
366 {
367 handle->debug_print("wav: format is invalid.\n"); /* format is invalid */
368
369 return 4; /* return error */
370 }
371
372 return 0; /* success return 0 */
373}
374
391uint8_t wav_player_config(wav_handle_t *handle, uint32_t standard,
392 uint32_t mode, uint32_t polarity,
393 uint32_t format, uint32_t enable, uint32_t freq)
394{
395 if (handle == NULL) /* check handle */
396 {
397 return 2; /* return error */
398 }
399 if (handle->inited != 1) /* check handle initialization */
400 {
401 return 3; /* return error */
402 }
403
404 if (handle->iis_set_freq(freq) != 0) /* set freq */
405 {
406 handle->debug_print("wav: set freq failed.\n"); /* set freq failed */
407
408 return 1; /* return error */
409 }
410 if (handle->iis_init(standard, mode, polarity, format, enable, freq) != 0) /* iis init */
411 {
412 handle->debug_print("wav: config failed.\n"); /* config failed */
413
414 return 1; /* return error */
415 }
416
417 return 0; /* success return 0 */
418}
419
432{
433 if (handle == NULL) /* check handle */
434 {
435 return 2; /* return error */
436 }
437 if (handle->inited != 1) /* check handle initialization */
438 {
439 return 3; /* return error */
440 }
441 if (handle->status != 0) /* check status */
442 {
443 handle->debug_print("wav: be playing.\n"); /* be playing */
444
445 return 4; /* return error */
446 }
447
448 if (handle->audio_read(handle->pos, WAV_BUFFER_SIZE, handle->buf) != 0) /* read data */
449 {
450 handle->debug_print("wav: read failed\n"); /* read failed */
451
452 return 1; /* return error */
453 }
454 if (handle->iis_write((uint16_t *)handle->buf, WAV_BUFFER_SIZE / 2) != 0) /* write data */
455 {
456 handle->debug_print("wav: iis write failed\n"); /* iis write failed */
457
458 return 1; /* return error */
459 }
460 handle->pos += WAV_BUFFER_SIZE; /* add pos */
461 handle->status = 1; /* set status */
462
463 return 0; /* success return 0 */
464}
465
479uint8_t wav_record_start(wav_handle_t *handle, uint32_t sample_rate, char *path)
480{
481 if (handle == NULL) /* check handle */
482 {
483 return 2; /* return error */
484 }
485 if (handle->inited != 1) /* check handle initialization */
486 {
487 return 3; /* return error */
488 }
489 if (handle->status != 0) /* check status */
490 {
491 handle->debug_print("wav: be recording.\n"); /* be recording */
492
493 return 4; /* return error */
494 }
495
496 handle->pos = 0; /* init 0 */
497 if (handle->audio_init(1, path, &handle->size) != 0) /* audio init */
498 {
499 handle->debug_print("wav: record failed.\n"); /* record failed */
500
501 return 1; /* return error */
502 }
503 handle->wav.chunk_id[0] = 'R'; /* R */
504 handle->wav.chunk_id[1] = 'I'; /* I */
505 handle->wav.chunk_id[2] = 'F'; /* F */
506 handle->wav.chunk_id[3] = 'F'; /* F */
507 handle->wav.chunk_size = 0x00000000; /* init 0 */
508 handle->wav.format[0] = 'W'; /* W */
509 handle->wav.format[1] = 'A'; /* A */
510 handle->wav.format[2] = 'V'; /* V */
511 handle->wav.format[3] = 'E'; /* E */
512 handle->wav.sub_chunk1_id[0] = 'f'; /* f */
513 handle->wav.sub_chunk1_id[1] = 'm'; /* m */
514 handle->wav.sub_chunk1_id[2] = 't'; /* t */
515 handle->wav.sub_chunk1_id[3] = 0x20; /* 0x20 */
516 handle->wav.sub_chunk1_size = 16; /* 16 bytes */
517 handle->wav.audio_format = 0x01; /* pcm */
518 handle->wav.num_channel = 2; /* 2 channel */
519 handle->wav.sample_rate = sample_rate; /* set sample rate */
520 handle->wav.byte_rate = handle->wav.sample_rate * 4; /* 4 times */
521 handle->wav.block_align = 4; /* 4 bytes */
522 handle->wav.bit_per_sample = 16; /* 16 bits */
523 handle->wav.sub_chunk2_id[0] = 'd'; /* d */
524 handle->wav.sub_chunk2_id[1] = 'a'; /* a */
525 handle->wav.sub_chunk2_id[2] = 't'; /* t */
526 handle->wav.sub_chunk2_id[3] = 'a'; /* a */
527 handle->wav.sub_chunk2_size = 0x00000000; /* init 0 */
528 if (a_wav_write_wav_header(handle) != 0) /* write header */
529 {
530 handle->debug_print("wav: write header failed.\n"); /* write header failed */
531
532 return 1; /* return error */
533 }
534 if (handle->iis_read((uint16_t *)handle->buf,
535 WAV_BUFFER_SIZE / 2) != 0) /* read data */
536 {
537 handle->debug_print("wav: iis read failed\n"); /* iis read failed */
538
539 return 1; /* return error */
540 }
541 handle->status = 1; /* set status */
542
543 return 0; /* success return 0 */
544}
545
558uint8_t wav_record_file_fill(wav_handle_t *handle, uint8_t index)
559{
560 if (handle == NULL) /* check handle */
561 {
562 return 2; /* return error */
563 }
564 if (handle->inited != 1) /* check handle initialization */
565 {
566 return 3; /* return error */
567 }
568 if (handle->status != 1) /* check status */
569 {
570 handle->debug_print("wav: not recording.\n"); /* not recording */
571
572 return 4; /* return error */
573 }
574
575 if (index != 0) /* buffer 1 */
576 {
577 if (handle->audio_write(handle->pos, WAV_BUFFER_SIZE / 2,
578 handle->buf + WAV_BUFFER_SIZE / 2) != 0) /* write data */
579 {
580 handle->debug_print("wav: write failed\n"); /* write failed */
581
582 return 1; /* return error */
583 }
584 handle->pos += WAV_BUFFER_SIZE / 2; /* add pos */
585 }
586 else /* buffer 0 */
587 {
588 if (handle->audio_write(handle->pos, WAV_BUFFER_SIZE / 2, handle->buf) != 0) /* write data */
589 {
590 handle->debug_print("wav: write failed\n"); /* write failed */
591
592 return 1; /* return error */
593 }
594 handle->pos += WAV_BUFFER_SIZE / 2; /* add pos */
595 }
596
597 return 0; /* success return 0 */
598}
599
612{
613 if (handle == NULL) /* check handle */
614 {
615 return 2; /* return error */
616 }
617 if (handle->inited != 1) /* check handle initialization */
618 {
619 return 3; /* return error */
620 }
621 if (handle->status != 1) /* check status */
622 {
623 handle->debug_print("wav: not recording.\n"); /* not recording */
624
625 return 4; /* return error */
626 }
627
628 if (handle->iis_stop() != 0) /* iis stop */
629 {
630 handle->debug_print("wav: iis stop failed.\n"); /* iis stop failed */
631
632 return 1; /* return error */
633 }
634 handle->wav.chunk_size = handle->pos - 44 + 36; /* set chunk size */
635 handle->wav.sub_chunk2_size = handle->pos - 44; /* set sub chunk2 size */
636 if (a_wav_write_wav_header(handle) != 0) /* write header */
637 {
638 handle->debug_print("wav: write header failed.\n"); /* write header failed */
639
640 return 1; /* return error */
641 }
642 if (handle->iis_deinit() != 0) /* iis deinit */
643 {
644 handle->debug_print("wav: iis deinit failed.\n"); /* iis deinit failed */
645
646 return 1; /* return error */
647 }
648 if (handle->audio_deinit() != 0) /* audio deinit */
649 {
650 handle->debug_print("wav: audio deinit failed.\n"); /* audio deinit failed */
651
652 return 1; /* return error */
653 }
654 handle->status = 0; /* stop */
655
656 return 0; /* success return 0 */
657}
658
671{
672 if (handle == NULL) /* check handle */
673 {
674 return 2; /* return error */
675 }
676 if (handle->inited != 1) /* check handle initialization */
677 {
678 return 3; /* return error */
679 }
680 if (handle->status != 1) /* check status */
681 {
682 handle->debug_print("wav: not playing.\n"); /* not playing */
683
684 return 4; /* return error */
685 }
686
687 if (handle->iis_stop() != 0) /* iis stop */
688 {
689 handle->debug_print("wav: iis stop failed.\n"); /* iis stop failed */
690
691 return 1; /* return error */
692 }
693 if (handle->iis_deinit() != 0) /* iis deinit */
694 {
695 handle->debug_print("wav: iis deinit failed.\n"); /* iis deinit failed */
696
697 return 1; /* return error */
698 }
699 if (handle->audio_deinit() != 0) /* audio deinit */
700 {
701 handle->debug_print("wav: audio deinit failed.\n"); /* audio deinit failed */
702
703 return 1; /* return error */
704 }
705 handle->status = 0; /* stop */
706
707 return 0; /* success return 0 */
708}
709
722{
723 if (handle == NULL) /* check handle */
724 {
725 return 2; /* return error */
726 }
727 if (handle->inited != 1) /* check handle initialization */
728 {
729 return 3; /* return error */
730 }
731 if (handle->status != 1) /* check status */
732 {
733 handle->debug_print("wav: not playing.\n"); /* not playing */
734
735 return 4; /* return error */
736 }
737
738 if (handle->iis_pause() != 0) /* iis pause */
739 {
740 handle->debug_print("wav: iis pause failed.\n"); /* iis pause failed */
741
742 return 1; /* return error */
743 }
744
745 return 0; /* success return 0 */
746}
747
760{
761 if (handle == NULL) /* check handle */
762 {
763 return 2; /* return error */
764 }
765 if (handle->inited != 1) /* check handle initialization */
766 {
767 return 3; /* return error */
768 }
769 if (handle->status != 0) /* check status */
770 {
771 handle->debug_print("wav: be playing.\n"); /* be playing */
772
773 return 4; /* return error */
774 }
775
776 if (handle->iis_pause() != 0) /* iis pause */
777 {
778 handle->debug_print("wav: iis pause failed.\n"); /* iis pause failed */
779
780 return 1; /* return error */
781 }
782
783 return 0; /* success return 0 */
784}
785
798{
799 if (handle == NULL) /* check handle */
800 {
801 return 2; /* return error */
802 }
803 if (handle->inited != 1) /* check handle initialization */
804 {
805 return 3; /* return error */
806 }
807
808 *status = (wav_status_t)(handle->status); /* get status */
809
810 return 0; /* success return 0 */
811}
812
825uint8_t wav_player_buffer_fill(wav_handle_t *handle, uint8_t index)
826{
827 if (handle == NULL) /* check handle */
828 {
829 return 2; /* return error */
830 }
831 if (handle->inited != 1) /* check handle initialization */
832 {
833 return 3; /* return error */
834 }
835 if (handle->status != 1) /* check status */
836 {
837 handle->debug_print("wav: not playing.\n"); /* not playing */
838
839 return 4; /* return error */
840 }
841
842 if (handle->pos > handle->size) /* check pos */
843 {
844 if (handle->iis_stop() != 0) /* iis stop */
845 {
846 handle->debug_print("wav: iis stop failed.\n"); /* iis stop failed */
847
848 return 1; /* return error */
849 }
850 if (handle->audio_deinit() != 0) /* audio deinit */
851 {
852 handle->debug_print("wav: audio deinit failed.\n"); /* audio deinit failed */
853
854 return 1; /* return error */
855 }
856 handle->status = 0; /* stop */
857 handle->debug_print("wav: play end.\n"); /* play end */
858
859 return 0; /* success return 0 */
860 }
861 if (index != 0) /* buffer 1 */
862 {
863 if (handle->audio_read(handle->pos, WAV_BUFFER_SIZE / 2,
864 handle->buf + WAV_BUFFER_SIZE / 2) != 0) /* read data */
865 {
866 handle->debug_print("wav: read failed\n"); /* read failed */
867
868 return 1; /* return error */
869 }
870 handle->pos += WAV_BUFFER_SIZE / 2; /* add pos */
871 if (handle->pos > handle->size) /* if oversize */
872 {
873 uint32_t diff = handle->pos - handle->size; /* get over part */
874
875 memset(handle->buf + WAV_BUFFER_SIZE - diff, 0, diff); /* set all 0 */
876 }
877 }
878 else /* buffer 0 */
879 {
880 if (handle->audio_read(handle->pos, WAV_BUFFER_SIZE / 2, handle->buf) != 0) /* read data */
881 {
882 handle->debug_print("wav: read failed\n"); /* read failed */
883
884 return 1; /* return error */
885 }
886 handle->pos += WAV_BUFFER_SIZE / 2; /* add pos */
887 if (handle->pos > handle->size) /* if oversize */
888 {
889 uint32_t diff = handle->pos - handle->size; /* get over part */
890
891 memset(handle->buf + WAV_BUFFER_SIZE / 2 - diff, 0, diff); /* set all 0 */
892 }
893 }
894
895 return 0; /* success return 0 */
896}
struct wav_handle_s wav_handle_t
wav handle structure definition
uint8_t wav_record_stop(wav_handle_t *handle)
wav record stop
Definition wav_player.c:611
uint8_t wav_player_config(wav_handle_t *handle, uint32_t standard, uint32_t mode, uint32_t polarity, uint32_t format, uint32_t enable, uint32_t freq)
wav player config
Definition wav_player.c:391
wav_status_t
wav status enumeration definition
Definition wav_player.h:66
uint8_t wav_record_file_fill(wav_handle_t *handle, uint8_t index)
wav record file fill
Definition wav_player.c:558
uint8_t wav_player_stop(wav_handle_t *handle)
wav player stop
Definition wav_player.c:670
uint8_t wav_player_resume(wav_handle_t *handle)
wav player resume
Definition wav_player.c:759
uint8_t wav_player_get_status(wav_handle_t *handle, wav_status_t *status)
wav get status
Definition wav_player.c:797
uint8_t wav_player_pause(wav_handle_t *handle)
wav player pause
Definition wav_player.c:721
#define WAV_BUFFER_SIZE
wav buffer size definition
Definition wav_player.h:59
uint8_t wav_record_start(wav_handle_t *handle, uint32_t sample_rate, char *path)
wav record start
Definition wav_player.c:479
uint8_t wav_init(wav_handle_t *handle)
initialize the wav
Definition wav_player.c:117
uint8_t wav_player_start(wav_handle_t *handle)
wav player start
Definition wav_player.c:431
uint8_t wav_deinit(wav_handle_t *handle)
wav deinit
Definition wav_player.c:221
uint8_t wav_player_buffer_fill(wav_handle_t *handle, uint8_t index)
wav player buffer fill
Definition wav_player.c:825
uint8_t wav_player_init(wav_handle_t *handle, char *path)
initialize the wav player
Definition wav_player.c:249
uint8_t(* iis_deinit)(void)
Definition wav_player.h:98
uint8_t(* iis_pause)(void)
Definition wav_player.h:100
uint8_t inited
Definition wav_player.h:111
void(* delay_ms)(uint32_t ms)
Definition wav_player.h:109
uint8_t(* iis_stop)(void)
Definition wav_player.h:99
uint8_t buf[WAV_BUFFER_SIZE]
Definition wav_player.h:116
uint8_t(* iis_resume)(void)
Definition wav_player.h:101
uint8_t(* iis_write)(uint16_t *buf, uint16_t len)
Definition wav_player.h:103
void(* debug_print)(const char *const fmt,...)
Definition wav_player.h:110
uint8_t(* iis_init)(uint32_t standard, uint32_t mode, uint32_t polarity, uint32_t format, uint32_t enable, uint32_t freq)
Definition wav_player.h:96
uint8_t(* audio_init)(uint8_t type, char *name, uint32_t *size)
Definition wav_player.h:105
wav_header_t wav
Definition wav_player.h:113
uint8_t(* audio_read)(uint32_t addr, uint16_t size, uint8_t *buffer)
Definition wav_player.h:107
uint8_t(* iis_set_freq)(uint32_t freq)
Definition wav_player.h:102
uint8_t(* iis_read)(uint16_t *buf, uint16_t len)
Definition wav_player.h:104
uint32_t size
Definition wav_player.h:114
uint8_t status
Definition wav_player.h:112
uint8_t(* audio_deinit)(void)
Definition wav_player.h:106
uint32_t pos
Definition wav_player.h:115
uint8_t(* audio_write)(uint32_t addr, uint16_t size, uint8_t *buffer)
Definition wav_player.h:108
uint32_t sub_chunk1_size
Definition wav_player.h:80
uint32_t sample_rate
Definition wav_player.h:83
uint32_t sub_chunk2_size
Definition wav_player.h:88
char chunk_id[4]
Definition wav_player.h:76
char sub_chunk2_id[4]
Definition wav_player.h:87
uint16_t num_channel
Definition wav_player.h:82
char format[4]
Definition wav_player.h:78
char sub_chunk1_id[4]
Definition wav_player.h:79
uint32_t byte_rate
Definition wav_player.h:84
uint16_t block_align
Definition wav_player.h:85
uint32_t chunk_size
Definition wav_player.h:77
uint16_t bit_per_sample
Definition wav_player.h:86
uint16_t audio_format
Definition wav_player.h:81
wav player header file