Class: Scissor::Writer

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/scissor/writer.rb

Defined Under Namespace

Classes: CommandFailed, CommandNotFound, EmptyFragment, Error, FileExists

Instance Method Summary collapse

Methods included from Loggable

#logger

Constructor Details

#initializeWriter

Returns a new instance of Writer.



17
18
19
20
21
22
# File 'lib/scissor/writer.rb', line 17

def initialize
  @tracks = []

  which('ecasound')
  which('rubberband')
end

Instance Method Details

#add_track(fragments) ⇒ Object



24
25
26
# File 'lib/scissor/writer.rb', line 24

def add_track(fragments)
  @tracks << fragments
end

#join_fragments(fragments, outfile, tmpdir) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/scissor/writer.rb', line 28

def join_fragments(fragments, outfile, tmpdir)
  position = 0.0
  cmd = %w/ecasound/
  is_mono = {}

  fragments.each_with_index do |fragment, index|
    fragment_filename = fragment.filename

    is_mono[fragment_filename] ||= mono?(fragment_filename)

    if !index.zero? && (index % 28).zero?
      run_command(cmd.join(' '))
      cmd = %w/ecasound/
    end

    fragment_outfile = tmpdir + (Digest::MD5.hexdigest(fragment_filename.to_s) + '.wav')

    unless fragment_outfile.exist?
      movie = FFMPEG::Movie.new(fragment_filename.to_s)
      movie.transcode(fragment_outfile.to_s, :audio_sample_rate => 44100)
    end

    cmd << "-a:#{index} -o:#{outfile} -y:#{position}#{is_mono[fragment_filename] ? ' -chcopy:1,2' : ''}"

    if fragment.pan != 50
      cmd << "-epp:%i" % fragment.pan
    end

    if fragment.stretched? && fragment.pitch.to_f != 100.0
      rubberband_out = tmpdir + (Digest::MD5.hexdigest(fragment_filename.to_s) + "rubberband_#{index}.wav")
      rubberband_temp = tmpdir + "_rubberband.wav"

      run_command("ecasound " +
        "-i:" +
        (fragment.reversed? ? 'reverse,' : '') +
        "select,#{fragment.start},#{fragment.original_duration},\"#{fragment_outfile}\" -o:#{rubberband_temp} "
      )
      run_command("rubberband -T #{fragment.pitch.to_f/100} \"#{rubberband_temp}\" \"#{rubberband_out}\"")

      cmd << "-i:\"#{rubberband_out}\""
    else
      cmd <<
        "-i:" +
        (fragment.reversed? ? 'reverse,' : '') +
        "select,#{fragment.start},#{fragment.original_duration},\"#{fragment_outfile}\" " +
        (fragment.pitch.to_f == 100.0 ? "" : "-ei:#{fragment.pitch} ")
    end

    position += fragment.duration
  end

  run_command(cmd.join(' '))
end

#mix_files(filenames, outfile) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/scissor/writer.rb', line 82

def mix_files(filenames, outfile)
  cmd = %w/ecasound/

  filenames.each_with_index do |tf, index|
    cmd << "-a:#{index} -i:#{tf}"
  end

  cmd << "-a:all -o:#{outfile}"
  run_command(cmd.join(' '))
end

#run_command(cmd) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/scissor/writer.rb', line 137

def run_command(cmd)
  logger.debug("run_command: #{cmd}")

  result, error = '', ''

  begin
    status = Open4.spawn cmd, 'stdout' => result, 'stderr' => error
  rescue Open4::SpawnError => e
    raise CommandFailed.new(e.cmd)
  ensure
    logger.debug(result)
    logger.debug(error)
  end

  if status && status.exitstatus != 0
    raise CommandFailed.new(cmd)
  end

  return result
end

#to_file(filename, options) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/scissor/writer.rb', line 93

def to_file(filename, options)
  filename = Pathname.new(filename)
  full_filename = filename.expand_path

  if @tracks.flatten.empty?
    raise EmptyFragment
  end

  options = {
    :overwrite => false,
    :bitrate => '128k'
  }.merge(options)

  if filename.exist?
    if options[:overwrite]
      filename.unlink
    else
      raise FileExists
    end
  end

  TempDir.create do |dir|
    tmpdir = Pathname.new(dir)
    tmpfiles = []

    @tracks.each_with_index do |fragments, track_index|
      tmpfile = tmpdir + 'track_%s.wav' % track_index.to_s
      tmpfiles << tmpfile
      join_fragments(fragments, tmpfile, tmpdir)
    end

    mix_files(tmpfiles, final_tmpfile = tmpdir + 'tmp.wav')

    movie = FFMPEG::Movie.new(final_tmpfile.to_s)
    movie.transcode(full_filename.to_s, :audio_sample_rate => 44100, :audio_bitrate => options[:bitrate])
  end
end

#which(command) ⇒ Object



131
132
133
134
135
# File 'lib/scissor/writer.rb', line 131

def which(command)
  run_command("which #{command}")
rescue
  raise CommandNotFound.new(command + ': command not found')
end