@geastack/apple 0.2.30 → 0.2.31

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geastack/apple",
3
- "version": "0.2.30",
3
+ "version": "0.2.31",
4
4
  "description": "Generated Apple SDK bindings plus the macOS, iOS and tvOS native build targets for Gea apps.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -116,13 +116,13 @@
116
116
  },
117
117
  "dependencies": {
118
118
  "@geastack/compiler": "^1.0.34",
119
- "@geastack/core": "^0.1.59",
119
+ "@geastack/core": "^0.1.64",
120
120
  "@geastack/elements": "^0.1.7",
121
- "@geastack/engine": "^0.1.28",
121
+ "@geastack/engine": "^0.1.31",
122
122
  "@geastack/geaos": "^0.1.3",
123
123
  "@geastack/geatsc-plugin-apple-native": "^0.1.4",
124
124
  "@geastack/geatsc-plugin-gea": "^0.1.28",
125
- "@geastack/host": "^0.1.23"
125
+ "@geastack/host": "^0.1.25"
126
126
  },
127
127
  "scripts": {
128
128
  "prepublishOnly": "npm run build",
@@ -166,8 +166,23 @@ static NSFont *self_resolveFont(int fontId, int sizePx, int weight, int stretch)
166
166
  const CGFloat nativeWeight = weight <= 400 ? (weight - 400) / 1000.0 : (weight - 400) / 800.0;
167
167
  return [NSFont systemFontOfSize:size weight:nativeWeight];
168
168
  }
169
- NSDictionary *baseAttributes = @{ (__bridge NSString *)kCTFontFamilyNameAttribute: family };
170
- CTFontDescriptorRef descriptor = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)baseAttributes);
169
+ FontBytes alias;
170
+ {
171
+ TtfByteRegistry &registry = ttfByteRegistry();
172
+ std::scoped_lock guard(registry.lock);
173
+ auto match = registry.byAlias.find(lower.UTF8String);
174
+ if (match != registry.byAlias.end()) alias = match->second;
175
+ }
176
+ CTFontDescriptorRef descriptor = nullptr;
177
+ if (alias) {
178
+ NSData *data = [NSData dataWithBytes:alias->data() length:alias->size()];
179
+ descriptor = CTFontManagerCreateFontDescriptorFromData((__bridge CFDataRef)data);
180
+ }
181
+ if (!descriptor) {
182
+ NSDictionary *baseAttributes = @{ (__bridge NSString *)kCTFontFamilyNameAttribute: family };
183
+ descriptor = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)baseAttributes);
184
+ alias.reset();
185
+ }
171
186
  CTFontRef base = CTFontCreateWithFontDescriptor(descriptor, size, nullptr);
172
187
  CFRelease(descriptor);
173
188
  if (!base) return [NSFont systemFontOfSize:size];
@@ -185,6 +200,9 @@ static NSFont *self_resolveFont(int fontId, int sizePx, int weight, int stretch)
185
200
  }
186
201
  CFRelease(axes);
187
202
  }
203
+ // A static @font-face names an exact face. A weight lookup by family
204
+ // could substitute a different file from the same installed family.
205
+ if (alias && variation.count == 0) return (__bridge_transfer NSFont *)base;
188
206
  NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
189
207
  if (variation.count) attributes[(__bridge NSString *)kCTFontVariationAttribute] = variation;
