This project has moved and is read-only. For the latest updates, please go here.

Waveform using WasapiLoopbackCapture WPF thread updating ui

Feb 21 at 6:06 PM
has someone manage to implement and how??? 3 days trying to make it work please share you solution. Thanks guys...
already tried:
private readonly SynchronizationContext _syncContext = SynchronizationContext.Current;
{
_syncContext.Post(o => waveCanvas.Children.Clear(), null);
...
}
Polyline pl;
double plH = 0;
double plW = 0;

List<byte> totalbytes;
Queue<Point> displaypts;
Queue<Int32> displaysht;
long count = 0;
int numtodisplay = 2205;

public IWaveIn wi;

private void start()
{
    wi = new WasapiLoopbackCapture();
    wi.DataAvailable += new EventHandler<WaveInEventArgs>(wi_DataAvailable);

    //
    pl = new Polyline();
    pl.Stroke = Brushes.Blue;
    pl.Name = "waveform";
    pl.StrokeThickness = 2;
    pl.MaxHeight = SystemParameters.FullPrimaryScreenHeight;
    pl.MaxWidth = SystemParameters.FullPrimaryScreenWidth;

    plH = pl.MaxHeight;
    plW = pl.MaxWidth;

    displaypts = new Queue<Point>();
    totalbytes = new List<byte>();
    displaysht = new Queue<Int32>();
    //

    wi.StartRecording();
}

public void wi_DataAvailable(object sender, WaveInEventArgs e)
{
    if (e != null)
    {
        totalbytes.AddRange(e.Buffer);
        byte[] shts = new byte[4];

        for (int i = 0; i < e.BytesRecorded - 1; i += 100)
        {
            shts[0] = e.Buffer[i];
            shts[1] = e.Buffer[i + 1];
            shts[2] = e.Buffer[i + 2];
            shts[3] = e.Buffer[i + 3];
            if (count < numtodisplay)
            {
                displaysht.Enqueue(BitConverter.ToInt32(shts, 0));
                ++count;
            }
            else
            {
                displaysht.Dequeue();
                displaysht.Enqueue(BitConverter.ToInt32(shts, 0));
            }
        }

        waveCanvas.Children.Clear(); // Here updating ui from another thread
        pl.Points.Clear(); // Here updating ui from another thread

        Int32[] shts2 = displaysht.ToArray();
        for (Int32 x = 0; x < shts2.Length; ++x)
        {
            pl.Points.Add(Normalize(x, shts2[x]));  // Here updating ui from another thread
        }
        this.waveCanvas.Children.Add(pl);  // Here updating ui from another thread
    }
}

Point Normalize(Int32 x, Int32 y)
{
    Point p = new Point();
    p.X = 1.0 * x / numtodisplay * plW;
    p.Y = plH / 2.0 - y / (Int32.MaxValue * 1.0) * (plH / 2.0);
    return p;
}
Feb 23 at 2:20 AM
Nobody?????
Mar 4 at 4:44 PM
Was there a question?
Mar 16 at 10:55 PM
Yes, the title: "Waveform using WasapiLoopbackCapture WPF thread updating ui "
WasapiLoopbackCapture dosent draw the waveform on the UI form.
If you are sombody else have a simple sample code that can capture and draw wave form on the UI tthread using WasapiLoopbackCapture it would be very appreciated . But i believe it is not possible.
Thank you
Mar 23 at 9:03 AM
Bump