ACMX 2.136.0
Dual-Backend Real-Time GPU Video Synthesis
Loading...
Searching...
No Matches
ACMX2/audio.hpp
Go to the documentation of this file.
1#ifndef ACMX2_AUDIO_HPP
2#define ACMX2_AUDIO_HPP
3
4#include <cstddef>
5#include <memory>
6#include <string>
7#include <vector>
8
9namespace acmx2::audio {
10
11 inline constexpr std::size_t FFT_SIZE = 512;
12
13 struct AudioMetrics {
14 float amplitude = 0.0f;
15 float frequency = 0.0f;
16 float peak = 0.0f;
17 float rms = 0.0f;
18 float smooth = 0.0f;
19 float low = 0.0f;
20 float mid = 0.0f;
21 float high = 0.0f;
22 };
23
24 /**
25 * Owns the audio-reactive analysis state shared by live and file audio.
26 *
27 * process_samples() is safe to call from the audio callback while the render
28 * thread reads metrics and computes the spectrum.
29 */
31 public:
34
35 AudioAnalyzer(const AudioAnalyzer &) = delete;
37
38 void process_samples(const float *samples, unsigned int frame_count, unsigned int channels);
39 void reset();
40
41 void set_sample_rate(unsigned int sample_rate);
42 unsigned int sample_rate() const;
43 void set_sensitivity(float sensitivity);
44 float sensitivity() const;
45
46 AudioMetrics metrics() const;
47
48 void compute_spectrum();
49 const std::vector<float> &spectrum() const;
50 static constexpr int spectrum_bin_count() { return static_cast<int>(FFT_SIZE / 2); }
51
52 private:
53 class Impl;
54 std::unique_ptr<Impl> impl;
55 };
56
57 /**
58 * Asynchronously records interleaved float PCM samples to a 16-bit WAV file.
59 */
61 public:
64
65 AudioRecorder(const AudioRecorder &) = delete;
67
68 bool start(const std::string &filepath, unsigned int sample_rate, unsigned int channels);
69 void stop();
70 bool is_recording() const;
71
72 void capture(const float *samples, unsigned int frame_count, unsigned int channels);
73
74 void set_gain(float gain);
75 float gain() const;
76 double duration_seconds() const;
77
78 private:
79 class Impl;
80 std::unique_ptr<Impl> impl;
81 };
82
84 unsigned int channels = 2;
85 float sensitivity = 1.0f;
86 int input_device = -1;
87 int output_device = -1;
88 bool pass_through = false;
89 };
90
91 /**
92 * RAII owner for the RtAudio stream, analysis pipeline, and WAV recorder.
93 */
95 public:
98
99 AudioEngine(const AudioEngine &) = delete;
101
102 bool open(const AudioStreamConfig &config);
103 void close();
104 bool is_open() const;
105 unsigned int input_channels() const;
106
108 const AudioAnalyzer &analyzer() const;
110 const AudioRecorder &recorder() const;
111
112 static void list_devices();
113
114 private:
115 class Impl;
116 std::unique_ptr<Impl> impl;
117 };
118
119} // namespace acmx2::audio
120
121#endif
Owns the audio-reactive analysis state shared by live and file audio.
const std::vector< float > & spectrum() const
AudioAnalyzer(const AudioAnalyzer &)=delete
void set_sensitivity(float sensitivity)
unsigned int sample_rate() const
void process_samples(const float *samples, unsigned int frame_count, unsigned int channels)
AudioMetrics metrics() const
AudioAnalyzer & operator=(const AudioAnalyzer &)=delete
static constexpr int spectrum_bin_count()
std::unique_ptr< Impl > impl
void set_sample_rate(unsigned int sample_rate)
AudioRecorder & recorder()
AudioAnalyzer & analyzer()
AudioEngine(const AudioEngine &)=delete
unsigned int input_channels() const
AudioEngine & operator=(const AudioEngine &)=delete
bool open(const AudioStreamConfig &config)
std::unique_ptr< Impl > impl
Asynchronously records interleaved float PCM samples to a 16-bit WAV file.
AudioRecorder & operator=(const AudioRecorder &)=delete
AudioRecorder(const AudioRecorder &)=delete
void capture(const float *samples, unsigned int frame_count, unsigned int channels)
std::unique_ptr< Impl > impl
bool start(const std::string &filepath, unsigned int sample_rate, unsigned int channels)
constexpr std::size_t FFT_SIZE