Skip to content

Commit

Permalink
Fix creating audio_image spectrogram
Browse files Browse the repository at this point in the history
  • Loading branch information
skanderm committed Sep 9, 2024
1 parent 02c3fd7 commit 2603b35
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 35 deletions.
2 changes: 2 additions & 0 deletions server/audio_viz/core/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def lambda_handler(event, context):
}

def make_spectrogram(job: SpectrogramJob, store_audio=False, show_image=False, store_image=False):
print(f"Received job: {json.dumps(job)}")
local_path = f"/tmp/{job["id"]}"
s3 = boto3.client("s3")

Expand Down Expand Up @@ -91,6 +92,7 @@ def make_spectrogram(job: SpectrogramJob, store_audio=False, show_image=False, s
else:
image_store.seek(0)
image_size = sys.getsizeof(image_store)
image_store.seek(0)

if job["image_key"] is not None and job["image_bucket"] is not None:
image_content = open(image_store, "rb") if store_image else image_store
Expand Down
5 changes: 3 additions & 2 deletions server/lib/orcasite/global_setup.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ defmodule Orcasite.GlobalSetup do

def populate_latest_feed_segments(feed) do
feed
|> Orcasite.Radio.AwsClient.request_timestamps()
|> Orcasite.Radio.AwsClient.list_timestamps()
|> case do
{:ok, %{timestamps: [timestamp | _]}} ->
{:ok, %{timestamps: [_ | _] = timestamps}} ->
timestamp = List.last(timestamps)
{:ok, feed_stream} =
Orcasite.Radio.FeedStream
|> Ash.Changeset.for_create(:create, %{feed: feed, playlist_timestamp: timestamp})
Expand Down
68 changes: 36 additions & 32 deletions server/lib/orcasite/radio/audio_image.ex
Original file line number Diff line number Diff line change
Expand Up @@ -143,38 +143,42 @@ defmodule Orcasite.Radio.AudioImage do
# Only one feed segment at a time for now
[feed_segment] = image |> Ash.load!(:feed_segments) |> Map.get(:feed_segments)

%{
image_id: image.id,
audio_bucket: feed_segment.bucket,
audio_key: feed_segment.segment_path,
image_bucket: image.bucket,
image_key: image.object_path
}
|> Orcasite.Radio.AwsClient.generate_spectrogram()
|> IO.inspect(label: "gen spect result")
|> case do
{:ok, %{image_size: image_size, sample_rate: _sample_rate}} ->
image
|> Ash.Changeset.for_update(:update, %{
status: :complete,
image_size: image_size
})
|> Ash.Changeset.force_change_attribute(:last_error, nil)
|> Ash.update(authorize?: false)

{:ok, image}

{:error, error} ->
image
|> Ash.Changeset.for_update(:update, %{
status: :failed
})
|> Ash.Changeset.force_change_attribute(:last_error, inspect(error))
|> Ash.update(authorize?: false)

# error
{:ok, image}
end
timeout = :timer.minutes(2)

Task.Supervisor.async_nolink(
Orcasite.TaskSupervisor,
fn ->
%{
image_id: image.id,
audio_bucket: feed_segment.bucket,
audio_key: feed_segment.segment_path,
image_bucket: image.bucket,
image_key: image.object_path
}
|> Orcasite.Radio.AwsClient.generate_spectrogram()
|> case do
{:ok, %{file_size: image_size}} ->
image
|> Ash.Changeset.for_update(:update, %{
status: :complete,
image_size: image_size
})
|> Ash.Changeset.force_change_attribute(:last_error, nil)
|> Ash.update(authorize?: false)

{:error, error} ->
image
|> Ash.Changeset.for_update(:update, %{
status: :failed
})
|> Ash.Changeset.force_change_attribute(:last_error, inspect(error))
|> Ash.update(authorize?: false)
end
end,
timeout: timeout
)

{:ok, image}
end,
prepend?: true
)
Expand Down
2 changes: 1 addition & 1 deletion server/lib/orcasite/radio/aws_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ defmodule Orcasite.Radio.AwsClient do
|> ExAws.request(timeout: :timer.minutes(2))
|> case do
{:ok, %{"image_size" => image_size, "sample_rate" => sample_rate}} ->
{:ok, %{image_size: image_size, sample_rate: sample_rate}}
{:ok, %{file_size: image_size, sample_rate: sample_rate}}

{:ok, %{"errorMessage" => _} = err} ->
{:error, err}
Expand Down

0 comments on commit 2603b35

Please sign in to comment.