File: SoundParameterSpaceControlPoint.cpp

package info (click to toggle)
transcend 0.3.dfsg1-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,816 kB
  • ctags: 2,912
  • sloc: cpp: 26,890; ansic: 693; sh: 210; makefile: 131; perl: 67
file content (333 lines) | stat: -rw-r--r-- 9,713 bytes parent folder | download | duplicates (6)
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
/*
 * Modification History
 *
 * 2004-August-6   Jason Rohrer
 * Created.
 *
 * 2004-August-9   Jason Rohrer
 * Changed to use sine components.
 *
 * 2004-August-12   Jason Rohrer
 * Added support for getting blocks of samples.
 *
 * 2004-August-19   Jason Rohrer
 * Fixed bug in walking through wavetable.
 *
 * 2004-September-3   Jason Rohrer
 * Added brief fade in/out at beginning/end of sound to avoid clicks.
 */



#include "SoundParameterSpaceControlPoint.h"


#include "minorGems/math/geometry/Vector3D.h"



#include <math.h>



SoundParameterSpaceControlPoint::SoundParameterSpaceControlPoint(
    int inNumWaveComponents,
    double *inWaveComponentFrequencies,
    double *inWaveComponentAmplitudes,
    double inStartFrequency,
    double inEndFrequency,
    double inStartLoudness,
    double inEndLoudness )
    : mNumWaveComponents( inNumWaveComponents ),
      mWaveComponentFrequencies( inWaveComponentFrequencies ),
      mWaveComponentAmplitudes( inWaveComponentAmplitudes ),
      mStartFrequency( inStartFrequency ),
      mEndFrequency( inEndFrequency ),
      mStartLoudness( inStartLoudness ),
      mEndLoudness( inEndLoudness ),
      mCurrentWavePoint( 0 ) {

    }



SoundParameterSpaceControlPoint::SoundParameterSpaceControlPoint(
    FILE *inFILE, char *outError ) {

    int totalNumRead = 0;
    
    mNumWaveComponents = 0;
    totalNumRead += fscanf( inFILE, "%d", &mNumWaveComponents );

    // how many values should we successfully read (cheap error checking)
    int totalToRead =
        1 +                 // read the number of wave points
        mNumWaveComponents * 2  + // for each point, read x and y
        + 4;                // read Start/End frequency and loudness
        
    
    mWaveComponentFrequencies = new double[ mNumWaveComponents ];
    mWaveComponentAmplitudes = new double[ mNumWaveComponents ];

    // read frequency and amplitude for each component
    for( int i=0; i<mNumWaveComponents; i++ ) {
        double frequency = 0;
        double amplitude = 0;

        totalNumRead += fscanf( inFILE, "%lf", &frequency );
        totalNumRead += fscanf( inFILE, "%lf", &amplitude );

        mWaveComponentFrequencies[i] = frequency;
        mWaveComponentAmplitudes[i] = amplitude;
        }

    mStartFrequency = 0;
    mEndFrequency = 0;
    mStartLoudness = 1;
    mEndLoudness = 1;
    
    totalNumRead += fscanf( inFILE, "%lf", &mStartFrequency );
    totalNumRead += fscanf( inFILE, "%lf", &mEndFrequency );
    totalNumRead += fscanf( inFILE, "%lf", &mStartLoudness );
    totalNumRead += fscanf( inFILE, "%lf", &mEndLoudness );


    if( totalNumRead != totalToRead ) {
        printf( "Expecting to read %d values, but read %d\n",
                totalToRead, totalNumRead );
        *outError = true;
        }
    else {
        *outError = false;
        }
    }
        

        
SoundParameterSpaceControlPoint::~SoundParameterSpaceControlPoint() {
    delete [] mWaveComponentFrequencies;
    delete [] mWaveComponentAmplitudes;
    }



ParameterSpaceControlPoint *SoundParameterSpaceControlPoint::copy() {

    return new SoundParameterSpaceControlPoint(
        mNumWaveComponents,
        copyDoubleArray( mWaveComponentFrequencies, mNumWaveComponents ),
        copyDoubleArray( mWaveComponentAmplitudes, mNumWaveComponents ),
        mStartFrequency,
        mEndFrequency,
        mStartLoudness,    
        mEndLoudness );    
    }



