Blender V5.3
BKE_sound_sample.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include <memory>
12#include <optional>
13
14#include "BLI_array.hh"
15#include "BLI_cache_mutex.hh"
16#include "BLI_hash.hh"
17
18#include "BKE_sound.hh"
19
20namespace blender::bke {
21
22#if defined(WITH_AUDASPACE)
23class SoundReaderCache;
24#endif
25
34 public:
40 enum class WindowFunction {
41 Hann,
42 Hamming,
43 Blackman,
44 Rectangular,
45 };
46
54 CatmullRom,
56 BSpline,
57 };
58
59 struct Key {
64 std::optional<int> channel;
65
66 uint64_t hash() const
67 {
68 return get_default_hash(this->window_function, this->fft_size, this->channel.value_or(-1));
69 }
70
71 friend bool operator==(const Key &a, const Key &b) = default;
72 };
73
79
80 private:
82 struct WindowCache {
83 mutable CacheMutex mutex;
88 mutable std::optional<Array<float, 0>> cumulative_amplitudes;
89 };
90
91 Key key_;
92#if defined(WITH_AUDASPACE)
93 std::shared_ptr<SoundReaderCache> reader_cache_;
94#endif
96 int samples_per_second_;
103 int window_cache_stride_;
104 Array<WindowCache> window_caches_;
105
106#if defined(WITH_AUDASPACE)
108 const WindowWeights &window_weights_;
109#endif
110
111 public:
113 static std::shared_ptr<const bSoundFrequencySampler> get_cached(const bSound &sound,
114 const Key &key);
115
117 float sample(float time,
118 float low,
119 float high,
120 InterpolationMethod time_interpolation,
121 InterpolationMethod frequency_interpolation) const;
122
123 private:
124#if defined(WITH_AUDASPACE)
127 std::shared_ptr<SoundReaderCache> reader_cache,
128 const Key &key);
129#endif
130 float sample_frequency_range_in_window(int window_i,
131 float low,
132 float high,
133 InterpolationMethod method) const;
134 float sample_cumulative_frequency(Span<float> window_values,
135 float frequency,
136 InterpolationMethod method) const;
137 std::optional<Span<float>> ensure_window_cache(int window_i) const;
138 std::optional<Array<float>> compute_fft(int start_sample) const;
139};
140
141} // namespace blender::bke
std::shared_ptr< void > AUD_Sound
static std::shared_ptr< const bSoundFrequencySampler > get_cached(const bSound &sound, const Key &key)
#define frequency(freq)
unsigned __int64 uint64_t
Definition BKE_text.h:91
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
friend bool operator==(const Key &a, const Key &b)=default