DSPark 1.6.1
Header-only audio DSP framework in pure C++20 — zero dependencies
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 Member Functions

 ~WavFile () override
 
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
 Retrieves metadata of the currently opened file.
 
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.
 
- 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).

Note
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 66 of file WavFile.h.

Constructor & Destructor Documentation

◆ ~WavFile()

dspark::WavFile::~WavFile ( )
inlineoverride

Definition at line 69 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 when you need the write error-checked (a destructor cannot report failure) or the file released before the object dies.

Implements dspark::AudioFile.

Definition at line 226 of file WavFile.h.

◆ getInfo()

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

Retrieves metadata of the currently opened file.

Returns
AudioFileInfo struct. Returns default values if no file is open.

Implements dspark::AudioFile.

Definition at line 145 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 240 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 78 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 106 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 147 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 156 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 191 of file WavFile.h.


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