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 
15 
16 
17 
18 #include <utility>
19 #include "Prefs.h"
20 #include "Track.h"
21 
22 #if defined(USE_MIDI)
23 
24 // define this switch to play MIDI during redisplay to sonify run times
25 // Note that if SONIFY is defined, the default MIDI device will be opened
26 // and may block normal MIDI playback.
27 //#define SONIFY 1
28 
29 #ifdef SONIFY
30 
31 #define SONFNS(name) \
32  void Begin ## name(); \
33  void End ## name();
34 
35 SONFNS(NoteBackground)
36 SONFNS(NoteForeground)
37 SONFNS(Measures)
38 SONFNS(Serialize)
39 SONFNS(Unserialize)
40 SONFNS(ModifyState)
41 SONFNS(AutoSave)
42 
43 #undef SONFNS
44 
45 #endif
46 
47 class wxDC;
48 class wxRect;
49 
50 class Alg_seq; // from "allegro.h"
51 
53 #ifdef EXPERIMENTAL_MIDI_OUT
55 #else
57 #endif
58  ;
59 
60 using QuantizedTimeAndBeat = std::pair< double, double >;
61 
62 class StretchHandle;
63 class TimeWarper;
64 
65 class AUDACITY_DLL_API NoteTrack final
66  : public NoteTrackBase
67 {
68 public:
69  // Construct and also build all attachments
70  static NoteTrack *New(AudacityProject &project);
71 
72  NoteTrack();
73  virtual ~NoteTrack();
74 
75  using Holder = std::shared_ptr<NoteTrack>;
76 
77 private:
78  Track::Holder Clone() const override;
79 
80 public:
81  double GetOffset() const override;
82  double GetStartTime() const override;
83  double GetEndTime() const override;
84 
85  Alg_seq &GetSeq() const;
86 
87  void WarpAndTransposeNotes(double t0, double t1,
88  const TimeWarper &warper, double semitones);
89 
90  static void DrawLabelControls
91  ( const NoteTrack *pTrack, wxDC & dc, const wxRect &rect,
92  int highlightedChannel = -1 );
93  int FindChannel(const wxRect &rect, int mx, int my);
94  bool LabelClick(const wxRect &rect, int x, int y, bool right);
95 
96  void SetSequence(std::unique_ptr<Alg_seq> &&seq);
97  void PrintSequence();
98 
99  Alg_seq *MakeExportableSeq(std::unique_ptr<Alg_seq> &cleanup) const;
100  bool ExportMIDI(const wxString &f) const;
101  bool ExportAllegro(const wxString &f) const;
102 
103  // High-level editing
104  Track::Holder Cut (double t0, double t1) override;
105  Track::Holder Copy (double t0, double t1, bool forClipboard = true) const override;
106  bool Trim (double t0, double t1) /* not override */;
107  void Clear(double t0, double t1) override;
108  void Paste(double t, const Track *src) override;
109  void Silence(double t0, double t1) override;
110  void InsertSilence(double t, double len) override;
111  bool Shift(double t) /* not override */;
112 
113 #ifdef EXPERIMENTAL_MIDI_OUT
114  float GetVelocity() const { return mVelocity; }
115  void SetVelocity(float velocity);
116 #endif
117 
118  QuantizedTimeAndBeat NearestBeatTime( double time ) const;
119  bool StretchRegion
120  ( QuantizedTimeAndBeat t0, QuantizedTimeAndBeat t1, double newDur );
121 
123  int GetBottomNote() const { return mBottomNote; }
125  int GetTopNote() const { return mTopNote; }
127  void SetBottomNote(int note);
129  void SetTopNote(int note);
131  void SetNoteRange(int note1, int note2);
132 
134  void ZoomAllNotes();
136  void ZoomMaxExtent() { SetNoteRange(MinPitch, MaxPitch); }
138  void ShiftNoteRange(int offset);
139 
141  void ZoomOut(const wxRect &rect, int y) { Zoom(rect, y, 1.0f / ZoomStep, true); }
143  void ZoomIn(const wxRect &rect, int y) { Zoom(rect, y, ZoomStep, true); }
146  void Zoom(const wxRect &rect, int y, float multiplier, bool center);
147  void ZoomTo(const wxRect &rect, int start, int end);
148 
149 #if 0
150  // Vertical scrolling is performed by dragging the keyboard at
151  // left of track. Protocol is call StartVScroll, then update by
152  // calling VScroll with original and final mouse position.
153  // These functions are not used -- instead, zooming/dragging works like
154  // audio track zooming/dragging. The vertical scrolling is nice however,
155  // so I left these functions here for possible use in the future.
156  void StartVScroll();
157  void VScroll(int start, int end);
158 #endif
159 
160  bool HandleXMLTag(const std::string_view& tag, const AttributesList& attrs) override;
161  XMLTagHandler *HandleXMLChild(const std::string_view& tag) override;
162  void WriteXML(XMLWriter &xmlFile) const override;
163 
164  // channels are numbered as integers 0-15, visible channels
165  // (mVisibleChannels) is a bit set. Channels are displayed as
166  // integers 1-16.
167 
168  // Allegro's data structure does not restrict channels to 16.
169  // Since there is not way to select more than 16 channels,
170  // map all channel numbers mod 16. This will have no effect
171  // on MIDI files, but it will allow users to at least select
172  // all channels on non-MIDI event sequence data.
173 #define NUM_CHANNELS 16
174  // Bitmask with all NUM_CHANNELS bits set
175 #define ALL_CHANNELS (1 << NUM_CHANNELS) - 1
176 #define CHANNEL_BIT(c) (1 << (c % NUM_CHANNELS))
177  bool IsVisibleChan(int c) const {
178  return (mVisibleChannels & CHANNEL_BIT(c)) != 0;
179  }
180  void SetVisibleChan(int c) { mVisibleChannels |= CHANNEL_BIT(c); }
181  void ClearVisibleChan(int c) { mVisibleChannels &= ~CHANNEL_BIT(c); }
182  void ToggleVisibleChan(int c) { mVisibleChannels ^= CHANNEL_BIT(c); }
183  // Solos the given channel. If it's the only channel visible, all channels
184  // are enabled; otherwise, it is set to the only visible channel.
185  void SoloVisibleChan(int c) {
186  if (mVisibleChannels == CHANNEL_BIT(c))
187  mVisibleChannels = ALL_CHANNELS;
188  else
189  mVisibleChannels = CHANNEL_BIT(c);
190  }
191 
192  const TypeInfo &GetTypeInfo() const override;
193  static const TypeInfo &ClassTypeInfo();
194 
195  Track::Holder PasteInto( AudacityProject & ) const override;
196 
197  ConstIntervals GetIntervals() const override;
198  Intervals GetIntervals() override;
199 
200  private:
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;
210  mutable long mSerializationLength;
211 
212 #ifdef EXPERIMENTAL_MIDI_OUT
213  float mVelocity; // velocity offset
214 #endif
215 
216  int mBottomNote, mTopNote;
217 #if 0
218  // Also unused from vertical scrolling
219  int mStartBottomNote;
220 #endif
221 
222  // Remember continuous variation for zooming,
223  // but it is rounded off whenever drawing:
225 
226  enum { MinPitch = 0, MaxPitch = 127 };
227  static const float ZoomStep;
228 
229  int mVisibleChannels; // bit set of visible channels
230 
231  std::weak_ptr<StretchHandle> mStretchHandle;
232 };
233 
236 private:
238  // mBottom is the Y offset of pitch 0 (normally off screen)
239  // Used so that mBottomNote is located at
240  // mY + mHeight - (GetNoteMargin() + 1 + GetPitchHeight())
241  int mBottom;
242  int mMargin;
243 
244  enum { MinPitchHeight = 1, MaxPitchHeight = 25 };
245 public:
246  NoteTrackDisplayData(const NoteTrack* track, const wxRect &r);
247 
248  int GetPitchHeight(int factor) const
249  { return std::max(1, (int)(factor * mPitchHeight)); }
250  int GetNoteMargin() const { return mMargin; };
251  int GetOctaveHeight() const { return GetPitchHeight(12) + 2; }
252  // IPitchToY returns Y coordinate of top of pitch p
253  int IPitchToY(int p) const;
254  // compute the window coordinate of the bottom of an octave: This is
255  // the bottom of the line separating B and C.
256  int GetOctaveBottom(int oct) const {
257  return IPitchToY(oct * 12) + GetPitchHeight(1) + 1;
258  }
259  // Y coordinate for given floating point pitch (rounded to int)
260  int PitchToY(double p) const {
261  return IPitchToY((int) (p + 0.5));
262  }
263  // Integer pitch corresponding to a Y coordinate
264  int YToIPitch(int y) const;
265  // map pitch class number (0-11) to pixel offset from bottom of octave
266  // (the bottom of the black line between B and C) to the top of the
267  // note. Note extra pixel separates B(11)/C(0) and E(4)/F(5).
268  int GetNotePos(int p) const
269  { return 1 + GetPitchHeight(p + 1) + (p > 4); }
270  // get pixel offset to top of ith black key note
271  int GetBlackPos(int i) const { return GetNotePos(i * 2 + 1 + (i > 1)); }
272  // GetWhitePos tells where to draw lines between keys as an offset from
273  // GetOctaveBottom. GetWhitePos(0) returns 1, which matches the location
274  // of the line separating B and C
275  int GetWhitePos(int i) const { return 1 + (i * GetOctaveHeight()) / 7; }
276 };
277 
278 extern AUDACITY_DLL_API StringSetting MIDIPlaybackDevice;
279 extern AUDACITY_DLL_API StringSetting MIDIRecordingDevice;
280 extern AUDACITY_DLL_API IntSetting MIDISynthLatency_ms;
281 
283 
284 #endif // USE_MIDI
285 
286 #ifndef SONIFY
287 // no-ops:
288 #define SonifyBeginSonification()
289 #define SonifyEndSonification()
290 #define SonifyBeginNoteBackground()
291 #define SonifyEndNoteBackground()
292 #define SonifyBeginNoteForeground()
293 #define SonifyEndNoteForeground()
294 #define SonifyBeginMeasures()
295 #define SonifyEndMeasures()
296 #define SonifyBeginSerialize()
297 #define SonifyEndSerialize()
298 #define SonifyBeginUnserialize()
299 #define SonifyEndUnserialize()
300 #define SonifyBeginAutoSave()
301 #define SonifyEndAutoSave()
302 #define SonifyBeginModifyState()
303 #define SonifyEndModifyState()
304 #endif
305 
306 
307 AUDACITY_DLL_API wxString GetMIDIDeviceInfo();
308 
309 #endif
XMLWriter
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:26
NoteTrack::mStretchHandle
std::weak_ptr< StretchHandle > mStretchHandle
Definition: NoteTrack.h:231
NoteTrackDisplayData::NoteTrackDisplayData
NoteTrackDisplayData(const NoteTrack *track, const wxRect &r)
Definition: NoteTrack.cpp:1117
NoteTrackDisplayData::IPitchToY
int IPitchToY(int p) const
Definition: NoteTrack.cpp:1155
NoteTrackDisplayData::PitchToY
int PitchToY(double p) const
Definition: NoteTrack.h:260
TimeWarper
Transforms one point in time to another point. For example, a time stretching effect might use one to...
Definition: TimeWarper.h:62
NoteTrackDisplayData::mMargin
int mMargin
Definition: NoteTrack.h:242
NoteTrackDisplayData::MinPitchHeight
@ MinPitchHeight
Definition: NoteTrack.h:244
IntSetting
Specialization of Setting for int.
Definition: Prefs.h:213
Track::GetEndTime
virtual double GetEndTime() const =0
NoteTrackDisplayData::GetOctaveBottom
int GetOctaveBottom(int oct) const
Definition: NoteTrack.h:256
NoteTrackDisplayData::GetBlackPos
int GetBlackPos(int i) const
Definition: NoteTrack.h:271
Track::GetTypeInfo
virtual const TypeInfo & GetTypeInfo() const =0
Track::GetIntervals
virtual ConstIntervals GetIntervals() const
Report times on the track where important intervals begin and end, for UI to snap to.
Definition: Track.cpp:1108
ENUMERATE_TRACK_TYPE
ENUMERATE_TRACK_TYPE(NoteTrack)
Track::Cut
virtual Holder Cut(double WXUNUSED(t0), double WXUNUSED(t1))=0
NoteTrack::ToggleVisibleChan
void ToggleVisibleChan(int c)
Definition: NoteTrack.h:182
Track::InsertSilence
virtual void InsertSilence(double WXUNUSED(t), double WXUNUSED(len))=0
ALL_CHANNELS
#define ALL_CHANNELS
Definition: NoteTrack.h:175
Track::Paste
virtual void Paste(double WXUNUSED(t), const Track *WXUNUSED(src))=0
MIDISynthLatency_ms
AUDACITY_DLL_API IntSetting MIDISynthLatency_ms
Definition: NoteTrack.cpp:1285
MIDIPlaybackDevice
AUDACITY_DLL_API StringSetting MIDIPlaybackDevice
Definition: NoteTrack.cpp:1283
GetMIDIDeviceInfo
AUDACITY_DLL_API wxString GetMIDIDeviceInfo()
Definition: NoteTrack.cpp:1179
QuantizedTimeAndBeat
std::pair< double, double > QuantizedTimeAndBeat
Definition: NoteTrack.h:60
NoteTrack::IsVisibleChan
bool IsVisibleChan(int c) const
Definition: NoteTrack.h:177
StringSetting
Specialization of Setting for strings.
Definition: Prefs.h:227
Track::GetStartTime
virtual double GetStartTime() const =0
NoteTrack::ZoomStep
static const float ZoomStep
Definition: NoteTrack.h:227
NoteTrack::ZoomOut
void ZoomOut(const wxRect &rect, int y)
Zooms out a constant factor (subject to zoom limits)
Definition: NoteTrack.h:141
NoteTrackDisplayData::mPitchHeight
float mPitchHeight
Definition: NoteTrack.h:237
NoteTrack::mPitchHeight
float mPitchHeight
Definition: NoteTrack.h:224
NoteTrackDisplayData
Data used to display a note track.
Definition: NoteTrack.h:235
CHANNEL_BIT
#define CHANNEL_BIT(c)
Definition: NoteTrack.h:176
Track::Holder
std::shared_ptr< Track > Holder
Definition: Track.h:347
NoteTrack::mTopNote
int mTopNote
Definition: NoteTrack.h:216
NoteTrack::SetVisibleChan
void SetVisibleChan(int c)
Definition: NoteTrack.h:180
NoteTrackDisplayData::YToIPitch
int YToIPitch(int y) const
Definition: NoteTrack.cpp:1158
NoteTrack::GetTopNote
int GetTopNote() const
Gets the current top note (a pitch)
Definition: NoteTrack.h:125
NoteTrack::GetBottomNote
int GetBottomNote() const
Gets the current bottom note (a pitch)
Definition: NoteTrack.h:123
Track::Silence
virtual void Silence(double WXUNUSED(t0), double WXUNUSED(t1))=0
Track::GetOffset
virtual double GetOffset() const =0
NoteTrackDisplayData::mBottom
int mBottom
Definition: NoteTrack.h:241
AudioTrack
Track subclass holding data representing sound (as notes, or samples, or ...)
Definition: Track.h:855
Track::Clone
virtual Holder Clone() const =0
AudioTrack::ClassTypeInfo
static const TypeInfo & ClassTypeInfo()
Definition: Track.cpp:1163
NoteTrackDisplayData::GetWhitePos
int GetWhitePos(int i) const
Definition: NoteTrack.h:275
NoteTrack::ClearVisibleChan
void ClearVisibleChan(int c)
Definition: NoteTrack.h:181
NoteTrackDisplayData::MaxPitchHeight
@ MaxPitchHeight
Definition: NoteTrack.h:244
NoteTrack::mSerializationLength
long mSerializationLength
Definition: NoteTrack.h:210
PlayableTrack
AudioTrack subclass that can also be audibly replayed by the program.
Definition: Track.h:875
XMLTagHandler
This class is an interface which should be implemented by classes which wish to be able to load and s...
Definition: XMLTagHandler.h:42
NoteTrack::SoloVisibleChan
void SoloVisibleChan(int c)
Definition: NoteTrack.h:185
Track::WriteXML
virtual void WriteXML(XMLWriter &xmlFile) const =0
XMLTagHandler::HandleXMLChild
virtual XMLTagHandler * HandleXMLChild(const std::string_view &tag)=0
NoteTrackDisplayData::GetOctaveHeight
int GetOctaveHeight() const
Definition: NoteTrack.h:251
Track
Abstract base class for an object holding data associated with points on a time axis.
Definition: Track.h:224
NoteTrackDisplayData::GetNoteMargin
int GetNoteMargin() const
Definition: NoteTrack.h:250
StretchHandle
Definition: StretchHandle.h:22
AudacityProject
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:89
NoteTrackDisplayData::GetNotePos
int GetNotePos(int p) const
Definition: NoteTrack.h:268
NoteTrack::mVisibleChannels
int mVisibleChannels
Definition: NoteTrack.h:229
NoteTrack::ZoomIn
void ZoomIn(const wxRect &rect, int y)
Zooms in a constant factor (subject to zoom limits)
Definition: NoteTrack.h:143
NoteTrackDisplayData::GetPitchHeight
int GetPitchHeight(int factor) const
Definition: NoteTrack.h:248
Track::Clear
virtual void Clear(double WXUNUSED(t0), double WXUNUSED(t1))=0
Track.h
declares abstract base class Track, TrackList, and iterators over TrackList
NoteTrack::ZoomMaxExtent
void ZoomMaxExtent()
Zooms so that the entire track is visible.
Definition: NoteTrack.h:136
Prefs.h
NoteTrack::mSerializationBuffer
std::unique_ptr< char[]> mSerializationBuffer
Definition: NoteTrack.h:209
NoteTrack::Holder
std::shared_ptr< NoteTrack > Holder
Definition: NoteTrack.h:75
Track::Copy
virtual Holder Copy(double WXUNUSED(t0), double WXUNUSED(t1), bool forClipboard=true) const =0
Track::PasteInto
virtual Holder PasteInto(AudacityProject &) const =0
Find or create the destination track for a paste, maybe in a different project.
NoteTrack::mSeq
std::unique_ptr< Alg_seq > mSeq
Definition: NoteTrack.h:208
AttributesList
std::vector< Attribute > AttributesList
Definition: XMLTagHandler.h:40
NoteTrack
A Track that is used for Midi notes. (Somewhat old code).
Definition: NoteTrack.h:67
MIDIRecordingDevice
AUDACITY_DLL_API StringSetting MIDIRecordingDevice
Definition: NoteTrack.cpp:1284
XMLTagHandler::HandleXMLTag
virtual bool HandleXMLTag(const std::string_view &tag, const AttributesList &attrs)=0