views:

1127

answers:

5

I'm trying to use DirectSound to capture sound from a microphone. Here's my code:

    using Microsoft.DirectX.DirectSound;
    public MicrophoneSensor()
    {
            CaptureBufferDescription micBufferDesc = new CaptureBufferDescription();
            WaveFormat format = new WaveFormat();
            format.SamplesPerSecond = 22000;
            format.Channels = 1;
            format.BitsPerSample = 8;
            format.AverageBytesPerSecond = 22000;
            format.BlockAlign = 1;

            micBufferDesc.Format = format;
            micBufferDesc.BufferBytes = 100000;
            micBufferDesc.ControlEffects = false;
            micBufferDesc.WaveMapped = true;

            micBuffer = new CaptureBuffer(micBufferDesc, microphone);
     }

The instantiations of the micBufferDesc and format variables cause Visual Studio 2008 to throw the following error:

The call is ambiguous between the following methods or properties: 'Microsoft.DirectX.DirectSound.CaptureBufferDescription.CaptureBufferDescription()' and 'Microsoft.DirectX.DirectSound.CaptureBufferDescription.CaptureBufferDescription()'

and

The call is ambiguous between the following methods or properties: 'Microsoft.DirectX.DirectSound.WaveFormat.WaveFormat()' and 'Microsoft.DirectX.DirectSound.WaveFormet.WaveFormat()'

I've tried quite a few different combinations of stating the namespace and using statements but no luck.

I've also checked the references in the solution explorer and as far as I can tell there are no duplicates.

A brand new test project with only the Microsoft.DirectX.DirectSound reference and nothing else still throws the same error.

I have also uninstalled and reinstalled the DirectX SDK (March 2009) as well as the DirectX SDK (November 2008). Still no luck.

Finally, I've tried a new project on another computer here in the lab and it still doesn't work.

Here are the references I have:

  • Graph
  • Microsoft.DirectX.DirectSound
  • Microsoft.DirectX.DirectInput
  • PresentationCore
  • PresentationFramework
  • Service
  • System
  • System.Core
  • System.Data
  • System.Data.DataSetExtensions
  • System.Deployment
  • System.Drawing
  • System.Runtime.Serialization
  • System.ServiceModel
  • System.Windows.Forms
  • System.Xml
  • System.Xml.Linq
  • UIAutomationProvider
  • WindowsBase
  • WindowsFormsIntegration
+1  A: 

It sounds like maybe you are referencing multiple versions of the directx assembly. Perhaps double-check your references. If you need multiple versions, then extern alias may help - but it isn't pretty.


In Visual Studio, look for the "solution explorer" (commonly on the right hand side) - this is the tree of everything in your project. One of the items in this tree is the "References". This is a visual representation of the external dlls that your code is configured to make use of.

(there are many, many .NET dlls - you need to tell each project what dlls it might need)

Expand this node, and look for 2 entries that look like directx. If there are two, get rid of one of them (ideally the one with the lower version). Then try and rebuild.

Marc Gravell
I'm sorry, I'm rather new to C# and Visual Studio. Can you clarify what you mean here?
JohnS
editing... 2 seconds
Marc Gravell
Yeah, I checked over in the references folder. The only thing as far as I can tell that contain the classes is Microsoft.DirectX.DirectSound and nothing else. Even if I remove the other DirectX reference (Microsoft.DirectX.DirectInput) it still doesn't work. Should I post all of the entries in my references folder?
JohnS
Hmm.... you've got me there...
Marc Gravell
It's alright. Thanks for the help!
JohnS
+1  A: 

you've included two references to different versions of the assembly that contains that function. Remove one of the references.

Henri
A: 

You may have multiple references to the DirectX assembly. Check your project, in the References folder. Look for duplicate entries, specifically references to multiple version of microsoft.directx.directsound.dll. If there are duplicates remove one and try again.

heavyd
How do I tell where the two references are coming from? I'm not even sure this is the problem since when I delete the one reference I know is relevant (Microsoft.DirectX.DirectSound) it complains about not being able to find the classes at all.
JohnS
You would see duplicate entries in your references folder. What version of Microsoft.DirectX.DirectSound are you using?
heavyd
There are no duplicates in my References folder. It is version 1.0.2902.0 (from the add reference dialog). It came in the March 2009 DirectX SDK.
JohnS
What do you have in your References folder? I was able to start a new project in VS C# Express with the March 2009 SDK, and just creating a new class given your above code seems to work. Maybe try it in an isolated environment and see if its something in your project or your machine.
heavyd
I've tried it on another machine in the lab that I am working in with the same results. I've updated the original post with the list of references.
JohnS
A: 

I had the same error, it's not a double reference. Click run and the compiler magically forgets it, or you can stop the annoyance completely with the following.

using System.Reflection;

// then instead of WaveFormat fmt = new WaveFormat()

ConstructorInfo constructor = typeof(WaveFormat).GetConstructor(Type.EmptyTypes);
WaveFormat fmt = (WaveFormat)constructor.Invoke(null);

// do the same for CaptureBufferDescription
It'll be awhile before I can test this, but as soon as I do I'll post my results.Thanks a bunch!
JohnS
A: 

This is common problem with DirectSound. You will find also many many other problems ;) Remember that with DS nothing is what it looks. When buffer return null it probably cause only because "read position" is just inner buffer write pointer. So when you ask read or write pointers calculate allways at least one block safe zone ;) and when you get buffer positions from ds methods use try cast because it can throw random errors.

Burner