~ubuntu-branches/ubuntu/trusty/stormbaancoureur/trusty

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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#ifndef SOUNDFEED_H
#define SOUNDFEED_H

#include <cstring>
#include "soundclip.h"


// Abstract baseclass for all soundfeed objects
class SoundFeed
{
  public:

    SoundFeed(int psz) : periodsz(psz) {}
    virtual ~SoundFeed() {}

    virtual void Paste(const std::string &fname, int delay, bool looping=false) = 0;
    virtual short *Get(int &numperiods) = 0;
    virtual float FractionDone(int delay) { return 0.0; }

  protected:

    int periodsz;	// in frames
};


// A soundfeed that plays a single soundclip, to be used for musicscore e.g.
class SoundFeedSimple : public SoundFeed
{
  public:

    SoundFeedSimple(int psz) :
      SoundFeed(psz),
      clip(0),
      currentperiod(0)
    {
    }

    virtual ~SoundFeedSimple()
    {
      clip = 0;
    }

    virtual void Paste(const std::string &fname, int delay, bool looping=false)
    {
      assert(!delay);
      clip = SoundClip::Get(fname);
      currentperiod = 0;
    }

    virtual short *Get(int &numperiods)
    {
      if (clip)
      {
        int maxperiod = clip->framecnt/periodsz;
        if (currentperiod < maxperiod)
        {
          int togo = maxperiod - currentperiod;
          if (numperiods > togo)
            numperiods = togo;
          short *retval = clip->data + currentperiod * 2 * periodsz;
          currentperiod += numperiods;
          return retval;
        }
      }
      numperiods = 0;
      return 0;
    }

    virtual float FractionDone(int delay)
    {
      // delay is the nr of frames still in the driver soundbuffer.
      if (!clip) return 0.0;
      int framesdone = currentperiod*periodsz - delay;
      return framesdone / (float) ((clip->framecnt/periodsz)*periodsz);
    }

  protected:

    SoundClip *clip;
    int currentperiod;
};


// A soundfeed that mixes multiple channels, to be used in-game
class SoundFeedComplex : public SoundFeed
{
  class ModulatedChannel
  {
    public:
      ModulatedChannel(int psz) :
        periodsz(psz),
        clip(0),
        sz(0),
        idx(0.0),
        freq(1.0),
        ampl(1.0)
      {
        scratch = new short[100000];
      }
      virtual ~ModulatedChannel()
      {
        delete scratch;
      }
      virtual void Paste(const std::string &fname, int delay, bool looping=false)
      {
        // get the mono soundclip.
        clip = SoundClip::Get(fname, 1);
        sz = clip->framecnt;
        idx = 0.0;
      }
      virtual short *Get(int &numframes)
      {
        if (!clip)
        {
          numframes=0;
          return 0;
        }
        short *writer=scratch;
        for (int i=0; i<numframes; i++)
        {
          int v = (int) (ampl * clip->data[(int)idx]);
          if (v>32767)  v=32767;
          if (v<-32768) v=-32768;
          writer[0] = v;
          writer[1] = v;
          writer += 2;
          idx += freq;
          if (idx >= sz)
            idx -= sz;
        }
        return scratch;
      }
      void SetModulation(float f, float a)
      {
        freq = f;
        ampl = a;
      }
    protected:
      int periodsz;
      SoundClip *clip;
      int sz;
      float idx;
      short *scratch;
      float freq;
      float ampl;
  };

