Zrythm v2.0.0-DEV
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:
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 auto get_clip_ptrs () const
112 {
113 return std::views::transform (
114 clip_registry_.get_hash_map () | std::views::values,
115 [] (const auto &clip) { return std::get<dsp::FileAudioSource *> (clip); });
116 }
117
118private:
119 friend void init_from (
120 AudioPool &obj,
121 const AudioPool &other,
122 utils::ObjectCloneType clone_type);
123
124 static constexpr auto kLastKnownFileHashesKey = "fileHashes"sv;
125 friend void to_json (nlohmann::json &j, const AudioPool &pool);
126 friend void from_json (const nlohmann::json &j, AudioPool &pool);
127
128private:
129 SampleRateGetter sample_rate_getter_;
130 ProjectPoolPathGetter project_pool_path_getter_;
131
135 FileAudioSourceRegistry &clip_registry_;
136
139 boost::unordered::concurrent_flat_map<FileAudioSource::Uuid, utils::hash::HashT>
140 last_known_file_hashes_;
141};
142} // namespace zrythm::dsp
143
144// Formatter for AudioPool
145template <>
146struct fmt::formatter<zrythm::dsp::AudioPool> : fmt::formatter<std::string_view>
147{
148 template <typename FormatContext>
149 auto format (const zrythm::dsp::AudioPool &pool, FormatContext &ctx) const
150 {
151 std::stringstream ss;
152 ss << "\nAudio Pool:\n";
153 for (const auto &clip : pool.get_clip_ptrs ())
154 {
155 auto pool_path = pool.get_clip_path (clip->get_uuid (), false);
156 ss << fmt::format (
157 "[Clip {}] {}: {}\n", clip->get_uuid (), clip->get_name (), pool_path);
158 }
159
160 return fmt::formatter<std::string_view>::format (
161 fmt::format ("{}", ss.str ()), ctx);
162 }
163};
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...