Audacity 3.2.0
ProjectTempoListener.cpp
Go to the documentation of this file.
1#include "ClientData.h"
2#include "Observer.h"
3#include "Project.h"
5#include "Track.h"
6
7#include <cassert>
8
9#include "ViewInfo.h"
10
12{
13public:
15 void OnProjectTempoChange(double newTempo);
16
17private:
20 double mTempo { 0 };
24};
25
28 return std::make_shared<ProjectTempoListener>(
30 }
31};
32
35 : mProject { project }
36 , mViewInfo { ViewInfo::Get(project) }
37 , mTempo { ProjectTimeSignature::Get(project).GetTempo() }
38 , mTrackList { trackList }
39 , mTrackListSubstription { trackList.Subscribe(
40 [this](const TrackListEvent& event) {
41 if (event.mType == TrackListEvent::ADDITION)
42 {
43 const auto tempo = ProjectTimeSignature::Get(mProject).GetTempo();
44 if (const auto track = event.mpTrack.lock()) {
45 // TODO wide wave tracks: just call on the track itself
46 if (auto pLeader = *mTrackList.Find(track.get()))
47 pLeader->OnProjectTempoChange(tempo);
48 }
49 }
50 }) }
51{
52 mProjectTimeSignatureSubscription =
54 [this](const TimeSignatureChangedMessage& event) {
55 OnProjectTempoChange(event.newTempo);
56 });
57 assert(mTrackList.empty()); // No need to call `OnProjectTempoChange` yet ...
58}
59
61{
62 for (auto track : mTrackList)
63 track->OnProjectTempoChange(newTempo);
64
65 if(!mViewInfo.playRegion.Empty() && mTempo > 0 && newTempo > 0)
66 {
67 const auto tempoRate = mTempo / newTempo;
69 mViewInfo.playRegion.GetStart() * tempoRate,
70 mViewInfo.playRegion.GetEnd() * tempoRate
71 );
72 }
73 mTempo = newTempo;
74}
Utility ClientData::Site to register hooks into a host class that attach client data.
static const AttachedProjectObjects::RegisteredFactory key
const auto project
declares abstract base class Track, TrackList, and iterators over TrackList
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
Client code makes static instance from a factory of attachments; passes it to Get or Find as a retrie...
Definition: ClientData.h:274
Subscription Subscribe(Callback callback)
Connect a callback to the Publisher; later-connected are called earlier.
Definition: Observer.h:199
A move-only handle representing a connection to a Publisher.
Definition: Observer.h:70
double GetStart() const
Definition: ViewInfo.h:128
double GetEnd() const
Definition: ViewInfo.h:135
bool Empty() const
Definition: ViewInfo.h:127
void SetTimes(double start, double end)
Definition: ViewInfo.cpp:181
ProjectTempoListener(AudacityProject &project, TrackList &trackList)
Observer::Subscription mProjectTimeSignatureSubscription
void OnProjectTempoChange(double newTempo)
AudacityProject & mProject
Observer::Subscription mTrackListSubstription
static ProjectTimeSignature & Get(AudacityProject &project)
A flat linked list of tracks supporting Add, Remove, Clear, and Contains, serialization of the list o...
Definition: Track.h:975
TrackIter< Track > Find(Track *pTrack)
Definition: Track.cpp:519
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:347
PlayRegion playRegion
Definition: ViewInfo.h:216
Services * Get()
Fetch the global instance, or nullptr if none is yet installed.
Definition: BasicUI.cpp:196
A convenient default parameter for class template Site.
Definition: ClientData.h:28
Notification of changes in individual tracks of TrackList, or of TrackList's composition.
Definition: Track.h:928
@ ADDITION
Posted when a track has been added to a tracklist. Also posted when one track replaces another.
Definition: Track.h:944