@@ -33,6 +33,9 @@ public class SFF
3333 public List < double [ ] > Ffts { get ; private set ; }
3434 public int ImageHeight { get { return ( Ffts is null ) ? 0 : Ffts [ 0 ] . Length ; } }
3535 public int ImageWidth { get { return ( Ffts is null ) ? 0 : Ffts . Count ; } }
36+ public double [ ] times { get ; private set ; }
37+ public double [ ] freqs { get ; private set ; }
38+ public double [ ] mels { get ; private set ; }
3639
3740 [ Obsolete ( "use ImageWidth" , error : false ) ]
3841 public int FftWidth { get { return ImageWidth ; } }
@@ -53,6 +56,8 @@ public override string ToString()
5356 public SFF ( string loadFilePath )
5457 {
5558 Load ( loadFilePath ) ;
59+ CalculateTimes ( ) ;
60+ CalculateFrequencies ( ) ;
5661 }
5762
5863 public SFF ( Spectrogram spec , int melBinCount = 0 )
@@ -65,10 +70,9 @@ public SFF(Spectrogram spec, int melBinCount = 0)
6570 Height = spec . Height ;
6671 OffsetHz = spec . OffsetHz ;
6772 MelBinCount = melBinCount ;
68- if ( MelBinCount > 0 )
69- Ffts = spec . GetMelFFTs ( melBinCount ) ;
70- else
71- Ffts = spec . GetFFTs ( ) ;
73+ Ffts = ( melBinCount > 0 ) ? spec . GetMelFFTs ( melBinCount ) : spec . GetFFTs ( ) ;
74+ CalculateTimes ( ) ;
75+ CalculateFrequencies ( ) ;
7276 }
7377
7478 public Bitmap GetBitmap ( Colormap cmap = null , double intensity = 1 , bool dB = false )
@@ -247,5 +251,36 @@ public void Save(string filePath)
247251
248252 return ( timeSec , freq , mag ) ;
249253 }
254+
255+ private void CalculateTimes ( )
256+ {
257+ times = new double [ ImageWidth ] ;
258+ double stepSec = ( double ) StepSize / SampleRate ;
259+ for ( int i = 0 ; i < ImageWidth ; i ++ )
260+ times [ i ] = i * stepSec ;
261+ }
262+
263+ private void CalculateFrequencies ( )
264+ {
265+ freqs = new double [ ImageHeight ] ;
266+ mels = new double [ ImageHeight ] ;
267+
268+ double maxFreq = SampleRate / 2 ;
269+ double maxMel = FftSharp . Transform . MelFromFreq ( maxFreq ) ;
270+ for ( int y = 0 ; y < ImageHeight ; y ++ )
271+ {
272+ double frac = ( ImageHeight - y ) / ( double ) ImageHeight ;
273+ if ( IsMel )
274+ {
275+ mels [ y ] = frac * maxMel ;
276+ freqs [ y ] = FftSharp . Transform . MelToFreq ( mels [ y ] ) ;
277+ }
278+ else
279+ {
280+ freqs [ y ] = frac * maxFreq ;
281+ mels [ y ] = FftSharp . Transform . MelFromFreq ( freqs [ y ] ) ;
282+ }
283+ }
284+ }
250285 }
251286}
0 commit comments