Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Util/Clooper/SoundClient.cpp
blob: e527c027efa9784f4c58ddc68391988125e0e367 (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
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
#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>
#include <cmath>

using namespace std;

struct ev_t
{
    char type;
    int onset;
    bool time_in_ticks;
    MYFLT prev_secs_per_tick;
    MYFLT duration, attack, decay;
    std::vector<MYFLT> param;

    ev_t(char type, bool in_ticks, 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)
        : type(type), onset(0), time_in_ticks(in_ticks), param(15)
    {
        onset = (int) p2;
        duration = p3;
        attack = p9;
        decay = p10;
        prev_secs_per_tick = -1.0;

        param[0] = p1;
        param[1] = 0.0; //onset
        param[2] = p3;  //duration
        param[3] = p4;  //pitch
        param[4] = p5;  //reverbSend
        param[5] = p6;  //amplitude
        param[6] = p7;  //pan
        param[7] = p8;  //table
        param[8] = p9;  //attack
        param[9] = p10; //decay
        param[10] = p11;//filterType
        param[11] = p12;//filterCutoff
        param[12] = p13;//loopStart
        param[13] = p14;//loopEnd
        param[14] = p15;//crossDur
    }
    bool operator<(const ev_t &e) const
    {
        return onset < e.onset;
    }
    void ev_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, MYFLT secs_per_tick)
    {
        if (time_in_ticks && (secs_per_tick != prev_secs_per_tick))
        {
            param[2] = duration * secs_per_tick;
            param[8] = max(0.002f, attack * secs_per_tick);
            param[9] = max(0.002f, decay * secs_per_tick);
            prev_secs_per_tick = secs_per_tick;
            if (0) fprintf(stdout, "setting duration to %f\n", param[5]);
        }
        csoundScoreEvent(csound, type, &param[0], param.size());
    }
};
struct EvLoop
{
    int tick_prev;
    int tickMax;
    MYFLT rtick;
    MYFLT secs_per_tick;
    MYFLT 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), ev(), ev_pos(ev.end()), csound(cs), mutex(NULL)
    {
        setTickDuration(0.05);
        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 = (MYFLT)(t % tickMax);
        //TODO: binary search would be faster
        csoundLockMutex(mutex);
        ev_pos = ev.lower_bound( t );
        csoundUnlockMutex(mutex);
    }
    void setTickDuration(MYFLT d)
    {
        if (!csound) {
            fprintf(stderr, "skipping setTickDuration, csound==NULL\n");
            return;
        }
        secs_per_tick = 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 * logf)
    {
        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 = 8;  //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 (logf) ev_pos->second->ev_print(logf);
                    if (events < eventMax) ev_pos->second->event(csound, secs_per_tick);
                    ++ev_pos;
                    ++events;
                    ++loop0;
                }
                ev_pos = ev.begin();
            }
            while ((ev_pos != ev.end()) && (tick >= ev_pos->first))
            {
                if (logf) ev_pos->second->ev_print(logf);
                if (events < eventMax) ev_pos->second->event(csound, secs_per_tick);
                ++ev_pos;
                ++events;
                ++loop1;
            }
        }
        csoundUnlockMutex(mutex);
        tick_prev = tick;
        if (logf && (events >= eventMax)) fprintf(logf, "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)
    {
        if (1)
        {
            csound = csoundCreate(NULL);
        }
        else
        {
            csound = NULL;
        }
        csound_orc = strdup(orc);

        loop = new EvLoop(csound);
    }
    ~TamTamSound()
    {
        if (csound)
        {
            stop();
            delete loop;
            csoundDestroy(csound);
        }
        free(csound_orc);
        if (_debug) 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;
                        if (_debug) fprintf(_debug, "maximum lag %lf\n", m);
                    }
                }
                t_prev = t_this;
            }
            if (thread_playloop)
            {
                loop->step(NULL);
            }
            ++loops;
        }
        return 0;
    }
    static uintptr_t csThread(void *clientData)
    {
        return ((TamTamSound*)clientData)->thread_fn();
    }
    int start()
    {
        if (!csound) {
            fprintf(stderr, "skipping %s, csound==NULL\n", __FUNCTION__);
            return 1;
        }
        if (!ThreadID)
        {
            int argc=3;
            char  **argv = (char**)malloc(argc*sizeof(char*));
            argv[0] = "csound";
            argv[1] ="-m0";
            argv[2] = csound_orc;
            if (_debug) 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
            {
                if (_debug) fprintf(_debug, "ERROR: failed to compile orchestra\n");
                ThreadID =  NULL;
                return 1;
            }
        }
        return 1;
    }
    int stop()
    {
        if (!csound) {
            fprintf(stderr, "skipping %s, csound==NULL\n", __FUNCTION__);
            return 1;
        }
        if (ThreadID)
        {
            PERF_STATUS = STOP;
            if (_debug) fprintf(_debug, "INFO: stop()");
            uintptr_t rval = csoundJoinThread(ThreadID);
            if (rval) if (_debug) 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(stderr, "skipping %s, csound==NULL\n", __FUNCTION__);
            return ;
        }
        if ((verbosity > 2) && _debug)
        {
            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(stderr, "skipping %s, csound==NULL\n", __FUNCTION__);
            return ;
        }
        if (_debug && (verbosity > 2)) fprintf(_debug, "%s\n", msg);
        csoundInputMessage(csound, msg);
    }
    bool good()
    {
        return csound != NULL;
    }

    void setMasterVolume(MYFLT vol)
    {
        if (!csound) {
            fprintf(stderr, "skipping %s, csound==NULL\n", __FUNCTION__);
            return ;
        }
        MYFLT *p;
        if (!(csoundGetChannelPtr(csound, &p, "masterVolume", CSOUND_CONTROL_CHANNEL | CSOUND_INPUT_CHANNEL)))
            *p = (MYFLT) vol;
        else
        {
            if (_debug) 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(MYFLT 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(MYFLT secs_per_tick)
{
    sc_tt->loop->setTickDuration(secs_per_tick);
}
void sc_loop_addScoreEvent15(int in_ticks, 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, in_ticks, 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;
}