File: OpenALDiagnostics.cs

package info (click to toggle)
opentk 1.1.4c%2Bdfsg-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 68,640 kB
  • sloc: cs: 525,501; xml: 277,501; ansic: 3,597; makefile: 41
file content (632 lines) | stat: -rw-r--r-- 21,645 bytes parent folder | download | duplicates (4)
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
#region License
//
// The Open Toolkit Library License
//
// Copyright (c) 2006 - 2009 the Open Toolkit library.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights to 
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#endregion

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;

using OpenTK;
using OpenTK.Audio;
using OpenTK.Audio.OpenAL;

namespace Examples
{
    /// <summary>
    /// A text-based diagnosis program for OpenAL.
    /// The constructors will call the OpenAL commands, the Print() methods just show the information.
    /// </summary>
    [Example("OpenAL diagnostics", ExampleCategory.OpenAL, "Test", 0, true)]
    class OpenALDiagnostics
    {
        public static void checkForErrors()
        {
            {
                IntPtr device = Alc.GetContextsDevice(Alc.GetCurrentContext());
                AlcError error = Alc.GetError(device);

                if (error != AlcError.NoError)
                    Trace.WriteLine("ALC ERROR: (" + error + ")  " + Alc.GetString(device, (AlcGetString)error));
            }
            {
                ALError error = AL.GetError();
                if (error != ALError.NoError)
                    Trace.WriteLine("AL ERROR: (" + error + ") " + AL.GetErrorString(error));
            }
        }

        [STAThread]
        static void Main()
        {
            Trace.Listeners.RemoveAt(0);
            Trace.Listeners.Add(new ConsoleTraceListener());

            Trace.WriteLine("This application is currently running as " + (IntPtr.Size == 4 ? "x86" : "x64"));

            DeviceDiagnostic DevDiag = new DeviceDiagnostic();
            DevDiag.Print();
            DevDiag = null;

            using (AudioContext A = new AudioContext())
            {
                AlcDiagnostic AlcDiag = new AlcDiagnostic(Alc.GetContextsDevice(Alc.GetCurrentContext()));
                checkForErrors();
                AlcDiag.Print();
                AlcDiag = null;

                ALDiagnostic ALdiag = new ALDiagnostic(A);
                checkForErrors();
                ALdiag.Print();
                ALdiag = null;

                EfxDiagnostic EfxDiag = new EfxDiagnostic();
                checkForErrors();
                EfxDiag.Print();
                EfxDiag = null;

                XRamDiagnostic XRamDiag = new XRamDiagnostic();
                checkForErrors();
                XRamDiag.Print();
                XRamDiag = null;

                RecorderDiagnostic rec = new RecorderDiagnostic();
                rec.Print();
                rec = null;
            }

           // Trace.WriteLine("All done. Press Enter to exit.");
           // Console.ReadLine();
        }
    }

    class DeviceDiagnostic
    {
        #region Fields
        public readonly IList<string> AllPlaybackDevices;
        public readonly IList<string> AllRecordingDevices;

        public readonly string DefaultPlaybackDevice;
        public readonly string DefaultRecordingDevice;
        #endregion Fields

        public DeviceDiagnostic()
        {
            Trace.WriteLine("--- Device related errors ---");

            AllPlaybackDevices = AudioContext.AvailableDevices;
            AllRecordingDevices = AudioCapture.AvailableDevices;

            DefaultPlaybackDevice = AudioContext.DefaultDevice;
            DefaultRecordingDevice = AudioCapture.DefaultDevice;
        }

        public void Print()
        {
            Trace.WriteLine("--- Device related analysis ---");
            Trace.Indent();
            {
                Trace.WriteLine("Default playback device: " + DefaultPlaybackDevice);
                Trace.WriteLine("All known playback devices:");
                Trace.Indent();
                {
                    foreach (string s in AllPlaybackDevices)
                        Trace.WriteLine(s);
                }
                Trace.Unindent();

                Trace.WriteLine("Default recording device: " + DefaultRecordingDevice);
                Trace.WriteLine("All known recording devices:");
                Trace.Indent();
                {
                    foreach (string s in AllRecordingDevices)
                        Trace.WriteLine(s);
                }
                Trace.Unindent();
            }
            Trace.Unindent();
        }
    }

