Zrythm v2.0.0-alpha.1
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
audio_pool.h
1// SPDX-FileCopyrightText: © 2019-2026 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "dsp/file_audio_source.h"
7#include "utils/hash.h"
8#include "utils/units.h"
9
10#include <boost/unordered/concurrent_flat_map.hpp>
11
12namespace zrythm::dsp
13{
14
25struct AudioPool final
26{
27public:
35 std::function<std::filesystem::path (bool backup)>;
36 using SampleRateGetter = std::function<units::sample_rate_t ()>;
37
38 AudioPool (
39 utils::IObjectRegistry &registry,
40 ProjectPoolPathGetter path_getter,
41 SampleRateGetter sr_getter);
42
43public:
49 void init_loaded ();
50
59 auto duplicate_clip (const FileAudioSource::Uuid &clip_id, bool write_file)
60 -> FileAudioSourceUuidReference;
61
67 [[nodiscard]] std::filesystem::path
68 get_clip_path (const dsp::FileAudioSource::Uuid &id, bool is_backup) const;
69
79 void write_clip (const FileAudioSource * clip, bool parts, bool backup);
80
88 void remove_unused (bool backup);
89
100
110 void write_to_disk (bool is_backup);
111
112 void
113 for_each_clip (std::function<void (dsp::FileAudioSource &)> visitor) const;
114
115private:
116 friend void init_from (
117 AudioPool &obj,
118 const AudioPool &other,
119 utils::ObjectCloneType clone_type);
120
121 friend void to_json (nlohmann::json &j, const AudioPool &pool);
122 friend void from_json (const nlohmann::json &j, AudioPool &pool);
123
124private:
125 SampleRateGetter sample_rate_getter_;
126 ProjectPoolPathGetter project_pool_path_getter_;
127
131 utils::IObjectRegistry &registry_;
132
135 boost::unordered::concurrent_flat_map<FileAudioSource::Uuid, utils::hash::HashT>
136 last_known_file_hashes_;
137};
138} // namespace zrythm::dsp
139
140// Formatter for AudioPool
141template <>
142struct fmt::formatter<zrythm::dsp::AudioPool> : fmt::formatter<std::string_view>
143{
144 template <typename FormatContext>
145 auto format (const zrythm::dsp::AudioPool &pool, FormatContext &ctx) const
146 {
147 std::stringstream ss;
148 ss << "\nAudio Pool:\n";
149 pool.for_each_clip ([&] (const auto &clip) {
150 auto pool_path = pool.get_clip_path (clip.get_uuid (), false);
151 ss << fmt::format (
152 "[Clip {}] {}: {}\n", clip.get_uuid (), clip.get_name (), pool_path);
153 });
154
155 return fmt::formatter<std::string_view>::format (
156 fmt::format ("{}", ss.str ()), ctx);
157 }
158};
Audio clips for the pool.
Abstract interface for a UUID-keyed object registry.
A manager for a registry of FileAudioSource inside a project.
Definition audio_pool.h:26
std::filesystem::path get_clip_path(const dsp::FileAudioSource::Uuid &id, bool is_backup) const
Gets the path of a clip matching name from the pool.
std::function< std::filesystem::path(bool backup)> ProjectPoolPathGetter
Returns a path that will be used to manage audio files in.
Definition audio_pool.h:34
void remove_unused(bool backup)
Removes and frees (and removes the files for) all clips not used by the project or undo stacks.
void init_loaded()
Initializes the audio pool after deserialization.
void write_to_disk(bool is_backup)
Writes all the clips to disk.
auto duplicate_clip(const FileAudioSource::Uuid &clip_id, bool write_file) -> FileAudioSourceUuidReference
Duplicates the clip with the given ID and returns the duplicate.
void write_clip(const FileAudioSource *clip, bool parts, bool backup)
Writes the clip to the pool as a wav file.
void reload_clip_frame_bufs()
Loads the frame buffers of clips currently in use in the project from their files and frees the buffe...