Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
audio_pool.h
1// SPDX-FileCopyrightText: © 2019-2025 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:
34 using ProjectPoolPathGetter = std::function<fs::path (bool backup)>;
35 using SampleRateGetter = std::function<units::sample_rate_t ()>;
36
37 AudioPool (
38 dsp::FileAudioSourceRegistry &file_audio_source_registry,
39 ProjectPoolPathGetter path_getter,
40 SampleRateGetter sr_getter);
41
42public:
48 void init_loaded ();
49
58 auto duplicate_clip (const FileAudioSource::Uuid &clip_id, bool write_file)
59 -> FileAudioSourceUuidReference;
60
66 [[nodiscard]] fs::path
67 get_clip_path (const dsp::FileAudioSource::Uuid &id, bool is_backup) const;
68
78 void write_clip (const FileAudioSource * clip, bool parts, bool backup);
79
87 void remove_unused (bool backup);
88
99
109 void write_to_disk (bool is_backup);
110
111#if 0
121 std::unique_ptr<juce::PositionableAudioSource>
122 generate_audio_source (const FileAudioSource::Uuid &id) const;
123#endif
124
125 auto get_clip_ptrs () const
126 {
127 return std::views::transform (
128 clip_registry_.get_hash_map () | std::views::values,
129 [] (const auto &clip) { return std::get<dsp::FileAudioSource *> (clip); });
130 }
131
132private:
133 friend void init_from (
134 AudioPool &obj,
135 const AudioPool &other,
136 utils::ObjectCloneType clone_type);
137
138 static constexpr auto kClipsKey = "clips"sv;
139 static constexpr auto kLastKnownFileHashesKey = "lastKnownFileHashes"sv;
140 friend void to_json (nlohmann::json &j, const AudioPool &pool)
141 {
142 j[AudioPool::kLastKnownFileHashesKey] = pool.last_known_file_hashes_;
143 }
144 friend void from_json (const nlohmann::json &j, AudioPool &pool)
145 {
146 j.at (AudioPool::kLastKnownFileHashesKey)
147 .get_to (pool.last_known_file_hashes_);
148 }
149
150private:
151 SampleRateGetter sample_rate_getter_;
152 ProjectPoolPathGetter project_pool_path_getter_;
153
157 FileAudioSourceRegistry &clip_registry_;
158
161 boost::unordered::concurrent_flat_map<FileAudioSource::Uuid, utils::hash::HashT>
162 last_known_file_hashes_;
163};
164} // namespace zrythm::dsp
165
166// Formatter for AudioPool
167template <>
168struct fmt::formatter<zrythm::dsp::AudioPool> : fmt::formatter<std::string_view>
169{
170 template <typename FormatContext>
171 auto format (const zrythm::dsp::AudioPool &pool, FormatContext &ctx) const
172 {
173 std::stringstream ss;
174 ss << "\nAudio Pool:\n";
175 for (const auto &clip : pool.get_clip_ptrs ())
176 {
177 auto pool_path = pool.get_clip_path (clip->get_uuid (), false);
178 ss << fmt::format (
179 "[Clip {}] {}: {}\n", clip->get_uuid (), clip->get_name (), pool_path);
180 }
181
182 return fmt::formatter<std::string_view>::format (
183 fmt::format ("{}", ss.str ()), ctx);
184 }
185};
Audio clips for the pool.
A manager for a registry of FileAudioSource inside a project.
Definition audio_pool.h:26
void remove_unused(bool backup)
Removes and frees (and removes the files for) all clips not used by the project or undo stacks.
fs::path get_clip_path(const dsp::FileAudioSource::Uuid &id, bool is_backup) const
Gets the path of a clip matching name from the pool.
void init_loaded()
Initializes the audio pool after deserialization.
std::function< fs::path(bool backup)> ProjectPoolPathGetter
Returns a path that will be used to manage audio files in.
Definition audio_pool.h:34
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...