    class AlcDiagnostic
    {
        #region Fields
        private string[] Alc_Extension_C_Names = new string[]
          {
            "ALC_ENUMERATE_ALL_EXT",
            "ALC_ENUMERATION_EXT",
            "ALC_EXT_ASA",
            "ALC_EXT_ASA_DISTORTION",
            "ALC_EXT_ASA_ROGER_BEEP",
            "ALC_EXT_BRS_GAME_LICENSE_REQUIRED",
            "ALC_EXT_capture",
            "ALC_EXT_DEDICATED",
            "ALC_EXT_EFX",
          };

        public readonly int MajorVersion;
        public readonly int MinorVersion;
        public readonly int EfxMajorVersion;
        public readonly int EfxMinorVersion;
        public readonly int EfxMaxAuxiliarySends;
        public readonly string ExtensionString;
        public readonly Dictionary<string, bool> Extensions = new Dictionary<string, bool>();
        #endregion Fields

        public AlcDiagnostic(IntPtr dev)
        {
            Trace.WriteLine("--- Alc related errors ---");

            Alc.GetInteger(dev, AlcGetInteger.MajorVersion, 1, out MajorVersion);
            Alc.GetInteger(dev, AlcGetInteger.MinorVersion, 1, out MinorVersion);
            Alc.GetInteger(dev, AlcGetInteger.EfxMajorVersion, 1, out EfxMajorVersion);
            Alc.GetInteger(dev, AlcGetInteger.EfxMinorVersion, 1, out EfxMinorVersion);
            Alc.GetInteger(dev, AlcGetInteger.EfxMaxAuxiliarySends, 1, out EfxMaxAuxiliarySends);

            ExtensionString = Alc.GetString(dev, AlcGetString.Extensions);

            foreach (string s in Alc_Extension_C_Names)
                Extensions.Add(s, Alc.IsExtensionPresent(dev, s));
        }

        public void Print()
        {
            Trace.WriteLine("--- Alc related analysis ---");
            Trace.Indent();
            {
                Trace.WriteLine("Alc Version: " + MajorVersion + "." + MinorVersion);
                Trace.WriteLine("Efx Version: " + EfxMajorVersion + "." + EfxMinorVersion);
                Trace.WriteLine("Efx max. Auxiliary sends: " + EfxMaxAuxiliarySends);
                Trace.WriteLine("Alc Extension string: " + ExtensionString);

                Trace.WriteLine("Confirmed Alc Extensions:");
                Trace.Indent();
                {
                    foreach (KeyValuePair<string, bool> pair in Extensions)
                        Trace.WriteLine(pair.Key + ": " + pair.Value);
                }
                Trace.Unindent();
            }
            Trace.Unindent();
        }
    }

    class ALDiagnostic
    {
        #region Fields
        private string[] AL_Extension_Names = new string[]
        {
          "AL_EXT_ALAW",
          "AL_EXT_BFORMAT",
          "AL_EXT_double",
          "AL_EXT_EXPONENT_DISTANCE",
          "AL_EXT_float32",
          "AL_EXT_FOLDBACK",
          "AL_EXT_IMA4",
          "AL_EXT_LINEAR_DISTANCE",
          "AL_EXT_MCFORMATS",
          "AL_EXT_mp3",
          "AL_EXT_MULAW",
          "AL_EXT_OFFSET",
          "AL_EXT_vorbis",
          "AL_LOKI_quadriphonic",
          "EAX-RAM",
          "EAX",
          "EAX1.0",
          "EAX2.0",
          "EAX3.0",
          "EAX3.0EMULATED",
          "EAX4.0",
          "EAX4.0EMULATED",
          "EAX5.0"
        };

        public readonly Dictionary<string, bool> Extensions = new Dictionary<string, bool>();

        public readonly string DeviceName;
        public readonly float SpeedOfSound;
        public readonly string ExtensionString;
        public readonly string Renderer;
        public readonly string Vendor;
        public readonly string Version;
        public readonly ALDistanceModel DistanceModel;

