Audacity  3.0.3
CrossFade.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  CrossFade.cpp
6 
7 *******************************************************************//********************************************************************/
14 
15 #pragma message( "--- CrossFade.cpp: This is abandoned code, not included in Audacity builds")
16 
17 #include "CrossFade.h"
18 
19 #include <iostream>
20 #include <vector>
21 
22 using std::vector;
23 using std::cout;
24 using std::endl;
25 
27  mType(FT_MIX)
28 {
29 }
30 
31 
32 
34 {
35 }
36 
37 
38 
40  sampleCount start, size_t len)
41 {
42  switch (mType)
43  {
44  case FT_MIX:
45  return CrossFadeMix(buffer,format, start,len);
46  break;
47  case FT_TRIANGULAR:
48  return CrossFadeMix(buffer,format, start,len);
49  break;
50  case FT_EXPONENTIAL:
51  default:
52  return CrossFadeMix(buffer,format, start,len);
53  break;
54 
55  }
56 
57 }
58 
60 {
61 
62  std::cout << "Crossfading from " << start.as_long_long()
63  << " to " << ( len + start ).as_long_long()
64  << std::endl;
65 
66  // start refers to the position in the wave.
67 
68 
69  //just mix the right things together.
70 
71 
72  //For each relevant clip, we need to construct a buffer.
73  //we should use one of the size len, because this has already
74  //been determined to be good in Mixer
75 
76  //Go through each clip, adding it to the total in the appropriate way.
77 
78  //this could be 'optimized' by getting all of the sequences and then
79  //iterating through each of them.
80 
81  int numclips = mClips.size();
82 
83  //create vectors to store the important info for each clip.
84  std::vector<sampleCount> clipStart(numclips);
85  std::vector<sampleCount> clipLength(numclips);
86  std::vector<Sequence*> tmpSequence(numclips);
87 
88 
89  unsigned int i = 0;
90  //Now, go through the clips and load up the vectors.
91  for(const auto &tmpclip: mClips)
92  {
93  tmpSequence[i] = tmpclip->GetSequence();
94 
95 
96  //start is the position of the beginning of the buffer
97  //relative to the beginning of the clip. It could be negative.
98  clipStart[i]= start - tmpclip->GetStartSample();
99 
100 
101 
102  //determine the index of the last sample to get, relative to the start of the clip
103 
104  //it will be no longer than the clip itself.
105  clipLength[i] = tmpclip->GetNumSamples()-clipStart[i];
106 
107  std::cout << "X:" << " "
108  << clipLength[i].as_long_long()
109  << " "
110  << tmpclip->GetStartSample().as_long_long()
111  << " ";
112  //if the buffer ends before the clip does, adjust the length
113  if(clipStart[i] + len < clipLength[i])
114  {
115  clipLength[i] = len + clipStart[i];
116  }
117  std::cout
118  << clipStart[i].as_long_long()
119  << " "
120  << clipLength[i].as_long_long()
121  << " "
122  << ( clipLength[i] - clipStart[i] ).as_long_long()
123  << std::endl;
124  }
125  std::cout << "-------------\n";
126 
127  //Now, determine the sample format:
128  switch(format) {
129  case int16Sample:
130  {
131  std::cout << "int\n";
132  short *dest = (short *)buffer;
133  vector<short*> shortSeq;
134 
135  //Copy the sequences over to the NEW vector, casting as you go.
136  for(int i = 0; i < numclips; i++)
137  // PRL: what the ... ? This cast is just wrong!
138  shortSeq.push_back((short*)tmpSequence[i]);
139 
140 
141  int clips;
142  double f;
143  //now, shortSeq contains the samples to mix together.
144  for (int j = 0; j < (int)len; j++)
145  {
146 
147  //Go through each clip
148  for(int i = 0; i < numclips; i++)
149  {
150  clips = 0;
151  f = 0;
152  if(j + clipStart[i] >= 0 &&
153  clipStart[i]+len < clipLength[i])//only copy if we are within the clip
154  {
155  // UNSAFE_SAMPLE_COUNT_TRUNCATION
156  // -- but class CrossFader is not used as of this writing
157  f += shortSeq[ i ][ j+ clipStart[i].as_long_long() ];
158  clips++;
159  }
160 
161  f/= clips;
162 
163  //Do bounds-checking
164  if (f > 32767)
165  f = 32767;
166  if (f < -32768)
167  f = -32768;
168 
169  //Set value
170  *dest = (short)f;
171  }
172  dest++;
173  }
174  }
175  break;
176 
177 
178  case int24Sample:
179  {
180  std::cout << "int24\n";
181  int *dest = (int *)buffer;
182  vector<int *> intSeq;
183 
184 
185  //Copy the sequences over to the NEW vector, casting as you go.
186  for(int i = 0; i < numclips; i++)
187  // Murder most foul! As in the best it is,
188  // But this most foul, strange, and unnatural.
189  intSeq.push_back((int*)tmpSequence[i]);
190 
191  int clips=0;
192  double f;
193  //Go through each sample position
194  for (int j = 0; j < (int)len; j++) {
195 
196  //go through each clip.
197  for(int i= 0; i < numclips; i++)
198  {
199  clips = 0;
200  f = 0;
201 
202  //only copy if we are within the clip
203  if(j + clipStart[i] >= 0 && clipStart[i] + len < clipLength[i])
204  {
205  // UNSAFE_SAMPLE_COUNT_TRUNCATION
206  // -- but class CrossFader is not used as of this writing
207  f+= intSeq[ i ][ j + clipStart[ i ].as_long_long() ];
208  clips++;
209  }
210 
211  f /= clips;
212 
213  if (f > 8388607)
214  f = 8388607;
215  if (f < -8388608)
216  f = -8388608;
217  *dest = (int)f;
218 
219  }
220  dest ++;
221  }
222  }
223  break;
224 
225 
226  case floatSample: {
227  std::cout << "float\n";
228  float *dest = (float *)buffer;
229  vector<float*> floatSeq;
230 
231  int clips = 0;
232  float f;
233 
234  //go through each sample position
235  for (int j = 0; j < (int)len; j++) {
236 
237 
238  clips = 0;
239  f = 0;
240 
241  for(int i = 0; i < numclips; i++)
242  {
243 
244  cout << numclips << " " ;
245 
246  cout <<f << " " ;
247 
248  if(j + clipStart[i] >= 0 &&
249  clipStart[i] + j < clipLength[i])//only copy if we are within the clip
250  {
251  // UNSAFE_SAMPLE_COUNT_TRUNCATION
252  // -- but class CrossFader is not used as of this writing
253  // -- hey wait, you never even tried to initialize floatSeq,
254  // not even with bad casts!!!
255  // Subscripts out of bounds, bombs away!!!
256  f += floatSeq[ i ][ ( j + clipStart[ i ] ).as_long_long() ];
257  clips++;
258  }
259  cout << f << " "<< i << " "
260  << floatSeq[ i ][ j + clipStart[ i ].as_long_long() ] << "|";
261  }
262  if(clips == 0)
263  *dest = 0.0f;
264  else
265  {
266 
267  f /= clips;
268  cout << f << "--";
269  // MM: XXX Should probably go outside the loop
270  if (f > 1.0f)
271  *dest = 1.0f;
272  else if (f < -1.0f)
273  *dest = -1.0f;
274  else
275  *dest = (float)f;
276 
277  }
278  cout << *dest << endl;
279  dest++;
280  }
281  } break;
282  } // switch
283 
284 
285  return true;
286 }
287 
288 
290 {
291  mClips.clear();
292 }
FT_EXPONENTIAL
@ FT_EXPONENTIAL
Definition: CrossFade.h:25
CrossFader::GetSamples
bool GetSamples(samplePtr buffer, sampleFormat format, sampleCount start, size_t len)
Definition: CrossFade.cpp:39
CrossFader::CrossFader
CrossFader()
Definition: CrossFade.cpp:26
int24Sample
@ int24Sample
Definition: SampleFormat.h:33
floatSample
@ floatSample
Definition: SampleFormat.h:34
CrossFade.h
sampleCount::as_long_long
long long as_long_long() const
Definition: SampleCount.h:47
int16Sample
@ int16Sample
Definition: SampleFormat.h:32
format
int format
Definition: ExportPCM.cpp:56
CrossFader::mClips
WaveClipHolders mClips
Definition: CrossFade.h:47
FT_MIX
@ FT_MIX
Definition: CrossFade.h:23
sampleFormat
sampleFormat
Definition: SampleFormat.h:29
samplePtr
char * samplePtr
Definition: SampleFormat.h:49
CrossFader::mType
FadeType mType
Definition: CrossFade.h:53
FT_TRIANGULAR
@ FT_TRIANGULAR
Definition: CrossFade.h:24
sampleCount
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:18
CrossFader::ClearClips
void ClearClips()
Definition: CrossFade.cpp:289
CrossFader::~CrossFader
~CrossFader()
Definition: CrossFade.cpp:33
CrossFader::CrossFadeMix
bool CrossFadeMix(samplePtr buffer, sampleFormat format, sampleCount start, size_t len)
Definition: CrossFade.cpp:59