~ubuntu-branches/ubuntu/trusty/teeworlds/trusty

« back to all changes in this revision

Viewing changes to .pc/new-wavpack.patch/src/engine/client/sound.cpp

  • Committer: Package Import Robot
  • Author(s): Felix Geyer
  • Date: 2013-05-05 09:49:34 UTC
  • mfrom: (1.2.5)
  • Revision ID: package-import@ubuntu.com-20130505094934-uidw42ov1t0jlvrz
Tags: 0.6.2+dfsg-1
* New upstream release.
  - Update patches.
* Pass $CPPFLAGS to the build system.
* Switch to my @debian.org email address.
* Bump Standards-Version to 3.9.4, no changes needed.
* Change Vcs host to anonscm.debian.org.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2
2
/* If you are missing that file, acquire a complete release at teeworlds.com.                */
 
3
#include <base/math.h>
3
4
#include <base/system.h>
 
5
 
4
6
#include <engine/graphics.h>
5
7
#include <engine/storage.h>
 
8
 
6
9
#include <engine/shared/config.h>
7
10
 
8
11
#include "SDL.h"
19
22
        NUM_SAMPLES = 512,
20
23
        NUM_VOICES = 64,
21
24
        NUM_CHANNELS = 16,
22
 
        
23
 
        MAX_FRAMES = 1024
24
25
};
25
26
 
26
27
struct CSample
63
64
static volatile int m_SoundVolume = 100;
64
65
 
65
66
static int m_NextVoice = 0;
66
 
 
 
67
static int *m_pMixBuffer = 0;   // buffer only used by the thread callback function
 
68
static unsigned m_MaxFrames = 0;
67
69
 
68
70
// TODO: there should be a faster way todo this
69
71
static short Int2Short(int i)
84
86
 
