DSPark 1.6.1
Header-only audio DSP framework in pure C++20 — zero dependencies
Loading...
Searching...
No Matches
AudioFile.h
Go to the documentation of this file.
1// DSPark - Professional Audio DSP Framework
2// Copyright (c) 2026 Cristian Moresi - MIT License
3
4#pragma once
5
24#include "../Core/AudioBuffer.h"
25#include "../Core/AudioSpec.h"
26
27#include <algorithm>
28#include <cstdint>
29#include <filesystem>
30#include <limits>
31
32namespace dspark {
33
39{
41 double sampleRate = 44100.0;
42
44 uint32_t numChannels = 0;
45
47 int64_t numSamples = 0;
48
56 uint32_t bitsPerSample = 16;
57
59 bool isFloatingPoint = false;
60
78 [[nodiscard]] AudioSpec toSpec(int defaultBlockSize = 0) const noexcept
79 {
80 // Clamp to [0, INT_MAX]: numSamples beyond 2^31-1 frames cannot be a
81 // single block, and a negative count (corrupt info) must not produce
82 // a "valid-looking" negative block size.
83 const int64_t clamped = std::clamp<int64_t>(
84 numSamples, 0, std::numeric_limits<int>::max());
85 const int safeFullLength = static_cast<int>(clamped);
86
87 return {
88 .sampleRate = sampleRate,
89 .maxBlockSize = (defaultBlockSize > 0) ? defaultBlockSize : safeFullLength,
90 .numChannels = static_cast<int>(numChannels)
91 };
92 }
93};
94
107{
108public:
109 virtual ~AudioFile() = default;
110
119 [[nodiscard]] virtual bool openRead(const std::filesystem::path& path) = 0;
120
130 [[nodiscard]] virtual bool openWrite(const std::filesystem::path& path, const AudioFileInfo& info) = 0;
131
136 [[nodiscard]] virtual AudioFileInfo getInfo() const = 0;
137
149 [[nodiscard]] virtual bool readSamples(AudioBufferView<float> dest) = 0;
150
164 [[nodiscard]] virtual bool readSamples(AudioBufferView<float> dest,
165 int64_t startFrame, int64_t numFrames) = 0;
166
176 [[nodiscard]] virtual bool writeSamples(AudioBufferView<const float> src) = 0;
177
186 virtual void close() = 0;
187
189 [[nodiscard]] virtual bool isOpen() const noexcept = 0;
190};
191
192} // namespace dspark
Non-owning view over audio channel data.
Definition AudioBuffer.h:50
Abstract base class for audio file readers and writers.
Definition AudioFile.h:107
virtual bool openRead(const std::filesystem::path &path)=0
Opens a file for reading.
virtual bool isOpen() const noexcept=0
Checks if a valid file handle is currently open.
virtual bool readSamples(AudioBufferView< float > dest, int64_t startFrame, int64_t numFrames)=0
Reads a specific range of sample frames. Useful for chunked streaming.
virtual ~AudioFile()=default
virtual bool writeSamples(AudioBufferView< const float > src)=0
Writes samples from the view to the file.
virtual AudioFileInfo getInfo() const =0
Retrieves metadata of the currently opened file.
virtual bool openWrite(const std::filesystem::path &path, const AudioFileInfo &info)=0
Opens a file for writing, creating it or overwriting if it exists.
virtual bool readSamples(AudioBufferView< float > dest)=0
Reads samples from the start of the file into the destination view.
virtual void close()=0
Finalizes file headers and releases system handles.
Main namespace for the DSPark framework.
Metadata describing an audio file's format and dimensions.
Definition AudioFile.h:39
uint32_t numChannels
Number of audio channels (1 = mono, 2 = stereo).
Definition AudioFile.h:44
bool isFloatingPoint
True if the file stores floating-point samples (IEEE 754).
Definition AudioFile.h:59
uint32_t bitsPerSample
Bits per sample in the stored format (8, 16, 24, 32, 64).
Definition AudioFile.h:56
AudioSpec toSpec(int defaultBlockSize=0) const noexcept
Converts this file info to an AudioSpec for processor preparation.
Definition AudioFile.h:78
double sampleRate
Sample rate in Hz (e.g., 44100.0, 48000.0, 96000.0).
Definition AudioFile.h:41
int64_t numSamples
Total number of sample frames in the file.
Definition AudioFile.h:47
Describes the audio environment for a DSP processor.
Definition AudioSpec.h:35