Packages

A client for abair.ie's Gaeilge voice synthesis

Current section

Files

Jump to
abair src abair.erl
Raw

src/abair.erl

-module(abair).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/abair.gleam").
-export([synthesis_request/1, synthesis_response/1]).
-export_type([synthesised/0, abair_error/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.
-type synthesised() :: {synthesised, list(binary()), bitstring()}.
-type abair_error() :: unexpected_response |
{failed_to_decode_json, gleam@json:decode_error()}.
-file("src/abair.gleam", 14).
-spec synthesised_decoder() -> gleam@dynamic@decode:decoder(synthesised()).
synthesised_decoder() ->
gleam@dynamic@decode:field(
<<"phonemes"/utf8>>,
gleam@dynamic@decode:list(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(Phonemes) ->
gleam@dynamic@decode:field(
<<"audioContent"/utf8>>,
begin
gleam@dynamic@decode:then(
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Text) -> case gleam@bit_array:base64_decode(Text) of
{ok, Audio} ->
gleam@dynamic@decode:success(Audio);
{error, _} ->
gleam@dynamic@decode:failure(
<<>>,
<<"Base64 encoded data"/utf8>>
)
end end
)
end,
fun(Audio@1) ->
gleam@dynamic@decode:success(
{synthesised, Phonemes, Audio@1}
)
end
)
end
).
-file("src/abair.gleam", 32).
?DOC(" Create a request to abair.ie\n").
-spec synthesis_request(binary()) -> gleam@http@request:request(binary()).
synthesis_request(Content) ->
Payload = gleam@json:object(
[{<<"synthinput"/utf8>>,
gleam@json:object(
[{<<"text"/utf8>>, gleam@json:string(Content)},
{<<"ssml"/utf8>>, gleam@json:string(<<"string"/utf8>>)}]
)},
{<<"voiceparams"/utf8>>,
gleam@json:object(
[{<<"languageCode"/utf8>>,
gleam@json:string(<<"ga-IE"/utf8>>)},
{<<"name"/utf8>>,
gleam@json:string(<<"ga_MU_cmg_piper"/utf8>>)},
{<<"ssmlGender"/utf8>>,
gleam@json:string(<<"UNSPECIFIED"/utf8>>)}]
)},
{<<"audioconfig"/utf8>>,
gleam@json:object(
[{<<"audioEncoding"/utf8>>,
gleam@json:string(<<"LINEAR16"/utf8>>)},
{<<"speakingRate"/utf8>>, gleam@json:int(1)},
{<<"volumeGainDb"/utf8>>, gleam@json:int(1)},
{<<"htsParams"/utf8>>,
gleam@json:string(<<"string"/utf8>>)},
{<<"sampleRateHertz"/utf8>>, gleam@json:int(0)},
{<<"effectsProfileId"/utf8>>,
gleam@json:preprocessed_array([])}]
)},
{<<"outputType"/utf8>>, gleam@json:string(<<"JSON"/utf8>>)}]
),
_pipe = gleam@http@request:new(),
_pipe@1 = gleam@http@request:set_method(_pipe, post),
_pipe@2 = gleam@http@request:set_host(_pipe@1, <<"api.abair.ie"/utf8>>),
_pipe@3 = gleam@http@request:set_path(_pipe@2, <<"/v3/synthesis"/utf8>>),
_pipe@4 = gleam@http@request:prepend_header(
_pipe@3,
<<"content-type"/utf8>>,
<<"application/json"/utf8>>
),
gleam@http@request:set_body(_pipe@4, gleam@json:to_string(Payload)).
-file("src/abair.gleam", 72).
?DOC(" Parse a synthesis response from abair.ie\n").
-spec synthesis_response(gleam@http@response:response(binary())) -> {ok,
synthesised()} |
{error, abair_error()}.
synthesis_response(Response) ->
gleam@bool:guard(
erlang:element(2, Response) /= 200,
{error, unexpected_response},
fun() ->
_pipe = gleam@json:parse(
erlang:element(4, Response),
synthesised_decoder()
),
gleam@result:map_error(
_pipe,
fun(Field@0) -> {failed_to_decode_json, Field@0} end
)
end
).