fna-workbench

fna-workbench Git Source Tree


Root/src/Audio/SoundEffect.cs

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
#region License
/* FNA - XNA4 Reimplementation for Desktop Platforms
 * Copyright 2009-2016 Ethan Lee and the MonoGame Team
 *
 * Released under the Microsoft Public License.
 * See LICENSE for details.
 */
#endregion
 
#region Using Statements
using System;
using System.IO;
using System.Collections.Generic;
#endregion
 
namespace Microsoft.Xna.Framework.Audio
{
    public sealed class SoundEffect : IDisposable
    {
        #region Public Properties
 
        public TimeSpan Duration
        {
            get
            {
                return INTERNAL_buffer.Duration;
            }
        }
 
        public bool IsDisposed
        {
            get;
            private set;
        }
 
        public string Name
        {
            get;
            set;
        }
 
        #endregion
 
        #region Public Static Properties
 
        // FIXME: This should affect all sounds! alListener? -flibit
        private static float INTERNAL_masterVolume = 1.0f;
        public static float MasterVolume
        {
            get
            {
                return INTERNAL_masterVolume;
            }
            set
            {
                INTERNAL_masterVolume = value;
            }
        }
 
        // FIXME: How does this affect OpenAL? -flibit
        private static float INTERNAL_distanceScale = 1.0f;
        public static float DistanceScale
        {
            get
            {
                return INTERNAL_distanceScale;
            }
            set
            {
                if (value <= 0.0f)
                {
                    throw new ArgumentOutOfRangeException("value of DistanceScale");
                }
                INTERNAL_distanceScale = value;
            }
        }
 
        // FIXME: How does this affect OpenAL? -flibit
        private static float INTERNAL_dopplerScale = 1.0f;
        public static float DopplerScale
        {
            get
            {
                return INTERNAL_dopplerScale;
            }
            set
            {
                if (value <= 0.0f)
                {
                    throw new ArgumentOutOfRangeException("value of DopplerScale");
                }
                INTERNAL_dopplerScale = value;
            }
        }
 
        // FIXME: How does this affect OpenAL? -flibit
        private static float INTERNAL_speedOfSound = 343.5f;
        public static float SpeedOfSound
        {
            get
            {
                return INTERNAL_speedOfSound;
            }
            set
            {
                INTERNAL_speedOfSound = value;
            }
        }
 
        #endregion
 
        #region Internal Variables
 
        internal List<WeakReference> Instances = new List<WeakReference>();
        internal IALBuffer INTERNAL_buffer;
 
        #endregion
 
        #region Public Constructors
 
        public SoundEffect(
            byte[] buffer,
            int sampleRate,
            AudioChannels channels
        ) {
            INTERNAL_buffer = AudioDevice.GenBuffer(
                buffer,
                (uint) sampleRate,
                (uint) channels,
                0,
                0,
                false,
                1
            );
        }
 
        public SoundEffect(
            byte[] buffer,
            int offset,
            int count,
            int sampleRate,
            AudioChannels channels,
            int loopStart,
            int loopLength
        ) {
            byte[] sendBuf;
            if (offset != 0 || count != buffer.Length)
            {
                // I kind of hate this. -flibit
                sendBuf = new byte[count];
                Array.Copy(buffer, offset, sendBuf, 0, count);
            }
            else
            {
                sendBuf = buffer;
            }
 
            INTERNAL_buffer = AudioDevice.GenBuffer(
                sendBuf,
                (uint) sampleRate,
                (uint) channels,
                (uint) loopStart,
                (uint) (loopStart + loopLength),
                false,
                1
            );
        }
 
        #endregion
 
        #region Internal Constructors
 
        internal SoundEffect(Stream s)
        {
            INTERNAL_loadAudioStream(s);
        }
 
        internal SoundEffect(
            string name,
            byte[] buffer,
            uint sampleRate,
            uint channels,
            uint loopStart,
            uint loopLength,
            bool isADPCM,
            uint formatParameter
        ) {
            Name = name;
            INTERNAL_buffer = AudioDevice.GenBuffer(
                buffer,
                sampleRate,
                channels,
                loopStart,
                loopStart + loopLength,
                isADPCM,
                formatParameter
            );
        }
 
        #endregion
 
        #region Destructor
 
        ~SoundEffect()
        {
            Dispose();
        }
 
        #endregion
 
        #region Public Dispose Method
 
        public void Dispose()
        {
            if (!IsDisposed)
            {
                /* FIXME: Is it ironic that we're generating
                 * garbage with ToArray while cleaning up after
                 * the program's leaks?
                 * -flibit
                 */
                foreach (WeakReference instance in Instances.ToArray())
                {
                    object target = instance.Target;
                    if (target != null)
                    {
                        (target as IDisposable).Dispose();
                    }
                }
                Instances.Clear();
                if (INTERNAL_buffer != null)
                {
                    AudioDevice.ALDevice.DeleteBuffer(INTERNAL_buffer);
                }
                IsDisposed = true;
            }
        }
 
