class WavTool
Public Class Methods
new(out: 'out.wav', channels: :mono, format_code: :pcm_16, sample_rate: 44100, src_path: '.', verbose: false, debug: false)
click to toggle source
# File lib/wavtool.rb, line 14 def initialize(out: 'out.wav', channels: :mono, format_code: :pcm_16, sample_rate: 44100, src_path: '.', verbose: false, debug: false) @format = Format.new(channels, format_code, sample_rate) @src_path, @out = src_path, out @verbose, @debug = verbose, debug if @verbose then puts 'WavTool'.highlight puts 'options: generate_silence, concat'.info puts end end
Public Instance Methods
concat(files, save_file=@out)
click to toggle source
concatenates WAV files into a single WAV file
# File lib/wavtool.rb, line 46 def concat(files, save_file=@out) puts ('Running concat ...').info; puts if @verbose puts ('files: ' + files.inspect).debug if @debug verbose, debug = @verbose, @debug Writer.new(save_file, @format) do |writer| files.each do |file_name| filepath = File.join(@src_path, file_name) puts ('concatenating: ' + filepath.inspect).debug if debug Reader.new(filepath).each_buffer(samples_per_buffer=4096) do |buffer| writer.write(buffer) end end end puts ('WAV file ' + @out + ' created').info if @verbose end
duration(file_name)
click to toggle source
# File lib/wavtool.rb, line 69 def duration(file_name) Reader.new(file_name).total_duration end
generate_silence(filename=@out, duration: 1)
click to toggle source
creates a WAV file containing only silence for a fixed duration
# File lib/wavtool.rb, line 30 def generate_silence(filename=@out, duration: 1) square_cycle = [0] * 100 * duration buffer = Buffer.new(square_cycle, Format.new(@format.channels, :float, @format.sample_rate)) Writer.new(filename, @format) do |writer| (@format.sample_rate.to_i / 100).times { writer.write(buffer) } end end
Also aliased as: silence