190
208
  else attributes[(__bridge NSString *)kCTFontTraitsAttribute] = @{
@@ -355,6 +355,20 @@ static bool geaOwnsContourPress(int x, int y) {
355
355
  }
356
356
  @end
357
357
 
358
+ static void installStorageProviders()
359
+ {
360
+ gea::platform::storage::setMountProvider(+[]() { return true; });
361
+ gea::platform::storage::setReadPathProvider(+[](const std::string &path) -> std::string {
362
+ if (!path.starts_with("/sdcard/")) return path;
363
+ NSString *relative = [NSString stringWithUTF8String:path.c_str() + 1];
364
+ if (relative == nil || [relative.pathComponents containsObject:@".."]) return path;
365
+ NSString *resource = [NSBundle.mainBundle.resourcePath stringByAppendingPathComponent:relative];
366
+ if (resource != nil && [NSFileManager.defaultManager isReadableFileAtPath:resource])
367
+ return resource.fileSystemRepresentation;
368
+ return path;
369
+ });
370
+ }
371
+
358
372
  @interface AppDelegate : NSObject <NSApplicationDelegate, NSWindowDelegate>
359
373
  @property(nonatomic, strong) NSWindow *window;
360
374
  @property(nonatomic, strong) GeaContentView *rootView;
@@ -446,7 +460,7 @@ static __weak AppDelegate *gGeaAppDelegate = nil;
446
460
  // macOS has an available host filesystem before app initialization.
447
461
  // ImageService gates external reads on this provider just as device targets
448
462
  // gate theirs on mounted storage.
449
- gea::platform::storage::setMountProvider(+[]() { return true; });
463
+ installStorageProviders();
450
464
  const bool thermalrightTarget = gea::macos::thermalright::enabled();
451
465
  if (!thermalrightTarget) [self installMainMenu];
452
466
 
@@ -1257,7 +1271,7 @@ static void preparePrerenderHost()
1257
1271
  NSString *fontsDir = [NSBundle.mainBundle.resourcePath stringByAppendingPathComponent:@"Fonts"];
1258
1272
  gea::macos::registerCustomTtfDirectory(fontsDir);
1259
1273
  gea::macos::useSoftwareTextMeasurement();
1260
- gea::platform::storage::setMountProvider(+[]() { return true; });
1274
+ installStorageProviders();
1261
1275
  }
1262
1276
 
1263
1277
  static bool loadPrerenderPage(int width, int height, double dpr)
@@ -11,7 +11,8 @@
11
11
  // │ is rendered here, sample-accurately scheduled and
12
12
  // │ mixed against every other live note.
13
13
  // ├─ AVAudioPlayerNode ── AudioSystem::playFile
14
- // └─ AVAudioPlayerNode ── AudioSystem::playPcm
14
+ // ├─ AVAudioPlayerNode ── AudioSystem::playPcm
15
+ // └─ AVAudioPlayerNode ── continuous PCM streams (including AudioWorklets)
15
16
  // └─ mainMixerNode ─ outputNode
16
17
  //
17
18
  // Timebase
@@ -63,18 +64,22 @@
63
64
  #include <TargetConditionals.h>
64
65
 
65
66
  #include "audio.h"
67
+ #include "audio_stream.h"
66
68
 
67
69
  #include "apple_audio_testing.h"
68
70
 
69
71
  #include <algorithm>
72
+ #include <array>
70
73
  #include <atomic>
71
74
  #include <chrono>
72
75
  #include <cmath>
76
+ #include <condition_variable>
73
77
  #include <cstdint>
74
78
  #include <cstdio>
75
79
  #include <cstdlib>
76
80
  #include <cstring>
77
81
  #include <limits>
82
+ #include <memory>
78
83
  #include <mutex>
79
84
  #include <string>
80
85
  #include <thread>
@@ -422,6 +427,7 @@ public:
422
427
  @try {
423
428
  if (filePlayer_ != nil) filePlayer_.volume = linear;
424
429
  if (pcmPlayer_ != nil) pcmPlayer_.volume = linear;
430
+ if (streamPlayer_ != nil) streamPlayer_.volume = linear;
425
431
  } @catch (NSException *exception) {
426
432
  (void)exception;
427
433
  }
@@ -494,9 +500,51 @@ public:
494
500
  }
495
501
  }
496
502
 
