]> git.sesse.net Git - nageru/blob - nageru/audio_clip.h
Make the delay analyzer understand that two sources can have different starting times.
[nageru] / nageru / audio_clip.h
1 #ifndef AUDIO_CLIP_H
2 #define AUDIO_CLIP_H
3
4 // A short single-channel recording of an audio clip, for the delay analyzer.
5 // Thread safe.
6
7 #include <chrono>
8 #include <memory>
9 #include <mutex>
10 #include <utility>
11 #include <vector>
12
13 class AudioClip
14 {
15 public:
16         void clear();
17         void add_audio(const float *samples, size_t num_samples, double sample_rate, std::chrono::steady_clock::time_point frame_time);
18         double get_length_seconds() const;  // 0.0 if empty().
19         double get_length_seconds_after_base(std::chrono::steady_clock::time_point base) const;
20         bool empty() const;
21
22         // Only valid if not empty.
23         std::chrono::steady_clock::time_point get_first_sample() const;
24
25         std::unique_ptr<std::pair<float, float>[]> get_min_max_peaks(unsigned width, std::chrono::steady_clock::time_point base) const;
26
27 private:
28         mutable std::mutex mu;
29         std::vector<float> vals;  // Under <mutex>.
30         double sample_rate;  // Under <mutex>.
31         std::chrono::steady_clock::time_point first_sample;  // Under <mutex>.
32 };
33
34 #endif