This commit is contained in:
Chris McCord 2023-03-14 10:18:57 -04:00
parent 5384d9ee4b
commit 54ab46f72d
3 changed files with 10 additions and 21 deletions

View file

@ -6,7 +6,7 @@ defmodule LiveBeats.MediaLibrary do
require Logger
import Ecto.Query, warn: false
alias LiveBeats.{Repo, MP3Stat, Accounts}
alias LiveBeats.MediaLibrary.{Profile, Song, Events, Genre, TextSegment}
alias LiveBeats.MediaLibrary.{Profile, Song, Events, Genre}
alias Ecto.{Multi, Changeset}
@pubsub LiveBeats.PubSub
@ -79,9 +79,10 @@ defmodule LiveBeats.MediaLibrary do
})
stopped_query =
from s in Song,
from(s in Song,
where: s.user_id == ^song.user_id and s.status in [:playing, :paused],
update: [set: [status: :stopped]]
)
{:ok, %{now_playing: new_song}} =
Multi.new()
@ -102,9 +103,10 @@ defmodule LiveBeats.MediaLibrary do
pause_query = from(s in Song, where: s.id == ^song.id, update: [set: ^set])
stopped_query =
from s in Song,
from(s in Song,
where: s.user_id == ^song.user_id and s.status in [:playing, :paused],
update: [set: [status: :stopped]]
)
{:ok, _} =
Multi.new()
@ -165,7 +167,7 @@ defmodule LiveBeats.MediaLibrary do
Ecto.Multi.new()
|> lock_playlist(user.id)
|> Ecto.Multi.run(:starting_position, fn repo, _changes ->
count = repo.one(from s in Song, where: s.user_id == ^user.id, select: count(s.id))
count = repo.one(from(s in Song, where: s.user_id == ^user.id, select: count(s.id)))
{:ok, count - 1}
end)
@ -209,7 +211,6 @@ defmodule LiveBeats.MediaLibrary do
|> Enum.filter(&match?({{:song, _ref}, _}, &1))
|> Enum.map(fn {{:song, ref}, song} ->
consume_file.(ref, fn tmp_path -> store_mp3(song, tmp_path) end)
async_text_to_speech(song, user)
{ref, song}
end)
@ -230,19 +231,6 @@ defmodule LiveBeats.MediaLibrary do
end
end
defp async_text_to_speech(%Song{} = song, %Accounts.User{} = user) do
Task.Supervisor.start_child(LiveBeats.TaskSupervisor, fn ->
segments =
LiveBeats.Audio.speech_to_text(song.mp3_filepath, 20.0, fn ss, text ->
segment = %TextSegment{start_time: ss, text: text}
broadcast!(user.id, %Events.SpeechToText{song_id: song.id, segment: %{segment | in_progress?: true}})
segment
end)
Repo.update_all(from(s in Song, where: s.id == ^song.id), set: [speech_segments: segments])
end)
end
defp broadcast_imported(%Accounts.User{} = user, songs) do
songs = Enum.map(songs, fn {_ref, song} -> song end)
broadcast!(user.id, %Events.SongsImported{user_id: user.id, songs: songs})
@ -289,7 +277,7 @@ defmodule LiveBeats.MediaLibrary do
def get_current_active_song(%Profile{user_id: user_id}) do
Repo.replica().one(
from s in Song, where: s.user_id == ^user_id and s.status in [:playing, :paused]
from(s in Song, where: s.user_id == ^user_id and s.status in [:playing, :paused])
)
end
@ -376,7 +364,7 @@ defmodule LiveBeats.MediaLibrary do
Ecto.Multi.new()
|> lock_playlist(song.user_id)
|> Ecto.Multi.run(:index, fn repo, _changes ->
case repo.one(from s in Song, where: s.user_id == ^song.user_id, select: count(s.id)) do
case repo.one(from(s in Song, where: s.user_id == ^song.user_id, select: count(s.id))) do
count when new_index < count -> {:ok, new_index}
count -> {:ok, count - 1}
end

View file

@ -319,6 +319,7 @@ defmodule LiveBeatsWeb.ProfileLive do
stream_insert(socket, :songs, song)
active_song_id ->
socket
|> stop_song(active_song_id)
|> stream_insert(:songs, song)
|> assign(active_song_id: song.id)

View file

@ -9,7 +9,7 @@
"cowlib": {:hex, :cowlib, "2.11.0", "0b9ff9c346629256c42ebe1eeb769a83c6cb771a6ee5960bd110ab0b9b872063", [:make, :rebar3], [], "hexpm", "2b3e9da0b21c4565751a6d4901c20d1b4cc25cbb7fd50d91d2ab6dd287bc86a9"},
"db_connection": {:hex, :db_connection, "2.4.2", "f92e79aff2375299a16bcb069a14ee8615c3414863a6fef93156aee8e86c2ff3", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "4fe53ca91b99f55ea249693a0229356a08f4d1a7931d8ffa79289b145fe83668"},
"decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"},
"ecto": {:hex, :ecto, "3.9.1", "67173b1687afeb68ce805ee7420b4261649d5e2deed8fe5550df23bab0bc4396", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c80bb3d736648df790f7f92f81b36c922d9dd3203ca65be4ff01d067f54eb304"},
"ecto": {:hex, :ecto, "3.9.4", "3ee68e25dbe0c36f980f1ba5dd41ee0d3eb0873bccae8aeaf1a2647242bffa35", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "de5f988c142a3aa4ec18b85a4ec34a2390b65b24f02385c1144252ff6ff8ee75"},
"ecto_network": {:hex, :ecto_network, "1.3.0", "1e77fa37c20e0f6a426d3862732f3317b0fa4c18f123d325f81752a491d7304e", [:mix], [{:ecto_sql, ">= 3.0.0", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:phoenix_html, ">= 0.0.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:postgrex, ">= 0.14.0", [hex: :postgrex, repo: "hexpm", optional: false]}], "hexpm", "053a5e46ef2837e8ea5ea97c82fa0f5494699209eddd764e663c85f11b2865bd"},
"ecto_sql": {:hex, :ecto_sql, "3.9.0", "2bb21210a2a13317e098a420a8c1cc58b0c3421ab8e3acfa96417dab7817918c", [:mix], [{:db_connection, "~> 2.5 or ~> 2.4.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.9.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.6.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.16.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a8f3f720073b8b1ac4c978be25fa7960ed7fd44997420c304a4a2e200b596453"},
"elixir_make": {:hex, :elixir_make, "0.7.5", "784cc00f5fa24239067cc04d449437dcc5f59353c44eb08f188b2b146568738a", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: true]}], "hexpm", "c3d63e8d5c92fa3880d89ecd41de59473fa2e83eeb68148155e25e8b95aa2887"},