1
+ use super :: Language ;
1
2
use crate :: properties:: FileProperties ;
2
3
3
4
/// Properties from the EBML header
@@ -113,17 +114,41 @@ impl Default for SegmentInfo {
113
114
}
114
115
115
116
/// A full descriptor for an audio track
116
- #[ derive( Debug , Clone , PartialEq , Default ) ]
117
+ #[ derive( Debug , Clone , PartialEq ) ]
117
118
pub struct AudioTrackDescriptor {
118
119
pub ( crate ) number : u64 ,
119
120
pub ( crate ) uid : u64 ,
120
- pub ( crate ) language : String ,
121
+ pub ( crate ) enabled : bool ,
122
+ pub ( crate ) default : bool ,
123
+ pub ( crate ) language : Language ,
121
124
pub ( crate ) default_duration : u64 ,
122
125
pub ( crate ) codec_id : String ,
123
- pub ( crate ) codec_private : Vec < u8 > ,
126
+ pub ( crate ) codec_private : Option < Vec < u8 > > ,
127
+ pub ( crate ) codec_name : Option < String > ,
124
128
pub ( crate ) settings : AudioTrackSettings ,
125
129
}
126
130
131
+ impl Default for AudioTrackDescriptor {
132
+ fn default ( ) -> Self {
133
+ AudioTrackDescriptor {
134
+ // Note, these values are not spec compliant and will hopefully be overwritten when
135
+ // parsing. It doesn't really matter though, since we aren't an encoder.
136
+ number : 0 ,
137
+ uid : 0 ,
138
+ default_duration : 0 ,
139
+ codec_id : String :: new ( ) ,
140
+
141
+ // Spec-compliant defaults
142
+ enabled : true ,
143
+ default : true ,
144
+ language : Language :: Iso639_2 ( String :: from ( "eng" ) ) ,
145
+ codec_private : None ,
146
+ codec_name : None ,
147
+ settings : AudioTrackSettings :: default ( ) ,
148
+ }
149
+ }
150
+ }
151
+
127
152
impl AudioTrackDescriptor {
128
153
/// The track number
129
154
pub fn number ( & self ) -> u64 {
@@ -135,10 +160,20 @@ impl AudioTrackDescriptor {
135
160
self . uid
136
161
}
137
162
163
+ /// Whether the track is usable
164
+ pub fn is_enabled ( & self ) -> bool {
165
+ self . enabled
166
+ }
167
+
168
+ /// Whether the track is eligible for automatic selection
169
+ pub fn is_default ( & self ) -> bool {
170
+ self . default
171
+ }
172
+
138
173
/// The language of the track, in the Matroska languages form
139
174
///
140
175
/// NOTE: See [basics](https://matroska.org/technical/basics.html#language-codes) on language codes.
141
- pub fn language ( & self ) -> & str {
176
+ pub fn language ( & self ) -> & Language {
142
177
& self . language
143
178
}
144
179
@@ -157,8 +192,13 @@ impl AudioTrackDescriptor {
157
192
}
158
193
159
194
/// Private data only known to the codec
160
- pub fn codec_private ( & self ) -> & [ u8 ] {
161
- & self . codec_private
195
+ pub fn codec_private ( & self ) -> Option < & [ u8 ] > {
196
+ self . codec_private . as_deref ( )
197
+ }
198
+
199
+ /// A human-readable string for the [codec_id](AudioTrackDescriptor::codec_id)
200
+ pub fn codec_name ( & self ) -> Option < & str > {
201
+ self . codec_name . as_deref ( )
162
202
}
163
203
164
204
/// The audio settings of the track
@@ -170,23 +210,23 @@ impl AudioTrackDescriptor {
170
210
/// Settings for an audio track
171
211
#[ derive( Debug , Clone , PartialEq , Default ) ]
172
212
pub struct AudioTrackSettings {
173
- pub ( crate ) sampling_frequency : u32 ,
174
- pub ( crate ) output_sampling_frequency : u32 ,
213
+ pub ( crate ) sampling_frequency : f64 ,
214
+ pub ( crate ) output_sampling_frequency : f64 ,
175
215
pub ( crate ) channels : u8 ,
176
216
pub ( crate ) bit_depth : Option < u8 > ,
177
217
pub ( crate ) emphasis : Option < EbmlAudioTrackEmphasis > ,
178
218
}
179
219
180
220
impl AudioTrackSettings {
181
221
/// The sampling frequency of the track
182
- pub fn sampling_frequency ( & self ) -> u32 {
222
+ pub fn sampling_frequency ( & self ) -> f64 {
183
223
self . sampling_frequency
184
224
}
185
225
186
226
/// Real output sampling frequency in Hz (used for SBR techniques).
187
227
///
188
228
/// The default value for `output_sampling_frequency` of the same TrackEntry is equal to the [`Self::sampling_frequency`].
189
- pub fn output_sampling_frequency ( & self ) -> u32 {
229
+ pub fn output_sampling_frequency ( & self ) -> f64 {
190
230
self . output_sampling_frequency
191
231
}
192
232
@@ -210,7 +250,6 @@ impl AudioTrackSettings {
210
250
#[ allow( missing_docs) ]
211
251
#[ derive( Debug , Copy , Clone , Eq , PartialEq ) ]
212
252
pub enum EbmlAudioTrackEmphasis {
213
- None = 0 ,
214
253
CdAudio = 1 ,
215
254
Reserved = 2 ,
216
255
CcitJ17 = 3 ,
@@ -225,6 +264,27 @@ pub enum EbmlAudioTrackEmphasis {
225
264
PhonoNartb = 16 ,
226
265
}
227
266
267
+ impl EbmlAudioTrackEmphasis {
268
+ /// Get the audio emphasis from a `u8`
269
+ pub fn from_u8 ( value : u8 ) -> Option < Self > {
270
+ match value {
271
+ 1 => Some ( Self :: CdAudio ) ,
272
+ 2 => Some ( Self :: Reserved ) ,
273
+ 3 => Some ( Self :: CcitJ17 ) ,
274
+ 4 => Some ( Self :: Fm50 ) ,
275
+ 5 => Some ( Self :: Fm75 ) ,
276
+ 10 => Some ( Self :: PhonoRiaa ) ,
277
+ 11 => Some ( Self :: PhonoIecN78 ) ,
278
+ 12 => Some ( Self :: PhonoTeldec ) ,
279
+ 13 => Some ( Self :: PhonoEmi ) ,
280
+ 14 => Some ( Self :: PhonoColumbiaLp ) ,
281
+ 15 => Some ( Self :: PhonoLondon ) ,
282
+ 16 => Some ( Self :: PhonoNartb ) ,
283
+ _ => None ,
284
+ }
285
+ }
286
+ }
287
+
228
288
/// EBML audio properties
229
289
#[ derive( Debug , Clone , PartialEq , Default ) ]
230
290
pub struct EbmlProperties {
@@ -248,28 +308,24 @@ impl EbmlProperties {
248
308
& self . extensions
249
309
}
250
310
251
- /// Information from the `\EBML\ Segment\Info` element
311
+ /// Information from the `\Segment\Info` element
252
312
pub fn segment_info ( & self ) -> & SegmentInfo {
253
313
& self . segment_info
254
314
}
255
315
256
316
/// All audio tracks in the file
257
317
///
258
- /// This includes all audio tracks in the Matroska `\EBML\Segment\Tracks` element.
259
- ///
260
- /// NOTE: The first audio track is **always** the default audio track.
318
+ /// This includes all audio tracks in the Matroska `\Segment\Tracks` element.
261
319
pub fn audio_tracks ( & self ) -> & [ AudioTrackDescriptor ] {
262
320
& self . audio_tracks
263
321
}
264
322
265
323
/// Information about the default audio track
266
324
///
267
325
/// The information is extracted from the first audio track with its default flag set
268
- /// in the `\EBML\Segment\Tracks` element.
269
- ///
270
- /// NOTE: This will always return `Some` unless [`ParseOptions::read_properties`](crate::config::ParseOptions::read_properties) is set to `false`.
326
+ /// in the `\Segment\Tracks` element.
271
327
pub fn default_audio_track ( & self ) -> Option < & AudioTrackDescriptor > {
272
- self . audio_tracks . first ( )
328
+ self . audio_tracks . iter ( ) . find ( |track| track . default )
273
329
}
274
330
}
275
331
@@ -283,7 +339,7 @@ impl From<EbmlProperties> for FileProperties {
283
339
duration : todo ! ( "Support duration" ) ,
284
340
overall_bitrate : todo ! ( "Support bitrate" ) ,
285
341
audio_bitrate : todo ! ( "Support bitrate" ) ,
286
- sample_rate : Some ( default_audio_track. settings . sampling_frequency ) ,
342
+ sample_rate : Some ( default_audio_track. settings . sampling_frequency as u32 ) ,
287
343
bit_depth : default_audio_track. settings . bit_depth ,
288
344
channels : Some ( default_audio_track. settings . channels ) ,
289
345
channel_mask : todo ! ( "Channel mask" ) ,
0 commit comments