@geastack/host 0.1.14 → 0.1.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/host/wifi.cpp CHANGED
@@ -24,61 +24,59 @@ void sleepForMs(int ms) {
24
24
  #endif
25
25
  }
26
26
 
27
- } // namespace
27
+ } // namespace
28
28
 
29
- bool WifiBackend::enabled() {
30
- return wifi().enabled();
31
- }
29
+ bool WifiBackend::enabled() { return wifi().enabled(); }
32
30
 
33
- void WifiBackend::setEnabled(bool enabled) {
34
- wifi().setEnabled(enabled);
35
- }
31
+ void WifiBackend::setEnabled(bool enabled) { wifi().setEnabled(enabled); }
36
32
 
37
- bool WifiBackend::connected() {
38
- return wifi().connected();
39
- }
33
+ bool WifiBackend::connected() { return wifi().connected(); }
40
34
 
41
- double WifiBackend::rssi() {
42
- return static_cast<double>(wifi().rssi());
43
- }
35
+ double WifiBackend::rssi() { return static_cast<double>(wifi().rssi()); }
44
36
 
45
- std::string WifiBackend::ssid() {
46
- return wifi().ssid();
47
- }
37
+ std::string WifiBackend::ssid() { return wifi().ssid(); }
48
38
 
49
- std::string WifiBackend::ip() {
50
- return wifi().ip();
51
- }
39
+ std::string WifiBackend::ip() { return wifi().ip(); }
52
40
 
53
- std::string WifiBackend::mac() {
54
- return wifi().mac();
55
- }
41
+ std::string WifiBackend::mac() { return wifi().mac(); }
56
42
 
57
- void WifiBackend::configure(const std::string &ssid, const std::string &password) {
43
+ void WifiBackend::configure(const std::string &ssid,
44
+ const std::string &password) {
58
45
  wifi().configure(ssid, password);
59
46
  }
60
47
 
48
+ bool WifiBackend::startAccessPoint(const std::string &ssid,
49
+ const std::string &password) {
50
+ return wifi().startAccessPoint(ssid, password);
51
+ }
52
+
53
+ void WifiBackend::stopAccessPoint() { wifi().stopAccessPoint(); }
54
+
55
+ std::string WifiBackend::accessPointMac() { return wifi().accessPointMac(); }
56
+
57
+ bool WifiBackend::startCaptivePortal() { return wifi().startCaptivePortal(); }
58
+
59
+ void WifiBackend::stopCaptivePortal() { wifi().stopCaptivePortal(); }
60
+
61
61
  bool WifiBackend::waitForConnection(double timeoutMs) {
62
- if (wifi().connected()) return true;
62
+ if (wifi().connected())
63
+ return true;
63
64
  wifi().setEnabled(true);
64
65
 
65
66
  const int timeout = timeoutMs > 0 ? static_cast<int>(timeoutMs) : 0;
66
67
  int elapsed = 0;
67
68
  while (elapsed < timeout) {
68
- if (wifi().connected()) return true;
69
+ if (wifi().connected())
70
+ return true;
69
71
  sleepForMs(100);
70
72
  elapsed += 100;
71
73
  }
72
74
  return wifi().connected();
73
75
  }
74
76
 
75
- void WifiBackend::startScan() {
76
- wifi().scan();
77
- }
77
+ void WifiBackend::startScan() { wifi().scan(); }
78
78
 
79
- bool WifiBackend::scanning() {
80
- return wifi().scanning();
81
- }
79
+ bool WifiBackend::scanning() { return wifi().scanning(); }
82
80
 
83
81
  double WifiBackend::scanCount() {
84
82
  return static_cast<double>(wifi().scanCount());
@@ -96,4 +94,4 @@ bool WifiBackend::scanSecuredAt(double index) {
96
94
  return wifi().networkAt(static_cast<int>(index)).secured;
97
95
  }
98
96
 
99
- } // namespace gea::framework::network
97
+ } // namespace gea::framework::network
@@ -12,7 +12,7 @@ gea::Promise<void> workletPromise(Action action) {
12
12
  }
13
13
  inline gea::host::AudioContext createAudioContext() {
14
14
  hostworker::installRealmRuntime();
15
- return gea::host::AudioContext(16000.0);
15
+ return gea::host::AudioContext(double(gea::platform::audio::deviceSampleRate));
16
16
  }
17
17
  template <typename Options>