85
87
static void Mix(short *pFinalOut, unsigned Frames)
86
88
{
87
 
        int aMixBuffer[MAX_FRAMES*2] = {0};
88
89
        int MasterVol;
 
90
        mem_zero(m_pMixBuffer, m_MaxFrames*2*sizeof(int));
 
91
        Frames = min(Frames, m_MaxFrames);
89
92
 
90
93
        // aquire lock while we are mixing
91
94
        lock_wait(m_SoundLock);
92
 
        
 
95
 
93
96
        MasterVol = m_SoundVolume;
94
 
        
 
97
 
95
98
        for(unsigned i = 0; i < NUM_VOICES; i++)
96
99
        {
97
100
                if(m_aVoices[i].m_pSample)
98
101
                {
99
102
                        // mix voice
100
103
                        CVoice *v = &m_aVoices[i];
101
 
                        int *pOut = aMixBuffer;
 
104
                        int *pOut = m_pMixBuffer;
102
105
 
103
106
                        int Step = v->m_pSample->m_Channels; // setup input sources
104
107
                        short *pInL = &v->m_pSample->m_pData[v->m_Tick*Step];
105
108
                        short *pInR = &v->m_pSample->m_pData[v->m_Tick*Step+1];
106
 
                        
 
109
 
107
110
                        unsigned End = v->m_pSample->m_NumFrames-v->m_Tick;
108
111
 
109
112
                        int Rvol = v->m_pChannel->m_Vol;
112
115
                        // make sure that we don't go outside the sound data
113
116
                        if(Frames < End)
114
117
                                End = Frames;
115
 
                        
 
118
 
116
119
                        // check if we have a mono sound
117
120
                        if(v->m_pSample->m_Channels == 1)
118
121
                                pInR = pInL;
126
129
                                int dy = v->m_Y - m_CenterY;
127
130
                                int Dist = (int)sqrtf((float)dx*dx+dy*dy); // float here. nasty
128
131
                                int p = IntAbs(dx);
129
 
                                if(Dist < Range)
 
132
                                if(Dist >= 0 && Dist < Range)
130
133
                                {
131
134
                                        // panning
132
135
                                        if(dx > 0)
133
136
                                                Lvol = ((Range-p)*Lvol)/Range;
134
137
                                        else
135
138
                                                Rvol = ((Range-p)*Rvol)/Range;
136
 
                                        
 
139
 
137
140
                                        // falloff
138
141
                                        Lvol = (Lvol*(Range-Dist))/Range;
139
142
                                        Rvol = (Rvol*(Range-Dist))/Range;
154
157
                                pInR += Step;
155
158
                                v->m_Tick++;
156
159
                        }
157
 
                        
 
160
 
158
161
                        // free voice if not used any more
159
162
                        if(v->m_Tick == v->m_pSample->m_NumFrames)
160
163
                        {
165
168
                        }
166
169
                }
167
170
        }
168
 
        
169
 
        
 
171
 
 
172
 
170
173
        // release the lock
171
174
        lock_release(m_SoundLock);
172
175
 
176
179
                for(unsigned i = 0; i < Frames; i++)
177
180
                {
178
181
                        int j = i<<1;
179
 
                        int vl = ((aMixBuffer[j]*MasterVol)/101)>>8;
180
 
                        int vr = ((aMixBuffer[j+1]*MasterVol)/101)>>8;
 
182
                        int vl = ((m_pMixBuffer[j]*MasterVol)/101)>>8;
 
183
                        int vr = ((m_pMixBuffer[j+1]*MasterVol)/101)>>8;
181
184
 
182
185
                        pFinalOut[j] = Int2Short(vl);
183
186
                        pFinalOut[j+1] = Int2Short(vr);
201
204
        m_SoundEnabled = 0;
202
205
        m_pGraphics = Kernel()->RequestInterface<IEngineGraphics>();
203
206
        m_pStorage = Kernel()->RequestInterface<IStorage>();
204
 
        
 
207
 
205
208
        SDL_AudioSpec Format;
206
 
        
 
209
 
207
210
        m_SoundLock = lock_create();
208
 
        
 
211
 
209
212
        if(!g_Config.m_SndEnable)
210
213
                return 0;
211
 
        
 
214
 
 
215
        if(SDL_InitSubSystem(SDL_INIT_AUDIO) < 0)
 
216
        {
 
217
                dbg_msg("gfx", "unable to init SDL audio: %s", SDL_GetError());
 
218
                return -1;
 
219
        }
 
220
 
212
221
        m_MixingRate = g_Config.m_SndRate;
213
222
 
214
223
        // Set 16-bit stereo audio at 22Khz
228
237
        else
229
238
                dbg_msg("client/sound", "sound init successful");
230
239
 
 
240
        m_MaxFrames = g_Config.m_SndBufferSize*2;
 
241
        m_pMixBuffer = (int *)mem_alloc(m_MaxFrames*2*sizeof(int), 1);
 
242
 
231
243
        SDL_PauseAudio(0);
232
 
        
 
244
 
233
245
        m_SoundEnabled = 1;
234
246
        Update(); // update the volume
235
247
        return 0;
239
251
{
240
252
        // update volume
241
253
        int WantedVolume = g_Config.m_SndVolume;
242
 
        
 
254
 
243
255
        if(!m_pGraphics->WindowActive() && g_Config.m_SndNonactiveMute)
244
256
                WantedVolume = 0;
245
 
        
 
257
 
246
258
        if(WantedVolume != m_SoundVolume)
247
259
        {
248
260
                lock_wait(m_SoundLock);
249
261
                m_SoundVolume = WantedVolume;
250
262
                lock_release(m_SoundLock);
251
263
        }
252
 
        
 
264
 
253
265
        return 0;
254
266
}
255
267
 
256
268
int CSound::Shutdown()
257
269
{
258
270
        SDL_CloseAudio();
 
271
        SDL_QuitSubSystem(SDL_INIT_AUDIO);
259
272
        lock_destroy(m_SoundLock);
 
273
        if(m_pMixBuffer)
 
274
        {
 
275
                mem_free(m_pMixBuffer);
 
276
                m_pMixBuffer = 0;
 
277
        }
260
278
        return 0;
261
279
}
262
280
 
277
295
        CSample *pSample = &m_aSamples[SampleID];
278
296
        int NumFrames = 0;
279
297
        short *pNewData = 0;
280
 
        
 
298
 
281
299
        // make sure that we need to convert this sound
282
300
        if(!pSample->m_pData || pSample->m_Rate == m_MixingRate)
283
301
                return;
285
303
        // allocate new data
286
304
        NumFrames = (int)((pSample->m_NumFrames/(float)pSample->m_Rate)*m_MixingRate);
287
305
        pNewData = (short *)mem_alloc(NumFrames*pSample->m_Channels*sizeof(short), 1);
288
 
        
 
306
 
289
307
        for(int i = 0; i < NumFrames; i++)
290
308
        {
291
309
                // resample TODO: this should be done better, like linear atleast
293
311
                int f = (int)(a*pSample->m_NumFrames);
294
312
                if(f >= pSample->m_NumFrames)
295
313
                        f = pSample->m_NumFrames-1;
296
 
                
 
314
 
297
315
                // set new data
298
316
                if(pSample->m_Channels == 1)
299
317
                        pNewData[i] = pSample->m_pData[f];
303
321
                        pNewData[i*2+1] = pSample->m_pData[f*2+1];
304
322
                }
305
323
        }
306
 
        
 
324
 
307
325
        // free old data and apply new
308
326
        mem_free(pSample->m_pData);
309
327
        pSample->m_pData = pNewData;
321
339
        int SampleID = -1;
322
340
        char aError[100];
323
341
        WavpackContext *pContext;
324
 
        
 
342
 
325
343
        // don't waste memory on sound when we are stress testing
326
344
        if(g_Config.m_DbgStress)
327
345
                return -1;
328
 
                
 
346
 
329
347
        // no need to load sound when we are running with no sound
330
348
        if(!m_SoundEnabled)
331
349
                return 1;
332
 
                
 
350
 
333
351
        if(!m_pStorage)
334
352
                return -1;
335
353
 
372
390
                        dbg_msg("sound/wv", "file is %d Hz, not 44100 Hz. filename='%s'", snd->rate, filename);
373
391
                        return -1;
374
392
                }*/
