class EasyAudioUtils

Public Class Methods

new(audio_in=nil, audio_out='audio.wav', out: audio_out, working_dir: '/tmp') click to toggle source
# File lib/easyaudio_utils.rb, line 60
def initialize(audio_in=nil, audio_out='audio.wav', out: audio_out, 
               working_dir: '/tmp')

  @file_in, @file_out, @working_dir = audio_in, out, working_dir

end

Public Instance Methods

capture(show: false) click to toggle source

records audio in mono audio tested with .flac

# File lib/easyaudio_utils.rb, line 70
def capture(show: false)

  command = "rec -c 1 -r 8000 -t alsa default #{@file_out} " + 
      "silence 1 0.1 5% 5 1.0 5%"
  run command, show

end
Also aliased as: record
concat(files=[])
Alias for: concat_files
concat_files(files=[]) click to toggle source
# File lib/easyaudio_utils.rb, line 78
def concat_files(files=[])
  WavTool.new(out: @file_out, ).concat files
end
Also aliased as: concat
convert() click to toggle source

convert either wav to ogg or ogg to wav

# File lib/easyaudio_utils.rb, line 86
def convert()
      
  if File.extname(@file_in) == '.ogg' then
    ogg_to_wav() if File.extname(@file_out) == '.wav' 
  else
    wav_to_ogg() if File.extname(@file_out) == '.ogg' 
  end
  
end
cut(starttime, duration) click to toggle source

cut a section of audio and save it to file

# File lib/easyaudio_utils.rb, line 98
def cut(starttime, duration)    
  
  command = "avconv -i %s -ss %s -t %s %s" % \
      [@file_in, starttime, duration, @file_out]
  run command, show    
  
end
duration() click to toggle source
# File lib/easyaudio_utils.rb, line 106
def duration()
  
  case File.extname(@file_in)
  when '.ogg'
    OggInfo.open(@file_in).length
  when '.wav'
    WavTool.new().duration(@file_in)
  when '.mp3'
    Mp3Info.new(@file_in).length
  end    
  
end
generate_silence(duration) click to toggle source

silence duration in seconds

# File lib/easyaudio_utils.rb, line 121
def generate_silence(duration)
  WavTool.new(out: @file_out).silence duration: duration
end
play(show: false) click to toggle source
# File lib/easyaudio_utils.rb, line 127
def play(show: false)
  command = "mplayer #{@file_out}"
  run command, show
end
record(show: false)
Alias for: capture
split(show: false) click to toggle source

split by silence

# File lib/easyaudio_utils.rb, line 134
def split(show: false)    
  command = "sox -V3 #{@file_in} #{@file_out} silence -l  0  " +
      " 1 0.5 0.1% : newfile : restart"
  run command, show
end
youtube_dl(show: false) click to toggle source

Download and extract audio from a video on YouTube

By default, Youtube-dl will save the audio in Ogg (opus) format.

# File lib/easyaudio_utils.rb, line 144
def youtube_dl(show: false)    
  
  command = "youtube-dl -x #{url=@file_in}"
  command += ' -o ' + @file_out if @file_out
  run command, show    

end

Private Instance Methods

ogg_to_wav() click to toggle source
# File lib/easyaudio_utils.rb, line 156
def ogg_to_wav()
  `oggdec #{@file_in} #{@file_out}`
end
run(command, show=false) click to toggle source
# File lib/easyaudio_utils.rb, line 164
def run(command, show=false)

  if show then 
    command
  else
    puts "Using ->" + command
    system command
  end

end