Skip to content

Commit

Permalink
Spectrogram generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark committed Oct 23, 2012
1 parent 0b2c340 commit abea0e5
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
22 changes: 20 additions & 2 deletions app/controllers/media_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
class MediaController < ApplicationController
include Spectrogram
def index
@input_path = './test/fixtures/'
@audio = 'TorresianCrow.wav'
@input_audio = @input_path + @audio
@result = Audio::info(@input_audio)
end
def audio
@input_path = './test/fixtures/'
@output_path = './public/tests/'

@audio = 'TorresianCrow.wav'
@modified_audio = 'TorresianCrow.wav'

@input_audio = @input_path + @audio
@output_audio = @output_path + @modified_audio

@result = Audio::segment(@input_audio, @output_audio)
end
def spectrogram
@input_path = './test/fixtures/'
@output_path = './public/tests/'

Expand All @@ -9,7 +28,6 @@ def index
@input_audio = @input_path + @audio
@output_image = @output_path + @image

stdout_str, stderr_str, status = Open3.capture3("./vendor/bin/sox/windows/sox.exe -V \"#{@input_audio}\" -n rate 22050 spectrogram -m -r -l -a -q 249 -w hann -y 257 -X 43.06640625 -z 100 -o \"#{@output_image}\"")
@result = [stdout_str, stderr_str, status]
@result = Spectrogram::generate(@input_audio, @output_image)
end
end
33 changes: 33 additions & 0 deletions lib/modules/audio.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Audio

@sox_path = "./vendor/bin/sox/windows/sox.exe"

@sox_arguments_info = "--info"
@@my_logger ||= Logger.new("#{Rails.root}/log/my.log")

def self.info(source)
# sox command to create a spectrogram from an audio file
command = "#{@sox_path} #{@sox_arguments_info} \"#{source}\""

# run the command and wait for the result
stdout_str, stderr_str, status = Open3.capture3(command)

# get the audio file information from the stdout_str
something = stdout_str.each_line { |substr| p substr.index(':') }

# package up all the available information and return it
result = [ stdout_str, stderr_str, status, File.exist?(source), something ]

end
def self.segment(source, destination)
# sox command to create a spectrogram from an audio file
command = "#{@sox_path} #{@sox_arguments_verbose} \"#{source}\" #{@sox_arguments_output_audio} #{@sox_arguments_sample_rate} #{@sox_arguments_spectrogram} #{@sox_arguments_output} \"#{destination}\""

# run the command and wait for the result
stdout_str, stderr_str, status = Open3.capture3(command)

# package up all the available information and return it
result = [ stdout_str, stderr_str, status, File.exist?(source), File.exist?(destination) ]

end
end
17 changes: 17 additions & 0 deletions lib/modules/spectrogram.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Spectrogram
@sox_path = "./vendor/bin/sox/windows/sox.exe"
@sox_arguments_verbose = "-V"
@sox_arguments_output_audio = "-n"
@sox_arguments_sample_rate = "rate 22050"
@sox_arguments_spectrogram = "spectrogram -m -r -l -a -q 249 -w hann -y 257 -X 43.06640625 -z 100"
@sox_arguments_output = "-o"
@@my_logger ||= Logger.new("#{Rails.root}/log/my.log")
def self.generate(source, destination)
# sox command to create a spectrogram from an audio file
command = "#{@sox_path} #{@sox_arguments_verbose} \"#{source}\" #{@sox_arguments_output_audio} #{@sox_arguments_sample_rate} #{@sox_arguments_spectrogram} #{@sox_arguments_output} \"#{destination}\""
# run the command and wait for the result
stdout_str, stderr_str, status = Open3.capture3(command)
# package up all the available information and return it
result = [ stdout_str, stderr_str, status, File.exist?(source), File.exist?(destination) ]
end
end
Binary file added vendor/bin/ffmpeg/windows/ffmpeg.exe
Binary file not shown.
Binary file added vendor/bin/ffmpeg/windows/ffprobe.exe
Binary file not shown.

0 comments on commit abea0e5

Please sign in to comment.