DirectSound has a lot of nuance to it that can make it difficult to work with. If you're open to using some third-party options, there are a few free ones available that abstract the technical details of DirectSound and make this problem much more approachable. I personally recommend BASS.NET - and NAudio is good if you're more interested in an entirely managed solution.
In BASS.NET, your code would look something like this:
private RECORDPROC _myRecProc; // make it global, so that the Garbage Collector can not remove it
...
Bass.BASS_RecordInit(-1);
_myRecProc = new RECORDPROC(MyRecording);
// start recording paused
int settings = 0;
int inputSource = 0;
while (settings != -1)
{
// get the settings of that input
settings = Bass.BASS_RecordGetInput(inputSource, ref vol);
if ( Bass.BASS_RecordGetInputName(inputSource) == "What U Hear" ||
Bass.BASS_RecordGetInputName(inputSource) == "Stereo Mix")
{
break;
}
inputSource++;
}
Bass.BASS_RecordSetInput(inputSource, BASSInput.BASS_INPUT_ON, 0.5F)
int recChannel = Bass.BASS_RecordStart(44100, 2, BASSFlag.BASS_RECORD_PAUSE, 50, _myRecProc, IntPtr.Zero);
...
// really start recording
Bass.BASS_ChannelPlay(recChannel, false);
...
// the recording callback
private bool MyRecording(int handle, IntPtr buffer, int length, IntPtr user)
{
return true;
}
Basically, you're initializing BASS. Then you loop through all the possible input sources searching for "What U Hear" or "Stereo Mix." The name of the channel that is a combination of all your speaker output varies from soundcard to soundcard, so you'll have to get a list of the common names. After you've found an appropriate channel, you'll start recording. The MyRecording method will have a buffer for you to analyze.
This is just one way to do it, with one library. Take a look around and see which library provides you with data in a format you want it in.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…