Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IO error when reading 32-bit WAV file #21

Closed
asehgal123 opened this issue Jul 29, 2020 · 8 comments
Closed

IO error when reading 32-bit WAV file #21

asehgal123 opened this issue Jul 29, 2020 · 8 comments

Comments

@asehgal123
Copy link

First of all great package, I am definately going to play around with it.

However I am gettting follwing error on line 105 in WavFile.cs

System.IO.EndOfStreamException: 'Unable to read beyond the end of the stream.'

By reading the source code it looks like the loop on line 103 goes through to sampleCount which is calculated based on blocksize. For some reason blocksize is being read as 1 (if I set it to 2 then everything works). I am new to this so I don't fully understand the file format.

Wav file that I am loading has channelCount of 1 and blockSize of 1 (Is that even correct? could it be that wav file is incorrect), you think you can take a look at this issue?

  • Abhi
@swharden
Copy link
Owner

Hi @asehgal123, thanks for reporting this! Can you upload the WAV file you're trying to read? My guess is something is weird about the WAV file header.

An alternative solution is to use a different library (one designed just for reading audio files) to read your WAV file. I think NAudio has a module which can do this.

@swharden swharden changed the title System.IO.EndOfStreamException: 'Unable to read beyond the end of the stream.' IO error when reading WAV file Jul 29, 2020
@asehgal123
Copy link
Author

Apparently github won't allow me to upload wav file, can I email that to you?

@swharden
Copy link
Owner

Interesting! Perhaps try zipping it, then uploading it?

You're welcome to email it to me too, though if it's too big for email it might not come through

@asehgal123
Copy link
Author

asehgal123 commented Jul 29, 2020

sound1.zip

These 2 files are a better example of the issue, original won't work but converted does.
Used this online tool (https://audio.online-convert.com/convert-to-wav) to convert wav without making any changes to the parameters (size of the file was doubled after convertion).

@swharden
Copy link
Owner

swharden commented Jul 31, 2020

It doubled in size because the original is mono but the converted one is stereo 🤔

Interestingly the source audio is a 32-bit WAV file, and I'm pretty sure that's the core problem here, but I'll work try to get get it supported anyhow. We're making progress here, but it might take another day or two... thanks for uploading these samples! 👍

Reading it outputs this:

First chunk 'RIFF' indicates 3,200,036 bytes
Format chunk 'fmt ' indicates 16 bytes
audio format: 1
channel count: 1
sample rate: 40000 Hz
byteRate: 80000
block size: 1 bytes per sample
resolution: 16-bit
Chunk at 36 ('data') indicates 3,200,000 bytes
PCM data starts at 44 and contains 3200000 bytes

@swharden swharden changed the title IO error when reading WAV file IO error when reading 32-bit WAV file Jul 31, 2020
@swharden

This comment has been minimized.

swharden added a commit that referenced this issue Aug 1, 2020
@swharden
Copy link
Owner

swharden commented Aug 1, 2020

@asehgal123 install NAudio then use its AudioFileReader like this:

double[] audio;
int sampleRate;
using (var audioFileReader = new AudioFileReader("asehgal-original.wav"))
{
	sampleRate = audioFileReader.WaveFormat.SampleRate;
	var wholeFile = new List<float>((int)(audioFileReader.Length / 4));
	var readBuffer = new float[audioFileReader.WaveFormat.SampleRate * audioFileReader.WaveFormat.Channels];
	int samplesRead = 0;
	while ((samplesRead = audioFileReader.Read(readBuffer, 0, readBuffer.Length)) > 0)
		wholeFile.AddRange(readBuffer.Take(samplesRead));
	audio = Array.ConvertAll(wholeFile.ToArray(), x => (double)x);
}

int fftSize = 8192;
var spec = new Spectrogram(sampleRate, fftSize, stepSize: 4_000, maxFreq: 2_000);
spec.Add(audio);
spec.SaveImage("asehgal.png", intensity: 10_000, dB: true);

The audio file you provided is pretty boring - it's just silence - but the bottom 2kHz of its spectrogram looks like this when you run the code above:

image

Hope it helps!

@swharden swharden closed this as completed Aug 1, 2020
@asehgal123
Copy link
Author

asehgal123 commented Aug 3, 2020

thanks for your time and help. I will try this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants