comparison libtest/wavrdhelp.c @ 13:30d1d0a705c0

libtest: add WAV reader helper functions
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 20 Nov 2022 01:34:57 +0000
parents
children
comparison
equal deleted inserted replaced
12:f88817a233fb 13:30d1d0a705c0
1 /*
2 * In this module we implement our helper functions
3 * on top of the basic WAV reader.
4 */
5
6 #include <stdio.h>
7 #include <stdint.h>
8 #include "wavreader.h"
9 #include "wavrdhelp.h"
10
11 int wavrd_check_header(void *wav, const char *filename)
12 {
13 int format, sampleRate, channels, bitsPerSample;
14
15 if (!wav_get_header(wav, &format, &channels, &sampleRate,
16 &bitsPerSample, NULL)) {
17 fprintf(stderr, "error: %s is not a valid WAV file\n",
18 filename);
19 return -1;
20 }
21 if (format != 1) {
22 fprintf(stderr, "error: %s has unsupported WAV format %d\n",
23 filename, format);
24 return -1;
25 }
26 if (bitsPerSample != 16) {
27 fprintf(stderr,
28 "error: %s has unsupported WAV sample depth %d\n",
29 filename, bitsPerSample);
30 return -1;
31 }
32 if (channels != 1) {
33 fprintf(stderr, "error: %s has %d channels, need 1\n",
34 filename, channels);
35 return -1;
36 }
37 if (sampleRate != 8000) {
38 fprintf(stderr,
39 "error: %s has %d Hz sample rate, need 8000 Hz\n",
40 filename, sampleRate);
41 return -1;
42 }
43 return 0;
44 }
45
46 int wavrd_get_pcm_block(void *wav, int16_t *pcm)
47 {
48 uint8_t bytes[320], *dp;
49 int cc, i;
50
51 cc = wav_read_data(wav, bytes, 320);
52 cc >>= 1;
53 dp = bytes;
54 for (i = 0; i < cc; i++) {
55 pcm[i] = dp[0] | (dp[1] << 8);
56 dp += 2;
57 }
58 while (i < 160)
59 pcm[i++] = 0;
60 return cc;
61 }