2 * Copyright (C) 2005-2011 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file write.c Paraslash's standalone wav/raw player. */
10 #include <sys/types.h>
16 #include "write.cmdline.h"
21 #include "buffer_tree.h"
23 #include "write_common.h"
30 enum check_wav_state {
36 /* Information extracted from the wav header. */
37 struct check_wav_task {
39 /** Number of channels. */
41 unsigned sample_format;
42 /** Sample rate specified in wav header given by \a buf. */
44 /** The task structure used by the scheduler. */
46 struct btr_node *btrn;
50 static struct write_args_info conf;
52 static struct stdin_task sit;
54 /** Length of a standard wav header. */
55 #define WAV_HEADER_LEN 44
57 static void check_wav_pre_select(struct sched *s, struct task *t)
59 struct check_wav_task *cwt = container_of(t, struct check_wav_task, task);
62 ret = btr_node_status(cwt->btrn, cwt->min_iqs, BTR_NT_INTERNAL);
67 #define HANDLE_EXEC(_cmd) \
68 if (!strcmp(cmd, #_cmd)) { \
69 if (!conf._cmd ## _given && cwt->state == CWS_NEED_HEADER) \
70 return -E_BTR_NAVAIL; \
71 *result = make_message("%d", cwt->state == CWS_NO_HEADER || conf._cmd ## _given? \
72 conf._cmd ## _arg : cwt->_cmd); \
77 static int check_wav_exec(struct btr_node *btrn, const char *cmd, char **result)
79 struct check_wav_task *cwt = btr_context(btrn);
81 HANDLE_EXEC(sample_rate);
82 HANDLE_EXEC(channels);
83 HANDLE_EXEC(sample_format);
84 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
87 static void check_wav_post_select(__a_unused struct sched *s, struct task *t)
89 struct check_wav_task *cwt = container_of(t, struct check_wav_task, task);
90 struct btr_node *btrn = cwt->btrn;
94 uint16_t bps; /* bits per sample */
95 const char *sample_formats[] = {SAMPLE_FORMATS};
98 ret = btr_node_status(btrn, cwt->min_iqs, BTR_NT_INTERNAL);
101 if (cwt->state != CWS_NEED_HEADER)
103 btr_merge(btrn, cwt->min_iqs);
104 sz = btr_next_buffer(btrn, (char **)&a);
105 if (sz < cwt->min_iqs) /* file size less than WAV_HEADER_SIZE */
109 * The default byte ordering assumed for WAVE data files is
110 * little-endian. Files written using the big-endian byte ordering
111 * scheme have the identifier RIFX instead of RIFF.
113 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' ||
114 (a[3] != 'F' && a[3] != 'X')) {
115 PARA_NOTICE_LOG("wav header not found\n");
116 cwt->state = CWS_NO_HEADER;
117 sprintf(t->status, "check wav: no header");
120 PARA_INFO_LOG("found wav header\n");
121 cwt->state = CWS_HAVE_HEADER;
122 sprintf(t->status, "check wav: have header");
123 cwt->channels = (unsigned) a[22];
124 cwt->sample_rate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
125 bps = a[34] + ((unsigned)a[35] << 8);
126 if (bps != 8 && bps != 16) {
127 PARA_WARNING_LOG("%u bps not supported, assuming 16\n", bps);
131 * 8-bit samples are stored as unsigned bytes, ranging from 0 to 255.
132 * 16-bit samples are stored as 2's-complement signed integers, ranging
133 * from -32768 to 32767.
136 cwt->sample_format = SF_U8;
138 cwt->sample_format = (a[3] == 'F')? SF_S16_LE : SF_S16_BE;
139 PARA_NOTICE_LOG("%dHz, %s, %s\n", cwt->sample_rate,
140 cwt->channels == 1? "mono" : "stereo",
141 sample_formats[cwt->sample_format]);
142 btr_consume(btrn, WAV_HEADER_LEN);
148 btr_remove_node(btrn);
152 INIT_STDERR_LOGGING(loglevel)
154 __noreturn static void print_help_and_die(void)
156 int d = conf.detailed_help_given;
157 const char **p = d? write_args_info_detailed_help
158 : write_args_info_help;
160 printf_or_die("%s\n\n", WRITE_CMDLINE_PARSER_PACKAGE "-"
161 WRITE_CMDLINE_PARSER_VERSION);
162 printf_or_die("%s\n\n", write_args_info_usage);
164 printf_or_die("%s\n", *p);
165 print_writer_helps(d);
169 static int main_btr(struct sched *s)
172 struct check_wav_task _cwt, *cwt = &_cwt;
173 struct writer_node *wns;
175 loglevel = get_loglevel_by_name(conf.loglevel_arg);
176 sit.btrn = btr_new_node(&(struct btr_node_description)
177 EMBRACE(.name = "stdin"));
178 stdin_set_defaults(&sit);
179 register_task(&sit.task);
181 cwt->state = CWS_NEED_HEADER;
182 cwt->min_iqs = WAV_HEADER_LEN;
183 cwt->btrn = btr_new_node(&(struct btr_node_description)
184 EMBRACE(.name = "check_wav", .parent = sit.btrn,
185 .handler = check_wav_exec, .context = cwt));
186 sprintf(cwt->task.status, "check_wav");
187 cwt->task.pre_select = check_wav_pre_select;
188 cwt->task.post_select = check_wav_post_select;
190 register_task(&cwt->task);
192 ret = -E_WRITE_SYNTAX;
193 if (!conf.writer_given) {
195 wns = para_calloc(sizeof(*wns));
196 ret = setup_writer_node(NULL, cwt->btrn, wns);
201 wns = para_calloc(conf.writer_given * sizeof(*wns));
202 for (i = 0; i < conf.writer_given; i++) {
203 ret = setup_writer_node(conf.writer_arg[i],
210 s->default_timeout.tv_sec = 10;
211 s->default_timeout.tv_usec = 50000;
215 for (j = 0; j < i; j++) {
216 struct task *t = &wns[j].task;
217 assert(t->error < 0);
218 if (t->error != -E_WRITE_COMMON_EOF
219 && t->error != -E_BTR_EOF) {
220 PARA_ERROR_LOG("%s: %s\n", t->status,
221 para_strerror(-t->error));
228 for (i--; i >= 0; i--) {
229 struct writer_node *wn = wns + i;
230 struct writer *w = writers + wn->writer_num;
233 btr_free_node(wn->btrn);
234 w->free_config(wn->conf);
238 btr_free_node(cwt->btrn);
243 * Para_write's main function.
245 * \param argc The usual argument counter.
246 * \param argv The usual argument vector.
248 * It sets up and starts the tasks and the buffer tree nodes determined by
249 * command line options.
251 * \return \p EXIT_SUCCESS or EXIT_FAILURE
253 int main(int argc, char *argv[])
255 int ret = -E_WRITE_SYNTAX;
256 static struct sched s;
259 write_cmdline_parser(argc, argv, &conf);
260 HANDLE_VERSION_FLAG("write", conf);
261 if (conf.help_given || conf.detailed_help_given)
262 print_help_and_die();
266 PARA_ERROR_LOG("%s\n", para_strerror(-ret));