DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
AudioFile.h
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
60 uint32_t bitsPerSample = 16;
61
63 bool isFloatingPoint = false;
64
82 [[nodiscard]] AudioSpec toSpec(int defaultBlockSize = 0) const noexcept
83 {
84 // Clamp to [0, INT_MAX]: numSamples beyond 2^31-1 frames cannot be a
85 // single block, and a negative count (corrupt info) must not produce
86 // a "valid-looking" negative block size.
87 const int64_t clamped = std::clamp<int64_t>(
88 numSamples, 0, std::numeric_limits<int>::max());
89 const int safeFullLength = static_cast<int>(clamped);
90
91 return {
92 .sampleRate = sampleRate,
93 .maxBlockSize = (defaultBlockSize > 0) ? defaultBlockSize : safeFullLength,
94 .numChannels = static_cast<int>(numChannels)
95 };
96 }
97};
98
111{
112public:
113 virtual ~AudioFile() = default;
114
123 [[nodiscard]] virtual bool openRead(const std::filesystem::path& path) = 0;
124
134 [[nodiscard]] virtual bool openWrite(const std::filesystem::path& path, const AudioFileInfo& info) = 0;
135
140 [[nodiscard]] virtual AudioFileInfo getInfo() const = 0;
141
153 [[nodiscard]] virtual bool readSamples(AudioBufferView<float> dest) = 0;
154
168 [[nodiscard]] virtual bool readSamples(AudioBufferView<float> dest,
169 int64_t startFrame, int64_t numFrames) = 0;
170
180 [[nodiscard]] virtual bool writeSamples(AudioBufferView<const float> src) = 0;
181
191 virtual void close() = 0;
192
194 [[nodiscard]] virtual bool isOpen() const noexcept = 0;
195
206 [[nodiscard]] virtual bool hasWriteError() const noexcept { return false; }
207};
208
209} // namespace dspark
Non-owning view over audio channel data.
Definition AudioBuffer.h:53
Abstract base class for audio file readers and writers.
Definition AudioFile.h:111
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 hasWriteError() const noexcept
Reports whether the current or most recent writer hit an I/O failure.
Definition AudioFile.h:206
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:63
uint32_t bitsPerSample
Bits per sample in the stored format (8, 16, 24, 32, 64).
Definition AudioFile.h:60
AudioSpec toSpec(int defaultBlockSize=0) const noexcept
Converts this file info to an AudioSpec for processor preparation.
Definition AudioFile.h:82
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:37