Returns:
- rate : int
Sample rate of WAV file.
- data : numpy array
Data read from WAV file. Data-type is determined from the file;
see Notes. Data is 1-D for 1-channel WAV, or 2-D of shape
(Nsamples, Nchannels) otherwise. If a file-like input without a
C-like file descriptor (e.g., io.BytesIO) is
passed, this will not be writeable.
Notes
WAV format | Min | Max | NumPy dtype |
|---|
32-bit floating-point | -1.0 | +1.0 | float32 |
32-bit integer PCM | -2147483648 | +2147483647 | int32 |
24-bit integer PCM | -2147483648 | +2147483392 | int32 |
16-bit integer PCM | -32768 | +32767 | int16 |
8-bit integer PCM | 0 | 255 | uint8 |
WAV files can specify arbitrary bit depth, and this function supports
reading any integer PCM depth from 1 to 64 bits. Data is returned in the
smallest compatible numpy int type, in left-justified format. 8-bit and
lower is unsigned, while 9-bit and higher is signed.
For example, 24-bit data will be stored as int32, with the MSB of the
24-bit data stored at the MSB of the int32, and typically the least
significant byte is 0x00. (However, if a file actually contains data past
its specified bit depth, those bits will be read and output, too. [2])
This bit justification and sign matches WAV’s native internal format, which
allows memory mapping of WAV files that use 1, 2, 4, or 8 bytes per sample
(so 24-bit files cannot be memory-mapped, but 32-bit can).
IEEE float PCM in 32- or 64-bit format is supported, with or without mmap.
Values exceeding [-1, +1] are not clipped.
Non-linear PCM (mu-law, A-law) is not supported.
References
Examples
]
/
samplerate
print
(
f
"length =
{
length
}
s"
)
length = 0.01s
plot
(
time
,
data
[:,
0
],
label
=
"Left channel"
)
plt
.
plot
(
time
,
data
[:,
1
],
label
=
"Right channel"
)
plt
.
legend
()
plt
.
xlabel
(
"Time [s]"
)
plt
.
ylabel
(
"Amplitude"
)
plt
.
show
()