1- use std:: { num:: NonZeroUsize , ops:: Range } ;
1+ use std:: {
2+ num:: { NonZeroU32 , NonZeroUsize } ,
3+ ops:: Range ,
4+ } ;
25
36use firewheel_core:: {
47 collector:: ArcGc ,
@@ -12,14 +15,24 @@ pub struct DecodedAudio(pub symphonium::DecodedAudio);
1215
1316impl DecodedAudio {
1417 pub fn duration_seconds ( & self ) -> f64 {
15- self . 0 . frames ( ) as f64 / self . 0 . sample_rate ( ) as f64
18+ self . 0 . frames ( ) as f64 / self . 0 . sample_rate ( ) . get ( ) as f64
1619 }
1720
1821 pub fn into_dyn_resource ( self ) -> ArcGc < dyn SampleResource > {
1922 ArcGc :: new_unsized ( || {
2023 bevy_platform:: sync:: Arc :: new ( self ) as bevy_platform:: sync:: Arc < dyn SampleResource >
2124 } )
2225 }
26+
27+ /// The sample rate of this resource.
28+ pub fn sample_rate ( & self ) -> NonZeroU32 {
29+ self . 0 . sample_rate ( )
30+ }
31+
32+ /// The sample rate of the audio resource before it was resampled (if it was resampled).
33+ pub fn original_sample_rate ( & self ) -> NonZeroU32 {
34+ self . 0 . original_sample_rate ( )
35+ }
2336}
2437
2538impl From < DecodedAudio > for ArcGc < dyn SampleResource > {
@@ -36,6 +49,10 @@ impl SampleResourceInfo for DecodedAudio {
3649 fn len_frames ( & self ) -> u64 {
3750 self . 0 . frames ( ) as u64
3851 }
52+
53+ fn sample_rate ( & self ) -> Option < NonZeroU32 > {
54+ Some ( self . 0 . sample_rate ( ) )
55+ }
3956}
4057
4158impl SampleResource for DecodedAudio {
@@ -77,8 +94,18 @@ impl From<symphonium::DecodedAudio> for DecodedAudio {
7794pub struct DecodedAudioF32 ( pub symphonium:: DecodedAudioF32 ) ;
7895
7996impl DecodedAudioF32 {
80- pub fn duration_seconds ( & self , sample_rate : u32 ) -> f64 {
81- self . 0 . frames ( ) as f64 / sample_rate as f64
97+ pub fn duration_seconds ( & self , sample_rate : NonZeroU32 ) -> f64 {
98+ self . 0 . frames ( ) as f64 / sample_rate. get ( ) as f64
99+ }
100+
101+ /// The sample rate of this resource.
102+ pub fn sample_rate ( & self ) -> NonZeroU32 {
103+ self . 0 . sample_rate
104+ }
105+
106+ /// The sample rate of the audio resource before it was resampled (if it was resampled).
107+ pub fn original_sample_rate ( & self ) -> NonZeroU32 {
108+ self . 0 . original_sample_rate
82109 }
83110}
84111
@@ -90,6 +117,10 @@ impl SampleResourceInfo for DecodedAudioF32 {
90117 fn len_frames ( & self ) -> u64 {
91118 self . 0 . frames ( ) as u64
92119 }
120+
121+ fn sample_rate ( & self ) -> Option < NonZeroU32 > {
122+ Some ( self . 0 . sample_rate )
123+ }
93124}
94125
95126impl SampleResource for DecodedAudioF32 {
@@ -135,7 +166,7 @@ pub fn load_audio_file<P: AsRef<std::path::Path>>(
135166 . load (
136167 path,
137168 #[ cfg( feature = "resample" ) ]
138- target_sample_rate. map ( |s| s . get ( ) ) ,
169+ target_sample_rate,
139170 #[ cfg( feature = "resample" ) ]
140171 resample_quality,
141172 None ,
@@ -169,7 +200,7 @@ pub fn load_audio_file_from_source(
169200 source,
170201 hint,
171202 #[ cfg( feature = "resample" ) ]
172- target_sample_rate. map ( |s| s . get ( ) ) ,
203+ target_sample_rate,
173204 #[ cfg( feature = "resample" ) ]
174205 resample_quality,
175206 None ,
@@ -197,7 +228,7 @@ pub fn load_audio_file_stretched<P: AsRef<std::path::Path>>(
197228 stretch : f64 ,
198229) -> Result < DecodedAudio , symphonium:: error:: LoadError > {
199230 loader
200- . load_f32_stretched ( path, stretch, target_sample_rate. map ( |s| s . get ( ) ) , None )
231+ . load_stretched ( path, stretch, target_sample_rate, None )
201232 . map ( |d| DecodedAudio ( d. into ( ) ) )
202233}
203234
@@ -224,13 +255,7 @@ pub fn load_audio_file_from_source_stretched(
224255 stretch : f64 ,
225256) -> Result < DecodedAudio , symphonium:: error:: LoadError > {
226257 loader
227- . load_f32_from_source_stretched (
228- source,
229- hint,
230- stretch,
231- target_sample_rate. map ( |s| s. get ( ) ) ,
232- None ,
233- )
258+ . load_from_source_stretched ( source, hint, stretch, target_sample_rate, None )
234259 . map ( |d| DecodedAudio ( d. into ( ) ) )
235260}
236261
0 commit comments