package shakuhachi
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
A music collection manager
Install
dune-project
Dependency
Authors
Maintainers
Sources
0.3.2.tar.gz
sha256=c72807d303de137441b643fec1c7630c930ebc792b576783c7961b237c2114db
sha512=770f878bbdac8033d90e904ce8e9c84fc75331c7ff9b35e2204fafe3746f432f2664296dac91942a788b82a2da07478d91b76ed85cd880758d2def8a55144840
doc/src/shakuhachi.metadata/metadata.ml.html
Source file metadata.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340(*****************************************************************************) (* *) (* Copyright (C) 2026 Yves Ndiaye *) (* *) (* This Source Code Form is subject to the terms of the Mozilla Public *) (* License, v. 2.0. If a copy of the MPL was not distributed with this *) (* file, You can obtain one at https://mozilla.org/MPL/2.0/. *) (* *) (*****************************************************************************) module Values = Set.Make (String) module Properties = Map.Make (String) module Keys = struct let title = "TITLE" let artist = "ARTIST" let album = "ALBUM" let date = "DATE" let comment = "COMMENT" let genre = "GENRE" let disc_number = "DISCNUMBER" let track = "TRACKNUMBER" let album_artist = "ALBUMARTIST" let composer = "COMPOSER" let title_sort = "TITLESORT" let artist_sort = "ARTISTSORT" let album_sort = "ALBUMSORT" let skhc_file_extension = "SHAKUHACHI_FILEEXTENSION" let skhc_file_size = "SHAKUHACHI_FILESIZE" end type t = { audioproperties : Otaglibc.audioproperties option; covers : Otaglibc.picture array; properties : Values.t Properties.t; } let init audioproperties covers properties = { audioproperties; covers; properties } let of_properties ?audioproperties ?(covers = [||]) (properties : Values.t Properties.t) = init audioproperties covers properties let empty = { audioproperties = None; covers = [||]; properties = Properties.empty } let int_of_string_conv s = let negative = String.starts_with ~prefix:"-" s in let seq = String.to_seq s in let seq = match negative with true -> Seq.drop 1 seq | false -> seq in let s = seq |> Seq.take_while (function '0' .. '9' -> true | _ -> false) |> String.of_seq in let s = match negative with true -> "-" ^ s | false -> s in int_of_string_opt s let set_property key value metadata = { metadata with properties = Properties.add key (Values.singleton value) metadata.properties; } let add_property key value metadata = let properties = Properties.update key (function | None -> Some (Values.singleton value) | Some values -> Some (Values.add value values) ) metadata.properties in if properties == metadata.properties then metadata else { metadata with properties } let property key metadata = Properties.find_opt key metadata.properties let properties metadata = metadata.properties let remove_property key metadata = let properties = Properties.remove key metadata.properties in if properties == metadata.properties then metadata else { metadata with properties } let property_min key metadata = match property key metadata with | Some values -> Values.min_elt_opt values | None -> None let property_list key metadata = match property key metadata with | Some values -> Values.elements values | None -> [] let property_match conv key metadata = match property key metadata with | None -> None | Some values -> let found = Values.find_first_opt (fun s -> Option.is_some (conv s)) values in Option.bind found conv let equal lhs rhs = lhs.audioproperties = rhs.audioproperties && lhs.covers = rhs.covers && Properties.equal Values.equal lhs.properties rhs.properties let is_empty m = equal m empty (* title *) let title metadata = property_min Keys.title metadata let set_title title m = set_property Keys.title title m (* title sort *) let title_sort m = property_min Keys.title_sort m let set_title_sort title m = set_property Keys.title_sort title m (* artist *) let artists metadata = property_list Keys.artist metadata let artist ~sep metadata = String.concat sep (artists metadata) let set_artist artist m = set_property Keys.artist artist m let add_artist artist m = add_property Keys.artist artist m (* artist sort *) let artist_sort = property_min Keys.artist_sort let set_artist_sort value metadata = set_property Keys.artist_sort value metadata (* album artist *) let album_artists m = property_list Keys.album_artist m let album_artist ~sep m = String.concat sep (album_artists m) let set_album_artist artist m = set_property Keys.album_artist artist m let add_album_artist artist m = add_property Keys.album_artist artist m (* composer *) let composers metadata = property_list Keys.composer metadata let composer ~sep m = String.concat sep (composers m) let set_composer artist m = set_property Keys.composer artist m let add_composer artist m = add_property Keys.composer artist m (* album *) let album metadata = property_min Keys.album metadata let set_album album m = set_property Keys.album album m (* album sort *) let album_sort metadata = property_min Keys.album_sort metadata let set_album_sort album m = set_property Keys.album_sort album m (* track *) let track metadata = property_match int_of_string_conv Keys.track metadata let set_track track m = set_property Keys.track (Int.to_string track) m (* comment *) let comments m = property_list Keys.comment m let comment ~sep m = String.concat sep (comments m) let set_comment comment m = set_property Keys.comment comment m let add_comment comment m = add_property Keys.comment comment m (* genre *) let genre m = property_min Keys.genre m let set_genre genre m = set_property Keys.genre genre m (* year *) let year metadata = property_match int_of_string_conv Keys.date metadata let set_year year m = set_property Keys.date (Int.to_string year) m (* disc *) let disc metadata = property_match int_of_string_conv Keys.disc_number metadata let disc' m = Option.value ~default:0 (disc m) let set_disc disc m = set_property Keys.disc_number (Int.to_string disc) m let covers metadata = metadata.covers let add_cover ?(first = true) cover metadata = let covers = match first with | true -> Array.append [| cover |] metadata.covers | false -> Array.append metadata.covers [| cover |] in { metadata with covers } (** [patch base p] updates [base] with the field set in [p]. *) let patch base p = let adefault base p = match p with [||] -> base | p -> p in let properties base p = Properties.merge (fun _ base p -> match p with Some _ -> p | None -> base) base p in { covers = adefault base.covers p.covers; audioproperties = base.audioproperties; properties = properties base.properties p.properties; } let audioproperties metadata = metadata.audioproperties let set_audioproperties audioproperties metadata = { metadata with audioproperties } let set_audioproperties' audioproperties metadata = set_audioproperties (Some audioproperties) metadata let properties_iter f metadata = Properties.iter f metadata.properties let unicode_normalize (form : Uunf.form) metadata = { metadata with properties = Properties.map (Values.map (Uunf_string.normalize_utf_8 form)) metadata.properties; } let is_set field metadata = let is_set_str s = match s with None | Some "" -> false | Some _ -> true in match field with | Field.FTitle -> is_set_str (title metadata) | FArtist -> artists metadata <> [] | FAlbum -> is_set_str (album metadata) | FAlbumArtist -> album_artists metadata <> [] | FTitleSort -> is_set_str (title_sort metadata) | FAlbumSort -> is_set_str (album_sort metadata) | FArtistSort -> is_set_str (artist_sort metadata) | FTrack -> Option.is_some (track metadata) | FYear -> Option.is_some (year metadata) | FDisc -> Option.is_some (disc metadata) | FCover -> Array.length metadata.covers <> 0 | FGenre -> is_set_str (genre metadata) | FComment -> comments metadata <> [] let of_taglib taglib = let module T = Otaglibc in let covers = T.pictures taglib in let audioproperties = T.audioproperties taglib in let keys = T.property_keys taglib in let keys = Option.map (Array.map (fun key -> let p = key |> T.property taglib |> Option.value ~default:[||] |> Array.to_list in (key, p) ) ) keys in let properties = match keys with | None -> Properties.empty | Some keys -> Array.fold_left (fun properties (key, values) -> Properties.update key (function | None -> Some (Values.of_list values) | Some old -> Some (Values.union (Values.of_list values) old) ) properties ) Properties.empty keys in { covers; audioproperties; properties } let apply ~clear_picture ~save metadata taglib = let module T = Otaglibc in let { covers; audioproperties = _; properties } = metadata in let () = Properties.iter (fun key values -> T.set_properties taglib key (Values.elements values)) properties in let () = if clear_picture then T.remove_pictures taglib in let () = Array.iter (T.add_picture taglib) covers in if save then T.save taglib else true let pp format metadata = let { audioproperties; covers; properties } = metadata in let pp_audio = Format.pp_print_option ~none:(fun format () -> Format.fprintf format "none") @@ fun format (audioproperties : Otaglibc.audioproperties) -> Format.fprintf format "bitrate : %u - channels : %u - samplerate : %u - length : %u%a" audioproperties.bitrate audioproperties.channels audioproperties.samplerate audioproperties.length Format.pp_force_newline () in let pp_values format values = Format.pp_print_list ~pp_sep:(fun format () -> Format.fprintf format " - ") Format.pp_print_string format (Values.elements values) in let pp_properties format properties = Properties.iter (fun key values -> Format.fprintf format "%s = %a%a" key pp_values values Format.pp_force_newline () ) properties in Format.fprintf format "%aCovers = %u%a%a" pp_audio audioproperties (Array.length covers) Format.pp_force_newline () pp_properties properties
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>