mirror of
https://github.com/cookiengineer/audacity
synced 2025-06-21 14:50:06 +02:00
ChoiceSetting contains vectors; simplify constructions of it
This commit is contained in:
parent
e485afa156
commit
9b67e7538f
@ -397,39 +397,35 @@ inline float Dither::ShapedDither(float sample)
|
||||
return result;
|
||||
}
|
||||
|
||||
static const EnumValueSymbol choicesDither[] = {
|
||||
static const std::initializer_list<EnumValueSymbol> choicesDither{
|
||||
{ XO("None") },
|
||||
{ XO("Rectangle") },
|
||||
{ XO("Triangle") },
|
||||
{ XO("Shaped") },
|
||||
};
|
||||
static const size_t nChoicesDither = WXSIZEOF( choicesDither );
|
||||
static const int intChoicesDither[] = {
|
||||
(int) DitherType::none,
|
||||
(int) DitherType::rectangle,
|
||||
(int) DitherType::triangle,
|
||||
(int) DitherType::shaped,
|
||||
static const std::initializer_list<int> intChoicesDither{
|
||||
DitherType::none,
|
||||
DitherType::rectangle,
|
||||
DitherType::triangle,
|
||||
DitherType::shaped,
|
||||
};
|
||||
static_assert(
|
||||
nChoicesDither == WXSIZEOF( intChoicesDither ),
|
||||
"size mismatch"
|
||||
);
|
||||
|
||||
static const size_t defaultFastDither = 0; // none
|
||||
|
||||
EnumSetting Dither::FastSetting{
|
||||
wxT("Quality/DitherAlgorithmChoice"),
|
||||
choicesDither, nChoicesDither, defaultFastDither,
|
||||
choicesDither,
|
||||
0, // none
|
||||
|
||||
// for migrating old preferences:
|
||||
intChoicesDither,
|
||||
wxT("Quality/DitherAlgorithm")
|
||||
};
|
||||
|
||||
static const size_t defaultBestDither = 3; // shaped
|
||||
|
||||
EnumSetting Dither::BestSetting{
|
||||
wxT("Quality/HQDitherAlgorithmChoice"),
|
||||
choicesDither, nChoicesDither, defaultBestDither,
|
||||
choicesDither,
|
||||
3, // shaped
|
||||
|
||||
// for migrating old preferences:
|
||||
intChoicesDither,
|
||||
wxT("Quality/HQDitherAlgorithm")
|
||||
};
|
||||
|
@ -253,16 +253,17 @@ wxString ChoiceSetting::Read() const
|
||||
// Remap to default if the string is not known -- this avoids surprises
|
||||
// in case we try to interpret config files from future versions
|
||||
auto index = Find( value );
|
||||
if ( index >= mnSymbols )
|
||||
if ( index >= mSymbols.size() )
|
||||
value = defaultValue;
|
||||
return value;
|
||||
}
|
||||
|
||||
size_t ChoiceSetting::Find( const wxString &value ) const
|
||||
{
|
||||
auto start = begin();
|
||||
return size_t(
|
||||
std::find( begin(), end(), EnumValueSymbol{ value, {} } )
|
||||
- mSymbols );
|
||||
std::find( start, end(), EnumValueSymbol{ value, {} } )
|
||||
- start );
|
||||
}
|
||||
|
||||
void ChoiceSetting::Migrate( wxString &value )
|
||||
@ -273,7 +274,7 @@ void ChoiceSetting::Migrate( wxString &value )
|
||||
bool ChoiceSetting::Write( const wxString &value )
|
||||
{
|
||||
auto index = Find( value );
|
||||
if (index >= mnSymbols)
|
||||
if (index >= mSymbols.size())
|
||||
return false;
|
||||
|
||||
auto result = gPrefs->Write( mKey, value );
|
||||
@ -281,24 +282,38 @@ bool ChoiceSetting::Write( const wxString &value )
|
||||
return result;
|
||||
}
|
||||
|
||||
EnumSetting::EnumSetting(
|
||||
const wxString &key,
|
||||
EnumValueSymbols symbols,
|
||||
size_t defaultSymbol,
|
||||
|
||||
std::vector<int> intValues, // must have same size as symbols
|
||||
const wxString &oldKey
|
||||
)
|
||||
: ChoiceSetting{ key, std::move( symbols ), defaultSymbol }
|
||||
, mIntValues{ std::move( intValues ) }
|
||||
, mOldKey{ oldKey }
|
||||
{
|
||||
auto size = mSymbols.size();
|
||||
if( mIntValues.size() != size ) {
|
||||
wxASSERT( false );
|
||||
mIntValues.resize( size );
|
||||
}
|
||||
}
|
||||
|
||||
int EnumSetting::ReadInt() const
|
||||
{
|
||||
if (!mIntValues)
|
||||
return 0;
|
||||
|
||||
auto index = Find( Read() );
|
||||
wxASSERT( index < mnSymbols );
|
||||
wxASSERT( index < mIntValues.size() );
|
||||
return mIntValues[ index ];
|
||||
}
|
||||
|
||||
size_t EnumSetting::FindInt( int code ) const
|
||||
{
|
||||
if (!mIntValues)
|
||||
return mnSymbols;
|
||||
|
||||
const auto start = mIntValues.begin();
|
||||
return size_t(
|
||||
std::find( mIntValues, mIntValues + mnSymbols, code )
|
||||
- mIntValues );
|
||||
std::find( start, mIntValues.end(), code )
|
||||
- start );
|
||||
}
|
||||
|
||||
void EnumSetting::Migrate( wxString &value )
|
||||
@ -311,7 +326,7 @@ void EnumSetting::Migrate( wxString &value )
|
||||
// Audacity. But further changes will be stored only to the NEW key
|
||||
// and won't be seen then.
|
||||
auto index = FindInt( intValue );
|
||||
if ( index >= mnSymbols )
|
||||
if ( index >= mSymbols.size() )
|
||||
index = mDefaultSymbol;
|
||||
value = mSymbols[index].Internal();
|
||||
Write(value);
|
||||
@ -322,7 +337,7 @@ void EnumSetting::Migrate( wxString &value )
|
||||
bool EnumSetting::WriteInt( int code ) // you flush gPrefs afterward
|
||||
{
|
||||
auto index = FindInt( code );
|
||||
if ( index >= mnSymbols )
|
||||
if ( index >= mSymbols.size() )
|
||||
return false;
|
||||
return Write( mSymbols[index].Internal() );
|
||||
}
|
||||
|
32
src/Prefs.h
32
src/Prefs.h
@ -86,6 +86,8 @@ public:
|
||||
int mVersionMicroKeyInit{};
|
||||
};
|
||||
|
||||
using EnumValueSymbols = std::vector< EnumValueSymbol >;
|
||||
|
||||
/// Packages a table of user-visible choices each with an internal code string,
|
||||
/// a preference key path, and a default choice
|
||||
class ChoiceSetting
|
||||
@ -93,24 +95,23 @@ class ChoiceSetting
|
||||
public:
|
||||
ChoiceSetting(
|
||||
const wxString &key,
|
||||
const EnumValueSymbol symbols[], size_t nSymbols,
|
||||
EnumValueSymbols symbols,
|
||||
size_t defaultSymbol
|
||||
)
|
||||
: mKey{ key }
|
||||
|
||||
, mSymbols{ symbols }
|
||||
, mnSymbols{ nSymbols }
|
||||
, mSymbols{ std::move( symbols ) }
|
||||
|
||||
, mDefaultSymbol{ defaultSymbol }
|
||||
{
|
||||
wxASSERT( defaultSymbol < nSymbols );
|
||||
wxASSERT( defaultSymbol < mSymbols.size() );
|
||||
}
|
||||
|
||||
const wxString &Key() const { return mKey; }
|
||||
const EnumValueSymbol &Default() const
|
||||
{ return mSymbols[mDefaultSymbol]; }
|
||||
const EnumValueSymbol *begin() const { return mSymbols; }
|
||||
const EnumValueSymbol *end() const { return mSymbols + mnSymbols; }
|
||||
EnumValueSymbols::const_iterator begin() const { return mSymbols.begin(); }
|
||||
EnumValueSymbols::const_iterator end() const { return mSymbols.end(); }
|
||||
|
||||
wxString Read() const;
|
||||
bool Write( const wxString &value ); // you flush gPrefs afterward
|
||||
@ -121,8 +122,7 @@ protected:
|
||||
|
||||
const wxString mKey;
|
||||
|
||||
const EnumValueSymbol *mSymbols;
|
||||
const size_t mnSymbols;
|
||||
const EnumValueSymbols mSymbols;
|
||||
|
||||
// stores an internal value
|
||||
mutable bool mMigrated { false };
|
||||
@ -139,18 +139,12 @@ class EnumSetting : public ChoiceSetting
|
||||
public:
|
||||
EnumSetting(
|
||||
const wxString &key,
|
||||
const EnumValueSymbol symbols[], size_t nSymbols,
|
||||
EnumValueSymbols symbols,
|
||||
size_t defaultSymbol,
|
||||
|
||||
const int intValues[] = nullptr, // must have same size as symbols
|
||||
const wxString &oldKey = wxString("")
|
||||
)
|
||||
: ChoiceSetting{ key, symbols, nSymbols, defaultSymbol }
|
||||
, mIntValues{ intValues }
|
||||
, mOldKey{ oldKey }
|
||||
{
|
||||
wxASSERT( mIntValues );
|
||||
}
|
||||
std::vector<int> intValues, // must have same size as symbols
|
||||
const wxString &oldKey
|
||||
);
|
||||
|
||||
// Read and write the encoded values
|
||||
virtual int ReadInt() const;
|
||||
@ -161,7 +155,7 @@ protected:
|
||||
void Migrate( wxString& ) override;
|
||||
|
||||
private:
|
||||
const int *mIntValues;
|
||||
std::vector<int> mIntValues;
|
||||
const wxString mOldKey;
|
||||
};
|
||||
|
||||
|
@ -52,53 +52,36 @@ Resample::~Resample()
|
||||
}
|
||||
|
||||
//////////
|
||||
static const EnumValueSymbol methodNames[] = {
|
||||
static const std::initializer_list<EnumValueSymbol> methodNames{
|
||||
{ wxT("LowQuality"), XO("Low Quality (Fastest)") },
|
||||
{ wxT("MediumQuality"), XO("Medium Quality") },
|
||||
{ wxT("HighQuality"), XO("High Quality") },
|
||||
{ wxT("BestQuality"), XO("Best Quality (Slowest)") }
|
||||
};
|
||||
|
||||
static const size_t numMethods = WXSIZEOF(methodNames);
|
||||
|
||||
static const wxString fastMethodKey =
|
||||
wxT("/Quality/LibsoxrSampleRateConverterChoice");
|
||||
|
||||
static const wxString bestMethodKey =
|
||||
wxT("/Quality/LibsoxrHQSampleRateConverterChoice");
|
||||
|
||||
static const wxString oldFastMethodKey =
|
||||
wxT("/Quality/LibsoxrSampleRateConverter");
|
||||
|
||||
static const wxString oldBestMethodKey =
|
||||
wxT("/Quality/LibsoxrHQSampleRateConverter");
|
||||
|
||||
static const size_t fastMethodDefault = 1; // Medium Quality
|
||||
static const size_t bestMethodDefault = 3; // Best Quality
|
||||
|
||||
static const int intChoicesMethod[] = {
|
||||
static auto intChoicesMethod = {
|
||||
0, 1, 2, 3
|
||||
};
|
||||
|
||||
static_assert( WXSIZEOF(intChoicesMethod) == numMethods, "size mismatch" );
|
||||
|
||||
EnumSetting Resample::FastMethodSetting{
|
||||
fastMethodKey,
|
||||
methodNames, numMethods,
|
||||
fastMethodDefault,
|
||||
wxT("/Quality/LibsoxrSampleRateConverterChoice"),
|
||||
methodNames,
|
||||
1, // Medium Quality
|
||||
|
||||
// for migrating old preferences:
|
||||
intChoicesMethod,
|
||||
oldFastMethodKey
|
||||
wxT("/Quality/LibsoxrSampleRateConverter")
|
||||
};
|
||||
|
||||
EnumSetting Resample::BestMethodSetting
|
||||
{
|
||||
bestMethodKey,
|
||||
methodNames, numMethods,
|
||||
bestMethodDefault,
|
||||
wxT("/Quality/LibsoxrHQSampleRateConverterChoice"),
|
||||
methodNames,
|
||||
3, // Best Quality,
|
||||
|
||||
// for migrating old preferences:
|
||||
intChoicesMethod,
|
||||
oldBestMethodKey
|
||||
wxT("/Quality/LibsoxrHQSampleRateConverter")
|
||||
};
|
||||
|
||||
//////////
|
||||
|
@ -32,26 +32,21 @@
|
||||
|
||||
//////////
|
||||
|
||||
static const EnumValueSymbol choicesFormat[] = {
|
||||
{ wxT("Format16Bit"), XO("16-bit") },
|
||||
{ wxT("Format24Bit"), XO("24-bit") },
|
||||
{ wxT("Format32BitFloat"), XO("32-bit float") }
|
||||
};
|
||||
static const size_t nChoicesFormat = WXSIZEOF( choicesFormat );
|
||||
static const int intChoicesFormat[] = {
|
||||
int16Sample,
|
||||
int24Sample,
|
||||
floatSample
|
||||
};
|
||||
static_assert( nChoicesFormat == WXSIZEOF(intChoicesFormat), "size mismatch" );
|
||||
|
||||
static const size_t defaultChoiceFormat = 2; // floatSample
|
||||
|
||||
static EnumSetting formatSetting{
|
||||
wxT("/SamplingRate/DefaultProjectSampleFormatChoice"),
|
||||
choicesFormat, nChoicesFormat, defaultChoiceFormat,
|
||||
|
||||
intChoicesFormat,
|
||||
{
|
||||
{ wxT("Format16Bit"), XO("16-bit") },
|
||||
{ wxT("Format24Bit"), XO("24-bit") },
|
||||
{ wxT("Format32BitFloat"), XO("32-bit float") }
|
||||
},
|
||||
2, // floatSample
|
||||
|
||||
// for migrating old preferences:
|
||||
{
|
||||
int16Sample,
|
||||
int24Sample,
|
||||
floatSample
|
||||
},
|
||||
wxT("/SamplingRate/DefaultProjectSampleFormat"),
|
||||
};
|
||||
|
||||
|
@ -54,33 +54,20 @@ namespace {
|
||||
|
||||
|
||||
//////////
|
||||
static const EnumValueSymbol choicesView[] = {
|
||||
{ XO("Waveform") },
|
||||
{ wxT("WaveformDB"), XO("Waveform (dB)") },
|
||||
{ XO("Spectrogram") }
|
||||
};
|
||||
static const int intChoicesView[] = {
|
||||
(int)(WaveTrackViewConstants::Waveform),
|
||||
(int)(WaveTrackViewConstants::obsoleteWaveformDBDisplay),
|
||||
(int)(WaveTrackViewConstants::Spectrum)
|
||||
};
|
||||
static const size_t nChoicesView = WXSIZEOF(choicesView);
|
||||
static_assert( nChoicesView == WXSIZEOF(intChoicesView), "size mismatch" );
|
||||
|
||||
static const size_t defaultChoiceView = 0;
|
||||
|
||||
class TracksViewModeSetting : public EnumSetting {
|
||||
class TracksViewModeEnumSetting : public EnumSetting {
|
||||
public:
|
||||
TracksViewModeSetting(
|
||||
TracksViewModeEnumSetting(
|
||||
const wxString &key,
|
||||
const EnumValueSymbol symbols[], size_t nSymbols,
|
||||
EnumValueSymbols symbols,
|
||||
size_t defaultSymbol,
|
||||
|
||||
const int intValues[],
|
||||
std::vector<int> intValues,
|
||||
const wxString &oldKey
|
||||
)
|
||||
: EnumSetting{
|
||||
key, symbols, nSymbols, defaultSymbol, intValues, oldKey }
|
||||
key, std::move( symbols ), defaultSymbol,
|
||||
std::move( intValues ), oldKey
|
||||
}
|
||||
{}
|
||||
|
||||
void Migrate( wxString &value ) override
|
||||
@ -106,18 +93,28 @@ public:
|
||||
// Now future-proof 2.1.1 against a recurrence of this sort of bug!
|
||||
viewMode = WaveTrackViewConstants::ValidateWaveTrackDisplay(viewMode);
|
||||
|
||||
const_cast<TracksViewModeSetting*>(this)->WriteInt( viewMode );
|
||||
const_cast<TracksViewModeEnumSetting*>(this)->WriteInt( viewMode );
|
||||
gPrefs->Flush();
|
||||
|
||||
value = mSymbols[ FindInt(viewMode) ].Internal();
|
||||
}
|
||||
};
|
||||
|
||||
static TracksViewModeSetting viewModeSetting{
|
||||
static TracksViewModeEnumSetting viewModeSetting{
|
||||
wxT("/GUI/DefaultViewModeChoice"),
|
||||
choicesView, nChoicesView, defaultChoiceView,
|
||||
{
|
||||
{ XO("Waveform") },
|
||||
{ wxT("WaveformDB"), XO("Waveform (dB)") },
|
||||
{ XO("Spectrogram") }
|
||||
},
|
||||
0, // Waveform
|
||||
|
||||
intChoicesView,
|
||||
// for migrating old preferences:
|
||||
{
|
||||
WaveTrackViewConstants::Waveform,
|
||||
WaveTrackViewConstants::obsoleteWaveformDBDisplay,
|
||||
WaveTrackViewConstants::Spectrum
|
||||
},
|
||||
wxT("/GUI/DefaultViewModeNew")
|
||||
};
|
||||
|
||||
@ -127,25 +124,19 @@ WaveTrackViewConstants::Display TracksPrefs::ViewModeChoice()
|
||||
}
|
||||
|
||||
//////////
|
||||
static const EnumValueSymbol choicesSampleDisplay[] = {
|
||||
{ wxT("ConnectDots"), XO("Connect dots") },
|
||||
{ wxT("StemPlot"), XO("Stem plot") }
|
||||
};
|
||||
static const size_t nChoicesSampleDisplay = WXSIZEOF( choicesSampleDisplay );
|
||||
static const int intChoicesSampleDisplay[] = {
|
||||
(int) WaveTrackViewConstants::LinearInterpolate,
|
||||
(int) WaveTrackViewConstants::StemPlot
|
||||
};
|
||||
static_assert(
|
||||
nChoicesSampleDisplay == WXSIZEOF(intChoicesSampleDisplay), "size mismatch" );
|
||||
|
||||
static const size_t defaultChoiceSampleDisplay = 1;
|
||||
|
||||
static EnumSetting sampleDisplaySetting{
|
||||
wxT("/GUI/SampleViewChoice"),
|
||||
choicesSampleDisplay, nChoicesSampleDisplay, defaultChoiceSampleDisplay,
|
||||
{
|
||||
{ wxT("ConnectDots"), XO("Connect dots") },
|
||||
{ wxT("StemPlot"), XO("Stem plot") }
|
||||
},
|
||||
1, // StemPlot
|
||||
|
||||
intChoicesSampleDisplay,
|
||||
// for migrating old preferences:
|
||||
{
|
||||
WaveTrackViewConstants::LinearInterpolate,
|
||||
WaveTrackViewConstants::StemPlot
|
||||
},
|
||||
wxT("/GUI/SampleView")
|
||||
};
|
||||
|
||||
@ -155,7 +146,7 @@ WaveTrackViewConstants::SampleDisplay TracksPrefs::SampleViewChoice()
|
||||
}
|
||||
|
||||
//////////
|
||||
static const EnumValueSymbol choicesZoom[] = {
|
||||
static const std::initializer_list<EnumValueSymbol> choicesZoom{
|
||||
{ wxT("FitToWidth"), XO("Fit to Width") },
|
||||
{ wxT("ZoomToSelection"), XO("Zoom to Selection") },
|
||||
{ wxT("ZoomDefault"), XO("Zoom Default") },
|
||||
@ -172,8 +163,7 @@ static const EnumValueSymbol choicesZoom[] = {
|
||||
{ wxT("FourPixelsPerSample"), XO("4 Pixels per Sample") },
|
||||
{ wxT("MaxZoom"), XO("Max Zoom") },
|
||||
};
|
||||
static const size_t nChoicesZoom = WXSIZEOF( choicesZoom );
|
||||
static const int intChoicesZoom[] = {
|
||||
static const std::initializer_list<int> intChoicesZoom{
|
||||
WaveTrackViewConstants::kZoomToFit,
|
||||
WaveTrackViewConstants::kZoomToSelection,
|
||||
WaveTrackViewConstants::kZoomDefault,
|
||||
@ -190,24 +180,23 @@ static const int intChoicesZoom[] = {
|
||||
WaveTrackViewConstants::kZoom4To1,
|
||||
WaveTrackViewConstants::kMaxZoom,
|
||||
};
|
||||
static_assert( nChoicesZoom == WXSIZEOF(intChoicesZoom), "size mismatch" );
|
||||
|
||||
static const size_t defaultChoiceZoom1 = 2; // kZoomDefault
|
||||
|
||||
static EnumSetting zoom1Setting{
|
||||
wxT("/GUI/ZoomPreset1Choice"),
|
||||
choicesZoom, nChoicesZoom, defaultChoiceZoom1,
|
||||
choicesZoom,
|
||||
2, // kZoomDefault
|
||||
|
||||
// for migrating old preferences:
|
||||
intChoicesZoom,
|
||||
wxT("/GUI/ZoomPreset1")
|
||||
};
|
||||
|
||||
static const size_t defaultChoiceZoom2 = 13; // kZoom4To1
|
||||
|
||||
static EnumSetting zoom2Setting{
|
||||
wxT("/GUI/ZoomPreset2Choice"),
|
||||
choicesZoom, nChoicesZoom, defaultChoiceZoom2,
|
||||
choicesZoom,
|
||||
13, // kZoom4To1
|
||||
|
||||
// for migrating old preferences:
|
||||
intChoicesZoom,
|
||||
wxT("/GUI/ZoomPreset2")
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user