File: SoundEffectsBank.cpp

package info (click to toggle)
cultivation 9+dfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 4,452 kB
  • sloc: cpp: 35,783; ansic: 693; sh: 163; makefile: 122; perl: 67
file content (215 lines) | stat: -rw-r--r-- 5,680 bytes parent folder | download | duplicates (2)
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
/*
 * Modification History
 *
 * 2007-August-7   Jason Rohrer
 * Created.
 */


#include "SoundEffectsBank.h"

#include "minorGems/util/random/StdRandomSource.h"

#include <math.h>

extern int globalSoundSampleRate;

extern StdRandomSource globalRandomSource;


SoundEffectsBank::SoundEffectsBank( SoundPlayer *inPlayer )
        : mSoundPlayer( inPlayer ) {

    double effectTime = 0.5;
    
    int numEffectSamples = (int)( effectTime * globalSoundSampleRate );
    

    float *left = new float[ numEffectSamples ];
    float *right = new float[ numEffectSamples ];
    
    int i;
    
    // swish swish swish for planting (digging sound)
    for( i=0; i<numEffectSamples; i++ ) {
        left[i] = right[i] =
            globalRandomSource.getRandomBoundedDouble( -1, 1 ) * 
            ( numEffectSamples - i ) / (float)numEffectSamples *
            ( ( i % 1500 ) / 1500.0 );
        }
    

    mEffectSamples[ effect_plantSeed ] = 
        new SoundSamples( numEffectSamples, left, right );
    

    left = new float[ numEffectSamples ];
    right = new float[ numEffectSamples ];

    // rising sine for pickup
    for( i=0; i<numEffectSamples; i++ ) {
        double iFraction = (double)i / (double)numEffectSamples;
        
        double frequency = 200 *( 1 - iFraction ) + 400 * iFraction;
        
        left[i] = right[i] =
            sin( frequency / globalSoundSampleRate * 2 * M_PI * i );
        }
    

    mEffectSamples[ effect_pickupWater ] = 
        new SoundSamples( numEffectSamples, left, right );



    left = new float[ numEffectSamples ];
    right = new float[ numEffectSamples ];

    // falling sine for dump
    for( i=0; i<numEffectSamples; i++ ) {
        double iFraction = (double)i / (double)numEffectSamples;
        
        double frequency = 400 *( 1 - iFraction ) + 200 * iFraction;
        
        left[i] = right[i] =
            sin( frequency / globalSoundSampleRate * 2 * M_PI * i );
        }
    

    mEffectSamples[ effect_dumpWater ] = 
        new SoundSamples( numEffectSamples, left, right );



    left = new float[ numEffectSamples ];
    right = new float[ numEffectSamples ];


    // deep falling sine for poison
    for( i=0; i<numEffectSamples; i++ ) {
        double iFraction = (double)i / (double)numEffectSamples;
        
        double frequency = 200 *( 1 - iFraction ) + 100 * iFraction;
        
        left[i] = right[i] =
            sin( frequency / globalSoundSampleRate * 2 * M_PI * i );
        }
    

    mEffectSamples[ effect_poison ] = 
        new SoundSamples( numEffectSamples, left, right );

    
    

    left = new float[ numEffectSamples ];
    right = new float[ numEffectSamples ];

    // high sine ping for gift
    for( i=0; i<numEffectSamples; i++ ) {
        double frequency = 800;
        
        left[i] = right[i] =
            ( numEffectSamples - i ) / (float)numEffectSamples *
            sin( frequency / globalSoundSampleRate * 2 * M_PI * i );
        }
    

    mEffectSamples[ effect_giveFruit ] = 
        new SoundSamples( numEffectSamples, left, right );


    



    // these ones are much shorter
    effectTime = 0.125;
    numEffectSamples = (int)( effectTime * globalSoundSampleRate );



    left = new float[ numEffectSamples ];
    right = new float[ numEffectSamples ];


    // quick falling sine for eat
    for( i=0; i<numEffectSamples; i++ ) {
        double iFraction = (double)i / (double)numEffectSamples;
        
        double frequency = 800 *( 1 - iFraction ) + 400 * iFraction;
        
        left[i] = right[i] =
            sin( frequency / globalSoundSampleRate * 2 * M_PI * i );
        }
    

    mEffectSamples[ effect_eatFruit ] = 
        new SoundSamples( numEffectSamples, left, right );



    left = new float[ numEffectSamples ];
    right = new float[ numEffectSamples ];

    // quick rising sine for pick
    for( i=0; i<numEffectSamples; i++ ) {
        double iFraction = (double)i / (double)numEffectSamples;
        
        double frequency = 400 *( 1 - iFraction ) + 800 * iFraction;
        
        left[i] = right[i] =
            ( numEffectSamples - i ) / (float)numEffectSamples *
            sin( frequency / globalSoundSampleRate * 2 * M_PI * i );
        }
    

    mEffectSamples[ effect_pickFruit ] = 
        new SoundSamples( numEffectSamples, left, right );



    left = new float[ numEffectSamples ];
    right = new float[ numEffectSamples ];

    // quick, low rising sine for mate
    for( i=0; i<numEffectSamples; i++ ) {
        double iFraction = (double)i / (double)numEffectSamples;
        
        double frequency = 200 *( 1 - iFraction ) + 400 * iFraction;
        
        left[i] = right[i] =
            ( numEffectSamples - i ) / (float)numEffectSamples *
            sin( frequency / globalSoundSampleRate * 2 * M_PI * i );
        }
    

    mEffectSamples[ effect_mate ] = 
        new SoundSamples( numEffectSamples, left, right );

    
    mEffectLoudness[ effect_plantSeed ] = 0.05;
    mEffectLoudness[ effect_pickupWater ] = 0.05;
    mEffectLoudness[ effect_dumpWater ] = 0.05;
    mEffectLoudness[ effect_poison ] = 0.1;
    mEffectLoudness[ effect_pickFruit ] = 0.05;
    mEffectLoudness[ effect_eatFruit ] = 0.05;
    mEffectLoudness[ effect_giveFruit ] = 0.05;
    mEffectLoudness[ effect_mate ] = 0.1; 
    }


        
SoundEffectsBank::~SoundEffectsBank() {
    for( int i=0; i<numSoundEffects; i++ ) {
        delete mEffectSamples[i];
        }
    }




void SoundEffectsBank::playEffect( SoundEffectKey inKey ) {
    mSoundPlayer->playSoundNow( mEffectSamples[ inKey ], false, 
                                mEffectLoudness[ inKey ] );
    }