-
Notifications
You must be signed in to change notification settings - Fork 0
/
maFeelMap.cpp
400 lines (322 loc) · 9.36 KB
/
maFeelMap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
//
// MultiArp - Another step in the Great Midi Adventure!
// Copyright (C) 2017, 2018 Barry Neilsen
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//////////////////////////////////////////////////////////////////////////////
#include <algorithm>
#include <cmath>
#include <fstream>
#include "maFeelMap.h"
#include "maUtility.h"
using namespace std;
#define LOG_ON 0
#if LOG_ON
ofstream fLog;
#endif
FeelMap::FeelMap():
m_Active(true),
m_EditPoint(1)
{
//ctor
m_Help = "S-Left/Right: insert point, S-Del: delete, (S-)Up/Dn: change value";
#if LOG_ON
fLog.open("FeelMap.log");
#endif
#if 0
m_StretchPoints.push_back(0.0);
m_StretchPoints.push_back(0.6);
m_StretchPoints.push_back(1.0); // Do we need the last one, it's always 1.0, after all.
m_Points = 2;
#endif
#if 0
m_StretchPoints.push_back(0.0);
m_StretchPoints.push_back(0.5);
m_StretchPoints.push_back(0.75);
m_StretchPoints.push_back(1.0); // Do we need the last one, it's always 1.0, after all.
m_Points = 3;
#endif
#if 0
m_StretchPoints.push_back(0.0);
m_StretchPoints.push_back(0.3);
m_StretchPoints.push_back(0.6);
m_StretchPoints.push_back(1.0); // Do we need the last one, it's always 1.0, after all.
m_Points = 3;
#endif
#if 0
m_StretchPoints.push_back(0.0);
m_StretchPoints.push_back(0.30);
m_StretchPoints.push_back(0.5);
m_StretchPoints.push_back(0.80);
m_StretchPoints.push_back(1.0); // Do we need the last one, it's always 1.0, after all.
m_Points = 4;
#endif
}
FeelMap::~FeelMap()
{
//dtor
#if LOG_ON
fLog.close();
#endif
}
void FeelMap::New(vector<string> & tokens)
{
m_StretchPoints.clear();
m_StretchPoints.push_back(0.0);
m_StretchPoints.push_back(1.0);
if ( tokens.size() > 2 )
{
for ( unsigned i = 2; i < tokens.size(); i++ )
{
try
{
double t = stod(tokens.at(i));
// Check if we're creating a default array of size 't',
// (Don't allow higher than 12, mostly because we
// can't fit any more on one line of the display.)
if ( i == 2 && t > 1 && t < 13 )
{
m_Points = t;
m_StretchPoints.resize(m_Points + 1);
m_EditPoint = 1;
Respace();
return;
}
if ( t > 0 && t < 1.0 )
m_StretchPoints.push_back(t); // We'll sort them after the loop.
}
catch(...)
{
// Invalid parameter. Do nothing and try the next one.
}
}
sort(m_StretchPoints.begin(), m_StretchPoints.end());
}
m_Points = m_StretchPoints.size() - 1;
if ( m_Points > 1 )
m_EditPoint = 1;
else
m_EditPoint = 0;
SetStatus();
}
void FeelMap::Add()
{
double val = m_StretchPoints.at(m_EditPoint) + (m_StretchPoints.at(m_EditPoint + 1) - m_StretchPoints.at(m_EditPoint))/2;
vector<double>::iterator it = m_StretchPoints.begin();
m_StretchPoints.insert(it + m_EditPoint + 1, val);
m_Points = m_StretchPoints.size() - 1;
m_EditPoint += 1;
SetStatus();
}
void FeelMap::Remove()
{
if ( m_Points <= 1 )
return;
vector<double>::iterator it = m_StretchPoints.begin();
m_StretchPoints.erase(it + m_EditPoint);
m_Points = m_StretchPoints.size() - 1;
if ( m_EditPoint == m_Points )
m_EditPoint -= 1;
SetStatus();
}
void FeelMap::Respace()
{
if ( m_Points <= 1 )
return;
for ( int i = 0; i < m_Points + 1; i++ )
m_StretchPoints.at(i) = double(i)/(m_Points);
SetStatus();
}
string FeelMap::ToString(const char * prefix)
{
char buff[100];
string result;
if ( prefix != NULL )
{
result += prefix;
result += ' ';
}
result += "Feel Map";
if ( m_StretchPoints.size() > 2 )
for ( unsigned i = 1; i < m_StretchPoints.size() - 1; i++ )
{
result += " ";
sprintf(buff, "%.3f", m_StretchPoints.at(i));
result += buff;
}
return result;
}
void FeelMap::FromString(string s)
{
if ( s.find("Feel Map") == string::npos )
throw string("FeelMap::FromString() - Not a valid field list.");
vector<string> tokens = split(s.c_str(), ' ');
New(tokens);
}
void FeelMap::SetStatus()
{
char buff[200];
m_Status = "Feel: ";
m_FieldPositions.clear();
m_Highlights.clear();
if ( ! m_Active )
{
m_Status += " Off";
return;
}
if ( m_StretchPoints.size() < 2 )
{
m_Status += "No stretch points set.";
return;
}
int pos = 0;
for ( unsigned i = 0; i < m_StretchPoints.size(); i++ )
{
pos = m_Status.size();
sprintf(buff, " %.2f", m_StretchPoints.at(i));
m_Status += buff;
m_FieldPositions.emplace_back(pos + 1, m_Status.size() - pos - 1);
}
if ( m_Points > 1 )
m_Highlights.push_back(m_FieldPositions.at(m_EditPoint));
}
bool FeelMap::HandleKey(key_type_t k)
{
if ( ! m_Active /*|| m_EditPoint == 0 || m_EditPoint == m_StretchPoints.size() - 1*/ )
return true;
// A couple of klunky steps to get things started. (This code was first
// written before I started using shift/ctrl to add elements to lists.)
if ( m_StretchPoints.empty() )
{
if ( k == shift_left || k == shift_right )
{
m_StretchPoints.push_back(0.0);
m_StretchPoints.push_back(1.0);
SetStatus();
}
return true;
}
if ( m_StretchPoints.size() < 3 )
{
if ( k == shift_left || k == shift_right )
{
m_EditPoint = 0;
Add();
SetStatus();
}
return true;
}
// int tEditPoint = m_EditPoint;
double tStretchPoint = m_StretchPoints.at(m_EditPoint);
double inc = 0.1;
switch ( k )
{
case left:
if ( m_EditPoint > 1 )
m_EditPoint -= 1;
break;
case right:
if ( static_cast<unsigned>(m_EditPoint) < m_StretchPoints.size() - 2 )
m_EditPoint += 1;
break;
// case left:
// case right:
// if ( k == left )
// tEditPoint -= 1;
// else
// tEditPoint += 1;
// if ( tEditPoint >= 1 && tEditPoint < m_StretchPoints.size() - 1 )
// m_EditPoint = tEditPoint;
// break;
case shift_up:
inc = 0.01;
case up:
tStretchPoint += inc;
if ( tStretchPoint < m_StretchPoints.at(m_EditPoint + 1) )
m_StretchPoints.at(m_EditPoint) = tStretchPoint;
break;
case shift_down:
inc = 0.01;
case down:
tStretchPoint -= inc;
if ( tStretchPoint > m_StretchPoints.at(m_EditPoint -1) )
m_StretchPoints.at(m_EditPoint) = tStretchPoint;
break;
// case up:
// case down:
// if ( k == up )
// tStretchPoint += 0.01;
// else
// tStretchPoint -= 0.01;
// if ( tStretchPoint > m_StretchPoints.at(m_EditPoint -1) && tStretchPoint < m_StretchPoints.at(m_EditPoint + 1) )
// m_StretchPoints.at(m_EditPoint) = tStretchPoint;
// break;
case shift_left:
m_EditPoint -= 1;
Add();
break;
case shift_right:
Add();
break;
case shift_delete:
Remove();
break;
default:
return false;
}
SetStatus();
return true;
}
double FeelMap::Adjust( double beat )
{
if ( ! m_Active )
return beat;
if ( m_StretchPoints.empty() )
return beat;
int wholeBeats = lround(1000 * beat)/1000; // Round to 3 DP.
// 't' is current fraction of of current beat.
double t = beat - wholeBeats;
int milliBeats = lround(1000 * t); // For safe comparisons.
if ( milliBeats == 0 )
return beat;
// 'n' is nearest stretch point.
int n = lround(t * m_Points);
double P = static_cast<double>(m_Points);
double T;
// char buff[100];
if ( n == 0 )
{
T = P * m_StretchPoints.at(1) * t;
#if LOG_ON
sprintf(buff, "Beat %6.2f -> %6.2f (n = %i)\n", beat, T, n);
#endif
}
else
{
double windowIn = 1.0 / P;
double windowOut = m_StretchPoints.at(n) - m_StretchPoints.at(n-1);
double tFrac = t - static_cast<double>(n-1)/P;
T = m_StretchPoints.at(n-1) + tFrac * windowOut / windowIn;
#if LOG_ON
sprintf(buff, "t %6.2f -> %6.2f (n = %i, wIn = %.3f wOut = %.3f tFrac %.3f)\n", t, T, n, windowIn, windowOut, tFrac);
#endif
}
// T = m_StretchPoints.at(n-1) + (1.0 * m_Points * t - 1) * (m_StretchPoints.at(n) - m_StretchPoints.at(n-1));
T += wholeBeats;
#if LOG_ON
fLog << buff;
#endif
return T;
}