Debian Codegraph @

src/tracks/playabletrack/notetrack/ui/NoteTrackDisplayData.cpp

raw
1 /**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file NoteTrackDisplayData.cpp
6 @brief Implements NoteTrackDisplayData
7
8 Paul Licameli split from NoteTrack.cpp
9
10 **********************************************************************/
11
12 #include "NoteTrackDisplayData.h"
13 #include "NoteTrack.h"
14 #include "WrapAllegro.h"
15
16 static NoteTrackAttachments::RegisteredFactory key{
17 [](NoteTrack&){ return std::make_unique<NoteTrackRange>(); }
18 };
19
20 NoteTrackRange &NoteTrackRange::Get(const NoteTrack &track)
21 {
22 auto &mutTrack = const_cast<NoteTrack&>(track);
23 return static_cast<NoteTrackRange&>(
24 mutTrack.NoteTrackAttachments::Get(key));
25 }
26
27 NoteTrackRange::~NoteTrackRange() = default;
28
29 std::unique_ptr<NoteTrackAttachment> NoteTrackRange::Clone() const
30 {
31 return std::make_unique<NoteTrackRange>(*this);
32 }
33
34 void NoteTrackRange::WriteXML(XMLWriter &xmlFile) const
35 {
36 xmlFile.WriteAttr(wxT("bottomnote"), mBottomNote);
37 xmlFile.WriteAttr(wxT("topnote"), mTopNote);
38 }
39
40 bool NoteTrackRange::HandleAttribute(const Attribute &pair)
41 {
42 auto attr = pair.first;
43 auto value = pair.second;
44 long nValue{};
45 if (attr == "bottomnote" && value.TryGet(nValue)) {
46 SetBottomNote(nValue);
47 return true;
48 }
49 if (attr == "topnote" && value.TryGet(nValue)) {
50 SetTopNote(nValue);
51 return true;
52 }
53 return false;
54 }
55
56 void NoteTrackRange::SetBottomNote(int note)
57 {
58 if (note < MinPitch)
59 note = MinPitch;
60 else if (note > 96)
61 note = 96;
62
63 wxCHECK(note <= mTopNote, );
64
65 mBottomNote = note;
66 }
67
68 void NoteTrackRange::SetTopNote(int note)
69 {
70 if (note > MaxPitch)
71 note = MaxPitch;
72
73 wxCHECK(note >= mBottomNote, );
74
75 mTopNote = note;
76 }
77
78 void NoteTrackRange::SetNoteRange(int note1, int note2)
79 {
80 // Bounds check
81 if (note1 > MaxPitch)
82 note1 = MaxPitch;
83 else if (note1 < MinPitch)
84 note1 = MinPitch;
85 if (note2 > MaxPitch)
86 note2 = MaxPitch;
87 else if (note2 < MinPitch)
88 note2 = MinPitch;
89 // Swap to ensure ordering
90 if (note2 < note1) { std::swap(note1, note2); }
91
92 mBottomNote = note1;
93 mTopNote = note2;
94 }
95
96 void NoteTrackRange::ShiftNoteRange(int offset)
97 {
98 // Ensure everything stays in bounds
99 if (mBottomNote + offset < MinPitch || mTopNote + offset > MaxPitch)
100 return;
101
102 mBottomNote += offset;
103 mTopNote += offset;
104 }
105
106 #if 0
107 void NoteTrackRange::StartVScroll()
108 {
109 mStartBottomNote = mBottomNote;
110 }
111
112 void NoteTrackRange::VScroll(int start, int end)
113 {
114 int ph = GetPitchHeight();
115 int delta = ((end - start) + ph / 2) / ph;
116 ShiftNoteRange(delta);
117 }
118 #endif
119
120 void NoteTrackRange::ZoomAllNotes(Alg_seq *pSeq)
121 {
122 Alg_iterator iterator(pSeq, false);
123 iterator.begin();
124 Alg_event_ptr evt;
125
126 // Go through all of the notes, finding the minimum and maximum value pitches.
127 bool hasNotes = false;
128 int minPitch = MaxPitch;
129 int maxPitch = MinPitch;
130
131 while (nullptr != (evt = iterator.next())) {
132 if (evt->is_note()) {
133 int pitch = (int) evt->get_pitch();
134 hasNotes = true;
135 if (pitch < minPitch)
136 minPitch = pitch;
137 if (pitch > maxPitch)
138 maxPitch = pitch;
139 }
140 }
141
142 if (!hasNotes) {
143 // Semi-arbitrary default values:
144 minPitch = 48;
145 maxPitch = 72;
146 }
147
148 SetNoteRange(minPitch, maxPitch);
149 }
150
151 NoteTrackDisplayData::NoteTrackDisplayData(
152 const NoteTrack &track, const wxRect &rect)
153 : mTrack{ track }
154 , mRect{ rect }
155 {
156 auto &data = NoteTrackRange::Get(mTrack);
157 auto span = data.GetTopNote() - data.GetBottomNote() + 1; // + 1 to make sure it includes both
158
159 mMargin = std::min((int) (rect.height / (float)(span)) / 2, rect.height / 4);
160
161 // Count the number of dividers between B/C and E/F
162 int numC = 0, numF = 0;
163 auto botOctave = data.GetBottomNote() / 12, botNote = data.GetBottomNote() % 12;
164 auto topOctave = data.GetTopNote() / 12, topNote = data.GetTopNote() % 12;
165 if (topOctave == botOctave)
166 {
167 if (botNote == 0) numC = 1;
168 if (topNote <= 5) numF = 1;
169 }
170 else
171 {
172 numC = topOctave - botOctave;
173 numF = topOctave - botOctave - 1;
174 if (botNote == 0) numC++;
175 if (botNote <= 5) numF++;
176 if (topOctave <= 5) numF++;
177 }
178 // Effective space, excluding the margins and the lines between some notes
179 auto effectiveHeight = rect.height - (2 * (mMargin + 1)) - numC - numF;
180 // Guaranteed that both the bottom and top notes will be visible
181 // (assuming that the clamping below does not happen)
182 mPitchHeight = effectiveHeight / ((float) span);
183
184 if (mPitchHeight < MinPitchHeight)
185 mPitchHeight = MinPitchHeight;
186 if (mPitchHeight > MaxPitchHeight)
187 mPitchHeight = MaxPitchHeight;
188
189 mBottom = rect.y + rect.height - GetNoteMargin() - 1 - GetPitchHeight(1) +
190 botOctave * GetOctaveHeight() + GetNotePos(botNote);
191 }
192
193 int NoteTrackDisplayData::IPitchToY(int p) const
194 { return mBottom - (p / 12) * GetOctaveHeight() - GetNotePos(p % 12); }
195
196 int NoteTrackDisplayData::YToIPitch(int y) const
197 {
198 y = mBottom - y; // pixels above pitch 0
199 int octave = (y / GetOctaveHeight());
200 y -= octave * GetOctaveHeight();
201 // result is approximate because C and G are one pixel taller than
202 // mPitchHeight.
203 // Poke 1-13-18: However in practice this seems not to be an issue,
204 // as long as we use mPitchHeight and not the rounded version
205 return (y / mPitchHeight) + octave * 12;
206 }
207
208 void NoteTrackDisplayData::Zoom(int y, float multiplier, bool center)
209 {
210 auto &data = NoteTrackRange::Get(mTrack);
211 int clickedPitch = YToIPitch(y);
212 int extent = data.GetTopNote() - data.GetBottomNote() + 1;
213 int newExtent = (int) (extent / multiplier);
214 float position;
215 if (center) {
216 // center the pitch that the user clicked on
217 position = .5;
218 } else {
219 // align to keep the pitch that the user clicked on in the same place
220 position = extent / (clickedPitch - data.GetBottomNote());
221 }
222 int newBottomNote = clickedPitch - (newExtent * position);
223 int newTopNote = clickedPitch + (newExtent * (1 - position));
224 data.SetNoteRange(newBottomNote, newTopNote);
225 }
226
227 void NoteTrackDisplayData::ZoomTo(int start, int end)
228 {
229 wxRect trackRect(0, mRect.GetY(), 1, mRect.GetHeight());
230 int pitch1 = YToIPitch(start);
231 int pitch2 = YToIPitch(end);
232 if (pitch1 == pitch2) {
233 // Just zoom in instead of zooming to show only one note
234 Zoom(start, 1, true);
235 return;
236 }
237 // It's fine for this to be in either order
238 NoteTrackRange::Get(mTrack).SetNoteRange(pitch1, pitch2);
239 }
240
241 int NoteTrackDisplayData::GetPitchHeight(int factor) const
242 {
243 return std::max(1, (int)(factor * mPitchHeight));
244
245 }
246
247 const float NoteTrackDisplayData::ZoomStep = powf( 2.0f, 0.25f );