BW64 I/O¶
To read or write a BW64 file, the primary interface is the
openBw64Adm() and openBw64() functions.
To read samples and ADM metadata (as an ADM object) from a file, use
something like:
from ear.fileio import openBw64Adm
with openBw64Adm("path/to/file.wav") as f:
adm = f.adm # get the ADM metadata
for sample_block in f.iter_sample_blocks(1024):
# do something with sample_block, which will be a numpy float array
# of (nsamples, nchannels)
print(sample_block.shape)
For lower level access without parsing ADM data:
from ear.fileio import openBw64
with openBw64("path/to/file.wav") as f:
print(f.axml) # get the raw AXML data
print(f.chna) # get the CHNA data
while True:
sample_block = f.read(1024)
if not len(sample_block):
break
print(sample_block.shape)
To write a file, you have to construct the format chunk manually:
from ear.fileio.bw64.chunks import FormatInfoChunk, ChnaChunk, AudioID
import numpy as np
# dummy ADM data
axml = b'some AXML data here'
chna = ChnaChunk([
AudioID(1, 'ATU_00000001', 'AT_00010001_01', 'AP_00010003'),
])
formatInfo = FormatInfoChunk(formatTag=1,
channelCount=1,
sampleRate=48000,
bitsPerSample=24)
with openBw64("path/to/file.wav", "w", formatInfo=formatInfo) as f:
# optionally write axml and chna data
f.axml = axml
f.chna = chna
# write some sample blocks
for i in range(10):
f.write(np.zeros((1024, 1)))
To write some generated adm data, use something like this to generate the CHNA and AXML chunk data:
from ear.fileio.adm.chna import populate_chna_chunk
from ear.fileio.adm.generate_ids import generate_ids
from ear.fileio.adm.xml import adm_to_xml
import lxml.etree
adm = ...
generate_ids(adm)
chna = ChnaChunk()
populate_chna_chunk(chna, adm)
xml = adm_to_xml(adm)
axml = lxml.etree.tostring(xml, pretty_print=True)
See also
generate_ids(), populate_chna_chunk(), adm_to_xml().
For generating ADM metadata, see ADM Builder.
These functions and classes are documented below:
- ear.fileio.openBw64(filename, mode='r', **kwargs)¶
Open a BW64 file for reading or writing.
- Parameters
- Returns
file object
- Return type
- ear.fileio.openBw64Adm(filename, fix_block_format_durations=False)¶
Open a BW64 ADM file for reading. This automatically parses the ADM data, adds the common definitions, and adds information from the CHNA chunk. This can be accessed through the
.admattribute of the returned Bw64AdmReader.- Parameters
- Returns
file object
- Return type
- class ear.fileio.utils.Bw64AdmReader(bw64FileHandle, fix_block_format_durations=False)¶
Utility for reading ADM data from a BW64 file; use
openBw64Adm()to create these.- property bitdepth¶
number of bits per sample
- property channels¶
number of channels
- property chna¶
CHNA data
- iter_sample_blocks(blockSize)¶
Read blocks of samples from the file.
- Parameters
blockSize (int) – number of samples to read at a time
- Yields
np.ndarray of float – sample blocks of shape (nsamples, nchannels), where nsamples is <= blockSize, and nchannels is the number of channels
- property sampleRate¶
sample rate in Hz
- property selected_items¶
default list of rendering items
- Type
list of ear.core.metadata_input.RenderingItem
- class ear.fileio.bw64.Bw64Reader(buffer)¶
Read a WAVE/RF64/BW64 file.
Only PCM data (16bit, 24bit, 32bit) is currently supported. The class provides easy access to the axml, chna, bext chunks. The most important format information (samplerate, sample rate, bit rate, …) can be directly accessed as properties.
- property bitdepth¶
number of bits per sample
- property channels¶
number of channels
- property chna¶
CHNA data
- Type
- get_chunk_data(chunk_name)¶
Read and return the binary data of a named chunk.
- read(numberOfFrames)¶
read up to numberOfFrames samples
- Returns
sample blocks of shape (nsamples, nchannels), where nsamples is <= numberOfFrames, and nchannels is the number of channels
- Return type
np.ndarray of float
- property sampleRate¶
sample rate in Hz
- tell()¶
Get the sample number of the next sample returned by read.
- class ear.fileio.bw64.Bw64Writer(buffer, formatInfo=[ formatTag: 1 channelCount: 1 sampleRate: 48000 bytesPerSecond: 96000 blockAlignment: 2 bitsPerSample: 16 ], chna=None, axml=None, bext=None, forceBw64=False)¶
- close()¶
Close and finalize the BW64 output.
This means that the final chunk sizes will be written to the buffer.
If you forget to call this function, the resulting file will be corrupted. Thus, it might be a good idea to use this with a contextmanager.
- write(samples)¶
Append sample data to the BW64 data chunk.
- Parameters
samples (array - like, dtype float) – Array of audio samples, columns correspond to channels Expects float sample values in the range(-1, 1).
Chunk Classes¶
These classes represent chunks (or parts of chunks) in a BW64 file:
- class ear.fileio.bw64.chunks.ChnaChunk(audioIDs=NOTHING)¶
Class representation of the ChannelAllocationChunk
- audioIDs¶
CHNA entries
- Type
list of AudioID
- asByteArray()¶
Get the binary representation of this chunk data.
- class ear.fileio.bw64.chunks.AudioID(trackIndex, audioTrackUID, audioTrackFormatIDRef, audioPackFormatIDRef)¶
Class representation of a chna audioIDs list entry.
- class ear.fileio.bw64.chunks.FormatInfoChunk(formatTag=1, channelCount=1, sampleRate=48000, bytesPerSecond=None, blockAlignment=None, bitsPerSample=16, cbSize=None, extraData=None)¶
Class representation of the FormatChunk
This class can be either used to create a new format chunk or simplify reading and validation of a format chunk. Once created the object cannot be changed. You can only read the saved data. The order of the constructor arguments might seem a bit strange at first sight. The order corresponds to the order within a BW64 file. This makes it easier to create an object from the data read from a file. Cumbersome values like bytesPerSecond or blockAlignment can be omitted (thus set to None). But: if they are set, they have to be correct. Otherwise a ValueError is raised.
To simplify the writing of files the FormatChunk (like every Chunk class in this module) has a asByteArray method. This method returns the correct byte array representation of the FormatChunk, which can be directly written to a file.
- asByteArray()¶
- property bitsPerSample¶
- property blockAlignment¶
- property bytesPerSecond¶
- property cbSize¶
- property channelCount¶
- property extraData¶
- property formatTag¶
- property sampleRate¶