MaKiPL / OpenVIII-monogame

Open source Final Fantasy VIII engine implementation in C# working on Windows and Linux (Android and iOS planned too!) [Monogame]
MIT License
637 stars 58 forks source link

XAudio throws exceptions when try to playback audio from ffmpeg. #101

Closed Sebanisu closed 5 years ago

Sebanisu commented 5 years ago

Effects Direct3D build XAudio throws exceptions when try to playback audio from ffmpeg.

related? https://github.com/MonoGame/MonoGame/issues/5662

Sebanisu commented 5 years ago

SharpDX.SharpDXException HResult=0x88960001 Message=HRESULT: [0x88960001], Module: [SharpDX.XAudio2], ApiCode: [XAUDIO2_E_INVALID_CALL/InvalidCall], Message: Unknown Source=SharpDX StackTrace: at SharpDX.Result.CheckError() at SharpDX.XAudio2.SourceVoice.SubmitSourceBuffer(AudioBuffer bufferRef, IntPtr bufferWMARef) at SharpDX.XAudio2.SourceVoice.SubmitSourceBuffer(AudioBuffer bufferRef, UInt32[] decodedXMWAPacketInfo) at Microsoft.Xna.Framework.Audio.DynamicSoundEffectInstance.PlatformSubmitBuffer(Byte[] buffer, Int32 offset, Int32 count) at Microsoft.Xna.Framework.Audio.DynamicSoundEffectInstance.SubmitBuffer(Byte[] buffer, Int32 offset, Int32 count) at FF8.Ffcc.LoadSoundFromStream(Byte[]& buffer, Int32 start, Int32& length) in D:\OpenVIII\FF8\Ffcc.cs:line 965 at FF8.Ffcc.WritetoMs(Byte[]& output, Int32 start, Int32& length) in D:\OpenVIII\FF8\Ffcc.cs:line 1349 at FF8.Ffcc.Resample(AVFrame& frame) in D:\OpenVIII\FF8\Ffcc.cs:line 1255 at FF8.Ffcc.Process() in D:\OpenVIII\FF8\Ffcc.cs:line 1201 at FF8.Ffcc.PrepareProcess() in D:\OpenVIII\FF8\Ffcc.cs:line 1100 at FF8.Ffcc.Update(FfccState state, Int32 ret) in D:\OpenVIII\FF8\Ffcc.cs:line 1323 at FF8.Ffcc.Init(String filename, AVMediaType mediatype, FfccMode mode, Int32 loopstart) in D:\OpenVIII\FF8\Ffcc.cs:line 883 at FF8.Ffcc..ctor(String filename, AVMediaType mediatype, FfccMode mode, Int32 loopstart) in D:\OpenVIII\FF8\Ffcc.cs:line 69 at FF8.Module_movie_test.LoadMovie() in D:\OpenVIII\FF8\module_movie_test.cs:line 197 at FF8.Module_movie_test.Update() in D:\OpenVIII\FF8\module_movie_test.cs:line 102 at FF8.ModuleHandler.<Update>d__2.MoveNext() in D:\OpenVIII\FF8\ModuleHandler.cs:line 68

benjaminfoo commented 5 years ago

Im glad my thoughts worked on this :) - I'm getting the exact same results however.

Sebanisu commented 5 years ago

so you select directx project and it didn't work?

benjaminfoo commented 5 years ago

As I've developed this on my own, yes - I haven't tried your latest commit for now, I'll test it tomorrow but I think it should work - I've also disabled the audio previously and it worked.

Sebanisu commented 5 years ago

Oh when I commented above I thought you meant you got the new code and it didn't work. :P I might wanna see if naudio can help make things work. I was avoiding it because some of it's functions are windows only and i was going ffmpeg -> monogame so why add another layer. Though we still have the library there so maybe it can eat the audio samples and spit it out of waveout or directsound. instead of xaudio. At Least till someone finds a fix for xaudio.

Sebanisu commented 5 years ago

https://github.com/MonoGame/MonoGame/issues/6611 per this issue sharpdx is a discontinued project monogame was using to interface with xaudio. Resent 21 day old post says they forked it. So maybe a fix will happen when they update it.

benjaminfoo commented 5 years ago

After setting the target-platform to x86 everywhere it worked - I guess the midi-sounds are still played (hearable at the black/white intro, not working in the first amv-intro).

Sebanisu commented 5 years ago

yeah midi only works in x86. because it uses an old library. Maki is working on replacing it. https://github.com/MaKiPL/OpenVIII/issues/7 They have it replaced in linux but having trouble compiling the new library in Windows.

All Ogg, game sounds and movie sounds are piped through ffmpeg into monogame.

Sebanisu commented 5 years ago

https://stackoverflow.com/questions/6377597/streaming-non-pcm-raw-audio-using-naudio

I think this is promising info though it's been a while since I've messed with ffcc. I basically need to take any part that writes to a SoundEffect or DynamicSoundEffectInstance and say when error occured use naudio calls instead.

Another thought was we build monogram ourselves and use desktopgl's audio with everything else windows. Though I haven't a clue how difficult that would be.

Sebanisu commented 5 years ago

Maybe I should make a wrapper class that holds all the soundeffects or dynamic sounds and naudio and just presents a dumbed down interface for ffcc or other libraries to use. Though I should at least make sure the naudio Idea works lol. And ffcc is kinda the wrapper class for all things audio and video though it's kinda big.

Sebanisu commented 5 years ago

https://markheath.net/post/how-to-record-and-play-audio-at-same <- this looks like exactly what I need.

Sebanisu commented 5 years ago

Okay I added workaround till monogame fixes SharpDX.XAudio2. Though maybe there's a better way I haven't found. When it detects the above exception it switches to nAudio mode. And all audio is piped through that into DirectSoundOut. It's not perfect. Sometimes the sound glitches. But it seems to be working.