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 using bpm_t = float;
35
36public:
37 FileAudioSource (QObject * parent = nullptr);
38
50 const std::filesystem::path &full_path,
51 units::sample_rate_t project_sample_rate,
52 bpm_t current_bpm,
53 QObject * parent = nullptr);
54
63 utils::audio::BitDepth bit_depth,
64 units::sample_rate_t project_sample_rate,
65 bpm_t current_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 bpm_t current_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,
91 current_bpm,
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_; }
110 auto get_bpm () const { return bpm_; }
111 const auto &get_samples () const { return ch_frames_; }
112 auto get_samplerate () const { return samplerate_; }
113
114 void set_name (const utils::Utf8String &name) { name_ = name; }
115
123
134 const utils::audio::AudioBuffer &src_frames,
135 units::sample_u64_t start_frame);
136
147 const float * frames,
148 units::sample_u64_t start_frame,
149 units::sample_u64_t num_frames_per_channel,
150 channels_t channels);
151
156 {
157 ch_frames_.setSize (ch_frames_.getNumChannels (), 0, false, true);
158 Q_EMIT samplesChanged ();
159 }
160
161 auto get_num_channels () const { return ch_frames_.getNumChannels (); };
162 auto get_num_frames () const { return ch_frames_.getNumSamples (); };
163
174 const std::filesystem::path &full_path,
175 units::sample_rate_t project_sample_rate,
176 std::optional<bpm_t> bpm_to_set);
177
178private:
179 friend void init_from (
180 FileAudioSource &obj,
181 const FileAudioSource &other,
182 utils::ObjectCloneType clone_type);
183
184 void convert_mono_to_stereo ();
185
186 friend void to_json (nlohmann::json &j, const FileAudioSource &clip);
187 friend void from_json (const nlohmann::json &j, FileAudioSource &clip);
188
189private:
191 utils::Utf8String name_;
192
196 utils::audio::AudioBuffer ch_frames_;
197
201 bpm_t bpm_{};
202
207 units::sample_rate_t samplerate_;
208
212 utils::audio::BitDepth bit_depth_{};
213};
214
215// ========================================================================
216
221{
222public:
232 const FileAudioSource &source,
233 std::filesystem::path path,
234 bool parts);
235
244
250
256
257private:
261 bool parts_;
262
263 const FileAudioSource &source_;
264 std::unique_ptr<juce::AudioFormatWriter> writer_;
265 std::filesystem::path writer_path_;
266
272 units::sample_u64_t frames_written_;
273
281 utils::MonotonicTime last_write_{};
282};
283
284using FileAudioSourcePtrVariant = std::variant<FileAudioSource *>;
285using FileAudioSourceUuidReference = utils::TypedUuidReference<FileAudioSource>;
286
287} // namespace zrythm::dsp
288
289DEFINE_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, bpm_t current_bpm, const utils::Utf8String &name, QObject *parent=nullptr)
Creates an audio clip by copying the given buffer.
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.
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, bpm_t current_bpm, const utils::Utf8String &name, QObject *parent=nullptr)
Creates an audio clip by copying the given interleaved float array.
void init_from_file(const std::filesystem::path &full_path, units::sample_rate_t project_sample_rate, std::optional< bpm_t > bpm_to_set)
Initializes members from an audio file.
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 expand_with_frames(const utils::audio::AudioBuffer &frames)
Expands (appends to the end) the frames in the clip by the given frames.
FileAudioSource(const std::filesystem::path &full_path, units::sample_rate_t project_sample_rate, bpm_t current_bpm, QObject *parent=nullptr)
Creates an audio clip from a file.
Q_SIGNAL void samplesChanged()
Emitted when the source samples change.
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.