Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Util/Clooper/SoundClient.cpp
blob: 116e8c6ab7aa3ccc00a11d62df2f69743d92640f (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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#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>
#include <map>

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");
    }

    void event(CSOUND * csound)
    {
        csoundScoreEvent(csound, type, &param[0], param.size());
    }
};
struct EvLoop
{
    int tick_prev;
    int tickMax;
    double rtick;
    double ticks_per_ksmp;
    typedef std::pair<int, ev_t *> pair_t;
    typedef std::multimap<int, ev_t *>::iterator iter_t;
    std::multimap<int, ev_t *> ev;
    std::multimap<int, ev_t *>::iterator ev_pos;
    CSOUND * csound;
    void * mutex;

    EvLoop(CSOUND * cs) : tick_prev(0), tickMax(1), rtick(0.0), ticks_per_ksmp(0.3333), ev_pos(ev.end()), csound(cs), mutex(NULL)
    {
        mutex = csoundCreateMutex(0);
    }
    ~EvLoop()
    {
        csoundLockMutex(mutex);
        for (iter_t i = ev.begin(); i != ev.end(); ++i)
        {
            delete i->second;
        }
        csoundUnlockMutex(mutex);
        csoundDestroyMutex(mutex);
    }
    void clear()
    {
        csoundLockMutex(mutex);
        for (iter_t i = ev.begin(); i != ev.end(); ++i)
        {
            delete i->second;
        }
        ev.erase(ev.begin(), ev.end());
        ev_pos = ev.end();
        csoundUnlockMutex(mutex);
    }
    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);
        ev_pos = ev.lower_bound( t );
        csoundUnlockMutex(mutex);
    }
    void setTickDuration(double d)
    {
        ticks_per_ksmp = 1.0 / (d * csoundGetKr(csound));
        if (0) fprintf(stderr, "INFO: duration %lf -> ticks_pr_skmp %lf\n", d, ticks_per_ksmp);
    }
    void step(FILE * f)
    {
        rtick += ticks_per_ksmp;
        int tick = (int)rtick % tickMax;
        if (tick == tick_prev) return;

        csoundLockMutex(mutex);
        int events = 0;
        int loop0 = 0;
        int loop1 = 0;
        const int eventMax = 6;  //NOTE: events beyond this number will be ignored!!!
        if (!ev.empty()) 
        {
            if (tick < tick_prev) // should be true only after the loop wraps (not after insert)
            {
                while (ev_pos != ev.end())
                {
                    if (f) ev_pos->second->print(f);
                    if (events < eventMax) ev_pos->second->event(csound);
                    ++ev_pos;
                    ++events;
                    ++loop0;
                }
                ev_pos = ev.begin();
            }
            while ((ev_pos != ev.end()) && (tick >= ev_pos->first))
            {
                if (f) ev_pos->second->print(f);
                if (events < eventMax) ev_pos->second->event(csound);
                ++ev_pos;
                ++events;
                ++loop1;
            }
        }
        csoundUnlockMutex(mutex);
        tick_prev = tick;
        if (events >= eventMax) fprintf(stderr, "WARNING: %i/%i events at once (%i, %i)\n", events,ev.size(),loop0,loop1);
    }
    void addEvent(ev_t *e)
    {
        csoundLockMutex(mutex);
        ev.insert(pair_t(e->onset, e));
        ev_pos = ev.upper_bound( tick_prev );
        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 = 0.0; //value will be ignored
        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();
    }
    int 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);
                return 0;
            }
            else
            {
                fprintf(_debug, "ERROR: failed to compile orchestra\n");
                ThreadID =  NULL;
                return 1;
            }
        }
        return 1;
    }
    int 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);
            return 0;
        }
        return 1;
    }

    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 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 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;

//call once at startup, should return 0
int sc_initialize(char * csd)
{
    sc_tt = new TamTamSound(csd);
    atexit(&sc_destroy);
    if (sc_tt->good()) return 0;
    else return -1;
}
//call once at end
void sc_destroy()
{
    if (sc_tt)
    {
        delete sc_tt;
        sc_tt = NULL;
    }
}
//compile the score, connect to device, start a sound rendering thread
int sc_start()
{
    return sc_tt->start();
}
//stop csound rendering thread, disconnect from sound device, clear tables.
int sc_stop()
{
    return sc_tt->stop();
}
//set the output volume to given level.  max volume is 100.0
void sc_setMasterVolume(double v)
{
    sc_tt->setMasterVolume(v);
}

void sc_inputMessage(const char *msg)
{
    sc_tt->inputMessage(msg);
}
void sc_scoreEvent4(char type, MYFLT p0, MYFLT p1, MYFLT p2, MYFLT p3)
{
    MYFLT p[4];
    p[0] = p0;
    p[1] = p1;
    p[2] = p2;
    p[3] = p3;
    sc_tt->scoreEvent(type, p, 4);
}
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)
{
    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;
    sc_tt->scoreEvent(type, p, 15);
}

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;
}