894cd78284d36c9de7ef1aa565894e6dd6017384
2 * Copyright (C) 2008-2009 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
};
26 INIT_STDERR_LOGGING(loglevel
)
28 static void print_info(int audio_format_num
, struct afh_info
*afhi
)
30 printf("%s: %dkbit/s\n" /* bitrate */
31 "%s: %s\n" /* format */
32 "%s: %dHz\n" /* frequency */
33 "%s: %d\n" /* channels */
34 "%s: %lu\n" /* seconds total */
35 "%s: %lu: %lu\n" /* chunk time */
36 "%s: %lu\n" /* num chunks */
37 "%s: %s\n" /* techinfo */
38 "%s: %s\n" /* artist */
39 "%s: %s\n" /* title */
41 "%s: %s\n" /* album */
42 "%s: %s\n", /* comment */
43 status_item_list
[SI_BITRATE
], afhi
->bitrate
,
44 status_item_list
[SI_FORMAT
], audio_format_name(audio_format_num
),
45 status_item_list
[SI_FREQUENCY
], afhi
->frequency
,
46 status_item_list
[SI_CHANNELS
], afhi
->channels
,
47 status_item_list
[SI_SECONDS_TOTAL
], afhi
->seconds_total
,
48 status_item_list
[SI_CHUNK_TIME
], (long unsigned)afhi
->chunk_tv
.tv_sec
,
49 (long unsigned)afhi
->chunk_tv
.tv_usec
,
50 status_item_list
[SI_NUM_CHUNKS
], afhi
->chunks_total
,
51 status_item_list
[SI_TECHINFO
], afhi
->techinfo
? afhi
->techinfo
: "",
52 status_item_list
[SI_ARTIST
], afhi
->tags
.artist
? afhi
->tags
.artist
: "",
53 status_item_list
[SI_TITLE
], afhi
->tags
.title
? afhi
->tags
.title
: "",
54 status_item_list
[SI_YEAR
], afhi
->tags
.year
? afhi
->tags
.year
: "",
55 status_item_list
[SI_ALBUM
], afhi
->tags
.album
? afhi
->tags
.album
: "",
56 status_item_list
[SI_COMMENT
], afhi
->tags
.comment
? afhi
->tags
.comment
: ""
60 static void print_chunk_table(struct afh_info
*afhi
)
64 printf("chunk_table: ");
65 for (i
= 0; i
<= afhi
->chunks_total
; i
++)
66 printf("%u ", afhi
->chunk_table
[i
]);
70 static int cat_file(void *audio_file_data
, struct afh_info
*afhi
)
73 struct timeval stream_start
;
74 long unsigned i
, first_chunk
, last_chunk
;
79 if (conf
.begin_chunk_arg
< 0) {
80 if (-conf
.begin_chunk_arg
> afhi
->chunks_total
)
81 return -ERRNO_TO_PARA_ERROR(EINVAL
);
82 first_chunk
= afhi
->chunks_total
+ conf
.begin_chunk_arg
;
84 first_chunk
= conf
.begin_chunk_arg
;
85 if (conf
.end_chunk_given
) {
86 if (conf
.end_chunk_arg
< 0) {
87 if (-conf
.end_chunk_arg
> afhi
->chunks_total
)
88 return -ERRNO_TO_PARA_ERROR(EINVAL
);
89 last_chunk
= afhi
->chunks_total
+ conf
.end_chunk_arg
;
91 if (conf
.end_chunk_arg
>= afhi
->chunks_total
)
92 return -ERRNO_TO_PARA_ERROR(EINVAL
);
93 last_chunk
= conf
.end_chunk_arg
;
96 last_chunk
= afhi
->chunks_total
- 1;
97 if (first_chunk
>= last_chunk
)
98 return -ERRNO_TO_PARA_ERROR(EINVAL
);
99 if (!afhi
->chunks_total
)
101 afh_get_header(afhi
, audio_file_data
, &buf
, &size
);
102 if (size
&& first_chunk
&& !conf
.no_header_given
) {
103 PARA_INFO_LOG("writing audio file header (%zu bytes)\n", size
);
104 ret
= write(STDOUT_FILENO
, buf
, size
);
108 return -E_AFH_SHORT_WRITE
;
110 PARA_NOTICE_LOG("writing chunks %lu - %lu\n", first_chunk
, last_chunk
);
111 gettimeofday(&stream_start
, NULL
);
112 for (i
= first_chunk
; i
<= last_chunk
; i
++) {
113 struct timeval now
, diff
, next_chunk
;
114 afh_get_chunk(i
, afhi
, audio_file_data
, &buf
, &size
);
115 PARA_DEBUG_LOG("chunk %lu: size %zu\n", i
, size
);
116 if (conf
.just_in_time_given
) {
117 compute_chunk_time(i
- first_chunk
, &afhi
->chunk_tv
,
118 &stream_start
, &next_chunk
);
119 gettimeofday(&now
, NULL
);
120 ret
= tv_diff(&next_chunk
, &now
, &diff
);
122 ret
= para_select(1, NULL
, NULL
, &diff
);
129 PARA_INFO_LOG("writing chunk %lu\n", i
);
130 ret
= write_all(STDOUT_FILENO
, buf
, &size
);
138 * The main function of para_afh.
140 * \param argc Usual argument count.
141 * \param argv Usual argument vector.
143 * \return \p EXIT_FAILURE or \p EXIT_SUCCESS.
145 int main(int argc
, char **argv
)
147 int i
, ret
, audio_format_num
, fd
;
148 void *audio_file_data
;
149 size_t audio_file_size
;
150 struct afh_info afhi
;
152 afh_cmdline_parser(argc
, argv
, &conf
);
153 HANDLE_VERSION_FLAG("afh", conf
);
154 loglevel
= get_loglevel_by_name(conf
.loglevel_arg
);
156 if (conf
.inputs_num
== 0)
158 if (conf
.stream_given
&& conf
.inputs_num
!= 1)
161 for (i
= 0; i
< conf
.inputs_num
; i
++) {
163 ret
= mmap_full_file(conf
.inputs
[i
], O_RDONLY
, &audio_file_data
,
164 &audio_file_size
, &fd
);
167 ret
= compute_afhi(conf
.inputs
[i
], audio_file_data
, audio_file_size
,
171 audio_format_num
= ret
;
172 if (conf
.stream_given
)
173 ret
= cat_file(audio_file_data
, &afhi
);
175 printf("File %d: %s\n", i
+ 1, conf
.inputs
[i
]);
176 print_info(audio_format_num
, &afhi
);
177 if (conf
.chunk_table_given
)
178 print_chunk_table(&afhi
);
180 free(afhi
.tags
.artist
);
181 free(afhi
.tags
.title
);
182 free(afhi
.tags
.year
);
183 free(afhi
.tags
.album
);
184 free(afhi
.tags
.comment
);
185 free(afhi
.chunk_table
);
188 ret2
= para_munmap(audio_file_data
, audio_file_size
);
189 if (ret2
< 0 && ret
>= 0)
196 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
197 return ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
;