This file is indexed.

/usr/share/ecere/extras/audio/mixer.ec is in ecere-extras 0.44.09.9-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
public import "ecere"
import "audio"

#include <string.h>

// public define AUDIO_BUFFER_SIZE = 21024;
public define AUDIO_BUFFER_SIZE = 48000;

struct WAVEHDR
{
   byte   format[4]           __attribute__((packed));
   uint32 f_len               __attribute__((packed));
   byte   wave_fmt[8]         __attribute__((packed));
   uint32 fmt_len             __attribute__((packed));
   uint16 fmt_tag             __attribute__((packed));
   uint16 channel             __attribute__((packed));
   uint32 samples_per_sec     __attribute__((packed));
   uint32 avg_bytes_per_sec   __attribute__((packed));
   uint16 blk_align           __attribute__((packed));
   uint16 bits_per_sample     __attribute__((packed));
   byte   data[4]             __attribute__((packed));
   uint32 data_len            __attribute__((packed));
};

public class Sound
{
public:
   property String fileName { set { Load(value); } }

   int frequency, bits, channels, length;
   byte * data;

   ~Sound() { delete data; }

   bool Load(char * fileName)
   {
      bool result = false;
      WAVEHDR header;
      File f = FileOpen(fileName, read);
      if(f)
      {
         if(f.Read(header,sizeof(WAVEHDR),1) && !memcmp(header.format,"RIFF",4) && !memcmp(header.wave_fmt,"WAVEfmt ",8))
         {
            data = new byte[header.data_len];
            if(data)
            {
               int c = f.Read(data, 1, header.data_len);
               if(c)
               {
                  frequency = header.samples_per_sec;
                  bits = header.bits_per_sample;
                  channels = header.channel;
                  length = Min(header.data_len, c);
                  if(c < header.data_len)
                     length -= 2048;

                  if(bits == 16)
                     length /= 2;
                  result = true;
               }
            }
         }
         delete f;
      }
      return result;
   }
}

public class Voice
{
public:
   Sound sound;
   double volume, balance, pitch;
   int pos;
   bool looped;
   int loopStart, loopEnd;
}

public class Mixer
{
   AudioSpec spec { };
   Mutex mutex { };

   ~Mixer()
   {
      CloseAudio();
      while(voices.count)
         voices.Delete((void *)voices.array);
   }

