diff --git a/lib/live_beats/media_library.ex b/lib/live_beats/media_library.ex index 2d28dbf..f5469e5 100644 --- a/lib/live_beats/media_library.ex +++ b/lib/live_beats/media_library.ex @@ -209,17 +209,7 @@ 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) - - 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}) - segment - end) - - insert_speech_segments(song, segments) - end) + async_text_to_speech(song, user) {ref, song} end) @@ -240,8 +230,17 @@ defmodule LiveBeats.MediaLibrary do end end - defp insert_speech_segments(song, segments) do - Repo.update_all(from(s in Song, where: s.id == ^song.id), set: [speech_segments: segments]) + 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}) + 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 diff --git a/lib/live_beats/media_library/song.ex b/lib/live_beats/media_library/song.ex index c61810a..cf9cf50 100644 --- a/lib/live_beats/media_library/song.ex +++ b/lib/live_beats/media_library/song.ex @@ -51,9 +51,19 @@ defmodule LiveBeats.MediaLibrary.Song do def put_stats(%Ecto.Changeset{} = changeset, %LiveBeats.MP3Stat{} = stat) do changeset |> put_duration(stat.duration) + |> maybe_put(:artist, stat.artist) + |> maybe_put(:attribution, stat.attrib) |> Ecto.Changeset.put_change(:mp3_filesize, stat.size) end + def maybe_put(%Ecto.Changeset{} = changeset, field, value) do + if Ecto.Changeset.get_field(changeset, field) in [nil, ""] do + Ecto.Changeset.change(changeset, %{field => value}) + else + changeset + end + end + defp put_duration(%Ecto.Changeset{} = changeset, duration) when is_integer(duration) do changeset |> Ecto.Changeset.change(%{duration: duration}) diff --git a/lib/live_beats/mp3_stat.ex b/lib/live_beats/mp3_stat.ex index 4647b3b..a464aeb 100644 --- a/lib/live_beats/mp3_stat.ex +++ b/lib/live_beats/mp3_stat.ex @@ -8,7 +8,7 @@ defmodule LiveBeats.MP3Stat do import Bitwise alias LiveBeats.MP3Stat - defstruct duration: 0, size: 0, path: nil, title: nil, artist: nil, tags: nil + defstruct duration: 0, size: 0, path: nil, title: nil, artist: nil, tags: nil, attrib: nil @declared_frame_ids ~w(AENC APIC ASPI COMM COMR ENCR EQU2 ETCO GEOB GRID LINK MCDI MLLT OWNE PRIV PCNT POPM POSS RBUF RVA2 RVRB SEEK SIGN SYLT SYTC TALB TBPM TCOM TCON TCOP TDEN TDLY TDOR TDRC TDRL TDTG TENC TEXT TFLT TIPL TIT1 TIT2 TIT3 TKEY TLAN TLEN TMCL TMED TMOO TOAL TOFN TOLY TOPE TOWN TPE1 TPE2 TPE3 TPE4 TPOS TPRO TPUB TRCK TRSN TRSO TSOA TSOP TSOT TSRC TSSE TSST TXXX UFID USER USLT WCOM WCOP WOAF WOAR WOAS WORS WPAY WPUB WXXX) @@ -42,6 +42,13 @@ defmodule LiveBeats.MP3Stat do duration when is_float(duration) and duration > 0 -> title = Enum.at(tag_info["TIT2"] || [], 0) artist = Enum.at(tag_info["TPE1"] || [], 0) + + attrib = + case tag_info["COMM"] do + {_, _, info} -> info + _ -> nil + end + seconds = round(duration) {:ok, @@ -51,7 +58,8 @@ defmodule LiveBeats.MP3Stat do path: path, tags: tag_info, title: title, - artist: artist + artist: artist, + attrib: attrib }} _other -> diff --git a/lib/live_beats_web/live/profile_live.ex b/lib/live_beats_web/live/profile_live.ex index 15713fc..7c250bd 100644 --- a/lib/live_beats_web/live/profile_live.ex +++ b/lib/live_beats_web/live/profile_live.ex @@ -54,19 +54,6 @@ defmodule LiveBeatsWeb.ProfileLive do -
-
- - [<%= seconds_to_mm_ss(segment.start_time) %>] - - <%= segment.text %> -
-
- uri.path end - - defp seconds_to_mm_ss(seconds) do - seconds |> trunc() |> Time.from_seconds_after_midnight() |> Calendar.strftime("%M:%S") - end end