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 "TempoChange.h"
6#include "Track.h"
7
8#include <cassert>
9
10#include "ViewInfo.h"
11
13{
14public:
16 void OnProjectTempoChange(double newTempo);
17
18private:
21 double mTempo { 0 };
25};
26
29 return std::make_shared<ProjectTempoListener>(
31 }
32};
33
36 : mProject { project }
37 , mViewInfo { ViewInfo::Get(project) }
38 , mTempo { ProjectTimeSignature::Get(project).GetTempo() }
39 , mTrackList { trackList }
40 , mTrackListSubstription { trackList.Subscribe(
41 [this](const TrackListEvent& event) {
42 if (event.mType == TrackListEvent::ADDITION)
43 {
44 const auto tempo = ProjectTimeSignature::Get(mProject).GetTempo();
45 if (const auto track = event.mpTrack.lock())
46 DoProjectTempoChange(*track, tempo);
47 }
48 }) }
49{
50 mProjectTimeSignatureSubscription =
52 [this](const TimeSignatureChangedMessage& event) {
54 });
55 assert(mTrackList.empty()); // No need to call `OnProjectTempoChange` yet ...
56}
57
59{
60 for (auto track : mTrackList)
61 DoProjectTempoChange(*track, newTempo);
62
63 if(!mViewInfo.playRegion.Empty() && mTempo > 0 && newTempo > 0)
64 {
65 const auto tempoRate = mTempo / newTempo;
67 mViewInfo.playRegion.GetStart() * tempoRate,
68 mViewInfo.playRegion.GetEnd() * tempoRate
69 );
70 }
71 mTempo = newTempo;
72}
Utility ClientData::Site to register hooks into a host class that attach client data.
static const AttachedProjectObjects::RegisteredFactory key
void DoProjectTempoChange(ChannelGroup &group, double newTempo)
Definition: TempoChange.cpp:41
AttachedVirtualFunction< struct OnProjectTempoChangeTag, void, ChannelGroup, const std::optional< double > &, double > OnProjectTempoChange
Method to set project tempo on a channel group, defaulting to no-op.
Definition: TempoChange.h:22
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:275
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:850
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:314
PlayRegion playRegion
Definition: ViewInfo.h:216
Services * Get()
Fetch the global instance, or nullptr if none is yet installed.
Definition: BasicUI.cpp:202
A convenient default parameter for class template Site.
Definition: ClientData.h:29
Notification of changes in individual tracks of TrackList, or of TrackList's composition.
Definition: Track.h:803
@ ADDITION
Posted when a track has been added to a tracklist. Also posted when one track replaces another.
Definition: Track.h:819