@@ -39,30 +39,13 @@ class W_Spectrogram extends Widget {
3939 int paddingRight = 26 ;
4040 int paddingTop = 8 ;
4141 int paddingBottom = 50 ;
42- int numHorizAxisDivs = 2 ; // == 40Hz
43- int numVertAxisDivs = 8 ;
44- final int [][] vertAxisLabels = {
45- {20 , 15 , 10 , 5 , 0 , 5 , 10 , 15 , 20 },
46- {40 , 30 , 20 , 10 , 0 , 10 , 20 , 30 , 40 },
47- {60 , 45 , 30 , 15 , 0 , 15 , 30 , 45 , 60 },
48- {100 , 75 , 50 , 25 , 0 , 25 , 50 , 75 , 100 },
49- {120 , 90 , 60 , 30 , 0 , 30 , 60 , 90 , 120 },
50- {250 , 188 , 125 , 63 , 0 , 63 , 125 , 188 , 250 }
51- };
52- int [] vertAxisLabel;
53- final float [][] horizAxisLabels = {
54- {30 , 25 , 20 , 15 , 10 , 5 , 0 },
55- {6 , 5 , 4 , 3 , 2 , 1 , 0 },
56- {3 , 2 , 1 , 0 },
57- {1.5 , 1 , .5 , 0 },
58- {1 , .5 , 0 }
59- };
60- float [] horizAxisLabel;
61- StringList horizAxisLabelStrings;
42+ StringList horizontalAxisLabelStrings;
6243
6344 float [] topFFTAvg;
6445 float [] botFFTAvg;
6546
47+ private SpectrogramMaxFrequency maxFrequency = SpectrogramMaxFrequency . MAX_60 ;
48+ private SpectrogramWindowSize windowSize = SpectrogramWindowSize . ONE_MINUTE ;
6649 private FFTLogLin logLin = FFTLogLin . LIN ;
6750
6851 W_Spectrogram (PApplet _parent ) {
@@ -84,25 +67,18 @@ class W_Spectrogram extends Widget {
8467 graphW = w - paddingRight - paddingLeft;
8568 graphH = h - paddingBottom - paddingTop;
8669
87- // FIX ME REMOVE THESE
88- settings. spectMaxFrqSave = 2 ;
89- settings. spectSampleRateSave = 4 ;
90-
91- vertAxisLabel = vertAxisLabels[settings. spectMaxFrqSave];
92- horizAxisLabel = horizAxisLabels[settings. spectSampleRateSave];
93- horizAxisLabelStrings = new StringList ();
9470 // Fetch/calculate the time strings for the horizontal axis ticks
95- fetchTimeStrings(numHorizAxisDivs );
71+ horizontalAxisLabelStrings = fetchTimeStrings();
9672
9773 // This is the protocol for setting up dropdowns.
9874 // Note that these 3 dropdowns correspond to the 3 global functions below
9975 // You just need to make sure the "id" (the 1st String) has the same name as the corresponding function
100- addDropdown(" SpectrogramMaxFreq " , " Max Freq " , Arrays . asList(settings . spectMaxFrqArray ), settings . spectMaxFrqSave );
101- addDropdown(" SpectrogramSampleRate " , " Window" , Arrays . asList(settings . spectSampleRateArray ), settings . spectSampleRateSave );
102- addDropdown(" SpectrogramLogLin " , " Log/Lin" , logLin. getEnumStringsAsList(), logLin. getIndex());
76+ addDropdown(" spectrogramMaxFrequencyDropdown " , " Max Hz " , maxFrequency . getEnumStringsAsList( ), maxFrequency . getIndex() );
77+ addDropdown(" spectrogramSampleRateDropdown " , " Window" , windowSize . getEnumStringsAsList( ), windowSize . getIndex() );
78+ addDropdown(" spectrogramLogLinDropdown " , " Log/Lin" , logLin. getEnumStringsAsList(), logLin. getIndex());
10379
10480 // Resize the height of the data image using default
105- dataImageH = vertAxisLabel [0 ] * 2 ;
81+ dataImageH = maxFrequency . getAxisLabels() [0 ] * 2 ;
10682 // Create image using correct dimensions! Fixes bug where image size and labels do not align on session start.
10783 dataImg = createImage (dataImageW, dataImageH, RGB );
10884 }
@@ -133,7 +109,8 @@ class W_Spectrogram extends Widget {
133109 // Make sure we are always draw new pixels on the right
134110 xPos = dataImg. width - 1 ;
135111 // Fetch/calculate the time strings for the horizontal axis ticks
136- fetchTimeStrings(numHorizAxisDivs);
112+ horizontalAxisLabelStrings. clear();
113+ horizontalAxisLabelStrings = fetchTimeStrings();
137114 }
138115
139116 // State change check
@@ -281,17 +258,18 @@ class W_Spectrogram extends Widget {
281258 pushStyle ();
282259 // draw horizontal axis ticks from left to right
283260 int tickMarkSize = 7 ; // in pixels
284- float horizAxisX = graphX;
285- float horizAxisY = graphY + scaledH * dataImageH;
261+ float horizontalAxisX = graphX;
262+ float horizontalAxisY = graphY + scaledH * dataImageH;
286263 stroke (255 );
287264 fill (255 );
288265 strokeWeight (2 );
289266 textSize (11 );
290- for (int i = 0 ; i <= numHorizAxisDivs; i++ ) {
291- float offset = scaledW * dataImageW * (float (i) / numHorizAxisDivs);
292- line (horizAxisX + offset, horizAxisY, horizAxisX + offset, horizAxisY + tickMarkSize);
293- if (horizAxisLabelStrings. get(i) != null ) {
294- text (horizAxisLabelStrings. get(i), horizAxisX + offset - (int )textWidth (horizAxisLabelStrings. get(i))/ 2 , horizAxisY + tickMarkSize * 3 );
267+ int horizontalAxisDivCount = windowSize. getAxisLabels(). length;
268+ for (int i = 0 ; i < horizontalAxisDivCount; i++ ) {
269+ float offset = scaledW * dataImageW * (float (i) / horizontalAxisDivCount);
270+ line (horizontalAxisX + offset, horizontalAxisY, horizontalAxisX + offset, horizontalAxisY + tickMarkSize);
271+ if (horizontalAxisLabelStrings. get(i) != null ) {
272+ text (horizontalAxisLabelStrings. get(i), horizontalAxisX + offset - (int )textWidth (horizontalAxisLabelStrings. get(i))/ 2 , horizontalAxisY + tickMarkSize * 3 );
295273 }
296274 }
297275 popStyle ();
@@ -312,19 +290,20 @@ class W_Spectrogram extends Widget {
312290
313291 pushStyle ();
314292 // draw vertical axis ticks from top to bottom
315- float vertAxisX = graphX;
316- float vertAxisY = graphY;
293+ float verticalAxisX = graphX;
294+ float verticalAxisY = graphY;
317295 stroke (255 );
318296 fill (255 );
319297 textSize (12 );
320298 strokeWeight (2 );
321- for (int i = 0 ; i <= numVertAxisDivs; i++ ) {
322- float offset = scaledH * dataImageH * (float (i) / numVertAxisDivs);
323- // if (i <= numVertAxisDivs/2) offset -= 2;
324- line (vertAxisX, vertAxisY + offset, vertAxisX - tickMarkSize, vertAxisY + offset);
325- if (vertAxisLabel[i] == 0 ) midLineY = int (vertAxisY + offset);
299+ int verticalAxisDivCount = maxFrequency. getAxisLabels(). length - 1 ;
300+ for (int i = 0 ; i < verticalAxisDivCount; i++ ) {
301+ float offset = scaledH * dataImageH * (float (i) / verticalAxisDivCount);
302+ // if (i <= verticalAxisDivCount/2) offset -= 2;
303+ line (verticalAxisX, verticalAxisY + offset, verticalAxisX - tickMarkSize, verticalAxisY + offset);
304+ if (maxFrequency. getAxisLabels()[i] == 0 ) midLineY = int (verticalAxisY + offset);
326305 offset += paddingTop/ 2 ;
327- text (vertAxisLabel [i], vertAxisX - tickMarkSize* 2 - textWidth (Integer . toString(vertAxisLabel [i])), vertAxisY + offset);
306+ text (maxFrequency . getAxisLabels() [i], verticalAxisX - tickMarkSize* 2 - textWidth (Integer . toString(maxFrequency . getAxisLabels() [i])), verticalAxisY + offset);
328307 }
329308 popStyle ();
330309
@@ -415,23 +394,22 @@ class W_Spectrogram extends Widget {
415394 return sum / _activeChan. size();
416395 }
417396
418- void fetchTimeStrings (int numAxisTicks ) {
419- horizAxisLabelStrings . clear ();
397+ private StringList fetchTimeStrings () {
398+ StringList output = new StringList ();
420399 LocalDateTime time;
421400 DateTimeFormatter formatter = DateTimeFormatter . ofPattern(" HH:mm:ss" );
422-
423401 if (getCurrentTimeStamp() == 0 ) {
424402 time = LocalDateTime . now();
425403 } else {
426404 time = LocalDateTime . ofInstant(Instant . ofEpochMilli(getCurrentTimeStamp()),
427405 TimeZone . getDefault(). toZoneId());
428406 }
429-
430- for (int i = 0 ; i <= numAxisTicks; i++ ) {
431- long l = (long )(horizAxisLabel[i] * 60f );
407+ for (int i = 0 ; i < windowSize. getAxisLabels(). length; i++ ) {
408+ long l = (long )(windowSize. getAxisLabels()[i] * 60f );
432409 LocalDateTime t = time. minus(l, ChronoUnit . SECONDS );
433- horizAxisLabelStrings . append(t. format(formatter));
410+ output . append(t. format(formatter));
434411 }
412+ return output;
435413 }
436414
437415 // Identical to the method in TimeSeries, but allows spectrogram to get the data directly from the playback data in the background
@@ -457,45 +435,33 @@ class W_Spectrogram extends Widget {
457435 public void setLogLin (int n ) {
458436 logLin = logLin. values()[n];
459437 }
438+
439+ public void setMaxFrequency (int n ) {
440+ maxFrequency = maxFrequency. values()[n];
441+ // Resize the height of the data image
442+ dataImageH = maxFrequency. getAxisLabels()[0 ] * 2 ;
443+ // Overwrite the existing image
444+ dataImg = createImage (dataImageW, dataImageH, RGB );
445+ }
446+
447+ public void setWindowSize (int n ) {
448+ windowSize = windowSize. values()[n];
449+ setScrollSpeed(windowSize. getScrollSpeed());
450+ horizontalAxisLabelStrings. clear();
451+ horizontalAxisLabelStrings = fetchTimeStrings();
452+ dataImg = createImage (dataImageW, dataImageH, RGB );
453+
454+ }
460455};
461456
462- // These functions need to be global! These functions are activated when an item from the corresponding dropdown is selected
463- // triggered when there is an event in the Spectrogram Widget MaxFreq. Dropdown
464- void SpectrogramMaxFreq (int n ) {
465- settings. spectMaxFrqSave = n;
466- // reset the vertical axis labels
467- w_spectrogram. vertAxisLabel = w_spectrogram. vertAxisLabels[n];
468- // Resize the height of the data image
469- w_spectrogram. dataImageH = w_spectrogram. vertAxisLabel[0 ] * 2 ;
470- // overwrite the existing image because the sample rate is about to change
471- w_spectrogram. dataImg = createImage (w_spectrogram. dataImageW, w_spectrogram. dataImageH, RGB );
457+ public void spectrogramMaxFrequencyDropdown (int n ) {
458+ w_spectrogram. setMaxFrequency(n);
472459}
473460
474- void SpectrogramSampleRate (int n ) {
475- settings. spectSampleRateSave = n;
476- // overwrite the existing image because the sample rate is about to change
477- w_spectrogram. dataImg = createImage (w_spectrogram. dataImageW, w_spectrogram. dataImageH, RGB );
478- w_spectrogram. horizAxisLabel = w_spectrogram. horizAxisLabels[n];
479- if (n == 0 ) {
480- w_spectrogram. numHorizAxisDivs = 6 ;
481- w_spectrogram. setScrollSpeed(1000 );
482- } else if (n == 1 ) {
483- w_spectrogram. numHorizAxisDivs = 6 ;
484- w_spectrogram. setScrollSpeed(200 );
485- } else if (n == 2 ) {
486- w_spectrogram. numHorizAxisDivs = 3 ;
487- w_spectrogram. setScrollSpeed(100 );
488- } else if (n == 3 ) {
489- w_spectrogram. numHorizAxisDivs = 3 ;
490- w_spectrogram. setScrollSpeed(50 );
491- } else if (n == 4 ) {
492- w_spectrogram. numHorizAxisDivs = 2 ;
493- w_spectrogram. setScrollSpeed(25 );
494- }
495- w_spectrogram. horizAxisLabelStrings. clear();
496- w_spectrogram. fetchTimeStrings(w_spectrogram. numHorizAxisDivs);
461+ public void spectrogramWindowDropdown (int n ) {
462+ w_spectrogram. setWindowSize(n);
497463}
498464
499- public void SpectrogramLogLin (int n ) {
465+ public void spectrogramLogLinDropdown (int n ) {
500466 w_spectrogram. setLogLin(n);
501467}
0 commit comments