-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabsound-c.cpp
1113 lines (928 loc) · 31.4 KB
/
labsound-c.cpp
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef LABSOUNDC_H
#include "labsound-c.h"
#endif
#include <LabSound/LabSound.h>
#include <LabSound/backends/AudioDevice_RtAudio.h>
#include "flecs.h"
#include <memory>
#include <string>
#include <unordered_map>
using std::shared_ptr;
using std::unique_ptr;
using std::string;
using std::unordered_map;
using namespace lab;
using namespace LabSoundNS;
struct e_Node {
uint32_t hash_index;
};
struct e_Input {
ls_Node node;
int index;
};
struct e_Output {
ls_Node node;
int index;
};
struct e_Param {
ls_Node node;
int index;
};
struct e_Setting {
ls_Node node;
int index;
};
struct e_Connection {
ls_OutputPin from_pin;
ls_InputPin to_pin;
ls_Node from_node, to_node;
int from_output_index, to_index;
bool to_is_param;
};
struct e_BusData {
uint32_t hash_index;
};
ECS_COMPONENT_DECLARE(e_Node);
ECS_COMPONENT_DECLARE(e_Input);
ECS_COMPONENT_DECLARE(e_Output);
ECS_COMPONENT_DECLARE(e_Param);
ECS_COMPONENT_DECLARE(e_Setting);
ECS_COMPONENT_DECLARE(e_Connection);
ECS_COMPONENT_DECLARE(e_BusData);
// Returns input, output
inline std::pair<AudioStreamConfig, AudioStreamConfig>
GetDefaultAudioDeviceConfiguration(const bool with_input = true)
{
const std::vector<AudioDeviceInfo> audioDevices = lab::AudioDevice_RtAudio::MakeAudioDeviceList();
AudioDeviceInfo defaultOutputInfo, defaultInputInfo;
for (auto & info : audioDevices) {
if (info.is_default_output)
defaultOutputInfo = info;
if (info.is_default_input)
defaultInputInfo = info;
}
AudioStreamConfig outputConfig;
if (defaultOutputInfo.index != -1) {
outputConfig.device_index = defaultOutputInfo.index;
outputConfig.desired_channels = std::min(uint32_t(2), defaultOutputInfo.num_output_channels);
outputConfig.desired_samplerate = defaultOutputInfo.nominal_samplerate;
}
AudioStreamConfig inputConfig;
if (with_input) {
if (defaultInputInfo.index != -1) {
inputConfig.device_index = defaultInputInfo.index;
inputConfig.desired_channels = std::min(uint32_t(1), defaultInputInfo.num_input_channels);
inputConfig.desired_samplerate = defaultInputInfo.nominal_samplerate;
}
else {
throw std::invalid_argument("the default audio input device was requested but none were found");
}
}
// RtAudio doesn't support mismatched input and output rates.
// this may be a pecularity of RtAudio, but for now, force an RtAudio
// compatible configuration
if (defaultOutputInfo.nominal_samplerate != defaultInputInfo.nominal_samplerate) {
float min_rate = std::min(defaultOutputInfo.nominal_samplerate, defaultInputInfo.nominal_samplerate);
inputConfig.desired_samplerate = min_rate;
outputConfig.desired_samplerate = min_rate;
printf("Warning ~ input and output sample rates don't match, attempting to set minimum");
}
return {inputConfig, outputConfig};
}
struct ls_APIDetail {
shared_ptr<lab::AudioContext> ac;
shared_ptr<lab::AudioDestinationNode> dn;
shared_ptr<lab::AudioDevice_RtAudio> dv;
ls_Alloc alloc;
ls_Node destination_node;
uint32_t next_id;
unordered_map<uint32_t, shared_ptr<lab::AudioNode>> nodes;
unordered_map<uint32_t, shared_ptr<lab::AudioBus>> busses;
ecs_world_t *ecs;
ecs_entity_t has_input_rel;
ecs_entity_t has_output_rel;
ecs_entity_t has_param_rel;
ecs_entity_t has_setting_rel;
ecs_entity_t has_connection_rel;
ecs_app_desc_t desc;
};
namespace ls_1_0 {
shared_ptr<lab::AudioNode> ls_node(struct ls_API* ls, const e_Node* id)
{
if (!id)
return {};
auto it = ls->_detail->nodes.find(id->hash_index);
if (it == ls->_detail->nodes.end())
return {};
return it->second;
};
shared_ptr<lab::AudioNode> ls_node(struct ls_API* ls, const ls_Node n)
{
if (!ecs_is_alive(ls->_detail->ecs, n.id))
return {};
const e_Node* node = ecs_get(ls->_detail->ecs, n.id, e_Node);
return ls_node(ls, node);
};
const e_Connection* ls_connection(struct ls_API* ls, const ls_Connection c)
{
auto w = ls->_detail->ecs;
if (!ecs_is_alive(w, c.id))
return nullptr;
const e_Connection* ec = ecs_get(w, c.id, e_Connection);
if (!ec)
return nullptr;
auto node_from = ls_node(ls, ec->from_node);
if (!node_from)
return nullptr;
auto node_to = ls_node(ls, ec->to_node);
if (!node_to)
return nullptr;
return ec;
}
// operations on nodes
//
ls_Seconds node_get_timing(struct ls_API* ls,
ls_Node n)
{
auto ln = ls_node(ls, n);
if (!ln)
return ls_Seconds { -1.f };
return ls_Seconds{ ln->graphTime().microseconds.count() * 1.e-6f };
}
ls_Seconds node_get_self_timing(struct ls_API* ls,
ls_Node n)
{
auto ln = ls_node(ls, n);
if (!ln)
return ls_Seconds{ -1.f };
return ls_Seconds{ ln->totalTime().microseconds.count() -
ln->graphTime().microseconds.count() * 1.e-6f };
}
void node_start(struct ls_API* ls,
ls_Node n, ls_Seconds when)
{
auto ln = ls_node(ls, n);
if (!ln)
return;
lab::SampledAudioNode* san =
dynamic_cast<lab::SampledAudioNode*>(ln.get());
if (san) {
san->schedule(when.t);
return;
}
lab::AudioScheduledSourceNode* asn =
dynamic_cast<lab::AudioScheduledSourceNode*>(ln.get());
if (!asn)
return;
asn->start(when.t);
}
void node_diagnose(struct ls_API* ls, ls_Node n)
{
auto ln = ls_node(ls, n);
if (!ln)
return;
lab::AudioContext& ac = *ls->_detail->ac.get();
ac.diagnose(ln);
}
void node_schedule(struct ls_API* ls,
ls_Node n, ls_Seconds when, int count)
{
if (!count)
return;
auto ln = ls_node(ls, n);
if (!ln)
return;
lab::SampledAudioNode* san =
dynamic_cast<lab::SampledAudioNode*>(ln.get());
if (san) {
san->schedule(when.t, count);
return;
}
lab::AudioScheduledSourceNode* asn =
dynamic_cast<lab::AudioScheduledSourceNode*>(ln.get());
if (!asn)
return;
asn->start(when.t); // disregarding count.
}
const char* node_scheduled_state_name(struct ls_API* ls,
ls_Node n)
{
auto ln = ls_node(ls, n);
if (!ln)
return "Nonexistent node";
return schedulingStateName(ln->schedulingState());
}
void node_stop(struct ls_API* ls,
ls_Node n, ls_Seconds when)
{
auto ln = ls_node(ls, n);
if (!ln)
return;
lab::SampledAudioNode* san =
dynamic_cast<lab::SampledAudioNode*>(ln.get());
if (san) {
san->stop(when.t);
return;
}
lab::AudioScheduledSourceNode* asn =
dynamic_cast<lab::AudioScheduledSourceNode*>(ln.get());
if (!asn)
return;
asn->stop(when.t);
}
// getting pins from nodes
//
ls_InputPin node_named_input(
struct ls_API* ls,
ls_Node n, ls_StringSlice str)
{
auto ln = ls_node(ls, n);
if (!ln)
return ls_InputPin_empty;
string name(str.start, str.end - str.start);
string path = string(ln->name()) + "(" + name;
// return the pin if it's pin looked up already
ecs_entity_t e = ecs_lookup(ls->_detail->ecs, path.c_str());
if (e)
return ls_InputPin { e };
int ic = ln->numberOfInputs();
for (int idx = 0; idx < ic; ++idx) {
auto i = ln->input(idx);
if (i->name() == name) {
// create the input pin on the fly
e_Input eIn { n, idx };
ecs_entity_desc_t desc = { 0 };
desc.name = path.c_str();
e = ecs_entity_init(ls->_detail->ecs, &desc);
ecs_add(ls->_detail->ecs, e, e_Input);
ecs_set_id(ls->_detail->ecs, e,
ecs_id(e_Input), sizeof(eIn), &eIn);
// associate the pin with the node
ecs_add_pair(ls->_detail->ecs, n.id, ls->_detail->has_input_rel, e);
ecs_add_pair(ls->_detail->ecs, e, EcsChildOf, n.id); // autodelete
return ls_InputPin { e };
}
}
return ls_InputPin_empty;
}
ls_InputPin node_indexed_input(
struct ls_API* ls,
ls_Node n, int index)
{
auto ln = ls_node(ls, n);
if (!ln)
return ls_InputPin_empty;
if (ln->numberOfInputs() <= index)
return ls_InputPin_empty;
string name = ln->input(index)->name();
string path = string(ln->name()) + "(" + name;
// return the pin if it's pin looked up already
ecs_entity_t e = ecs_lookup(ls->_detail->ecs, path.c_str());
if (e)
return ls_InputPin { e };
// create the input pin on the fly
e_Input eIn { n, index };
ecs_entity_desc_t desc = { 0 };
desc.name = path.c_str();
e = ecs_entity_init(ls->_detail->ecs, &desc);
ecs_add(ls->_detail->ecs, e, e_Input);
ecs_set_id(ls->_detail->ecs, e,
ecs_id(e_Input), sizeof(eIn), &eIn);
// associate the pin with the node
ecs_add_pair(ls->_detail->ecs, n.id, ls->_detail->has_input_rel, e);
ecs_add_pair(ls->_detail->ecs, e, EcsChildOf, n.id); // autodelete
return ls_InputPin { e };
}
ls_OutputPin node_named_output(struct ls_API* ls,
ls_Node n, ls_StringSlice str)
{
auto ln = ls_node(ls, n);
if (!ln)
return ls_OutputPin_empty;
string name(str.start, str.end - str.start);
string path = string(ln->name()) + ")" + name;
// return the pin if it's pin looked up already
ecs_entity_t e = ecs_lookup(ls->_detail->ecs, path.c_str());
if (e)
return ls_OutputPin { e };
// create the output pin on the fly
int ic = ln->numberOfOutputs();
for (int index = 0; index < ic; ++index) {
auto output = ln->output(index);
if (output->name() == name) {
e_Output eOutput { n, index };
ecs_entity_desc_t desc = { 0 };
desc.name = path.c_str();
e = ecs_entity_init(ls->_detail->ecs, &desc);
ecs_add(ls->_detail->ecs, e, e_Output);
ecs_set_id(ls->_detail->ecs, e,
ecs_id(e_Input), sizeof(eOutput), &eOutput);
// associate the pin with the node
ecs_add_pair(ls->_detail->ecs, n.id, ls->_detail->has_output_rel, e);
ecs_add_pair(ls->_detail->ecs, e, EcsChildOf, n.id); // autodelete
return ls_OutputPin { e };
}
++index;
}
return ls_OutputPin_empty;
}
ls_OutputPin node_indexed_output(struct ls_API* ls,
ls_Node n, int index)
{
auto ln = ls_node(ls, n);
if (!ln)
return ls_OutputPin_empty;
if (index >= ln->numberOfOutputs())
return ls_OutputPin_empty;
string name = ln->output(index)->name();
string path = string(ln->name()) + ")" + name;
// return the pin if it's pin looked up already
ecs_entity_t e = ecs_lookup(ls->_detail->ecs, path.c_str());
if (e)
return ls_OutputPin { e };
// create the input pin on the fly
e_Output eOut { n, index };
ecs_entity_desc_t desc = { 0 };
desc.name = path.c_str();
e = ecs_entity_init(ls->_detail->ecs, &desc);
ecs_add(ls->_detail->ecs, e, e_Output);
ecs_set_id(ls->_detail->ecs, e,
ecs_id(e_Output), sizeof(eOut), &eOut);
// associate the pin with the node
ecs_add_pair(ls->_detail->ecs, n.id, ls->_detail->has_output_rel, e);
ecs_add_pair(ls->_detail->ecs, e, EcsChildOf, n.id); // autodelete
return ls_OutputPin { e };
}
ls_InputPin node_parameter(struct ls_API* ls,
ls_Node n, ls_StringSlice name)
{
auto ln = ls_node(ls, n);
if (!ln)
return ls_InputPin_empty;
string nameStr(name.start, name.end - name.start);
string path = string(ln->name()) + "*" + nameStr;
// return the pin if it's pin looked up already
ecs_entity_t e = ecs_lookup(ls->_detail->ecs, path.c_str());
if (e)
return ls_InputPin { e };
int param = ln->param_index(nameStr.c_str());
if (param < 0)
return ls_InputPin_empty;
e_Param eParam { n, param };
ecs_entity_desc_t desc = { 0 };
desc.name = path.c_str();
e = ecs_entity_init(ls->_detail->ecs, &desc);
ecs_add(ls->_detail->ecs, e, e_Param);
ecs_set_id(ls->_detail->ecs, e,
ecs_id(e_Param), sizeof(eParam), &eParam);
// associate the pin with the node
ecs_add_pair(ls->_detail->ecs, n.id, ls->_detail->has_param_rel, e);
ecs_add_pair(ls->_detail->ecs, e, EcsChildOf, n.id); // autodelete
return ls_InputPin { e };
}
ls_InputPin node_setting(struct ls_API* ls,
ls_Node n, ls_StringSlice name)
{
auto ln = ls_node(ls, n);
if (!ln)
return ls_InputPin_empty;
string nameStr(name.start, name.end - name.start);
string path = string(ln->name()) + string("@") + nameStr;
// return the pin if it's pin looked up already
auto w = ls->_detail->ecs;
ecs_entity_t e = ecs_lookup(w, path.c_str());
if (e)
return ls_InputPin { e };
int setting = ln->setting_index(nameStr.c_str());
if (setting < 0)
return ls_InputPin_empty;
e_Setting eSetting { n, setting };
ecs_entity_desc_t desc = { 0 };
desc.name = path.c_str();
e = ecs_entity_init(w, &desc);
ecs_add(ls->_detail->ecs, e, e_Setting);
ecs_set_id(ls->_detail->ecs, e,
ecs_id(e_Setting), sizeof(eSetting), &eSetting);
// associate the pin with the node
ecs_add_pair(w, n.id, ls->_detail->has_setting_rel, e);
ecs_add_pair(w, e, EcsChildOf, n.id); // autodelete
return ls_InputPin { e };
}
void node_set_on_ended(struct ls_API* ls,
ls_Node n, void(*fn)())
{
auto ln = ls_node(ls, n);
if (!ln)
return;
auto san = dynamic_cast<lab::AudioScheduledSourceNode*>(ln.get());
if (!san)
return;
san->setOnEnded([fn]() { fn(); });
}
ls_PinKind pin_kind(struct ls_API* ls,
ls_InputPin p)
{
auto w = ls->_detail->ecs;
if (!ecs_is_alive(w, p.id))
return ls_PinKindInvalid;
const e_Input* ei = ecs_get(w, p.id, e_Input);
if (ei) return ls_PinKindInput;
const e_Output* eo = ecs_get(w, p.id, e_Output);
if (eo) return ls_PinKindOutput;
const e_Param* ep = ecs_get(w, p.id, e_Param);
if (eo) return ls_PinKindParam;
const e_Setting* es = ecs_get(w, p.id, e_Setting);
if (es) return ls_PinKindSetting;
return ls_PinKindInvalid;
}
ls_PinDataType pin_data_type(struct ls_API* ls,
ls_InputPin p)
{
auto w = ls->_detail->ecs;
if (!ecs_is_alive(w, p.id))
return ls_Invalid;
const e_Input* ei = ecs_get(w, p.id, e_Input);
if (ei) return ls_Bus;
const e_Output* eo = ecs_get(w, p.id, e_Output);
if (eo) return ls_Bus;
const e_Param* ep = ecs_get(w, p.id, e_Param);
if (eo) return ls_Float;
const e_Setting* es = ecs_get(w, p.id, e_Setting);
if (es) {
auto ln = ls_node(ls, es->node);
if (!ln)
return ls_Invalid;
switch (ln->settings()[es->index]->type()) {
case lab::SettingType::None:
return ls_Invalid;
case lab::SettingType::Bool:
return ls_Bool;
case lab::SettingType::Integer:
return ls_Int;
case lab::SettingType::Float:
return ls_Float;
case lab::SettingType::Enum:
return ls_Enum;
case lab::SettingType::Bus:
return ls_Bus;
}
}
return ls_Invalid;
}
// managing nodes
//
const ls_NameArray* node_names(struct ls_API* ls)
{
static auto src_names = lab::NodeRegistry::Instance().Names();
static ls_NameArray names;
static bool init = true;
if (init) {
names.names = (ls_StringSlice*) ls->_detail->alloc.malloc(
sizeof(ls_StringSlice) * src_names.size());
names.count = (int) src_names.size();
for (int i = 0; i < names.count; ++i) {
names.names[i].start = src_names[i].c_str();
names.names[i].end = names.names[i].start + src_names[i].size();
}
}
return &names;
}
ls_Node node_create(struct ls_API* ls,
ls_StringSlice name, ls_StringSlice type)
{
string n(type.start, type.end - type.start);
lab::AudioContext& ac = *ls->_detail->ac.get();
lab::AudioNode* node = lab::NodeRegistry::Instance().Create(n, ac);
if (!node)
return ls_Node_empty;
string path(name.start, name.end - name.start);
uint32_t id = ls->_detail->next_id++;
ls->_detail->nodes.insert({ id , shared_ptr<lab::AudioNode>(node) });
e_Node en = { id };
ecs_entity_desc_t desc = { 0 };
desc.name = path.c_str();
auto e = ecs_entity_init(ls->_detail->ecs, &desc);
ecs_add(ls->_detail->ecs, e, e_Node);
ecs_set_id(ls->_detail->ecs, e,
ecs_id(e_Node), sizeof(en), &en);
return ls_Node { e };
}
void node_delete(struct ls_API* ls,
ls_Node n)
{
auto w = ls->_detail->ecs;
if (!ecs_is_alive(w, n.id))
return;
auto ln = ls_node(ls, n);
if (!ln)
return;
ls->_detail->ac->disconnect(ln);
ecs_query_desc_t desc = {0};
desc.filter.terms[0].id = ecs_pair(ls->_detail->has_connection_rel, n.id);
ecs_query_t *q = ecs_query_init(w, &desc);
ecs_iter_t it = ecs_query_iter(w, q);
while (ecs_query_next(&it)) {
/* e_Node* eTo = ecs_term(&it, e_Node, 1);
if (!eTo)
continue;
ls_Connection* c = ecs_term(&it, ls_Connection, 2);
if (!c)
continue;
const e_Connection* ec = ecs_get(w, c->id, e_Connection);
if (!ec)
continue;
const e_Param* ep = ecs_get(w, ec->to.id, e_Param);
if (ep) {
const e_Node* paramNode = ecs_get(w, ep->node.id, e_Node);
if (!paramNode)
continue;
ls->_detail->ac->disconnectParam(paramNode->node->param(ep->index), eTo->node, ep->index);
}
else {
//ls->_detail->disconnectNode(c->node, eTo->node);
}*/
}
const e_Node* en = ecs_get(w, n.id, e_Node);
auto hash_it = ls->_detail->nodes.find(en->hash_index);
if (hash_it != ls->_detail->nodes.end())
ls->_detail->nodes.erase(hash_it);
ecs_remove_all(w, n.id); // remove components
ecs_delete(w, n.id); // and delete the node
}
void create_node_output(
struct ls_API* ls,
ls_Node n, ls_StringSlice name, int channels)
{
auto w = ls->_detail->ecs;
auto ln = ls_node(ls, n);
if (!ln)
return;
string name_str(name.start, name.end - name.start);
auto existing_output = ln->output(name_str.c_str());
if (!existing_output) {
auto new_output = std::unique_ptr<AudioNodeOutput>(new AudioNodeOutput(ln.get(), name_str.c_str(), channels));
lab::ContextGraphLock r(ls->_detail->ac.get(), "create_node_output");
ln->addOutput(r, std::move(new_output));
}
else {
lab::ContextRenderLock r(ls->_detail->ac.get(), "create_node_output");
if (existing_output->bus(r)->numberOfChannels() != channels) {
existing_output->bus(r)->setNumberOfChannels(r, channels);
}
}
}
// setting and getting pin values
//
void set_float(struct ls_API* ls,
ls_InputPin p, float val)
{
auto w = ls->_detail->ecs;
const e_Param* ep = ecs_get(w, p.id, e_Param);
if (ep) {
auto ln = ls_node(ls, ep->node);
if (ln && ln->param(ep->index))
ln->param(ep->index)->setValue(val);
return;
}
const e_Setting* es = ecs_get(w, p.id, e_Setting);
if (es) {
auto ln = ls_node(ls, ep->node);
if (ln && ln->setting(es->index))
ln->setting(es->index)->setFloat(val);
}
}
void set_int(struct ls_API* ls,
ls_InputPin p, uint32_t i)
{
auto w = ls->_detail->ecs;
if (!ecs_is_alive(w, p.id))
return;
const e_Setting* es = ecs_get(w, p.id, e_Setting);
if (es) {
auto ln = ls_node(ls, es->node);
if (ln && ln->setting(es->index))
ln->setting(es->index)->setUint32(i);
}
}
void set_enum(struct ls_API* ls,
ls_InputPin p, uint32_t i)
{
auto w = ls->_detail->ecs;
if (!ecs_is_alive(w, p.id))
return;
const e_Setting* es = ecs_get(w, p.id, e_Setting);
if (es) {
auto ln = ls_node(ls, es->node);
if (ln && ln->setting(es->index))
ln->setting(es->index)->setEnumeration(i);
}
}
void set_named_enum(struct ls_API* ls,
ls_InputPin p, ls_StringSlice enum_name)
{
auto w = ls->_detail->ecs;
if (!ecs_is_alive(w, p.id))
return;
const e_Setting* es = ecs_get(w, p.id, e_Setting);
if (es) {
auto ln = ls_node(ls, es->node);
if (ln && ln->setting(es->index)) {
string name(enum_name.start, enum_name.end - enum_name.start);
ln->setting(es->index)->setEnumeration(name.c_str());
}
}
}
void set_bool(struct ls_API* ls,
ls_InputPin p, bool b)
{
auto w = ls->_detail->ecs;
if (!ecs_is_alive(w, p.id))
return;
const e_Setting* es = ecs_get(w, p.id, e_Setting);
if (es) {
auto ln = ls_node(ls, es->node);
if (ln && ln->setting(es->index))
ln->setting(es->index)->setBool(b);
}
}
void set_bus(struct ls_API* ls,
ls_InputPin p, ls_BusData d)
{
auto w = ls->_detail->ecs;
if (!ecs_is_alive(w, p.id) || !ecs_is_alive(w, d.id))
return;
const e_Setting* es = ecs_get(w, p.id, e_Setting);
if (!es)
return;
auto ln = ls_node(ls, es->node);
if (ln && ln->setting(es->index)) {
const e_BusData* bd = ecs_get(w, d.id, e_BusData);
auto it = ls->_detail->busses.find(bd->hash_index);
if (it != ls->_detail->busses.end() && it->second.get()) {
ln->setting(es->index)->setBus(it->second.get());
}
}
}
void set_bus_from_file(struct ls_API* ls,
ls_InputPin p, ls_StringSlice path)
{
auto w = ls->_detail->ecs;
if (!ecs_is_alive(w, p.id))
return;
const e_Setting* es = ecs_get(w, p.id, e_Setting);
if (!es)
return;
auto ln = ls_node(ls, es->node);
if (ln && ln->setting(es->index)) {
string pathStr(path.start, path.end - path.start);
auto soundClip = lab::MakeBusFromFile(pathStr.c_str(), false);
ln->setting(es->index)->setBus(soundClip.get());
}
}
ls_Node destination_node(struct ls_API* ls)
{
return ls->_detail->destination_node;
}
// graph management
//
ls_Connection connect(struct ls_API* ls,
ls_InputPin input, ls_OutputPin output)
{
auto w = ls->_detail->ecs;
if (!ecs_is_alive(w, input.id) || !ecs_is_alive(w, output.id))
return ls_Connection_empty;
const e_Output* source_output = ecs_get(w, output.id, e_Output);
if (!source_output || !ecs_is_alive(w, source_output->node.id))
return ls_Connection_empty;
auto source_node = ls_node(ls, source_output->node);
if (!source_node)
return ls_Connection_empty;
const e_Input* dest_input = ecs_get(w, input.id, e_Input);
if (dest_input) {
auto dst_node = ls_node(ls, dest_input->node);
if (!dst_node)
return ls_Connection_empty;
// connect node output to node input
ls->_detail->ac->connect(dst_node, source_node,
dest_input->index, source_output->index);
ecs_entity_desc_t desc = { 0 };
auto e = ecs_entity_init(w, &desc);
ls_Connection lc { e };
e_Connection ec { output, input,
source_output->node, dest_input->node, source_output->index, dest_input->index, false };
ecs_add(w, e, e_Connection);
ecs_set_id(ls->_detail->ecs, e,
ecs_id(e_Connection), sizeof(ec), &ec);
ecs_add_pair(w, source_output->node.id, ls->_detail->has_connection_rel, e);
ecs_add_pair(w, e, EcsChildOf, source_output->node.id); // autodelete
ecs_add_pair(w, dest_input->node.id, ls->_detail->has_connection_rel, e);
ecs_add_pair(w, e, EcsChildOf, dest_input->node.id); // autodelete
return lc;
}
const e_Param* p = ecs_get(w, input.id, e_Param);
if (!p || !ecs_is_alive(w, p->node.id))
return ls_Connection_empty;
auto param_node = ls_node(ls, p->node);
if (!param_node)
return ls_Connection_empty;
// connect node output to param
ls->_detail->ac->connectParam(param_node->param(p->index), source_node, source_output->index);
ecs_entity_desc_t desc = { 0 };
auto connection_e = ecs_entity_init(w, &desc);
ls_Connection lc { connection_e };
e_Connection ec{ output, input, source_output->node, p->node, source_output->index, p->index, true };
ecs_add(w, connection_e, e_Connection);
ecs_set_id(ls->_detail->ecs, connection_e,
ecs_id(e_Connection), sizeof(ec), &ec);
ecs_add_pair(w, source_output->node.id, ls->_detail->has_connection_rel, connection_e);
ecs_add_pair(w, connection_e, EcsChildOf, source_output->node.id); // autodelete
ecs_add_pair(w, p->node.id, ls->_detail->has_connection_rel, connection_e);
ecs_add_pair(w, connection_e, EcsChildOf, p->node.id); // autodelete
return lc;
}
void disconnect(struct ls_API* ls,
ls_Connection c)
{
const e_Connection* ec = ls_connection(ls, c);
if (!ec)
return;
auto from_node = ls_node(ls, ec->from_node);
auto to_node = ls_node(ls, ec->to_node);
if (!from_node || !to_node)
return;
if (ec->to_is_param) {
ls->_detail->ac->disconnectParam(
to_node->param(ec->to_index), from_node, ec->from_output_index);
}
else {
ls->_detail->ac->disconnect(
to_node, from_node, ec->to_index, ec->from_output_index);
}
ecs_delete(ls->_detail->ecs, c.id);
}
static
int ecs_run_action(
ecs_world_t* world,
ecs_app_desc_t* desc)
{
if (desc->init) {
desc->init(world);
}
auto api = (ls_API*)desc->ctx;
api->_detail->desc = *desc;
return ecs_app_run_frame(world, desc) == 0;
}
static
ls_BusData bus_create_from_file(struct ls_API* ls,
const char* name, bool mix_to_mono)
{
const std::string path(name);
std::shared_ptr<lab::AudioBus> bus;
try {
bus = lab::MakeBusFromFile(path, mix_to_mono);
}
catch (...) {
return ls_BusData_empty;
}
if (!bus)
return ls_BusData_empty;
uint32_t id = ls->_detail->next_id++;
ls->_detail->busses.insert({ id , bus });
e_BusData en = { id };
ecs_entity_desc_t desc = { 0 };
desc.name = path.c_str();
auto e = ecs_entity_init(ls->_detail->ecs, &desc);
ecs_add(ls->_detail->ecs, e, e_BusData);
ecs_set_id(ls->_detail->ecs, e,
ecs_id(e_BusData), sizeof(en), &en);
return ls_BusData{ e };
}
} // namespace ls_1_0
extern "C"
void ls_idle(struct ls_API* ls) {
if (!ls || !ls->_detail->ecs)
return;
/* int result = */ ecs_app_run_frame(ls->_detail->ecs, &ls->_detail->desc) /* == 0 */;
}
extern "C"
struct ls_API* ls_create_api_1_0(ls_Alloc alloc) {
ls_API* api = (ls_API*)
alloc.malloc(sizeof(ls_API));
if (!api)
return nullptr;
memset(api, 0, sizeof(ls_API));
api->_detail = (ls_APIDetail*)
alloc.malloc(sizeof(ls_APIDetail));
if (!api->_detail)
return nullptr;
memset(api->_detail, 0, sizeof(ls_APIDetail));
new(api->_detail) ls_APIDetail();
api->_detail->alloc = alloc;
AudioStreamConfig _inputConfig;
AudioStreamConfig _outputConfig;
auto config = GetDefaultAudioDeviceConfiguration(true);
_inputConfig = config.first;
_outputConfig = config.second;
std::shared_ptr<lab::AudioDevice_RtAudio> device(new lab::AudioDevice_RtAudio(_inputConfig, _outputConfig));
auto context = std::make_shared<lab::AudioContext>(false, true);
auto destinationNode = std::make_shared<lab::AudioDestinationNode>(*context.get(), device);
device->setDestinationNode(destinationNode);
context->setDestinationNode(destinationNode);
api->_detail->dv = device;
api->_detail->dn = destinationNode;
api->_detail->ac = context;
api->_detail->ecs = ecs_init();
ECS_COMPONENT_DEFINE(api->_detail->ecs, e_Node);