1 /*
2  * Modification History
3  *
4  * 2004-July-17   Jason Rohrer
5  * Created.
6  *
7  * 2004-August-12   Jason Rohrer
8  * Added a constructor that can specify all sound data.
9  */
10 
11 
12 
13 #include "SoundSamples.h"
14 
15 
16 #include <string.h>
17 
18 
19 
SoundSamples(unsigned long inSampleCount,float * inLeftChannel,float * inRightChannel)20 SoundSamples::SoundSamples( unsigned long inSampleCount,
21                             float *inLeftChannel, float *inRightChannel )
22     : mSampleCount( inSampleCount ),
23       mLeftChannel( inLeftChannel ),
24       mRightChannel( inRightChannel ) {
25 
26     }
27 
28 
29 
SoundSamples(unsigned long inSampleCount)30 SoundSamples::SoundSamples( unsigned long inSampleCount )
31     : mSampleCount( inSampleCount ),
32       mLeftChannel( new float[ inSampleCount ] ),
33       mRightChannel( new float[ inSampleCount ] ) {
34 
35     // zero out the channels;
36     for( unsigned long i=0; i<mSampleCount; i++ ) {
37         mLeftChannel[ i ] = 0;
38         mRightChannel[ i ] = 0;
39         }
40     }
41 
42 
43 
SoundSamples(SoundSamples * inSamplesToCopy)44 SoundSamples::SoundSamples( SoundSamples *inSamplesToCopy ) {
45     mSampleCount = inSamplesToCopy->mSampleCount;
46 
47     mLeftChannel = new float[ mSampleCount ];
48     mRightChannel = new float[ mSampleCount ];
49 
50 
51     memcpy( (void *)( mLeftChannel ),
52             (void *)( inSamplesToCopy->mLeftChannel ),
53             mSampleCount * sizeof( float ) );
54 
55     memcpy( (void *)( mRightChannel ),
56             (void *)( inSamplesToCopy->mRightChannel ),
57             mSampleCount * sizeof( float ) );
58     }
59 
60 
61 
SoundSamples(SoundSamples * inSamplesToCopy,unsigned long inNumToCopy)62 SoundSamples::SoundSamples( SoundSamples *inSamplesToCopy,
63                             unsigned long inNumToCopy ) {
64     mSampleCount = inSamplesToCopy->mSampleCount;
65 
66     if( inNumToCopy < mSampleCount ) {
67         mSampleCount = inNumToCopy;
68         }
69 
70     mLeftChannel = new float[ mSampleCount ];
71     mRightChannel = new float[ mSampleCount ];
72 
73 
74     memcpy( (void *)( mLeftChannel ),
75             (void *)( inSamplesToCopy->mLeftChannel ),
76             mSampleCount * sizeof( float ) );
77 
78     memcpy( (void *)( mRightChannel ),
79             (void *)( inSamplesToCopy->mRightChannel ),
80             mSampleCount * sizeof( float ) );
81     }
82 
83 
84 
~SoundSamples()85 SoundSamples::~SoundSamples() {
86     delete [] mRightChannel;
87     delete [] mLeftChannel;
88     }
89 
90 
91 
trim(unsigned long inNumSamplesToDrop)92 void SoundSamples::trim( unsigned long inNumSamplesToDrop ) {
93 
94     unsigned long newSampleCount = mSampleCount - inNumSamplesToDrop;
95 
96     float *newLeftChannel = new float[ newSampleCount ];
97     float *newRightChannel = new float[ newSampleCount ];
98 
99     // copy samples, skipping inNumSamplesToDrop
100     memcpy( (void *)( newLeftChannel ),
101             (void *)( &( mLeftChannel[ inNumSamplesToDrop ] ) ),
102             newSampleCount * sizeof( float ) );
103     memcpy( (void *)( newRightChannel ),
104             (void *)( &( mRightChannel[ inNumSamplesToDrop ] ) ),
105             newSampleCount * sizeof( float ) );
106 
107     delete [] mLeftChannel;
108     delete [] mRightChannel;
109 
110     mSampleCount = newSampleCount;
111 
112     mLeftChannel = newLeftChannel;
113     mRightChannel = newRightChannel;
114     }
115