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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//! Audio decoder module
//!
//! This module provides audio decoding capabilities using Symphonia
//! for various audio formats supported by Unity.
use super::formats::AudioCompressionFormat;
use super::types::{AudioClip, DecodedAudio};
use crate::error::{BinaryError, Result};
/// Main audio decoder
///
/// This struct provides methods for decoding various audio formats
/// using the Symphonia audio library.
pub struct AudioDecoder;
impl AudioDecoder {
/// Create a new audio decoder
pub fn new() -> Self {
Self
}
/// Decode audio using Symphonia (supports many formats)
pub fn decode(&self, clip: &AudioClip) -> Result<DecodedAudio> {
use std::io::Cursor;
use symphonia::core::audio::{AudioBufferRef, Signal};
use symphonia::core::codecs::{CODEC_TYPE_NULL, DecoderOptions};
use symphonia::core::errors::Error as SymphoniaError;
use symphonia::core::formats::FormatOptions;
use symphonia::core::io::MediaSourceStream;
use symphonia::core::meta::MetadataOptions;
use symphonia::core::probe::Hint;
if clip.data.is_empty() {
return Err(BinaryError::invalid_data("No audio data to decode"));
}
// Create a media source from the audio data
let cursor = Cursor::new(clip.data.clone());
let media_source = MediaSourceStream::new(Box::new(cursor), Default::default());
// Create a probe hint based on the compression format
let mut hint = Hint::new();
match clip.compression_format() {
AudioCompressionFormat::Vorbis => hint.with_extension("ogg"),
AudioCompressionFormat::MP3 => hint.with_extension("mp3"),
AudioCompressionFormat::AAC => hint.with_extension("aac"),
AudioCompressionFormat::PCM => hint.with_extension("wav"),
_ => &mut hint,
};
// Get the metadata and format readers
let meta_opts: MetadataOptions = Default::default();
let fmt_opts: FormatOptions = Default::default();
// Probe the media source
let probed = symphonia::default::get_probe()
.format(&hint, media_source, &fmt_opts, &meta_opts)
.map_err(|e| BinaryError::generic(format!("Failed to probe audio format: {}", e)))?;
// Get the instantiated format reader
let mut format = probed.format;
// Find the first audio track with a known (decodeable) codec
let track = format
.tracks()
.iter()
.find(|t| t.codec_params.codec != CODEC_TYPE_NULL)
.ok_or_else(|| BinaryError::generic("No supported audio tracks found"))?;
// Use the default options for the decoder
let dec_opts: DecoderOptions = Default::default();
// Create a decoder for the track
let mut decoder = symphonia::default::get_codecs()
.make(&track.codec_params, &dec_opts)
.map_err(|e| BinaryError::generic(format!("Failed to create decoder: {}", e)))?;
// Store the track identifier, it will be used to filter packets
let track_id = track.id;
let mut samples = Vec::new();
let mut sample_rate = 44100u32;
let mut channels = 2u32;
// The decode loop
loop {
// Get the next packet from the media format
let packet = match format.next_packet() {
Ok(packet) => packet,
Err(SymphoniaError::ResetRequired) => {
// The track list has been changed. Re-examine it and create a new set of decoders,
// then restart the decode loop. This is an advanced feature and it is not
// unreasonable to consider this "the end of the stream". As of v0.5.0, the only
// usage of this is for chained OGG physical streams.
break;
}
Err(SymphoniaError::IoError(_)) => {
// The packet reader has reached the end of the stream
break;
}
Err(err) => {
// A unrecoverable error occurred, halt decoding
return Err(BinaryError::generic(format!("Decode error: {}", err)));
}
};
// Consume any new metadata that has been read since the last packet
while !format.metadata().is_latest() {
// Pop the latest metadata and consume it
format.metadata().pop();
}
// If the packet does not belong to the selected track, skip over it
if packet.track_id() != track_id {
continue;
}
// Decode the packet into an audio buffer
match decoder.decode(&packet) {
Ok(decoded) => {
// Get audio buffer information
let spec = *decoded.spec();
sample_rate = spec.rate;
channels = spec.channels.count() as u32;
// Convert the audio buffer to f32 samples
match decoded {
AudioBufferRef::F32(buf) => {
samples.extend_from_slice(buf.chan(0));
if channels > 1 {
for ch in 1..channels as usize {
if ch < buf.spec().channels.count() {
let channel_samples = buf.chan(ch);
// Interleave channels
for (i, &sample) in channel_samples.iter().enumerate() {
if i * channels as usize + ch < samples.len() {
samples.insert(i * channels as usize + ch, sample);
} else {
samples.push(sample);
}
}
}
}
}
}
AudioBufferRef::U8(buf) => {
for ch in 0..channels as usize {
if ch < buf.spec().channels.count() {
let channel_samples = buf.chan(ch);
for &sample in channel_samples {
let normalized = (sample as f32 - 128.0) / 128.0;
samples.push(normalized);
}
}
}
}
AudioBufferRef::U16(buf) => {
for ch in 0..channels as usize {
if ch < buf.spec().channels.count() {
let channel_samples = buf.chan(ch);
for &sample in channel_samples {
let normalized = (sample as f32 - 32768.0) / 32768.0;
samples.push(normalized);
}
}
}
}
AudioBufferRef::U32(buf) => {
for ch in 0..channels as usize {
if ch < buf.spec().channels.count() {
let channel_samples = buf.chan(ch);
for &sample in channel_samples {
let normalized =
(sample as f32 - 2147483648.0) / 2147483648.0;
samples.push(normalized);
}
}
}
}
AudioBufferRef::S8(buf) => {
for ch in 0..channels as usize {
if ch < buf.spec().channels.count() {
let channel_samples = buf.chan(ch);
for &sample in channel_samples {
let normalized = sample as f32 / 128.0;
samples.push(normalized);
}
}
}
}
AudioBufferRef::S16(buf) => {
for ch in 0..channels as usize {
if ch < buf.spec().channels.count() {
let channel_samples = buf.chan(ch);
for &sample in channel_samples {
let normalized = sample as f32 / 32768.0;
samples.push(normalized);
}
}
}
}
AudioBufferRef::S32(buf) => {
for ch in 0..channels as usize {
if ch < buf.spec().channels.count() {
let channel_samples = buf.chan(ch);
for &sample in channel_samples {
let normalized = sample as f32 / 2147483648.0;
samples.push(normalized);
}
}
}
}
AudioBufferRef::F64(buf) => {
for ch in 0..channels as usize {
if ch < buf.spec().channels.count() {
let channel_samples = buf.chan(ch);
for &sample in channel_samples {
samples.push(sample as f32);
}
}
}
}
AudioBufferRef::U24(buf) => {
for ch in 0..channels as usize {
if ch < buf.spec().channels.count() {
let channel_samples = buf.chan(ch);
for &sample in channel_samples {
let value = sample.inner() as i32;
let normalized = (value as f32 - 8388608.0) / 8388608.0;
samples.push(normalized);
}
}
}
}
AudioBufferRef::S24(buf) => {
for ch in 0..channels as usize {
if ch < buf.spec().channels.count() {
let channel_samples = buf.chan(ch);
for &sample in channel_samples {
let value = sample.inner();
let normalized = value as f32 / 8388608.0;
samples.push(normalized);
}
}
}
}
}
}
Err(SymphoniaError::IoError(_)) => {
// The packet reader has reached the end of the stream
break;
}
Err(SymphoniaError::DecodeError(_)) => {
// Decode error, try to continue
continue;
}
Err(err) => {
// A unrecoverable error occurred, halt decoding
return Err(BinaryError::generic(format!("Decode error: {}", err)));
}
}
}
if samples.is_empty() {
return Err(BinaryError::generic("No audio samples decoded"));
}
Ok(DecodedAudio::new(samples, sample_rate, channels))
}
/// Check if a format can be decoded
pub fn can_decode(&self, format: AudioCompressionFormat) -> bool {
matches!(
format,
AudioCompressionFormat::PCM
| AudioCompressionFormat::Vorbis
| AudioCompressionFormat::MP3
| AudioCompressionFormat::AAC
| AudioCompressionFormat::ADPCM
)
}
/// Get list of supported formats
pub fn supported_formats(&self) -> Vec<AudioCompressionFormat> {
vec![
AudioCompressionFormat::PCM,
AudioCompressionFormat::Vorbis,
AudioCompressionFormat::MP3,
AudioCompressionFormat::AAC,
AudioCompressionFormat::ADPCM,
]
}
}
impl Default for AudioDecoder {
fn default() -> Self {
Self::new()
}
}