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
63
64
65
66
67
68
69
70
|
# File 'lib/parsers/flac_parser.rb', line 13
def call(io)
magic_bytes = safe_read(io, MAGIC_BYTES)
return unless magic_bytes == MAGIC_BYTE_STRING
safe_skip(io, BLOCK_HEADER_BYTES)
minimum_block_size = bytestring_to_int(safe_read(io, 2))
raise MalformedFile, 'FLAC file minimum block size must be larger than 16' if minimum_block_size < 16
maximum_block_size = bytestring_to_int(safe_read(io, 2))
raise MalformedFile, 'FLAC file maximum block size must be equal to or larger than minimum block size' if maximum_block_size < minimum_block_size
minimum_frame_size = bytestring_to_int(safe_read(io, 3))
maximum_frame_size = bytestring_to_int(safe_read(io, 3))
audio_info = safe_read(io, 8).unpack('B*')[0]
sample_rate = audio_info.slice!(0..19).to_i(2)
raise MalformedFile, 'FLAC file sample rate must be larger than 0' unless sample_rate > 0
num_channels = audio_info.slice!(0..2).to_i(2) + 1
bits_per_sample = audio_info.slice!(0..4).to_i(2) + 1
total_samples = audio_info.slice!(0..35).to_i(2)
duration = total_samples.to_f / sample_rate
FormatParser::Audio.new(
format: :flac,
num_audio_channels: num_channels,
audio_sample_rate_hz: sample_rate,
media_duration_seconds: duration,
media_duration_frames: total_samples,
content_type: FLAC_MIME_TYPE,
intrinsics: {
bits_per_sample: bits_per_sample,
minimum_frame_size: minimum_frame_size,
maximum_frame_size: maximum_frame_size,
minimum_block_size: minimum_block_size,
maximum_block_size: maximum_block_size
}
)
end
|