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/types.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
36 AudioPool (
37 dsp::FileAudioSourceRegistry &file_audio_source_registry,
38 ProjectPoolPathGetter path_getter,
39 SampleRateGetter sr_getter);
40
41public:
47 void init_loaded ();
48
57 auto duplicate_clip (const FileAudioSource::Uuid &clip_id, bool write_file)
58 -> FileAudioSourceUuidReference;
59
65 [[nodiscard]] fs::path
66 get_clip_path (const dsp::FileAudioSource::Uuid &id, bool is_backup) const;
67
77 void write_clip (const FileAudioSource * clip, bool parts, bool backup);
78
86 void remove_unused (bool backup);
87
98
108 void write_to_disk (bool is_backup);
109
110#if 0
120 std::unique_ptr<juce::PositionableAudioSource>
121 generate_audio_source (const FileAudioSource::Uuid &id) const;
122#endif
123
124 auto get_clip_ptrs () const
125 {
126 return std::views::transform (
127 clip_registry_.get_hash_map () | std::views::values,
128 [] (const auto &clip) { return std::get<dsp::FileAudioSource *> (clip); });
129 }
130
131private:
132 friend void init_from (
133 AudioPool &obj,
134 const AudioPool &other,
135 utils::ObjectCloneType clone_type);
136
137 static constexpr auto kClipsKey = "clips"sv;
138 static constexpr auto kLastKnownFileHashesKey = "lastKnownFileHashes"sv;
139 friend void to_json (nlohmann::json &j, const AudioPool &pool)
140 {
141 j[AudioPool::kLastKnownFileHashesKey] = pool.last_known_file_hashes_;
142 }
143 friend void from_json (const nlohmann::json &j, AudioPool &pool)
144 {
145 j.at (AudioPool::kLastKnownFileHashesKey)
146 .get_to (pool.last_known_file_hashes_);
147 }
148
149private:
150 SampleRateGetter sample_rate_getter_;
151 ProjectPoolPathGetter project_pool_path_getter_;
152
156 FileAudioSourceRegistry &clip_registry_;
157
160 boost::unordered::concurrent_flat_map<FileAudioSource::Uuid, utils::hash::HashT>
161 last_known_file_hashes_;
162};
163} // namespace zrythm::dsp
164
165// Formatter for AudioPool
166template <>
167struct fmt::formatter<zrythm::dsp::AudioPool> : fmt::formatter<std::string_view>
168{
169 template <typename FormatContext>
170 auto format (const zrythm::dsp::AudioPool &pool, FormatContext &ctx) const
171 {
172 std::stringstream ss;
173 ss << "\nAudio Pool:\n";
174 for (const auto &clip : pool.get_clip_ptrs ())
175 {
176 auto pool_path = pool.get_clip_path (clip->get_uuid (), false);
177 ss << fmt::format (
178 "[Clip {}] {}: {}\n", clip->get_uuid (), clip->get_name (), pool_path);
179 }
180
181 return fmt::formatter<std::string_view>::format (
182 fmt::format ("{}", ss.str ()), ctx);
183 }
184};
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...