   void AudioCallback(byte *stream, int lenToFill)
   {
      static byte buffer[AUDIO_BUFFER_SIZE];
      static float fBuffer[AUDIO_BUFFER_SIZE];
      int c;
      int numSamples = (bits == 16) ? (lenToFill / 2) : lenToFill;
      memset(fBuffer, 0, sizeof(float) * numSamples);
      mutex.Wait();
      for(v : voices)
      {
         Sound sound = v.sound;
         float volume = (float)v.volume;
         float balance = (float)v.balance;
         int freq = (int)(sound.frequency * v.pitch);
         int chn = sound.channels;
         if(sound.bits == 16)
         {
            short * sBuffer = sound.data;
            int se = 0;
            int s = v.pos;
            short sampleL = sBuffer[s];
            short sampleR = (chn == 2) ? sBuffer[s+1] : sampleL;

            for(c = 0; c < numSamples; c++)
            {
               float left  = sampleL * volume / 32767.0f;
               float right = sampleR * volume / 32767.0f;
               if(balance)
               {
                  float v = balance * ((balance < 0) ? right : left);
                  right += v;
                  left  -= v;
               }
               if(channels == 2)
               {
                  fBuffer[c++] += left;
                  fBuffer[c]   += right;
               }
               else
                  fBuffer[c] += (left + right)/2;

               se += freq;
               if(se >= frequency)
               {
                  do
                  {
                     s += chn;
                     se -= frequency;
                  } while(se >= frequency);
                  if(s < sound.length)
                  {
                     sampleL = sBuffer[s];
                     sampleR = (chn == 2) ? sBuffer[s+1] : sampleL;
                  }
                  else
                     break;
               }
            }
            v.pos = s;
         }
         else if(sound.bits == 8)
         {
            byte * sBuffer = sound.data;
            int se = 0;
            int s = v.pos;
            byte sampleL = sBuffer[s];
            byte sampleR = (chn == 2) ? sBuffer[s+1] : sampleL;
            bool looped = v.looped;
            int loopStart = v.loopStart, loopEnd = v.loopEnd;

            for(c = 0; c < numSamples; c++)
            {
               float left  = (sampleL - 127) * volume / 127.0f;
               float right = (sampleR - 127) * volume / 127.0f;;
               if(balance)
               {
                  float v = balance * ((balance < 0) ? right : left);
                  right += v;
                  left  -= v;
               }
               if(channels == 2)
               {
                  fBuffer[c++] += left;
                  fBuffer[c]   += right;
               }
               else
                  fBuffer[c] += (left + right)/2;

               se += freq;
               if(se >= frequency)
               {
                  do
                  {
                     s += chn;
                     if(looped && s >= loopEnd)
                        s = loopStart;
                     se -= frequency;
                  } while(se >= frequency);
                  if(s < sound.length)
                  {
                     sampleL = sBuffer[s];
                     sampleR = (chn == 2) ? sBuffer[s+1] : sampleL;
                  }
                  else
                     break;
               }
            }
            v.pos = s;
         }
      }
      if(bits == 16)
      {
         for(c = 0; c < numSamples; c++)
         {
            int i = (int)(fBuffer[c] * 32767);
            if(i > 32767) i = 32767; else if(i < -32768) i = -32768;
            ((short *)buffer)[c] = (short)i;
         }
      }
      else
      {
         for(c = 0; c < numSamples; c++)
         {
            int i = (int)(fBuffer[c] * 127) + 127;
            if(i > 255) i = 255; else if(i < 0) i = 0;
            buffer[c] = (byte)i;
         }
      }
      memcpy(stream, buffer, lenToFill);
      {
         bool removed;
         do
         {
            removed = false;
            c = 0;
            for(v : voices)
            {
               if(v.pos >= v.sound.length)
               {
                  removed = true;
                  voices.Delete((void *)(voices.array + c));
                  break;
               }
               c++;
            }
         } while(removed);
      }
      mutex.Release();
   }

public:
   void * systemHandle;
   int frequency;
   int bits;
   int channels;
   Array<Voice> voices { };

   frequency = 44100;
   bits = 16;
   channels = 2;

   property void * systemHandle { set { systemHandle = value; Init(); } }

   bool Init()
   {
      AudioSpec wantedSpec
      {
         freq = frequency;
         bits = bits;
         channels = channels;
         samples = AUDIO_BUFFER_SIZE;
         callback = AudioCallback;
         userdata = this;
         windowHandle = systemHandle;
         volume = 100;
      };

      if(!OpenAudio(wantedSpec, spec))
      {
         MessageBox { contents = "OpenAudio failed" }.Modal();
         return false;
      }
      PauseAudio(0);
      return true;
   }

   Voice Play(Sound sound, double volume, double balance, double pitch)
   {
      Voice voice { sound, volume, balance, pitch };
      mutex.Wait();
      voices.Add(voice);
      incref voice;
      mutex.Release();
      return voice;
   }

   void Wait()
   {
      mutex.Wait();
   }

   void Release()
   {
      mutex.Release();
   }

   void PlayInVoice(Voice voice, Sound sound, double volume, double balance, double pitch)
   {
      bool found = false;
      mutex.Wait();
      for(v : voices)
      {
         if(v == voice)
         {
            found = true;
            break;
         }
      }
      if(!found)
      {
         voices.Add(voice);
         incref voice;
      }
      voice.sound = sound;
      voice.volume = volume;
      voice.balance = balance;
      voice.pitch = pitch;
      voice.pos = 0;
      mutex.Release();
   }
}