@@ -7,7 +7,7 @@ use egui_plot::{GridMark, Line, Plot, PlotPoints};
7
7
use plotters:: prelude:: * ;
8
8
9
9
use super :: loading:: SaveFileContents ;
10
- use super :: state:: FftMicrophone ;
10
+ use super :: state:: { FftMicrophone , FftScaling } ;
11
11
use crate :: components:: microphone:: Microphone ;
12
12
use crate :: math:: fft:: calc_mic_spectrum;
13
13
@@ -33,6 +33,7 @@ pub struct PlotTabs<'a> {
33
33
pub mics : & ' a [ & ' a Microphone ] ,
34
34
pub pixel_buffer : & ' a mut PixelBuffersItem < ' a > ,
35
35
pub fft_microphone : & ' a mut FftMicrophone ,
36
+ pub scaling : & ' a mut FftScaling ,
36
37
pub commands : & ' a mut Commands < ' a , ' a > ,
37
38
pub enabled_spectrogram : bool ,
38
39
}
@@ -190,30 +191,54 @@ impl<'a> egui_dock::TabViewer for PlotTabs<'a> {
190
191
} ) ;
191
192
}
192
193
Tab :: Frequency => {
193
- egui:: ComboBox :: from_label ( "FFT Microphone" )
194
- . selected_text ( if let Some ( index) = self . fft_microphone . mic_id {
195
- format ! ( "Microphone {index}" )
196
- } else {
197
- "No Microphone Selected" . to_string ( )
198
- } )
199
- . show_ui ( ui, |ui| {
200
- for mic in self . mics {
194
+ ui. horizontal ( |ui| {
195
+ egui:: ComboBox :: from_label ( "FFT Microphone" )
196
+ . selected_text ( if let Some ( index) = self . fft_microphone . mic_id {
197
+ format ! ( "Microphone {index}" )
198
+ } else {
199
+ "No Microphone Selected" . to_string ( )
200
+ } )
201
+ . show_ui ( ui, |ui| {
202
+ for mic in self . mics {
203
+ ui. selectable_value (
204
+ & mut self . fft_microphone . mic_id ,
205
+ Some ( mic. id ) ,
206
+ format ! ( "Microphone {}" , mic. id) ,
207
+ ) ;
208
+ }
209
+ } ) ;
210
+
211
+ ui. add ( egui:: Separator :: default ( ) . vertical ( ) ) ;
212
+
213
+ egui:: ComboBox :: from_label ( "Scaling" )
214
+ . selected_text ( self . scaling . to_string ( ) )
215
+ . show_ui ( ui, |ui| {
201
216
ui. selectable_value (
202
- & mut self . fft_microphone . mic_id ,
203
- Some ( mic . id ) ,
204
- format ! ( "Microphone {}" , mic . id ) ,
217
+ self . scaling ,
218
+ FftScaling :: ZeroToOne ,
219
+ format ! ( "{}" , FftScaling :: ZeroToOne ) ,
205
220
) ;
206
- }
207
- } ) ;
221
+ ui. selectable_value (
222
+ self . scaling ,
223
+ FftScaling :: Decibels ,
224
+ format ! ( "{}" , FftScaling :: Decibels ) ,
225
+ ) ;
226
+ } ) ;
227
+ } ) ;
208
228
209
229
ui. separator ( ) ;
230
+
231
+ let unit = match self . scaling {
232
+ FftScaling :: ZeroToOne => "(0-1)" ,
233
+ FftScaling :: Decibels => "(dB)" ,
234
+ } ;
210
235
Plot :: new ( "fft_plot" )
211
236
. allow_zoom ( [ false , false ] )
212
237
. allow_scroll ( false )
213
238
. allow_drag ( false )
214
239
. allow_boxed_zoom ( false )
215
240
. x_axis_label ( "Frequency (Hz)" )
216
- . y_axis_label ( "Intensity (dB)" )
241
+ . y_axis_label ( format ! ( "Intensity {}" , unit ) )
217
242
. x_grid_spacer ( |input| {
218
243
let mut marks = Vec :: with_capacity (
219
244
input. bounds . 1 as usize - input. bounds . 0 as usize + 1 ,
@@ -228,10 +253,11 @@ impl<'a> egui_dock::TabViewer for PlotTabs<'a> {
228
253
marks
229
254
} )
230
255
. x_axis_formatter ( |mark, _, _| format ! ( "{:.0}" , 10_f64 . powf( mark. value) ) )
231
- . label_formatter ( |_, value| {
256
+ . label_formatter ( move |_, value| {
232
257
format ! (
233
- "Intensity: {:.2} dB \n Frequency: {:.2} Hz " ,
258
+ "Intensity: {:.2} {} \n Frequency: {:.2} (Hz) " ,
234
259
value. y,
260
+ unit,
235
261
10_f64 . powf( value. x)
236
262
)
237
263
} )
@@ -245,7 +271,7 @@ impl<'a> egui_dock::TabViewer for PlotTabs<'a> {
245
271
. iter ( )
246
272
. find ( |m| m. id == self . fft_microphone . mic_id . expect ( "no mic selected" ) )
247
273
{
248
- let mapped_spectrum = calc_mic_spectrum ( mic) ;
274
+ let mapped_spectrum = calc_mic_spectrum ( mic, * self . scaling ) ;
249
275
// remove the first element, because of log it is at x=-inf
250
276
let mapped_spectrum = & mapped_spectrum[ 1 ..] ;
251
277
0 commit comments