503
+ std::uint64_t playPcmStream(PcmStreamMixer::Pull pull)
504
+ {
505
+ if (!pull) return 0;
506
+ ensureSetup();
507
+ {
508
+ std::lock_guard<std::mutex> guard(graphMutex_);
509
+ @try {
510
+ if (streamFormat_ == nil) {
511
+ streamFormat_ = [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatFloat32
512
+ sampleRate:deviceSampleRate
513
+ channels:2
514
+ interleaved:NO];
515
+ }
516
+ if (!ensurePlayerLocked(streamPlayer_, streamPlayerFormat_, streamFormat_)) return 0;
517
+ } @catch (NSException *exception) {
518
+ (void)exception;
519
+ return 0;
520
+ }
521
+ }
522
+ const auto stream = [&] {
523
+ std::lock_guard<std::mutex> guard(streamWakeMutex_);
524
+ return streams_.add(std::move(pull));
525
+ }();
526
+ streamWake_.notify_one();
527
+ std::call_once(streamWorker_, [this] {
528
+ std::thread([this] { feedPcmStreams(); }).detach();
529
+ });
530
+ return stream;
531
+ }
532
+
533
+ void stopPcmStream(std::uint64_t stream) { streams_.remove(stream); }
534
+ bool pcmStreamSettled(std::uint64_t stream) const { return streams_.settled(stream); }
535
+
536
+ void flushPlayback()
537
+ {
538
+ std::lock_guard<std::mutex> guard(graphMutex_);
539
+ flushStreamBuffersLocked();
540
+ }
541
+
497
542
  void stopPlayback()
