filoe / cscore

An advanced audio library, written in C#. Provides tons of features. From playing/recording audio to decoding/encoding audio streams/files to processing audio data in realtime (e.g. applying custom effects during playback, create visualizations,...). The possibilities are nearly unlimited.
Other
2.23k stars 459 forks source link

WASAPIOUT 5.1 Channel --> IAudioClient::Initialize caused an error: 0x88890008, "Unknown HRESULT" #322

Open KalleStae opened 6 years ago

KalleStae commented 6 years ago

I try to play a AAC file in a endless stream to Realtek High Definition Speaker with WasapiOut. Without changing the Waveformat or the Out class i get this Error.

IAudioClient::Initialize caused an error: 0x88890008, "Unknown HRESULT"

This File has this WavFormat.

{ChannelsAvailable: 6|SampleRate: 48000|Bps: 1152000|BlockAlign: 24|BitsPerSample: 32|Encoding: Extensible|SubFormat: 00000003-0000-0010-8000-00aa00389b71|ChannelMask: SpeakerFrontLeft, SpeakerFrontRight, SpeakerFrontCenter, SpeakerLowFrequency, SpeakerBackLeft, SpeakerBackRight}

If i use WaveOut the sound is ok. When i changed the channels from 6 to stereo all is functional but Sound is not so good as before. If i set _soundOut.UseChannelMixingMatrices = true; this have no effort. I am a amateur in c#. What is wrong ??? Should i Use WaveOut or how can i solve this problem with WasApiOut?? Stacktrace:

/ StackTrace // bei CSCore.SoundOut.WasapiOut.InitializeInternal() // bei CSCore.SoundOut.WasapiOut.Initialize(IWaveSource source) // bei AudioSplit.Form1.PlayFile(String file)

Example:

 public void PlayFile(String file)
        {
            var selectedDevice = MMDeviceEnumerator.EnumerateDevices(DataFlow.Render, DeviceState.Active).First();
            var client = AudioClient.FromMMDevice(selectedDevice);

            _soundOut.Stop();
            if (_notificationSource != null)
                _notificationSource.Dispose();
            var source = CodecFactory.Instance.GetCodec(file);

            var ok = client.IsFormatSupported(AudioClientShareMode.Shared, source.WaveFormat, out WaveFormat wavfmt);

            _sampleSource = source.ToSampleSource();
            //_sampleSource = _sampleSource.ToStereo();

            //source.Position = 0;
            _notificationSource = new NotificationSource(_sampleSource) { Interval = 100 };
            _notificationSource.BlockRead += (o, args) =>
            {
                // UpdatePosition();
            };
            waveSource = _notificationSource.ToWaveSource();

            // Error with this waveformat 
            //_waveFormat = {ChannelsAvailable: 6|SampleRate: 48000|Bps: 1152000|BlockAlign: 24|BitsPerSample: 32|Encoding: Extensible|SubFormat: 00000003-0000-0010-8000-00aa00389b71|ChannelMask: SpeakerFrontLeft, SpeakerFrontRight, SpeakerFrontCenter, SpeakerLowFrequency, SpeakerBack...

            loopStream = new LoopStream(waveSource);
            _soundOut.Initialize(waveSource);
            _soundOut.Play();
        }
filoe commented 6 years ago

Could you may upload the file?

KalleStae commented 6 years ago

Audio.zip

Plays also in Vlc and Windows Media Player

KalleStae commented 6 years ago

close issue was a mistake,

I don´t get any answers? Why do you close this issue??

KalleStae commented 6 years ago

I don´t get any answers? Why do you close this issue??

filoe commented 6 years ago

I am sorry I ve missinterpreted your message regarding "close issue". I ll have a look at it.

filoe commented 6 years ago

Works perfectly on my system. What system are you using? Can you please post a screenshot of the choosen device? Following settings: https://i.imgur.com/Ghb4Xhl.png

Seems like your audio driver does not support a certain format (which is quite unusual).

KalleStae commented 6 years ago

