Recent

Author Topic: How does one read a wav file to an array?  (Read 3430 times)

Sixthofmay

  • Newbie
  • Posts: 6
How does one read a wav file to an array?
« on: May 25, 2020, 06:29:18 pm »
I need to read a wav file to an array, do some analysis of the PCM samples, and output an xml report. The wav file is mono 16 bit PCM at 48000 samples/sec, size around 500MB.

I can handle everything except getting the wav file to an array. Array will be dynamic of Int16.

My issue is properly parsing the wav file before storing the sample data to the array. I only need the sample data (no need to save header or subchunk info anywhere).

I carefully read the following webpage and took note of the extra subchunks one must parse when reading a wav file. I noted that headers are sometimes not 44 bytes. How does one handle that? Presumably after reading the header, one is looking for subchunks and data. What exactly defines a subchunk? What does one do with that subchunk info?

https://stackoverflow.com/questions/8754111/how-to-read-the-data-in-a-wav-file-to-an-array

The use of an audio library is mentioned to simplify things. Thus I have studied the UOS, BASS, and ACS libraries and they all seem to hide the data buffer for direct access (or make access slow- BASS appears to have lots of overhead accessing samples).

I don't need to play, manipulate, or save the wav file. I only need to do some special analysis of the PCM samples that are in the array. It may take thousands of iterations of the 500MB of data to find what I'm looking for, thus I need the whole file in RAM if I want to have it finish running before I die.

I have many years of experience writing parsers in Delphi and Lazarus, but need to know specifics of what I'm parsing. I have studied several C, C++, and C# RIFF parsers, but none output data, or handle extra subchunks. I have attempted to study the source code for UOS and ACS to find the RIFF parser, but the complexity of the code makes for a very steep learning curve.

I have no problem using a command line program to parse the wav and save the raw PCM to a file (I do know how to read that to an array). I found Sox, but it doesn't seem to have the ability to output raw PCM (no headers or subchunks).

I have Lazarus 2.0.6 running on a Windows 7 Pro 64bit PC with 64GB ECC RAM.

Can anyone help?
« Last Edit: May 25, 2020, 06:42:58 pm by Sixthofmay »

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: How does one read a wav file to an array?
« Reply #1 on: May 25, 2020, 06:43:20 pm »
Quote
Thus I have studied the UOS, BASS, and ACS libraries and they all seem to hide the data buffer for direct access

Hum, uos does it:

Code: Pascal  [Select][+][-]
  1. DynArrayOfSamples := uos_File2Buffer(pchar(SoundFilename), int16 , BufferInfo, FromPos, NumSamples);

Take a look at the example:
https://github.com/fredvs/uos/blob/master/examples/consoleplaymemorybuffer.pas

« Last Edit: May 28, 2020, 07:51:36 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: How does one read a wav file to an array?
« Reply #2 on: May 25, 2020, 07:35:35 pm »
You can try my WavViewer (https://github.com/wp-xyz/WavViewer). It is written in plain Pascal, no external libraries needed. The wave samples are displayed in a chart (you my have to scroll in by rotating the mouse wheel or dragging a zoom rectangle; click to unzoom). A hex editor is embedded to view at details. A few sample files are included.

Note that it supports only 8 and 16 bit samples (it is easy to extend).

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How does one read a wav file to an array?
« Reply #3 on: May 25, 2020, 09:19:04 pm »
Maybe the attached program can give you some ideas?

It's a very old utility I made to convert stereo wave files to mono but note that the program is not only very old (from 1999), which means it deals only with an earlier RIFF format (though it hasn't changed that much), but it's a Turbo Pascal program and uses a custom unit (lcmmisc) which I didn't add, so it's not directly compilable.

Nevertheless, it shows how to read (and convert/write) wave data from a WAV file, so it might be useful as a starting point or to get some ideas. And to laugh a little, too; it's not by any stretch a "software gem"
;)

HTH


BTW, you can find a specification of WAV files at McGill, including links to the formal specs  at Microsoft.
« Last Edit: May 25, 2020, 09:25:38 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: How does one read a wav file to an array?
« Reply #4 on: May 26, 2020, 01:55:20 am »
I think there is a wavout/in unit that comes with with fpc. No one used it?

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How does one read a wav file to an array?
« Reply #5 on: May 26, 2020, 12:28:09 pm »
I think there is a wavout/in unit that comes with with fpc. No one used it?

Is there? Where is it? :o
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

TRon

  • Hero Member
  • *****
  • Posts: 2515
Re: How does one read a wav file to an array?
« Reply #6 on: May 26, 2020, 12:36:13 pm »
Is there? Where is it? :o
package fcl-sound  :)

It can use a little TLC though

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How does one read a wav file to an array?
« Reply #7 on: May 26, 2020, 12:53:17 pm »
package fcl-sound  :)

Woah, I just looked there and didn't see it (so I rather "overlooked" it). Units fpwavformat.pas, fpwavreader.pas and fpwavwriter.pas; just what the doctor recommended ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018