Skip to content

Commit

Permalink
proxy controller, audio player tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
michelson committed Jun 20, 2022
1 parent 7494a4f commit 112d1ea
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 43 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,17 @@ Now you can visit [`localhost:4000`](http://localhost:4000) from your browser.
- [ ] Playlists
- [ ] Likes
- [ ] Followers / Followings
- [ ] Range responses https://github.com/elixir-plug/plug/pull/526/files
- [ ] load waveform data as data
- [x] Range responses https://github.com/elixir-plug/plug/pull/526/files

with audiowaveform
audiowaveform -i ~/Desktop/patio/STE-098.mp3 -o long_clip.json --pixels-per-second 20 --bits 8

as binary
ffmpeg -i ~/Desktop/patio/STE-098.mp3 -ac 1 -filter:a aresample=200 -map 0:a -c:a pcm_s16le -f data - > SomeFile.txt

as text
ffmpeg -i in.mp3 -af astats=metadata=1:reset=1,ametadata=print:key=lavfi.astats.Overall.RMS_level:file=log.txt -f null -

# Setup:

Expand Down
14 changes: 9 additions & 5 deletions assets/js/controllers/audio_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ export default class extends Controller {
this._wave = WaveSurfer.create({
container: this.playerTarget,
backend: 'MediaElement',
waveColor: 'violet',
progressColor: 'purple',
waveColor: 'grey',
progressColor: 'tomato',
height: this.heightValue || 70,
//partialRender: true,
pixelRatio: 1,
//fillParent: false,
barWidth: 2,
barHeight: 10, // the height of the wave
barGap: null
barGap: null,

cursorWidth: 1,
cursorColor: "lightgray",
normalize: true,
responsive: true,
fillParent: true
})
this._wave.load(this.data.get('url'))
var _this = this
Expand Down
26 changes: 26 additions & 0 deletions lib/rauversion/services/peaks_generator.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule Rauversion.Services.PeaksGenerator do
def run(file) do
{output, _status} =
System.cmd(audiowaveform_path(), [
"-i",
file,
"--pixels-per-second",
"100",
"--output-format",
"json",
"-"
])

IO.inspect(output)

frames = Jason.decode!(output)["data"]

# audiowaveform -i ~/Desktop/patio/STE-098.mp3

# audiowaveform -i /root/audio.mp3 --pixels-per-second 100 --output-format json -
end

def audiowaveform_path() do
"audiowaveform"
end
end
30 changes: 28 additions & 2 deletions lib/rauversion_web/active_storage/blobs/proxy_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
defmodule RauversionWeb.ActiveStorage.Blobs.ProxyController do
use RauversionWeb, :controller

action_fallback RauversionWeb.FallbackController
# action_fallback RauversionWeb.FallbackController

def show(conn, %{"signed_id" => signed_id}) do
case conn |> handle_action(signed_id) do
Expand Down Expand Up @@ -57,7 +57,8 @@ defmodule RauversionWeb.ActiveStorage.Blobs.ProxyController do
send_multipart_ranges(conn, ranges)

_ ->
raise "no ranges???"
conn
|> Plug.Conn.send_resp(422, "not found")
end

_ ->
Expand All @@ -67,7 +68,17 @@ defmodule RauversionWeb.ActiveStorage.Blobs.ProxyController do
#
# send_blob_stream @blob, disposition: params[:disposition]
# end

# disposition: params[:disposition]

conn
|> Plug.Conn.put_resp_header("accept-ranges", "bytes")
|> Plug.Conn.put_resp_header("content-length", conn.assigns.blob.byte_size |> to_string)
|> Plug.Conn.put_resp_header(
"content-disposition",
~s(inline; filename="#{ActiveStorage.Blob.filename(conn.assigns.blob).filename}")
)
|> send_blob_stream(blob)
end
end

Expand Down Expand Up @@ -200,6 +211,21 @@ defmodule RauversionWeb.ActiveStorage.Blobs.ProxyController do
defaults = [disposition: nil]
options = Keyword.merge(defaults, options)

conn =
conn
|> put_resp_content_type(blob.content_type)
|> send_chunked(200)

downloaded_stream =
ActiveStorage.Blob.download(blob, fn chunk_data ->
IO.inspect(chunk_data)
IO.inspect("chunk_data: !!")
chunk(conn, chunk_data)
end)

{:ok, conn} = downloaded_stream
conn

# send_stream(
# filename: blob.filename.sanitized,
# disposition: blob.forced_disposition_for_serving || disposition || DEFAULT_BLOB_STREAMING_DISPOSITION,
Expand Down
12 changes: 6 additions & 6 deletions lib/rauversion_web/live/track_live/components/track_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ defmodule RauversionWeb.TrackLive.TrackComponent do
def render(%{track: track, current_user: current_user} = assigns) do
~H"""
<div class="sm:flex border rounded-md shadow-sm my-2">
<div class="w-1/4 mb-4 flex-shrink-0 sm:mb-0 sm:mr-4">
<div class="flex flex-col sm:flex-row border rounded-md shadow-sm my-2">
<div class="w-full sm:w-1/4 mb-4 flex-shrink-0 sm:mb-0 sm:mr-4">
<div class="group relative aspect-w-1 aspect-h-1 rounded-md bg-gray-100 overflow-hidden">
<% #= image_tag url_for(track.cover.variant(resize_to_fit: [300, 300])), class: "object-center object-cover group-hover:opacity-75" %>
Expand All @@ -32,12 +32,12 @@ defmodule RauversionWeb.TrackLive.TrackComponent do
<% # if track.audio.persisted? %>
<%= content_tag :div, "data-controller": "audio",
"data-audio-target": "player",
"data-audio-height-value": 70,
"data-audio-target": "player",
"data-audio-height-value": 70,
"data-audio-url": Rauversion.Tracks.blob_proxy_url(track, "audio"),
class: "h-32" do %>
<div class='controls flex items-center'>
<span class="relative z-0 inline-flex py-2">
<span class="relative z-0 inline-flex py-2 px-2 sm:px-0">
<button type="button"
data-action='audio#play'
data-audio-target="play"
Expand Down Expand Up @@ -71,7 +71,7 @@ defmodule RauversionWeb.TrackLive.TrackComponent do
<%= track.description %>
</p>
<div class="pt-2" data-turbo="false">
<div class="p-2 sm:p-0 sm:pt-2" data-turbo="false">
<%= live_redirect "Show", to: Routes.track_show_path(@socket, :show, track), class: "inline-flex items-center px-2.5 py-1.5 border border-gray-300 shadow-sm text-xs font-medium rounded text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" %>
<%= if current_user && current_user.id == track.user_id do %>
<%= live_patch "Edit", to: Routes.track_index_path(@socket, :edit, track), class: "inline-flex items-center px-2.5 py-1.5 border border-gray-300 shadow-sm text-xs font-medium rounded text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" %>
Expand Down
2 changes: 1 addition & 1 deletion lib/rauversion_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule RauversionWeb.Router do

scope "/" do
pipe_through :browser
live_dashboard "/dashboard", metrics: RauversionWeb.Telemetry
live_dashboard "/dashboard", metrics: RauversionWeb.Telemetry, ecto_repos: [Rauversion.Repo]
end
end

Expand Down
91 changes: 68 additions & 23 deletions lib/rauversion_web/templates/layout/root.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,79 @@
<%= live_title_tag assigns[:page_title] || "Rauversion", suffix: " · Phoenix Framework" %>
<link phx-track-static rel="stylesheet" href={Routes.static_path(@conn, "/assets/app.css")}/>
<script defer phx-track-static type="text/javascript" src={Routes.static_path(@conn, "/assets/app.js")}></script>


<link rel="stylesheet" href="https://unpkg.com/@cassette/player/dist/css/cassette-player.css">
<!-- dependencies -->
<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/[email protected]/prop-types.js"></script>
<script src="https://unpkg.com/[email protected]/dist/ResizeObserver.js"></script>

<!-- other @cassette packages which are dependencies -->
<script src="https://unpkg.com/@cassette/[email protected]/dist/es5/cassette-core.js"></script>
<script src="https://unpkg.com/@cassette/[email protected]/dist/es5/cassette-components.js"></script>

<!-- @cassette/player's javascript -->
<script src="https://unpkg.com/@cassette/[email protected]/dist/es5/cassette-player.js"></script>

</head>
<body>

<%= render "_user_menu.html", assigns %>

<p class="notice"></p>
<p class="alert"></p>
<%= @inner_content %>


<%= if System.get_env("CHASKIQ_APP_ID") do %>
<script>
(function(d,t) {
var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
g.src="https://app.chaskiq.io/embed.js"
s.parentNode.insertBefore(g,s);
g.onload=function(){
new window.ChaskiqMessengerEncrypted({
domain: 'https://app.chaskiq.io',
ws: 'wss://app.chaskiq.io/cable',
app_id: "<%= System.get_env("CHASKIQ_APP_ID") %>",
//data: "YOUR_ENCRYPTED_JWE_DATA",
//lang: "USER_LANG_OR_DEFAULTS_TO_BROWSER_LANG"
})
}
})(document,"script");
</script>
<% end %>
<p class="notice"></p>
<p class="alert"></p>
<%= @inner_content %>

<div id="app">app</div>

<%= if System.get_env("CHASKIQ_APP_ID") do %>
<script>
(function(d,t) {
var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
g.src="https://app.chaskiq.io/embed.js"
s.parentNode.insertBefore(g,s);
g.onload=function(){
new window.ChaskiqMessengerEncrypted({
domain: 'https://app.chaskiq.io',
ws: 'wss://app.chaskiq.io/cable',
app_id: "<%= System.get_env("CHASKIQ_APP_ID") %>",
//data: "YOUR_ENCRYPTED_JWE_DATA",
//lang: "USER_LANG_OR_DEFAULTS_TO_BROWSER_LANG"
})
}
})(document,"script");
</script>
<% end %>

<script>
// your code!


var MediaPlayer = cassettePlayer.MediaPlayer;

var playlist = [
{
url:
'http://localhost:4000/active_storage/blobs/proxy/SFMyNTY.eyJleHBpcmVzX2luIjpudWxsLCJtZXNzYWdlIjoiNDciLCJwdXJwb3NlIjpudWxsfQ.AO68siAf82PqS4YOCET8OZlUB3MoMCKPcOjnJv-4yYs/STE-064.mp3',
title: 'Big Buck Bunny'
}
];

ReactDOM.render(
React.createElement(
MediaPlayer,
{
playlist: playlist,
showVideo: false
}
),
document.getElementById('app')
);

</script>

</body>
</html>

Expand Down
9 changes: 5 additions & 4 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ defmodule Rauversion.MixProject do
{:phoenix_ecto, "~> 4.4"},
{:ecto_sql, "~> 3.7.2"},
{:postgrex, ">= 0.0.0"},
{:ecto_psql_extras, "~> 0.6"},
{:phoenix_html, "~> 3.0"},
{:phoenix_live_reload, "~> 1.2", only: :dev},
{:phoenix_live_view, "~> 0.17.0"},
Expand All @@ -57,11 +58,11 @@ defmodule Rauversion.MixProject do
{
:active_storage,
git: "https://github.com/chaskiq/ex-rails.git", sparse: "apps/active_storage"
},
{
:active_job,
"0.1.1"
}
# {
# :active_job,
# "0.1.1"
# }
# {:active_storage,
# path: "/Users/michelson/Documents/chaskiq/chaskiq-phoenix/ex_rails/apps/active_storage"}
# {:active_job,
Expand Down
4 changes: 3 additions & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%{
"active_job": {:hex, :active_job, "0.1.1", "e0a8c5beee0f4aca70c900aee23d54caf282eb541204b43375402d6504a0ac21", [:mix], [{:ecto, "~> 3.7.2", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.7.2", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:exq, "~> 0.16.2", [hex: :exq, repo: "hexpm", optional: true]}, {:oban, "~> 2.12", [hex: :oban, repo: "hexpm", optional: true]}], "hexpm", "3c3ed5d413a174dbb63f7481ec0d34d47a95eb4ba4dee76470bc5577d9f5fd22"},
"active_storage": {:git, "https://github.com/chaskiq/ex-rails.git", "432241fc949a846c6f5473373c6486c7b96de3e4", [sparse: "apps/active_storage"]},
"active_storage": {:git, "https://github.com/chaskiq/ex-rails.git", "01a8a49ce96f211e40d8abd1b20270eee88af949", [sparse: "apps/active_storage"]},
"bcrypt_elixir": {:hex, :bcrypt_elixir, "3.0.0", "851d16b901b6c94a0ceadd3470f2b58d9899007bf04a42e0e4b399e2dd6ab307", [:make, :mix], [{:comeonin, "~> 5.3", [hex: :comeonin, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "fe251accb51f1aa17d67009cb125bddf81fb056cacad71bfad7827a44652f4ee"},
"castore": {:hex, :castore, "0.1.17", "ba672681de4e51ed8ec1f74ed624d104c0db72742ea1a5e74edbc770c815182f", [:mix], [], "hexpm", "d9844227ed52d26e7519224525cb6868650c272d4a3d327ce3ca5570c12163f9"},
"certifi": {:hex, :certifi, "2.9.0", "6f2a475689dd47f19fb74334859d460a2dc4e3252a3324bd2111b8f0429e7e21", [:rebar3], [], "hexpm", "266da46bdb06d6c6d35fde799bcb28d36d985d424ad7c08b5bb48f5b5cdd4641"},
Expand All @@ -15,6 +15,7 @@
"dotenv": {:hex, :dotenv, "3.0.0", "52a28976955070d8312a81d59105b57ecf5d6a755c728b49c70a7e2120e6cb40", [:mix], [], "hexpm", "f8a7d800b6b419a8d8a8bc5b5cd820a181c2b713aab7621794febe934f7bd84e"},
"ecto": {:hex, :ecto, "3.7.2", "44c034f88e1980754983cc4400585970b4206841f6f3780967a65a9150ef09a8", [: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", "a600da5772d1c31abbf06f3e4a1ffb150e74ed3e2aa92ff3cee95901657a874e"},
"ecto_autoslug_field": {:hex, :ecto_autoslug_field, "3.0.0", "37fbc2f07e6691136afff246f2cf5b159ad395b665a55d06db918975fd2397db", [:mix], [{:ecto, ">= 3.7.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:slugger, ">= 0.3.0", [hex: :slugger, repo: "hexpm", optional: false]}], "hexpm", "8ec252c7cf85f13132062f56a484d6a0ef1f981f7be9ce4ad7e9546dd8c0cc0f"},
"ecto_psql_extras": {:hex, :ecto_psql_extras, "0.7.4", "5d43fd088d39a158c860b17e8d210669587f63ec89ea122a4654861c8c6e2db4", [:mix], [{:ecto_sql, "~> 3.4", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:postgrex, ">= 0.15.7", [hex: :postgrex, repo: "hexpm", optional: false]}, {:table_rex, "~> 3.1.1", [hex: :table_rex, repo: "hexpm", optional: false]}], "hexpm", "311db02f1b772e3d0dc7f56a05044b5e1499d78ed6abf38885e1ca70059449e5"},
"ecto_sql": {:hex, :ecto_sql, "3.7.2", "55c60aa3a06168912abf145c6df38b0295c34118c3624cf7a6977cd6ce043081", [:mix], [{:db_connection, "~> 2.2", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.7.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.4.0 or ~> 0.5.0 or ~> 0.6.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0 or ~> 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", "3c218ea62f305dcaef0b915fb56583195e7b91c91dcfb006ba1f669bfacbff2a"},
"elixir_make": {:hex, :elixir_make, "0.6.3", "bc07d53221216838d79e03a8019d0839786703129599e9619f4ab74c8c096eac", [:mix], [], "hexpm", "f5cbd651c5678bcaabdbb7857658ee106b12509cd976c2c2fca99688e1daf716"},
"elixir_uuid": {:hex, :elixir_uuid, "1.2.1", "dce506597acb7e6b0daeaff52ff6a9043f5919a4c3315abb4143f0b00378c097", [:mix], [], "hexpm", "f7eba2ea6c3555cea09706492716b0d87397b88946e6380898c2889d68585752"},
Expand Down Expand Up @@ -55,6 +56,7 @@
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
"sweet_xml": {:hex, :sweet_xml, "0.7.3", "debb256781c75ff6a8c5cbf7981146312b66f044a2898f453709a53e5031b45b", [:mix], [], "hexpm", "e110c867a1b3fe74bfc7dd9893aa851f0eed5518d0d7cad76d7baafd30e4f5ba"},
"swoosh": {:hex, :swoosh, "1.7.1", "53bb5b8efaf5577bbad1480fb057d8bcb7b16097aa015ab9f369a01592c961ec", [:mix], [{:cowboy, "~> 1.1 or ~> 2.4", [hex: :cowboy, repo: "hexpm", optional: true]}, {:ex_aws, "~> 2.1", [hex: :ex_aws, repo: "hexpm", optional: true]}, {:finch, "~> 0.6", [hex: :finch, repo: "hexpm", optional: true]}, {:gen_smtp, "~> 0.13 or ~> 1.0", [hex: :gen_smtp, repo: "hexpm", optional: true]}, {:hackney, "~> 1.9", [hex: :hackney, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mail, "~> 0.2", [hex: :mail, repo: "hexpm", optional: true]}, {:mime, "~> 1.1 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_cowboy, ">= 1.0.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.2 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "8206d7da5038f0e11292b775b7e588fcd5dc74ac4dd8b661fe72f58207234747"},
"table_rex": {:hex, :table_rex, "3.1.1", "0c67164d1714b5e806d5067c1e96ff098ba7ae79413cc075973e17c38a587caa", [:mix], [], "hexpm", "678a23aba4d670419c23c17790f9dcd635a4a89022040df7d5d772cb21012490"},
"tailwind": {:hex, :tailwind, "0.1.6", "d8d05ed1b8bfcfdc896f107f5bc0c740d7d509d6d0f1cb381d58aabff2ddff39", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}], "hexpm", "41ccc86cc42ba97bef23391f21548694709da0c3001714a971fa008ab12ae170"},
"telemetry": {:hex, :telemetry, "1.1.0", "a589817034a27eab11144ad24d5c0f9fab1f58173274b1e9bae7074af9cbee51", [:rebar3], [], "hexpm", "b727b2a1f75614774cff2d7565b64d0dfa5bd52ba517f16543e6fc7efcc0df48"},
"telemetry_metrics": {:hex, :telemetry_metrics, "0.6.1", "315d9163a1d4660aedc3fee73f33f1d355dcc76c5c3ab3d59e76e3edf80eef1f", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7be9e0871c41732c233be71e4be11b96e56177bf15dde64a8ac9ce72ac9834c6"},
Expand Down
11 changes: 11 additions & 0 deletions test/rauversion/peak_genenerator_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule Rauversion.PeaksGeneratorTest do
use Rauversion.DataCase

alias Rauversion.Playlists

describe "peaks generator" do
test "peaks gen" do
Rauversion.Services.PeaksGenerator.run("/Users/michelson/Desktop/patio/STE-098.mp3")
end
end
end

0 comments on commit 112d1ea

Please sign in to comment.