grafik

i have tested all formats. as you can see only the formats with 48000 hz throw the Error! Perhaps some speciality with the driver or channel-byte-bits sample rate calculations in the initialize method????? 16 bit -44100 hz - OK 16 bit -48000 hz - ERROR 16 bit -88200 hz - OK 16 bit -96000 hz - OK 16 bit -176400 hz - OK 16 bit -192000 hz - OK 24 bit -44100 hz -OK 24 bit -48000 hz - ERROR 24 bit -88200 hz - OK 24 bit -96000 hz - OK 24 bit -176400 hz - OK 24 bit -192000 hz - OK 32 bit -44100 hz -OK 32 bit -48000 hz - ERROR 32 bit -88200 hz - OK 32 bit -96000 hz - OK 32 bit -176400 hz- OK 32 bit -192000 hz -OK

filoe commented 6 years ago

Ok, thanks. I ll check that.

Am 07.02.2018 um 10:10 schrieb KalleStae notifications@github.com:

i have tested all formats. as you can see only the formats with 48000 hz throw the Error! Perhaps some speciality with the driver or channel-byte-bits sample rate calculations in the initialize method????? 16 bit -44100 hz - OK 16 bit -48000 hz - ERROR 16 bit -88200 hz - OK 16 bit -96000 hz - OK 16 bit -176400 hz - OK 16 bit -192000 hz - OK 24 bit -44100 hz -OK 24 bit -48000 hz - ERROR 24 bit -88200 hz - OK 24 bit -96000 hz - OK 24 bit -176400 hz - OK 24 bit -192000 hz - OK 32 bit -44100 hz -OK 32 bit -48000 hz - ERROR 32 bit -88200 hz - OK 32 bit -96000 hz - OK 32 bit -176400 hz- OK 32 bit -192000 hz -OK

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub, or mute the thread.

filoe commented 6 years ago

Are you really on the latest version? On my system it works perfectly. If yes, I would guess this is some kind of driver issue. WasapiOut tries to find a supported format: https://github.com/filoe/cscore/blob/master/CSCore/SoundOut/WasapiOut.cs#L747 As far as I can remember, there was once the issue with a audio driver, that the IsFormatSupported method reportat the format is supported, but if the driver got initialized with that format, it reported that it is not supported. Can you debug the posted method and tell me what's happening in detail?

KalleStae commented 6 years ago

I use version 1.2.1.2

Debug information from CSCore.CoreAudioAPI.AudioClient.IsFormatSupported:

the result from "IsFormatSupportedNative" is 0 = true . closestMatch = 0

[Source:]https://github.com/filoe/cscore/blob/master/CSCore/CoreAudioAPI/AudioClient.cs#L452

Later in CSCore.CoreAudioAPI.AudioClient.InitializeNative it throws Error 0x88890008, same as
case unchecked((int) 0x88890008): in the previous switch(result) selection

2018-02-16 11_29_38-cscore debugging - microsoft visual studio

filoe commented 6 years ago

But the result of IsFormatSupportedNative is true (0x0)? And the same format will be used in InitializeNative?

KalleStae commented 6 years ago

Result is 0x0 and waveformat is always the same as you see at the screencopy If i use AudioClient. GetMixedFormat (Speaker Device) i get the following format:

{ChannelsAvailable: 6|SampleRate: 48000|Bps: 1152000|BlockAlign: 24|BitsPerSample: 32|Encoding: Extensible|SubFormat: 00000003-0000-0010-8000-00aa00389b71|ChannelMask: SpeakerFrontLeft, SpeakerFrontRight, SpeakerFrontCenter, SpeakerLowFrequency, SpeakerBackLeft, SpeakerBackRight}

The format from var source = CodecFactory.Instance.GetCodec(file); is:

