0

I am trying to output audio samples, and do so with cswavplay from http://www.codeproject.com/KB/audio-video/cswavplay.aspx which in turn seem to use DllImports from winmm.dll.

I did get it to play using 8-bit samples, however it fails miserably when I try to feed it 16-bit samples. I dug through the code as best as I can and I understand it as this:

I get a pointer to a buffer to fill each time cswavplay finished playing the last buffer. It works for one iteration, it plays back one buffer, sometimes... I get all sorts of funny exceptions, AccessViolationException just now for instance when I tried to use a buffer size of 44100 to hear more clearly how much gets played. But when I put breakpoints in various places inside the WaveOut class (part of cswavplay) it seems none of the objects it uses, like the buffers and an instance of AutoResetEvent, are still alive the second iteration. My best guess is that these problems are related to threading or GC. The exceptions seem pretty random, and I am far too inexperienced to comprehend fully what's going on.

I'm asking for either of the following:

1) Wild guesses as to what could be the problem

2) Educated guesses as to what could be the problem

3) Pointers to an alternative way of outputting sound in realtime using C#

I'm not asking for a thorough bug tracking of software i didn't write, so don't mind cswavplay...

At the end of the day, i might be doing something wrong here, but it's hard to know when i don't get a relevant exception (along the lines of BufferAllocationException or something)...

EDIT:

Thanks for all the suggestions about other sound API's. They all seem to assume a .wav file. I'm sorry for not being clear, i'm not playing .wav files, i synthesize samples in realtime.

mickey
  • 403
  • 1
  • 6
  • 20
  • GC. Don't think so. Threading? Maybe, do you use threads? – Albin Sunnanbo Nov 07 '11 at 20:27
  • I can only offer on (II), but using 8-bit-per-sample code in 16-bit-per-sample operation could cause you buffer overruns. Access violation exceptions and missing objects -- you're probably on the right track that the GC is prematurely eating your data -- looks like half-baked attempts at unsafe code. Is this something you can do in unsafe code, from the ground up, with correct marshaling? – ssamuel Nov 07 '11 at 20:29
  • Have you tried the `SoundPlayer` class? http://msdn.microsoft.com/en-us/library/system.media.soundplayer.aspx – Jim Mischel Nov 07 '11 at 20:47
  • @ssamuel I was outputting 8-bit samples with the WaveOut configured for 8-bits at the time. However it doesn't seem to work in 16-bit mode... – mickey Nov 07 '11 at 20:58
  • @ssamuel The problem is that i am not at all able to even begin to quarter-bake anything, much less from the ground up ;) Maybe i should just give up and buy a pet... :( – mickey Nov 07 '11 at 21:01
  • Don't give up that easily. Perhaps try the suggestion from @alois-kraus below. Audio is rarely easy, especially when you're separated from the low-level commands by a CLR. – ssamuel Nov 07 '11 at 21:07
  • If it is the GC, can i somehow see when it happens? – mickey Nov 07 '11 at 21:18

1 Answers1

2

DirectSound and for .NET the XNA framework comes to my mind. There are many very high quality samples out there how to play sound and animate graphics at the same time with .NET.

Alois Kraus
  • 13,229
  • 1
  • 38
  • 64
  • Thanks, though it seems so cumbersome compared to what i already have that is 'almost' working... Tomorrow i'll brew myself some strong coffee and roll my sleeves up and see if i can't get that darn DirectSound to play... – mickey Nov 07 '11 at 21:30