DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
dspark::WavFile Class Reference

Complete WAV file reader and writer in pure C++20. More...

#include <WavFile.h>

Inheritance diagram for dspark::WavFile:
Collaboration diagram for dspark::WavFile:

Public Types

enum class  Encoding {
  Pcm , Float , ALaw , MuLaw ,
  ImaAdpcm , MsAdpcm
}
 How the samples of the file open for reading are coded. More...
 
enum class  Rf64Mode { Never , Auto , Always }
 Container used by the next openWrite(). More...
 

Public Member Functions

 ~WavFile () override
 
void setRf64Mode (Rf64Mode mode) noexcept
 Chooses the container of files opened for writing from now on.
 
Rf64Mode getRf64Mode () const noexcept
 Container chosen for files opened for writing.
 
bool openRead (const std::filesystem::path &path) override
 Opens a WAV file for reading.
 
bool openWrite (const std::filesystem::path &path, const AudioFileInfo &info) override
 Opens a WAV file for writing, overwriting if it exists.
 
AudioFileInfo getInfo () const override
 Format of the open file.
 
Encoding getEncoding () const noexcept
 Sample coding of the file open for reading (Pcm when none is).
 
bool readSamples (AudioBufferView< float > dest) override
 Reads samples from the start of the file into the destination view.
 
bool readSamples (AudioBufferView< float > dest, int64_t startFrame, int64_t numFrames) override
 Reads a specific range of sample frames. Useful for chunked streaming.
 
bool writeSamples (AudioBufferView< const float > src) override
 Writes samples from the view to the file.
 
void close () override
 Finalizes file headers and releases system handles.
 
bool isOpen () const noexcept override
 Checks if a valid file handle is currently open.
 
bool hasWriteError () const noexcept override
 True when a write or the header finalisation of the current or most recent writer failed.
 
- Public Member Functions inherited from dspark::AudioFile
virtual ~AudioFile ()=default
 

Detailed Description

Complete WAV file reader and writer in pure C++20.

Inherits from AudioFile to provide a uniform interface. Handles both standard PCM and IEEE float formats, mono through multi-channel (up to 64 channels), and reads G.711 and IMA/Microsoft ADPCM files.

Note
An ADPCM block whose header is out of range (an IMA step index above 88, a Microsoft predictor beyond the coefficient table) makes a read that touches it return false. Reads of other blocks are unaffected.
Files with more than 2 channels or more than 16 bits per sample are written as WAVE_FORMAT_EXTENSIBLE, per the Microsoft specification. Plain PCM/float headers are used otherwise for maximum compatibility.

Definition at line 96 of file WavFile.h.

Member Enumeration Documentation

◆ Encoding

enum class dspark::WavFile::Encoding
strong

How the samples of the file open for reading are coded.

Enumerator
Pcm 

Linear integer PCM (including EXTENSIBLE and 12/20-bit).

Float 

IEEE 754 float.

ALaw 

ITU-T G.711 A-law.

MuLaw 

ITU-T G.711 mu-law.

ImaAdpcm 

IMA/DVI ADPCM, 4 bits per sample.

MsAdpcm 

Microsoft ADPCM, 4 bits per sample.

Definition at line 100 of file WavFile.h.

◆ Rf64Mode

enum class dspark::WavFile::Rf64Mode
strong

Container used by the next openWrite().

Enumerator
Never 

Classic RIFF/WAVE, at most 4 GiB (the default).

Auto 

RIFF with a reserved ds64 slot, promoted to RF64 beyond 4 GiB.

Always 

RF64 whatever the size.

Definition at line 111 of file WavFile.h.

Constructor & Destructor Documentation

◆ ~WavFile()

dspark::WavFile::~WavFile ( )
inlineoverride

Definition at line 118 of file WavFile.h.

Member Function Documentation

◆ close()

void dspark::WavFile::close ( )
inlineoverridevirtual

Finalizes file headers and releases system handles.

Safe to call multiple times. Implementations invoke this from their destructor, so an open file is always finalized; call it explicitly, then hasWriteError(), when you need to know that a written file is complete (a destructor cannot report failure), or to release the file before the object dies.

Implements dspark::AudioFile.

Definition at line 313 of file WavFile.h.

◆ getEncoding()

Encoding dspark::WavFile::getEncoding ( ) const
inlinenoexcept

Sample coding of the file open for reading (Pcm when none is).

