File: MusicPlayer.cpp

package info (click to toggle)
transcend 0.3.dfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 5,832 kB
  • ctags: 2,912
  • sloc: cpp: 26,890; ansic: 693; sh: 210; makefile: 131; perl: 67
file content (366 lines) | stat: -rw-r--r-- 10,780 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
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
/*
 * Modification History
 *
 * 2004-August-22   Jason Rohrer
 * Created.
 *
 * 2004-August-23   Jason Rohrer
 * Fixed bug in note offsets.
 *
 * 2004-August-24   Jason Rohrer
 * Added missing support for reversed notes and stereo split.
 * Changed to use constant power panning.
 */



#include "MusicPlayer.h"
#include "LevelDirectoryManager.h"



MusicPlayer::MusicPlayer( unsigned long inSamplesPerSecond,
                          SculptureManager *inSculptureManager,
                          MusicNoteWaveTable *inWaveTable,
                          double inWorldWidth,
                          double inWorldHeight,
                          double inGridSpaceWidth  )
    : mSculptureManager( inSculptureManager ),
      mWaveTable( inWaveTable ),
      mActiveNotes( new SimpleVector<SoundSamples*>() ),
      mNotePositions( new SimpleVector<unsigned long>() ),
      mSampleRate( inSamplesPerSecond ),
      mWorldWidth( inWorldWidth ),
      mWorldHeight( inWorldHeight ),
      mGridSpaceWidth( inGridSpaceWidth ) {


    // start at far left
    mCurrentPartGridPosition = -mWorldWidth / 2; 

    
    char error = false;
    
    mPartLengthInSeconds =
        LevelDirectoryManager::readDoubleFileContents(
            "musicPartLength", &error, true );

    if( error ) {
        // default to 10 second parts
        mPartLengthInSeconds = 10.0;
        }

    
    
    }



MusicPlayer::~MusicPlayer() {
    int numActiveNotes = mActiveNotes->size();

    for( int i=0; i<numActiveNotes; i++ ) {
        delete *( mActiveNotes->getElement( i ) );
        }

    delete mActiveNotes;
    delete mNotePositions;
    }



