1+ use super :: Language ;
12use crate :: properties:: FileProperties ;
23
34/// Properties from the EBML header
@@ -113,17 +114,41 @@ impl Default for SegmentInfo {
113114}
114115
115116/// A full descriptor for an audio track
116- #[ derive( Debug , Clone , PartialEq , Default ) ]
117+ #[ derive( Debug , Clone , PartialEq ) ]
117118pub struct AudioTrackDescriptor {
118119 pub ( crate ) number : u64 ,
119120 pub ( crate ) uid : u64 ,
120- pub ( crate ) language : String ,
121+ pub ( crate ) enabled : bool ,
122+ pub ( crate ) default : bool ,
123+ pub ( crate ) language : Language ,
121124 pub ( crate ) default_duration : u64 ,
122125 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 > ,
124128 pub ( crate ) settings : AudioTrackSettings ,
125129}
126130
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+
127152impl AudioTrackDescriptor {
128153 /// The track number
129154 pub fn number ( & self ) -> u64 {
@@ -135,10 +160,20 @@ impl AudioTrackDescriptor {
135160 self . uid
136161 }
137162
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+
138173 /// The language of the track, in the Matroska languages form
139174 ///
140175 /// 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 {
142177 & self . language
143178 }
144179
@@ -157,8 +192,13 @@ impl AudioTrackDescriptor {
157192 }
158193
159194 /// 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 ( )
162202 }
163203
164204 /// The audio settings of the track
@@ -170,23 +210,23 @@ impl AudioTrackDescriptor {
170210/// Settings for an audio track
171211#[ derive( Debug , Clone , PartialEq , Default ) ]
172212pub 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 ,
175215 pub ( crate ) channels : u8 ,
176216 pub ( crate ) bit_depth : Option < u8 > ,
177217 pub ( crate ) emphasis : Option < EbmlAudioTrackEmphasis > ,
178218}
179219
180220impl AudioTrackSettings {
181221 /// The sampling frequency of the track
182- pub fn sampling_frequency ( & self ) -> u32 {
222+ pub fn sampling_frequency ( & self ) -> f64 {
183223 self . sampling_frequency
184224 }
185225
186226 /// Real output sampling frequency in Hz (used for SBR techniques).
187227 ///
188228 /// 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 {
190230 self . output_sampling_frequency
191231 }
192232
@@ -210,7 +250,6 @@ impl AudioTrackSettings {
210250#[ allow( missing_docs) ]
211251#[ derive( Debug , Copy , Clone , Eq , PartialEq ) ]
212252pub enum EbmlAudioTrackEmphasis {
213- None = 0 ,
214253 CdAudio = 1 ,
215254 Reserved = 2 ,
216255 CcitJ17 = 3 ,
@@ -225,6 +264,27 @@ pub enum EbmlAudioTrackEmphasis {
225264 PhonoNartb = 16 ,
226265}
227266
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+
228288/// EBML audio properties
229289#[ derive( Debug , Clone , PartialEq , Default ) ]
230290pub struct EbmlProperties {
@@ -248,28 +308,24 @@ impl EbmlProperties {
248308 & self . extensions
249309 }
250310
251- /// Information from the `\EBML\ Segment\Info` element
311+ /// Information from the `\Segment\Info` element
252312 pub fn segment_info ( & self ) -> & SegmentInfo {
253313 & self . segment_info
254314 }
255315
256316 /// All audio tracks in the file
257317 ///
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.
261319 pub fn audio_tracks ( & self ) -> & [ AudioTrackDescriptor ] {
262320 & self . audio_tracks
263321 }
264322
265323 /// Information about the default audio track
266324 ///
267325 /// 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.
271327 pub fn default_audio_track ( & self ) -> Option < & AudioTrackDescriptor > {
272- self . audio_tracks . first ( )
328+ self . audio_tracks . iter ( ) . find ( |track| track . default )
273329 }
274330}
275331
@@ -283,7 +339,7 @@ impl From<EbmlProperties> for FileProperties {
283339 duration : todo ! ( "Support duration" ) ,
284340 overall_bitrate : todo ! ( "Support bitrate" ) ,
285341 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 ) ,
287343 bit_depth : default_audio_track. settings . bit_depth ,
288344 channels : Some ( default_audio_track. settings . channels ) ,
289345 channel_mask : todo ! ( "Channel mask" ) ,
0 commit comments