How would I retrieve samples from a stream?

Jul 7, 2013 at 12:19 PM
Edited Jul 7, 2013 at 1:06 PM
Title says it all, what would be the best way to retrieve samples into a float array from a WaveStream?
Coordinator
Jul 9, 2013 at 4:06 PM
if you convert to a SampleProvider (using SampleProviderConverters.ConvertWaveProviderIntoSampleProvider) then you can call Read directly into a float array.
Jul 9, 2013 at 5:12 PM
Alright, and how would I set a 20ms window from which the samples are taken from the file whilst it is played as well.
Jul 9, 2013 at 8:06 PM
Right sorry, do you have any full code demonstrating the use of SampleProvider as I can't seem to figure it out.
Jul 10, 2013 at 1:45 PM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NAudio;
using NAudio.Wave;
using NAudio.Wave.SampleProviders;


class Naudio2
{
    //Declarations required for audio out and the MP3 stream
    static IWavePlayer waveOutDevice;
    static WaveStream mainOutputStream;
    static WaveChannel32 volumeStream;
    //static SampleChannel volumeStream;
    static float[] sampleData;
    static int l4;
    static int length20ms;

    static void Main()
    {
        waveOutDevice = new WaveOut();
        mainOutputStream = CreateInputStream("path to song");
        waveOutDevice.Init(mainOutputStream);
        waveOutDevice.Play();
        WaveToSampleProvider test = new WaveToSampleProvider(mainOutputStream);
        length20ms = ((test.WaveFormat.BitsPerSample * test.WaveFormat.SampleRate) / 100) * 20;
        l4 = length20ms / 4;
        sampleData = new float[l4];
        test.Read(sampleData, 1000, 1);
        for (int i = 0; i < sampleData.Length; i++)
        {
            System.Console.WriteLine(sampleData[i]);
        }
        //ISampleProvider inputsteam = (ISampleProvider)mainOutputStream;
        //inputsteam.Read(sampleData, 0, length20ms);
        /*for (int i = 0; i < sampleData.Length; i++)
        {
            System.Console.WriteLine(sampleData[i]);   
        }*/
        if (mainOutputStream.Position >= mainOutputStream.Length)
        {
            CloseWaveOut();
        }
        System.Console.ReadLine();
        
     
    }
    static WaveStream CreateInputStream(string fileName)
    {
        WaveChannel32 inputStream;
        if (fileName.EndsWith(".mp3"))
        {
            WaveStream mp3Reader = new Mp3FileReader(fileName);
            inputStream = new WaveChannel32(mp3Reader);
        }
        else
        {
            throw new InvalidOperationException("Unsupported extension");
        }
        volumeStream = inputStream;
        return volumeStream;
    }
    static void CloseWaveOut()
    {
        if (waveOutDevice != null)
        {
            waveOutDevice.Stop();
        }
        if (mainOutputStream != null)
        {
            // this one really closes the file and ACM conversion
            volumeStream.Close();
            volumeStream = null;
            // this one does the metering stream
            mainOutputStream.Close();
            mainOutputStream = null;
        }
        if (waveOutDevice != null)
        {
            waveOutDevice.Dispose();
            waveOutDevice = null;
        }
    }
}
That's what I have so far, unfortunately all of the samples seem to be equal to 0, any ideas?