  class Channel
  {
    public:
      Channel() : data(0), sz(0), sent(0), is_looping(false) {}
      virtual ~Channel() { delete data; data=0; }
      virtual void Paste(const std::string &fname, int delay, bool looping)
      {
        // delay is in nr of frames
        SoundClip *clip = SoundClip::Get(fname);
        is_looping = looping;
        if (is_looping) assert(!delay);
        sz = clip->framecnt + delay;
        delete data;
        if (is_looping)
        {
          data  = new short [sz*2*2];
          memcpy(data,      clip->data, clip->framecnt*2*sizeof(short));
          memcpy(data+sz*2, clip->data, clip->framecnt*2*sizeof(short));
        }
        else
        {
          data  = new short [sz*2];
          memset(data, 0, delay*2*sizeof(short));
          memcpy(data+2*delay, clip->data, clip->framecnt*2*sizeof(short));
        }
        sent=0;
      }
      virtual short *Get(int &numframes)
      {
        if (!sz || (sent==sz && !is_looping))
        {
          numframes=0;
          return 0;
        }

        short *retval=data+2*sent;
        if (is_looping)
        {
          retval = data + 2*sent;
          sent += numframes;
          if (sent > sz) sent -= sz;
        }
        else
        {
          int togo = sz - sent;
          if (numframes > togo)
            numframes = togo;
          sent += numframes;
        }
        return retval;
      }
      bool IsIdle(void) const 
      {
        if (is_looping) return false;
        return sent==sz; 
      }
      float GetCompletionFactor(void) const
      {
        if (is_looping) return 0.0;
        if (!sz) return 1.0;
        return sent / (float) sz;
      }
    protected:
      short *data;
      int    sz;	// in nr of frames
      int    sent;	// in nr of frames
      bool   is_looping;
  };

  public:

    SoundFeedComplex(int psz) : 
      SoundFeed(psz),
      modulated_channel(psz)
    {
      scratch32 = new int   [100000];
      scratch16 = new short [100000];
    }
    ~SoundFeedComplex()
    {
      delete scratch32;
      delete scratch16;
    }
    short *Get(int &numperiods)
    {
      int numframes = numperiods * periodsz;
      int framecounts[NUMCHANNELS+1];
      short *subdata[NUMCHANNELS+1];
      int total=0;
      int i;
      for (i=0; i<NUMCHANNELS; i++)
      {
        framecounts[i] = numframes;
        subdata[i] = channels[i].Get(framecounts[i]);
        total += framecounts[i];
      }
      framecounts[NUMCHANNELS]=numframes;
      subdata[NUMCHANNELS] = modulated_channel.Get(framecounts[NUMCHANNELS]);
      total += framecounts[NUMCHANNELS];
      if (!total)
      {
        // All channels are silent
        numperiods = 0;
        return 0;
      }
      int periodcounts[NUMCHANNELS+1];
      int max=0;
      for (i=0; i<NUMCHANNELS+1; i++)
      {
        periodcounts[i] = (framecounts[i] + (periodsz-1)) / periodsz;
        if (periodcounts[i] > max)
          max=periodcounts[i];
      }
      numperiods = max;
      int sz = numperiods * periodsz;
      memset(scratch32, 0, sz*2*sizeof(int));
      for (i=0; i<NUMCHANNELS; i++)
      {
        short *data = subdata[i];
        for (int j=0; j<2*framecounts[i]; j++)
          scratch32[j] += data[j];
      }
      for (int j=0; j<2*framecounts[NUMCHANNELS]; j++)
        scratch32[j] += subdata[NUMCHANNELS][j];

      for (int j=0; j<2*sz; j++)
        scratch16[j] = (scratch32[j]>32767)?32767:(scratch32[j]<-32768)?-32768:scratch32[j];
      return scratch16;
    }
    int GetIdleChannel(void) const
    {
      float maxv = 0.0;
      int maxi = -1;
      for (int j=0; j<NUMCHANNELS; j++)
      {
        float v = channels[j].GetCompletionFactor();
        if (v>maxv)
        {
          maxv = v;
          maxi = j;
        }
      }
      return maxi;
    }
    void Paste(const std::string &fname, int delay, bool looping=false)
    {
      if (looping)
      {
        modulated_channel.Paste(fname, delay, looping);
        return;
      }
      int channr = GetIdleChannel();
      if (channr == -1) return;
      channels[channr].Paste(fname, delay, looping);
    }
    void SetModulation(float f, float a)
    {
      modulated_channel.SetModulation(f,a);
    }

  protected:

