svcore  1.9
Preferences.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 Chris Cannam and QMUL.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #include "Preferences.h"
17 
18 #include "Exceptions.h"
19 
20 #include "TempDirectory.h"
21 
22 #include <QDir>
23 #include <QFileInfo>
24 #include <QMutex>
25 #include <QSettings>
26 
29 
32 {
33  if (!m_instance) m_instance = new Preferences();
34  return m_instance;
35 }
36 
38  m_spectrogramSmoothing(SpectrogramInterpolated),
39  m_spectrogramXSmoothing(SpectrogramXInterpolated),
40  m_tuningFrequency(440),
41  m_propertyBoxLayout(VerticallyStacked),
42  m_windowType(HanningWindow),
43  m_resampleQuality(1),
44  m_omitRecentTemps(true),
45  m_tempDirRoot(""),
46  m_fixedSampleRate(0),
47  m_resampleOnLoad(false),
48  m_normaliseAudio(false),
49  m_viewFontSize(10),
50  m_backgroundMode(BackgroundFromTheme),
51  m_timeToTextMode(TimeToTextMs),
52  m_octave(4),
53  m_showSplash(true)
54 {
55  QSettings settings;
56  settings.beginGroup("Preferences");
58  (settings.value("spectrogram-y-smoothing", int(m_spectrogramSmoothing)).toInt());
60  (settings.value("spectrogram-x-smoothing", int(m_spectrogramXSmoothing)).toInt());
61  m_tuningFrequency = settings.value("tuning-frequency", 440.f).toDouble();
63  (settings.value("property-box-layout", int(VerticallyStacked)).toInt());
65  (settings.value("window-type", int(HanningWindow)).toInt());
66  m_resampleQuality = settings.value("resample-quality", 1).toInt();
67  m_fixedSampleRate = settings.value("fixed-sample-rate", 0).toInt();
68  m_resampleOnLoad = settings.value("resample-on-load", false).toBool();
69  m_normaliseAudio = settings.value("normalise-audio", false).toBool();
71  (settings.value("background-mode", int(BackgroundFromTheme)).toInt());
73  (settings.value("time-to-text-mode", int(TimeToTextMs)).toInt());
74  m_octave = (settings.value("octave-of-middle-c", 4)).toInt();
75  m_viewFontSize = settings.value("view-font-size", 10).toInt();
76  m_showSplash = settings.value("show-splash", true).toBool();
77  settings.endGroup();
78 
79  settings.beginGroup("TempDirectory");
80  m_tempDirRoot = settings.value("create-in", "$HOME").toString();
81  settings.endGroup();
82 }
83 
85 {
86 }
87 
90 {
91  PropertyList props;
92  props.push_back("Spectrogram Y Smoothing");
93  props.push_back("Spectrogram X Smoothing");
94  props.push_back("Tuning Frequency");
95  props.push_back("Property Box Layout");
96  props.push_back("Window Type");
97  props.push_back("Resample Quality");
98  props.push_back("Omit Temporaries from Recent Files");
99  props.push_back("Resample On Load");
100  props.push_back("Normalise Audio");
101  props.push_back("Fixed Sample Rate");
102  props.push_back("Temporary Directory Root");
103  props.push_back("Background Mode");
104  props.push_back("Time To Text Mode");
105  props.push_back("Octave Numbering System");
106  props.push_back("View Font Size");
107  props.push_back("Show Splash Screen");
108  return props;
109 }
110 
111 QString
113 {
114  if (name == "Spectrogram Y Smoothing") {
115  return tr("Spectrogram y-axis interpolation:");
116  }
117  if (name == "Spectrogram X Smoothing") {
118  return tr("Spectrogram x-axis interpolation:");
119  }
120  if (name == "Tuning Frequency") {
121  return tr("Frequency of concert A");
122  }
123  if (name == "Property Box Layout") {
124  return tr("Property box layout");
125  }
126  if (name == "Window Type") {
127  return tr("Spectral analysis window shape");
128  }
129  if (name == "Resample Quality") {
130  return tr("Playback resampler type");
131  }
132  if (name == "Normalise Audio") {
133  return tr("Normalise audio signal when reading from audio file");
134  }
135  if (name == "Omit Temporaries from Recent Files") {
136  return tr("Omit temporaries from Recent Files menu");
137  }
138  if (name == "Resample On Load") {
139  return tr("Resample mismatching files on import");
140  }
141  if (name == "Fixed Sample Rate") {
142  return tr("Single fixed sample rate to resample all files to");
143  }
144  if (name == "Temporary Directory Root") {
145  return tr("Location for cache file directory");
146  }
147  if (name == "Background Mode") {
148  return tr("Background colour preference");
149  }
150  if (name == "Time To Text Mode") {
151  return tr("Time display format");
152  }
153  if (name == "Octave Numbering System") {
154  return tr("Label middle C as");
155  }
156  if (name == "View Font Size") {
157  return tr("Font size for text overlays");
158  }
159  if (name == "Show Splash Screen") {
160  return tr("Show splash screen on startup");
161  }
162  return name;
163 }
164 
167 {
168  if (name == "Spectrogram Y Smoothing") {
169  return ValueProperty;
170  }
171  if (name == "Spectrogram X Smoothing") {
172  return ValueProperty;
173  }
174  if (name == "Tuning Frequency") {
175  return RangeProperty;
176  }
177  if (name == "Property Box Layout") {
178  return ValueProperty;
179  }
180  if (name == "Window Type") {
181  return ValueProperty;
182  }
183  if (name == "Resample Quality") {
184  return ValueProperty;
185  }
186  if (name == "Normalise Audio") {
187  return ToggleProperty;
188  }
189  if (name == "Omit Temporaries from Recent Files") {
190  return ToggleProperty;
191  }
192  if (name == "Resample On Load") {
193  return ToggleProperty;
194  }
195  if (name == "Fixed Sample Rate") {
196  return ValueProperty;
197  }
198  if (name == "Temporary Directory Root") {
199  // It's an arbitrary string, we don't have a set of values for this
200  return InvalidProperty;
201  }
202  if (name == "Background Mode") {
203  return ValueProperty;
204  }
205  if (name == "Time To Text Mode") {
206  return ValueProperty;
207  }
208  if (name == "Octave Numbering System") {
209  return ValueProperty;
210  }
211  if (name == "View Font Size") {
212  return RangeProperty;
213  }
214  if (name == "Show Splash Screen") {
215  return ToggleProperty;
216  }
217  return InvalidProperty;
218 }
219 
220 int
222  int *min, int *max, int *deflt) const
223 {
224  if (name == "Spectrogram Y Smoothing") {
225  if (min) *min = 0;
226  if (max) *max = 3;
227  if (deflt) *deflt = int(SpectrogramInterpolated);
228  return int(m_spectrogramSmoothing);
229  }
230  if (name == "Spectrogram X Smoothing") {
231  if (min) *min = 0;
232  if (max) *max = 1;
233  if (deflt) *deflt = int(SpectrogramXInterpolated);
234  return int(m_spectrogramXSmoothing);
235  }
236 
238 
239  if (name == "Property Box Layout") {
240  if (min) *min = 0;
241  if (max) *max = 1;
242  if (deflt) *deflt = 0;
243  return m_propertyBoxLayout == Layered ? 1 : 0;
244  }
245 
246  if (name == "Window Type") {
247  if (min) *min = int(RectangularWindow);
248  if (max) *max = int(BlackmanHarrisWindow);
249  if (deflt) *deflt = int(HanningWindow);
250  return int(m_windowType);
251  }
252 
253  if (name == "Resample Quality") {
254  if (min) *min = 0;
255  if (max) *max = 2;
256  if (deflt) *deflt = 1;
257  return m_resampleQuality;
258  }
259 
260  if (name == "Omit Temporaries from Recent Files") {
261  if (deflt) *deflt = 1;
262  }
263 
264  if (name == "Background Mode") {
265  if (min) *min = 0;
266  if (max) *max = 2;
267  if (deflt) *deflt = 0;
268  return int(m_backgroundMode);
269  }
270 
271  if (name == "Time To Text Mode") {
272  if (min) *min = 0;
273  if (max) *max = 6;
274  if (deflt) *deflt = 0;
275  return int(m_timeToTextMode);
276  }
277 
278  if (name == "Octave Numbering System") {
279  // we don't support arbitrary octaves in the gui, because we
280  // want to be able to label what the octave system comes
281  // from. so we support 0, 3, 4 and 5.
282  if (min) *min = 0;
283  if (max) *max = 3;
284  if (deflt) *deflt = 2;
286  }
287 
288  if (name == "View Font Size") {
289  if (min) *min = 3;
290  if (max) *max = 48;
291  if (deflt) *deflt = 10;
292  return int(m_viewFontSize);
293  }
294 
295  if (name == "Show Splash Screen") {
296  if (deflt) *deflt = 1;
297  }
298 
299  return 0;
300 }
301 
302 QString
304  int value) const
305 {
306  if (name == "Property Box Layout") {
307  if (value == 0) return tr("Show boxes for all panes");
308  else return tr("Show box for current pane only");
309  }
310  if (name == "Window Type") {
311  switch (WindowType(value)) {
312  case RectangularWindow: return tr("Rectangular");
313  case BartlettWindow: return tr("Triangular");
314  case HammingWindow: return tr("Hamming");
315  case HanningWindow: return tr("Hann");
316  case BlackmanWindow: return tr("Blackman");
317  case GaussianWindow: return tr("Gaussian");
318  case ParzenWindow: return tr("Parzen");
319  case NuttallWindow: return tr("Nuttall");
320  case BlackmanHarrisWindow: return tr("Blackman-Harris");
321  }
322  }
323  if (name == "Resample Quality") {
324  switch (value) {
325  case 0: return tr("Fastest");
326  case 1: return tr("Standard");
327  case 2: return tr("Highest quality");
328  }
329  }
330  if (name == "Spectrogram Y Smoothing") {
331  switch (value) {
332  case NoSpectrogramSmoothing: return tr("None");
333  case SpectrogramInterpolated: return tr("Linear interpolation");
334  case SpectrogramZeroPadded: return tr("4 x Oversampling");
335  case SpectrogramZeroPaddedAndInterpolated: return tr("4 x Oversampling with interpolation");
336  }
337  }
338  if (name == "Spectrogram X Smoothing") {
339  switch (value) {
340  case NoSpectrogramXSmoothing: return tr("None");
341  case SpectrogramXInterpolated: return tr("Linear interpolation");
342  }
343  }
344  if (name == "Background Mode") {
345  switch (value) {
346  case BackgroundFromTheme: return tr("Follow desktop theme");
347  case DarkBackground: return tr("Dark background");
348  case LightBackground: return tr("Light background");
349  }
350  }
351  if (name == "Time To Text Mode") {
352  switch (value) {
353  case TimeToTextMs: return tr("Standard (to millisecond)");
354  case TimeToTextUs: return tr("High resolution (to microsecond)");
355  case TimeToText24Frame: return tr("24 FPS");
356  case TimeToText25Frame: return tr("25 FPS");
357  case TimeToText30Frame: return tr("30 FPS");
358  case TimeToText50Frame: return tr("50 FPS");
359  case TimeToText60Frame: return tr("60 FPS");
360  }
361  }
362  if (name == "Octave Numbering System") {
363  switch (value) {
364  case C0_Centre: return tr("C0 - middle of octave scale");
365  case C3_Logic: return tr("C3 - common MIDI sequencer convention");
366  case C4_ASA: return tr("C4 - ASA American standard");
367  case C5_Sonar: return tr("C5 - used in Cakewalk and others");
368  }
369  }
370 
371  return "";
372 }
373 
374 QString
376 {
377  return tr("Preferences");
378 }
379 
380 QString
382 {
383  return "preferences";
384 }
385 
386 void
387 Preferences::setProperty(const PropertyName &name, int value)
388 {
389  if (name == "Spectrogram Y Smoothing") {
391  } else if (name == "Spectrogram X Smoothing") {
393  } else if (name == "Tuning Frequency") {
395  } else if (name == "Property Box Layout") {
397  } else if (name == "Window Type") {
398  setWindowType(WindowType(value));
399  } else if (name == "Resample Quality") {
400  setResampleQuality(value);
401  } else if (name == "Omit Temporaries from Recent Files") {
402  setOmitTempsFromRecentFiles(value ? true : false);
403  } else if (name == "Background Mode") {
405  } else if (name == "Time To Text Mode") {
407  } else if (name == "Octave Numbering System") {
409  (OctaveNumberingSystem(value)));
410  } else if (name == "View Font Size") {
411  setViewFontSize(value);
412  } else if (name == "Show Splash Screen") {
413  setShowSplash(value ? true : false);
414  }
415 }
416 
417 void
419 {
420  if (m_spectrogramSmoothing != smoothing) {
421 
422  // "smoothing" is one of those words that looks increasingly
423  // ridiculous the more you see it. Smoothing smoothing smoothing.
424  m_spectrogramSmoothing = smoothing;
425 
426  QSettings settings;
427  settings.beginGroup("Preferences");
428  settings.setValue("spectrogram-y-smoothing", int(smoothing));
429  settings.endGroup();
430  emit propertyChanged("Spectrogram Y Smoothing");
431  }
432 }
433 
434 void
436 {
437  if (m_spectrogramXSmoothing != smoothing) {
438 
439  // "smoothing" is one of those words that looks increasingly
440  // ridiculous the more you see it. Smoothing smoothing smoothing.
441  m_spectrogramXSmoothing = smoothing;
442 
443  QSettings settings;
444  settings.beginGroup("Preferences");
445  settings.setValue("spectrogram-x-smoothing", int(smoothing));
446  settings.endGroup();
447  emit propertyChanged("Spectrogram X Smoothing");
448  }
449 }
450 
451 void
453 {
454  if (m_tuningFrequency != freq) {
455  m_tuningFrequency = freq;
456  QSettings settings;
457  settings.beginGroup("Preferences");
458  settings.setValue("tuning-frequency", freq);
459  settings.endGroup();
460  emit propertyChanged("Tuning Frequency");
461  }
462 }
463 
464 void
466 {
467  if (m_propertyBoxLayout != layout) {
468  m_propertyBoxLayout = layout;
469  QSettings settings;
470  settings.beginGroup("Preferences");
471  settings.setValue("property-box-layout", int(layout));
472  settings.endGroup();
473  emit propertyChanged("Property Box Layout");
474  }
475 }
476 
477 void
479 {
480  if (m_windowType != type) {
481  m_windowType = type;
482  QSettings settings;
483  settings.beginGroup("Preferences");
484  settings.setValue("window-type", int(type));
485  settings.endGroup();
486  emit propertyChanged("Window Type");
487  }
488 }
489 
490 void
492 {
493  if (m_resampleQuality != q) {
494  m_resampleQuality = q;
495  QSettings settings;
496  settings.beginGroup("Preferences");
497  settings.setValue("resample-quality", q);
498  settings.endGroup();
499  emit propertyChanged("Resample Quality");
500  }
501 }
502 
503 void
505 {
506  if (m_omitRecentTemps != omit) {
507  m_omitRecentTemps = omit;
508  QSettings settings;
509  settings.beginGroup("Preferences");
510  settings.setValue("omit-recent-temporaries", omit);
511  settings.endGroup();
512  emit propertyChanged("Omit Temporaries from Recent Files");
513  }
514 }
515 
516 void
518 {
519  if (root == QDir::home().absolutePath()) {
520  root = "$HOME";
521  }
522  if (m_tempDirRoot != root) {
523  m_tempDirRoot = root;
524  QSettings settings;
525  settings.beginGroup("TempDirectory");
526  settings.setValue("create-in", root);
527  settings.endGroup();
528  emit propertyChanged("Temporary Directory Root");
529  }
530 }
531 
532 void
534 {
535  if (m_resampleOnLoad != resample) {
536  m_resampleOnLoad = resample;
537  QSettings settings;
538  settings.beginGroup("Preferences");
539  settings.setValue("resample-on-load", resample);
540  settings.endGroup();
541  emit propertyChanged("Resample On Load");
542  }
543 }
544 
545 void
547 {
548  if (m_fixedSampleRate != rate) {
549  m_fixedSampleRate = rate;
550  QSettings settings;
551  settings.beginGroup("Preferences");
552  settings.setValue("fixed-sample-rate", rate);
553  settings.endGroup();
554  emit propertyChanged("Fixed Sample Rate");
555  }
556 }
557 
558 void
560 {
561  if (m_normaliseAudio != norm) {
562  m_normaliseAudio = norm;
563  QSettings settings;
564  settings.beginGroup("Preferences");
565  settings.setValue("normalise-audio", norm);
566  settings.endGroup();
567  emit propertyChanged("Normalise Audio");
568  }
569 }
570 
571 void
573 {
574  if (m_backgroundMode != mode) {
575 
576  m_backgroundMode = mode;
577 
578  QSettings settings;
579  settings.beginGroup("Preferences");
580  settings.setValue("background-mode", int(mode));
581  settings.endGroup();
582  emit propertyChanged("Background Mode");
583  }
584 }
585 
586 void
588 {
589  if (m_timeToTextMode != mode) {
590 
591  m_timeToTextMode = mode;
592 
593  QSettings settings;
594  settings.beginGroup("Preferences");
595  settings.setValue("time-to-text-mode", int(mode));
596  settings.endGroup();
597  emit propertyChanged("Time To Text Mode");
598  }
599 }
600 
601 void
603 {
604  if (m_octave != oct) {
605 
606  m_octave = oct;
607 
608  QSettings settings;
609  settings.beginGroup("Preferences");
610  settings.setValue("octave-of-middle-c", int(oct));
611  settings.endGroup();
612  emit propertyChanged("Octave Numbering System");
613  }
614 }
615 
616 int
618 {
619  switch (s) {
620  case C0_Centre: return 0;
621  case C3_Logic: return 3;
622  case C4_ASA: return 4;
623  case C5_Sonar: return 5;
624  default: return 4;
625  }
626 }
627 
630 {
631  switch (o) {
632  case 0: return C0_Centre;
633  case 3: return C3_Logic;
634  case 4: return C4_ASA;
635  case 5: return C5_Sonar;
636  default: return C4_ASA;
637  }
638 }
639 
640 void
642 {
643  if (m_viewFontSize != size) {
644 
645  m_viewFontSize = size;
646 
647  QSettings settings;
648  settings.beginGroup("Preferences");
649  settings.setValue("view-font-size", size);
650  settings.endGroup();
651  emit propertyChanged("View Font Size");
652  }
653 }
654 
655 void
657 {
658  if (m_showSplash != show) {
659 
660  m_showSplash = show;
661 
662  QSettings settings;
663  settings.beginGroup("Preferences");
664  settings.setValue("show-splash", show);
665  settings.endGroup();
666  emit propertyChanged("Show Splash Screen");
667  }
668 }
669 
int m_viewFontSize
Definition: Preferences.h:156
void setOctaveOfMiddleC(int oct)
virtual PropertyList getProperties() const
Get a list of the names of all the supported properties on this container.
Definition: Preferences.cpp:89
void setResampleOnLoad(bool)
SpectrogramSmoothing m_spectrogramSmoothing
Definition: Preferences.h:145
virtual ~Preferences()
Definition: Preferences.cpp:84
void propertyChanged(PropertyContainer::PropertyName)
virtual QString getPropertyLabel(const PropertyName &) const
Return the human-readable (and i18n'ised) name of a property.
PropertyBoxLayout m_propertyBoxLayout
Definition: Preferences.h:148
bool m_resampleOnLoad
Definition: Preferences.h:154
void setSpectrogramXSmoothing(SpectrogramXSmoothing smoothing)
bool m_showSplash
Definition: Preferences.h:160
static Preferences * getInstance()
Definition: Preferences.cpp:31
void setShowSplash(bool)
bool m_omitRecentTemps
Definition: Preferences.h:151
WindowType
Definition: Window.h:27
void setTimeToTextMode(TimeToTextMode mode)
void setNormaliseAudio(bool)
void setWindowType(WindowType type)
SpectrogramXSmoothing m_spectrogramXSmoothing
Definition: Preferences.h:146
static int getOctaveOfMiddleCInSystem(OctaveNumberingSystem s)
virtual PropertyType getPropertyType(const PropertyName &) const
Return the type of the given property, or InvalidProperty if the property is not supported on this co...
void setSpectrogramSmoothing(SpectrogramSmoothing smoothing)
WindowType m_windowType
Definition: Preferences.h:149
void setOmitTempsFromRecentFiles(bool omit)
virtual QString getPropertyValueLabel(const PropertyName &, int value) const
If the given property is a ValueProperty, return the display label to be used for the given value for...
BackgroundMode m_backgroundMode
Definition: Preferences.h:157
PropertyBoxLayout
!! harmonise with PaneStack
Definition: Preferences.h:57
static Preferences * m_instance
Definition: Preferences.h:130
void setPropertyBoxLayout(PropertyBoxLayout layout)
void setTemporaryDirectoryRoot(QString tempDirRoot)
static OctaveNumberingSystem getSystemWithMiddleCInOctave(int o)
virtual QString getPropertyContainerName() const
virtual void setProperty(const PropertyName &, int)
void setViewFontSize(int size)
bool m_normaliseAudio
Definition: Preferences.h:155
float m_tuningFrequency
Definition: Preferences.h:147
virtual int getPropertyRangeAndValue(const PropertyName &, int *, int *, int *) const
Return the minimum and maximum values for the given property and its current value in this container.
int m_fixedSampleRate
Definition: Preferences.h:153
void setFixedSampleRate(int)
int m_resampleQuality
Definition: Preferences.h:150
std::vector< PropertyName > PropertyList
TimeToTextMode m_timeToTextMode
Definition: Preferences.h:158
QString m_tempDirRoot
Definition: Preferences.h:152
virtual QString getPropertyContainerIconName() const
void setTuningFrequency(float freq)
void setBackgroundMode(BackgroundMode mode)
void setResampleQuality(int quality)