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 static int check_wav_exec(struct btr_node *btrn, const char *cmd, char **result)
72 struct check_wav_task *cwt = btr_context(btrn);
75 if (!strcmp(cmd, "samplerate")) {
76 if (cwt->state != CWS_HAVE_HEADER)
78 *result = make_message("%d", cwt->samplerate);
81 if (!strcmp(cmd, "channels")) {
82 if (cwt->state != CWS_HAVE_HEADER)
84 *result = make_message("%d", cwt->channels);
87 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
90 static void check_wav_post_select(__a_unused struct sched *s, struct task *t)
92 struct check_wav_task *cwt = container_of(t, struct check_wav_task, task);
93 struct btr_node *btrn = cwt->btrn;
99 ret = btr_node_status(btrn, cwt->min_iqs, BTR_NT_INTERNAL);
102 if (cwt->state != CWS_NEED_HEADER)
104 btr_merge(btrn, cwt->min_iqs);
105 sz = btr_next_buffer(btrn, (char **)&a);
106 if (sz < cwt->min_iqs) /* file size less than WAV_HEADER_SIZE */
110 cwt->samplerate = 44100;
111 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F') {
112 PARA_NOTICE_LOG("wav header not found\n");
113 cwt->state = CWS_NO_HEADER;
114 sprintf(t->status, "check wav: no header");
117 PARA_INFO_LOG("found wav header\n");
118 cwt->state = CWS_HAVE_HEADER;
119 sprintf(t->status, "check wav: have header");
120 cwt->channels = (unsigned) a[22];
121 cwt->samplerate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
122 PARA_INFO_LOG("channels: %d, sample rate: %d\n", cwt->channels, cwt->samplerate);
123 btr_consume(btrn, WAV_HEADER_LEN);
129 btr_remove_node(btrn);
133 INIT_STDERR_LOGGING(loglevel)
135 __noreturn static void print_help_and_die(void)
137 int d = conf.detailed_help_given;
138 const char **p = d? write_args_info_detailed_help
139 : write_args_info_help;
141 printf_or_die("%s\n\n", WRITE_CMDLINE_PARSER_PACKAGE "-"
142 WRITE_CMDLINE_PARSER_VERSION);
143 printf_or_die("%s\n\n", write_args_info_usage);
145 printf_or_die("%s\n", *p);
146 print_writer_helps(d);
150 static int main_btr(struct sched *s)
153 struct check_wav_task _cwt, *cwt = &_cwt;
154 struct writer_node *wns;
156 loglevel = get_loglevel_by_name(conf.loglevel_arg);
157 sit.btrn = btr_new_node(&(struct btr_node_description)
158 EMBRACE(.name = "stdin"));
159 stdin_set_defaults(&sit);
160 register_task(&sit.task);
162 cwt->state = CWS_NEED_HEADER;
163 cwt->min_iqs = WAV_HEADER_LEN;
164 cwt->btrn = btr_new_node(&(struct btr_node_description)
165 EMBRACE(.name = "check_wav", .parent = sit.btrn,
166 .handler = check_wav_exec, .context = cwt));
167 sprintf(cwt->task.status, "check_wav");
168 cwt->task.pre_select = check_wav_pre_select;
169 cwt->task.post_select = check_wav_post_select;
171 register_task(&cwt->task);
173 ret = -E_WRITE_SYNTAX;
174 if (!conf.writer_given) {
176 wns = para_calloc(sizeof(*wns));
177 ret = setup_writer_node(NULL, cwt->btrn, wns);
182 wns = para_calloc(conf.writer_given * sizeof(*wns));
183 for (i = 0; i < conf.writer_given; i++) {
184 ret = setup_writer_node(conf.writer_arg[i],
191 s->default_timeout.tv_sec = 10;
192 s->default_timeout.tv_usec = 50000;
195 for (i--; i >= 0; i--) {
196 struct writer_node *wn = wns + i;
197 struct writer *w = writers + wn->writer_num;
200 btr_free_node(wn->btrn);
204 btr_free_node(cwt->btrn);
209 * Para_write's main function.
211 * \param argc The usual argument counter.
212 * \param argv The usual argument vector.
214 * It sets up and starts the tasks and the buffer tree nodes determined by
215 * command line options.
217 * \return \p EXIT_SUCCESS or EXIT_FAILURE
219 int main(int argc, char *argv[])
221 int ret = -E_WRITE_SYNTAX;
222 static struct sched s;
225 write_cmdline_parser(argc, argv, &conf);
226 HANDLE_VERSION_FLAG("write", conf);
227 if (conf.help_given || conf.detailed_help_given)
228 print_help_and_die();
232 PARA_ERROR_LOG("%s\n", para_strerror(-ret));