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 void ShiftBy(double t0, double delta) override;
100
101 Alg_seq &GetSeq() const;
102
103 void WarpAndTransposeNotes(double t0, double t1,
104 const TimeWarper &warper, double semitones);
105
106 void SetSequence(std::unique_ptr<Alg_seq> &&seq);
107 void PrintSequence();
108
109 Alg_seq *MakeExportableSeq(std::unique_ptr<Alg_seq> &cleanup) const;
110 bool ExportMIDI(const wxString &f) const;
111 bool ExportAllegro(const wxString &f) const;
112
113 // High-level editing
114 Track::Holder Cut(double t0, double t1) override;
115 Track::Holder Copy(double t0, double t1, bool forClipboard = true)
116 const override;
117 bool Trim (double t0, double t1) /* not override */;
118 void Clear(double t0, double t1) override;
119 void Paste(double t, const Track &src) override;
120 void
121 Silence(double t0, double t1, ProgressReporter reportProgress = {}) override;
122 void InsertSilence(double t, double len) override;
123 bool Shift(double t) /* not override */;
124
125 float GetVelocity() const {
126 return mVelocity.load(std::memory_order_relaxed); }
127 void SetVelocity(float velocity);
128
129 QuantizedTimeAndBeat NearestBeatTime( double time ) const;
130 bool StretchRegion
131 ( QuantizedTimeAndBeat t0, QuantizedTimeAndBeat t1, double newDur );
132
133 bool HandleXMLTag(const std::string_view& tag, const AttributesList& attrs) override;
134 XMLTagHandler *HandleXMLChild(const std::string_view& tag) override;
135 void WriteXML(XMLWriter &xmlFile) const override;
136
137 // channels are numbered as integers 0-15, visible channels
138 // (mVisibleChannels) is a bit set. Channels are displayed as
139 // integers 1-16.
140
141 // Allegro's data structure does not restrict channels to 16.
142 // Since there is not way to select more than 16 channels,
143 // map all channel numbers mod 16. This will have no effect
144 // on MIDI files, but it will allow users to at least select
145 // all channels on non-MIDI event sequence data.
146#define NUM_CHANNELS 16
147 // Bitmask with all NUM_CHANNELS bits set
148#define ALL_CHANNELS (1 << NUM_CHANNELS) - 1
149#define CHANNEL_BIT(c) (1 << (c % NUM_CHANNELS))
150 unsigned GetVisibleChannels() const {
151 return mVisibleChannels.load(std::memory_order_relaxed);
152 }
153 void SetVisibleChannels(unsigned value) {
154 mVisibleChannels.store(value, std::memory_order_relaxed);
155 }
156 bool IsVisibleChan(int c) const {
157 return (GetVisibleChannels() & CHANNEL_BIT(c)) != 0;
158 }
159 void SetVisibleChan(int c) {
160 mVisibleChannels.fetch_or(CHANNEL_BIT(c), std::memory_order_relaxed); }
161 void ClearVisibleChan(int c) {
162 mVisibleChannels.fetch_and(~CHANNEL_BIT(c), std::memory_order_relaxed); }
163 void ToggleVisibleChan(int c) {
164 mVisibleChannels.fetch_xor(CHANNEL_BIT(c), std::memory_order_relaxed); }
165 // Solos the given channel. If it's the only channel visible, all channels
166 // are enabled; otherwise, it is set to the only visible channel.
167 void SoloVisibleChan(int c) {
168 auto visibleChannels = 0u;
169 if (GetVisibleChannels() == CHANNEL_BIT(c))
170 visibleChannels = ALL_CHANNELS;
171 else
172 visibleChannels = CHANNEL_BIT(c);
173 mVisibleChannels.store(visibleChannels, std::memory_order_relaxed);
174 }
175
176 const TypeInfo &GetTypeInfo() const override;
177 static const TypeInfo &ClassTypeInfo();
178
180 const override;
181
182 size_t NIntervals() const override;
183
185 explicit Interval(const NoteTrack &track);
186 ~Interval() override;
187 std::shared_ptr<ChannelInterval> DoGetChannel(size_t iChannel) override;
188 double Start() const override;
189 double End() const override;
190 size_t NChannels() const override;
191 private:
193 const std::shared_ptr<const NoteTrack> mpTrack;
194 };
195
196private:
197 std::shared_ptr<WideChannelGroupInterval> DoGetInterval(size_t iInterval)
198 override;
199
200 void DoSetVelocity(float velocity);
201
202 void AddToDuration( double delta );
203
204 // These are mutable to allow NoteTrack to switch details of representation
205 // in logically const methods
206 // At most one of the two pointers is not null at any time.
207 // Both are null in a newly constructed NoteTrack.
208 mutable std::unique_ptr<Alg_seq> mSeq;
209 mutable std::unique_ptr<char[]> mSerializationBuffer;
211
213 std::atomic<float> mVelocity{ 0.0f }; // velocity offset
214
216 std::atomic<unsigned> mVisibleChannels{ ALL_CHANNELS };
217 double mOrigin{ 0.0 };
218};
219
220extern NOTE_TRACK_API StringSetting MIDIPlaybackDevice;
221extern NOTE_TRACK_API StringSetting MIDIRecordingDevice;
222extern NOTE_TRACK_API IntSetting MIDISynthLatency_ms;
223
225
226#endif // USE_MIDI
227
228#ifndef SONIFY
229// no-ops:
230#define SonifyBeginSonification()
231#define SonifyEndSonification()
232#define SonifyBeginNoteBackground()
233#define SonifyEndNoteBackground()
234#define SonifyBeginNoteForeground()
235#define SonifyEndNoteForeground()
236#define SonifyBeginMeasures()
237#define SonifyEndMeasures()
238#define SonifyBeginSerialize()
239#define SonifyEndSerialize()
240#define SonifyBeginUnserialize()
241#define SonifyEndUnserialize()
242#define SonifyBeginAutoSave()
243#define SonifyEndAutoSave()
244#define SonifyBeginModifyState()
245#define SonifyEndModifyState()
246#endif
247
248
249NOTE_TRACK_API wxString GetMIDIDeviceInfo();
250
251#endif
ClientData::Site< NoteTrack, NoteTrackAttachment, ClientData::DeepCopying > NoteTrackAttachments
Definition: NoteTrack.h:72
NOTE_TRACK_API wxString GetMIDIDeviceInfo()
Definition: NoteTrack.cpp:890
#define CHANNEL_BIT(c)
Definition: NoteTrack.h:149
CRTP_BASE(NoteTrackAttachmentBase, struct, ClientData::Cloneable< NoteTrackAttachment, ClientData::UniquePtr >)
NOTE_TRACK_API StringSetting MIDIPlaybackDevice
Definition: NoteTrack.cpp:989
ENUMERATE_TRACK_TYPE(NoteTrack)
NOTE_TRACK_API IntSetting MIDISynthLatency_ms
Definition: NoteTrack.cpp:991
NOTE_TRACK_API StringSetting MIDIRecordingDevice
Definition: NoteTrack.cpp:990
#define ALL_CHANNELS
Definition: NoteTrack.h:148
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
void ShiftBy(double t)
Change start time by given duration.
Definition: Channel.h:296
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:540
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:209
bool IsVisibleChan(int c) const
Definition: NoteTrack.h:156
void MoveTo(double origin) override
Change start time to given time point.
Definition: NoteTrack.h:98
void SetVisibleChannels(unsigned value)
Definition: NoteTrack.h:153
float GetVelocity() const
Definition: NoteTrack.h:125
std::unique_ptr< Alg_seq > mSeq
Definition: NoteTrack.h:208
std::shared_ptr< NoteTrack > Holder
Definition: NoteTrack.h:92
static EnumSetting< bool > AllegroStyleSetting
Definition: NoteTrack.h:81
unsigned GetVisibleChannels() const
Definition: NoteTrack.h:150
void ToggleVisibleChan(int c)
Definition: NoteTrack.h:163
void SoloVisibleChan(int c)
Definition: NoteTrack.h:167
void ClearVisibleChan(int c)
Definition: NoteTrack.h:161
long mSerializationLength
Definition: NoteTrack.h:210
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:159
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:193
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