        const uint MaxSourcesLimit = 128;
        public readonly uint MaxSources;
        #endregion Fields

        public ALDiagnostic(AudioContext ac)
        {
            Trace.WriteLine("--- AL related errors ---");

            DeviceName = ac.CurrentDevice;

            ExtensionString = AL.Get(ALGetString.Extensions);
            Renderer = AL.Get(ALGetString.Renderer);
            Vendor = AL.Get(ALGetString.Vendor);
            Version = AL.Get(ALGetString.Version);

            SpeedOfSound = AL.Get(ALGetFloat.SpeedOfSound);
            DistanceModel = AL.GetDistanceModel();

            foreach (string s in AL_Extension_Names)
                Extensions.Add(s, AL.IsExtensionPresent(s));

            AL.GetError(); // clear it, need this for the source counting to work properly
            uint[] dummy_sources = new uint[MaxSourcesLimit];
            for (MaxSources = 0; MaxSources < MaxSourcesLimit; MaxSources++)
            {
                AL.GenSource(out dummy_sources[MaxSources]);
                if (AL.GetError() != ALError.NoError)
                    break;
            }

            for (int i = 0; i < MaxSources; i++)
                AL.DeleteSource(ref dummy_sources[i]);
        }

        public void Print()
        {
            Trace.WriteLine("--- AL related analysis ---");
            Trace.Indent();
            {
                Trace.WriteLine("Used Device: "+DeviceName);
                Trace.WriteLine("AL Renderer: " + Renderer);
                Trace.WriteLine("AL Vendor: " + Vendor);
                Trace.WriteLine("AL Version: " + Version);

                Trace.WriteLine("AL Speed of sound: " + SpeedOfSound);
                Trace.WriteLine("AL Distance Model: " + DistanceModel.ToString());
                Trace.WriteLine("AL Maximum simultanous Sources: " + MaxSources);

                Trace.WriteLine("AL Extension string: " + ExtensionString);
                Trace.WriteLine("Confirmed AL Extensions:");
                Trace.Indent();
                {
                    foreach (KeyValuePair<string, bool> pair in Extensions)
                        Trace.WriteLine(pair.Key + ": " + pair.Value);
                }
                Trace.Unindent();
            }
            Trace.Unindent();
        }
    }

    class EfxDiagnostic
    {
        #region Fields
        private EfxEffectType[] EffectNames = new EfxEffectType[]
          { 
            EfxEffectType.Autowah,
            EfxEffectType.Chorus,
            EfxEffectType.Compressor,
            EfxEffectType.Distortion,
            EfxEffectType.EaxReverb,
            EfxEffectType.Echo,
            EfxEffectType.Equalizer,
            EfxEffectType.Flanger,
            EfxEffectType.FrequencyShifter, 
            EfxEffectType.PitchShifter,
            EfxEffectType.Reverb,
            EfxEffectType.RingModulator,
            EfxEffectType.VocalMorpher,
          };

        private EfxFilterType[] FilterNames = new EfxFilterType[] 
          {
            EfxFilterType.Bandpass,
            EfxFilterType.Highpass,
            EfxFilterType.Lowpass,
          };

        public readonly Dictionary<string, bool> Effects = new Dictionary<string, bool>();
        public readonly Dictionary<string, bool> Filters = new Dictionary<string, bool>();

        const uint MaxAuxiliaryEffectSlotsLimit = 4;
        public readonly uint MaxAuxiliaryEffectSlots;
        #endregion Fields

