2 * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file afh.c Paraslash's standalone audio format handler tool. */
14 #include "afh.cmdline.h"
19 static struct afh_args_info conf;
20 /** The list of all status items */
21 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
24 INIT_STDERR_LOGGING(conf.loglevel_arg)
26 static void print_info(int audio_format_num, struct afh_info *afhi)
28 printf("%s: %dkbit/s\n" /* bitrate */
29 "%s: %s\n" /* format */
30 "%s: %dHz\n" /* frequency */
31 "%s: %d\n" /* channels */
32 "%s: %lu\n" /* seconds total */
34 "%s: %lu: %lu\n" /* chunk time */
35 "%s: %lu\n", /* num chunks */
36 status_item_list[SI_BITRATE], afhi->bitrate,
37 status_item_list[SI_FORMAT], audio_format_name(audio_format_num),
38 status_item_list[SI_FREQUENCY], afhi->frequency,
39 status_item_list[SI_CHANNELS], afhi->channels,
40 status_item_list[SI_SECONDS_TOTAL], afhi->seconds_total,
42 status_item_list[SI_CHUNK_TIME], (long unsigned)afhi->chunk_tv.tv_sec,
43 (long unsigned)afhi->chunk_tv.tv_usec,
44 status_item_list[SI_NUM_CHUNKS], afhi->chunks_total
48 static void print_chunk_table(struct afh_info *afhi)
52 printf("chunk_table: ");
53 for (i = 0; i <= afhi->chunks_total; i++)
54 printf("%u ", afhi->chunk_table[i]);
58 static int cat_file(void *audio_file_data, struct afh_info *afhi)
61 struct timeval stream_start;
62 long unsigned i, first_chunk, last_chunk;
67 if (conf.begin_chunk_arg < 0)
68 return -ERRNO_TO_PARA_ERROR(EINVAL);
69 first_chunk = conf.begin_chunk_arg;
71 if (conf.end_chunk_given) {
72 if (conf.end_chunk_arg < 0)
73 return -ERRNO_TO_PARA_ERROR(EINVAL);
74 if (conf.end_chunk_arg >= afhi->chunks_total)
75 return -ERRNO_TO_PARA_ERROR(EINVAL);
76 last_chunk = conf.end_chunk_arg;
78 last_chunk = afhi->chunks_total - 1;
79 if (!afhi->chunks_total)
81 afh_get_header(afhi, audio_file_data, &buf, &size);
82 if (size && first_chunk && !conf.no_header_given) {
83 PARA_INFO_LOG("writing audio file header (%zu bytes)\n", size);
84 ret = write(STDOUT_FILENO, buf, size);
88 return -E_AFH_SHORT_WRITE;
90 PARA_NOTICE_LOG("writing chunks %lu - %lu\n", first_chunk, last_chunk);
91 gettimeofday(&stream_start, NULL);
92 for (i = first_chunk; i <= last_chunk; i++) {
93 struct timeval now, diff, next_chunk;
94 afh_get_chunk(i, afhi, audio_file_data, &buf, &size);
95 PARA_DEBUG_LOG("chunk %lu: size %zu\n", i, size);
96 if (conf.just_in_time_given) {
97 compute_chunk_time(i - first_chunk, &afhi->chunk_tv,
98 &stream_start, &next_chunk);
99 gettimeofday(&now, NULL);
100 ret = tv_diff(&next_chunk, &now, &diff);
102 ret = para_select(1, NULL, NULL, &diff);
107 PARA_INFO_LOG("writing chunk %lu\n", i);
108 ret = write_all(STDOUT_FILENO, buf, &size);
115 int main(int argc, char **argv)
117 int ret, audio_format_num;
118 void *audio_file_data;
119 size_t audio_file_size;
120 struct afh_info afhi;
122 afh_cmdline_parser(argc, argv, &conf);
123 HANDLE_VERSION_FLAG("afh", conf);
125 if (conf.inputs_num != 1)
128 ret = mmap_full_file(conf.inputs[0], O_RDONLY, &audio_file_data,
129 &audio_file_size, NULL);
132 ret = compute_afhi(conf.inputs[0], audio_file_data, audio_file_size, &afhi);
135 audio_format_num = ret;
136 if (conf.stream_given)
137 ret = cat_file(audio_file_data, &afhi);
139 print_info(audio_format_num, &afhi);
140 if (conf.chunk_table_given)
141 print_chunk_table(&afhi);
146 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
147 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;