    static const int NUMCHANNELS=7;
    Channel channels[NUMCHANNELS];
    ModulatedChannel modulated_channel;
    int   *scratch32;
    short *scratch16;
    int    modulation_numerator;
};


class SoundFeedModulated : public SoundFeed
{
  public:
    SoundFeedModulated(int psz) :
      SoundFeed(psz),
      clip(0),
      sz(0),
      idx(0.0),
      freq(1.0),
      ampl(1.0)
    {
      scratch = new short[100000];
    }
    virtual ~SoundFeedModulated()
    {
      delete scratch;
    }
    virtual void Paste(const std::string &fname, int delay, bool looping=false)
    {
      // get the mono soundclip.
      clip = SoundClip::Get(fname, 1);
      sz = clip->framecnt;
      idx = 0.0;
    }
    virtual short *Get(int &numperiods)
    {
      if (!clip)
      {
        numperiods=0;
        return 0;
      }
      int numframes = numperiods*periodsz;
      short *writer=scratch;
      for (int i=0; i<numframes; i++)
      {
        int v = (int) (ampl * clip->data[(int)idx]);
        if (v>32767)  v=32767;
        if (v<-32768) v=-32768;
        writer[0] = v;
        writer[1] = v;
        writer += 2;
        idx += freq;
        if (idx >= sz)
          idx -= sz;
      }
      return scratch;
    }
    void SetModulation(float f, float a)
    {
      freq = f;
      ampl = a;
    }
  protected:
    SoundClip *clip;
    int sz;
    float idx;
    short *scratch;
    float freq;
    float ampl;
};


class SoundFeedEngine : public SoundFeed
{
  public:
    SoundFeedEngine(int psz) :
      SoundFeed(psz),
      freq(1.0),
      ampl(1.0),
      sz(0),
      rps(1.0),
      cycletime((int)((1/rps) * 44100)),
      clip(0)
    {
      scratch = new short[100000];
      SetModulation(1,1);
    }
    ~SoundFeedEngine()
    {
      delete scratch;
    }
    virtual void Paste(const std::string &fname, int delay, bool looping=false)
    {
      // get the mono soundclip.
      clip = SoundClip::Get(fname, 1);
      sz = clip->framecnt;
      float step = cycletime/NUMCYLS;
      for (int i=0; i<NUMCYLS; i++)
      {
        framecounters[i]=sz;
        restartdelays[i]=step*i;
      }
    }
    virtual short *Get(int &numperiods)
    {
      if (!clip)
      {
        numperiods=0;
        return 0;
      }
      int numframes = numperiods*periodsz;
      short *writer=scratch;
      for (int i=0; i<numframes; i++)
      {
        int j;
        for (j=0; j<NUMCYLS; j++)
        {
          if (restartdelays[j]<=0.0)
          {
            restartdelays[j] += cycletime;
            framecounters[j] = 0;
          }
          restartdelays[j] -= freq;
        }
        int vals[2]={0,0};
        for (j=0; j<NUMCYLS; j++)
        {
          if (framecounters[j] < sz)
          {
            vals[j&1] += clip->data[framecounters[j]];
            framecounters[j] += 1;
          }
        }
        vals[0] = (int) (vals[0] * ampl);
        vals[1] = (int) (vals[1] * ampl);
        if (vals[0]>32767) vals[0]=32767;
        if (vals[0]<-32768) vals[0]=-32768;
        if (vals[1]>32767) vals[1]=32767;
        if (vals[1]<-32768) vals[1]=-32768;
        *writer++ = (short) vals[0];
        *writer++ = (short) vals[1];
      }
      return scratch;
    }
    void SetModulation(float f, float a)
    {
      freq = f;
      ampl = a;
    }
  protected:
    static const int NUMCYLS=8;
    int framecounters[NUMCYLS];
    float restartdelays[NUMCYLS];
    float freq;
    float ampl;
    int sz;
    float rps;		// revs per second
    int cycletime; 	// in nr of frames
    SoundClip *clip;
    short *scratch;
};


#endif