2 * Copyright (C) 1997-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file afh_common.c Common audio format handler functions. */
9 #include <sys/mman.h> /* mmap */
10 #include <sys/time.h> /* gettimeofday */
11 #include <sys/types.h>
19 /* The mp3 audio format handler does not need any libs. */
20 void mp3_init(struct audio_format_handler
*);
23 void ogg_init(struct audio_format_handler
*);
26 void aac_afh_init(struct audio_format_handler
*);
30 * The list of supported audio formats.
32 * We always define the full array of audio formats even if some audio formats
33 * were not compiled in. This is because for each audio file the number of its
34 * audio format is stored in the database. We don't want that numbers to become
35 * stale just because the user installed a new version of paraslash that
36 * supports a different set of audio formats.
38 * It can still be easily detected whether an audio format is compiled in by
39 * checking if the init function pointer is not \p NULL.
41 static struct audio_format_handler afl
[] = {
63 static inline int next_audio_format(int format
)
66 if (!afl
[format
].name
)
75 /** Iterate over each supported audio format. */
76 #define FOR_EACH_AUDIO_FORMAT(i) for (i = 0; afl[i].name; i = next_audio_format(i))
79 * Call the init function of each supported audio format handler.
85 PARA_INFO_LOG("supported audio formats: %s\n",
86 SUPPORTED_AUDIO_FORMATS
);
87 FOR_EACH_AUDIO_FORMAT(i
) {
88 PARA_NOTICE_LOG("initializing %s handler\n",
89 audio_format_name(i
));
96 * Guess the audio format judging from filename.
98 * \param name The filename.
100 * \return This function returns -1 if it has no idea what kind of audio
101 * file this might be. Otherwise the (non-negative) number of the audio format
104 int guess_audio_format(const char *name
)
106 int i
,j
, len
= strlen(name
);
108 FOR_EACH_AUDIO_FORMAT(i
) {
109 for (j
= 0; afl
[i
].suffixes
[j
]; j
++) {
110 const char *p
= afl
[i
].suffixes
[j
];
111 int plen
= strlen(p
);
114 if (name
[len
- plen
- 1] != '.')
116 if (strcasecmp(name
+ len
- plen
, p
))
118 // PARA_DEBUG_LOG("might be %s\n", audio_format_name(i));
125 char *make_taginfo(char *title
, char *artist
, char *album
, char *year
,
128 return make_message("%s: %s, by %s\n" /* taginfo1 */
129 "%s: A: %s, Y: %s, C: %s\n", /* taginfo 2*/
130 status_item_list
[SI_TAGINFO1
],
131 (title
&& *title
)? title
: "(title tag not set)",
132 (artist
&& *artist
)? artist
: "(artist tag not set)",
133 status_item_list
[SI_TAGINFO2
],
134 (album
&& *album
)? album
: "(album tag not set)",
135 (year
&& *year
)? year
: "????",
136 (comment
&& *comment
)? comment
: "(comment tag not set)"
141 * Call get_file_info() to obtain an afhi structure.
143 * \param path The full path of the audio file.
144 * \param data Pointer to the contents of the (mapped) file.
145 * \param size The file size in bytes.
146 * \param fd The open file descriptor.
147 * \param afhi Result pointer.
149 * \return The number of the audio format on success, \p -E_AUDIO_FORMAT if no
150 * compiled in audio format handler is able to handler the file.
152 * This function tries to find an audio format handler that can interpret the
153 * file given by \a data and \a size.
155 * It first tries to determine the audio format from the filename given by \a
156 * path. If this doesn't work, all other audio format handlers are tried until
157 * one is found that can handle the file.
159 int compute_afhi(const char *path
, char *data
, size_t size
, int fd
,
160 struct afh_info
*afhi
)
164 afhi
->header_offset
= 0;
165 afhi
->header_len
= 0;
166 format
= guess_audio_format(path
);
169 ret
= afl
[format
].get_file_info(data
, size
, fd
, afhi
);
173 FOR_EACH_AUDIO_FORMAT(i
) {
174 if (i
== format
) /* we already tried this one to no avail */
176 ret
= afl
[i
].get_file_info(data
, size
, fd
, afhi
);
179 PARA_WARNING_LOG("%s\n", para_strerror(-ret
));
181 return -E_AUDIO_FORMAT
;
185 * Get the name of the given audio format.
187 * \param i The audio format number.
189 * This returns a pointer to statically allocated memory so it
190 * must not be freed by the caller.
192 const char *audio_format_name(int i
)
194 //PARA_NOTICE_LOG("array size: %u¸ requested: %d\n", ARRAY_SIZE(afl), i);
195 assert(i
< 0 || i
< ARRAY_SIZE(afl
) - 1);
196 return i
>= 0? afl
[i
].name
: "(none)";
200 void afh_get_chunk(long unsigned chunk_num
, struct afh_info
*afhi
,
201 void *map
, const char **buf
, size_t *len
)
203 size_t pos
= afhi
->chunk_table
[chunk_num
];
205 *len
= afhi
->chunk_table
[chunk_num
+ 1] - pos
;
209 * Get the header of an audio file.
211 * \param afhi The audio file handler data describing the file.
212 * \param map The data of the audio file.
213 * \param buf The length of the header is stored here.
214 * \param len Points to a buffer containing the header on return.
216 * This function sets \a buf to \p NULL and \a len to zero if \a map or \a
217 * afhi is \p NULL, or if the current audio format does not need special
220 void afh_get_header(struct afh_info
*afhi
, void *map
, const char **buf
, size_t *len
)
222 if (!map
|| !afhi
|| ! afhi
->header_len
) {
226 *len
= afhi
->header_len
;
227 *buf
= map
+ afhi
->header_offset
;