498
543
  {
544
+ std::lock_guard<std::mutex> streamsGuard(streamWakeMutex_);
545
+ streams_.clear();
499
546
  std::lock_guard<std::mutex> guard(graphMutex_);
547
+ flushStreamBuffersLocked();
500
548
  @try {
501
549
  if (filePlayer_ != nil) [filePlayer_ stop];
502
550
  if (pcmPlayer_ != nil) [pcmPlayer_ stop];
@@ -609,6 +657,83 @@ public:
609
657
  private:
610
658
  Engine() = default;
611
659
 
660
+ // Pull callbacks may take locks. Feed the player on a worker and leave
661
+ // the CoreAudio render callback free of application code.
662
+ void feedPcmStreams()
663
+ {
664
+ while (true) {
665
+ {
666
+ std::unique_lock<std::mutex> lock(streamWakeMutex_);
667
+ streamWake_.wait(lock, [this] { return streams_.active(); });
668
+ }
669
+ @autoreleasepool {
670
+ queuePcmStreamBuffer();
671
+ }
672
+ std::this_thread::sleep_for(std::chrono::milliseconds(4));
673
+ }
674
+ }
675
+
676
+ void flushStreamBuffersLocked()
677
+ {
678
+ // Completion callbacks can arrive after stop. Retire their counter
679
+ // along with the queued buffers so it cannot affect the next queue.
680
+ queuedStreamBuffers_ = std::make_shared<std::atomic<int>>(0);
681
+ @try {
682
+ if (streamPlayer_ != nil) [streamPlayer_ stop];
683
+ } @catch (NSException *exception) {
684
+ (void)exception;
685
+ }
686
+ }
687
+
688
+ void queuePcmStreamBuffer()
689
+ {
690
+ std::shared_ptr<std::atomic<int>> queued;
691
+ {
692
+ std::lock_guard<std::mutex> guard(graphMutex_);
693
+ queued = queuedStreamBuffers_;
694
+ if (queued->load(std::memory_order_relaxed) >= 2) return;
695
+ }
696
+ std::array<std::int16_t, PcmStreamMixer::chunkFrames * 2> samples{};
697
+ const auto frames = streams_.mix(samples.data(), PcmStreamMixer::chunkFrames);
698
+ if (frames == 0) return;
699
+
700
+ std::lock_guard<std::mutex> guard(graphMutex_);
701
+ if (queued != queuedStreamBuffers_) {
702
+ streams_.didWrite(true);
703
+ return;
704
+ }
705
+ bool submitted = false;
706
+ @try {
707
+ if (ensurePlayerLocked(streamPlayer_, streamPlayerFormat_, streamFormat_)) {
708
+ const auto count = static_cast<AVAudioFrameCount>(frames);
709
+ AVAudioPCMBuffer *buffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:streamFormat_
710
+ frameCapacity:count];
711
+ if (buffer != nil) {
712
+ buffer.frameLength = count;
713
+ float *const *channels = buffer.floatChannelData;
714
+ for (std::size_t frame = 0; frame < frames; ++frame) {
715
+ channels[0][frame] = static_cast<float>(samples[2 * frame]) / 32768.0f;
716
+ channels[1][frame] = static_cast<float>(samples[2 * frame + 1]) / 32768.0f;
717
+ }
718
+ queued->fetch_add(1, std::memory_order_relaxed);
719
+ @try {
720
+ [streamPlayer_ scheduleBuffer:buffer completionCallbackType:AVAudioPlayerNodeCompletionDataRendered
721
+ completionHandler:^(AVAudioPlayerNodeCompletionCallbackType) {
722
+ queued->fetch_sub(1, std::memory_order_relaxed);
723
+ }];
724
+ submitted = true;
725
+ } @finally {
726
+ if (!submitted) queued->fetch_sub(1, std::memory_order_relaxed);
727
+ }
728
+ if (!streamPlayer_.isPlaying) [streamPlayer_ play];
729
+ }
730
+ }
731
+ } @catch (NSException *exception) {
732
+ (void)exception;
733
+ }
734
+ streams_.didWrite(submitted);
735
+ }
736
+
612
737
  // ---- records ------------------------------------------------------------
613
738
  OscillatorRecord *find(std::uint64_t handle)
614
739
  {
@@ -1084,6 +1209,14 @@ private:
1084
1209
  AVAudioFormat *filePlayerFormat_ = nil;
1085
1210
  AVAudioPlayerNode *pcmPlayer_ = nil;
1086
1211
  AVAudioFormat *pcmPlayerFormat_ = nil;
1212
+ PcmStreamMixer streams_;
1213
+ std::mutex streamWakeMutex_;
1214
+ std::condition_variable streamWake_;
1215
+ std::once_flag streamWorker_;
1216
+ std::shared_ptr<std::atomic<int>> queuedStreamBuffers_ = std::make_shared<std::atomic<int>>(0);
1217
+ AVAudioPlayerNode *streamPlayer_ = nil;
1218
+ AVAudioFormat *streamPlayerFormat_ = nil;
1219
+ AVAudioFormat *streamFormat_ = nil;
1087
1220
  id configurationObserver_ = nil;
1088
1221
  id interruptionObserver_ = nil;
1089
1222
  };
@@ -1175,9 +1308,15 @@ bool AudioSystem::playPcm(const std::int16_t *samples, std::size_t sample_count,
1175
1308
  }
1176
1309
 
1177
1310
  void AudioSystem::stopPlayback() { Engine::instance().stopPlayback(); }
1178
- // No PCM stream output is queued by this backend (only the ESP32 runtime
1179
- // streams PCM), so a flush has nothing to discard.
1180
- void AudioSystem::flushPlayback() {}
1311
+ void AudioSystem::flushPlayback() { Engine::instance().flushPlayback(); }
1312
+
1313
+ std::uint64_t AudioSystem::playPcmStream(PcmStreamMixer::Pull pull)
1314
+ {
1315
+ return Engine::instance().playPcmStream(std::move(pull));
1316
+ }
1317
+
1318
+ void AudioSystem::stopPcmStream(std::uint64_t stream) { Engine::instance().stopPcmStream(stream); }
1319
+ bool AudioSystem::pcmStreamSettled(std::uint64_t stream) { return Engine::instance().pcmStreamSettled(stream); }
1181
1320
 
1182
1321
  namespace testing {
1183
1322