18
18
  gea::host::AudioContext createAudioContext(const Options& options) {
@@ -59,8 +59,9 @@ gea::host::AudioWorkletNodeOptions workletOptions(const Options& options) {
59
59
  if constexpr (requires { options.outputChannelCount; }) {
60
60
  hostworker::present(options.outputChannelCount, [&](const auto& counts) {
61
61
  const auto& values = [&]() -> const auto& { if constexpr (requires { counts->size(); }) return *counts; else return counts; }();
62
- if (values.size() != result.numberOfOutputs || values.size() != 1 || values.at(0) != 1)
63
- throw std::invalid_argument("Embedded AudioWorklet requires one mono output");
62
+ if (values.size() != result.numberOfOutputs || values.size() > 1 ||
63
+ (values.size() == 1 && values.at(0) != 1))
64
+ throw std::invalid_argument("Embedded AudioWorklet output channel counts must match zero or one mono output");
64
65
  });
65
66
  }
66
67
  }
@@ -0,0 +1,127 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #pragma once
3
+
4
+ #include "gea_runtime.h"
5
+ #include "host/http.h"
6
+
7
+ namespace gea::host::http {
8
+ namespace detail {
9
+
10
+ template <typename T> T &mutableRecord(T &value) { return value; }
11
+
12
+ template <typename T> T &mutableRecord(gea::Ref<T> &value) {
13
+ value = gea::makeRef<T>();
14
+ return *value;
15
+ }
16
+
17
+ template <typename T> const T &record(const T &value) { return value; }
18
+
19
+ template <typename T> const T &record(const gea::Ref<T> &value) {
20
+ return *value;
21
+ }
22
+
23
+ template <typename T> T field(const T &value, const T &) { return value; }
24
+
25
+ template <typename T>
26
+ T field(const gea::Optional<T> &value, const T &fallback) {
27
+ return value.has_value() ? *value : fallback;
28
+ }
29
+
30
+ template <typename T> gea::Ref<T> arrayField(const gea::Ref<T> &value) {
31
+ return value;
32
+ }
33
+
34
+ template <typename T>
35
+ gea::Ref<T> arrayField(const gea::Optional<gea::Ref<T>> &value) {
36
+ return value.has_value() ? *value : gea::Ref<T>{};
37
+ }
38
+
39
+ template <typename T> struct RequestArgument {
40
+ static T make(const HttpRequest &request) {
41
+ T value{};
42
+ value.method = request.method;
43
+ value.path = request.path;
44
+ value.query = request.query;
45
+ value.url = request.query.empty() ? request.path
46
+ : request.path + "?" + request.query;
47
+ if constexpr (requires { value.body; }) {
48
+ value.body =
49
+ gea::makeRef<gea::TypedArray<std::uint8_t>>(request.body.size());
50
+ std::copy(request.body.begin(), request.body.end(), value.body->data());
51
+ }
52
+ if constexpr (requires { value.headers; }) {
53
+ using Array =
54
+ typename std::remove_cvref_t<decltype(value.headers)>::element_type;
55
+ using Header =
56
+ std::remove_cvref_t<decltype(std::declval<Array>().cells[0].value)>;
57
+ value.headers = gea::makeRef<Array>();
58
+ for (const auto &header : request.headers) {
59
+ Header entry{};
60
+ auto &fields = mutableRecord(entry);
61
+ fields.name = header.name;
62
+ fields.value = header.value;
63
+ value.headers->push(entry);
64
+ }
65
+ }
66
+ return value;
67
+ }
68
+ };
69
+
70
+ template <typename T> struct RequestArgument<gea::Ref<T>> {
71
+ static gea::Ref<T> make(const HttpRequest &request) {
72
+ return gea::makeRef<T>(RequestArgument<T>::make(request));
73
+ }
74
+ };
75
+
76
+ template <typename Handler, typename Argument>
77
+ NativeHttpServerHandle registerTypedHandler(Handler handler) {
78
+ return register_handler([handler](const HttpRequest &request) -> HttpReply {
79
+ const auto result =
80
+ handler(RequestArgument<std::remove_cvref_t<Argument>>::make(request));
81
+ const auto &value = record(result);
82
+ HttpReply reply;
83
+ if constexpr (requires { value.status; }) {
84
+ reply.status = static_cast<int>(field(value.status, 200.0));
85
+ }
86
+ if constexpr (requires { value.contentType; }) {
87
+ reply.contentType = field(value.contentType, std::string("text/plain"));
88
+ }
89
+ if constexpr (requires { value.body; }) {
90
+ reply.body = field(value.body, std::string{});
91
+ }
92
+ if constexpr (requires { value.file; }) {
93
+ reply.file = field(value.file, std::string{});
94
+ }
95
+ if constexpr (requires { value.download; }) {
96
+ reply.download = field(value.download, std::string{});
97
+ }
98
+ if constexpr (requires { value.headers; }) {
99
+ const auto headers = arrayField(value.headers);
100
+ if (headers) {
101
+ for (const auto &cell : headers->cells) {
102
+ const auto &header = record(cell.value);
103
+ reply.headers.push_back({header.name, header.value});
104
+ }
105
+ }
106
+ }
107
+ return reply;
108
+ });
109
+ }
110
+
111
+ } // namespace detail
112
+
113
+ template <typename Result, typename Argument>
114
+ NativeHttpServerHandle
115
+ create_typed_server(gea::CallableObject<Result(Argument)> handler) {
116
+ return detail::registerTypedHandler<decltype(handler), Argument>(
117
+ std::move(handler));
118
+ }
119
+
120
+ template <typename Result, typename Argument>
121
+ NativeHttpServerHandle
122
+ create_typed_server(std::function<Result(Argument)> handler) {
123
+ return detail::registerTypedHandler<decltype(handler), Argument>(
124
+ std::move(handler));
125
+ }
126
+
127
+ } // namespace gea::host::http
@@ -13,6 +13,11 @@
13
13
  #include <utility>
14
14
  #include <vector>
15
15
 
16
+ namespace gea {
17
+ template <typename T> struct Ref;
18
+ template <typename T> class TypedArray;
19
+ } // namespace gea
20
+
16
21
  namespace gea::embedded::ui {
17
22
  class NodeHandle;
18
23
  }
@@ -53,14 +58,60 @@ struct AudioContextCurrentTimeProperty {
53
58
  operator double() const;
54
59
  };
55
60
 
61
+ struct AudioBufferStorage;
62
+
56
63
  struct AudioBuffer {
57
64
  std::vector<std::int16_t> samples;
58
65
  int sampleRate = 16000;
59
66
  int channels = 1;
67
+ mutable std::shared_ptr<AudioBufferStorage> storage;
60
68
 
61
- constexpr AudioBuffer() = default;
69
+ AudioBuffer() = default;
62
70
  AudioBuffer(std::vector<std::int16_t> pcm, int rate, int channelCount)
63
- : samples(std::move(pcm)), sampleRate(rate), channels(channelCount) {}
71
+ : samples(std::move(pcm)), sampleRate(rate), channels(channelCount) {
72
+ ensureStorage();
73
+ }
74
+
75
+ double getLength() const;
76
+ double getDuration() const;
77
+ double getNumberOfChannels() const;
78
+ gea::Ref<gea::TypedArray<float>> getChannelData(double channel) const;
79
+ const std::vector<std::int16_t> &pcmSamples() const;
80
+
81
+ template <typename Source>
82
+ void copyToChannel(const Source &source, double channel,
83
+ double offset = 0) const {
84
+ if constexpr (requires {
85
+ source.get();
86
+ *source;
87
+ }) {
88
+ copyToChannel(*source, channel, offset);
89
+ } else {
90
+ copyToChannelValues(source.data(), source.size(), channel, offset);
91
+ }
92
+ }
93
+
94
+ template <typename Destination>
95
+ void copyFromChannel(const Destination &destination, double channel,
96
+ double offset = 0) const {
97
+ if constexpr (requires {
98
+ destination.get();
99
+ *destination;
100
+ }) {
101
+ copyFromChannelValues(destination->data(), destination->size(), channel,
102
+ offset);
103
+ } else {
104
+ copyFromChannelValues(destination.data(), destination.size(), channel,
105
+ offset);
106
+ }
107
+ }
108
+
109
+ private:
110
+ void ensureStorage() const;
111
+ void copyToChannelValues(const float *source, std::size_t count,
112
+ double channel, double offset) const;
113
+ void copyFromChannelValues(float *destination, std::size_t count,
114
+ double channel, double offset) const;
64
115
  };
65
116
 
66
117
  struct AudioParamValueProperty {
@@ -100,12 +151,15 @@ struct OscillatorTypeProperty {
100
151
 
101
152
  struct OscillatorNode {
102
153
  NativeAudioHandle nativeHandle = 0;
154
+ std::shared_ptr<audio_worklet::ContextState> context;
155
+ mutable double clockOffset = 0;
156
+ mutable bool clockAligned = false;
103
157
  mutable AudioParam frequency;
104
158
  mutable OscillatorTypeProperty type;
105
159
 
106
- constexpr OscillatorNode() = default;
107
- explicit constexpr OscillatorNode(NativeAudioHandle oscillator) : nativeHandle(oscillator), frequency(oscillator), type(oscillator) {}
108
- explicit constexpr OscillatorNode(double oscillator) : nativeHandle(static_cast<NativeAudioHandle>(oscillator)), frequency(oscillator), type(oscillator) {}
160
+ OscillatorNode() = default;
161
+ explicit OscillatorNode(NativeAudioHandle oscillator) : nativeHandle(oscillator), frequency(oscillator), type(oscillator) {}
162
+ explicit OscillatorNode(double oscillator) : nativeHandle(static_cast<NativeAudioHandle>(oscillator)), frequency(oscillator), type(oscillator) {}
109
163
 
110
164
  constexpr operator double() const { return static_cast<double>(nativeHandle); }
111
165
 
@@ -117,6 +171,15 @@ struct OscillatorNode {
117
171
  };
118
172
 
119
173
  struct AudioBufferSourceNode {
174
+ private:
175
+ bool present_ = false;
176
+ explicit AudioBufferSourceNode(bool present) : present_(present) {}
177
+ friend struct AudioContext;
178
+
179
+ public:
180
+ AudioBufferSourceNode() = default;
181
+ explicit operator bool() const { return present_; }
182
+ bool operator==(std::nullptr_t) const { return !present_; }
120
183
  mutable AudioBuffer buffer;
121
184
  mutable bool connected = false;
122
185
 
@@ -139,7 +202,7 @@ struct AudioContext {
139
202
  bool operator==(std::nullptr_t) const { return !state_; }
140
203
  template <typename Options>
141
204
  explicit AudioContext(const Options& options) : AudioContext(sampleRateOption(options)) {}
142
- double sampleRate = 16000;
205
+ double sampleRate = gea::platform::audio::deviceSampleRate;
143
206
  AudioDestinationProperty destination;
144
207
  AudioContextCurrentTimeProperty currentTime;
145
208
  AudioWorklet audioWorklet;
@@ -155,19 +218,27 @@ struct AudioContext {
155
218
 
156
219
  OscillatorNode createOscillator() const;
157
220
  AudioBufferSourceNode createBufferSource() const;
221
+ AudioBuffer createBuffer(double channels, double length, double rate) const;
158
222
  AudioBuffer decodeAudioData(const std::vector<std::uint8_t> &bytes) const;
159
223
  private:
160
224
  template <typename Options>
161
225
  static double sampleRateOption(const Options& options) {
162
- if constexpr (requires { options.has_value(); *options; })
163
- return options.has_value() ? sampleRateOption(*options) : 16000;
164
- else if constexpr (requires { options.get(); *options; })
165
- return options.get() ? sampleRateOption(*options) : 16000;
226
+ if constexpr (requires {
227
+ options.has_value();
228
+ *options;
229
+ })
230
+ return options.has_value() ? sampleRateOption(*options)
231
+ : gea::platform::audio::deviceSampleRate;
232
+ else if constexpr (requires {
233
+ options.get();
234
+ *options;
235
+ })
236
+ return options.get() ? sampleRateOption(*options) : gea::platform::audio::deviceSampleRate;
166
237
  else if constexpr (requires { options.sampleRate; })
167
238
  return sampleRateOption(options.sampleRate);
168
239
  else if constexpr (requires { static_cast<double>(options); })
169
240
  return static_cast<double>(options);
170
- else return 16000;
241
+ else return gea::platform::audio::deviceSampleRate;
171
242
  }
172
243
  };
173
244
 
@@ -204,7 +275,7 @@ class HTMLAudioElement {
204
275
  };
205
276
 
206
277
  // Streaming mono PCM transport independent of any speech provider. Wire PCM
207
- // uses sampleRate; capture/playback use the device's 16 kHz audio clock.
278
+ // uses sampleRate; capture/playback use the configured device audio clock.
208
279
  class WebSocket;
209
280
  class PcmAudioStream {
210
281
  public:
@@ -238,7 +309,7 @@ struct AudioFacade {
238
309
  };
239
310
 
240
311
  inline const AudioContext& sharedAudioContext() {
241
- static const AudioContext context(16000.0);
312
+ static const AudioContext context{double(gea::platform::audio::deviceSampleRate)};
242
313
  return context;
243
314
  }
244
315
  inline constexpr AudioFacade Audio{};