bitskull's picture

AudioContext constructor fail

Hi,
I have problem with audio context.
Constructor fails with "The type initializer for 'OpenTK.Audio.AudioContext' threw an exception."

            try
            {
                // writes "OpenAL Soft" to console
                OpenTK.ContextHandle ch = OpenTK.Audio.Alc.GetCurrentContext();
                IntPtr device = Alc.GetContextsDevice(ch);
                string s = Alc.GetString(device, AlcGetString.DefaultDeviceSpecifier);                
                Console.Out.WriteLine("audio device: " + s );
 
                // exception here
                AC = new AudioContext();
            }
            catch (AudioException e)
            {
                Console.Out.WriteLine(e.Message);
            }

I tried to run OpenAL Diagnostics from Examples and it fails too - screenshot included.

I tried two releases - opentk-2010-10-06.exe & opentk-2014-01-03.exe - same error.
I have Windows 8.1 x64.
My default playback device is "VIA High Definition Audio".

Any ideas?

Inline Images
OpenAL Diag

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

Can you please file a bug report at https://github.com/opentk/opentk/issues?

Have you installed OpenAL drivers or are you using OpenAL Soft (i.e. openal32.dll from opentk/Dependencies/)? In the latter case, double check that you are using the correct x86 or x64 version of the dll.

bitskull's picture

I use provided dependencies for x64.

I was able to fix this problem by wrapping Alc.GetString into try catch:

        public static IList<string> GetString(IntPtr device, AlcGetStringList param)
        {
            List<string> result = new List<string>();
 
            try
            {
                IntPtr t = GetStringPrivate(IntPtr.Zero, (AlcGetString)param);
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                byte b;
                int offset = 0;
                do
                {
                    b = Marshal.ReadByte(t, offset++);
                    if (b != 0)
                        sb.Append((char)b);
                    if (b == 0)
                    {
                        result.Add(sb.ToString());
                        if (Marshal.ReadByte(t, offset) == 0) // offset already properly increased through ++
                            break; // 2x null
                        else
                            sb.Remove(0, sb.Length); // 1x null
                    }
                } while (true);
            }
            catch (Exception e)
            {
                System.Console.Out.WriteLine(e.Message);
            }</b>
 
            return (IList<string>)result;
        }

OpenAL diagnostics result with my fix - there are errors related to capture device:
(sometimes I connect BT headset but it is not connected now - maybe it is conencted with this error)

This application is currently running as x64
--- Device related errors ---
Enumerating audio devices.
    Found playback devices:
    Speakers (VIA High Definition Audio)
    Speakers (VIA High Definition Audio)
    Default playback device: Speakers (VIA High Definition Audio)
    Found recording devices:
    Default recording device: 
--- Device related analysis ---
    Default playback device: Speakers (VIA High Definition Audio)
    All known playback devices:
        Speakers (VIA High Definition Audio)
        Speakers (VIA High Definition Audio)
    Default recording device: 
    All known recording devices:
--- Alc related errors ---
--- Alc related analysis ---
    Alc Version: 1.1
    Efx Version: 1.0
    Efx max. Auxiliary sends: 4
    Alc Extension string: ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_DEDICATED ALC_EXT_disconnect ALC_EXT_EFX ALC_EXT_thread_local_context ALC_SOFT_loopback
    Confirmed Alc Extensions:
        ALC_ENUMERATE_ALL_EXT: True
        ALC_ENUMERATION_EXT: True
        ALC_EXT_ASA: False
        ALC_EXT_ASA_DISTORTION: False
        ALC_EXT_ASA_ROGER_BEEP: False
        ALC_EXT_BRS_GAME_LICENSE_REQUIRED: False
        ALC_EXT_capture: True
        ALC_EXT_DEDICATED: True
        ALC_EXT_EFX: True
