6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/whisper/audio_processor.rb', line 6
def self.convert_to_float_array(file_path)
wav_file = "#{Dir.tmpdir}/temp_#{Time.now.to_i}_#{rand(1000)}.wav"
cmd = [
'ffmpeg', '-y', '-i', file_path,
'-ar', '16000', '-ac', '1', '-f', 'wav', wav_file
]
stdout_str, stderr_str, status = Open3.capture3(*cmd)
raise "ffmpeg error: #{stderr_str}" unless status.success?
data = File.binread wav_file
pcm_data = data[44..-1].unpack 's<*'
pcm_data.map { |sample| sample / 32768.0 }
ensure
File.delete wav_file if File.exist? wav_file
end
|