Packages

Gleam bindings for the Rockbox DSP / metadata / playback engine

Current section

Files

Jump to
rockbox_ffi src rockbox@metadata.erl
Raw

src/rockbox@metadata.erl

-module(rockbox@metadata).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/rockbox/metadata.gleam").
-export([read/1, probe/1, decoder/0]).
-export_type([replay_gain/0, metadata/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(" Audio file metadata / tag parsing.\n").
-type replay_gain() :: {replay_gain,
gleam@option:option(float()),
gleam@option:option(float()),
gleam@option:option(float()),
gleam@option:option(float()),
integer(),
integer(),
integer(),
integer()}.
-type metadata() :: {metadata,
binary(),
integer(),
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
gleam@option:option(integer()),
gleam@option:option(integer()),
gleam@option:option(integer()),
integer(),
integer(),
integer(),
integer(),
integer(),
boolean(),
replay_gain()}.
-file("src/rockbox/metadata.gleam", 145).
-spec optional_float() -> gleam@dynamic@decode:decoder(gleam@option:option(float())).
optional_float() ->
gleam@dynamic@decode:one_of(
gleam@dynamic@decode:map(
{decoder, fun gleam@dynamic@decode:decode_float/1},
fun(Field@0) -> {some, Field@0} end
),
[gleam@dynamic@decode:success(none)]
).
-file("src/rockbox/metadata.gleam", 118).
-spec replaygain_decoder() -> gleam@dynamic@decode:decoder(replay_gain()).
replaygain_decoder() ->
gleam@dynamic@decode:field(
<<"track_gain_db"/utf8>>,
optional_float(),
fun(Track_gain_db) ->
gleam@dynamic@decode:field(
<<"album_gain_db"/utf8>>,
optional_float(),
fun(Album_gain_db) ->
gleam@dynamic@decode:field(
<<"track_peak"/utf8>>,
optional_float(),
fun(Track_peak) ->
gleam@dynamic@decode:field(
<<"album_peak"/utf8>>,
optional_float(),
fun(Album_peak) ->
gleam@dynamic@decode:field(
<<"raw_track_gain"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_int/1},
fun(Raw_track_gain) ->
gleam@dynamic@decode:field(
<<"raw_album_gain"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_int/1},
fun(Raw_album_gain) ->
gleam@dynamic@decode:field(
<<"raw_track_peak"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_int/1},
fun(Raw_track_peak) ->
gleam@dynamic@decode:field(
<<"raw_album_peak"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_int/1},
fun(
Raw_album_peak
) ->
gleam@dynamic@decode:success(
{replay_gain,
Track_gain_db,
Album_gain_db,
Track_peak,
Album_peak,
Raw_track_gain,
Raw_album_gain,
Raw_track_peak,
Raw_album_peak}
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
).
-file("src/rockbox/metadata.gleam", 141).
-spec optional_int() -> gleam@dynamic@decode:decoder(gleam@option:option(integer())).
optional_int() ->
gleam@dynamic@decode:one_of(
gleam@dynamic@decode:map(
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Field@0) -> {some, Field@0} end
),
[gleam@dynamic@decode:success(none)]
).
-file("src/rockbox/metadata.gleam", 77).
-spec metadata_decoder() -> gleam@dynamic@decode:decoder(metadata()).
metadata_decoder() ->
gleam@dynamic@decode:field(
<<"codec"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Codec) ->
gleam@dynamic@decode:field(
<<"codec_id"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Codec_id) ->
gleam@dynamic@decode:field(
<<"title"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Title) ->
gleam@dynamic@decode:field(
<<"artist"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(Artist) ->
gleam@dynamic@decode:field(
<<"album"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(Album) ->
gleam@dynamic@decode:field(
<<"albumartist"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(Albumartist) ->
gleam@dynamic@decode:field(
<<"composer"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(Composer) ->
gleam@dynamic@decode:field(
<<"genre"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(Genre) ->
gleam@dynamic@decode:field(
<<"year"/utf8>>,
optional_int(
),
fun(
Year
) ->
gleam@dynamic@decode:field(
<<"track_number"/utf8>>,
optional_int(
),
fun(
Track_number
) ->
gleam@dynamic@decode:field(
<<"disc_number"/utf8>>,
optional_int(
),
fun(
Disc_number
) ->
gleam@dynamic@decode:field(
<<"duration_ms"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_int/1},
fun(
Duration_ms
) ->
gleam@dynamic@decode:field(
<<"bitrate"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_int/1},
fun(
Bitrate
) ->
gleam@dynamic@decode:field(
<<"sample_rate"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_int/1},
fun(
Sample_rate
) ->
gleam@dynamic@decode:field(
<<"filesize"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_int/1},
fun(
Filesize
) ->
gleam@dynamic@decode:field(
<<"samples"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_int/1},
fun(
Samples
) ->
gleam@dynamic@decode:field(
<<"vbr"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_bool/1},
fun(
Vbr
) ->
gleam@dynamic@decode:field(
<<"replaygain"/utf8>>,
replaygain_decoder(
),
fun(
Replaygain
) ->
gleam@dynamic@decode:success(
{metadata,
Codec,
Codec_id,
Title,
Artist,
Album,
Albumartist,
Composer,
Genre,
Year,
Track_number,
Disc_number,
Duration_ms,
Bitrate,
Sample_rate,
Filesize,
Samples,
Vbr,
Replaygain}
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
).
-file("src/rockbox/metadata.gleam", 49).
?DOC(
" Parse the metadata of the audio file at `path`. `Error(Nil)` if the file\n"
" cannot be opened, no parser recognises it, or the JSON fails to decode.\n"
).
-spec read(binary()) -> {ok, metadata()} | {error, nil}.
read(Path) ->
case gleam@dynamic@decode:run(
rockbox_ffi_nif:meta_read_json(Path),
{decoder, fun gleam@dynamic@decode:decode_string/1}
) of
{ok, Json} ->
case gleam@dynamic@decode:run(json:decode(Json), metadata_decoder()) of
{ok, Meta} ->
{ok, Meta};
{error, _} ->
{error, nil}
end;
{error, _} ->
{error, nil}
end.
-file("src/rockbox/metadata.gleam", 62).
?DOC(
" Guess the codec label (e.g. `\"FLAC\"`) from a filename's extension without\n"
" opening the file. `None` for an unknown extension.\n"
).
-spec probe(binary()) -> gleam@option:option(binary()).
probe(Filename) ->
case gleam@dynamic@decode:run(
rockbox_ffi_nif:meta_probe(Filename),
{decoder, fun gleam@dynamic@decode:decode_string/1}
) of
{ok, Label} ->
{some, Label};
{error, _} ->
none
end.
-file("src/rockbox/metadata.gleam", 73).
?DOC(
" The `Metadata` JSON decoder, shared with `rockbox/decoder` (which reads the\n"
" same JSON shape straight from an open codec).\n"
).
-spec decoder() -> gleam@dynamic@decode:decoder(metadata()).
decoder() ->
metadata_decoder().