-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample_03.rb
62 lines (47 loc) · 2.11 KB
/
sample_03.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 音源に対してスペクトル解析した動画を生成します。
# 横軸:周波数
# 縦軸:大きさ
require 'open3'
require 'numo/narray'
require 'numo/fftw'
require 'ruby-sox'
require 'parallel'
require_relative 'lib/plotter'
FPS = 24
BUFFER_SIZE = 44100 / FPS
def read_channel_data(filename, channel_number)
data = File.read(filename).split("\n")[2..-1].map { |row| row.split.map(&:to_f) }
data.each_slice(BUFFER_SIZE).map { |data_unit| [data_unit.map { |r| r[channel_number] }, data_unit.first[0], data_unit.last[0]] }
end
def calculate_fft(signal, duration, max_points = 3000)
na = Numo::NArray[signal]
fc = Numo::FFTW.dft(na, 1)
fc.real.to_a.flatten.first(na.length / 2).first(max_points).each_with_index.map do |val, index|
[index / duration, val.abs]
end
end
puts 'Converting WAV file to DAT file...'
dat_file_name = 'tmp/files/sample_03.dat'
Sox::Cmd.new.add_input(ARGV[0]).set_output(dat_file_name).run
data = read_channel_data(dat_file_name, 1)
# data.each_with_index do |data_unit, index|
Parallel.each_with_index(data, in_processes: 4) do |data_unit, index|
puts "#{index} / #{data.size}"
signal = data_unit[0]
duration = data_unit[2] - data_unit[1]
spectrum = calculate_fft(signal, duration)
# max_frequency = spectrum.sort_by(&:last).last.first.round(2)
spectrum_plot_params = {
image_name: "tmp/gnuplot/spectrum_#{'%04d' % index}.png",
title: "spectrum (#{data_unit[1].ceil(2)} - #{data_unit[2].ceil(2)}s)"
}
plotter = GNUPlotter.new(spectrum, spectrum_plot_params, 4096, 192)
plotter.plot
end
puts 'Converting PNG files to GIF Animation...'
system 'convert -delay 10 -loop 0 tmp/gnuplot/spectrum_*.png tmp/gnuplot/spectrum.gif'
system "ffmpeg -r #{FPS} -i tmp/gnuplot/spectrum.gif -movflags faststart -pix_fmt yuv420p -vf 'scale=trunc(iw/2)*2:trunc(ih/2)*2' tmp/gnuplot/spectrum.tmp.mp4"
system "ffmpeg -i tmp/gnuplot/spectrum.tmp.mp4 -i #{ARGV[0]} -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 tmp/gnuplot/spectrum#{Time.now.strftime("%Y%m%d%H%M%S")}.mp4"
system 'rm tmp/gnuplot/spectrum_*.png'
system 'rm tmp/gnuplot/spectrum.gif'
system 'rm tmp/gnuplot/spectrum.tmp.mp4'