{ChannelsAvailable: 6|SampleRate: 48000|Bps: 576000|BlockAlign: 12|BitsPerSample: 16|Encoding: Extensible|SubFormat: 00000001-0000-0010-8000-00aa00389b71|ChannelMask: SpeakerFrontLeft, SpeakerFrontRight, SpeakerFrontCenter, SpeakerLowFrequency, SpeakerBackLeft, SpeakerBackRight}

In the _soundOut.Initialize(waveSource); the GetMixedFormat is used:

DanielGilbert commented 6 years ago

I was the guy with this issue, and occasionally I get complaints that this issue still persists.

What helps most of the time is to switch to the "Verbesserungen" tab and tick the "Alle Soundeffekte deaktivieren" checkbox. Personally, I think the Realtek driver does some weird stuff. But I haven't digged further into this issue.

KalleStae commented 6 years ago

@DanielGilbert I tried this first but with no effort. The same error occur.

filoe commented 6 years ago

Does any of the samples provided work? The problem is, that we could try to build some kind of workaround. But in fact, CSCore implements the API according to the standard provided by Microsoft. I don't want to start building custom logic for every driver bug we discover.

DanielGilbert commented 6 years ago

@KalleStae Do you have a "real" 6 channel (e.g. 5.1 Surround) system, or is this some kind of virtual stuff?

KalleStae commented 6 years ago

@DanielGilbert ROG SupremeFX 8-Channel High Definition Audio CODEC S1220A @filoe

Does any of the samples provided work?

What do you mean with samples??? I dont't want to force you to build a custom logic, but i think there is some curious thing with the sample rate. I'm an C# amateur with little knowlegde of programming theorie and driver structure. Could it be some problem of the extended WaveFormat? If we can't get new ideas what is going wrong you can close the issue!


I try to get a summary of this issue with my own words:

  1. My System: Windows 10 pro, i7 -Intel core i7-7700K 4200 1151, GTX 1080, ASUS Motherboard Strix Z270F Gaming with ROG SupremeFX 8-Channel High Definition Audio CODEC S1220A Standard installation format = 24 Bit 48kHz !!

  2. The audio file was recorded from my Speakers with cscore _writerAac = MediaFoundationEncoder.CreateAACEncoder(_convertedSoundInSource.WaveFormat, fileName.Replace(".wav", ".aac"));
    Format of the file from MediaInfo.exe:

    Audio ID : 2 Format : AAC Format/Info : Advanced Audio Codec Format-Profil : LC Codec-ID : mp4a-40-2 Dauer : 1 min 7s Bitraten-Modus : konstant Bitrate : 407 kb/s nominale Bitrate : 576 kb/s Kanäle : 6 Kanäle Kanal-Positionen : Front: L C R, Side: L R, LFE Samplingrate : 48,0 kHz Bildwiederholungsrate : 46,875 FPS (1024 SPF) Stream-Größe : 3,27 MiB (100%) Kodierungs-Datum : UTC 2018-01-30 14:03:10 Tagging-Datum : UTC 2018-01-30 14:03:10 mdhd_Duration : 67392

  3. The error occurs only with sample rate 48000 Hz in the speakers format ( see comment above) therefore i could live with the other formats but for other people it isnt nice when you don't know where the error comes from!

  4. The error don't occur when i play the file with VLC or Groove-Music or Windows media player or Dopamin (could be transformed to Stereo?? i couldn't check this )

  5. NAudio Mediafoundation Tests throw in the AudioClient.Initialize Error 0x80070057 One or more arguments are not valid when i play the AAC file (init is waveformat from the File).

  6. If i only changed the definition for _soundOut from WasApiOut to WaveOut there is no error!!!???? //_soundOut = new WasapiOut(true, AudioClientShareMode.Shared, 100); _soundOut = new WaveOut(); What is the difference between WaveOut to WasAPiOut??

DanielGilbert commented 6 years ago

I might have found something, but I need some confirmation first. Basically, this issue has nothing todo with the issue I had about the device reporting some "weird" parameters.

@KalleStae , please try the following:

Find this line in your copy of the cscore source:

https://github.com/filoe/cscore/blob/master/CSCore/SoundOut/WasapiOut.cs#L645

