Class: EasyAudioUtils

Inherits:
Object
  • Object
show all
Extended by:
CommandHelper
Defined in:
lib/easyaudio_utils.rb

Instance Method Summary collapse

Methods included from CommandHelper

list, search

Constructor Details

#initialize(audio_in = nil, audio_out = 'audio.wav', out: audio_out, working_dir: '/tmp') ⇒ EasyAudioUtils

Returns a new instance of EasyAudioUtils.



61
62
63
64
65
66
# File 'lib/easyaudio_utils.rb', line 61

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

Instance Method Details

#capture(show: false) ⇒ Object Also known as: record

records audio in mono audio tested with .flac



71
72
73
74
75
76
77
# File 'lib/easyaudio_utils.rb', line 71

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

#concat_files(files = [], sample_rate: nil) ⇒ Object Also known as: concat



79
80
81
# File 'lib/easyaudio_utils.rb', line 79

def concat_files(files=[], sample_rate: nil)
  WavTool.new(out: @file_out, sample_rate: sample_rate).concat files
end

#convertObject

convert either wav to ogg or ogg to wav



87
88
89
90
91
92
93
94
95
# File 'lib/easyaudio_utils.rb', line 87

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) ⇒ Object

cut a section of audio and save it to file



99
100
101
102
103
104
105
# File 'lib/easyaudio_utils.rb', line 99

def cut(starttime, duration)

  command = "avconv -i %s -ss %s -t %s %s" % \
      [@file_in, starttime, duration, @file_out]
  run command, show

end

#durationObject



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/easyaudio_utils.rb', line 107

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) ⇒ Object

silence duration in seconds



122
123
124
# File 'lib/easyaudio_utils.rb', line 122

def generate_silence(duration)
  WavTool.new(out: @file_out).silence duration: duration
end

#play(show: false) ⇒ Object



128
129
130
131
# File 'lib/easyaudio_utils.rb', line 128

def play(show: false)
  command = "mplayer #{@file_out}"
  run command, show
end

#split(show: false) ⇒ Object

split by silence



135
136
137
138
139
# File 'lib/easyaudio_utils.rb', line 135

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

#volume(amount = 1.0, show: false) ⇒ Object

volume increase or decrease



143
144
145
146
# File 'lib/easyaudio_utils.rb', line 143

def volume(amount=1.0, show: false)
  command = "sox -v #{amount} #{@file_in} #{@file_out}"
  run command, show
end

#youtube_dl(show: false) ⇒ Object

Download and extract audio from a video on YouTube

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



152
153
154
155
156
157
158
# File 'lib/easyaudio_utils.rb', line 152

def youtube_dl(show: false)

  command = "youtube-dl -x #{url=@file_in}"
  command += ' -o ' + @file_out if @file_out
  run command, show

end