/
clm2xen.c
13840 lines (11752 loc) · 496 KB
/
clm2xen.c
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
/* tie CLM module into Scheme, Ruby, or Forth */
/* if the optimizer stops working inexplicably, look for any symbols used before this that
* might shadow a generator name; one such case was (make-hook 'env...) in snd-env.c
*
* (env env) is accepted by the optimizer in error
*/
#include "mus-config.h"
#if USE_SND
#include "snd.h"
#endif
#include <stddef.h>
#include <math.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <stdarg.h>
#ifndef _MSC_VER
#include <unistd.h>
#else
#include <io.h>
#pragma warning(disable: 4244)
#endif
#include "_sndlib.h"
#include "xen.h"
#include "clm.h"
#include "sndlib2xen.h"
#include "vct.h"
#include "clm2xen.h"
#include "clm-strings.h"
#ifndef TWO_PI
#define TWO_PI (2.0 * M_PI)
#endif
#ifndef PROC_FALSE
#if HAVE_RUBY
#define PROC_FALSE "false"
#define PROC_TRUE "true"
#else
#define PROC_FALSE "#f"
#define PROC_TRUE "#t"
#endif
#endif
static Xen xen_float_zero;
/* -------------------------------------------------------------------------------- */
#if HAVE_SCHEME
static bool mus_simple_out_any_to_file(mus_long_t samp, mus_float_t val, int chan, mus_any *IO)
{
rdout *gen = (rdout *)IO;
if ((chan < gen->chans) &&
(samp <= gen->data_end) &&
(samp >= gen->data_start))
{
gen->obufs[chan][samp - gen->data_start] += val;
if (samp > gen->out_end)
gen->out_end = samp;
return(true);
}
return(false);
}
#endif
/* -------------------------------------------------------------------------------- */
struct mus_xen {
mus_any *gen;
int nvcts;
Xen *vcts; /* one for each accessible mus_float_t array (wrapped up here in a vct) */
struct mus_xen *next;
};
enum {MUS_DATA_WRAPPER, MUS_INPUT_FUNCTION, MUS_ANALYZE_FUNCTION, MUS_EDIT_FUNCTION, MUS_SYNTHESIZE_FUNCTION,
MUS_SELF_WRAPPER, MUS_INPUT_DATA, MUS_MAX_VCTS}; /* order matters, stuff before self_wrapper is GC marked */
static mus_xen *mx_free_lists[9] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
static mus_xen *mx_alloc(int vcts)
{
mus_xen *p;
if (mx_free_lists[vcts])
{
p = mx_free_lists[vcts];
mx_free_lists[vcts] = p->next;
return(p);
}
p = (mus_xen *)malloc(sizeof(mus_xen));
p->nvcts = vcts;
if (vcts > 0)
p->vcts = (Xen *)malloc(vcts * sizeof(Xen));
else p->vcts = NULL;
return(p);
}
static void mx_free(mus_xen *p)
{
p->next = mx_free_lists[p->nvcts];
mx_free_lists[p->nvcts] = p;
}
mus_any *mus_xen_gen(mus_xen *x) {return(x->gen);}
#define mus_xen_to_mus_any(Gn) (((mus_xen *)Gn)->gen)
#if (!HAVE_SCHEME)
#define XEN_NULL 0
#define Xen_real_to_C_double_if_bound(Xen_Arg, C_Val, Caller, ArgNum) \
if (Xen_is_bound(Xen_Arg)) {if (Xen_is_number(Xen_Arg)) C_Val = Xen_real_to_C_double(Xen_Arg); else Xen_check_type(false, Xen_Arg, ArgNum, Caller, "a number");}
#define Xen_to_C_double_or_error(Xen_Arg, C_Val, Caller, ArgNum) \
do {C_Val = 0.0; if (Xen_is_number(Xen_Arg)) C_Val = Xen_real_to_C_double(Xen_Arg); else Xen_check_type(false, Xen_Arg, ArgNum, Caller, "a number");} while (0)
#define Xen_real_to_C_double_with_caller(Xen_Arg, Caller) Xen_real_to_C_double(Xen_Arg)
#define Xen_to_C_integer_or_error(Xen_Arg, C_Val, Caller, ArgNum) \
do {if (Xen_is_integer(Xen_Arg)) C_Val = Xen_integer_to_C_int(Xen_Arg); else {C_Val = 0.0; Xen_check_type(false, Xen_Arg, ArgNum, Caller, "an integer");}} while (0)
#if (HAVE_FORTH) || (HAVE_RUBY)
#define Xen_object_ref_checked(Obj, Type) (Xen_c_object_is_type(Obj, Type) ? Xen_object_ref(Obj) : NULL)
#else
#define Xen_object_ref_checked(Obj, Type) NULL
#endif
#else
#define Xen_real_to_C_double_if_bound(Xen_Arg, C_Val, Caller, ArgNum) if (Xen_is_bound(Xen_Arg)) C_Val = (double)s7_number_to_real_with_caller(s7, Xen_Arg, Caller)
#define Xen_to_C_double_or_error(Xen_Arg, C_Val, Caller, ArgNum) C_Val = (double)s7_number_to_real_with_caller(s7, Xen_Arg, Caller)
#define Xen_real_to_C_double_with_caller(Xen_Arg, Caller) s7_number_to_real_with_caller(s7, Xen_Arg, Caller)
#define Xen_to_C_integer_or_error(Xen_Arg, C_Val, Caller, ArgNum) \
do {if (s7_is_integer(Xen_Arg)) C_Val = s7_integer(Xen_Arg); else {C_Val = 0.0; Xen_check_type(false, Xen_Arg, ArgNum, Caller, "an integer");}} while (0)
#define Xen_object_ref_checked(Obj, Type) s7_c_object_value_checked(Obj, Type)
#define XEN_NULL NULL
#endif
static int local_error_type = MUS_NO_ERROR;
static char *local_error_msg = NULL;
static void local_mus_error(int type, char *msg)
{
local_error_type = type;
if (local_error_msg) free(local_error_msg);
local_error_msg = mus_strdup(msg);
}
static Xen clm_mus_error(int type, const char *msg, const char *caller)
{
/* mus_error returns an int, which is a bother in this context */
mus_error(type, "%s: %s", caller, msg);
return(Xen_false);
}
#if HAVE_SCHEME
static s7_pointer mus_error_symbol, clm_error_info, clm_err1, clm_err2, clm_err3;
#define CLM_ERROR mus_error_symbol
static void clm_error(const char *caller, const char *msg, Xen val)
{
s7_set_car(clm_err1, s7_make_string_wrapper(s7, caller));
s7_set_car(clm_err2, s7_make_string_wrapper(s7, msg));
s7_set_car(clm_err3, val);
s7_error(s7, mus_error_symbol, clm_error_info);
}
#else
#define CLM_ERROR Xen_make_error_type("mus-error")
static void clm_error(const char *caller, const char *msg, Xen val)
{
Xen_error(CLM_ERROR,
Xen_list_4(C_string_to_Xen_string("~A: ~A ~A"),
C_string_to_Xen_string(caller),
C_string_to_Xen_string(msg),
val));
}
#endif
/* ---------------- optional-key ---------------- */
#if HAVE_SCHEME
static s7_pointer extra_args_string;
#endif
int mus_optkey_unscramble(const char *caller, int num_args, int nkeys, Xen *keys, Xen *args, int *orig)
{
/* implement the &optional-key notion in CLM */
/* "keys" holds the keywords the calling function accepts,
* upon return, if a key was given in the arglist or its position had a value, the corresponding value is in its keys location
* "nkeys is the size of "keys"
* "args" contains the original arguments passed to the function in order
* it should be of size nkeys * 2, and any trailing (unspecified) args should be Xen_undefined
* "orig" should be of size nkeys, and will contain upon return the 1-based location of the original keyword value argument
* (it is intended for error reports)
*/
int arg_ctr = 0, key_start = 0, rtn_ctr = 0, end;
bool keying = false;
end = num_args - 1;
while (arg_ctr < num_args)
{
Xen key;
key = args[arg_ctr];
if (!(Xen_is_keyword(key)))
{
if (arg_ctr >= nkeys) /* we aren't handling a keyword arg, so the underlying args should only take nkeys args */
#if HAVE_SCHEME
{
s7_set_car(clm_err1, s7_make_string_wrapper(s7, caller));
s7_set_car(clm_err2, extra_args_string);
s7_set_car(clm_err3, key);
s7_error(s7, mus_error_symbol, clm_error_info);
}
#else
clm_error(caller, "extra trailing args?", key);
#endif
if (keying)
clm_error(caller, "unmatched value within keyword section?", key);
/* type checking on the actual values has to be the caller's problem */
keys[arg_ctr] = key;
orig[arg_ctr] = arg_ctr + 1;
arg_ctr++;
key_start = arg_ctr;
rtn_ctr++;
}
else
{
int i;
Xen val;
bool key_found;
if (arg_ctr >= end)
clm_error(caller, "keyword without value?", key);
val = args[arg_ctr + 1];
if (Xen_is_keyword(val))
clm_error(caller, "two keywords in a row?", key);
keying = true;
key_found = false;
for (i = key_start; i < nkeys; i++)
{
if (Xen_keyword_is_eq(keys[i], key))
{
keys[i] = val;
arg_ctr += 2;
orig[i] = arg_ctr;
rtn_ctr++;
key_found = true;
break;
}
}
if (!key_found)
{
/* either there's a redundant keyword pair or a keyword that 'caller' doesn't recognize */
clm_error(caller, "redundant or invalid key found", key);
/* normally (all local cases) the error returns */
arg_ctr += 2;
}
}
}
return(rtn_ctr);
}
#if (!HAVE_SCHEME)
static mus_float_t optkey_float_error(Xen key, int n, const char *caller)
{
Xen_check_type(false, key, n, caller, "a number");
return(0.0);
}
#define Xen_optkey_to_float(Original_key, Key, Caller, N, Def) \
((Xen_keyword_is_eq(Original_key, Key)) ? Def : ((Xen_is_number(Key)) ? Xen_real_to_C_double(Key) : optkey_float_error(Key, N, Caller)))
#endif
mus_float_t mus_optkey_to_float(Xen key, const char *caller, int n, mus_float_t def)
{
if (Xen_is_number(key))
return(Xen_real_to_C_double(key));
if (!(Xen_is_keyword(key)))
Xen_check_type(false, key, n, caller, "a number");
return(def);
}
#if (!HAVE_SCHEME)
static int optkey_int_error(Xen key, int n, const char *caller)
{
Xen_check_type(false, key, n, caller, "an integer");
return(0);
}
#define Xen_optkey_to_int(Original_key, Key, Caller, N, Def) \
((Xen_keyword_is_eq(Original_key, Key)) ? Def : ((Xen_is_integer(Key)) ? Xen_integer_to_C_int(Key) : optkey_int_error(Key, N, Caller)))
#endif
int mus_optkey_to_int(Xen key, const char *caller, int n, int def)
{
if (Xen_is_integer(key))
return(Xen_integer_to_C_int(key));
if (!(Xen_is_keyword(key)))
Xen_check_type(false, key, n, caller, "an integer");
return(def);
}
bool mus_optkey_to_bool(Xen key, const char *caller, int n, bool def)
{
if (Xen_is_boolean(key))
return(Xen_boolean_to_C_bool(key));
if (!(Xen_is_keyword(key)))
Xen_check_type(false, key, n, caller, "#f or #t");
return(def);
}
#if (!HAVE_SCHEME)
static mus_long_t optkey_llong_error(Xen key, int n, const char *caller)
{
Xen_check_type(false, key, n, caller, "an integer");
return(0);
}
#endif
#define Xen_optkey_to_mus_long_t(Original_key, Key, Caller, N, Def) \
((Xen_keyword_is_eq(Original_key, Key)) ? Def : ((Xen_is_integer(Key)) ? Xen_llong_to_C_llong(Key) : optkey_llong_error(Key, N, Caller)))
mus_long_t mus_optkey_to_mus_long_t(Xen key, const char *caller, int n, mus_long_t def)
{
if (Xen_is_integer(key))
return(Xen_llong_to_C_llong(key));
if (!(Xen_is_keyword(key)))
Xen_check_type(false, key, n, caller, "a sample number or size");
return(def);
}
const char *mus_optkey_to_string(Xen key, const char *caller, int n, char *def)
{
if (Xen_is_string(key))
return(Xen_string_to_C_string(key));
if ((!(Xen_is_keyword(key))) && (!(Xen_is_false(key))))
Xen_check_type(false, key, n, caller, "a string");
return(def);
}
#if (!HAVE_SCHEME)
static vct *mus_optkey_to_vct(Xen key, const char *caller, int n, vct *def)
{
if (mus_is_vct(key))
return(Xen_to_vct(key));
if ((!(Xen_is_keyword(key))) && (!(Xen_is_false(key))))
Xen_check_type(false, key, n, caller, "a " S_vct);
return(def);
}
#endif
static bool local_arity_ok(Xen proc, int args) /* from snd-xen.c minus (inconvenient) gc protection */
{
#if HAVE_SCHEME
return(s7_is_aritable(s7, proc, args));
#else
Xen arity;
int rargs;
arity = Xen_arity(proc);
rargs = Xen_integer_to_C_int(arity);
#if HAVE_RUBY
return(xen_rb_arity_ok(rargs, args));
#endif
#if HAVE_FORTH
return(rargs == args);
#endif
#endif
return(true);
}
Xen mus_optkey_to_procedure(Xen key, const char *caller, int n, Xen def, int required_args, const char *err)
{
/* in this case, it's faster to look for the keyword first */
if ((!(Xen_is_keyword(key))) &&
(!(Xen_is_false(key))))
{
Xen_check_type(Xen_is_procedure(key), key, n, caller, "a procedure");
if (!(local_arity_ok(key, required_args)))
Xen_bad_arity_error(caller, n, key, err);
return(key);
}
return(def);
}
/* ---------------- clm keywords ---------------- */
#if HAVE_SCHEME
static s7_pointer kw_frequency, kw_radius, kw_readable;
static void init_keywords(void)
{
kw_frequency = Xen_make_keyword("frequency");
kw_radius = Xen_make_keyword("radius");
kw_readable = Xen_make_keyword("readable");
}
#else
static Xen kw_frequency, kw_initial_phase, kw_wave, kw_amplitude,
kw_r, kw_ratio, kw_size, kw_a0, kw_a1, kw_a2, kw_b1, kw_b2, kw_max_size,
kw_input, kw_srate, kw_file, kw_channel, kw_start,
kw_initial_contents, kw_initial_element, kw_scaler, kw_feedforward, kw_feedback,
kw_radius, kw_partials, kw_a, kw_n,
kw_order, kw_x_coeffs, kw_y_coeffs, kw_envelope, kw_base, kw_duration, kw_offset, kw_end,
kw_direction, kw_degree, kw_distance, kw_reverb, kw_output, kw_fft_size,
kw_expansion, kw_length, kw_hop, kw_ramp, kw_jitter,
kw_type, kw_channels, kw_filter, kw_revout, kw_width,
kw_edit, kw_synthesize, kw_analyze, kw_interp, kw_overlap, kw_pitch,
kw_distribution, kw_coeffs, kw_kind;
static void init_keywords(void)
{
/* in Ruby there's rb_intern of the symbol -- is it safe? */
kw_frequency = Xen_make_keyword("frequency");
kw_initial_phase = Xen_make_keyword("initial-phase");
kw_wave = Xen_make_keyword("wave");
kw_amplitude = Xen_make_keyword("amplitude");
kw_r = Xen_make_keyword("r");
kw_ratio = Xen_make_keyword("ratio");
kw_size = Xen_make_keyword("size");
kw_a0 = Xen_make_keyword("a0");
kw_a1 = Xen_make_keyword("a1");
kw_a2 = Xen_make_keyword("a2");
kw_b1 = Xen_make_keyword("b1");
kw_b2 = Xen_make_keyword("b2");
kw_max_size = Xen_make_keyword("max-size");
kw_input = Xen_make_keyword("input");
kw_srate = Xen_make_keyword("srate");
kw_file = Xen_make_keyword("file");
kw_channel = Xen_make_keyword("channel");
kw_start = Xen_make_keyword("start"); /* make-readin */
kw_initial_contents = Xen_make_keyword("initial-contents");
kw_initial_element = Xen_make_keyword("initial-element");
kw_scaler = Xen_make_keyword("scaler");
kw_feedforward = Xen_make_keyword("feedforward");
kw_feedback = Xen_make_keyword("feedback");
kw_radius = Xen_make_keyword("radius");
kw_partials = Xen_make_keyword("partials");
kw_a = Xen_make_keyword("a");
kw_n = Xen_make_keyword("n");
kw_order = Xen_make_keyword("order");
kw_x_coeffs = Xen_make_keyword("xcoeffs");
kw_y_coeffs = Xen_make_keyword("ycoeffs");
kw_envelope = Xen_make_keyword("envelope");
kw_base = Xen_make_keyword("base");
kw_duration = Xen_make_keyword("duration");
kw_offset = Xen_make_keyword("offset");
kw_end = Xen_make_keyword("end");
kw_direction = Xen_make_keyword("direction");
kw_degree = Xen_make_keyword("degree");
kw_distance = Xen_make_keyword("distance");
kw_reverb = Xen_make_keyword("reverb");
kw_output = Xen_make_keyword("output");
kw_fft_size = Xen_make_keyword("fft-size");
kw_expansion = Xen_make_keyword("expansion");
kw_length = Xen_make_keyword("length");
kw_hop = Xen_make_keyword("hop");
kw_ramp = Xen_make_keyword("ramp");
kw_jitter = Xen_make_keyword("jitter");
kw_type = Xen_make_keyword("type");
kw_channels = Xen_make_keyword("channels");
kw_filter = Xen_make_keyword("filter");
kw_revout = Xen_make_keyword("revout");
kw_width = Xen_make_keyword("width");
kw_edit = Xen_make_keyword("edit");
kw_synthesize = Xen_make_keyword("synthesize");
kw_analyze = Xen_make_keyword("analyze");
kw_interp = Xen_make_keyword("interp");
kw_overlap = Xen_make_keyword("overlap");
kw_pitch = Xen_make_keyword("pitch");
kw_distribution = Xen_make_keyword("distribution");
kw_coeffs = Xen_make_keyword("coeffs");
kw_kind = Xen_make_keyword("kind");
}
#endif
/* ---------------- *clm-table-size* ---------------- */
static mus_long_t clm_table_size = MUS_CLM_DEFAULT_TABLE_SIZE;
#if HAVE_SCHEME
static s7_pointer clm_table_size_symbol;
#endif
mus_long_t clm_default_table_size_c(void) {return(clm_table_size);}
static Xen g_clm_table_size(void) {return(C_llong_to_Xen_llong(clm_table_size));}
static Xen g_set_clm_table_size(Xen val)
{
mus_long_t size;
#define H_clm_table_size "(" S_clm_table_size "): the default table size for most generators (512)"
Xen_check_type(Xen_is_llong(val), val, 1, S_set S_clm_table_size, "an integer");
size = Xen_llong_to_C_llong(val);
if ((size <= 0) ||
(size > mus_max_table_size()))
Xen_out_of_range_error(S_set S_clm_table_size, 1, val, "invalid size (see mus-max-table-size)");
clm_table_size = size;
#if HAVE_SCHEME
s7_symbol_set_value(s7, clm_table_size_symbol, s7_make_integer(s7, clm_table_size));
#endif
return(C_llong_to_Xen_llong(clm_table_size));
}
/* ---------------- AM and simple stuff ---------------- */
static const char *fft_window_xen_names[MUS_NUM_FFT_WINDOWS] =
{S_rectangular_window, S_hann_window, S_welch_window, S_parzen_window, S_bartlett_window,
S_hamming_window, S_blackman2_window, S_blackman3_window, S_blackman4_window,
S_exponential_window, S_riemann_window, S_kaiser_window, S_cauchy_window,
S_poisson_window, S_gaussian_window, S_tukey_window, S_dolph_chebyshev_window,
S_hann_poisson_window, S_connes_window, S_samaraki_window, S_ultraspherical_window,
S_bartlett_hann_window, S_bohman_window, S_flat_top_window,
S_blackman5_window, S_blackman6_window, S_blackman7_window, S_blackman8_window, S_blackman9_window, S_blackman10_window,
S_rv2_window, S_rv3_window, S_rv4_window, S_mlt_sine_window, S_papoulis_window, S_dpss_window, S_sinc_window
};
const char *mus_fft_window_xen_name(mus_fft_window_t i) {return(fft_window_xen_names[(int)i]);}
static Xen g_mus_file_buffer_size(void)
{
#define H_mus_file_buffer_size "(" S_mus_file_buffer_size "): current CLM IO buffer size (default is 8192)"
return(C_llong_to_Xen_llong(mus_file_buffer_size()));
}
#if HAVE_SCHEME
static s7_pointer mus_file_buffer_size_symbol;
#endif
static Xen g_mus_set_file_buffer_size(Xen val)
{
mus_long_t len;
Xen_check_type(Xen_is_llong(val), val, 1, S_set S_mus_file_buffer_size, "an integer");
len = Xen_llong_to_C_llong(val);
if (len <= 0)
Xen_out_of_range_error(S_set S_mus_file_buffer_size, 1, val, "must be > 0");
mus_set_file_buffer_size(len);
#if HAVE_SCHEME
s7_symbol_set_value(s7, mus_file_buffer_size_symbol, s7_make_integer(s7, len));
#endif
return(val);
}
static Xen g_radians_to_hz(Xen val)
{
#define H_radians_to_hz "(" S_radians_to_hz " rads): convert radians per sample to frequency in Hz: rads * srate / (2 * pi)"
mus_float_t x;
Xen_to_C_double_or_error(val, x, S_radians_to_hz, 1);
return(C_double_to_Xen_real(mus_radians_to_hz(x)));
}
static Xen g_hz_to_radians(Xen val)
{
#define H_hz_to_radians "(" S_hz_to_radians " hz): convert frequency in Hz to radians per sample: hz * 2 * pi / srate"
mus_float_t x;
Xen_to_C_double_or_error(val, x, S_hz_to_radians, 1);
return(C_double_to_Xen_real(mus_hz_to_radians(x)));
}
static Xen g_radians_to_degrees(Xen val)
{
#define H_radians_to_degrees "(" S_radians_to_degrees " rads): convert radians to degrees: rads * 360 / (2 * pi)"
mus_float_t x;
Xen_to_C_double_or_error(val, x, S_radians_to_degrees, 1);
return(C_double_to_Xen_real(mus_radians_to_degrees(x)));
}
static Xen g_degrees_to_radians(Xen val)
{
#define H_degrees_to_radians "(" S_degrees_to_radians " deg): convert degrees to radians: deg * 2 * pi / 360"
mus_float_t x;
Xen_to_C_double_or_error(val, x, S_degrees_to_radians, 1);
return(C_double_to_Xen_real(mus_degrees_to_radians(x)));
}
static Xen g_db_to_linear(Xen val)
{
#define H_db_to_linear "(" S_db_to_linear " db): convert decibel value db to linear value: pow(10, db / 20)"
mus_float_t x;
Xen_to_C_double_or_error(val, x, S_db_to_linear, 1);
return(C_double_to_Xen_real(mus_db_to_linear(x)));
}
static Xen g_linear_to_db(Xen val)
{
#define H_linear_to_db "(" S_linear_to_db " lin): convert linear value to decibels: 20 * log10(lin)"
mus_float_t x;
Xen_to_C_double_or_error(val, x, S_linear_to_db, 1);
return(C_double_to_Xen_real(mus_linear_to_db(x)));
}
static Xen g_even_weight(Xen val)
{
#define H_even_weight "(" S_even_weight " x): return the even weight of x"
mus_float_t x;
Xen_to_C_double_or_error(val, x, S_even_weight, 1);
return(C_double_to_Xen_real(mus_even_weight(x)));
}
static Xen g_odd_weight(Xen val)
{
#define H_odd_weight "(" S_odd_weight " x): return the odd weight of x"
mus_float_t x;
Xen_to_C_double_or_error(val, x, S_odd_weight, 1);
return(C_double_to_Xen_real(mus_odd_weight(x)));
}
static Xen g_even_multiple(Xen val1, Xen val2)
{
#define H_even_multiple "(" S_even_multiple " x y): return the even multiple of x and y"
mus_float_t x, y;
Xen_to_C_double_or_error(val1, x, S_even_multiple, 1);
Xen_to_C_double_or_error(val2, y, S_even_multiple, 2);
return(C_double_to_Xen_real(mus_even_multiple(x, y)));
}
static Xen g_odd_multiple(Xen val1, Xen val2)
{
#define H_odd_multiple "(" S_odd_multiple " x y): return the odd multiple of x and y"
mus_float_t x, y;
Xen_to_C_double_or_error(val1, x, S_odd_multiple, 1);
Xen_to_C_double_or_error(val2, y, S_odd_multiple, 2);
return(C_double_to_Xen_real(mus_odd_multiple(x, y)));
}
static Xen g_seconds_to_samples(Xen val)
{
#define H_seconds_to_samples "(" S_seconds_to_samples " secs): use " S_mus_srate " to convert seconds to samples"
mus_float_t x;
Xen_to_C_double_or_error(val, x, S_seconds_to_samples, 1);
return(C_llong_to_Xen_llong(mus_seconds_to_samples(x)));
}
static Xen g_samples_to_seconds(Xen val)
{
#define H_samples_to_seconds "(" S_samples_to_seconds " samps): use " S_mus_srate " to convert samples to seconds"
Xen_check_type(Xen_is_llong(val), val, 1, S_samples_to_seconds, "a number");
return(C_double_to_Xen_real(mus_samples_to_seconds(Xen_llong_to_C_llong(val))));
}
#if HAVE_SCHEME
static s7_pointer clm_srate_symbol;
#endif
static Xen g_mus_srate(void)
{
#define H_mus_srate "(" S_mus_srate "): current sampling rate"
return(C_double_to_Xen_real(mus_srate()));
}
static Xen g_mus_set_srate(Xen val)
{
mus_float_t sr;
Xen_check_type(Xen_is_number(val), val, 1, S_set S_mus_srate, "a number");
sr = Xen_real_to_C_double(val);
if (sr != mus_srate())
{
if (sr <= 0.0)
Xen_out_of_range_error(S_set S_mus_srate, 1, val, "must be > 0.0");
mus_set_srate(sr);
#if HAVE_SCHEME
s7_symbol_set_value(s7, clm_srate_symbol, s7_make_real(s7, sr));
#endif
}
return(val);
}
#if HAVE_SCHEME
static s7_pointer mus_float_equal_fudge_factor_symbol;
#endif
static Xen g_mus_float_equal_fudge_factor(void)
{
#define H_mus_float_equal_fudge_factor "(" S_mus_float_equal_fudge_factor "): floating point equality fudge factor"
return(C_double_to_Xen_real(mus_float_equal_fudge_factor()));
}
static Xen g_mus_set_float_equal_fudge_factor(Xen val)
{
mus_float_t factor;
Xen_check_type(Xen_is_number(val), val, 1, S_set S_mus_float_equal_fudge_factor, "a number");
factor = Xen_real_to_C_double(val);
if (factor != mus_float_equal_fudge_factor())
{
mus_set_float_equal_fudge_factor(factor);
#if HAVE_SCHEME
s7_symbol_set_value(s7, mus_float_equal_fudge_factor_symbol, s7_make_real(s7, factor));
#endif
}
return(val);
}
#if HAVE_SCHEME
static s7_pointer mus_array_print_length_symbol;
#endif
static Xen g_mus_array_print_length(void)
{
#define H_mus_array_print_length "(" S_mus_array_print_length "): current clm array print length (default is 8). This \
affects error reporting and generator descriptions. Array (" S_vct ") elements beyond this length are represented by '...'"
return(C_int_to_Xen_integer(mus_array_print_length()));
}
static Xen g_mus_set_array_print_length(Xen val)
{
int len;
Xen_check_type(Xen_is_integer(val), val, 1, S_set S_mus_array_print_length, "an integer");
len = Xen_integer_to_C_int(val);
if (len != mus_array_print_length())
{
if (len < 0)
Xen_out_of_range_error(S_set S_mus_array_print_length, 1, val, "must be >= 0");
mus_set_array_print_length(len);
#if HAVE_SCHEME
s7_symbol_set_value(s7, mus_array_print_length_symbol, s7_make_integer(s7, len));
#endif
}
return(val);
}
static Xen g_ring_modulate(Xen val1, Xen val2)
{
#define H_ring_modulate "(" S_ring_modulate " s1 s2): s1 * s2 (sample by sample multiply)"
Xen_check_type(Xen_is_number(val1), val1, 1, S_ring_modulate, "a number");
Xen_check_type(Xen_is_number(val2), val2, 2, S_ring_modulate, "a number");
return(C_double_to_Xen_real(mus_ring_modulate(Xen_real_to_C_double(val1), Xen_real_to_C_double(val2))));
}
static Xen g_amplitude_modulate(Xen val1, Xen val2, Xen val3)
{
#define H_amplitude_modulate "(" S_amplitude_modulate " carrier in1 in2): in1 * (carrier + in2)"
Xen_check_type(Xen_is_number(val1), val1, 1, S_amplitude_modulate, "a number");
Xen_check_type(Xen_is_number(val2), val2, 2, S_amplitude_modulate, "a number");
Xen_check_type(Xen_is_number(val3), val3, 3, S_amplitude_modulate, "a number");
return(C_double_to_Xen_real(mus_amplitude_modulate(Xen_real_to_C_double(val1), Xen_real_to_C_double(val2), Xen_real_to_C_double(val3))));
}
static Xen g_contrast_enhancement(Xen val1, Xen val2)
{
mus_float_t index = 1.0; /* this is the default in clm.html and mus.lisp */
#define H_contrast_enhancement "(" S_contrast_enhancement " sig (index 1.0)): sin(sig * pi / 2 + index * sin(sig * 2 * pi))"
Xen_check_type(Xen_is_number(val1), val1, 1, S_contrast_enhancement, "a number");
if (Xen_is_bound(val2))
{
Xen_check_type(Xen_is_number(val2), val2, 2, S_contrast_enhancement, "a number");
index = Xen_real_to_C_double(val2);
}
return(C_double_to_Xen_real(mus_contrast_enhancement(Xen_real_to_C_double(val1), index)));
}
static Xen g_dot_product(Xen val1, Xen val2, Xen size)
{
#define H_dot_product "(" S_dot_product " v1 v2 (size)): sum of v1[i] * v2[i] (also named scalar product)"
vct *v1, *v2;
mus_long_t len;
Xen_check_type(mus_is_vct(val1), val1, 1, S_dot_product, "a " S_vct);
Xen_check_type(mus_is_vct(val2), val2, 2, S_dot_product, "a " S_vct);
Xen_check_type(Xen_is_llong_or_unbound(size), size, 3, S_dot_product, "an integer");
v1 = Xen_to_vct(val1);
v2 = Xen_to_vct(val2);
if (Xen_is_llong(size))
{
len = Xen_llong_to_C_llong(size);
if (len == 0) return(C_double_to_Xen_real(0.0));
if (len < 0)
Xen_out_of_range_error(S_dot_product, 3, size, "size < 0?");
if (len > mus_vct_length(v1)) len = mus_vct_length(v1);
}
else len = mus_vct_length(v1);
if (len > mus_vct_length(v2)) len = mus_vct_length(v2);
return(C_double_to_Xen_real(mus_dot_product(mus_vct_data(v1), mus_vct_data(v2), len)));
}
#if HAVE_COMPLEX_TRIG && HAVE_COMPLEX_NUMBERS && (!HAVE_RUBY)
#if defined(__sun) && defined(__SVR4)
#undef _Complex_I
#define _Complex_I 1.0fi
#endif
#define S_edot_product "edot-product"
static Xen g_edot_product(Xen val1, Xen val2)
{
#define H_edot_product "(" S_edot_product " freq data): sum of (e^freq*i) * data[i]"
mus_long_t i, len;
vct *v = NULL;
complex double freq;
complex double *vals;
Xen result;
Xen_check_type(Xen_is_complex(val1), val1, 1, S_edot_product, "complex");
Xen_check_type((mus_is_vct(val2)) || (Xen_is_vector(val2)), val2, 2, S_edot_product, "a " S_vct);
freq = Xen_complex_to_C_complex(val1);
if (mus_is_vct(val2))
{
v = Xen_to_vct(val2);
len = mus_vct_length(v);
}
else
{
len = Xen_vector_length(val2);
}
vals = (complex double *)calloc(len, sizeof(complex double));
if (mus_is_vct(val2))
{
mus_float_t *vdata;
vdata = mus_vct_data(v);
for (i = 0; i < len; i++)
vals[i] = vdata[i];
}
else
{
for (i = 0; i < len; i++)
vals[i] = Xen_complex_to_C_complex(Xen_vector_ref(val2, i));
}
result = C_complex_to_Xen_complex(mus_edot_product(freq, vals, len));
free(vals);
return(result);
}
#endif
typedef enum {G_RECTANGULAR_POLAR, G_POLAR_RECTANGULAR, G_RECTANGULAR_MAGNITUDES} xclm_window_t;
static Xen g_fft_window_1(xclm_window_t choice, Xen val1, Xen val2, Xen ulen, const char *caller)
{
vct *v1, *v2;
mus_long_t len;
Xen_check_type(mus_is_vct(val1), val1, 1, caller, "a " S_vct);
Xen_check_type(mus_is_vct(val2), val2, 2, caller, "a " S_vct);
Xen_check_type(Xen_is_llong_or_unbound(ulen), ulen, 3, caller, "an integer");
v1 = Xen_to_vct(val1);
v2 = Xen_to_vct(val2);
if (Xen_is_llong(ulen))
{
len = Xen_llong_to_C_llong(ulen);
if (len == 0) return(Xen_false);
if (len < 0)
Xen_out_of_range_error(caller, 3, ulen, "size < 0?");
if (len > mus_vct_length(v1)) len = mus_vct_length(v1);
}
else len = mus_vct_length(v1);
if (len > mus_vct_length(v2)) len = mus_vct_length(v2);
switch (choice)
{
case G_RECTANGULAR_POLAR: mus_rectangular_to_polar(mus_vct_data(v1), mus_vct_data(v2), len); break;
case G_RECTANGULAR_MAGNITUDES: mus_rectangular_to_magnitudes(mus_vct_data(v1), mus_vct_data(v2), len); break;
case G_POLAR_RECTANGULAR: mus_polar_to_rectangular(mus_vct_data(v1), mus_vct_data(v2), len); break;
}
return(val1);
}
static Xen g_rectangular_to_polar(Xen val1, Xen val2)
{
#define H_rectangular_to_polar "(" S_rectangular_to_polar " rl im): convert real/imaginary \
data in " S_vct "s rl and im from rectangular form (fft output) to polar form (a spectrum)"
return(g_fft_window_1(G_RECTANGULAR_POLAR, val1, val2, Xen_undefined, S_rectangular_to_polar));
}
static Xen g_rectangular_to_magnitudes(Xen val1, Xen val2)
{
#define H_rectangular_to_magnitudes "(" S_rectangular_to_magnitudes " rl im): convert real/imaginary \
data in " S_vct "s rl and im from rectangular form (fft output) to polar form, but ignore the phases"
return(g_fft_window_1(G_RECTANGULAR_MAGNITUDES, val1, val2, Xen_undefined, S_rectangular_to_magnitudes));
}
static Xen g_polar_to_rectangular(Xen val1, Xen val2)
{
#define H_polar_to_rectangular "(" S_polar_to_rectangular " rl im): convert real/imaginary \
data in " S_vct "s rl and im from polar (spectrum) to rectangular (fft)"
return(g_fft_window_1(G_POLAR_RECTANGULAR, val1, val2, Xen_undefined, S_polar_to_rectangular));
}
static Xen g_mus_fft(Xen url, Xen uim, Xen len, Xen usign)
{
#define H_mus_fft "(" S_mus_fft " rl im (len) (dir 1)): return the fft of " S_vct "s rl and im which contain \
the real and imaginary parts of the data; len should be a power of 2, dir = 1 for fft, -1 for inverse-fft"
int sign;
mus_long_t n;
vct *v1, *v2;
Xen_check_type((mus_is_vct(url)), url, 1, S_mus_fft, "a " S_vct);
Xen_check_type((mus_is_vct(uim)), uim, 2, S_mus_fft, "a " S_vct);
v1 = Xen_to_vct(url);
v2 = Xen_to_vct(uim);
if (Xen_is_integer(usign))
sign = Xen_integer_to_C_int(usign);
else sign = 1;
if (Xen_is_llong(len))
{
n = Xen_llong_to_C_llong(len);
if (n <= 0)
Xen_out_of_range_error(S_mus_fft, 3, len, "size <= 0?");
if (n > mus_max_malloc())
Xen_out_of_range_error(S_mus_fft, 3, len, "size too large (see mus-max-malloc)");
if (n > mus_vct_length(v1))
n = mus_vct_length(v1);
}
else n = mus_vct_length(v1);
if (n > mus_vct_length(v2))
n = mus_vct_length(v2);
if (!(is_power_of_2(n)))
{
mus_float_t nf;
int np;
nf = (log(n) / log(2.0));
np = (int)nf;
n = (mus_long_t)pow(2.0, np);
}
if (n > 0)
mus_fft(mus_vct_data(v1), mus_vct_data(v2), n, sign);
/*
* in fftw, there's the extra complex array allocation, so for n = 2^29
* (and doubles for vcts as well as fftw), we need 24.6 Gbytes, and the FFT
* takes 144 secs on a 2.4 GHz machine. (Similarly, 2^28 needs 12.6 Gb
* and takes 61 secs).
*/
return(url);
}
static Xen g_make_fft_window(Xen type, Xen size, Xen ubeta, Xen ualpha)
{
#if HAVE_SCHEME
#define make_window_example "(" S_make_fft_window " " S_hamming_window " 256)"
#endif
#if HAVE_RUBY
#define make_window_example "make_fft_window(Hamming_window, 256)"
#endif
#if HAVE_FORTH