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"
15#include <juce_audio_formats/juce_audio_formats.h>
25class FileAudioSource final
31 using BitDepth = zrythm::utils::audio::BitDepth;
33 using channels_t = uint_fast8_t;
37 FileAudioSource (QObject * parent =
nullptr);
50 const std::filesystem::path &full_path,
51 units::sample_rate_t project_sample_rate,
53 QObject * parent =
nullptr);
63 utils::audio::BitDepth bit_depth,
64 units::sample_rate_t project_sample_rate,
67 QObject * parent =
nullptr);
79 units::sample_u64_t nframes,
81 zrythm::utils::audio::BitDepth bit_depth,
82 units::sample_rate_t project_sample_rate,
85 QObject * parent =
nullptr)
87 *
utils::audio::AudioBuffer::
88 from_interleaved (arr, nframes.in (units::samples), channels),
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_; }
114 void set_name (
const utils::Utf8String &name) { name_ = name; }
135 units::sample_u64_t start_frame);
147 const float * frames,
148 units::sample_u64_t start_frame,
149 units::sample_u64_t num_frames_per_channel,
150 channels_t channels);
157 ch_frames_.setSize (ch_frames_.getNumChannels (), 0,
false,
true);
161 auto get_num_channels ()
const {
return ch_frames_.getNumChannels (); };
162 auto get_num_frames ()
const {
return ch_frames_.getNumSamples (); };
174 const std::filesystem::path &full_path,
175 units::sample_rate_t project_sample_rate,
176 std::optional<bpm_t> bpm_to_set);
179 friend void init_from (
180 FileAudioSource &obj,
181 const FileAudioSource &other,
184 void convert_mono_to_stereo ();
186 friend void to_json (nlohmann::json &j,
const FileAudioSource &clip);
187 friend void from_json (
const nlohmann::json &j, FileAudioSource &clip);
207 units::sample_rate_t samplerate_;
212 utils::audio::BitDepth bit_depth_{};
233 std::filesystem::path path,
264 std::unique_ptr<juce::AudioFormatWriter> writer_;
265 std::filesystem::path writer_path_;
272 units::sample_u64_t frames_written_;
281 utils::MonotonicTime last_write_{};
284using FileAudioSourcePtrVariant = std::variant<FileAudioSource *>;
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.
CRTP base that adds a typed UUID strong-typedef to a class hierarchy.
RAII class to read and write audio files (or their metadata).