2 * Copyright (C) 2005-2010 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"
29 enum check_wav_state {
35 struct check_wav_task {
37 /** Number of channels specified in wav header given by \a buf. */
39 /** Sample rate specified in wav header given by \a buf. */
41 /** The task structure used by the scheduler. */
43 struct btr_node *btrn;
47 static struct write_args_info conf;
49 static struct stdin_task sit;
51 /** Length of a standard wav header. */
52 #define WAV_HEADER_LEN 44
55 * Test if audio buffer contains a valid wave header.
57 * \return If not, return -E_NO_WAV_HEADER, otherwise, return zero. If
58 * there is less than WAV_HEADER_LEN bytes available, return one.
60 static void check_wav_pre_select(struct sched *s, struct task *t)
62 struct check_wav_task *cwt = container_of(t, struct check_wav_task, task);
65 ret = btr_node_status(cwt->btrn, cwt->min_iqs, BTR_NT_INTERNAL);
70 #define HANDLE_EXEC(_cmd) \
71 if (!strcmp(cmd, #_cmd)) { \
72 if (!conf._cmd ## _given && cwt->state == CWS_NEED_HEADER) \
73 return -E_BTR_NAVAIL; \
74 *result = make_message("%d", cwt->state == CWS_NO_HEADER? \
75 conf._cmd ## _arg : cwt->_cmd); \
80 static int check_wav_exec(struct btr_node *btrn, const char *cmd, char **result)
82 struct check_wav_task *cwt = btr_context(btrn);
84 HANDLE_EXEC(sample_rate);
85 HANDLE_EXEC(channels);
86 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
89 static void check_wav_post_select(__a_unused struct sched *s, struct task *t)
91 struct check_wav_task *cwt = container_of(t, struct check_wav_task, task);
92 struct btr_node *btrn = cwt->btrn;
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 cwt->sample_rate = 44100;
110 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F') {
111 PARA_NOTICE_LOG("wav header not found\n");
112 cwt->state = CWS_NO_HEADER;
113 sprintf(t->status, "check wav: no header");
116 PARA_INFO_LOG("found wav header\n");
117 cwt->state = CWS_HAVE_HEADER;
118 sprintf(t->status, "check wav: have header");
119 cwt->channels = (unsigned) a[22];
120 cwt->sample_rate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
121 PARA_INFO_LOG("channels: %d, sample rate: %d\n", cwt->channels, cwt->sample_rate);
122 btr_consume(btrn, WAV_HEADER_LEN);
128 btr_remove_node(btrn);
132 INIT_STDERR_LOGGING(loglevel)
134 __noreturn static void print_help_and_die(void)
136 int d = conf.detailed_help_given;
137 const char **p = d? write_args_info_detailed_help
138 : write_args_info_help;
140 printf_or_die("%s\n\n", WRITE_CMDLINE_PARSER_PACKAGE "-"
141 WRITE_CMDLINE_PARSER_VERSION);
142 printf_or_die("%s\n\n", write_args_info_usage);
144 printf_or_die("%s\n", *p);
145 print_writer_helps(d);
149 static int main_btr(struct sched *s)
152 struct check_wav_task _cwt, *cwt = &_cwt;
153 struct writer_node *wns;
155 loglevel = get_loglevel_by_name(conf.loglevel_arg);
156 sit.btrn = btr_new_node(&(struct btr_node_description)
157 EMBRACE(.name = "stdin"));
158 stdin_set_defaults(&sit);
159 register_task(&sit.task);
161 cwt->state = CWS_NEED_HEADER;
162 cwt->min_iqs = WAV_HEADER_LEN;
163 cwt->btrn = btr_new_node(&(struct btr_node_description)
164 EMBRACE(.name = "check_wav", .parent = sit.btrn,
165 .handler = check_wav_exec, .context = cwt));
166 sprintf(cwt->task.status, "check_wav");
167 cwt->task.pre_select = check_wav_pre_select;
168 cwt->task.post_select = check_wav_post_select;
170 register_task(&cwt->task);
172 ret = -E_WRITE_SYNTAX;
173 if (!conf.writer_given) {
175 wns = para_calloc(sizeof(*wns));
176 ret = setup_writer_node(NULL, cwt->btrn, wns);
181 wns = para_calloc(conf.writer_given * sizeof(*wns));
182 for (i = 0; i < conf.writer_given; i++) {
183 ret = setup_writer_node(conf.writer_arg[i],
190 s->default_timeout.tv_sec = 10;
191 s->default_timeout.tv_usec = 50000;
194 for (i--; i >= 0; i--) {
195 struct writer_node *wn = wns + i;
196 struct writer *w = writers + wn->writer_num;
199 btr_free_node(wn->btrn);
203 btr_free_node(cwt->btrn);
208 * Para_write's main function.
210 * \param argc The usual argument counter.
211 * \param argv The usual argument vector.
213 * It sets up and starts the tasks and the buffer tree nodes determined by
214 * command line options.
216 * \return \p EXIT_SUCCESS or EXIT_FAILURE
218 int main(int argc, char *argv[])
220 int ret = -E_WRITE_SYNTAX;
221 static struct sched s;
224 write_cmdline_parser(argc, argv, &conf);
225 HANDLE_VERSION_FLAG("write", conf);
226 if (conf.help_given || conf.detailed_help_given)
227 print_help_and_die();
231 PARA_ERROR_LOG("%s\n", para_strerror(-ret));