1 
2 #include "filebuilding.h"
3 #include <stdlib.h>
4 #include <string>
5 #include <vector>
6 #include <map>
7 
8 #ifdef MACOSX
9 #define gcvt(val,dig,buf) snprintf(buf,dig,"%f",val)
10 #endif
11 
12 struct CsoundFile_
13 {
14   std::string options;
15   std::string orchestra;
16   std::vector<std::string> score;
17 };
18 
19 static std::map<CSOUND *, CsoundFile_> files;
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
perfthread(void * data)24   uintptr_t perfthread(void *data){
25     CSOUND *cs = (CSOUND *)data;
26     int res = 0;
27     while(res == 0) res = csoundPerformKsmps(cs);
28     return 0;
29   }
30 
csoundNewCSD(char * path)31   PUBLIC void csoundNewCSD(char *path) {
32    char *argv[2];
33    CSOUND *instance;
34    argv[0] = (char *)malloc(7);
35    argv[1] = (char *)malloc(strlen(path)+1);
36    strcpy(argv[0], "csound");
37    strcpy(argv[1], path);
38 
39    //argv[0] = "csound";
40    //argv[1] = path;
41 
42    printf("%s \n", argv[1]);
43    instance = csoundCreate(NULL);
44    csoundCompile(instance,2,(const char**)argv);
45    perfthread((void *) instance);
46    csoundReset(instance);
47    // csoundDestroy(instance);
48    free(argv[0]);
49    free(argv[1]);
50 
51   }
52 
53 
csoundPerformLoop(CSOUND * cs)54   PUBLIC int csoundPerformLoop(CSOUND *cs){
55     csoundCreateThread(perfthread, (void *)cs);
56     return 1;
57   }
58 
csoundCsdCreate(CSOUND * csound)59   PUBLIC void csoundCsdCreate(CSOUND *csound)
60   {
61     CsoundFile_ csoundFile;
62     files[csound] = csoundFile;
63   }
64 
csoundCsdSetOptions(CSOUND * csound,char * options)65   PUBLIC void csoundCsdSetOptions(CSOUND *csound, char *options)
66   {
67     files[csound].options = options;
68   }
69 
csoundCsdGetOptions(CSOUND * csound)70   PUBLIC const char* csoundCsdGetOptions(CSOUND *csound)
71   {
72     return files[csound].options.c_str();
73   }
74 
csoundCsdSetOrchestra(CSOUND * csound,char * orchestra)75   PUBLIC void csoundCsdSetOrchestra(CSOUND *csound, char *orchestra)
76   {
77     files[csound].orchestra = orchestra;
78   }
79 
csoundCsdGetOrchestra(CSOUND * csound)80   PUBLIC const char* csoundCsdGetOrchestra(CSOUND *csound)
81   {
82     return files[csound].orchestra.c_str();
83   }
84 
csoundCsdAddScoreLine(CSOUND * csound,char * line)85   PUBLIC void csoundCsdAddScoreLine(CSOUND *csound, char *line)
86   {
87     files[csound].score.push_back(line);
88   }
89 
csoundCsdAddEvent11(CSOUND * csound,double p1,double p2,double p3,double p4,double p5,double p6,double p7,double p8,double p9,double p10,double p11)90   PUBLIC void csoundCsdAddEvent11(CSOUND *csound, double p1, double p2, double p3,
91                                   double p4, double p5, double p6, double p7,
92                                   double p8, double p9, double p10, double p11)
93   {
94     char note[0x100];
95     sprintf(note, "i %-.10g %-.10g %-.10g %-.10g %-.10g %-.10g %-.10g %-.10g "
96             "%-.10g %-.10g %-.10g", p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11);
97     files[csound].score.push_back(note);
98   }
99 
csoundCsdAddEvent10(CSOUND * csound,double p1,double p2,double p3,double p4,double p5,double p6,double p7,double p8,double p9,double p10)100   PUBLIC void csoundCsdAddEvent10(CSOUND *csound, double p1, double p2, double p3,
101                                   double p4, double p5, double p6, double p7,
102                                   double p8, double p9, double p10)
103   {
104     char note[0x100];
105     sprintf(note, "i %-.10g %-.10g %-.10g %-.10g %-.10g %-.10g %-.10g %-.10g "
106             "%-.10g %-.10g", p1, p2, p3, p4, p5, p6, p7, p8, p9, p10);
107     files[csound].score.push_back(note);
108   }
109 
csoundCsdAddEvent9(CSOUND * csound,double p1,double p2,double p3,double p4,double p5,double p6,double p7,double p8,double p9)110   PUBLIC void csoundCsdAddEvent9(CSOUND *csound, double p1, double p2, double p3,
111                                  double p4, double p5, double p6, double p7,
112                                  double p8, double p9)
113   {
114     char note[0x100];
115     sprintf(note, "i %-.10g %-.10g %-.10g %-.10g %-.10g %-.10g %-.10g %-.10g "
116             "%-.10g", p1, p2, p3, p4, p5, p6, p7, p8, p9);
117     files[csound].score.push_back(note);
118   }
119 
csoundCsdAddEvent8(CSOUND * csound,double p1,double p2,double p3,double p4,double p5,double p6,double p7,double p8)120   PUBLIC void csoundCsdAddEvent8(CSOUND *csound, double p1, double p2, double p3,
121                                  double p4, double p5, double p6, double p7,
122                                  double p8)
123   {
124     char note[0x100];
125     sprintf(note, "i %-.10g %-.10g %-.10g %-.10g %-.10g %-.10g %-.10g %-.10g",
126             p1, p2, p3, p4, p5, p6, p7, p8);
127     files[csound].score.push_back(note);
128   }
csoundCsdAddEvent7(CSOUND * csound,double p1,double p2,double p3,double p4,double p5,double p6,double p7)129   PUBLIC void csoundCsdAddEvent7(CSOUND *csound, double p1, double p2, double p3,
130                                  double p4, double p5, double p6, double p7)
131   {
132     char note[0x100];
133     sprintf(note, "i %-.10g %-.10g %-.10g %-.10g %-.10g %-.10g %-.10g",
134             p1, p2, p3, p4, p5, p6, p7);
135     files[csound].score.push_back(note);
136   }
137 
csoundCsdAddEvent6(CSOUND * csound,double p1,double p2,double p3,double p4,double p5,double p6)138   PUBLIC void csoundCsdAddEvent6(CSOUND *csound, double p1, double p2, double p3,
139                                  double p4, double p5, double p6)
140   {
141     char note[0x100];
142     sprintf(note, "i %-.10g %-.10g %-.10g %-.10g %-.10g %-.10g",
143             p1, p2, p3, p4, p5, p6);
144     files[csound].score.push_back(note);
145   }
146 
csoundCsdAddEvent5(CSOUND * csound,double p1,double p2,double p3,double p4,double p5)147   PUBLIC void csoundCsdAddEvent5(CSOUND *csound, double p1, double p2, double p3,
148                                  double p4, double p5)
149   {
150     char note[0x100];
151     sprintf(note, "i %-.10g %-.10g %-.10g %-.10g %-.10g", p1, p2, p3, p4, p5);
152     files[csound].score.push_back(note);
153   }
154 
csoundCsdAddEvent4(CSOUND * csound,double p1,double p2,double p3,double p4)155   PUBLIC void csoundCsdAddEvent4(CSOUND *csound, double p1, double p2, double p3,
156                                  double p4)
157   {
158     char note[0x100];
159     sprintf(note, "i %-.10g %-.10g %-.10g %-.10g", p1, p2, p3, p4);
160     files[csound].score.push_back(note);
161   }
162 
csoundCsdAddEvent3(CSOUND * csound,double p1,double p2,double p3)163   PUBLIC void csoundCsdAddEvent3(CSOUND *csound, double p1, double p2, double p3)
164   {
165     char note[0x100];
166     sprintf(note, "i %-.10g %-.10g %-.10g", p1, p2, p3);
167     files[csound].score.push_back(note);
168   }
169 
csoundCsdSave(CSOUND * csound,char * filename)170   PUBLIC int csoundCsdSave(CSOUND *csound, char *filename)
171   {
172     CsoundFile_ &csoundFile = files[csound];
173     FILE *file = fopen(filename, "w+");
174     fprintf(file, "<CsoundSynthesizer>");
175     fprintf(file, "<CsOptions>");
176     fprintf(file, "%s", csoundFile.options.c_str());
177     fprintf(file, "<CsoundSynthesizer>");
178     fprintf(file, "<CsInstruments>");
179     fprintf(file, "%s", csoundFile.orchestra.c_str());
180     fprintf(file, "</CsInstruments>");
181     fprintf(file, "<CsScore>");
182     for (std::vector<std::string>::iterator it = csoundFile.score.begin();
183          it != csoundFile.score.end(); ++it) {
184       fprintf(file, "%s", (it->c_str()));
185     }
186     fprintf(file, "</CsScore>");
187     fprintf(file, "</CsoundSynthesizer>");
188     return fclose(file);
189   }
190 
csoundCsdCompile(CSOUND * csound,char * filename)191   PUBLIC int csoundCsdCompile(CSOUND *csound, char *filename)
192   {
193     csoundCsdSave(csound, filename);
194     return csoundCompileCsd(csound, filename);
195   }
196 
csoundCsdPerform(CSOUND * csound,char * filename)197   PUBLIC int csoundCsdPerform(CSOUND *csound, char *filename)
198   {
199     csoundCsdSave(csound, filename);
200     return csoundPerformCsd(csound, filename);
201   }
202 
203   /* VL: a new version of this function has been added to the main
204      Csound library.
205   PUBLIC int csoundCompileCsd(CSOUND *csound, char *csdFilename)
206   {
207     char *argv[2];
208     argv[0] = (char*)"csound";
209     argv[1] = csdFilename;
210     return csoundCompile(csound, 2, argv);
211   }
212   */
213 
csoundPerformCsd(CSOUND * csound,char * csdFilename)214   PUBLIC int csoundPerformCsd(CSOUND *csound, char *csdFilename)
215   {
216     int retval = csoundCompileCsd(csound, csdFilename);
217     if (!retval)
218       retval = csoundPerform(csound);
219     csoundCleanup(csound);
220     return (retval >= 0 ? 0 : retval);
221   }
222 
223 #ifdef __cplusplus
224 }
225 #endif
226 
227