--- AL related errors ---
--- AL related analysis ---
    Used Device: OpenAL Soft
    AL Renderer: OpenAL Soft
    AL Vendor: OpenAL Community
    AL Version: 1.1 ALSOFT 1.15.1
    AL Speed of sound: 343,3
    AL Distance Model: InverseDistanceClamped
    AL Maximum simultanous Sources: 128
    AL Extension string: AL_EXT_ALAW AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_MULAW AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET AL_EXT_source_distance_model AL_LOKI_quadriphonic AL_SOFT_buffer_samples AL_SOFT_buffer_sub_data AL_SOFTX_deferred_updates AL_SOFT_direct_channels AL_SOFT_loop_points AL_SOFT_source_latency
    Confirmed AL Extensions:
        AL_EXT_ALAW: True
        AL_EXT_BFORMAT: False
        AL_EXT_double: True
        AL_EXT_EXPONENT_DISTANCE: True
        AL_EXT_float32: True
        AL_EXT_FOLDBACK: False
        AL_EXT_IMA4: True
        AL_EXT_LINEAR_DISTANCE: True
        AL_EXT_MCFORMATS: True
        AL_EXT_mp3: False
        AL_EXT_MULAW: True
        AL_EXT_OFFSET: True
        AL_EXT_vorbis: False
        AL_LOKI_quadriphonic: True
        EAX-RAM: False
        EAX: False
        EAX1.0: False
        EAX2.0: False
        EAX3.0: False
        EAX3.0EMULATED: False
        EAX4.0: False
        EAX4.0EMULATED: False
        EAX5.0: False
--- Efx related errors ---
--- Efx related analysis ---
    Efx Effects supported:
        Autowah: False
        Chorus: False
        Compressor: False
        Distortion: False
        EaxReverb: True
        Echo: True
        Equalizer: False
        Flanger: False
        FrequencyShifter: False
        PitchShifter: False
        Reverb: True
        RingModulator: True
        VocalMorpher: False
    Efx Filters supported:
        Bandpass: False
        Highpass: False
        Lowpass: True
    Efx max. Auxiliary Effect Slots: 4
--- X-RAM related errors ---
--- X-RAM related analysis ---
    X-RAM extension is not available, skipping test.
--- AudioCapture related errors ---
The handle returned by Alc.CaptureOpenDevice is null.
Alc Error: InvalidValue: One of the parameters has an invalid value.
Device Name: 
Capture frequency: 16000
Buffer format: Mono16
Buffer Size: 4096
The handle returned by Alc.CaptureOpenDevice is null.
Alc Error: InvalidValue: One of the parameters has an invalid value.
Device Name: IntPtr.Zero
Capture frequency: 16000
Buffer format: Mono16
Buffer Size: 4096
The handle returned by Alc.CaptureOpenDevice is null.
Alc Error: InvalidValue: One of the parameters has an invalid value.
Device Name: 
Capture frequency: 16000
Buffer format: Mono16
Buffer Size: 4096
AudioCapture Exception caught: All attempts to open capture devices returned IntPtr.Zero. See debug log for verbose list.
--- AudioCapture related analysis ---
Capture Device could not be initlialized (no microphone plugged into the jack?). Skipping test.
the Fiddler's picture

Thanks, can you please change the exception handler from:

System.Console.Out.WriteLine(e.Message);

to

System.Diagnostics.Trace.WriteLine(e.ToString());

and post the results from OpenAL diagnostics again?

(The exception message is missing from the debug log, and using Trace.WriteLine will make it appear.)

bitskull's picture

Here it is:

This application is currently running as x64
--- Device related errors ---
Enumerating audio devices.
    System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at System.Runtime.InteropServices.Marshal.ReadByte(IntPtr ptr, Int32 ofs)
   at OpenTK.Audio.OpenAL.Alc.GetString(IntPtr device, AlcGetStringList param) in d:\lib\opentk1.1\1.1\Source\OpenTK\Audio\OpenAL\Alc\Alc.cs:line 289
    Found playback devices:
    Speakers (VIA High Definition Audio)
    Speakers (VIA High Definition Audio)
    Default playback device: Speakers (VIA High Definition Audio)
    Found recording devices:
    Default recording device: 
--- Device related analysis ---
    Default playback device: Speakers (VIA High Definition Audio)
    All known playback devices:
        Speakers (VIA High Definition Audio)
        Speakers (VIA High Definition Audio)
    Default recording device: 
    All known recording devices:
