Feb-24-2017, 08:32 PM
A .wav file (as well as most other audio and images actually) consists of two main parts, a header and the actual data. For a .wav file the 'header' is 44 bytes (0 - 43) and the data is the remainder of the file (bytes 44 - end). The 'header' is further divided into 'file' and 'format' bytes (chunks). The following is something I wrote to 'read' that information. It's some what long as I included a lot of comments. It only requires the built-in struct module. This is based on the information provided here: .WAV format. Hope this help you in understanding what's going on 'under the hood'
the output:
the output:
Output:C:\Python36\python.exe C:/Python/Sound/read_wave.py
.WAV File information for SineWave_440Hz.wav
===============================================================================
File ID: RIFF
File Size: 264644
File Type: WAVE
File Format: fmt
Length of Format Data: 16
Type of Format: PCM (Uncompressed)
Number of Channels: 1
Sample Rate: 44100
Byte Rate: 88200
Block Align: 2
Bits Per Sample: 16
Data Header: data
Size of Data Section: 264600
Process finished with exit code 0
I did not include the actual data, but concept remains the same. Just 'seek(44), read the appropriate bits per sample (i.e . 8, 16, 24, 32) then unpack it. Writing the file is pretty much just the reverse, except you 'pack' instead of 'unpack'.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition