-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcsnd.go
2405 lines (2149 loc) · 77.7 KB
/
csnd.go
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
package csnd
/*
#cgo CFLAGS: -DUSE_DOUBLE=1
#cgo CFLAGS: -I /usr/local/include
#cgo linux CFLAGS: -DLINUX=1
#cgo LDFLAGS: -lcsound64
#include <csound/csound.h>
CS_AUDIODEVICE *getAudioDevList(CSOUND *csound, int n, int isOutput)
{
CS_AUDIODEVICE *devs = (CS_AUDIODEVICE *)malloc(n*sizeof(CS_AUDIODEVICE));
csoundGetAudioDevList(csound, devs, isOutput);
return devs;
}
void getAudioDev(CS_AUDIODEVICE *devs, int i, char **pname, char **pid, char **pmodule,
int *nchnls, int *flag)
{
CS_AUDIODEVICE dev = devs[i];
*pname = dev.device_name;
*pid = dev.device_id;
*pmodule = dev.rt_module;
*nchnls = dev.max_nchnls;
*flag = dev.isOutput;
}
CS_MIDIDEVICE *getMidiDevList(CSOUND *csound, int n, int isOutput)
{
CS_MIDIDEVICE *devs = (CS_MIDIDEVICE *)malloc(n*sizeof(CS_MIDIDEVICE));
csoundGetMIDIDevList(csound, devs, isOutput);
return devs;
}
void getMidiDev(CS_MIDIDEVICE *devs, int i, char **pname, char** piname, char **pid,
char **pmodule, int *flag)
{
CS_MIDIDEVICE dev = devs[i];
*pname = dev.device_name;
*piname = dev.interface_name;
*pid = dev.device_id;
*pmodule = dev.midi_module;
*flag = dev.isOutput;
}
void cMessage(CSOUND *csound, char *msg)
{
csoundMessage(csound, "%s", msg);
}
void cMessageS(CSOUND *csound, int attr, char *msg)
{
csoundMessageS(csound, attr, "%s", msg);
}
void noMessageCallback(CSOUND* cs, int attr, const char *format, va_list valist)
{
// Do nothing so that Csound will not print any message,
// leaving a clean console for our app.
return;
}
void cNoMessage(CSOUND* cs)
{
csoundSetMessageCallback(cs, noMessageCallback);
}
void *getOpcodeList(CSOUND *csound, int *pn)
{
opcodeListEntry *opcodeList;
*pn = csoundNewOpcodeList(csound, &opcodeList);
return (void *)opcodeList;
}
void getOpcodeEntry(void *list, int n,
char **opname, char **outypes, char** intypes, int* flags )
{
opcodeListEntry entry = *((opcodeListEntry *)list + n);
*opname = entry.opname;
*outypes = entry.outypes;
*intypes = entry.intypes;
*flags = entry.flags;
}
void freeOpcodeList(CSOUND *cs, void *list)
{
csoundDisposeOpcodeList(cs, (opcodeListEntry *)list);
}
controlChannelHints_t *getControlChannelInfo(controlChannelInfo_t *list, int i,
char **name, int *type)
{
*name = list[i].name;
*type = list[i].type;
return &list[i].hints;
}
PVSDATEXT *newPvsData()
{
return (PVSDATEXT *)calloc(sizeof(PVSDATEXT), 1);
}
void freePvsData(PVSDATEXT *p)
{
if (p)
free(p);
}
typedef struct namedgen {
char *name;
int gennum;
struct namedgen *next;
} NAMEDGEN;
int getNumNamedGens(CSOUND *csound)
{
NAMEDGEN *p;
int n;
n = 0;
p = (NAMEDGEN *)csoundGetNamedGens(csound);
while (p) {
n++;
p = p->next;
}
return n;
}
void *getNamedGen(CSOUND *csound, void *currentGen, char **pname, int *num)
{
NAMEDGEN *p = (NAMEDGEN *)currentGen;
*pname = p->name;
*num = p->gennum;
return p->next;
}
int utilityListLength(char **list)
{
int n;
n = 0;
while (*list++) {
n++;
}
return n;
}
char *utilityName(char **list, int i)
{
return *(list+i);
}
CsoundRandMTState *newRandMTState()
{
return (CsoundRandMTState *)malloc(sizeof(CsoundRandMTState));
}
void freeRandMTState(CsoundRandMTState *p)
{
if (p)
free(p);
}
*/
import "C"
import (
"fmt"
"reflect"
"unsafe"
)
func cbool(flag bool) C.int {
if flag {
return 1
}
return 0
}
func cMYFLT(val MYFLT) C.double {
return C.double(val)
}
func cpMYFLT(pval *MYFLT) *C.double {
return (*C.double)(pval)
}
func cppMYFLT(ppval **MYFLT) **C.double {
return (**C.double)(unsafe.Pointer(ppval))
}
func FileOpen(name, mode string) *C.FILE {
var cname *C.char = C.CString(name)
defer C.free(unsafe.Pointer(cname))
var cmode *C.char = C.CString(mode)
defer C.free(unsafe.Pointer(cmode))
return C.fopen(cname, cmode)
}
func FileClose(f *C.FILE) {
C.fclose(f)
}
// Error Definitions
const (
CSOUND_SUCCESS = 0 // Completed successfully.
CSOUND_ERROR = -1 // Unspecified failure.
CSOUND_INITIALIZATION = -2 // Failed during initialization.
CSOUND_PERFORMANCE = -3 // Failed during performance.
CSOUND_MEMORY = -4 // Failed to allocate requested memory.
CSOUND_SIGNAL = -5 // Termination requested by SIGINT or SIGTERM.
)
// Flags for csoundInitialize().
const (
CSOUNDINIT_NO_SIGNAL_HANDLER = 1
CSOUNDINIT_NO_ATEXIT = 2
)
const (
// This should only be used internally by the original FileOpen()
// API call or for temp files written with <CsFileB>
CSFTYPE_UNKNOWN = iota
CSFTYPE_UNIFIED_CSD // Unified Csound document
CSFTYPE_ORCHESTRA // the primary orc file (may be temporary)
CSFTYPE_SCORE // the primary sco file (may be temporary)
// or any additional score opened by Cscore
CSFTYPE_ORC_INCLUDE // a file #included by the orchestra
CSFTYPE_SCO_INCLUDE // a file #included by the score
CSFTYPE_SCORE_OUT // used for score.srt score.xtr cscore.out
CSFTYPE_SCOT // Scot score input format
CSFTYPE_OPTIONS // for .csoundrc and -@ flag
CSFTYPE_EXTRACT_PARMS // extraction file specified by -x
// audio file types that Csound can write (10-19) or read
CSFTYPE_RAW_AUDIO
CSFTYPE_IRCAM
CSFTYPE_AIFF
CSFTYPE_AIFC
CSFTYPE_WAVE
CSFTYPE_AU
CSFTYPE_SD2
CSFTYPE_W64
CSFTYPE_WAVEX
CSFTYPE_FLAC
CSFTYPE_CAF
CSFTYPE_WVE
CSFTYPE_OGG
CSFTYPE_MPC2K
CSFTYPE_RF64
CSFTYPE_AVR
CSFTYPE_HTK
CSFTYPE_MAT4
CSFTYPE_MAT5
CSFTYPE_NIST
CSFTYPE_PAF
CSFTYPE_PVF
CSFTYPE_SDS
CSFTYPE_SVX
CSFTYPE_VOC
CSFTYPE_XI
CSFTYPE_UNKNOWN_AUDIO // used when opening audio file for reading
// or temp file written with <CsSampleB>
// miscellaneous music formats
CSFTYPE_SOUNDFONT
CSFTYPE_STD_MIDI // Standard MIDI file
CSFTYPE_MIDI_SYSEX // Raw MIDI codes eg. SysEx dump
// analysis formats
CSFTYPE_HETRO
CSFTYPE_HETROT
CSFTYPE_PVC // original PVOC format
CSFTYPE_PVCEX // PVOC-EX format
CSFTYPE_CVANAL
CSFTYPE_LPC
CSFTYPE_ATS
CSFTYPE_LORIS
CSFTYPE_SDIF
CSFTYPE_HRTF
// Types for plugins and the files they read/write
CSFTYPE_UNUSED
CSFTYPE_LADSPA_PLUGIN
CSFTYPE_SNAPSHOT
// Special formats for Csound ftables or scanned synthesis
// matrices with header info
CSFTYPE_FTABLES_TEXT // for ftsave and ftload
CSFTYPE_FTABLES_BINARY // for ftsave and ftload
CSFTYPE_XSCANU_MATRIX // for xscanu opcode
// These are for raw lists of numbers without header info
CSFTYPE_FLOATS_TEXT // used by GEN23 GEN28 dumpk readk
CSFTYPE_FLOATS_BINARY // used by dumpk readk etc.
CSFTYPE_INTEGER_TEXT // used by dumpk readk etc.
CSFTYPE_INTEGER_BINARY // used by dumpk readk etc.
// image file formats
CSFTYPE_IMAGE_PNG
// For files that don't match any of the above
CSFTYPE_POSTSCRIPT // EPS format used by graphs
CSFTYPE_SCRIPT_TEXT // executable script files (eg. Python)
CSFTYPE_OTHER_TEXT
CSFTYPE_OTHER_BINARY
)
// Encapsulates an opaque pointer to a Csound instance
type CSOUND struct {
Cs (*C.CSOUND)
}
type MYFLT float64
type CsoundParams struct {
DebugMode int32 // debug mode, 0 or 1
BufferFrames int32 // number of frames in in/out buffers
HardwareBufferFrames int32 // ibid. hardware
Displays int32 // graph displays, 0 or 1
AsciiGraphs int32 // use ASCII graphs, 0 or 1
PostscriptGraphs int32 // use postscript graphs, 0 or 1
MessageLevel int32 // message printout control
Tempo int32 // tempo (sets Beatmode)
RingBell int32 // bell, 0 or 1
UseCscore int32 // use cscore for processing
TerminateOnMidi int32 // terminate performance at the end of midifile, 0 or 1
HeartBeat int32 // print heart beat, 0 or 1
DeferGen01Load int32 // defer GEN01 load, 0 or 1
MidiKey int32 // pfield to map midi key no
MidiKeyCps int32 // pfield to map midi key no as cps
MidiKeyOct int32 // pfield to map midi key no as oct
MidiKeyPch int32 // pfield to map midi key no as pch
MidiVelocity int32 // pfield to map midi velocity
MidiVelocityAmp int32 // pfield to map midi velocity as amplitude
NoDefaultPaths int32 // disable relative paths from files, 0 or 1
NumberOfThreads int32 // number of threads for multicore performance
SyntaxCheckOnly int32 // do not compile, only check syntax
CsdLineCounts int32 // csd line error reporting
ComputeWeights int32 // deprecated, kept for backwards comp.
RealtimeMode int32 // use realtime priority mode, 0 or 1
SampleAccurate int32 // use sample-level score event accuracy
SampleRateOverride MYFLT // overriding sample rate
ControlRateOverride MYFLT // overriding control rate
NchnlsOverride int32 // overriding number of out channels
NchnlsIoverride int32 // overriding number of in channels
E0dbfsOverride MYFLT // overriding 0dbfs
Daemon int32 // daemon mode
KsmpsOverride int32 // ksmps override
FFT_library int32 // fft_lib
}
type CsoundAudioDevice struct {
DeviceName string
DeviceId string
RtModule string
MaxNchnls int
IsOutput bool
}
func (dev CsoundAudioDevice) String() string {
return fmt.Sprintf("(%s, %s, %s, %d, %t)", dev.DeviceName, dev.DeviceId,
dev.RtModule, dev.MaxNchnls, dev.IsOutput)
}
type CsoundMidiDevice struct {
DeviceName string
InterfaceName string
DeviceId string
MidiModule string
IsOutput bool
}
func (dev CsoundMidiDevice) String() string {
return fmt.Sprintf("(%s, %s, %s, %s, %t)", dev.DeviceName,
dev.InterfaceName, dev.DeviceId, dev.MidiModule, dev.IsOutput)
}
type TREE struct {
t (*C.TREE)
}
// Constants used by the bus interface (csoundGetChannelPtr() etc.).
const (
CSOUND_CONTROL_CHANNEL = 1
CSOUND_AUDIO_CHANNEL = 2
CSOUND_STRING_CHANNEL = 3
CSOUND_PVS_CHANNEL = 4
CSOUND_VAR_CHANNEL = 5
CSOUND_CHANNEL_TYPE_MASK = 15
CSOUND_INPUT_CHANNEL = 16
CSOUND_OUTPUT_CHANNEL = 32
)
const (
CSOUND_CONTROL_CHANNEL_NO_HINTS = 0
CSOUND_CONTROL_CHANNEL_INT = 1
CSOUND_CONTROL_CHANNEL_LIN = 2
CSOUND_CONTROL_CHANNEL_EXP = 3
)
type ControlChannelHints struct {
Behav int
Dflt MYFLT
Min MYFLT
Max MYFLT
X int
Y int
Width int
Height int
Attributes string // This member must be set explicitly to NULL if not used
}
type ControlChannelInfo struct {
Name string
Type int
Hints ControlChannelHints
}
const (
// message types (only one can be specified)
CSOUNDMSG_DEFAULT = 0x0000 // standard message
CSOUNDMSG_ERROR = 0x1000 // error message (initerror, perferror, etc.)
CSOUNDMSG_ORCH = 0x2000 // orchestra opcodes (e.g. printks)
CSOUNDMSG_REALTIME = 0x3000 // for progress display and heartbeat characters
CSOUNDMSG_WARNING = 0x4000 // warning messages
// format attributes (colors etc., use the bitwise OR of any of these:
CSOUNDMSG_FG_BLACK = 0x0100
CSOUNDMSG_FG_RED = 0x0101
CSOUNDMSG_FG_GREEN = 0x0102
CSOUNDMSG_FG_YELLOW = 0x0103
CSOUNDMSG_FG_BLUE = 0x0104
CSOUNDMSG_FG_MAGENTA = 0x0105
CSOUNDMSG_FG_CYAN = 0x0106
CSOUNDMSG_FG_WHITE = 0x0107
CSOUNDMSG_FG_BOLD = 0x0008
CSOUNDMSG_FG_UNDERLINE = 0x0080
CSOUNDMSG_BG_BLACK = 0x0200
CSOUNDMSG_BG_RED = 0x0210
CSOUNDMSG_BG_GREEN = 0x0220
CSOUNDMSG_BG_ORANGE = 0x0230
CSOUNDMSG_BG_BLUE = 0x0240
CSOUNDMSG_BG_MAGENTA = 0x0250
CSOUNDMSG_BG_CYAN = 0x0260
CSOUNDMSG_BG_GREY = 0x0270
//-------------------------------------------------------------------------
CSOUNDMSG_TYPE_MASK = 0x7000
CSOUNDMSG_FG_COLOR_MASK = 0x0107
CSOUNDMSG_FG_ATTR_MASK = 0x0088
CSOUNDMSG_BG_COLOR_MASK = 0x0270
)
/*
* Instantiation
*/
// Initialize Csound library with specific flags.
// This function is called internally by csoundCreate(), so there is
// generally no need to use it explicitly unless you need to avoid
// default initialization that sets signal handlers and atexit() callbacks.
// Return value is zero on success, positive if initialisation was
// done already, and negative on error.
func Initialize(flags int) int {
return int(C.csoundInitialize(C.int(flags)))
}
// Create an instance of Csound.
// Return an object with methods wrapping calls to the Csound API functions.
// The hostData parameter can be nil, or it can be a pointer to any sort of
// data; this pointer can be accessed from the Csound instance
// that is passed to callback routines.
func Create(hostData unsafe.Pointer) CSOUND {
var cs (*C.CSOUND)
if hostData != nil {
cs = C.csoundCreate(hostData)
} else {
cs = C.csoundCreate(nil)
}
return CSOUND{cs}
}
// Destroy an instance of Csound.
func (csound *CSOUND) Destroy() {
C.csoundDestroy(csound.Cs)
csound.Cs = nil
}
// Return the version number
func (csound CSOUND) Version() string {
n := int(C.csoundGetVersion())
l1, l3 := n/1000, n%1000
l2 := l3 / 10
l3 %= 10
return fmt.Sprintf("%d.%02d.%d", l1, l2, l3)
}
// Return the API version number
func (csound CSOUND) APIVersion() string {
n := int(C.csoundGetAPIVersion())
return fmt.Sprintf("%d.%02d", n/100, n%100)
}
/*
* Performance
*/
// Parse the given orchestra from an ASCII string into a TREE.
// This can be called during performance to parse new code.
func (csound CSOUND) ParseOrc(orc string) TREE {
var cstr *C.char = C.CString(orc)
defer C.free(unsafe.Pointer(cstr))
t := C.csoundParseOrc(csound.Cs, cstr)
return TREE{t}
}
// Compile the given TREE node into structs for Csound to use.
// This can be called during performance to compile a new TREE
func (csound CSOUND) CompileTree(root TREE) int {
result := C.csoundCompileTree(csound.Cs, root.t)
return int(result)
}
// Asynchronous version of CompileTree().
func (csound CSOUND) CompileTreeAsync(root TREE) int {
result := C.csoundCompileTreeAsync(csound.Cs, root.t)
return int(result)
}
// Free the resources associated with the TREE tree.
// This function should be called whenever the TREE was
// created with ParseOrc and memory can be deallocated.
func (csound CSOUND) DeleteTree(tree TREE) {
C.csoundDeleteTree(csound.Cs, tree.t)
}
// Parse, and compile the given orchestra from an ASCII string,
// also evaluating any global space code (i-time only)
// this can be called during performance to compile a new orchestra.
// orc := "instr 1 \n a1 rand 0dbfs/4 \n out a1 \n"
// csound.CompileOrc(orc)
func (csound CSOUND) CompileOrc(orc string) int {
var cstr *C.char = C.CString(orc)
defer C.free(unsafe.Pointer(cstr))
return int(C.csoundCompileOrc(csound.Cs, cstr))
}
// Async version of CompileOrc().
// The code is parsed and compiled, then placed on a queue for
// asynchronous merge into the running engine, and evaluation.
// The function returns following parsing and compilation.
func (csound CSOUND) CompileOrcAsync(orc string) int {
var cstr *C.char = C.CString(orc)
defer C.free(unsafe.Pointer(cstr))
return int(C.csoundCompileOrcAsync(csound.Cs, cstr))
}
// Parse and compile an orchestra given on a string,
// evaluating any global space code (i-time only).
// On SUCCESS it returns a value passed to the
// 'return' opcode in global space.
// code := "i1 = 2 + 2 \n return i1 \n"
// retval := csound.EvalCode(code)
func (csound CSOUND) EvalCode(code string) MYFLT {
var cstr *C.char = C.CString(code)
defer C.free(unsafe.Pointer(cstr))
return MYFLT(C.csoundEvalCode(csound.Cs, cstr))
}
// Prepare an instance of Csound for Cscore
// processing outside of running an orchestra (i.e. "standalone Cscore").
// It is an alternative to PreCompile(), Compile(), and
// Perform*() and should not be used with these functions.
//
// You must call this function before using the interface in "cscore.h"
// when you do not wish to compile an orchestra.
// Pass it the already open *C.FILE pointers to the input and
// output score files.
//
// It returns CSOUND_SUCCESS on success and CSOUND_INITIALIZATION or other
// error code if it fails.
func (csound CSOUND) InitializeCscore(insco, outsco *C.FILE) int {
return int(C.csoundInitializeCscore(csound.Cs, insco, outsco))
}
// Read arguments, parse and compile an orchestra, read, process and
// load a score.
func (csound CSOUND) CompileArgs(args []string) int {
argc := C.int(len(args))
argv := make([]*C.char, argc)
for i, arg := range args {
argv[i] = C.CString(arg)
}
result := C.csoundCompileArgs(csound.Cs, argc, &argv[0])
for _, arg := range argv {
C.free(unsafe.Pointer(arg))
}
return int(result)
}
// Prepares Csound for performance.
// Normally called after compiling a csd file or an orc file, in which
// case score preprocessing is performed and performance terminates
// when the score terminates.
// However, if called before compiling a csd file or an orc file,
// score preprocessing is not performed and "i" statements are dispatched
// as real-time events, the <CsOptions> tag is ignored, and performance
// continues indefinitely or until ended using the API.
// NB: this is called internally by Compile(), therefore
// it is only required if performance is started without
// a call to that function.
func (csound CSOUND) Start() int {
return int(C.csoundStart(csound.Cs))
}
// Compile Csound input files (such as an orchestra and score, or a CSD)
// as directed by the supplied command-line arguments,
// but does not perform them. Return a non-zero error code on failure.
// This function cannot be called during performance, and before a
// repeated call, Reset() needs to be called.
// In this (host-driven) mode, the sequence of calls should be as follows:
// csound.Compile(args)
// for csound.PerformBuffer() == 0 {
// }
// csound.Cleanup()
// csound.Reset()
// Calls Start() internally.
func (csound CSOUND) Compile(args []string) int {
argc := C.int(len(args))
argv := make([]*C.char, argc)
for i, arg := range args {
argv[i] = C.CString(arg)
}
result := C.csoundCompile(csound.Cs, argc, &argv[0])
for _, arg := range argv {
C.free(unsafe.Pointer(arg))
}
return int(result)
}
// Compile a Csound input file (.csd file)
// which includes command-line arguments,
// but does not perform the file. Return a non-zero error code on failure.
// In this (host-driven) mode, the sequence of calls should be as follows:
// csound.CompileCsd(fileName)
// for csound.PerformBuffer() == 0 {
// }
// csound.Cleanup()
// csound.Reset()
// NB: this function can be called during performance to
// replace or add new instruments and events.
// On a first call and if called before csound.Start(), this function
// behaves similarly to csound.Compile()
func (csound CSOUND) CompileCsd(fileName string) int {
var cfileName *C.char = C.CString(fileName)
defer C.free(unsafe.Pointer(cfileName))
return int(C.csoundCompileCsd(csound.Cs, cfileName))
}
// Compile a Csound input file contained in a string of text,
// which includes command-line arguments, orchestra, score, etc.,
// but does not perform the file. Returns a non-zero error code on failure.
// In this (host-driven) mode, the sequence of calls should be as follows:
// csound.CompileCsdText(csound, csdText)
// for csound.PerformBuffer() == 0 {
// }
// csound.Cleanup()
// csound.Reset()
// NB: A temporary file is created, the csd_text is written to the temporary
// file, and csound.CompileCsd is called with the name of the temporary file,
// which is deleted after compilation. Behavior may vary by platform.
func (csound CSOUND) CompileCsdText(csdText string) int {
var cCsdText *C.char = C.CString(csdText)
defer C.free(unsafe.Pointer(cCsdText))
return int(C.csoundCompileCsdText(csound.Cs, cCsdText))
}
// Senses input events and performs audio output until the end of score
// is reached (positive return value), an error occurs (negative return
// value), or performance is stopped by calling Stop() from another
// thread (zero return value).
//
// Note that Compile() or CompileOrc(), ReadScore(),
// Start() must be called first.
//
// In the case of zero return value, Perform() can be called again
// to continue the stopped performance. Otherwise, Reset() should be
// called to clean up after the finished or failed performance.
func (csound CSOUND) Perform() int {
return int(C.csoundPerform(csound.Cs))
}
// Senses input events, and performs one control sample worth (ksmps) of
// audio output.
//
// Note that Compile() or CompileOrc(), ReadScore(),
// Start() must be called first.
//
// Return false during performance, and true when performance is finished.
// If called until it returns true, will perform an entire score.
// Enables external software to control the execution of Csound,
// and to synchronize performance with audio input and output.
func (csound CSOUND) PerformKsmps() int {
return int(C.csoundPerformKsmps(csound.Cs))
}
// Performs Csound, sensing real-time and score events
// and processing one buffer's worth (-b frames) of interleaved audio.
// Return a pointer to the new output audio in 'outputAudio'
//
// Note that Compile must be called first, then call
// OutputBuffer() and InputBuffer() to get the pointer
// to csound's I/O buffers.
//
// Return false during performance, and true when performance is finished.
func (csound CSOUND) PerformBuffer() int {
return int(C.csoundPerformBuffer(csound.Cs))
}
// Stops a Perform() running in another thread. Note that it is
// not guaranteed that Perform() has already stopped when this
// function returns.
func (csound CSOUND) Stop() {
C.csoundStop(csound.Cs)
}
// Prints information about the end of a performance, and closes audio
// and MIDI devices.
//
// Note: after calling Cleanup(), the operation of the perform
// functions is undefined.
func (csound CSOUND) Cleanup() int {
numSenseEvent = 0
return int(C.csoundCleanup(csound.Cs))
}
// Resets all internal memory and state in preparation for a new performance.
// Enables external software to run successive Csound performances
// without reloading Csound. Implies Cleanup(), unless already called.
func (csound CSOUND) Reset() {
C.csoundReset(csound.Cs)
}
/*
* Attributes
*/
// Return the number of audio sample frames per second.
func (csound CSOUND) Sr() MYFLT {
return MYFLT(C.csoundGetSr(csound.Cs))
}
// Return the number of control samples per second.
func (csound CSOUND) Kr() MYFLT {
return MYFLT(C.csoundGetKr(csound.Cs))
}
// Return the number of audio sample frames per control sample.
func (csound CSOUND) Ksmps() int {
return int(C.csoundGetKsmps(csound.Cs))
}
// Return the number of audio output channels. Set through the nchnls
// header variable in the csd file.
func (csound CSOUND) Nchnls() int {
return int(C.csoundGetNchnls(csound.Cs))
}
// Return the number of audio input channels. Set through the
// nchnls_i header variable in the csd file. If this variable is
// not set, the value is taken from nchnls.
func (csound CSOUND) NchnlsInput() int {
return int(C.csoundGetNchnlsInput(csound.Cs))
}
// Return the 0dBFS level of the spin/spout buffers.
func (csound CSOUND) Get0dBFS() MYFLT {
return MYFLT(C.csoundGet0dBFS(csound.Cs))
}
// Return the A4 frequency reference.
func (csound CSOUND) A4() MYFLT {
return MYFLT(C.csoundGetA4(csound.Cs))
}
// Return the current performance time in samples.
func (csound CSOUND) CurrentTimeSamples() int {
return int(C.csoundGetCurrentTimeSamples(csound.Cs))
}
// Return the size of MYFLT in bytes.
func (csound CSOUND) SizeOfMYFLT() int {
return int(C.csoundGetSizeOfMYFLT())
}
// Return host data.
func (csound CSOUND) HostData() unsafe.Pointer {
return C.csoundGetHostData(csound.Cs)
}
// Set host data.
func (csound CSOUND) SetHostData(hostData unsafe.Pointer) {
C.csoundSetHostData(csound.Cs, hostData)
}
// Set a single csound option (flag). Return CSOUND_SUCCESS on success.
// NB: blank spaces are not allowed
func (csound CSOUND) SetOption(option string) int {
var coption *C.char = C.CString(option)
defer C.free(unsafe.Pointer(coption))
return int(C.csoundSetOption(csound.Cs, coption))
}
// Configure Csound with a given set of parameters defined in
// the CsoundParams structure. These parameters are the part of the
// OPARMS struct that are configurable through command line flags.
// The CsoundParams structure can be obtained using Params().
// These options should only be changed before performance has started.
func (csound CSOUND) SetParams(p *CsoundParams) {
pp := &p.DebugMode
C.csoundSetParams(csound.Cs, (*C.CSOUND_PARAMS)(unsafe.Pointer(pp)))
}
// Get the current set of parameters from a CSOUND instance in
// a CsoundParams structure. See SetParams().
func (csound CSOUND) Params(p *CsoundParams) {
pp := &p.DebugMode
C.csoundGetParams(csound.Cs, (*C.CSOUND_PARAMS)(unsafe.Pointer(pp)))
}
// Return whether Csound is set to print debug messages sent through the
// DebugMsg() internal API function.
func (csound CSOUND) Debug() bool {
return C.csoundGetDebug(csound.Cs) != 0
}
// Set whether Csound prints debug messages from the DebugMsg() internal
// API function.
func (csound CSOUND) SetDebug(debug bool) {
C.csoundSetDebug(csound.Cs, cbool(debug))
}
/*
* General Input/Output
*/
// Return the audio output name (-o).
func (csound CSOUND) OutputName() string {
return C.GoString(C.csoundGetOutputName(csound.Cs))
}
// Set output destination, type and format
// type can be one of "wav","aiff", "au","raw", "paf", "svx", "nist", "voc",
// "ircam","w64","mat4", "mat5", "pvf","xi", "htk","sds","avr","wavex","sd2",
// "flac", "caf","wve","ogg","mpc2k","rf64", or nil (use default or
// realtime IO).
// format can be one of "alaw", "schar", "uchar", "float", "double", "long",
// "short", "ulaw", "24bit", "vorbis", or nil (use default or realtime IO).
// For RT audio, use DeviceId from CsoundAudioDevice for a given audio device.
func (csound CSOUND) SetOutput(name, otype, format string) {
var cname, ctype, cformat *C.char
cname = C.CString(name)
defer C.free(unsafe.Pointer(cname))
if len(otype) > 0 {
ctype = C.CString(otype)
defer C.free(unsafe.Pointer(ctype))
} else {
ctype = nil
}
if len(format) > 0 {
cformat = C.CString(format)
defer C.free(unsafe.Pointer(cformat))
} else {
cformat = nil
}
C.csoundSetOutput(csound.Cs, cname, ctype, cformat)
}
// Get output type and format.
func (csound CSOUND) OutputFormat() (otype, format string) {
type_ := make([]byte, 6)
format_ := make([]byte, 8)
ctype := (*C.char)(unsafe.Pointer(&type_[0]))
cformat := (*C.char)(unsafe.Pointer(&format_[0]))
C.csoundGetOutputFormat(csound.Cs, ctype, cformat)
otype = C.GoString(ctype)
format = C.GoString(cformat)
return
}
// Set input source.
func (csound CSOUND) SetInput(name string) {
var cname *C.char = C.CString(name)
defer C.free(unsafe.Pointer(cname))
C.csoundSetInput(csound.Cs, cname)
}
// Set MIDI input device name/number.
func (csound CSOUND) SetMIDIInput(name string) {
var cname *C.char = C.CString(name)
defer C.free(unsafe.Pointer(cname))
C.csoundSetMIDIInput(csound.Cs, cname)
}
// Set MIDI file input name.
func (csound CSOUND) SetMIDIFileInput(name string) {
var cname *C.char = C.CString(name)
defer C.free(unsafe.Pointer(cname))
C.csoundSetMIDIFileInput(csound.Cs, cname)
}
// Set MIDI output device name/number.
func (csound CSOUND) SetMIDIOutput(name string) {
var cname *C.char = C.CString(name)
defer C.free(unsafe.Pointer(cname))
C.csoundSetMIDIOutput(csound.Cs, cname)
}
// Set MIDI file output name.
func (csound CSOUND) SetMIDIFileOutput(name string) {
var cname *C.char = C.CString(name)
defer C.free(unsafe.Pointer(cname))
C.csoundSetMIDIFileOutput(csound.Cs, cname)
}
/*
* Realtime Audio I/O
*/
// Set the current RT audio module.
func (csound CSOUND) SetRTAudioModule(module string) {
var cmodule *C.char = C.CString(module)
defer C.free(unsafe.Pointer(cmodule))
C.csoundSetRTAudioModule(csound.Cs, cmodule)
}
// Retrieve a module name and type ("audio" or "midi") given a
// number. Modules are added to list as csound loads them. Return
// CSOUND_SUCCESS on success and CSOUND_ERROR if module number
// was not found
//
// var name, mtype string
// err := CSOUND_SUCCESS
// n := 0;
// for err != CSOUND_ERROR {
// name, mtype, err = csound.Module(n++)
// fmt.Printf("Module %d: %s (%s)\n", n, name, mtype)
// }
func (csound CSOUND) Module(number int) (name, mtype string, error int) {
var cname, ctype *C.char
cerror := C.csoundGetModule(csound.Cs, C.int(number), &cname, &ctype)
name = C.GoString(cname)
mtype = C.GoString(ctype)
error = int(cerror)
return
}
// Return the number of samples in Csound input buffer.
func (csound CSOUND) InputBufferSize() int {
return int(C.csoundGetInputBufferSize(csound.Cs))
}
// Return the number of samples in Csound output buffer.
func (csound CSOUND) OutputBufferSize() int {
return int(C.csoundGetOutputBufferSize(csound.Cs))
}
// Return the Csound audio input buffer as a []MYFLT.
// Enable external software to write audio into Csound before calling
// PerformBuffer.
func (csound CSOUND) InputBuffer() []MYFLT {
buffer := (*MYFLT)(C.csoundGetInputBuffer(csound.Cs))
length := int(C.csoundGetInputBufferSize(csound.Cs))
var slice []MYFLT
sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&slice))
sliceHeader.Cap = length
sliceHeader.Len = length
sliceHeader.Data = uintptr(unsafe.Pointer(buffer))
return slice
}
// Return the Csound audio output buffer as a []MYFLT.
// Enable external software to read audio from Csound after calling
// PerformBuffer.
func (csound CSOUND) OutputBuffer() []MYFLT {
buffer := (*MYFLT)(C.csoundGetOutputBuffer(csound.Cs))
length := int(C.csoundGetOutputBufferSize(csound.Cs))
var slice []MYFLT
sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&slice))
sliceHeader.Cap = length
sliceHeader.Len = length
sliceHeader.Data = uintptr(unsafe.Pointer(buffer))
return slice
}
// Return the Csound audio input working buffer (spin) as a []MYFLT.