-
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathBaseAudioContext.h
More file actions
138 lines (122 loc) · 5.04 KB
/
BaseAudioContext.h
File metadata and controls
138 lines (122 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#pragma once
#include <audioapi/core/types/ContextState.h>
#include <audioapi/core/types/OscillatorType.h>
#include <audioapi/core/utils/worklets/SafeIncludes.h>
#include <atomic>
#include <cassert>
#include <complex>
#include <cstddef>
#include <functional>
#include <memory>
#include <string>
#include <utility>
#include <vector>
namespace audioapi {
class AudioBuffer;
class GainNode;
class DelayNode;
class AudioBuffer;
class PeriodicWave;
class OscillatorNode;
class ConstantSourceNode;
class StereoPannerNode;
class AudioGraphManager;
class BiquadFilterNode;
class IIRFilterNode;
class AudioDestinationNode;
class AudioBufferSourceNode;
class AudioBufferQueueSourceNode;
class AnalyserNode;
class AudioEventHandlerRegistry;
class ConvolverNode;
class IAudioEventHandlerRegistry;
class RecorderAdapterNode;
class WaveShaperNode;
class WorkletSourceNode;
class WorkletNode;
class WorkletProcessingNode;
class StreamerNode;
struct GainOptions;
struct StereoPannerOptions;
struct ConvolverOptions;
struct ConstantSourceOptions;
struct AnalyserOptions;
struct BiquadFilterOptions;
struct OscillatorOptions;
struct BaseAudioBufferSourceOptions;
struct AudioBufferSourceOptions;
struct StreamerOptions;
struct DelayOptions;
struct IIRFilterOptions;
struct WaveShaperOptions;
class BaseAudioContext : public std::enable_shared_from_this<BaseAudioContext> {
public:
explicit BaseAudioContext(
float sampleRate,
const std::shared_ptr<IAudioEventHandlerRegistry> &audioEventHandlerRegistry,
const RuntimeRegistry &runtimeRegistry);
virtual ~BaseAudioContext() = default;
ContextState getState();
[[nodiscard]] float getSampleRate() const;
[[nodiscard]] double getCurrentTime() const;
[[nodiscard]] std::size_t getCurrentSampleFrame() const;
std::shared_ptr<AudioDestinationNode> getDestination() const;
void setState(ContextState state);
std::shared_ptr<RecorderAdapterNode> createRecorderAdapter();
std::shared_ptr<WorkletSourceNode> createWorkletSourceNode(
std::shared_ptr<worklets::SerializableWorklet> &shareableWorklet,
std::weak_ptr<worklets::WorkletRuntime> runtime,
bool shouldLockRuntime = true);
std::shared_ptr<WorkletNode> createWorkletNode(
std::shared_ptr<worklets::SerializableWorklet> &shareableWorklet,
std::weak_ptr<worklets::WorkletRuntime> runtime,
size_t bufferLength,
size_t inputChannelCount,
bool shouldLockRuntime = true);
std::shared_ptr<WorkletProcessingNode> createWorkletProcessingNode(
std::shared_ptr<worklets::SerializableWorklet> &shareableWorklet,
std::weak_ptr<worklets::WorkletRuntime> runtime,
bool shouldLockRuntime = true);
std::shared_ptr<DelayNode> createDelay(const DelayOptions &options);
std::shared_ptr<IIRFilterNode> createIIRFilter(const IIRFilterOptions &options);
std::shared_ptr<OscillatorNode> createOscillator(const OscillatorOptions &options);
std::shared_ptr<ConstantSourceNode> createConstantSource(const ConstantSourceOptions &options);
std::shared_ptr<StreamerNode> createStreamer(const StreamerOptions &options);
std::shared_ptr<GainNode> createGain(const GainOptions &options);
std::shared_ptr<StereoPannerNode> createStereoPanner(const StereoPannerOptions &options);
std::shared_ptr<BiquadFilterNode> createBiquadFilter(const BiquadFilterOptions &options);
std::shared_ptr<AudioBufferSourceNode> createBufferSource(
const AudioBufferSourceOptions &options);
std::shared_ptr<AudioBufferQueueSourceNode> createBufferQueueSource(
const BaseAudioBufferSourceOptions &options);
std::shared_ptr<PeriodicWave> createPeriodicWave(
const std::vector<std::complex<float>> &complexData,
bool disableNormalization,
int length) const;
std::shared_ptr<AnalyserNode> createAnalyser(const AnalyserOptions &options);
std::shared_ptr<ConvolverNode> createConvolver(const ConvolverOptions &options);
std::shared_ptr<WaveShaperNode> createWaveShaper(const WaveShaperOptions &options);
std::shared_ptr<PeriodicWave> getBasicWaveForm(OscillatorType type);
[[nodiscard]] float getNyquistFrequency() const;
std::shared_ptr<AudioGraphManager> getGraphManager() const;
std::shared_ptr<IAudioEventHandlerRegistry> getAudioEventHandlerRegistry() const;
const RuntimeRegistry &getRuntimeRegistry() const;
virtual void initialize();
void setOnStateChangedCallbackId(uint64_t callbackId);
protected:
std::shared_ptr<AudioDestinationNode> destination_;
private:
std::atomic<uint64_t> onStateChangedCallbackId_ = 0; // 0 means no callback
std::atomic<ContextState> state_;
std::atomic<float> sampleRate_;
std::shared_ptr<AudioGraphManager> graphManager_;
std::shared_ptr<IAudioEventHandlerRegistry> audioEventHandlerRegistry_;
RuntimeRegistry runtimeRegistry_;
std::shared_ptr<PeriodicWave> cachedSineWave_ = nullptr;
std::shared_ptr<PeriodicWave> cachedSquareWave_ = nullptr;
std::shared_ptr<PeriodicWave> cachedSawtoothWave_ = nullptr;
std::shared_ptr<PeriodicWave> cachedTriangleWave_ = nullptr;
[[nodiscard]] virtual bool isDriverRunning() const = 0;
void sendOnStateChangedEvent();
};
} // namespace audioapi