Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Util/Clooper/SoundClient.cpp
blob: 9e39e6de8a21730b2baa114608906f3850e43cdd (plain)
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
#include <pthread.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>

#include <csound/csound.hpp>
#include "SoundClient.h"
#include <vector>

struct ev_t
{
    int onset;
    char type;
    std::vector<MYFLT> param;

    ev_t(char type, MYFLT p1, MYFLT p2, MYFLT p3, MYFLT p4, MYFLT p5, MYFLT p6, MYFLT p7, MYFLT p8, MYFLT p9, MYFLT p10, MYFLT p11, MYFLT p12, MYFLT p13, MYFLT p14, MYFLT p15)
        : onset(onset), type(type), param(15)
    {
        onset = (int) p2;
        param[0] = p1;
        param[1] = 0.0;
        param[2] = p3;
        param[3] = p4;
        param[4] = p5;
        param[5] = p6;
        param[6] = p7;
        param[7] = p8;
        param[8] = p9;
        param[9] = p10;
        param[10] = p11;
        param[11] = p12;
        param[12] = p13;
        param[13] = p14;
        param[14] = p15;
    }
    bool operator<(const ev_t &e) const
    {
        return onset < e.onset;
    }
    void print(FILE *f)
    {
        fprintf(f, "INFO: scoreEvent %c ", type);
        for (size_t i = 0; i < param.size(); ++i) fprintf(f, "%lf ", param[i]);
        fprintf(f, "\n");
    }
};
struct EvLoop
{
    int tick_prev;
    int tickMax;
    double rtick;
    double ticks_per_ksmp;
    size_t pos;
    std::vector<ev_t *> ev;
    CSOUND * csound;
    void * mutex;

    EvLoop(CSOUND * cs) : tick_prev(0), tickMax(1), rtick(0.0), ticks_per_ksmp(0.3333), pos(0), csound(cs), mutex(NULL)
    {
        mutex = csoundCreateMutex(0);
    }
    ~EvLoop()
    {
        csoundLockMutex(mutex);
        for (size_t i = 0; i < ev.size(); ++i)
        {
            delete ev[i];
        }
        csoundUnlockMutex(mutex);
        csoundDestroyMutex(mutex);
    }
    void clear()
    {
        csoundLockMutex(mutex);
        for (size_t i = 0; i < ev.size(); ++i)
        {
            delete ev[i];
        }
        ev.clear();
        csoundUnlockMutex(mutex);
        pos = 0;
    }
    int getTick()
    {
        return (int)rtick % tickMax;
    }
    void setNumTicks(int nticks)
    {
        tickMax = nticks;
        if ((int)rtick > nticks)
        {
            int t = (int)rtick % nticks;
            rtick = t;
        }
    }
    void setTick(int t)
    {
        t = t % tickMax;
        rtick = (double)(t % tickMax);
        //TODO: binary search would be faster
        csoundLockMutex(mutex);
        for (pos = 0; pos < ev.size() && ev[pos]->onset < t; ++pos) ;
        if (ev.size() == pos) pos = 0;
        csoundUnlockMutex(mutex);
    }
    void setTickDuration(double d)
    {
        ticks_per_ksmp = d / csoundGetKr(csound);
    }
    void step(FILE * f)
    {
        if (ev.empty()) return;

        csoundLockMutex(mutex);
        rtick += ticks_per_ksmp;
        int tick = (int)rtick % tickMax;
        if (tick < tick_prev)
        {
            while (pos < ev.size())
            {
                if (f) ev[pos]->print(f);
                csoundScoreEvent(csound, ev[pos]->type, &ev[pos]->param[0], ev[pos]->param.size());
                ++pos;
            }
            pos = 0;
        }
        while ((pos < ev.size()) && (tick >= ev[ pos ]->onset))
        {
            if (f) ev[pos]->print(f);
            csoundScoreEvent(csound, ev[pos]->type, &ev[pos]->param[0], ev[pos]->param.size());
            ++pos;
        }
        csoundUnlockMutex(mutex);
        tick_prev = tick;
    }
    void addEvent(ev_t *e)
    {
        csoundLockMutex(mutex);
        ev.push_back(e);
        csoundUnlockMutex(mutex);
    }
};
struct TamTamSound
{
    void * ThreadID;
    CSOUND * csound;
    char * csound_orc;
    enum {CONTINUE, STOP} PERF_STATUS;
    int verbosity;
    FILE * _debug;
    int thread_playloop;
    int thread_measurelag;
    EvLoop * loop;