        public EfxDiagnostic()
        {
            Trace.WriteLine("--- Efx related errors ---");

            EffectsExtension Efx = new EffectsExtension();
            Trace.Assert(Efx.IsInitialized);

            #region Test for available Effects
            uint effect;
            Efx.GenEffect(out effect);
            AL.GetError();
            foreach (EfxEffectType e in EffectNames)
            {
                Efx.BindEffect(effect, e);
                Effects.Add(e.ToString(), AL.GetError() == ALError.NoError);
            }
            Efx.DeleteEffect(ref effect);
            #endregion Test for available Effects

            #region Test for available Filters
            uint filter;
            Efx.GenFilter(out filter);
            AL.GetError();
            foreach (EfxFilterType f in FilterNames)
            {
                Efx.Filter(filter, EfxFilteri.FilterType, (int)f);
                Filters.Add(f.ToString(), AL.GetError() == ALError.NoError);
            }
            Efx.DeleteFilter(ref filter);
            #endregion Test for available Filters

            AL.GetError();
            uint[] dummy_slots = new uint[MaxAuxiliaryEffectSlotsLimit];
            for (MaxAuxiliaryEffectSlots = 0; MaxAuxiliaryEffectSlots < MaxAuxiliaryEffectSlotsLimit; MaxAuxiliaryEffectSlots++)
            {
                Efx.GenAuxiliaryEffectSlot(out dummy_slots[MaxAuxiliaryEffectSlots]);
                if (AL.GetError() != ALError.NoError)
                    break;
            }
            for (uint i = 0; i < MaxAuxiliaryEffectSlots; i++)
                Efx.DeleteAuxiliaryEffectSlot(ref dummy_slots[i]);
        }

        public void Print()
        {
            Trace.WriteLine("--- Efx related analysis ---");
            Trace.Indent();
            {
                Trace.WriteLine("Efx Effects supported:");
                Trace.Indent();
                {
                    foreach (KeyValuePair<string, bool> pair in Effects)
                        Trace.WriteLine(pair.Key + ": " + pair.Value);
                }
                Trace.Unindent();

                Trace.WriteLine("Efx Filters supported:");
                Trace.Indent();
                {
                    foreach (KeyValuePair<string, bool> pair in Filters)
                        Trace.WriteLine(pair.Key + ": " + pair.Value);
                }
                Trace.Unindent();

                Trace.WriteLine("Efx max. Auxiliary Effect Slots: " + MaxAuxiliaryEffectSlots);
            }
            Trace.Unindent();
        }
    }

    class XRamDiagnostic
    {
        #region Fields
        private XRamExtension.XRamStorage[] storagemodes = new XRamExtension.XRamStorage[]
          {
            XRamExtension.XRamStorage.Hardware,
            XRamExtension.XRamStorage.Accessible,
            XRamExtension.XRamStorage.Automatic,
          };

        public readonly bool XRamFound;
        public readonly int RamTotal;
        public readonly int RamFree;
        public readonly Dictionary<string, bool> BufferModes = new Dictionary<string, bool>();
        #endregion Fields

        public XRamDiagnostic()
        {
            Trace.WriteLine("--- X-RAM related errors ---");

            XRamExtension XRam = new XRamExtension();
            if (XRam.IsInitialized)
            {
                XRamFound = true;

                RamTotal = XRam.GetRamSize;
                RamFree = XRam.GetRamFree;

                uint buffer;
                AL.GenBuffer(out buffer);

                foreach (XRamExtension.XRamStorage m in storagemodes)
                {
                    XRam.SetBufferMode(1, ref buffer, m);
                    BufferModes.Add(m.ToString(), m == XRam.GetBufferMode(ref buffer));
                }

                AL.DeleteBuffer(ref buffer);
            }
            else
                XRamFound = false;
        }

        public void Print()
        {
            Trace.WriteLine("--- X-RAM related analysis ---");

            if (XRamFound)
            {
                Trace.Indent(); // *
                Trace.WriteLine("X-RAM extension found.");
            }
            else
            {
                Trace.Indent();
                {
                    Trace.WriteLine("X-RAM extension is not available, skipping test.");
                }
                Trace.Unindent();
                return;
            }

            Trace.WriteLine("Ram status (free/total) in bytes: " + RamFree + "/" + RamTotal);

            Trace.WriteLine("Available buffer modes:");
            Trace.Indent();
            {
                foreach (KeyValuePair<string, bool> pair in BufferModes)
                    Trace.WriteLine(pair.Key + ": " + pair.Value);
            }
            Trace.Unindent();

            Trace.Unindent(); // *
        }
    }

