about summary refs log tree commit homepage
path: root/test
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2023-10-12 09:22:51 +0000
committerEric Wong <e@80x24.org>2023-10-12 09:24:04 +0000
commit63fb7405952428a3093ea32865ef0336e1a46bda (patch)
tree7d71e4cdd3b0fb120763cf27194a22dcafcf1685 /test
parent958102f24071f114e0e1671aa6cbe519c69c7d07 (diff)
downloaddtas-63fb7405952428a3093ea32865ef0336e1a46bda.tar.gz
When this project started avconv was favored in Debian,
but that hasn't been the case in many years.  Increase
the priority of ffmpeg to match the current situation in
Debian.
Diffstat (limited to 'test')
-rw-r--r--test/test_source_ff.rb102
1 files changed, 102 insertions, 0 deletions
diff --git a/test/test_source_ff.rb b/test/test_source_ff.rb
new file mode 100644
index 0000000..e53e72e
--- /dev/null
+++ b/test/test_source_ff.rb
@@ -0,0 +1,102 @@
+# Copyright (C) all contributors <dtas-all@nongnu.org>
+# License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
+# frozen_string_literal: true
+require './test/helper'
+require 'dtas/source/ff'
+require 'tempfile'
+
+class TestSourceFf < Testcase
+  def teardown
+    @tempfiles.each(&:close!)
+  end
+
+  def setup
+    @tempfiles = []
+  end
+
+  def x(cmd)
+    system(*cmd)
+    assert $?.success?, cmd.inspect
+  end
+
+  def new_file(suffix)
+    tmp = Tempfile.new(%W(tmp .#{suffix}))
+    @tempfiles << tmp
+    cmd = %W(sox -r 44100 -b 16 -c 2 -n #{tmp.path} trim 0 1)
+    return tmp if system(*cmd)
+    nil
+  end
+
+  def test_flac
+    return if `which metaflac`.strip.size == 0
+    tmp = new_file('flac') or return
+
+    x(%W(metaflac --set-tag=FOO=BAR #{tmp.path}))
+    x(%W(metaflac --add-replay-gain #{tmp.path}))
+    source = DTAS::Source::Ff.new.try(tmp.path)
+    assert_equal source.comments["FOO"], "BAR", source.inspect
+    rg = source.replaygain('track_gain')
+    assert_kind_of DTAS::ReplayGain, rg
+    assert_in_delta 0.0, rg.track_peak.to_f, 0.00000001
+    assert_in_delta 0.0, rg.album_peak.to_f, 0.00000001
+    assert_operator rg.album_gain.to_f, :>, 1
+    assert_operator rg.track_gain.to_f, :>, 1
+  end
+
+  def test_mp3gain
+    return if `which mp3gain`.strip.size == 0
+    a = new_file('mp3') or return
+    b = new_file('mp3') or return
+
+    # redirect stdout to /dev/null temporarily, mp3gain is noisy
+    File.open("/dev/null", "w") do |null|
+      old_out = $stdout.dup
+      $stdout.reopen(null)
+      begin
+        x(%W(mp3gain -q #{a.path} #{b.path}))
+      ensure
+        $stdout.reopen(old_out)
+        old_out.close
+      end
+    end
+
+    source = DTAS::Source::Ff.new.try(a.path)
+    rg = source.replaygain('track_gain')
+    assert_kind_of DTAS::ReplayGain, rg
+    assert_in_delta 0.0, rg.track_peak.to_f, 0.00000001
+    assert_in_delta 0.0, rg.album_peak.to_f, 0.00000001
+    assert_operator rg.album_gain.to_f, :>, 1
+    assert_operator rg.track_gain.to_f, :>, 1
+  end
+
+  def test_offset
+    tmp = new_file('flac') or return
+    source = DTAS::Source::Ff.new.try(*%W(#{tmp.path} 5s))
+    assert_equal 5, source.offset_samples
+
+    source = DTAS::Source::Ff.new.try(*%W(#{tmp.path} 1:00:00.5))
+    expect = 1 * 60 * 60 * 44100 + (44100/2)
+    assert_equal expect, source.offset_samples
+
+    source = DTAS::Source::Ff.new.try(*%W(#{tmp.path} 1:10.5))
+    expect = 1 * 60 * 44100 + (10 * 44100) + (44100/2)
+    assert_equal expect, source.offset_samples
+
+    source = DTAS::Source::Ff.new.try(*%W(#{tmp.path} 10.03))
+    expect = (10 * 44100) + (44100 * 3/100.0)
+    assert_equal expect, source.offset_samples
+  end
+
+  def test_offset_us
+    tmp = new_file('flac') or return
+    source = DTAS::Source::Ff.new.try(*%W(#{tmp.path} 441s))
+    assert_equal 10000.0, source.offset_us
+
+    source = DTAS::Source::Ff.new.try(*%W(#{tmp.path} 22050s))
+    assert_equal 500000.0, source.offset_us
+
+    source = DTAS::Source::Ff.new.try(tmp.path, '1')
+    assert_equal 1000000.0, source.offset_us
+  end
+end if `which ffprobe 2>/dev/null` =~ /ffprobe/ &&
+       `which ffmpeg 2>/dev/null` =~ /ffmpeg/