Zrythm v2.0.0-alpha.1
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
file_audio_source.h
1// SPDX-FileCopyrightText: © 2019-2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "utils/audio.h"
7#include "utils/audio_file.h"
8#include "utils/icloneable.h"
9#include "utils/monotonic_time_provider.h"
10#include "utils/typed_uuid_reference.h"
11#include "utils/units.h"
12#include "utils/utf8_string.h"
13#include "utils/uuid_identifiable_object.h"
14
15#include <juce_audio_formats/juce_audio_formats.h>
16
17namespace zrythm::dsp
18{
19
25class FileAudioSource final
26 : public utils::UuidIdentifiableObject<FileAudioSource>
27{
28 Q_OBJECT
29
30public:
31 using BitDepth = zrythm::utils::audio::BitDepth;
32 using AudioFile = zrythm::utils::audio::AudioFile;
33 using channels_t = uint_fast8_t;
34
35public:
36 FileAudioSource (QObject * parent = nullptr);
37
49 const std::filesystem::path &full_path,
50 units::sample_rate_t project_sample_rate,
51 QObject * parent = nullptr);
52
63 utils::audio::BitDepth bit_depth,
64 units::sample_rate_t project_sample_rate,
65 units::bpm_t source_bpm,
66 const utils::Utf8String &name,
67 QObject * parent = nullptr);
68
78 const float * arr,
79 units::sample_u64_t nframes,
80 channels_t channels,
81 zrythm::utils::audio::BitDepth bit_depth,
82 units::sample_rate_t project_sample_rate,
83 units::bpm_t source_bpm,
84 const utils::Utf8String &name,
85 QObject * parent = nullptr)
86 : FileAudioSource (
87 *utils::audio::AudioBuffer::
88 from_interleaved (arr, nframes.in (units::samples), channels),
89 bit_depth,
90 project_sample_rate,
92 name,
93 parent)
94 {
95 }
96
97 // ========================================================================
98 // QML Interface
99 // ========================================================================
100
104 Q_SIGNAL void samplesChanged ();
105
106 // ========================================================================
107
108 auto get_bit_depth () const { return bit_depth_; }
109 auto get_name () const { return name_; }
118 auto source_bpm () const { return bpm_; }
119 const auto &get_samples () const { return ch_frames_; }
120 auto get_samplerate () const { return samplerate_; }
121
122 void set_name (const utils::Utf8String &name) { name_ = name; }
123
131
142 const utils::audio::AudioBuffer &src_frames,
143 units::sample_u64_t start_frame);
144
155 const float * frames,
156 units::sample_u64_t start_frame,
157 units::sample_u64_t num_frames_per_channel,
158 channels_t channels);
159
164 {
165 ch_frames_.setSize (ch_frames_.getNumChannels (), 0, false, true);
166 Q_EMIT samplesChanged ();
167 }
168
169 auto get_num_channels () const { return ch_frames_.getNumChannels (); };
170 auto get_num_frames () const { return ch_frames_.getNumSamples (); };
171
185 const std::filesystem::path &full_path,
186 units::sample_rate_t project_sample_rate,
187 std::optional<units::bpm_t> bpm_to_set);
188
189private:
190 friend void init_from (
191 FileAudioSource &obj,
192 const FileAudioSource &other,
193 utils::ObjectCloneType clone_type);
194
195 void convert_mono_to_stereo ();
196
197 friend void to_json (nlohmann::json &j, const FileAudioSource &clip);
198 friend void from_json (const nlohmann::json &j, FileAudioSource &clip);
199
200private:
202 utils::Utf8String name_;
203
207 utils::audio::AudioBuffer ch_frames_;
208
218 units::bpm_t bpm_{};
219
224 units::sample_rate_t samplerate_;
225
229 utils::audio::BitDepth bit_depth_{};
230};
231
232// ========================================================================
233
238{
239public:
249 const FileAudioSource &source,
250 std::filesystem::path path,
251 bool parts);
252
261
267
273
274private:
278 bool parts_;
279
280 const FileAudioSource &source_;
281 std::unique_ptr<juce::AudioFormatWriter> writer_;
282 std::filesystem::path writer_path_;
283
289 units::sample_u64_t frames_written_;
290
298 utils::MonotonicTime last_write_{};
299};
300
301using FileAudioSourcePtrVariant = std::variant<FileAudioSource *>;
302using FileAudioSourceUuidReference = utils::TypedUuidReference<FileAudioSource>;
303
304} // namespace zrythm::dsp
305
306DEFINE_UUID_HASH_SPECIALIZATION (zrythm::dsp::FileAudioSource::Uuid)
bool enough_time_elapsed_since_last_write() const
Returns whether enough time has elapsed since the last write to file.
void finalize_buffered_write()
To be called after the file has been written via write_file_buffered().
FileAudioSourceWriter(const FileAudioSource &source, std::filesystem::path path, bool parts)
Writes the given audio clip data to a file.
void write_to_file()
Write the file either in parts or whole (depending on constructor param).
Audio clips for the pool.
FileAudioSource(const utils::audio::AudioBuffer &buf, utils::audio::BitDepth bit_depth, units::sample_rate_t project_sample_rate, units::bpm_t source_bpm, const utils::Utf8String &name, QObject *parent=nullptr)
Creates an audio clip by copying the given buffer.
FileAudioSource(const float *arr, units::sample_u64_t nframes, channels_t channels, zrythm::utils::audio::BitDepth bit_depth, units::sample_rate_t project_sample_rate, units::bpm_t source_bpm, const utils::Utf8String &name, QObject *parent=nullptr)
Creates an audio clip by copying the given interleaved float array.
void clear_frames()
Unloads the clip's frames from memory.
void replace_frames(const utils::audio::AudioBuffer &src_frames, units::sample_u64_t start_frame)
Replaces the clip's frames starting from start_frame with frames.
auto source_bpm() const
The clip's permanent source BPM (its intrinsic musical tempo).
void replace_frames_from_interleaved(const float *frames, units::sample_u64_t start_frame, units::sample_u64_t num_frames_per_channel, channels_t channels)
Replaces the clip's frames starting from start_frame with frames.
void init_from_file(const std::filesystem::path &full_path, units::sample_rate_t project_sample_rate, std::optional< units::bpm_t > bpm_to_set)
Initializes members from an audio file.
void expand_with_frames(const utils::audio::AudioBuffer &frames)
Expands (appends to the end) the frames in the clip by the given frames.
Q_SIGNAL void samplesChanged()
Emitted when the source samples change.
FileAudioSource(const std::filesystem::path &full_path, units::sample_rate_t project_sample_rate, QObject *parent=nullptr)
Creates an audio clip from a file.
Typed, reference-counted UUID reference into an IObjectRegistry.
Lightweight UTF-8 string wrapper with safe conversions.
Definition utf8_string.h:37
CRTP base that adds a typed UUID strong-typedef to a class hierarchy.
RAII class to read and write audio files (or their metadata).
Definition audio_file.h:44
String utilities.