8
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Elixirで「ずんだもん」を喋らせよう

Last updated at Posted at 2023-04-20

Elixirで「ずんだもん」を喋らせたいと一度は思ったことありますよね?
と言うことで実践

前提条件

・OS: Ubuntu 22.04(実機)
・VOICEVOXのLinux版を予めインストール済みであること
・VOICEVOXが起動済みであること

VOICEVOXのLinux版が動かない場合FUSEが必要

ソース作成

プロジェクトの作成

$ mix new voi
$ cd voi

:req, "~> 0.2.1" を追加

mix.exs
defmodule Voi.MixProject do
  use Mix.Project

  def project do
    [
      app: :voi,
      version: "0.1.0",
      elixir: "~> 1.14",
      start_permanent: Mix.env() == :prod,
      deps: deps()
    ]
  end

  # Run "mix help compile.app" to learn about applications.
  def application do
    [
      extra_applications: [:logger]
    ]
  end

  # Run "mix help deps" to learn about dependencies.
  defp deps do
    [
+      {:req, "~> 0.2.1"}
      # {:dep_from_hexpm, "~> 0.3.0"},
      # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
    ]
  end
end

voi.exsを下記に上書きする

lib/voi.exs
defmodule Voi do
  @moduledoc """
  Documentation for `Voi`.
  """

  @doc """
  Hello world.

  ## Examples

      iex> Voi.hello()
      :world

  """
  def hello(text) do
    param = URI.encode_query(%{text: text, speaker: 1})

    json =
      ("http://localhost:50021/audio_query?" <> param)
      |> Req.post!("")
      |> Map.get(:body)
      |> Jason.encode!()

    headers = ["Content-Type": "application/json"]

    data =
      "http://localhost:50021/synthesis?speaker=1"
      |> Req.post!(json, headers: headers)
      |> Map.get(:body)

    File.rm("hoge.wav")
    File.write("hoge.wav", data)
    System.cmd("aplay", ["hoge.wav"])

    :world
  end
end

動かしてみる

iex 起動

$ mix deps.get
$ iex -S mix

Voi.hello関数を実行する

iex> Voi.hello("こんばんは、ymnなのだ")

ずんだもんが喋りました

補足

未検証ですが

System.cmd("aplay", ["hoge.wav"])

のaplayをOSごとにて適切なWav再生コマンドに置き換えれば他のOSでも動くかも
VOICEVOXは、Windows、Mac対応しているようです

実行動画

8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?