13bcd0d3143214bf10473673bfe2c833af597a41
2 * Copyright (C) 1997-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
6 /* \file afh_common.c: Common audio format handler functions. */
8 #include <sys/mman.h> /* mmap */
9 #include <sys/time.h> /* gettimeofday */
10 #include <sys/types.h>
18 /* The mp3 audio format handler does not need any libs. */
19 void mp3_init(struct audio_format_handler
*);
22 void ogg_init(struct audio_format_handler
*);
25 void aac_afh_init(struct audio_format_handler
*);
29 * The list of supported audio formats.
31 * We always define the full array of audio formats even if some audio formats
32 * were not compiled in. This is because for each audio file the number of its
33 * audio format is stored in the database. We don't want that numbers to become
34 * stale just because the user installed a new version of paraslash that
35 * supports a different set of audio formats.
37 * It can still be easily detected whether an audio format is compiled in by
38 * checking if the init function pointer is not \p NULL.
40 static struct audio_format_handler afl
[] = {
62 static inline int next_audio_format(int format
)
65 if (!afl
[format
].name
)
74 /** Iterate over each supported audio format. */
75 #define FOR_EACH_AUDIO_FORMAT(i) for (i = 0; afl[i].name; i = next_audio_format(i))
81 PARA_DEBUG_LOG("supported audio formats: %s\n",
82 SUPPORTED_AUDIO_FORMATS
);
83 FOR_EACH_AUDIO_FORMAT(i
) {
84 PARA_NOTICE_LOG("initializing %s handler\n",
85 audio_format_name(i
));
92 * guess the audio format judging from filename
94 * \param name the filename
96 * \return This function returns -1 if it has no idea what kind of audio
97 * file this might be. Otherwise the (non-negative) number of the audio format
100 int guess_audio_format(const char *name
)
102 int i
,j
, len
= strlen(name
);
104 FOR_EACH_AUDIO_FORMAT(i
) {
105 for (j
= 0; afl
[i
].suffixes
[j
]; j
++) {
106 const char *p
= afl
[i
].suffixes
[j
];
107 int plen
= strlen(p
);
110 if (name
[len
- plen
- 1] != '.')
112 if (strcasecmp(name
+ len
- plen
, p
))
114 // PARA_DEBUG_LOG("might be %s\n", audio_format_name(i));
118 return -E_BAD_AUDIO_FILE_SUFFIX
;
122 * Call get_file_info() to obtain an afhi structure.
124 * \param path The full path of the audio file.
125 * \param data Pointer to the contents of the (mapped) file.
126 * \param size The file size in bytes.
127 * \param afhi Result pointer.
129 * \return The number of the audio format on success, \p -E_AUDIO_FORMAT if no
130 * compiled in audio format handler is able to handler the file.
132 * This function tries to find an audio format handler that can interpret the
133 * file given by \a data and \a size.
135 * It first tries to determine the audio format from the filename given by \a
136 * path. If this doesn't work, all other audio format handlers are tried until
137 * one is found that can handle the file.
139 int compute_afhi(const char *path
, char *data
, size_t size
,
140 struct audio_format_info
*afhi
)
142 int ret
, i
, format
= guess_audio_format(path
);
145 ret
= afl
[format
].get_file_info(data
, size
, afhi
);
149 FOR_EACH_AUDIO_FORMAT(i
) {
150 if (i
== format
) /* we already tried this one to no avail */
152 ret
= afl
[i
].get_file_info(data
, size
, afhi
);
155 PARA_WARNING_LOG("%s\n", PARA_STRERROR(-ret
));
157 return -E_AUDIO_FORMAT
;
161 * Get the name of the given audio format.
163 * \param i The audio format number.
165 * This returns a pointer to statically allocated memory so it
166 * must not be freed by the caller.
168 const char *audio_format_name(int i
)
170 //PARA_NOTICE_LOG("array size: %u¸ requested: %d\n", ARRAY_SIZE(afl), i);
171 assert(i
< 0 || i
< ARRAY_SIZE(afl
) - 1);
172 return i
>= 0? afl
[i
].name
: "(none)";