    TamTamSound(char * orc)
        : ThreadID(NULL), csound(NULL), PERF_STATUS(STOP), verbosity(3), _debug(NULL), thread_playloop(0), thread_measurelag(0), loop(NULL)
    {
        _debug = fopen("debug.log", "w");

        csound = csoundCreate(NULL);
        csound_orc = strdup(orc);

        loop = new EvLoop(csound);
    }
    ~TamTamSound()
    {
        if (csound)
        {
            stop();
            delete loop;
            csoundDestroy(csound);
        }
        free(csound_orc);
        fclose(_debug);
    }
    static double pytime(const struct timeval * tv)
    {
        return (double) tv->tv_sec + (double) tv->tv_usec / 1000000.0;
    }
    uintptr_t thread_fn()
    {
        struct timeval tv;
        double t_prev;
        double m = 0.0;

        int loops = 0;

        while ( (csoundPerformKsmps(csound) == 0) 
                && (PERF_STATUS == CONTINUE))
        {
            if (thread_measurelag)
            {
                gettimeofday(&tv, 0);
                double t_this = pytime(&tv);
                if (loops)
                {
                    if (m < t_this - t_prev)
                    {
                        m = t_this - t_prev;
                        fprintf(_debug, "maximum lag %lf\n", m);
                    }
                }
                t_prev = t_this;
            }
            if (thread_playloop)
            {
                loop->step(_debug);
            }
            ++loops;
        }
        return 0;
    }
    static uintptr_t csThread(void *clientData)
    {
        return ((TamTamSound*)clientData)->thread_fn();
    }
    void start()
    {
        if (!ThreadID)
        {
            int argc=3;
            char  **argv = (char**)malloc(argc*sizeof(char*));
            argv[0] = "csound";
            argv[1] ="-m0";
            argv[2] = csound_orc;
            fprintf(_debug, "loading file %s\n", csound_orc);

            csoundInitialize(&argc, &argv, 0);
            int result = csoundCompile(csound, argc, &(argv[0]));
            free(argv);

            if (!result)
            {
                PERF_STATUS = CONTINUE;
                ThreadID = csoundCreateThread(csThread, (void*)this);
            }
            else
            {
                fprintf(_debug, "ERROR: failed to compile orchestra\n");
                ThreadID =  NULL;
            }
        }
    }
    void stop()
    {
        if (ThreadID)
        {
            PERF_STATUS = STOP;
            if (verbosity > 0) fprintf(_debug, "INFO: stop()");
            uintptr_t rval = csoundJoinThread(ThreadID);
            if (rval) fprintf(_debug, "WARNING: thread returned %zu\n", rval);
            ThreadID = NULL;
            csoundReset(csound);
        }
    }

    void scoreEvent(char type, MYFLT * p, int np)
    {
        if (!csound)
        {
            fprintf(_debug, "ERROR: TamTamSound::%s() csound not loaded\n", __FUNCTION__);
            return;
        }
        if (verbosity > 2)
        {
            fprintf(_debug, "INFO: scoreEvent %c ", type);
            for (int i = 0; i < np; ++i) fprintf(_debug, "%lf ", p[i]);
            fprintf(_debug, "\n");
        }
        csoundScoreEvent(csound, type, p, np);
    }
    void scoreEvent(char type, MYFLT p1, MYFLT p2, MYFLT p3, MYFLT p4, MYFLT p5, MYFLT p6, MYFLT p7, MYFLT p8, MYFLT p9, MYFLT p10, MYFLT p11, MYFLT p12, MYFLT p13, MYFLT p14, MYFLT p15)
    {
        MYFLT p[15];
        p[0] = p1;
        p[1] = p2;
        p[2] = p3;
        p[3] = p4;
        p[4] = p5;
        p[5] = p6;
        p[6] = p7;
        p[7] = p8;
        p[8] = p9;
        p[9] = p10;
        p[10] = p11;
        p[11] = p12;
        p[12] = p13;
        p[13] = p14;
        p[14] = p15;
        scoreEvent(type, p, 15);
    }
    void inputMessage(const char * msg)
    {
        if (!csound)
        {
            fprintf(_debug, "ERROR: TamTamSound::%s() csound not loaded\n", __FUNCTION__);
            return;
        }
        if (verbosity > 2) fprintf(_debug, "%s\n", msg);
        csoundInputMessage(csound, msg);
    }
    bool good()
    {
        return csound != NULL;
    }


