Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into bigarray2
Browse files Browse the repository at this point in the history
  • Loading branch information
toots committed Dec 13, 2023
2 parents 6853e84 + 11749ea commit 518938c
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 66 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ all: build
build:
@dune build

doc:
@dune build @doc

clean:
@dune clean
16 changes: 4 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,23 @@ Basic usage is

```ocaml
let () =
let filename = "test.mp3" in
let metadata = Metadata.parse_file filename in
let metadata = Metadata.parse_file "test.mp3" in
List.iter (fun (k,v) -> Printf.printf "- %s: %s\n" k v) metadata
```

In the above example, the function `Metadata.Any.parse_file` takes a file name
as argument and returns an association list describing its metadata. It consists
of pairs of strings being the name of the metadata and its value.
In the above example, the function `Metadata.parse_file` takes a file name as
argument and returns an association list describing its metadata. It consists of
pairs of strings being the name of the metadata and its value.

Installing
----------

The preferred way is via opam:

```bash
opam pin add .
opam install metadata
```

It can also be installed via dune:

```
dune install
```

Other libraries
---------------

Expand Down
5 changes: 2 additions & 3 deletions examples/basic.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(** Basic usage of the library. *)

let () =
let filename = "test.mp3" in
let metadata = Metadata.parse_file filename in
List.iter (fun (k,v) -> Printf.printf "- %s: %s\n" k v) metadata
let metadata = Metadata.parse_file "test.mp3" in
List.iter (fun (k, v) -> Printf.printf "- %s: %s\n" k v) metadata
2 changes: 1 addition & 1 deletion examples/meta.ml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ let () =
Printf.printf "\n# Metadata for %s\n\n%!" fname;
(* Store "APIC" as custom tag. *)
let apic_tag = ref None in
let custom_parser ?read_ba ~read:_ ~length:_ ~label () =
let custom_parser { Metadata.read_ba; label; _ } =
match (label, read_ba) with
| "APIC", Some r -> apic_tag := Some (r ())
| _ -> ()
Expand Down
7 changes: 7 additions & 0 deletions src/metadata.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Make (E : CharEncoding.T) = struct
module AVI = MetadataAVI
module MP4 = MetadataMP4

(** Charset conversion function. *)
let recode = E.convert

module ID3 = struct
Expand Down Expand Up @@ -65,10 +66,16 @@ module Make (E : CharEncoding.T) = struct

module Any = struct
let parsers = Audio.parsers @ Image.parsers @ Video.parsers

(** Genering parsing of metadata. *)
let parse = first_valid parsers

let parse_file ?custom_parser file =
Reader.with_file ?custom_parser parse file

(** Parse the metadatas of a string. *)
let parse_string ?custom_parser file =
Reader.with_string ?custom_parser parse file
end

include Any
Expand Down
53 changes: 12 additions & 41 deletions src/metadata.mli
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ module Make : functor (_ : CharEncoding.T) -> sig
Currently only supported for: ID3v2, MP4 and [metadata_block_picture] in
FLAC metadata. *)
type custom_parser =
?read_ba:(unit -> bigarray) ->
read:(unit -> string) ->
length:int ->
label:string ->
unit ->
unit
type parser_handler = MetadataBase.parser_handler = {
label : string;
length : int;
read : unit -> string;
read_ba : (unit -> bigarray) option;
skip : unit -> unit;
}

type custom_parser = parser_handler -> unit

module Reader : sig
(** A function to read taking the buffer to fill the offset and the length and
Expand Down Expand Up @@ -62,40 +64,6 @@ module Make : functor (_ : CharEncoding.T) -> sig
?custom_parser:custom_parser -> (t -> metadata) -> string -> metadata
end

module Int : sig
type t = int

val zero : int
val one : int
val minus_one : int
external neg : int -> int = "%negint"
external add : int -> int -> int = "%addint"
external sub : int -> int -> int = "%subint"
external mul : int -> int -> int = "%mulint"
external div : int -> int -> int = "%divint"
external rem : int -> int -> int = "%modint"
external succ : int -> int = "%succint"
external pred : int -> int = "%predint"
val abs : int -> int
val max_int : int
val min_int : int
external logand : int -> int -> int = "%andint"
external logor : int -> int -> int = "%orint"
external logxor : int -> int -> int = "%xorint"
val lognot : int -> int
external shift_left : int -> int -> int = "%lslint"
external shift_right : int -> int -> int = "%asrint"
external shift_right_logical : int -> int -> int = "%lsrint"
val equal : int -> int -> bool
val compare : int -> int -> int
val min : int -> int -> int
val max : int -> int -> int
external to_float : int -> float = "%floatofint"
external of_float : float -> int = "%intoffloat"
val to_string : int -> string
val find : (int -> bool) -> int
end

module ID3v1 = MetadataID3v1
module ID3v2 = MetadataID3v2
module OGG = MetadataOGG
Expand Down Expand Up @@ -151,6 +119,9 @@ module Make : functor (_ : CharEncoding.T) -> sig

val parse_file :
?custom_parser:custom_parser -> string -> MetadataBase.metadata

val parse_string :
?custom_parser:custom_parser -> string -> MetadataBase.metadata
end

val parsers : (Reader.t -> MetadataBase.metadata) list
Expand Down
24 changes: 16 additions & 8 deletions src/metadataBase.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ type bigarray =
type metadata = (string * string) list
type endianness = Big_endian | Little_endian

type custom_parser =
?read_ba:(unit -> bigarray) ->
read:(unit -> string) ->
length:int ->
label:string ->
unit ->
unit
type parser_handler = {
label : string;
length : int;
read : unit -> string;
read_ba : (unit -> bigarray) option;
skip : unit -> unit;
}

type custom_parser = parser_handler -> unit

module Reader = struct
(** A function to read taking the buffer to fill the offset and the length and
returning the number of bytes actually read. *)
type t = {
read : bytes -> int -> int -> int;
read_ba : (int -> bigarray) option;
Expand Down Expand Up @@ -49,6 +53,10 @@ module Reader = struct
| None -> false
| Some custom_parser ->
let is_custom = ref false in
let skip () =
is_custom := true;
f.seek length
in
let read () =
is_custom := true;
read f length
Expand All @@ -60,7 +68,7 @@ module Reader = struct
read_ba length)
f.read_ba
in
custom_parser ?read_ba ~read ~length ~label ();
custom_parser { read_ba; read; skip; length; label };
!is_custom
in
if is_custom then None else Some (read f length)
Expand Down
2 changes: 1 addition & 1 deletion test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ let () =
]
in
let custom_parser_labels = ref [] in
let custom_parser ?read_ba:_ ~read ~length ~label () =
let custom_parser { Metadata.read; length; label; _ } =
custom_parser_labels := label :: !custom_parser_labels;
match label with
| "TIT2" ->
Expand Down

0 comments on commit 518938c

Please sign in to comment.