2

I'm getting that exception when I try to run a simple DirectSound program. Here's the code:

using System;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.DirectX.DirectSound;

namespace AudioDemo
{
    class Program
    {
        [DllImport("user32.dll")]
        private static extern IntPtr GetDesktopWindow();

        static void Main(string[] args)
        {
            // output device

            var device = new Device();
            device.SetCooperativeLevel(GetDesktopWindow(), CooperativeLevel.Normal);

            // format description

            var format = new WaveFormat
            {
                BitsPerSample = 8,
                Channels = 1,
                FormatTag = WaveFormatTag.Pcm,
                SamplesPerSecond = 8000
            };

            format.BlockAlign = (short)(format.BitsPerSample / 8 * format.Channels);
            format.AverageBytesPerSecond = format.SamplesPerSecond * format.BlockAlign;

            var outputLatency = 20;
            var frameSize = format.AverageBytesPerSecond * outputLatency / 1000;

            // buffer 

            var description = new BufferDescription
            {
                BufferBytes = frameSize,
                Format = format,
                DeferLocation = true,
                GlobalFocus = true
            };

            var outputBuffer = new SecondaryBuffer(description, device);

            // buffer notifications

            var notify = new Notify(outputBuffer);

            // ...
        }
    }
}

I get the exception on the last line (var notify = new Notify(outputBuffer);).

Not sure what went wrong. The buffer was initialized correctly.

lzm
  • 827
  • 2
  • 11
  • 25

2 Answers2

0

"A SecondaryBuffer object will support notification events only if its BufferDescription.ControlPositionNotify is set to true."

(https://msdn.microsoft.com/en-us/library/windows/desktop/ms812460.aspx)

So try this:

        var description = new BufferDescription
        {
            BufferBytes = frameSize,
            Format = format,
            DeferLocation = true,
            GlobalFocus = true,
            ControlPositionNotify = true
        };

I've had the same exception, but this solved the problem!

beatcoder
  • 683
  • 1
  • 5
  • 19
0

It's not clear to me what you're trying to do with your outputLatency and frameSize variables, or with the BufferBytes property, but my guess is there is where your problem is.

MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
  • I want the buffer to hold 20ms of sound (= 320 bytes). that `*2` was just a test. – lzm Sep 16 '11 at 13:08
  • what C# framework would you use if you wanted to play sound data generated in real time? – lzm Sep 16 '11 at 15:03
  • The `waveOutOpen` API calls are still the best, IMHO (I use this for my own software synthesizer app). Here's a link to a good C# wrapper project: http://www.koders.com/csharp/fidA53BE0F5147E80A51FAC4A7CA140B80436D056EC.aspx – MusiGenesis Sep 16 '11 at 15:59
  • DirectSound is a gigantic pain in the butt to deal with, and support stopped for it a long time ago. – MusiGenesis Sep 16 '11 at 15:59