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/types.h"
11#include "utils/units.h"
12#include "utils/uuid_identifiable_object.h"
22class FileAudioSource final
29 using BitDepth = zrythm::utils::audio::BitDepth;
33 FileAudioSource (QObject * parent =
nullptr) : QObject (parent) { }
46 const fs::path &full_path,
47 units::sample_rate_t project_sample_rate,
49 QObject * parent =
nullptr);
59 utils::audio::BitDepth bit_depth,
60 units::sample_rate_t project_sample_rate,
63 QObject * parent =
nullptr);
77 zrythm::utils::audio::BitDepth bit_depth,
78 units::sample_rate_t project_sample_rate,
81 QObject * parent =
nullptr)
83 *
utils::audio::AudioBuffer::from_interleaved (arr, nframes, channels),
104 units::sample_rate_t project_sample_rate,
107 QObject * parent =
nullptr);
120 auto get_bit_depth ()
const {
return bit_depth_; }
121 auto get_name ()
const {
return name_; }
122 auto get_bpm ()
const {
return bpm_; }
123 const auto &get_samples ()
const {
return ch_frames_; }
124 auto get_samplerate ()
const {
return samplerate_; }
126 void set_name (
const utils::Utf8String &name) { name_ = name; }
159 const float * frames,
169 ch_frames_.setSize (ch_frames_.getNumChannels (), 0,
false,
true);
173 auto get_num_channels ()
const {
return ch_frames_.getNumChannels (); };
174 auto get_num_frames ()
const {
return ch_frames_.getNumSamples (); };
186 const fs::path &full_path,
187 units::sample_rate_t project_sample_rate,
188 std::optional<bpm_t> bpm_to_set);
191 friend void init_from (
192 FileAudioSource &obj,
193 const FileAudioSource &other,
196 static constexpr auto kNameKey =
"name"sv;
197 static constexpr auto kBpmKey =
"bpm"sv;
198 static constexpr auto kBitDepthKey =
"bitDepth"sv;
199 static constexpr auto kSamplerateKey =
"samplerate"sv;
200 friend void to_json (nlohmann::json &j,
const FileAudioSource &clip)
202 to_json (j,
static_cast<const UuidIdentifiableObject &
> (clip));
203 j[kNameKey] = clip.name_;
204 j[kBpmKey] = clip.bpm_;
205 j[kBitDepthKey] = clip.bit_depth_;
206 j[kSamplerateKey] = clip.samplerate_;
210 from_json (j,
static_cast<UuidIdentifiableObject &
> (clip));
211 j.at (kNameKey).get_to (clip.name_);
212 j.at (kBpmKey).get_to (clip.bpm_);
213 j.at (kBitDepthKey).get_to (clip.bit_depth_);
214 j.at (kSamplerateKey).get_to (clip.samplerate_);
235 units::sample_rate_t samplerate_;
240 utils::audio::BitDepth bit_depth_{};
292 std::unique_ptr<juce::AudioFormatWriter> writer_;
293 fs::path writer_path_;
309 utils::MonotonicTime last_write_{};
312using FileAudioSourcePtrVariant = std::variant<FileAudioSource *>;
313using FileAudioSourceRegistry =
314 utils::OwningObjectRegistry<FileAudioSourcePtrVariant, FileAudioSource>;
315using FileAudioSourceUuidReference =
316 utils::UuidReference<FileAudioSourceRegistry>;
320DEFINE_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.
FileAudioSourceWriter(const FileAudioSource &source, fs::path path, bool parts)
Writes the given audio clip data to a file.
void finalize_buffered_write()
To be called after the file has been written via write_file_buffered().
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, unsigned_frame_t start_frame)
Replaces the clip's frames starting from start_frame with frames.
FileAudioSource(const fs::path &full_path, units::sample_rate_t project_sample_rate, bpm_t current_bpm, QObject *parent=nullptr)
Creates an audio clip from a file.
FileAudioSource(channels_t channels, unsigned_frame_t nframes, units::sample_rate_t project_sample_rate, bpm_t current_bpm, const utils::Utf8String &name, QObject *parent=nullptr)
Create an audio clip while recording.
void expand_with_frames(const utils::audio::AudioBuffer &frames)
Expands (appends to the end) the frames in the clip by the given frames.
void replace_frames_from_interleaved(const float *frames, unsigned_frame_t start_frame, unsigned_frame_t num_frames_per_channel, channels_t channels)
Replaces the clip's frames starting from start_frame with frames.
FileAudioSource(const float *arr, unsigned_frame_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.
Q_SIGNAL void samplesChanged()
Emitted when the source samples change.
void init_from_file(const fs::path &full_path, units::sample_rate_t project_sample_rate, std::optional< bpm_t > bpm_to_set)
Initializes members from an audio file.
Lightweight UTF-8 string wrapper with safe conversions.
Base class for objects that need to be uniquely identified by UUID.
RAII class to read and write audio files (or their metadata).
uint_fast64_t unsigned_frame_t
Unsigned type for frame index.
uint_fast8_t channels_t
Number of channels.