Class: EasyAudioUtils
- Inherits:
-
Object
- Object
- EasyAudioUtils
- Extended by:
- CommandHelper
- Defined in:
- lib/easyaudio_utils.rb
Instance Method Summary collapse
-
#capture(show: false) ⇒ Object
(also: #record)
records audio in mono audio tested with .flac.
- #concat_files(files = []) ⇒ Object (also: #concat)
-
#convert ⇒ Object
convert either wav to ogg or ogg to wav.
-
#cut(starttime, duration) ⇒ Object
cut a section of audio and save it to file.
- #duration ⇒ Object
-
#generate_silence(duration) ⇒ Object
silence duration in seconds.
-
#initialize(audio_in = nil, audio_out = 'audio.wav', out: audio_out, working_dir: '/tmp') ⇒ EasyAudioUtils
constructor
A new instance of EasyAudioUtils.
- #play(show: false) ⇒ Object
-
#split(show: false) ⇒ Object
split by silence.
-
#youtube_dl(show: false) ⇒ Object
Download and extract audio from a video on YouTube.
Methods included from CommandHelper
Constructor Details
#initialize(audio_in = nil, audio_out = 'audio.wav', out: audio_out, working_dir: '/tmp') ⇒ EasyAudioUtils
Returns a new instance of EasyAudioUtils.
60 61 62 63 64 65 |
# 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 |
Instance Method Details
#capture(show: false) ⇒ Object Also known as: record
records audio in mono audio tested with .flac
70 71 72 73 74 75 76 |
# 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 |
#concat_files(files = []) ⇒ Object Also known as: concat
78 79 80 |
# File 'lib/easyaudio_utils.rb', line 78 def concat_files(files=[]) WavTool.new(out: @file_out, ).concat files end |
#convert ⇒ Object
convert either wav to ogg or ogg to wav
86 87 88 89 90 91 92 93 94 |
# 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) ⇒ Object
cut a section of audio and save it to file
98 99 100 101 102 103 104 |
# 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 ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 |
# 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) ⇒ Object
silence duration in seconds
121 122 123 |
# File 'lib/easyaudio_utils.rb', line 121 def generate_silence(duration) WavTool.new(out: @file_out).silence duration: duration end |
#play(show: false) ⇒ Object
127 128 129 130 |
# File 'lib/easyaudio_utils.rb', line 127 def play(show: false) command = "mplayer #{@file_out}" run command, show end |
#split(show: false) ⇒ Object
split by silence
134 135 136 137 138 |
# 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) ⇒ Object
Download and extract audio from a video on YouTube
By default, Youtube-dl will save the audio in Ogg (opus) format.
144 145 146 147 148 149 150 |
# 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 |