Definition at line 217 of file WavFile.h.

◆ getInfo()

AudioFileInfo dspark::WavFile::getInfo ( ) const
inlineoverridevirtual

Format of the open file.

bitsPerSample is the fmt chunk's wBitsPerSample for PCM and float: the container width for WAVE_FORMAT_EXTENSIBLE (32 for 24-in-32) and the declared depth for a plain header (12 for 12-bit in a 16-bit container). The companded and ADPCM codings report 16, the linear resolution they decode to, so that writing the decoded audio at that depth is lossless; getEncoding() tells them apart from 16-bit PCM.

Implements dspark::AudioFile.

Definition at line 214 of file WavFile.h.

◆ getRf64Mode()

Rf64Mode dspark::WavFile::getRf64Mode ( ) const
inlinenoexcept

Container chosen for files opened for writing.

Definition at line 124 of file WavFile.h.

◆ hasWriteError()

bool dspark::WavFile::hasWriteError ( ) const
inlineoverridevirtualnoexcept

True when a write or the header finalisation of the current or most recent writer failed.

Remains readable after close() until the next open call.

Reimplemented from dspark::AudioFile.

Definition at line 345 of file WavFile.h.

◆ isOpen()

bool dspark::WavFile::isOpen ( ) const
inlineoverridevirtualnoexcept

Checks if a valid file handle is currently open.

Implements dspark::AudioFile.

Definition at line 334 of file WavFile.h.

◆ openRead()

bool dspark::WavFile::openRead ( const std::filesystem::path &  path)
inlineoverridevirtual

Opens a WAV file for reading.

Parameters
pathPlatform-independent path to the WAV file.
Returns
True if the file was found and contains a valid RIFF/WAVE header.

Implements dspark::AudioFile.

Definition at line 133 of file WavFile.h.

◆ openWrite()

bool dspark::WavFile::openWrite ( const std::filesystem::path &  path,
const AudioFileInfo &  info 
)
inlineoverridevirtual

Opens a WAV file for writing, overwriting if it exists.

The requested format is validated BEFORE the destination is touched: an unsupported format returns false without creating or truncating anything on disk.

Parameters
pathPlatform-independent output path.
infoConfiguration for sample rate, bit depth, and channels.
Returns
True if the file was created and the preliminary header written.

Implements dspark::AudioFile.

Definition at line 162 of file WavFile.h.

◆ readSamples() [1/2]

bool dspark::WavFile::readSamples ( AudioBufferView< float >  dest)
inlineoverridevirtual

Reads samples from the start of the file into the destination view.

Reads up to min(view length, file length) frames into up to min(view channels, file channels) channels. Excess buffer space and channels beyond those present in the file are left untouched.

Parameters
destBuffer view to receive the audio data.
Returns
True if read successfully. False if an I/O error occurred or no file is open for reading.

Implements dspark::AudioFile.

Definition at line 219 of file WavFile.h.

◆ readSamples() [2/2]

bool dspark::WavFile::readSamples ( AudioBufferView< float >  dest,
int64_t  startFrame,
int64_t  numFrames 
)
inlineoverridevirtual

Reads a specific range of sample frames. Useful for chunked streaming.

The requested range must lie entirely within the file (startFrame >= 0, numFrames > 0, startFrame + numFrames <= numSamples); out-of-range requests return false without reading. As with the full-file overload, the transfer is clamped to the view's capacity.

Parameters
destBuffer view to receive the audio data.
startFrameThe absolute frame index in the file to start reading from.
numFramesThe number of frames to read.
Returns
True if the range was read successfully.

Implements dspark::AudioFile.

Definition at line 228 of file WavFile.h.

◆ setRf64Mode()

void dspark::WavFile::setRf64Mode ( Rf64Mode  mode)
inlinenoexcept

Chooses the container of files opened for writing from now on.

Definition at line 121 of file WavFile.h.

◆ writeSamples()

bool dspark::WavFile::writeSamples ( AudioBufferView< const float >  src)
inlineoverridevirtual

Writes samples from the view to the file.

Converts float [-1.0, 1.0] back to the native bit-depth defined in openWrite(). May be called repeatedly to append blocks (streaming writes).

Parameters
srcBuffer view containing the data to write.
Returns
True on success, False if disk is full or an I/O error occurred.

Implements dspark::AudioFile.

Definition at line 270 of file WavFile.h.


The documentation for this class was generated from the following file: