ab1c61887cc1ac3536dd8c364fdd0a9a1bd5cf7b
[paraslash.git] / check_wav.c
1 /*
2 * Copyright (C) 2005-2012 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file check_wav.c Detect and delete a wav header. */
8
9 #include <regex.h>
10
11 #include "para.h"
12 #include "string.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "buffer_tree.h"
16 #include "error.h"
17 #include "check_wav.h"
18
19 /** Length of a standard wav header. */
20 #define WAV_HEADER_LEN 44
21
22 enum check_wav_state {
23 CWS_NEED_HEADER,
24 CWS_HAVE_HEADER,
25 CWS_NO_HEADER,
26 };
27
28 struct check_wav_context {
29 enum check_wav_state state;
30 struct btr_node *btrn;
31 size_t min_iqs;
32 /* Command line args. */
33 struct wav_params params;
34 /* Extracted from the wav header.*/
35 unsigned channels;
36 unsigned sample_format;
37 unsigned sample_rate;
38 };
39
40 void check_wav_pre_select(struct sched *s, struct check_wav_context *cwc)
41 {
42 int ret = btr_node_status(cwc->btrn, cwc->min_iqs, BTR_NT_INTERNAL);
43 if (ret != 0)
44 sched_min_delay(s);
45 }
46
47 static int check_wav_exec(struct btr_node *btrn, const char *cmd, char **result)
48 {
49 struct check_wav_context *cwc = btr_context(btrn);
50 int val, header_val, given, arg;
51
52 header_val = cwc->channels;
53 arg = cwc->params.channels_arg;
54 given = cwc->params.channels_given;
55 if (!strcmp(cmd, "channels"))
56 goto out;
57
58 header_val = cwc->sample_rate;
59 arg = cwc->params.sample_rate_arg;
60 given = cwc->params.sample_rate_given;
61 if (!strcmp(cmd, "sample_rate"))
62 goto out;
63
64 header_val = cwc->sample_format;
65 arg = cwc->params.sample_format_arg;
66 given = cwc->params.sample_format_given;
67 if (!strcmp(cmd, "sample_format"))
68 goto out;
69
70 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
71 out:
72 if (given)
73 val = arg;
74 else {
75 switch (cwc->state) {
76 case CWS_HAVE_HEADER:
77 val = header_val;
78 break;
79 case CWS_NO_HEADER:
80 /*
81 * No wav header available and no value specified at
82 * the command line. Maybe one of our parent nodes
83 * knows.
84 */
85 if (btr_exec_up(btr_parent(cwc->btrn), cmd, result) >= 0)
86 return 1;
87 /* Use default value */
88 val = arg;
89 break;
90 default:
91 return -E_BTR_NAVAIL;
92 }
93 }
94 *result = make_message("%d", val);
95 return 1;
96 }
97
98 int check_wav_post_select(struct check_wav_context *cwc)
99 {
100 struct btr_node *btrn = cwc->btrn;
101 unsigned char *a;
102 size_t sz;
103 int ret;
104 uint16_t bps; /* bits per sample */
105 const char *sample_formats[] = {SAMPLE_FORMATS};
106
107 ret = btr_node_status(btrn, cwc->min_iqs, BTR_NT_INTERNAL);
108 if (ret <= 0)
109 goto out;
110 if (cwc->state != CWS_NEED_HEADER)
111 goto pushdown;
112 btr_merge(btrn, cwc->min_iqs);
113 sz = btr_next_buffer(btrn, (char **)&a);
114 if (sz < cwc->min_iqs) /* file size less than WAV_HEADER_SIZE */
115 goto pushdown;
116 cwc->min_iqs = 0;
117 /*
118 * The default byte ordering assumed for WAVE data files is
119 * little-endian. Files written using the big-endian byte ordering
120 * scheme have the identifier RIFX instead of RIFF.
121 */
122 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' ||
123 (a[3] != 'F' && a[3] != 'X')) {
124 PARA_NOTICE_LOG("wav header not found\n");
125 cwc->state = CWS_NO_HEADER;
126 goto out;
127 }
128 PARA_INFO_LOG("found wav header\n");
129 cwc->state = CWS_HAVE_HEADER;
130 /* Only set those values which have not already been set. */
131 cwc->channels = (unsigned)a[22];
132 cwc->sample_rate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
133 bps = a[34] + ((unsigned)a[35] << 8);
134 if (bps != 8 && bps != 16) {
135 PARA_WARNING_LOG("%u bps not supported, assuming 16\n",
136 bps);
137 bps = 16;
138 }
139 /*
140 * 8-bit samples are stored as unsigned bytes, ranging from 0
141 * to 255. 16-bit samples are stored as 2's-complement signed
142 * integers, ranging from -32768 to 32767.
143 */
144 if (bps == 8)
145 cwc->sample_format = SF_U8;
146 else
147 cwc->sample_format = (a[3] == 'F')?
148 SF_S16_LE : SF_S16_BE;
149 PARA_NOTICE_LOG("%dHz, %s, %s\n", cwc->sample_rate,
150 cwc->channels == 1? "mono" : "stereo",
151 sample_formats[cwc->sample_format]);
152 btr_consume(btrn, WAV_HEADER_LEN);
153 pushdown:
154 btr_pushdown(btrn);
155 out:
156 if (ret < 0)
157 btr_remove_node(&cwc->btrn);
158 return ret;
159 }
160
161 struct check_wav_context *check_wav_init(struct btr_node *parent,
162 struct wav_params *params, struct btr_node **cw_btrn)
163 {
164 struct check_wav_context *cwc = para_calloc(sizeof(*cwc));
165
166 cwc->state = CWS_NEED_HEADER;
167 cwc->min_iqs = WAV_HEADER_LEN;
168 cwc->params = *params;
169 cwc->btrn = btr_new_node(&(struct btr_node_description)
170 EMBRACE(.name = "check_wav", .parent = parent,
171 .handler = check_wav_exec, .context = cwc));
172 if (cw_btrn)
173 *cw_btrn = cwc->btrn;
174 return cwc;
175 }
176
177 void check_wav_shutdown(struct check_wav_context *cwc)
178 {
179 free(cwc);
180 }