Audacity 3.2.0
NoteTrack.h
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 NoteTrack.h
6
7 Dominic Mazzoni
8
9**********************************************************************/
10
11#ifndef __AUDACITY_NOTETRACK__
12#define __AUDACITY_NOTETRACK__
13
14#include <utility>
15#include "AudioIOSequences.h"
16#include "CRTPBase.h"
17#include "Prefs.h"
18#include "PlayableTrack.h"
19
20#if defined(USE_MIDI)
21
22// define this switch to play MIDI during redisplay to sonify run times
23// Note that if SONIFY is defined, the default MIDI device will be opened
24// and may block normal MIDI playback.
25//#define SONIFY 1
26
27#ifdef SONIFY
28
29#define SONFNS(name) \
30 void Begin ## name(); \
31 void End ## name();
32
33SONFNS(NoteBackground)
34SONFNS(NoteForeground)
35SONFNS(Measures)
36SONFNS(Serialize)
37SONFNS(Unserialize)
38SONFNS(ModifyState)
39SONFNS(AutoSave)
40
41#undef SONFNS
42
43#endif
44
45class wxDC;
46class wxRect;
47
48class Alg_seq; // from "allegro.h"
49
50using QuantizedTimeAndBeat = std::pair< double, double >;
51
52class NoteTrack;
53class StretchHandle;
54class TimeWarper;
55
56struct NOTE_TRACK_API NoteTrackAttachment;
57CRTP_BASE(NoteTrackAttachmentBase, struct,
59struct NoteTrackAttachment : NoteTrackAttachmentBase
60{
63 virtual void WriteXML(XMLWriter &xmlFile) const;
65 virtual bool HandleAttribute(const Attribute &attribute);
66};
67
72>;
73
74class NOTE_TRACK_API NoteTrack final
75 : public UniqueChannelTrack<PlayableTrack>
78{
79public:
82
83 // Construct and also build all attachments
84 static NoteTrack *New(AudacityProject &project);
85
86 NoteTrack();
88 NoteTrack(const NoteTrack &orig) = delete;
89 NoteTrack(const NoteTrack &orig, ProtectedCreationArg &&) = delete;
90 virtual ~NoteTrack();
91
92 using Holder = std::shared_ptr<NoteTrack>;
93
94private:
95 Track::Holder Clone(bool backup) const override;
96
97public:
98 void MoveTo(double origin) override { mOrigin = origin; }
99
100 Alg_seq &GetSeq() const;
101
102 void WarpAndTransposeNotes(double t0, double t1,
103 const TimeWarper &warper, double semitones);
104
105 void SetSequence(std::unique_ptr<Alg_seq> &&seq);
106 void PrintSequence();
107
108 Alg_seq *MakeExportableSeq(std::unique_ptr<Alg_seq> &cleanup) const;
109 bool ExportMIDI(const wxString &f) const;
110 bool ExportAllegro(const wxString &f) const;
111
112 // High-level editing
113 Track::Holder Cut(double t0, double t1) override;
114 Track::Holder Copy(double t0, double t1, bool forClipboard = true)
115 const override;
116 bool Trim (double t0, double t1) /* not override */;
117 void Clear(double t0, double t1) override;
118 void Paste(double t, const Track &src) override;
119 void
120 Silence(double t0, double t1, ProgressReporter reportProgress = {}) override;
121 void InsertSilence(double t, double len) override;
122 bool Shift(double t) /* not override */;
123
124 float GetVelocity() const {
125 return mVelocity.load(std::memory_order_relaxed); }
126 void SetVelocity(float velocity);
127
128 QuantizedTimeAndBeat NearestBeatTime( double time ) const;
129 bool StretchRegion
130 ( QuantizedTimeAndBeat t0, QuantizedTimeAndBeat t1, double newDur );
131
132 bool HandleXMLTag(const std::string_view& tag, const AttributesList& attrs) override;
133 XMLTagHandler *HandleXMLChild(const std::string_view& tag) override;
134 void WriteXML(XMLWriter &xmlFile) const override;
135
136 // channels are numbered as integers 0-15, visible channels
137 // (mVisibleChannels) is a bit set. Channels are displayed as
138 // integers 1-16.
139
140 // Allegro's data structure does not restrict channels to 16.
141 // Since there is not way to select more than 16 channels,
142 // map all channel numbers mod 16. This will have no effect
143 // on MIDI files, but it will allow users to at least select
144 // all channels on non-MIDI event sequence data.
145#define NUM_CHANNELS 16
146 // Bitmask with all NUM_CHANNELS bits set
147#define ALL_CHANNELS (1 << NUM_CHANNELS) - 1
148#define CHANNEL_BIT(c) (1 << (c % NUM_CHANNELS))
149 unsigned GetVisibleChannels() const {
150 return mVisibleChannels.load(std::memory_order_relaxed);
151 }
152 void SetVisibleChannels(unsigned value) {
153 mVisibleChannels.store(value, std::memory_order_relaxed);
154 }
155 bool IsVisibleChan(int c) const {
156 return (GetVisibleChannels() & CHANNEL_BIT(c)) != 0;
157 }
158 void SetVisibleChan(int c) {
159 mVisibleChannels.fetch_or(CHANNEL_BIT(c), std::memory_order_relaxed); }
160 void ClearVisibleChan(int c) {
161 mVisibleChannels.fetch_and(~CHANNEL_BIT(c), std::memory_order_relaxed); }
162 void ToggleVisibleChan(int c) {
163 mVisibleChannels.fetch_xor(CHANNEL_BIT(c), std::memory_order_relaxed); }
164 // Solos the given channel. If it's the only channel visible, all channels
165 // are enabled; otherwise, it is set to the only visible channel.
166 void SoloVisibleChan(int c) {
167 auto visibleChannels = 0u;
168 if (GetVisibleChannels() == CHANNEL_BIT(c))
169 visibleChannels = ALL_CHANNELS;
170 else
171 visibleChannels = CHANNEL_BIT(c);
172 mVisibleChannels.store(visibleChannels, std::memory_order_relaxed);
173 }
174
175 const TypeInfo &GetTypeInfo() const override;
176 static const TypeInfo &ClassTypeInfo();
177
179 const override;
180
181 size_t NIntervals() const override;
182
184 explicit Interval(const NoteTrack &track);
185 ~Interval() override;
186 std::shared_ptr<ChannelInterval> DoGetChannel(size_t iChannel) override;
187 double Start() const override;
188 double End() const override;
189 size_t NChannels() const override;
190 private:
192 const std::shared_ptr<const NoteTrack> mpTrack;
193 };
194
195private:
196 std::shared_ptr<WideChannelGroupInterval> DoGetInterval(size_t iInterval)
197 override;
198
199 void DoSetVelocity(float velocity);
200
201 void AddToDuration( double delta );
202
203 // These are mutable to allow NoteTrack to switch details of representation
204 // in logically const methods
205 // At most one of the two pointers is not null at any time.
206 // Both are null in a newly constructed NoteTrack.
207 mutable std::unique_ptr<Alg_seq> mSeq;
208 mutable std::unique_ptr<char[]> mSerializationBuffer;
210
212 std::atomic<float> mVelocity{ 0.0f }; // velocity offset
213
215 std::atomic<unsigned> mVisibleChannels{ ALL_CHANNELS };
216 double mOrigin{ 0.0 };
217};
218
219extern NOTE_TRACK_API StringSetting MIDIPlaybackDevice;
220extern NOTE_TRACK_API StringSetting MIDIRecordingDevice;
221extern NOTE_TRACK_API IntSetting MIDISynthLatency_ms;
222
224
225#endif // USE_MIDI
226
227#ifndef SONIFY
228// no-ops:
229#define SonifyBeginSonification()
230#define SonifyEndSonification()
231#define SonifyBeginNoteBackground()
232#define SonifyEndNoteBackground()
233#define SonifyBeginNoteForeground()
234#define SonifyEndNoteForeground()
235#define SonifyBeginMeasures()
236#define SonifyEndMeasures()
237#define SonifyBeginSerialize()
238#define SonifyEndSerialize()
239#define SonifyBeginUnserialize()
240#define SonifyEndUnserialize()
241#define SonifyBeginAutoSave()
242#define SonifyEndAutoSave()
243#define SonifyBeginModifyState()
244#define SonifyEndModifyState()
245#endif
246
247
248NOTE_TRACK_API wxString GetMIDIDeviceInfo();
249
250#endif
ClientData::Site< NoteTrack, NoteTrackAttachment, ClientData::DeepCopying > NoteTrackAttachments
Definition: NoteTrack.h:72
NOTE_TRACK_API wxString GetMIDIDeviceInfo()
Definition: NoteTrack.cpp:884
#define CHANNEL_BIT(c)
Definition: NoteTrack.h:148
CRTP_BASE(NoteTrackAttachmentBase, struct, ClientData::Cloneable< NoteTrackAttachment, ClientData::UniquePtr >)
NOTE_TRACK_API StringSetting MIDIPlaybackDevice
Definition: NoteTrack.cpp:983
ENUMERATE_TRACK_TYPE(NoteTrack)
NOTE_TRACK_API IntSetting MIDISynthLatency_ms
Definition: NoteTrack.cpp:985
NOTE_TRACK_API StringSetting MIDIRecordingDevice
Definition: NoteTrack.cpp:984
#define ALL_CHANNELS
Definition: NoteTrack.h:147
struct NOTE_TRACK_API NoteTrackAttachment
Definition: NoteTrack.h:56
std::pair< double, double > QuantizedTimeAndBeat
Definition: NoteTrack.h:50
Extends Track with notions of mute and solo setting.
const auto project
std::function< void(double)> ProgressReporter
Definition: Track.h:48
std::pair< std::string_view, XMLAttributeValueView > Attribute
Definition: XMLTagHandler.h:39
std::vector< Attribute > AttributesList
Definition: XMLTagHandler.h:40
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
virtual std::shared_ptr< Interval > DoGetInterval(size_t iInterval)=0
Retrieve an interval.
size_t NIntervals() const
Report the number of intervals.
Definition: Channel.h:537
Utility to register hooks into a host class that attach client data.
Definition: ClientData.h:229
Specialization of Setting for int.
Definition: Prefs.h:356
A Track that is used for Midi notes. (Somewhat old code).
Definition: NoteTrack.h:78
std::unique_ptr< char[]> mSerializationBuffer
Definition: NoteTrack.h:208
bool IsVisibleChan(int c) const
Definition: NoteTrack.h:155
void MoveTo(double origin) override
Change start time to given time point.
Definition: NoteTrack.h:98
void SetVisibleChannels(unsigned value)
Definition: NoteTrack.h:152
float GetVelocity() const
Definition: NoteTrack.h:124
std::unique_ptr< Alg_seq > mSeq
Definition: NoteTrack.h:207
std::shared_ptr< NoteTrack > Holder
Definition: NoteTrack.h:92
static EnumSetting< bool > AllegroStyleSetting
Definition: NoteTrack.h:81
unsigned GetVisibleChannels() const
Definition: NoteTrack.h:149
void ToggleVisibleChan(int c)
Definition: NoteTrack.h:162
void SoloVisibleChan(int c)
Definition: NoteTrack.h:166
void ClearVisibleChan(int c)
Definition: NoteTrack.h:160
long mSerializationLength
Definition: NoteTrack.h:209
NoteTrack(const NoteTrack &orig, ProtectedCreationArg &&)=delete
NoteTrack(const NoteTrack &orig)=delete
Copy construction hasn't been necessary yet.
void SetVisibleChan(int c)
Definition: NoteTrack.h:158
This is defined just to enable dynamic_cast on it.
Specialization of Setting for strings.
Definition: Prefs.h:370
Transforms one point in time to another point. For example, a time stretching effect might use one to...
Definition: TimeWarper.h:62
Abstract base class for an object holding data associated with points on a time axis.
Definition: Track.h:110
virtual Holder Clone(bool backup) const =0
virtual void Paste(double t, const Track &src)=0
Weak precondition allows overrides to replicate one channel into many.
virtual Holder PasteInto(AudacityProject &project, TrackList &list) const =0
virtual Holder Copy(double t0, double t1, bool forClipboard=true) const =0
Create new tracks and don't modify this track.
virtual const TypeInfo & GetTypeInfo() const =0
virtual void InsertSilence(double t, double len)=0
static const TypeInfo & ClassTypeInfo()
Definition: Track.cpp:790
virtual void Clear(double t0, double t1)=0
std::shared_ptr< Track > Holder
Definition: Track.h:202
virtual void WriteXML(XMLWriter &xmlFile) const =0
virtual void Silence(double t0, double t1, ProgressReporter reportProgress={})=0
virtual Holder Cut(double t0, double t1)=0
Create tracks and modify this track.
A flat linked list of tracks supporting Add, Remove, Clear, and Contains, serialization of the list o...
Definition: Track.h:850
Generates overrides of channel-related functions.
Definition: Track.h:450
This class is an interface which should be implemented by classes which wish to be able to load and s...
Definition: XMLTagHandler.h:42
virtual XMLTagHandler * HandleXMLChild(const std::string_view &tag)=0
virtual bool HandleXMLTag(const std::string_view &tag, const AttributesList &attrs)=0
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:25
@ DeepCopying
point to new sub-objects; these must define a Clone() member; won't compile for std::weak_ptr
std::string Serialize(const ProjectForm &form)
A convenient base class defining abstract virtual Clone() for a given kind of pointer.
Definition: ClientData.h:50
~Interval() override
const std::shared_ptr< const NoteTrack > mpTrack
Definition: NoteTrack.h:192
virtual void WriteXML(XMLWriter &xmlFile) const
Default implementation does nothing.
Definition: NoteTrack.cpp:124
virtual bool HandleAttribute(const Attribute &attribute)
Return whether the attribute was used; default returns false.
Definition: NoteTrack.cpp:127
~NoteTrackAttachment() override
Empty argument passed to some public constructors.
Definition: Track.h:117