This project has moved. For the latest updates, please go here.

How does ISampleProvider.Read(float[] buffer, int offset, int count) work?

Jul 14, 2013 at 1:01 PM
Hi, I'm trying to read samples from a sound file into an array of floats but it seems that the array gets filled with 0s each time, I have no clue what I'm doing wrong...
class Naudio{
public static void Main(String path){

            var reader = new AudioFileReader(path);
            var mysample = new MySampleProvider(reader);
            sampleData = new float[100];
            mysample.Read(sampleData, 0, 100);
            for (int i = 0; i < 100; i++)
            {
                System.Console.WriteLine(sampleData[0] + "test");
            }

}
}

class MySampleProvider : ISampleProvider
{
    private readonly ISampleProvider source;

    public MySampleProvider(ISampleProvider source)
    {
        this.source = source;
    }

    public int Read(float[] buffer, int offset, int count)
    {
        int samplesRead = source.Read(buffer, offset, count);
        // TODO: examine and optionally change the contents of buffer
        return samplesRead;
    }

    public WaveFormat WaveFormat
    {
        get { return source.WaveFormat; }
    }
}
I've been tearing my hair out for days, any help at all is appreciated.
Coordinator
Jul 15, 2013 at 1:33 PM
it might be because you are trying to read such a low number of samples - only 100 which is just 2ms worth of sound. Try increasing the amount you read at once and see if that helps
Jul 15, 2013 at 2:09 PM
No dice, it only seems to work if I put the mysample.Read(sampleData, 0, 1000); in a for loop that loops 1000 times. It's strange I have no clue why it works with the loop and not without it...
Coordinator
Jul 15, 2013 at 2:10 PM
well the start of most files would likely be silence, so it would be quite normal for the first few hundred or thousand samples to be all zeros
Jul 15, 2013 at 2:20 PM
Edited Jul 15, 2013 at 2:26 PM
Really? Even for a file that starts instantaneously? Sorry, I'm not exactly an expert with sound related programming or anything related to sound at all...

Edit:
 while(mysample.Read(sampleData, 0, 8) != 0){
                    mysample.Read(sampleData, 0, 8);
                }
This seems to solve the problem based on your earlier post, anyhow how would I find the first possible sample in an audio file? Would I do something like multiply the time (in seconds) * samples per second and divide that by a certain number which becomes our read method's sample count?
Coordinator
Jul 15, 2013 at 2:25 PM
Well I haven't seen your audio file, so I'm only guessing. 100 samples is only 2ms though, so it wouldn't surprise me at all if you had an empty buffer in your first read. One thing you haven't said is what the return value of Read is in each case. Read tells you how many samples it wrote into the buffer. Just because you asked for 100, doesn't mean you got 100, although the intention is that it only returns less than the requested count if the end of the file has been reached.
Jul 15, 2013 at 2:34 PM
Edited Jul 16, 2013 at 7:44 AM
Alright, System.Console.WriteLine(mysample.Read(sampleData, 0, 8)); returns 8 if called before the while statement but if called after it returns 0.

Edit: I also tried putting System.Console.WriteLine(mysample.Read(sampleData, 0, 8)); in the while statement, but the console just flickers with 8s...

Edit 2: I tried a number like 1024 samples and it also flickered but then it stopped with a 0. I also tried it with 1023 and did the sample thing, this isn't telling me much with regards to the number I should read however...

Edit 3: taking forever, I tried to retrieve 1 sample until the thing stopped which had a lower value for the first array value then the latter two.

Edit 4: Well I just figured I'm in a pickle, the number at which the audible samples start is prime so yea...