Skip to content

Commit

Permalink
fix peaks
Browse files Browse the repository at this point in the history
  • Loading branch information
michelson committed Oct 12, 2023
1 parent 86d98cd commit 327a676
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
14 changes: 14 additions & 0 deletions app/models/track.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,20 @@ def reprocess!
update_peaks
end

def process_with_mp3

temp_file = Tempfile.new(["audio", ".mp3"], binmode: true)

# Download the blob to the temp file in chunks
mp3_audio.download do |chunk|
temp_file.write(chunk)
end

temp_file.rewind

update_peaks
end

def update_mp3(temp_file = nil)
if temp_file.nil?
# Create a temp file
Expand Down
9 changes: 4 additions & 5 deletions app/services/peaks_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,11 @@ def calculate_pixels_per_second
end

def normalize(input)
min, max = input.minmax
new_min, new_max = [-1.0, 1.0]
# Find the maximum absolute value in the array
max_abs_value = input.map(&:abs).max.to_f

input.map do |x|
new_min + ((x - min) / (max - min)) * (new_max - new_min)
end.map { |x| x.round(3) }
# Normalize each value in the inputay
input.map { |val| val / max_abs_value }
end

def ffprobe_path
Expand Down
22 changes: 22 additions & 0 deletions spec/models/peaks_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "rails_helper"

def normalize(input)
# Find the maximum absolute value in the array
max_abs_value = input.map(&:abs).max.to_f

# Normalize each value in the inputay
input.map { |val| val / max_abs_value }
end

describe "something to be performed" do
context "under condition" do
it "behaves like" do

input = [-40,45,-45,56,-41,48,-34,45,-32,40,-24,37,-39,52]

peaks = normalize(input)
expect(peaks).to include(1.0)

end
end
end

0 comments on commit 327a676

Please sign in to comment.