_outputFormat = SetupWaveFormat(_source, _audioClient);

Replace it with this line:

_outputFormat = new WaveFormatExtensible(_outputFormat.SampleRate, _outputFormat.BitsPerSample, _outputFormat.Channels, AudioSubTypes.IeeeFloat);

Compile it and give it a try. :)

DanielGilbert commented 6 years ago

Hm - ok, I'll post the follow up without confirmation, in case there won't be a reply:

I had the same issue playing this file, using the exact same sample code as in the first post. The error happens in this line:

 _sampleSource = source.ToSampleSource();

The ToSampleSource() extension calls WaveToSampleBase.CreateConverter(waveSource);. In the protected Constructor of WaveToSampleBase, the following logic is executed:

protected WaveToSampleBase(IWaveSource source)
{
    if (source == null) 
        throw new ArgumentNullException("source");

    Source = source;
    _waveFormat = (WaveFormat) source.WaveFormat.Clone();
    _waveFormat.BitsPerSample = 32;
    _waveFormat.SetWaveFormatTagInternal(AudioEncoding.IeeeFloat);
}

However, when setting BitsPerSample to 32, the ValidBitsPerSample property doesn't match the 32 BitsPerSample, it still is 16, which comes from the original file. According to the docs, ValidBitsPerSample should resemble the "Number of bits of precision in the signal", so it should be 32. (Source: https://msdn.microsoft.com/en-us/library/windows/desktop/dd390971(v=vs.85).aspx).

It's funny though, that this issue only occurs when setting the frequence to 48k Hz.

Before creating the new WaveFormatEx instance:

image

Afterwards:

image

I currently don't have the time to do some excessive testing, or to create a pull request to fix this issue. So I'll leave this excercise to some one else, sorry.

KalleStae commented 6 years ago

@DanielGilbert Sorry for my late test and thank you for the solution. Sample rate calculations was in suspect since the start of the issue. With

_outputFormat = new WaveFormatExtensible(_outputFormat.SampleRate, _outputFormat.BitsPerSample, _outputFormat.Channels, AudioSubTypes.IeeeFloat);

all is functional. But i'm not able to change the error at the right position in CsCore, because i don't fully understand the complex structur of CsCore and Github! Hope somebody can fix it.

KalleStae commented 6 years ago

@DanielGilbert Hi Daniel I read the document about WaveFormatEx. I think "ValidBitsPerSample" can lower than BitsPerSample. When i change ValidBitsPerSample then the debugger changes also SamplesPerBlock. So i think it is not clear why the error occur. A good document about Multiple channel audio data and WAVE files

filoe commented 6 years ago

Ok I've double checked that behaviour. I can cnofirm that this occurs as described (awesome input btw). My system can deal with those formats. Can you check the following: Does changing the _samplesUnion of the WaveFormat to 32 solve the problem? Also does changing the _samplesUnion of the WaveFormat to zero solve the problem?

KalleStae commented 6 years ago

Hi filoe i just find out, if i use the outputformat from DanielGilbert the AAC file do not throw the error but now all Mp3 files throws the same error!! Therefore the solution is not correct.

filoe commented 6 years ago

Which solution exactly? Did you try the two points if described?

KalleStae commented 6 years ago

The solution change in WasApiOut to

_outputFormat = new WaveFormatExtensible(_outputFormat.SampleRate, _outputFormat.BitsPerSample, _outputFormat.Channels, AudioSubTypes.IeeeFloat);

I dont know if i do it correct to change _samplesUnion?? First i changed in the debugger field SamplesPerBlock from 16 to 32. Then the field ValidbitsperSample automatically goes to32. The File AAC was played correct. The value of the Field in a Mp3 file is 32 and played correct.

If i changed the field ValidbitsperSample from 16 to 0. SamplesPerBlockgoes to 0. Error grafik occur.

filoe commented 6 years ago

Ok fine: So if you remove the adjustment in WasapiOut and change the value of ValidBitsPerSample to the same value of BitsPerSample, the whole thing works? Correct?