--- Alc related errors ---
--- Alc related analysis ---
    Alc Version: 1.1
    Efx Version: 1.0
    Efx max. Auxiliary sends: 4
    Alc Extension string: ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_DEDICATED ALC_EXT_disconnect ALC_EXT_EFX ALC_EXT_thread_local_context ALC_SOFT_loopback
    Confirmed Alc Extensions:
        ALC_ENUMERATE_ALL_EXT: True
        ALC_ENUMERATION_EXT: True
        ALC_EXT_ASA: False
        ALC_EXT_ASA_DISTORTION: False
        ALC_EXT_ASA_ROGER_BEEP: False
        ALC_EXT_BRS_GAME_LICENSE_REQUIRED: False
        ALC_EXT_capture: True
        ALC_EXT_DEDICATED: True
        ALC_EXT_EFX: True
--- AL related errors ---
--- AL related analysis ---
    Used Device: OpenAL Soft
    AL Renderer: OpenAL Soft
    AL Vendor: OpenAL Community
    AL Version: 1.1 ALSOFT 1.15.1
    AL Speed of sound: 343,3
    AL Distance Model: InverseDistanceClamped
    AL Maximum simultanous Sources: 128
    AL Extension string: AL_EXT_ALAW AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_MULAW AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET AL_EXT_source_distance_model AL_LOKI_quadriphonic AL_SOFT_buffer_samples AL_SOFT_buffer_sub_data AL_SOFTX_deferred_updates AL_SOFT_direct_channels AL_SOFT_loop_points AL_SOFT_source_latency
    Confirmed AL Extensions:
        AL_EXT_ALAW: True
        AL_EXT_BFORMAT: False
        AL_EXT_double: True
        AL_EXT_EXPONENT_DISTANCE: True
        AL_EXT_float32: True
        AL_EXT_FOLDBACK: False
        AL_EXT_IMA4: True
        AL_EXT_LINEAR_DISTANCE: True
        AL_EXT_MCFORMATS: True
        AL_EXT_mp3: False
        AL_EXT_MULAW: True
        AL_EXT_OFFSET: True
        AL_EXT_vorbis: False
        AL_LOKI_quadriphonic: True
        EAX-RAM: False
        EAX: False
        EAX1.0: False
        EAX2.0: False
        EAX3.0: False
        EAX3.0EMULATED: False
        EAX4.0: False
        EAX4.0EMULATED: False
        EAX5.0: False
--- Efx related errors ---
--- Efx related analysis ---
    Efx Effects supported:
        Autowah: False
        Chorus: False
        Compressor: False
        Distortion: False
        EaxReverb: True
        Echo: True
        Equalizer: False
        Flanger: False
        FrequencyShifter: False
        PitchShifter: False
        Reverb: True
        RingModulator: True
        VocalMorpher: False
    Efx Filters supported:
        Bandpass: False
        Highpass: False
        Lowpass: True
    Efx max. Auxiliary Effect Slots: 4
--- X-RAM related errors ---
--- X-RAM related analysis ---
    X-RAM extension is not available, skipping test.
--- AudioCapture related errors ---
The handle returned by Alc.CaptureOpenDevice is null.
Alc Error: InvalidValue: One of the parameters has an invalid value.
Device Name: 
Capture frequency: 16000
Buffer format: Mono16
Buffer Size: 4096
The handle returned by Alc.CaptureOpenDevice is null.
Alc Error: InvalidValue: One of the parameters has an invalid value.
Device Name: IntPtr.Zero
Capture frequency: 16000
Buffer format: Mono16
Buffer Size: 4096
The handle returned by Alc.CaptureOpenDevice is null.
Alc Error: InvalidValue: One of the parameters has an invalid value.
Device Name: 
Capture frequency: 16000
Buffer format: Mono16
Buffer Size: 4096
AudioCapture Exception caught: All attempts to open capture devices returned IntPtr.Zero. See debug log for verbose list.
--- AudioCapture related analysis ---
Capture Device could not be initlialized (no microphone plugged into the jack?). Skipping test.
the Fiddler's picture

Thank you, this was a bug in OpenTK.

This is fixed by commit 7afe48c in the develop branch and will be included in the next release.

bitskull's picture

Great, thx for your work!