diff options
Diffstat (limited to 'apps/plugins/lib/mul_id3.c')
-rw-r--r-- | apps/plugins/lib/mul_id3.c | 188 |
1 files changed, 183 insertions, 5 deletions
diff --git a/apps/plugins/lib/mul_id3.c b/apps/plugins/lib/mul_id3.c index edf44f7282..c4b511c626 100644 --- a/apps/plugins/lib/mul_id3.c +++ b/apps/plugins/lib/mul_id3.c | |||
@@ -40,6 +40,13 @@ struct multiple_tracks_id3 { | |||
40 | 40 | ||
41 | static struct multiple_tracks_id3 mul_id3; | 41 | static struct multiple_tracks_id3 mul_id3; |
42 | 42 | ||
43 | static const int32_t units[] = | ||
44 | { | ||
45 | LANG_BYTE, | ||
46 | LANG_KIBIBYTE, | ||
47 | LANG_MEBIBYTE, | ||
48 | LANG_GIBIBYTE | ||
49 | }; | ||
43 | 50 | ||
44 | /* Calculate modified FNV hash of string | 51 | /* Calculate modified FNV hash of string |
45 | * has good avalanche behaviour and uniform distribution | 52 | * has good avalanche behaviour and uniform distribution |
@@ -130,9 +137,7 @@ void collect_id3(struct mp3entry *id3, bool is_first_track) | |||
130 | mul_id3.filesize += id3->filesize; | 137 | mul_id3.filesize += id3->filesize; |
131 | } | 138 | } |
132 | 139 | ||
133 | /* (!) Note scale factor applied to returned metadata: | 140 | /* (!) Note unit conversion below |
134 | * - Unit for filesize will be KiB instead of Bytes | ||
135 | * - Unit for length will be s instead of ms | ||
136 | * | 141 | * |
137 | * Use result only as input for browse_id3, | 142 | * Use result only as input for browse_id3, |
138 | * with the track_ct parameter set to > 1. | 143 | * with the track_ct parameter set to > 1. |
@@ -159,8 +164,8 @@ void finalize_id3(struct mp3entry *id3) | |||
159 | id3->track_string = NULL; | 164 | id3->track_string = NULL; |
160 | id3->year_string = NULL; | 165 | id3->year_string = NULL; |
161 | id3->year = mul_id3.year; | 166 | id3->year = mul_id3.year; |
162 | mul_id3.length /= 1000; | 167 | mul_id3.length /= 1000; /* convert from ms to s */ |
163 | mul_id3.filesize >>= 10; | 168 | mul_id3.filesize >>= 10; /* convert from B to KiB */ |
164 | id3->length = mul_id3.length > ULONG_MAX ? 0 : mul_id3.length; | 169 | id3->length = mul_id3.length > ULONG_MAX ? 0 : mul_id3.length; |
165 | id3->filesize = mul_id3.filesize > INT_MAX ? 0 : mul_id3.filesize; | 170 | id3->filesize = mul_id3.filesize > INT_MAX ? 0 : mul_id3.filesize; |
166 | id3->frequency = mul_id3.frequency; | 171 | id3->frequency = mul_id3.frequency; |
@@ -172,3 +177,176 @@ void finalize_id3(struct mp3entry *id3) | |||
172 | id3->track_level = 0; | 177 | id3->track_level = 0; |
173 | id3->album_level = 0; | 178 | id3->album_level = 0; |
174 | } | 179 | } |
180 | |||
181 | unsigned long human_size(unsigned long long byte_count, int32_t *unit_lang_id) | ||
182 | { | ||
183 | const size_t n = sizeof(units)/sizeof(units[0]); | ||
184 | unsigned int i; | ||
185 | |||
186 | /* margin set at 10K boundary: 10239 B +1 => 10 KB */ | ||
187 | for(i = 0; i < n-1 && byte_count >= 10*1024; i++) | ||
188 | byte_count >>= 10; /* div by 1024 */ | ||
189 | |||
190 | *unit_lang_id = units[i]; | ||
191 | return (unsigned long)byte_count; | ||
192 | } | ||
193 | |||
194 | /* missing filetype attribute for images */ | ||
195 | static const char *image_exts[] = {"bmp","jpg","jpe","jpeg","png","ppm"}; | ||
196 | /* and videos */ | ||
197 | static const char *video_exts[] = {"mpg","mpeg","mpv","m2v"}; | ||
198 | |||
199 | static void prn(const char *str, int y) | ||
200 | { | ||
201 | rb->lcd_puts(0, y, str); | ||
202 | #ifdef HAVE_REMOTE_LCD | ||
203 | rb->lcd_remote_puts(0, y, str); | ||
204 | #endif | ||
205 | } | ||
206 | |||
207 | void display_dir_stats(struct dir_stats *stats) | ||
208 | { | ||
209 | char buf[32]; | ||
210 | int32_t lang_size_unit; | ||
211 | unsigned long display_size = human_size(stats->byte_count, &lang_size_unit); | ||
212 | rb->lcd_clear_display(); | ||
213 | #ifdef HAVE_REMOTE_LCD | ||
214 | rb->lcd_remote_clear_display(); | ||
215 | #endif | ||
216 | rb->snprintf(buf, sizeof(buf), "Files: %d (%lu %s)", stats->file_count, | ||
217 | display_size, rb->str(lang_size_unit)); | ||
218 | prn(buf, 0); | ||
219 | rb->snprintf(buf, sizeof(buf), "Audio: %d", stats->audio_file_count); | ||
220 | prn(buf, 1); | ||
221 | if (stats->count_all) | ||
222 | { | ||
223 | rb->snprintf(buf, sizeof(buf), "Playlists: %d", stats->m3u_file_count); | ||
224 | prn(buf, 2); | ||
225 | rb->snprintf(buf, sizeof(buf), "Images: %d", stats->img_file_count); | ||
226 | prn(buf, 3); | ||
227 | rb->snprintf(buf, sizeof(buf), "Videos: %d", stats->vid_file_count); | ||
228 | prn(buf, 4); | ||
229 | rb->snprintf(buf, sizeof(buf), "Directories: %d", stats->dir_count); | ||
230 | prn(buf, 5); | ||
231 | rb->snprintf(buf, sizeof(buf), "Max files in Dir: %d", | ||
232 | stats->max_files_in_dir); | ||
233 | prn(buf, 6); | ||
234 | } | ||
235 | else | ||
236 | { | ||
237 | rb->snprintf(buf, sizeof(buf), "Directories: %d", stats->dir_count); | ||
238 | prn(buf, 2); | ||
239 | } | ||
240 | rb->lcd_update(); | ||
241 | #ifdef HAVE_REMOTE_LCD | ||
242 | rb->lcd_remote_update(); | ||
243 | #endif | ||
244 | |||
245 | } | ||
246 | |||
247 | /* Recursively scans directories in search of files | ||
248 | * and informs the user of the progress. | ||
249 | */ | ||
250 | bool collect_dir_stats(struct dir_stats *stats, bool (*id3_cb)(const char*)) | ||
251 | { | ||
252 | bool result = true; | ||
253 | unsigned int files_in_dir = 0; | ||
254 | static unsigned int id3_count; | ||
255 | static unsigned long last_displayed, last_get_action; | ||
256 | struct dirent* entry; | ||
257 | int dirlen = rb->strlen(stats->dirname); | ||
258 | DIR* dir = rb->opendir(stats->dirname); | ||
259 | if (!dir) | ||
260 | { | ||
261 | rb->splashf(HZ*2, "open error: %s", stats->dirname); | ||
262 | return false; | ||
263 | } | ||
264 | else if (!stats->dirname[1]) /* root dir */ | ||
265 | stats->dirname[0] = dirlen = 0; | ||
266 | |||
267 | /* walk through the directory content */ | ||
268 | while(result && (0 != (entry = rb->readdir(dir)))) | ||
269 | { | ||
270 | struct dirinfo info = rb->dir_get_info(dir, entry); | ||
271 | if (info.attribute & ATTR_DIRECTORY) | ||
272 | { | ||
273 | if (!rb->strcmp((char *)entry->d_name, ".") || | ||
274 | !rb->strcmp((char *)entry->d_name, "..")) | ||
275 | continue; /* skip these */ | ||
276 | |||
277 | rb->snprintf(stats->dirname + dirlen, sizeof(stats->dirname) - dirlen, | ||
278 | "/%s", entry->d_name); /* append name to current directory */ | ||
279 | if (!id3_cb) | ||
280 | { | ||
281 | stats->dir_count++; /* new directory */ | ||
282 | if (*rb->current_tick - last_displayed > (HZ/2)) | ||
283 | { | ||
284 | if (last_displayed) | ||
285 | display_dir_stats(stats); | ||
286 | last_displayed = *(rb->current_tick); | ||
287 | } | ||
288 | } | ||
289 | result = collect_dir_stats(stats, id3_cb); /* recursion */ | ||
290 | } | ||
291 | else if (!id3_cb) | ||
292 | { | ||
293 | char *ptr; | ||
294 | stats->file_count++; /* new file */ | ||
295 | files_in_dir++; | ||
296 | stats->byte_count += info.size; | ||
297 | |||
298 | int attr = rb->filetype_get_attr(entry->d_name); | ||
299 | if (attr == FILE_ATTR_AUDIO) | ||
300 | stats->audio_file_count++; | ||
301 | else if (attr == FILE_ATTR_M3U) | ||
302 | stats->m3u_file_count++; | ||
303 | /* image or video file attributes have to be compared manually */ | ||
304 | else if (stats->count_all && | ||
305 | (ptr = rb->strrchr(entry->d_name,'.'))) | ||
306 | { | ||
307 | unsigned int i; | ||
308 | ptr++; | ||
309 | for(i = 0; i < ARRAYLEN(image_exts); i++) | ||
310 | { | ||
311 | if(!rb->strcasecmp(ptr, image_exts[i])) | ||
312 | { | ||
313 | stats->img_file_count++; | ||
314 | break; | ||
315 | } | ||
316 | } | ||
317 | if (i >= ARRAYLEN(image_exts)) { | ||
318 | for(i = 0; i < ARRAYLEN(video_exts); i++) { | ||
319 | if(!rb->strcasecmp(ptr, video_exts[i])) { | ||
320 | stats->vid_file_count++; | ||
321 | break; | ||
322 | } | ||
323 | } | ||
324 | } | ||
325 | } | ||
326 | } | ||
327 | else if (rb->filetype_get_attr(entry->d_name) == FILE_ATTR_AUDIO) | ||
328 | { | ||
329 | rb->splash_progress(id3_count++, stats->audio_file_count, | ||
330 | "%s (%s)", | ||
331 | rb->str(LANG_WAIT), rb->str(LANG_OFF_ABORT)); | ||
332 | rb->snprintf(stats->dirname + dirlen, sizeof(stats->dirname) - dirlen, | ||
333 | "/%s", entry->d_name); /* append name to current directory */ | ||
334 | id3_cb(stats->dirname); /* allow metadata to be collected */ | ||
335 | } | ||
336 | |||
337 | if (TIME_AFTER(*(rb->current_tick), last_get_action + HZ/8)) | ||
338 | { | ||
339 | if(ACTION_STD_CANCEL == rb->get_action(CONTEXT_STD,TIMEOUT_NOBLOCK)) | ||
340 | { | ||
341 | stats->canceled = true; | ||
342 | result = false; | ||
343 | } | ||
344 | last_get_action = *(rb->current_tick); | ||
345 | } | ||
346 | rb->yield(); | ||
347 | } | ||
348 | rb->closedir(dir); | ||
349 | if (stats->max_files_in_dir < files_in_dir) | ||
350 | stats->max_files_in_dir = files_in_dir; | ||
351 | return result; | ||
352 | } | ||