forked from Sneeds-Feed-and-Seed/sneedacity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrossFade.cpp
292 lines (220 loc) · 7.19 KB
/
CrossFade.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
/**********************************************************************
Sneedacity: A Digital Audio Editor
CrossFade.cpp
*******************************************************************//**
\class CrossFader
\brief Not used by Sneedacity (yet) apparently work in progress that has
been abandoned.
*//********************************************************************/
#pragma message( "--- CrossFade.cpp: This is abandoned code, not included in Sneedacity builds")
#include "CrossFade.h"
#include <iostream>
#include <vector>
using std::vector;
using std::cout;
using std::endl;
CrossFader::CrossFader():
mType(FT_MIX)
{
}
CrossFader::~CrossFader()
{
}
bool CrossFader::GetSamples(samplePtr buffer, sampleFormat format,
sampleCount start, size_t len)
{
switch (mType)
{
case FT_MIX:
return CrossFadeMix(buffer,format, start,len);
break;
case FT_TRIANGULAR:
return CrossFadeMix(buffer,format, start,len);
break;
case FT_EXPONENTIAL:
default:
return CrossFadeMix(buffer,format, start,len);
break;
}
}
bool CrossFader::CrossFadeMix(samplePtr buffer, sampleFormat format, sampleCount start, size_t len)
{
std::cout << "Crossfading from " << start.as_long_long()
<< " to " << ( len + start ).as_long_long()
<< std::endl;
// start refers to the position in the wave.
//just mix the right things together.
//For each relevant clip, we need to construct a buffer.
//we should use one of the size len, because this has already
//been determined to be good in Mixer
//Go through each clip, adding it to the total in the appropriate way.
//this could be 'optimized' by getting all of the sequences and then
//iterating through each of them.
int numclips = mClips.size();
//create vectors to store the important info for each clip.
std::vector<sampleCount> clipStart(numclips);
std::vector<sampleCount> clipLength(numclips);
std::vector<Sequence*> tmpSequence(numclips);
unsigned int i = 0;
//Now, go through the clips and load up the vectors.
for(const auto &tmpclip: mClips)
{
tmpSequence[i] = tmpclip->GetSequence();
//start is the position of the beginning of the buffer
//relative to the beginning of the clip. It could be negative.
clipStart[i]= start - tmpclip->GetStartSample();
//determine the index of the last sample to get, relative to the start of the clip
//it will be no longer than the clip itself.
clipLength[i] = tmpclip->GetNumSamples()-clipStart[i];
std::cout << "X:" << " "
<< clipLength[i].as_long_long()
<< " "
<< tmpclip->GetStartSample().as_long_long()
<< " ";
//if the buffer ends before the clip does, adjust the length
if(clipStart[i] + len < clipLength[i])
{
clipLength[i] = len + clipStart[i];
}
std::cout
<< clipStart[i].as_long_long()
<< " "
<< clipLength[i].as_long_long()
<< " "
<< ( clipLength[i] - clipStart[i] ).as_long_long()
<< std::endl;
}
std::cout << "-------------\n";
//Now, determine the sample format:
switch(format) {
case int16Sample:
{
std::cout << "int\n";
short *dest = (short *)buffer;
vector<short*> shortSeq;
//Copy the sequences over to the NEW vector, casting as you go.
for(int i = 0; i < numclips; i++)
// PRL: what the ... ? This cast is just wrong!
shortSeq.push_back((short*)tmpSequence[i]);
int clips;
double f;
//now, shortSeq contains the samples to mix together.
for (int j = 0; j < (int)len; j++)
{
//Go through each clip
for(int i = 0; i < numclips; i++)
{
clips = 0;
f = 0;
if(j + clipStart[i] >= 0 &&
clipStart[i]+len < clipLength[i])//only copy if we are within the clip
{
// UNSAFE_SAMPLE_COUNT_TRUNCATION
// -- but class CrossFader is not used as of this writing
f += shortSeq[ i ][ j+ clipStart[i].as_long_long() ];
clips++;
}
f/= clips;
//Do bounds-checking
if (f > 32767)
f = 32767;
if (f < -32768)
f = -32768;
//Set value
*dest = (short)f;
}
dest++;
}
}
break;
case int24Sample:
{
std::cout << "int24\n";
int *dest = (int *)buffer;
vector<int *> intSeq;
//Copy the sequences over to the NEW vector, casting as you go.
for(int i = 0; i < numclips; i++)
// Murder most foul! As in the best it is,
// But this most foul, strange, and unnatural.
intSeq.push_back((int*)tmpSequence[i]);
int clips=0;
double f;
//Go through each sample position
for (int j = 0; j < (int)len; j++) {
//go through each clip.
for(int i= 0; i < numclips; i++)
{
clips = 0;
f = 0;
//only copy if we are within the clip
if(j + clipStart[i] >= 0 && clipStart[i] + len < clipLength[i])
{
// UNSAFE_SAMPLE_COUNT_TRUNCATION
// -- but class CrossFader is not used as of this writing
f+= intSeq[ i ][ j + clipStart[ i ].as_long_long() ];
clips++;
}
f /= clips;
if (f > 8388607)
f = 8388607;
if (f < -8388608)
f = -8388608;
*dest = (int)f;
}
dest ++;
}
}
break;
case floatSample: {
std::cout << "float\n";
float *dest = (float *)buffer;
vector<float*> floatSeq;
int clips = 0;
float f;
//go through each sample position
for (int j = 0; j < (int)len; j++) {
clips = 0;
f = 0;
for(int i = 0; i < numclips; i++)
{
cout << numclips << " " ;
cout <<f << " " ;
if(j + clipStart[i] >= 0 &&
clipStart[i] + j < clipLength[i])//only copy if we are within the clip
{
// UNSAFE_SAMPLE_COUNT_TRUNCATION
// -- but class CrossFader is not used as of this writing
// -- hey wait, you never even tried to initialize floatSeq,
// not even with bad casts!!!
// Subscripts out of bounds, bombs away!!!
f += floatSeq[ i ][ ( j + clipStart[ i ] ).as_long_long() ];
clips++;
}
cout << f << " "<< i << " "
<< floatSeq[ i ][ j + clipStart[ i ].as_long_long() ] << "|";
}
if(clips == 0)
*dest = 0.0f;
else
{
f /= clips;
cout << f << "--";
// MM: XXX Should probably go outside the loop
if (f > 1.0f)
*dest = 1.0f;
else if (f < -1.0f)
*dest = -1.0f;
else
*dest = (float)f;
}
cout << *dest << endl;
dest++;
}
} break;
} // switch
return true;
}
void CrossFader::ClearClips()
{
mClips.clear();
}