Audacity  3.0.3
ProjectRate.cpp
Go to the documentation of this file.
1 /*!********************************************************************
2 
3 Audacity: A Digital Audio Editor
4 
5 @file ProjectRate.cpp
6 
7 Paul Licameli split from ProjectSettings.cpp
8 
9 **********************************************************************/
10 
11 #include "ProjectRate.h"
12 
13 #include "AudioIOBase.h"
14 #include "Prefs.h"
15 #include "Project.h"
16 #include "QualitySettings.h"
17 #include "XMLWriter.h"
18 
19 wxDEFINE_EVENT(EVT_PROJECT_RATE_CHANGE, wxEvent);
20 
21 namespace {
22  struct MyEvent : wxEvent {
23  MyEvent() : wxEvent{ 0, EVT_PROJECT_RATE_CHANGE } {}
24  wxEvent *Clone() const override { return new MyEvent{*this}; }
25  };
26 
27  void Notify( AudacityProject &project )
28  {
29  MyEvent e;
30  project.ProcessEvent( e );
31  }
32 }
33 
36  []( AudacityProject &project ){
37  auto result = std::make_shared< ProjectRate >(project);
38  return result;
39  }
40 };
41 
43 {
44  return project.AttachedObjects::Get< ProjectRate >( sKey );
45 }
46 
48 {
49  return Get( const_cast< AudacityProject & >( project ) );
50 }
51 
53  : mProject{ project }
54 {
55  int intRate = 0;
56  bool wasDefined = QualitySettings::DefaultSampleRate.Read( &intRate );
57  mRate = intRate;
58  if ( !wasDefined ) {
59  // The default given above can vary with host/devices. So unless there is
60  // an entry for the default sample rate in audacity.cfg, Audacity can open
61  // with a rate which is different from the rate with which it closed.
62  // See bug 1879.
64  gPrefs->Flush();
65  }
66 }
67 
68 double ProjectRate::GetRate() const
69 {
70  return mRate;
71 }
72 
73 void ProjectRate::SetRate(double rate)
74 {
75  if (rate != mRate) {
76  mRate = rate;
78  }
79 }
80 
82 [](const AudacityProject &project, XMLWriter &xmlFile){
83  xmlFile.WriteAttr(wxT("rate"), ProjectRate::Get(project).GetRate());
84 }
85 };
86 
88 // Just a pointer to function, but needing overload resolution as non-const:
90  { L"rate", [](auto &settings, auto value){
91  double rate;
92  Internat::CompatibleToDouble(value, &rate);
93  settings.SetRate( rate );
94  } },
95 } };
XMLWriter
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:23
entries
static ProjectFileIORegistry::AttributeReaderEntries entries
Definition: ProjectRate.cpp:87
AudioIOBase.h
ProjectRate::ProjectRate
ProjectRate(AudacityProject &project)
Definition: ProjectRate.cpp:52
XMLWriter.h
gPrefs
FileConfig * gPrefs
Definition: Prefs.cpp:70
anonymous_namespace{ProjectRate.cpp}::Notify
void Notify(AudacityProject &project)
Definition: ProjectRate.cpp:27
Project.h
ProjectRate::Get
static ProjectRate & Get(AudacityProject &project)
Definition: ProjectRate.cpp:42
Setting::Write
bool Write(const T &value)
Write value to config and return true if successful.
Definition: Prefs.h:172
ProjectRate::mRate
double mRate
Definition: ProjectRate.h:41
ProjectRate::SetRate
void SetRate(double rate)
Definition: ProjectRate.cpp:73
ClientData::Site::RegisteredFactory
Client code makes static instance from a factory of attachments; passes it to Get or Find as a retrie...
Definition: ClientData.h:266
sKey
static const AudacityProject::AttachedObjects::RegisteredFactory sKey
Definition: ProjectRate.cpp:35
XMLMethodRegistry::WriterEntry
Typically statically constructed.
Definition: XMLMethodRegistry.h:150
Setting::Read
bool Read(T *pVar) const
overload of Read returning a boolean that is true if the value was previously defined *‍/
Definition: Prefs.h:128
ProjectRate
Holds project sample rate.
Definition: ProjectRate.h:27
anonymous_namespace{ProjectRate.cpp}::MyEvent::Clone
wxEvent * Clone() const override
Definition: ProjectRate.cpp:24
anonymous_namespace{ProjectRate.cpp}::MyEvent
Definition: ProjectRate.cpp:22
QualitySettings::DefaultSampleRate
PROJECT_RATE_API IntSetting DefaultSampleRate
Definition: QualitySettings.cpp:15
ProjectRate::GetRate
double GetRate() const
Definition: ProjectRate.cpp:68
entry
static ProjectFileIORegistry::WriterEntry entry
Definition: ProjectRate.cpp:81
ProjectRate::mProject
AudacityProject & mProject
Definition: ProjectRate.h:40
FileConfig::Flush
virtual bool Flush(bool bCurrentOnly=false) wxOVERRIDE
Definition: FileConfig.cpp:143
QualitySettings.h
AudacityProject
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:92
ProjectRate.h
an object holding per-project preferred sample rate
wxDEFINE_EVENT
wxDEFINE_EVENT(EVT_PROJECT_RATE_CHANGE, wxEvent)
Prefs.h
settings
static Settings & settings()
Definition: TrackInfo.cpp:86
XMLMethodRegistry::AttributeReaderEntries
Definition: XMLMethodRegistry.h:116
anonymous_namespace{ProjectRate.cpp}::MyEvent::MyEvent
MyEvent()
Definition: ProjectRate.cpp:23
Internat::CompatibleToDouble
static bool CompatibleToDouble(const wxString &stringToConvert, double *result)
Convert a string to a number.
Definition: Internat.cpp:134