SoundSamples *MusicPlayer::getMoreMusic( unsigned long inNumSamples ) {

    double halfWorldWidth = mWorldWidth / 2;
    

    double bufferLengthInSeconds = (double)inNumSamples / (double)mSampleRate;
    double bufferLengthInWorldUnits =
        ( bufferLengthInSeconds / mPartLengthInSeconds ) * mGridSpaceWidth;
    

    
    int numPieces;
    
    Vector3D **positions =
        mSculptureManager->getPiecePositions( &numPieces );

    MusicPart **musicParts =
        mSculptureManager->getPieceMusicParts( &numPieces );

    if( numPieces == 0 ) {
        // no pieces in sculpture... nothing to play

        delete [] positions;
        delete [] musicParts;

        // return silent samples
        return new SoundSamples( inNumSamples );        
        }

    
    // find right-most and left-most piece positions (bounds of song)
    
    double rightMostPiecePosition = -halfWorldWidth;
    double leftMostPiecePosition = halfWorldWidth;
    
    int i;
    for( i=0; i<numPieces; i++ ) {
        double x = positions[i]->mX;
        
        if( x < leftMostPiecePosition ) {
            leftMostPiecePosition = x;
            }
        if( x > rightMostPiecePosition ) {
            rightMostPiecePosition = x;
            }
        }

    
    if( mCurrentPartGridPosition < leftMostPiecePosition ) {
        // jump past empty space at far left
        mCurrentPartGridPosition = leftMostPiecePosition;
        }

    
    if( mCurrentPartGridPosition > rightMostPiecePosition + mGridSpaceWidth ) {
        // beyond music part of right-most sculpture piece

        // wrap around to the left-most in-sculpture piece
        mCurrentPartGridPosition = leftMostPiecePosition;
        }


    // find the pieces that play during this buffer
    for( i=0; i<numPieces; i++ ) {
        double x = positions[i]->mX;
        double y = positions[i]->mY;
        // if piece either starts during this buffer or started in a previous
        // buffer but is still playing during this buffer, then play notes
        // from it
        
        char playPiece = false;
        double offsetBeforePiece = 0;
        double offsetIntoPiece = 0;
        
        if( x >= mCurrentPartGridPosition &&
            x <= mCurrentPartGridPosition + bufferLengthInWorldUnits ) {
           
            playPiece = true;
            offsetBeforePiece = x - mCurrentPartGridPosition;

            // convert from world units into seconds
            offsetBeforePiece =
                ( offsetBeforePiece / mGridSpaceWidth ) * mPartLengthInSeconds;
            }

        
        if ( x < mCurrentPartGridPosition &&
             x + mGridSpaceWidth >= mCurrentPartGridPosition ) {

            playPiece = true;
            offsetIntoPiece = mCurrentPartGridPosition - x;

            // convert from world units into seconds
            offsetIntoPiece =
                ( offsetIntoPiece / mGridSpaceWidth ) * mPartLengthInSeconds;
            }
        

        if( playPiece ) {

            MusicPart *part = musicParts[i];

            MusicNote **notes;
            double *noteStartOffsets;
                    
            
            int numNotes = part->getNotesStartingInInterval( 
                offsetIntoPiece, 
                bufferLengthInSeconds,
                &notes,
                &noteStartOffsets );
                    
            
            // render each note and add it to our list of active notes
            for( int j=0; j<numNotes; j++ ) {

                double totalNoteOffset = offsetBeforePiece +
                    noteStartOffsets[j];

                unsigned long numSilentSamples =
                    (unsigned long)( totalNoteOffset * mSampleRate );
                
                unsigned long numNoteSamples;
                float *noteSamples =
                    mWaveTable->mapNoteToSamples( notes[j],
                                                  &numNoteSamples );

                
                unsigned long totalSamples = numSilentSamples + numNoteSamples;

                
                SoundSamples *samplesObject = new SoundSamples( totalSamples );

                float *leftChannel = samplesObject->mLeftChannel;
                float *rightChannel = samplesObject->mRightChannel;


                // compute stereo panning position

                /*
                 * Notes by Phil Burk (creator of portaudio):
                 *
                 * If you want to keep the power constant, then (L^2 + R^2)
                 * should be constant.  One way to do that is to use sine and
                 * cosine curves for left and right because
                 * (sin^2 + cos^2) = 1.
                 *
                 * pan = 0.0 to  PI/2
                 * LeftGain(pan) = cos(pan)
                 * RightGain(pan) = sin(pan)
                 */

                // pan smoothly between
                // -( mWorldHeight / 4 ) and +( mWorldHeight / 4 )
                // beyond this range, have constant right or left

                double panPosition;
                
                if( y >= -( mWorldHeight / 4 ) && y <= ( mWorldHeight / 4 ) ) {

                    // convert y position into a pan position in the range
                    // 0 to pi/2
                    
                    panPosition = y / ( mWorldHeight / 4 );

                    panPosition = ( panPosition + 1 ) / 2;
                    panPosition *= M_PI / 2;
                    }
                else if( y < -( mWorldHeight / 4 ) ) {
                    // hard left
                    panPosition = 0;
                    }
                else {
                    // hard right
                    panPosition = M_PI / 2;
                    }

                float leftGain = (float)( cos( panPosition ) );
                float rightGain = (float)( sin( panPosition ) );


                // samplesObject starts out with all 0 samples
                // just fill in the note samples beyond the starting silent
                // region
                char reversed = notes[j]->mReversed;
                
                for( unsigned long k=0; k<numNoteSamples; k++ ) {
                    unsigned long sampleIndex;
                    if( reversed ) {
                        sampleIndex =
                            ( numNoteSamples - k - 1 ) + numSilentSamples;
                        }
                    else {
                        sampleIndex = k + numSilentSamples;
                        }
                    
                    leftChannel[ sampleIndex ] =
                        leftGain * noteSamples[k];
                    rightChannel[ sampleIndex ] =
                        rightGain * noteSamples[k];
                    }

                mActiveNotes->push_back( samplesObject );
                mNotePositions->push_back( 0 );
                
                delete notes[j];
                }

            delete [] notes;
            delete [] noteStartOffsets;
            }


        delete positions[i];
        }

    delete [] positions;
    delete [] musicParts;


    // now we have added all notes that start playing sometime during
    // this buffer


    // next mix the samples from active notes that play during this buffer

    SoundSamples *returnSamples = new SoundSamples( inNumSamples );
    float *returnLeftChannel = returnSamples->mLeftChannel;
    float *returnRightChannel = returnSamples->mRightChannel;
    
    for( i=0; i<mActiveNotes->size(); i++ ) {

        SoundSamples *noteSamples = *( mActiveNotes->getElement( i ) );
        unsigned long notePosition = *( mNotePositions->getElement( i ) );

        unsigned long numNoteSamples = noteSamples->mSampleCount;
        
        unsigned long numSamplesToPlay = inNumSamples;

        char noteFinished = false;
        
        if( numSamplesToPlay + notePosition > numNoteSamples ) {
            // buffer goes beyond the end of this note

            numSamplesToPlay = numNoteSamples - notePosition;
            
            noteFinished = true;
            }
        
        float *noteLeftChannel = noteSamples->mLeftChannel;
        float *noteRightChannel = noteSamples->mRightChannel;
                
        for( unsigned long j=0; j<numSamplesToPlay; j++ ) {
            unsigned long noteIndex = j + notePosition;
            
            returnLeftChannel[j] += noteLeftChannel[ noteIndex ];
            returnRightChannel[j] += noteRightChannel[ noteIndex ];

            }

        notePosition += numSamplesToPlay;

        *( mNotePositions->getElement( i ) ) = notePosition;

        
        if( noteFinished ) {
            delete noteSamples;
            mActiveNotes->deleteElement( i );
            mNotePositions->deleteElement( i );

            // back up in for loop to reflect this note dropping out
            i--;
            }
        }
    
    

    // advance the grid position
    mCurrentPartGridPosition += bufferLengthInWorldUnits;

    
    return returnSamples;
    }



double MusicPlayer::getCurrentPartGridPosition() {
    return mCurrentPartGridPosition;
    }