Sha256: 036c1b02a107c92e028c3be04f55f4571d69de6a9d8476212f1e05857771dee6

Contents?: true

Size: 1.74 KB

Versions: 25

Compression:

Stored size: 1.74 KB

Contents

require 'coveralls'
Coveralls.wear!

require 'bundler/setup'
Bundler.setup

require 'rspec'
require 'libis-format'
require 'libis-tools'

require 'chromaprint'

RSpec::Matchers.define(:be_same_file_as) do |exected_file_path|
  match do |actual_file_path|
    expect(md5_hash(actual_file_path)).to eq md5_hash(exected_file_path)
  end

  def md5_hash(file_path)
    Digest::MD5.hexdigest(File.read(file_path))
  end
end

RSpec::Matchers.define :sound_like do |exp_file, threshold, rate, channels|
  match do |tgt_file|
    rate ||= 96000
    channels ||= 1
    threshold ||= 0.9

    if File.exists?(exp_file) && File.exists?(tgt_file)
      # Convert input files into raw 16-bit signed audio (WAV) to process with Chramaprint
      exp_raw = File.join('','tmp', File.basename(exp_file) + '.wav')
      tgt_raw = File.join('','tmp', File.basename(tgt_file) + '.wav')
      FileUtils.rm(exp_raw, force: true)
      FileUtils.rm(tgt_raw, force: true)
      cvt_cmd = "sox %s -e signed -b 16 -t wav %s rate #{rate} channels #{channels}"
      %x"#{cvt_cmd % [exp_file,exp_raw]}"
      %x"#{cvt_cmd % [tgt_file,tgt_raw]}"
      exp_audio = File.binread(exp_raw)
      tgt_audio = File.binread(tgt_raw)

      # Get audio fingerprints
      chromaprint = Chromaprint::Context.new(rate, channels, Chromaprint::ALGORITHM_TEST3)
      exp_fp = chromaprint.get_fingerprint(exp_audio)
      tgt_fp = chromaprint.get_fingerprint(tgt_audio)

      # Cleanup files
      FileUtils.rm(exp_raw, force: true)
      FileUtils.rm(tgt_raw, force: true)

      # Compare fingerprints and compare result against threshold
      cmp = exp_fp.compare(tgt_fp)
      # puts "Threshold[#{File.basename(exp_file)},#{File.basename(tgt_file)}: #{cmp}"
      cmp > threshold
    else
      false
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
libis-format-1.0.4 spec/spec_helper.rb
libis-format-1.0.2 spec/spec_helper.rb
libis-format-1.0.1 spec/spec_helper.rb
libis-format-1.0.0 spec/spec_helper.rb
libis-format-0.9.74 spec/spec_helper.rb
libis-format-0.9.72 spec/spec_helper.rb
libis-format-0.9.70 spec/spec_helper.rb
libis-format-0.9.69 spec/spec_helper.rb
libis-format-0.9.68 spec/spec_helper.rb
libis-format-0.9.67 spec/spec_helper.rb
libis-format-0.9.66 spec/spec_helper.rb
libis-format-0.9.65 spec/spec_helper.rb
libis-format-0.9.64 spec/spec_helper.rb
libis-format-0.9.63 spec/spec_helper.rb
libis-format-0.9.62 spec/spec_helper.rb
libis-format-0.9.50 spec/spec_helper.rb
libis-format-0.9.49 spec/spec_helper.rb
libis-format-0.9.48 spec/spec_helper.rb
libis-format-0.9.47 spec/spec_helper.rb
libis-format-0.9.46 spec/spec_helper.rb