Class: FormatParser::WAVParser
- Inherits:
-
Object
- Object
- FormatParser::WAVParser
show all
- Includes:
- IOUtils
- Defined in:
- lib/parsers/wav_parser.rb
Constant Summary
collapse
- WAV_MIME_TYPE =
'audio/x-wav'
Constants included
from IOUtils
IOUtils::INTEGER_DIRECTIVES
Instance Method Summary
collapse
Methods included from IOUtils
#read_bytes, #read_fixed_point, #read_int, #safe_read, #safe_skip, #skip_bytes
Instance Method Details
#call(io) ⇒ Object
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
|
# File 'lib/parsers/wav_parser.rb', line 10
def call(io)
chunk_id, _size, riff_type = safe_read(io, 12).unpack('a4la4')
return unless chunk_id == 'RIFF' && riff_type == 'WAVE'
fmt_processed = false
fact_processed = false
fmt_data = {}
total_sample_frames = 0
loop do
chunk_type, chunk_size = safe_read(io, 8).unpack('a4l')
case chunk_type
when 'fmt ' fmt_data = unpack_fmt_chunk(io, chunk_size)
return process_non_pcm(fmt_data, total_sample_frames) if fmt_data[:audio_format] != 1 and fact_processed
fmt_processed = true
when 'data'
return unless fmt_processed return process_pcm(fmt_data, chunk_size) if fmt_data[:audio_format] == 1
safe_skip(io, chunk_size)
when 'fact'
total_sample_frames = safe_read(io, 4).unpack('l').first
safe_skip(io, chunk_size - 4)
return process_non_pcm(fmt_data, total_sample_frames) if fmt_processed and fmt_data[:audio_format] != 1
fact_processed = true
else
safe_skip(io, chunk_size)
end
end
end
|
#file_info(fmt_data, sample_frames) ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/parsers/wav_parser.rb', line 89
def file_info(fmt_data, sample_frames)
return unless fmt_data[:sample_rate] > 0
duration_in_seconds = sample_frames / fmt_data[:sample_rate].to_f
FormatParser::Audio.new(
format: :wav,
num_audio_channels: fmt_data[:channels],
audio_sample_rate_hz: fmt_data[:sample_rate],
media_duration_frames: sample_frames,
media_duration_seconds: duration_in_seconds,
content_type: WAV_MIME_TYPE,
)
end
|
#likely_match?(filename) ⇒ Boolean
6
7
8
|
# File 'lib/parsers/wav_parser.rb', line 6
def likely_match?(filename)
filename =~ /\.wav$/i
end
|
#process_non_pcm(fmt_data, total_sample_frames) ⇒ Object
85
86
87
|
# File 'lib/parsers/wav_parser.rb', line 85
def process_non_pcm(fmt_data, total_sample_frames)
file_info(fmt_data, total_sample_frames)
end
|
#process_pcm(fmt_data, data_size) ⇒ Object
79
80
81
82
83
|
# File 'lib/parsers/wav_parser.rb', line 79
def process_pcm(fmt_data, data_size)
return unless fmt_data[:channels] > 0 and fmt_data[:bits_per_sample] > 0
sample_frames = data_size / (fmt_data[:channels] * fmt_data[:bits_per_sample] / 8)
file_info(fmt_data, sample_frames)
end
|
#unpack_fmt_chunk(io, chunk_size) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/parsers/wav_parser.rb', line 55
def unpack_fmt_chunk(io, chunk_size)
fmt_info = safe_read(io, 16).unpack('S_2I2S_2')
safe_skip(io, chunk_size - 16)
{
audio_format: fmt_info[0],
channels: fmt_info[1],
sample_rate: fmt_info[2],
byte_rate: fmt_info[3],
block_align: fmt_info[4],
bits_per_sample: fmt_info[5],
}
end
|