Napisałem pewien program z użyciem DirectSound, ale niestety deklaracja SecondaryBuffer wywala błąd : "Wartość jest spoza oczekiwanego zakresu." i ręce opadają bo nie wiem co z tym zrobić. Linijka ta to :
SecondaryBuffer sbuf = new SecondaryBuffer(desc,dev);
Program ma przechwycić dźwięk z mikrofonu i odtworzyć go. Cały program znajduje się poniżej:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.DirectSound;
using Buffer = Microsoft.DirectX.DirectSound.Buffer;
using DS = Microsoft.DirectX.DirectSound;
using System.IO;
namespace testmic
{
public partial class Form1 : Form
{
private WaveFormat fmt;
public Form1()
{
InitializeComponent();
fmt = new WaveFormat();
fmt.SamplesPerSecond = 44100;
fmt.BitsPerSample = 16;
fmt.Channels = 1;
fmt.FormatTag = WaveFormatTag.Pcm;
fmt.BlockAlign = (short)(fmt.Channels * (fmt.BitsPerSample / 8));
fmt.AverageBytesPerSecond = fmt.SamplesPerSecond * fmt.BlockAlign;
}
private void button1_Click(object sender, EventArgs e)
{
CaptureDevicesCollection cdc = new CaptureDevicesCollection();
for (int i = 0; i < cdc.Count; i++)
{
richtextbox1.AppendText("\n" + i.ToString() +" "+cdc[i].ToString());
}
DS.Device dev = new DS.Device();
dev.SetCooperativeLevel(this, CooperativeLevel.Priority);
BufferDescription desc = new BufferDescription(fmt);
desc.ControlEffects = false;
desc.GlobalFocus = true;
desc.DeferLocation = true;
desc.LocateInHardware = true;
desc.BufferBytes = 100000;
CaptureBufferDescription capdesc = new CaptureBufferDescription();
capdesc.BufferBytes = 100000;
capdesc.ControlEffects = false;
capdesc.Format = fmt;
DS.Capture cap = new DS.Capture(cdc[1].DriverGuid);
DS.CaptureBuffer capbuf = new DS.CaptureBuffer(capdesc, cap);
capbuf.Start(false);
Stream stream = new MemoryStream();
capbuf.Read(0, stream, 100000, LockFlag.None);
SecondaryBuffer sbuf = new SecondaryBuffer(stream,desc,dev);
sbuf.SetCurrentPosition(0);
sbuf.Play(0,BufferPlayFlags.Looping);
}
}
}
Jak ktoś zna inną metodę dostępu do mikrofonu to też bym prosił opisać.