    class RecorderDiagnostic
    {
        #region Fields
        bool IsDeviceAvailable;
        bool BufferContentsAllZero;
        short MaxSample = short.MinValue;
        short MinSample = short.MaxValue;

        private AudioCapture r;
        Dictionary<string, AlcError> Errorlist = new Dictionary<string, AlcError>();

        string DeviceName;

        #endregion Fields

        private void CheckRecorderError(string location)
        {
            AlcError err = r.CurrentError;
            if (err != AlcError.NoError)
                Errorlist.Add(location, err);
        }

        public RecorderDiagnostic()
        {
            Trace.WriteLine("--- AudioCapture related errors ---");
            IsDeviceAvailable = false;

            try
            {
                r = new AudioCapture(AudioCapture.DefaultDevice, 16000, ALFormat.Mono16, 4096);
            }
            catch (AudioDeviceException ade)
            {
                Trace.WriteLine("AudioCapture Exception caught: " + ade.Message);
                return;
            }
            IsDeviceAvailable = true;
            DeviceName = r.CurrentDevice;
            CheckRecorderError("Alc.CaptureOpenDevice");

            r.Start();
            CheckRecorderError("Alc.CaptureStart");
            Thread.Sleep(100);
            r.Stop();
            CheckRecorderError("Alc.CaptureStop");

            byte[] Buffer = new byte[8192];

            Thread.Sleep(10);  // Wait for a few samples to become available.
            int SamplesBefore = r.AvailableSamples;

            CheckRecorderError("Alc.GetInteger(...CaptureSamples...)");
            r.ReadSamples(Buffer, (SamplesBefore > 4096 ? 4096 : SamplesBefore));
            CheckRecorderError("Alc.CaptureSamples");

            int SamplesCaptured = SamplesBefore - r.AvailableSamples;

            uint ZeroCounter = 0;
            for (int i = 0; i < SamplesCaptured * 2; i++)
            {
                if (Buffer[i] == 0)
                    ZeroCounter++;
            }

            for (int i = 0; i < SamplesCaptured; i++)
            {
                short sample = BitConverter.ToInt16(Buffer, i * 2);
                if (sample > MaxSample)
                    MaxSample = sample;
                if (sample < MinSample)
                    MinSample = sample;
            }

            if (ZeroCounter < SamplesCaptured * 2 && SamplesCaptured > 0)
                BufferContentsAllZero = false;
            else
                BufferContentsAllZero = true;

            r.Dispose();
            CheckRecorderError("Alc.CaptureCloseDevice");

            // no playback test needed due to Parrot test app.
            /*
            uint buf;
            AL.GenBuffer(out buf);
            AL.BufferData(buf, ALFormat.Mono16, BufferPtr, SamplesCaptured * 2, 16000);
            uint src;
            AL.GenSource(out src);
            AL.BindBufferToSource(src, buf);
            AL.Listener(ALListenerf.Gain, 16.0f);
            AL.SourcePlay(src);
            while (AL.GetSourceState(src) == ALSourceState.Playing)
            {
                Thread.Sleep(0);
            }
            AL.SourceStop(src);

            AL.DeleteSource(ref src);
            AL.DeleteBuffer(ref buf);
            */
        }

        public void Print()
        {
            Trace.WriteLine("--- AudioCapture related analysis ---");

            if (!IsDeviceAvailable)
            {
                Trace.WriteLine("Capture Device could not be initlialized (no microphone plugged into the jack?). Skipping test.");
                return;
            }

            Trace.Indent();
            {
                Trace.WriteLine("Using capture device: " + DeviceName);
                if (Errorlist.Count > 0)
                {
                    Trace.WriteLine("Found Alc Errors:");
                    Trace.Indent();
                    {
                        foreach (KeyValuePair<string, AlcError> pair in Errorlist)
                            Trace.WriteLine(pair.Key + ": " + pair.Value);
                    }
                    Trace.Unindent();


                }
                Trace.WriteLine("Buffer contents after capture was all 0's: " + BufferContentsAllZero);
                Trace.WriteLine("Sample min/max in recorded data: " + MinSample + " / " + MaxSample);
            }
            Trace.Unindent();
        }
    }
}