ParameterSpaceControlPoint *
    SoundParameterSpaceControlPoint::createLinearBlend(
        ParameterSpaceControlPoint *inOtherPoint,
        double inWeightOfOtherPoint ) {

    // cast
    SoundParameterSpaceControlPoint *otherPoint =
        (SoundParameterSpaceControlPoint *)inOtherPoint;
    
        
    double weightOfThisPoint = 1 - inWeightOfOtherPoint;

    int otherNumWaveComponents = otherPoint->mNumWaveComponents;
    double *otherWaveComponentFrequencies =
        otherPoint->mWaveComponentFrequencies;
    double *otherWaveComponentAmplitudes =
        otherPoint->mWaveComponentAmplitudes;
    

    // pack our waveform control points into vector3D arrays so
    // they can be processed by the blend function

    Vector3D **thisWaveComponents = new Vector3D*[ mNumWaveComponents ];
    Vector3D **otherWaveComponents =
        new Vector3D*[ otherPoint->mNumWaveComponents ];

    int i;
    for( i=0; i<mNumWaveComponents; i++ ) {
        thisWaveComponents[i] = new Vector3D( mWaveComponentFrequencies[i],
                                          mWaveComponentAmplitudes[i],
                                          0 );
        }

    for( i=0; i<otherNumWaveComponents; i++ ) {
        otherWaveComponents[i] =
            new Vector3D( otherWaveComponentFrequencies[i],
                          otherWaveComponentAmplitudes[i],
                          0 );
        }


    int resultNumWaveComponents;
    
    Vector3D **resultWaveComponents =
        blendVertexArrays(
            thisWaveComponents,
            mNumWaveComponents,
            weightOfThisPoint,
            otherWaveComponents,
            otherNumWaveComponents,
            &resultNumWaveComponents );

    
    for( i=0; i<mNumWaveComponents; i++ ) {
        delete thisWaveComponents[i];
        }
    delete [] thisWaveComponents;

    for( i=0; i<otherNumWaveComponents; i++ ) {
        delete otherWaveComponents[i];
        }
    delete [] otherWaveComponents;

    
    
    // unpack result
    double *resultWaveComponentFrequencies =
        new double[ resultNumWaveComponents ];
    double *resultWaveComponentAmplitudes =
        new double[ resultNumWaveComponents ];

    for( i=0; i<resultNumWaveComponents; i++ ) {
        resultWaveComponentFrequencies[i] = resultWaveComponents[i]->mX;
        resultWaveComponentAmplitudes[i] = resultWaveComponents[i]->mY;
        

        delete resultWaveComponents[i];
        }
    delete [] resultWaveComponents;



    return new SoundParameterSpaceControlPoint(
        resultNumWaveComponents,
        resultWaveComponentFrequencies,
        resultWaveComponentAmplitudes,
        weightOfThisPoint * mStartFrequency +
            inWeightOfOtherPoint * otherPoint->mStartFrequency,
        weightOfThisPoint * mEndFrequency +
            inWeightOfOtherPoint * otherPoint->mEndFrequency,
        weightOfThisPoint * mStartLoudness +
            inWeightOfOtherPoint * otherPoint->mStartLoudness,
        weightOfThisPoint * mEndLoudness +
            inWeightOfOtherPoint * otherPoint->mEndLoudness );
    }



float *SoundParameterSpaceControlPoint::getSoundSamples(
    unsigned long inStartSample,
    unsigned long inSampleCount,
    unsigned long inSamplesPerSecond,
    double inSoundLengthInSeconds ) {

    if( inStartSample == 0 ) {
        // reset our wave pointer
        mCurrentWavePoint = 0;
        }

    double sampleDeltaInSeconds = 1.0 / inSamplesPerSecond;

    
    float *samples = new float[ inSampleCount ];


    unsigned long soundLengthInSamples =
        (unsigned long)( inSoundLengthInSeconds * inSamplesPerSecond );


    // try to fade in and out for 100 samples to avoid a click
    // at the start and end of the note
    unsigned long numFadeInSamples = 100;
    if( numFadeInSamples > soundLengthInSamples ) {
        numFadeInSamples = soundLengthInSamples / 2;
        }
    
    unsigned long numFadeOutSamples = 100;
    if( numFadeOutSamples > soundLengthInSamples ) {
        numFadeOutSamples = soundLengthInSamples / 2;
        }
    
    
    for( unsigned long i=0; i<inSampleCount; i++ ) {
        
        unsigned long currentSample = i + inStartSample;

        double samplePointInSeconds =
            (double)currentSample / (double)inSamplesPerSecond;

        double soundProgress = samplePointInSeconds / inSoundLengthInSeconds;
    

        double currentFrequency =
            soundProgress * mEndFrequency +
            ( 1 - soundProgress ) * mStartFrequency;
        double currentLoudness =
            soundProgress * mEndLoudness +
            ( 1 - soundProgress ) * mStartLoudness;
        
        // compute the time point in our wave, in the range [0,1]

        mCurrentWavePoint += sampleDeltaInSeconds * currentFrequency;


        // check if we are in the sample region that needs to be faded
        double fadeInFactor = 1;
        double fadeOutFactor = 1;

        if( currentSample < numFadeInSamples ) {
            fadeInFactor = (double)currentSample /
                (double)( numFadeInSamples - 1 );
            }
        if( currentSample >= soundLengthInSamples - numFadeOutSamples ) {
            fadeOutFactor =
                (double)( soundLengthInSamples - currentSample - 1 ) /
                (double)( numFadeOutSamples - 1 );
            }

        double fadeFactor = fadeInFactor * fadeOutFactor;
        
        
        // add up the components at this time point

        
        // sine function cycles once every 2*pi
        // we need to adjust the sine function to cycle according to
        // our wave's freqency
        double adjustedTime = mCurrentWavePoint * ( 2 * M_PI );

    

        double componentSum = 0;
    
        for( int j=0; j<mNumWaveComponents; j++ ) {
            componentSum += mWaveComponentAmplitudes[j] *
                sin( mWaveComponentFrequencies[j] * adjustedTime );
            }
        
        samples[i] = (float)( currentLoudness * fadeFactor * componentSum );
        }

    return samples;
    }
      


double *SoundParameterSpaceControlPoint::copyDoubleArray(
    double *inArray, int inLength ) {

    double *result = new double[ inLength ];

    memcpy( (void *)result, (void *)inArray, inLength * sizeof( double ) );

    return result;
    }