375
 
                
 
393
 
376
394
                if(BitsPerSample != 16)
377
395
                {
378
396
                        dbg_msg("sound/wv", "bps is %d, not 16, filname='%s'", BitsPerSample, pFilename);
382
400
                pData = (int *)mem_alloc(4*m_aSamples*m_aChannels, 1);
383
401
                WavpackUnpackSamples(pContext, pData, m_aSamples); // TODO: check return value
384
402
                pSrc = pData;
385
 
                
 
403
 
386
404
                pSample->m_pData = (short *)mem_alloc(2*m_aSamples*m_aChannels, 1);
387
405
                pDst = pSample->m_pData;
388
406
 
428
446
{
429
447
        int VoiceID = -1;
430
448
        int i;
431
 
        
 
449
 
432
450
        lock_wait(m_SoundLock);
433
 
        
 
451
 
434
452
        // search for voice
435
453
        for(i = 0; i < NUM_VOICES; i++)
436
454
        {
442
460
                        break;
443
461
                }
444
462
        }
445
 
        
 
463
 
446
464
        // voice found, use it
447
465
        if(VoiceID != -1)
448
466
        {
457
475
                m_aVoices[VoiceID].m_X = (int)x;
458
476
                m_aVoices[VoiceID].m_Y = (int)y;
459
477
        }
460
 
        
 
478
 
461
479
        lock_release(m_SoundLock);
462
480
        return VoiceID;
463
481
}