-
Notifications
You must be signed in to change notification settings - Fork 2
/
encoding_spec.rb
223 lines (169 loc) · 6.03 KB
/
encoding_spec.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
require 'spec_helper'
require 'wavefile'
# require 'digest'
describe "Encoding", :slow => true do
# regular 16-bit, 44.1kHz sine wave:
let(:wav_file) { WaveFileGenerator.new(:length => 2).generate }
let(:wav_path) { wav_file.path }
let(:wav_reader) { WaveFile::Reader.new(wav_path) }
# 24-bit, 192kHz sine wave:
let(:wav_file_24) { WaveFileGenerator.new(length: 2, bits: 24, sample_rate: 192000).generate }
let(:wav_path_24) { wav_file_24.path }
let(:wav_reader_24) { WaveFile::Reader.new(wav_path_24) }
let(:mp3_path_raw) { Tempfile.new("output-raw.mp3") }
let(:mp3_path_api) { Tempfile.new("output-api.mp3") }
it "encodes a file by api" do
encoder = LAME::Encoder.new
encoder.configure do |config|
config.id3.write_automatic = false
config.id3.v2 = true
config.id3.title = "Dies Irae"
config.id3.genre = "Classical"
#config.bitrate = 192
# config.preset = :V0 # doesn't work?
config.vbr.mode = :vbr_default
config.vbr.q = 0
end
File.open(mp3_path_api, "wb") do |file|
id3v2_size = 0
encoder.id3v2 do |tag|
file.write tag
id3v2_size = tag.size
end
wav_reader.each_buffer(encoder.framesize) do |read_buffer|
left = read_buffer.samples.map { |s| s[0] }
right = read_buffer.samples.map { |s| s[1] }
encoder.encode_short(left, right) do |mp3|
file.write mp3
end
end
encoder.flush do |flush_frame|
file.write(flush_frame)
end
encoder.id3v1 do |tag|
file.write tag
end
encoder.vbr_frame do |vbr_frame|
file.seek(id3v2_size)
file.write(vbr_frame)
end
end
# TODO: Need a better way to test output..
# Digest::MD5.hexdigest(File.read(mp3_path_api)).should eql "d1cd92c106e7aac4f5291fd141a19e10"
end
it "encodes a file using interleaved api" do
encoder = LAME::Encoder.new
encoder.configure do |config|
config.bitrate = 192
end
File.open(mp3_path_api, "wb") do |file|
wav_reader.each_buffer(encoder.framesize) do |read_buffer|
encoder.encode_interleaved_short(read_buffer.samples.flatten) do |mp3|
file.write mp3
end
end
encoder.flush do |flush_frame|
file.write(flush_frame)
end
end
# TODO: Need a better way to test output..
# Digest::MD5.hexdigest(File.read(mp3_path_api)).should eql "d1cd92c106e7aac4f5291fd141a19e10"
end
# This test serves as an example how to use the LAME C-API
it "encodes a wav file" do
# setup
flags_pointer = LAME.lame_init
LAME.lame_init_params(flags_pointer)
# LAME.id3tag_init(flags_pointer) # needed?
# LAME.id3tag_set_title(flags_pointer, "foo")
# number of samples to read
framesize = LAME.lame_get_framesize(flags_pointer)
# input buffers
left_buffer = ::FFI::MemoryPointer.new(:short, framesize)
right_buffer = ::FFI::MemoryPointer.new(:short, framesize)
# output buffer
buffer_size = (128*1024)+16384
buffer = ::FFI::MemoryPointer.new(:uchar, buffer_size)
File.open(mp3_path_raw, "wb") do |file|
wav_reader.each_buffer(framesize) do |read_buffer|
# read samples (ranges from -32k to +32k)
read_buffer.samples.each.with_index do |(left, right), index|
byte_offset = index * 2
left_buffer.put_short(byte_offset, left)
right_buffer.put_short(byte_offset, right)
end
input_buffer_size = read_buffer.samples.size
# encode to mp3 frame
size = LAME.lame_encode_buffer(
flags_pointer,
left_buffer, right_buffer, input_buffer_size,
buffer, buffer_size
)
# write to file
file.write buffer.get_bytes(0, size)
end
# flush final frame
size = LAME.lame_encode_flush(flags_pointer, buffer, buffer_size)
file.write buffer.get_bytes(0, size)
# write "lametag" frame with extra info
size = LAME.lame_get_lametag_frame(flags_pointer, buffer, buffer_size)
file.seek(0)
file.write buffer.get_bytes(0, size)
end
# close
LAME.lame_close(flags_pointer)
# TODO: Need a better way to test output..
# Digest::MD5.hexdigest(File.read(mp3_path_raw)).should eql "84a1ce7994bb4a54fc13fb5381ebac40"
end
it "encodes 24-bit source files using the long encoder" do
encoder = LAME::Encoder.new
encoder.configure do |config|
config.input_samplerate = 192000
config.id3.write_automatic = false
config.vbr.mode = :vbr_default
config.vbr.q = 0
end
File.open(mp3_path_api, "wb") do |file|
wav_reader_24.each_buffer(encoder.framesize) do |read_buffer|
# important: shift the 24-bit samples to the system's long-size (32/64-bit)
left = read_buffer.samples.map { |s| s[0] << (::LAME::FFI::LONG_SIZE-24) }
right = read_buffer.samples.map { |s| s[1] << (::LAME::FFI::LONG_SIZE-24) }
encoder.encode_long(left, right) do |mp3|
file.write mp3
end
end
encoder.flush do |flush_frame|
file.write(flush_frame)
end
encoder.vbr_frame do |vbr_frame|
file.write(vbr_frame)
end
end
end
it "encodes 24-bit source files using the float encoder" do
encoder = LAME::Encoder.new
encoder.configure do |config|
config.input_samplerate = 192000
config.id3.write_automatic = false
config.vbr.mode = :vbr_default
config.vbr.q = 0
end
max24 = 2**24/2.0
File.open(mp3_path_api, "wb") do |file|
wav_reader_24.each_buffer(encoder.framesize) do |read_buffer|
# important: scale the 24-bit input values to floats between -1.0 and +1.0
left = read_buffer.samples.map { |s| s[0] / max24 }
right = read_buffer.samples.map { |s| s[1] / max24 }
encoder.encode_float(left, right) do |mp3|
file.write mp3
end
end
encoder.flush do |flush_frame|
file.write(flush_frame)
end
encoder.vbr_frame do |vbr_frame|
file.write(vbr_frame)
end
end
end
end