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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
# File 'lib/cohere/transcribe/audio/decoder.rb', line 150
def decode(path, backend: "auto", sample_rate: SAMPLE_RATE, max_decoded_bytes: 4 * (1024**3))
requested = backend
unless requested.is_a?(String) && BACKENDS.include?(requested)
raise ArgumentError, "Unsupported audio backend: #{backend.inspect}"
end
raise ArgumentError, "sample_rate must be a positive integer" unless sample_rate.is_a?(Integer) && sample_rate.positive?
unless max_decoded_bytes.nil? || (max_decoded_bytes.is_a?(Integer) && max_decoded_bytes.positive?)
raise ArgumentError, "max_decoded_bytes must be a positive integer or nil"
end
source = Pathname(path).expand_path
raise TranscriptionInputError, "Input does not exist: #{source}" unless source.exist?
raise TranscriptionInputError, "Input is not a regular file: #{source}" unless source.file?
native_ffmpeg_available = %w[auto librosa].include?(requested) && FFmpegNative.available?
use_ffmpeg = requested == "ffmpeg" || requested == "torchcodec" ||
(%w[auto librosa].include?(requested) && native_ffmpeg_available)
if use_ffmpeg
samples = FFmpegNative.decode(
source,
sample_rate: sample_rate,
max_decoded_bytes: max_decoded_bytes
)
validate_finite!(samples)
return Decoded.new(
samples: samples.freeze,
sample_rate: sample_rate,
backend: "ffmpeg",
fallback_reason: if %w[torchcodec librosa].include?(requested)
"Ruby #{requested} compatibility mode uses FFmpeg through the native C ABI"
end
)
end
unless SoundFileABI::AVAILABLE
sound_file_error = if SoundFileABI.const_defined?(:LOAD_ERROR, false)
SoundFileABI::LOAD_ERROR.message
else
"not found"
end
if requested == "auto"
raise TranscriptionRuntimeError,
"Automatic audio decoding requires the native FFmpeg adapter or libsndfile " \
"(FFmpeg: #{FFmpegNative.diagnostic}; libsndfile: #{sound_file_error})"
end
raise TranscriptionRuntimeError, "libsndfile is required for native audio decoding: #{sound_file_error}"
end
info = SoundFileABI::SFInfo.malloc
handle = SoundFileABI.sf_open(source.to_s, SFM_READ, info.to_ptr)
raise TranscriptionRuntimeError, "Cannot decode #{source}: #{SoundFileABI.sf_strerror(handle)}" if handle.null?
begin
frames = Integer(info.frames)
channels = Integer(info.channels)
source_rate = Integer(info.samplerate)
unless frames >= 0 && channels.positive? && source_rate.positive?
raise TranscriptionRuntimeError, "Decoder returned invalid audio metadata for #{source}"
end
input_bytes = frames * channels * Fiddle::SIZEOF_FLOAT
output_frames = (frames * sample_rate.fdiv(source_rate)).ceil + 64
projected_bytes = [input_bytes, output_frames * Fiddle::SIZEOF_FLOAT].max
if max_decoded_bytes && projected_bytes > max_decoded_bytes
raise TranscriptionRuntimeError,
"Decoded audio exceeds the configured memory limit for #{source} " \
"(#{projected_bytes} > #{max_decoded_bytes} bytes)"
end
raw = Fiddle::Pointer.malloc([input_bytes, 1].max, Fiddle::RUBY_FREE)
read_frames = SoundFileABI.sf_readf_float(handle, raw, frames)
raise TranscriptionRuntimeError, "Cannot decode #{source}: #{SoundFileABI.sf_strerror(handle)}" if read_frames.negative?
raise TranscriptionRuntimeError, "Decoder returned more frames than allocated for #{source}" if read_frames > frames
frames = read_frames
begin
require "numo/narray"
rescue LoadError => e
raise TranscriptionRuntimeError, "numo-narray is required for decoded audio: #{e.message}"
end
interleaved = if frames.zero?
nil
else
Numo::SFloat.from_binary(raw[0, frames * channels * Fiddle::SIZEOF_FLOAT])
end
mono = if frames.zero?
Numo::SFloat.zeros(0)
elsif channels == 1
interleaved
else
interleaved.reshape(frames, channels).mean(1).cast_to(Numo::SFloat)
end
samples = source_rate == sample_rate ? mono : resample(mono, source_rate, sample_rate, max_decoded_bytes)
validate_finite!(samples)
Decoded.new(
samples: samples.freeze,
sample_rate: sample_rate,
backend: "libsndfile",
fallback_reason: if %w[auto libsndfile].include?(requested)
nil
else
"Ruby #{requested} compatibility mode uses the native libsndfile ABI"
end
)
ensure
SoundFileABI.sf_close(handle)
end
end
|