Quantcast
Channel: naudio Work Item Rss Feed
Viewing all articles
Browse latest Browse all 738

Commented Unassigned: DirectSoundOut fails if WaveFormat is WaveFormatExtraData [16487]

$
0
0
If the WaveFormat of the wave provider used as the parameter for Init is of type WaveFormatExtraData
The call within InitializeDirectSound to GCHandle.Alloc fails with an ArgumentException complaining that the “Object contains non-primitive or non-blittable data.”.

This is due to the extraData array of bytes field. Although it is a fixed size, alloc is still unable to pin the array because the array type itself is non-primitive and so cannot be pinned.


public TestPlay(Stream in)
{
var waveStream = new WaveFileReader(in);
var player = new DirectSoundOut(DirectSoundOut.DSDEVID_DefaultPlayback);
player.Init(waveStream);
player.Play();
//Waiting for play to stop removed for brevity.
}
Comments: I think the simple solution here is to replace the GCHandle.Alloc with a pair of Marshal.AllocHGlobal and Marshal.StructureToPtr calls. bufferDesc2.lpwfxFormat = Marshal.AllocHGlobal(Marshal.SizeOf(waveFormat)); try { Marshal.StructureToPtr(waveFormat, bufferDesc2.lpwfxFormat, false); bufferDesc2.guidAlgo = Guid.Empty; // Create SecondaryBuffer directSound.CreateSoundBuffer(bufferDesc2, out soundBufferObj, IntPtr.Zero); secondaryBuffer = (IDirectSoundBuffer)soundBufferObj; } finally { Marshal.DestroyStructure(bufferDesc2.lpwfxFormat, waveFormat.GetType()); Marshal.FreeHGlobal(bufferDesc2.lpwfxFormat); } I found a similar problem in WmaWriter, but I am not using it so I have not tested the problem or fix. But I think something like this would work. mt.pbFormat = Marshal.AllocHGlobal(Marshal.SizeOf(inputFormat)); try { Marshal.StructureToPtr(inputFormat, mt.pbFormat, false); InpProps.SetMediaType(ref mt); } finally { Marshal.DestroyStructure(mt.pbFormat, inputFormat.GetType()); Marshal.FreeHGlobal(mt.pbFormat); }

Viewing all articles
Browse latest Browse all 738

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>