libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
audiofile.h
Go to the documentation of this file.
1
8#pragma once
9
12#include <promeki/core/string.h>
13#include <promeki/core/error.h>
14#include <promeki/proav/audio.h>
15
17
18class IODevice;
19
39class AudioFile {
40 public:
43
50
57 class Impl {
59 public:
64 Impl(Operation op) : _operation(op) {}
65
67 virtual ~Impl();
68
73 bool isValid() const {
74 return _operation != InvalidOperation;
75 }
76
82 return _operation;
83 }
84
89 const String &filename() const {
90 return _filename;
91 }
92
97 void setFilename(const String &val) {
98 _filename = val;
99 return;
100 }
101
106 AudioDesc desc() const {
107 return _desc;
108 }
109
114 void setDesc(const AudioDesc &val) {
115 _desc = val;
116 return;
117 }
118
123 IODevice *device() const {
124 return _device;
125 }
126
135 _device = dev;
136 _ownsDevice = false;
137 return;
138 }
139
144 const String &formatHint() const {
145 return _formatHint;
146 }
147
152 void setFormatHint(const String &val) {
153 _formatHint = val;
154 return;
155 }
156
161 virtual Error open();
162
164 virtual void close();
165
172 virtual Error read(Audio &audio, size_t maxSamples);
173
179 virtual Error write(const Audio &audio);
180
186 virtual Error seekToSample(size_t sample);
187
192 virtual size_t sampleCount() const;
193
194 protected:
195 Operation _operation;
196 String _filename;
197 AudioDesc _desc;
198 IODevice *_device = nullptr;
199 bool _ownsDevice = false;
200 String _formatHint;
201 };
202
212
224
231
238
241
246 AudioFile(Impl *impl) : d(SharedPtr<Impl>::takeOwnership(impl)) {};
247
252 bool isValid() const { return d->isValid(); }
253
258 Operation operation() const { return d->operation(); }
259
264 const String &filename() const { return d->filename(); }
265
270 void setFilename(const String &val) { d.modify()->setFilename(val); return; }
271
276 AudioDesc desc() const { return d->desc(); }
277
282 void setDesc(const AudioDesc &val) { d.modify()->setDesc(val); return; }
283
288 IODevice *device() const { return d->device(); }
289
294 Error open() { return d.modify()->open(); }
295
297 void close() { d.modify()->close(); return; }
298
305 Error read(Audio &audio, size_t maxSamples) { return d.modify()->read(audio, maxSamples); }
306
312 Error write(const Audio &audio) { return d.modify()->write(audio); }
313
319 Error seekToSample(size_t sample) { return d.modify()->seekToSample(sample); }
320
325 size_t sampleCount() const { return d->sampleCount(); }
326
327 private:
329};
330
Describes an audio format including sample type, rate, and channel count.
Definition audiodesc.h:28
Abstract implementation backend for AudioFile.
Definition audiofile.h:57
void setDesc(const AudioDesc &val)
Sets the audio description for writing.
Definition audiofile.h:114
void setFormatHint(const String &val)
Sets the format hint for device-based operation.
Definition audiofile.h:152
virtual Error seekToSample(size_t sample)
Seeks to a specific sample position in the file.
virtual size_t sampleCount() const
Returns the total number of samples in the file.
AudioDesc desc() const
Returns the audio description (format, channels, sample rate, etc.).
Definition audiofile.h:106
const String & formatHint() const
Returns the format hint (e.g. "wav"), no dot.
Definition audiofile.h:144
virtual Error write(const Audio &audio)
Writes audio samples to the file.
Impl(Operation op)
Constructs an Impl for the given operation.
Definition audiofile.h:64
virtual Error open()
Opens the audio file.
Operation operation() const
Returns the operation type for this implementation.
Definition audiofile.h:81
virtual void close()
Closes the audio file.
virtual ~Impl()
Virtual destructor.
void setFilename(const String &val)
Sets the filename for this audio file.
Definition audiofile.h:97
const String & filename() const
Returns the filename associated with this audio file.
Definition audiofile.h:89
void setDevice(IODevice *dev)
Sets the IODevice for this audio file.
Definition audiofile.h:134
IODevice * device() const
Returns the IODevice associated with this audio file.
Definition audiofile.h:123
virtual Error read(Audio &audio, size_t maxSamples)
Reads audio samples from the file.
bool isValid() const
Returns true if this implementation has a valid operation.
Definition audiofile.h:73
Audio file reader and writer.
Definition audiofile.h:39
static AudioFile createForOperation(Operation op, const String &filename)
Creates an AudioFile for the given operation and filename.
Error open()
Opens the audio file.
Definition audiofile.h:294
size_t sampleCount() const
Returns the total number of samples in the file.
Definition audiofile.h:325
Operation operation() const
Returns the operation type (Reader or Writer).
Definition audiofile.h:258
Error write(const Audio &audio)
Writes audio samples to the file.
Definition audiofile.h:312
static Result< AudioFile > createForOperation(Operation op, IODevice *device, const String &formatHint="")
Creates an AudioFile for the given operation using an IODevice.
AudioFile()
Default constructor. Creates an invalid AudioFile.
Definition audiofile.h:240
Operation
The type of operation to perform on an audio file.
Definition audiofile.h:45
@ Writer
Open the file for writing.
Definition audiofile.h:48
@ InvalidOperation
No valid operation.
Definition audiofile.h:46
@ Reader
Open the file for reading.
Definition audiofile.h:47
AudioDesc desc() const
Returns the audio description.
Definition audiofile.h:276
AudioFile(Impl *impl)
Constructs an AudioFile from an Impl pointer, taking ownership.
Definition audiofile.h:246
Error seekToSample(size_t sample)
Seeks to a specific sample position in the file.
Definition audiofile.h:319
void setFilename(const String &val)
Sets the filename for this audio file.
Definition audiofile.h:270
Error read(Audio &audio, size_t maxSamples)
Reads audio samples from the file.
Definition audiofile.h:305
IODevice * device() const
Returns the IODevice associated with this audio file.
Definition audiofile.h:288
static AudioFile createWriter(const String &filename)
Creates an AudioFile writer for the given filename.
Definition audiofile.h:237
void close()
Closes the audio file.
Definition audiofile.h:297
bool isValid() const
Returns true if this AudioFile has a valid operation.
Definition audiofile.h:252
void setDesc(const AudioDesc &val)
Sets the audio description for writing.
Definition audiofile.h:282
const String & filename() const
Returns the filename associated with this audio file.
Definition audiofile.h:264
static AudioFile createReader(const String &filename)
Creates an AudioFile reader for the given filename.
Definition audiofile.h:230
Object to hold some number of audio samples.
Definition audio.h:35
Lightweight error code wrapper for the promeki library.
Definition error.h:39
Abstract base class for all I/O devices.
Definition iodevice.h:29
Dynamic array container wrapping std::vector.
Definition list.h:40
A smart pointer class with reference counting and optional copy-on-write semantics.
Definition sharedptr.h:252
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
#define PROMEKI_SHARED(BASE)
Macro to simplify making a base object into a native shared object.
Definition sharedptr.h:44
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19