class CarrierWave::AudioWaveform::WaveformData
Constants
- DefaultOptions
Public Class Methods
generate(source, options={})
click to toggle source
Generate a Waveform image at the given filename with the given options.
Available options (all optional) are:
:convert_to_extension_before_processing => Symbolized extension (:wav, :mp3, etc.) Useful if .wav or .mp3 isn't being passed in--you can convert to that format first. :set_extension_before_processing => Symbolized extension (:wav, :mp3, etc.) This is useful because CarrierWave will send files in with the wrong extension sometimes. For instance, if this is nested under a version, that version may be an .mp3, but its parent :pixels_per_second => The number of pixels per second to evaluate. :bits => 8 or 16 bit precision :logger => IOStream to log progress to.
Example:
CarrierWave::AudioWaveform::Waveformer.generate("Kickstart My Heart.wav") CarrierWave::AudioWaveform::Waveformer.generate("Kickstart My Heart.wav", :method => :rms) CarrierWave::AudioWaveform::Waveformer.generate("Kickstart My Heart.wav", :color => "#ff00ff", :logger => $stdout)
# File lib/carrierwave/audio_waveform/waveform_data.rb, line 40 def generate(source, options={}) options = DefaultOptions.merge(options) options[:filename] ||= self.generate_json_filename(source) old_source = source if options[:convert_to_extension_before_processing] source = generate_valid_source(source, options[:convert_to_extension_before_processing]) elsif options[:set_extension_before_processing] source = generate_proper_source(source, options[:set_extension_before_processing]) end raise ArgumentError.new("No source audio filename given, must be an existing sound file.") unless source raise ArgumentError.new("No destination filename given for waveform") unless options[:filename] raise RuntimeError.new("Source audio file '#{source}' not found.") unless File.exist?(source) @log = Log.new(options[:logger]) @log.start! @log.timed("\nGenerating...") do stdout_str, stderr_str, status = self.generate_waveform_data(source, options) if stderr_str.present? && !stderr_str.include?("Recoverable") raise RuntimeError.new(stderr_str) end end if source != old_source && options[:convert_to_extension_before_processing] @log.out("Removing temporary file at #{source}") FileUtils.rm(source) elsif source != old_source && options[:set_extension_before_processing] @log.out("Renaming file at #{source}") old_ext = File.extname(source).gsub(/\./, '').to_sym generate_proper_source(source, old_ext) end @log.done!("Generated waveform data '#{options[:filename]}'") options[:filename] end
generate_json_filename(source)
click to toggle source
# File lib/carrierwave/audio_waveform/waveform_data.rb, line 78 def generate_json_filename(source) ext = File.extname(source) source_file_path_without_extension = File.join File.dirname(source), File.basename(source, ext) "#{source_file_path_without_extension}.json" end
generate_waveform_data(source, options = DefaultOptions)
click to toggle source
# File lib/carrierwave/audio_waveform/waveform_data.rb, line 84 def generate_waveform_data(source, options = DefaultOptions) options[:filename] ||= self.generate_json_filename(source) Open3.capture3( "audiowaveform -i #{source} --pixels-per-second #{options[:pixels_per_second]} -b #{options[:bits]} -o #{options[:filename]}" ) end
Private Class Methods
generate_proper_source(source, proper_ext)
click to toggle source
Returns the proper file type if the one passed in was wrong, or the original if it wasn't.
# File lib/carrierwave/audio_waveform/waveform_data.rb, line 95 def generate_proper_source(source, proper_ext) ext = File.extname(source) ext_gsubbed = ext.gsub(/\./, '') if ext_gsubbed != proper_ext.to_s filename_with_proper_extension = "#{source.chomp(File.extname(source))}.#{proper_ext}" File.rename source, filename_with_proper_extension filename_with_proper_extension else source end rescue Sox::Error => e raise e unless e.message.include?("FAIL formats:") raise RuntimeError.new("Source file #{source} could not be converted to .wav by Sox (Sox: #{e.message})") end
generate_valid_source(source, proper_ext)
click to toggle source
Returns a converted file.
# File lib/carrierwave/audio_waveform/waveform_data.rb, line 112 def generate_valid_source(source, proper_ext) ext = File.extname(source) ext_gsubbed = ext.gsub(/\./, '') if ext_gsubbed != proper_ext.to_s input_options = { type: ext_gsubbed } output_options = { type: proper_ext.to_s } source_filename_without_extension = File.basename(source, ext) output_file_path = File.join File.dirname(source), "tmp_#{source_filename_without_extension}_#{Time.now.to_i}.#{proper_ext}" converter = Sox::Cmd.new converter.add_input source, input_options converter.set_output output_file_path, output_options converter.run output_file_path else source end rescue Sox::Error => e raise e unless e.message.include?("FAIL formats:") raise RuntimeError.new("Source file #{source} could not be converted to .wav by Sox (Sox: #{e.message})") end