    void instrumentLoad(int table, const char * fname)
    {
        char str[512];
        sprintf(str, "f%d 0 0 -1 \"%s\" 0 0 0", table, fname);
        inputMessage(str);
    }
    void instrumentUnloadBatch(int count)
    {
        MYFLT p[4] = {5000.0, 0.0, 0.1, 0.0};
        p[3] = (MYFLT) count;
        scoreEvent('i', p, 4);
    }
    void micRecord(int table)
    {
        MYFLT p[4] = {5201.0, 0.0, 5.0, 0.0};
        p[3] = (MYFLT) table;
        scoreEvent('i', p, 4);
    }
    void setMasterVolume(MYFLT vol)
    {
        MYFLT *p;
        if (!(csoundGetChannelPtr(csound, &p, "masterVolume", CSOUND_CONTROL_CHANNEL | CSOUND_INPUT_CHANNEL)))
            *p = (MYFLT) vol;
        else
        {
            fprintf(_debug, "ERROR: failed to set master volume\n");
        }
    }

};

TamTamSound * sc_tt = NULL;

int sc_initialize(char * csd)
{
    sc_tt = new TamTamSound(csd);
    if (sc_tt->good()) return 0;
    else return -1;
}
void sc_destroy()
{
    delete sc_tt;
    sc_tt = NULL;
}
void sc_instrumentLoad(int table, const char * fname)
{
    sc_tt->instrumentLoad(table, fname);
}
void sc_instrumentUnloadBatch(int count)
{
    sc_tt->instrumentUnloadBatch(count);
}
void sc_micRecord(int table)
{
    sc_tt->micRecord(table);
}
void sc_setMasterVolume(double v)
{
    sc_tt->setMasterVolume(v);
}
void sc_scoreEvent15(char type, MYFLT p1, MYFLT p2, MYFLT p3, MYFLT p4, MYFLT p5, MYFLT p6, MYFLT p7, MYFLT p8, MYFLT p9, MYFLT p10, MYFLT p11, MYFLT p12, MYFLT p13, MYFLT p14, MYFLT p15)
{
    sc_tt->scoreEvent(type, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15);
}
void sc_start()
{
    sc_tt->start();
}
void sc_stop()
{
    sc_tt->stop();
}

int sc_loop_getTick()
{
    return sc_tt->loop->getTick();
}
void sc_loop_setNumTicks(int nticks)
{
    sc_tt->loop->setNumTicks(nticks);
}
void sc_loop_setTick(int ctick)
{
    sc_tt->loop->setTick(ctick);
}
void sc_loop_setTickDuration(double secs_per_tick)
{
    sc_tt->loop->setTickDuration(secs_per_tick);
}
void sc_loop_addScoreEvent15(char type, MYFLT p1, MYFLT p2, MYFLT p3, MYFLT p4, MYFLT p5, MYFLT p6, MYFLT p7, MYFLT p8, MYFLT p9, MYFLT p10, MYFLT p11, MYFLT p12, MYFLT p13, MYFLT p14, MYFLT p15)
{
    sc_tt->loop->addEvent( new ev_t(type, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15));
}
void sc_loop_clear()
{
    sc_tt->loop->clear();
}
void sc_loop_playing(int tf)
{
    sc_tt->thread_playloop = tf;
}