        #endregion
 
        #region Additional SoundEffect/SoundEffectInstance Creation Methods
 
        public SoundEffectInstance CreateInstance()
        {
            return new SoundEffectInstance(this);
        }
 
        public static SoundEffect FromStream(Stream stream)
        {
            return new SoundEffect(stream);
        }
 
        #endregion
 
        #region Public Play Methods
 
        public bool Play()
        {
            // FIXME: Perhaps MasterVolume should be applied to alListener? -flibit
            return Play(MasterVolume, 0.0f, 0.0f);
        }
 
        public bool Play(float volume, float pitch, float pan)
        {
            SoundEffectInstance instance = CreateInstance();
            instance.Volume = volume;
            instance.Pitch = pitch;
            instance.Pan = pan;
            instance.Play();
            if (instance.State != SoundState.Playing)
            {
                // Ran out of AL sources, probably.
                instance.Dispose();
                return false;
            }
            AudioDevice.InstancePool.Add(instance);
            return true;
        }
 
        #endregion
 
        #region Private WAV Loading Method
 
        private void INTERNAL_loadAudioStream(Stream s)
        {
            byte[] data;
            uint sampleRate = 0;
            uint numChannels = 0;
            bool isADPCM = false;
            uint formatParameter = 0;
 
            using (BinaryReader reader = new BinaryReader(s))
            {
                // RIFF Signature
                string signature = new string(reader.ReadChars(4));
                if (signature != "RIFF")
                {
                    throw new NotSupportedException("Specified stream is not a wave file.");
                }
 
                reader.ReadUInt32(); // Riff Chunk Size
 
                string wformat = new string(reader.ReadChars(4));
                if (wformat != "WAVE")
                {
                    throw new NotSupportedException("Specified stream is not a wave file.");
                }
 
                // WAVE Header
                string format_signature = new string(reader.ReadChars(4));
                while (format_signature != "fmt ")
                {
                    reader.ReadBytes(reader.ReadInt32());
                    format_signature = new string(reader.ReadChars(4));
                }
 
                int format_chunk_size = reader.ReadInt32();
 
                // Header Information
                uint audio_format = reader.ReadUInt16();    // 2
                numChannels = reader.ReadUInt16();      // 4
                sampleRate = reader.ReadUInt32();       // 8
                reader.ReadUInt32();                // 12, Byte Rate
                ushort blockAlign = reader.ReadUInt16();    // 14, Block Align
                ushort bitDepth = reader.ReadUInt16();      // 16, Bits Per Sample
 
                if (audio_format == 1)
                {
                    System.Diagnostics.Debug.Assert(bitDepth == 8 || bitDepth == 16);
                    formatParameter = (uint) (bitDepth / 16); // 1 for 16, 0 for 8
                }
                else if (audio_format != 2)
                {
                    isADPCM = true;
                    formatParameter = (((blockAlign / numChannels) - 6) * 2);
                }
                else
                {
                    throw new NotSupportedException("Wave format is not supported.");
                }
 
                // Reads residual bytes
                if (format_chunk_size > 16)
                {
                    reader.ReadBytes(format_chunk_size - 16);
                }
 
                // data Signature
                string data_signature = new string(reader.ReadChars(4));
                while (data_signature.ToLowerInvariant() != "data")
                {
                    reader.ReadBytes(reader.ReadInt32());
                    data_signature = new string(reader.ReadChars(4));
                }
                if (data_signature != "data")
                {
                    throw new NotSupportedException("Specified wave file is not supported.");
                }
 
                int waveDataLength = reader.ReadInt32();
                data = reader.ReadBytes(waveDataLength);
            }
 
            INTERNAL_buffer = AudioDevice.GenBuffer(
                data,
                sampleRate,
                numChannels,
                0,
                0,
                isADPCM,
                formatParameter
            );
        }
 
        #endregion
 
        #region Public Static Methods
 
        public static TimeSpan GetSampleDuration(
            int sizeInBytes,
            int sampleRate,
            AudioChannels channels
        ) {
            sizeInBytes /= 2; // 16-bit PCM!
            int ms = (int) (
                (sizeInBytes / (int) channels) /
                (sampleRate / 1000.0f)
            );
            return new TimeSpan(0, 0, 0, 0, ms);
        }
 
        public static int GetSampleSizeInBytes(
            TimeSpan duration,
            int sampleRate,
            AudioChannels channels
        ) {
            return (int) (
                duration.TotalSeconds *
                sampleRate *
                (int) channels *
                2 // 16-bit PCM!
            );
        }
 
        #endregion
    }
}

Archive Download this file

Branches

Number of commits:
Page rendered in 0.17921s using 11 queries.