A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

at master 339 lines 12 kB view raw
1/*************************************************************************** 2 * __________ __ ___. 3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___ 4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / 5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < 6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ 7 * \/ \/ \/ \/ \/ 8 * $Id$ 9 * 10 * Copyright (C) 2023 Christian Soffke 11 * 12 * This program is free software; you can redistribute it and/or 13 * modify it under the terms of the GNU General Public License 14 * as published by the Free Software Foundation; either version 2 15 * of the License, or (at your option) any later version. 16 * 17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 18 * KIND, either express or implied. 19 * 20 ****************************************************************************/ 21#include "plugin.h" 22#include "mul_id3.h" 23 24struct multiple_tracks_id3 { 25 unsigned long long filesize; 26 unsigned long long length; 27 unsigned long frequency; 28 unsigned int artist_hash; 29 unsigned int composer_hash; 30 unsigned int albumartist_hash; 31 unsigned int grouping_hash; 32 unsigned int comment_hash; 33 unsigned int album_hash; 34 unsigned int genre_hash; 35 unsigned int codectype; 36 unsigned int bitrate; 37 int year; 38 bool vbr; 39}; 40 41static struct multiple_tracks_id3 mul_id3; 42 43static const int32_t units[] = 44{ 45 LANG_BYTE, 46 LANG_KIBIBYTE, 47 LANG_MEBIBYTE, 48 LANG_GIBIBYTE 49}; 50 51/* Calculate modified FNV hash of string 52 * has good avalanche behaviour and uniform distribution 53 * see http://home.comcast.net/~bretm/hash/ */ 54static unsigned int mfnv(char *str) 55{ 56 const unsigned int p = 16777619; 57 unsigned int hash = 0x811C9DC5; // 2166136261; 58 59 if (!str) 60 return 0; 61 62 while(*str) 63 hash = (hash ^ *str++) * p; 64 hash += hash << 13; 65 hash ^= hash >> 7; 66 hash += hash << 3; 67 hash ^= hash >> 17; 68 hash += hash << 5; 69 return hash; 70} 71 72static void init_mul_id3(void) 73{ 74 mul_id3.artist_hash = 0; 75 mul_id3.album_hash = 0; 76 mul_id3.genre_hash = 0; 77 mul_id3.composer_hash = 0; 78 mul_id3.albumartist_hash = 0; 79 mul_id3.grouping_hash = 0; 80 mul_id3.comment_hash = 0; 81 mul_id3.codectype = 0; 82 mul_id3.vbr = false; 83 mul_id3.bitrate = 0; 84 mul_id3.frequency = 0; 85 mul_id3.length = 0; 86 mul_id3.filesize = 0; 87 mul_id3.year = 0; 88} 89 90void collect_id3(struct mp3entry *id3, bool is_first_track) 91{ 92 if (is_first_track) 93 { 94 init_mul_id3(); 95 mul_id3.artist_hash = mfnv(id3->artist); 96 mul_id3.album_hash = mfnv(id3->album); 97 mul_id3.genre_hash = mfnv(id3->genre_string); 98 mul_id3.composer_hash = mfnv(id3->composer); 99 mul_id3.albumartist_hash = mfnv(id3->albumartist); 100 mul_id3.grouping_hash = mfnv(id3->grouping); 101 mul_id3.comment_hash = mfnv(id3->comment); 102 mul_id3.codectype = id3->codectype; 103 mul_id3.vbr = id3->vbr; 104 mul_id3.bitrate = id3->bitrate; 105 mul_id3.frequency = id3->frequency; 106 mul_id3.year = id3->year; 107 } 108 else 109 { 110 if (mul_id3.artist_hash && (mfnv(id3->artist) != mul_id3.artist_hash)) 111 mul_id3.artist_hash = 0; 112 if (mul_id3.album_hash && (mfnv(id3->album) != mul_id3.album_hash)) 113 mul_id3.album_hash = 0; 114 if (mul_id3.genre_hash && (mfnv(id3->genre_string) != mul_id3.genre_hash)) 115 mul_id3.genre_hash = 0; 116 if (mul_id3.composer_hash && (mfnv(id3->composer) != mul_id3.composer_hash)) 117 mul_id3.composer_hash = 0; 118 if (mul_id3.albumartist_hash && (mfnv(id3->albumartist) != 119 mul_id3.albumartist_hash)) 120 mul_id3.albumartist_hash = 0; 121 if (mul_id3.grouping_hash && (mfnv(id3->grouping) != mul_id3.grouping_hash)) 122 mul_id3.grouping_hash = 0; 123 if (mul_id3.comment_hash && (mfnv(id3->comment) != mul_id3.comment_hash)) 124 mul_id3.comment_hash = 0; 125 126 if (mul_id3.codectype && (id3->codectype != mul_id3.codectype)) 127 mul_id3.codectype = AFMT_UNKNOWN; 128 if (mul_id3.bitrate && (id3->bitrate != mul_id3.bitrate || 129 id3->vbr != mul_id3.vbr)) 130 mul_id3.bitrate = 0; 131 if (mul_id3.frequency && (id3->frequency != mul_id3.frequency)) 132 mul_id3.frequency = 0; 133 if (mul_id3.year && (id3->year != mul_id3.year)) 134 mul_id3.year = 0; 135 } 136 mul_id3.length += id3->length; 137 mul_id3.filesize += id3->filesize; 138} 139 140/* (!) Note unit conversion below 141 * 142 * Use result only as input for browse_id3, 143 * with the track_ct parameter set to > 1. 144 */ 145void finalize_id3(struct mp3entry *id3) 146{ 147 id3->path[0] = '\0'; 148 id3->title = NULL; 149 if (!mul_id3.artist_hash) 150 id3->artist = NULL; 151 if (!mul_id3.album_hash) 152 id3->album = NULL; 153 if (!mul_id3.genre_hash) 154 id3->genre_string = NULL; 155 if (!mul_id3.composer_hash) 156 id3->composer = NULL; 157 if (!mul_id3.albumartist_hash) 158 id3->albumartist = NULL; 159 if (!mul_id3.grouping_hash) 160 id3->grouping = NULL; 161 if (!mul_id3.comment_hash) 162 id3->comment = NULL; 163 id3->disc_string = NULL; 164 id3->track_string = NULL; 165 id3->year_string = NULL; 166 id3->year = mul_id3.year; 167 mul_id3.length /= 1000; /* convert from ms to s */ 168 mul_id3.filesize >>= 10; /* convert from B to KiB */ 169 id3->length = mul_id3.length > ULONG_MAX ? 0 : mul_id3.length; 170 id3->filesize = mul_id3.filesize > INT_MAX ? 0 : mul_id3.filesize; 171 id3->frequency = mul_id3.frequency; 172 id3->bitrate = mul_id3.bitrate; 173 id3->codectype = mul_id3.codectype; 174 id3->vbr = mul_id3.vbr; 175 id3->discnum = 0; 176 id3->tracknum = -1; 177 id3->track_level = 0; 178 id3->album_level = 0; 179} 180 181unsigned 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 */ 195static const char *image_exts[] = {"bmp","jpg","jpe","jpeg","png","ppm"}; 196/* and videos */ 197static const char *video_exts[] = {"mpg","mpeg","mpv","m2v"}; 198 199static void display_dir_stats_vp(struct dir_stats *stats, struct viewport *vp, 200 struct screen *display) 201{ 202 int32_t lang_size_unit; 203 unsigned long display_size = human_size(stats->byte_count, &lang_size_unit); 204 struct viewport *last_vp = display->set_viewport(vp); 205 display->clear_viewport(); 206 display->putsf(0, 0, "Files: %d (%lu %s)", stats->file_count, 207 display_size, rb->str(lang_size_unit)); 208 display->putsf(0, 1, "Audio: %d", stats->audio_file_count); 209 if (stats->count_all) 210 { 211 display->putsf(0, 2, "Playlists: %d", stats->m3u_file_count); 212 display->putsf(0, 3, "Images: %d", stats->img_file_count); 213 display->putsf(0, 4, "Videos: %d", stats->vid_file_count); 214 display->putsf(0, 5, "Directories: %d", stats->dir_count); 215 display->putsf(0, 6, "Max files in Dir: %d", stats->max_files_in_dir); 216 } 217 else 218 display->putsf(0, 2, "Directories: %d", stats->dir_count); 219 220 display->update_viewport(); 221 display->set_viewport(last_vp); 222} 223 224void display_dir_stats(struct dir_stats *stats) 225{ 226 struct viewport vps[NB_SCREENS]; 227 FOR_NB_SCREENS(i) 228 { 229 rb->viewport_set_defaults(&vps[i], i); 230 display_dir_stats_vp(stats, &vps[i], rb->screens[i]); 231 } 232} 233 234/* Recursively scans directories in search of files 235 * and informs the user of the progress. 236 */ 237bool collect_dir_stats(struct dir_stats *stats, bool (*id3_cb)(const char*)) 238{ 239 bool result = true; 240 unsigned int files_in_dir = 0; 241 static unsigned int id3_count; 242 static unsigned long last_displayed, last_get_action; 243 struct dirent* entry; 244 int dirlen = rb->strlen(stats->dirname); 245 DIR* dir = rb->opendir(stats->dirname); 246 if (!dir) 247 { 248 rb->splashf(HZ*2, "open error: %s", stats->dirname); 249 return false; 250 } 251 else if (!stats->dirname[1]) /* root dir */ 252 stats->dirname[0] = dirlen = 0; 253 254 /* walk through the directory content */ 255 while(result && (0 != (entry = rb->readdir(dir)))) 256 { 257 struct dirinfo info = rb->dir_get_info(dir, entry); 258 if (info.attribute & ATTR_DIRECTORY) 259 { 260 if (!rb->strcmp((char *)entry->d_name, ".") || 261 !rb->strcmp((char *)entry->d_name, "..")) 262 continue; /* skip these */ 263 264 rb->snprintf(stats->dirname + dirlen, sizeof(stats->dirname) - dirlen, 265 "/%s", entry->d_name); /* append name to current directory */ 266 if (!id3_cb) 267 { 268 stats->dir_count++; /* new directory */ 269 if (*rb->current_tick - last_displayed > (HZ/2)) 270 { 271 if (last_displayed) 272 display_dir_stats(stats); 273 last_displayed = *(rb->current_tick); 274 } 275 } 276 result = collect_dir_stats(stats, id3_cb); /* recursion */ 277 } 278 else if (!id3_cb) 279 { 280 char *ptr; 281 stats->file_count++; /* new file */ 282 files_in_dir++; 283 stats->byte_count += info.size; 284 285 int attr = rb->filetype_get_attr(entry->d_name); 286 if (attr == FILE_ATTR_AUDIO) 287 stats->audio_file_count++; 288 else if (attr == FILE_ATTR_M3U) 289 stats->m3u_file_count++; 290 /* image or video file attributes have to be compared manually */ 291 else if (stats->count_all && 292 (ptr = rb->strrchr(entry->d_name,'.'))) 293 { 294 unsigned int i; 295 ptr++; 296 for(i = 0; i < ARRAYLEN(image_exts); i++) 297 { 298 if(!rb->strcasecmp(ptr, image_exts[i])) 299 { 300 stats->img_file_count++; 301 break; 302 } 303 } 304 if (i >= ARRAYLEN(image_exts)) { 305 for(i = 0; i < ARRAYLEN(video_exts); i++) { 306 if(!rb->strcasecmp(ptr, video_exts[i])) { 307 stats->vid_file_count++; 308 break; 309 } 310 } 311 } 312 } 313 } 314 else if (rb->filetype_get_attr(entry->d_name) == FILE_ATTR_AUDIO) 315 { 316 rb->splash_progress(id3_count++, stats->audio_file_count, 317 "%s (%s)", 318 rb->str(LANG_WAIT), rb->str(LANG_OFF_ABORT)); 319 rb->snprintf(stats->dirname + dirlen, sizeof(stats->dirname) - dirlen, 320 "/%s", entry->d_name); /* append name to current directory */ 321 id3_cb(stats->dirname); /* allow metadata to be collected */ 322 } 323 324 if (TIME_AFTER(*(rb->current_tick), last_get_action + HZ/8)) 325 { 326 if(ACTION_STD_CANCEL == rb->get_action(CONTEXT_STD,TIMEOUT_NOBLOCK)) 327 { 328 stats->canceled = true; 329 result = false; 330 } 331 last_get_action = *(rb->current_tick); 332 } 333 rb->yield(); 334 } 335 rb->closedir(dir); 336 if (stats->max_files_in_dir < files_in_dir) 337 stats->max_files_in_dir = files_in_dir; 338 return result; 339}