KalleStae commented 6 years ago

yes, i changes the fields only in the debugger and AudioClient.InitializeNative works correct.

KalleStae commented 6 years ago

If i use the testfiles from Fraunhofer Instistut all is correct for multichannel files (mp4). (not for 7.1 testfiles andsome .wav files) They all have 32 Bit in the BitsPerSample and ValidBitsPerSample . But my testfile Audio.aac was recorded with CsCore and have 16 bit.

filoe commented 6 years ago

Can you provide the not working files?

KalleStae commented 6 years ago

HTML5 AAC Audio Playback Tests - Multichannel

KalleStae commented 6 years ago

@filoe with other tests i found a strange behavoir of the error. If i changed the format from the speakers to a format not equal the bitrate of the testfile the file was played without an error. If I play a AAC file ChID-BLITS-EBU.mp4 with bitrate 41000 hz all formats with 41000 hz throws the error 0x88890008. If I play the AAC file Audio.aac with bitrate 48000 hz all formats with 48000 hz throws the error 0x88890008 . Now i dont know nothing. I will give up.

filoe commented 6 years ago

I will check that.

BrianLima commented 5 years ago

I'm having the same problem while trying to capture output from the default audio of my system, i have the following specs:

With the audio drivers:

If i try to capture audio from it while using the onboard audio device, i get the error CSCore.CoreAudioAPI.CoreAudioAPIException: 'IAudioClient::Initialize caused an error: 0x88890008, "Unknown HRESULT".'

image

If i use the HDMI audio, it works perfectly

I read all the responses and was wondering about the WaveFormat reported, then i noticed that if i change the audio format from Stereo to 7.1 Sorround at Realtek's panel, even tho i don't have a sorround system setup, it magically works on the onboard audio. No need to change anything more.

So it seems that CSCore is trying to initialize the devices with the maximum number of channels reported by the audio driver, not the currently selected, or Realtek is just reporting 8 channels as active even tho only 2 are active.

If i initialize the default device with: _soundIn = new WasapiLoopbackCapture(100, new WaveFormat(48000, 24, 2)); Forcing CSCore to utilize 2 channels, it works fine.

sajeebchandan commented 4 years ago

I'm having the same problem while trying to capture output from the default audio of my system, i have the following specs:

  • MSI B350M AM4
  • Radeon R9 Fury X

With the audio drivers:

  • Nahimic Audio 2 2.5.23
  • Realtek 6.0.1.8269
  • AMD Audio Driver Version 10.0.1.7

If i try to capture audio from it while using the onboard audio device, i get the error CSCore.CoreAudioAPI.CoreAudioAPIException: 'IAudioClient::Initialize caused an error: 0x88890008, "Unknown HRESULT".'

image

If i use the HDMI audio, it works perfectly

I read all the responses and was wondering about the WaveFormat reported, then i noticed that if i change the audio format from Stereo to 7.1 Sorround at Realtek's panel, even tho i don't have a sorround system setup, it magically works on the onboard audio. No need to change anything more.

So it seems that CSCore is trying to initialize the devices with the maximum number of channels reported by the audio driver, not the currently selected, or Realtek is just reporting 8 channels as active even tho only 2 are active.

If i initialize the default device with: _soundIn = new WasapiLoopbackCapture(100, new WaveFormat(48000, 24, 2)); Forcing CSCore to utilize 2 channels, it works fine.

Thanks BrianLima for you explanation and solution! It really worked for me. Thank you very much.

OscarRoussel commented 4 years ago

As @BrianLima proposed :

I read all the responses and was wondering about the WaveFormat reported, then i noticed that if i change the audio format from Stereo to 7.1 Sorround at Realtek's panel, even tho i don't have a sorround system setup, it magically works on the onboard audio. No need to change anything more.

Worked almost perfectly for me. Somehow it took minutes to tens of minutes for the solution to apply. No idea why. Anyway, thank you!