aboutsummaryrefslogtreecommitdiff
path: root/sndfiler/src/sndfiler.c
blob: 005b75ffb1fe4983a58cb4306d5637c031e65891 (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
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
/*
 *
 * threaded soundfiler based on libsndfile
 * Copyright (C) 2005, Tim Blechmann
 *           (C) 2005, Georg Holzmann <grh@mur.at>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/* to be compatible with main pd */
#ifdef USE_PD_MAIN

#define  getalignedbytes(a)     getbytes(a)
#define  freealignedbytes(a,b)  freebytes(a,b)
#include "threadlib.h"

/* forward declaration */
struct _garray
{
    t_gobj x_gobj;
    t_scalar *x_scalar;     /* scalar "containing" the array */
    t_glist *x_glist;       /* containing glist */
    t_symbol *x_name;       /* unexpanded name (possibly with leading '$') */
    t_symbol *x_realname;   /* expanded name (symbol we're bound to) */
    char x_usedindsp;       /* true if some DSP routine is using this */
    char x_saveit;          /* true if we should save this with parent */
    char x_listviewing;     /* true if list view window is open */
};

#else /* now for pd_devel */

#include "m_pd.h"
#include "m_fifo.h"
#include "pthread.h"
#include "semaphore.h"

#endif /* USE_PD_MAIN */


#include "g_canvas.h"
#include "sndfile.h"

#include "stdlib.h"
#include "sched.h" /* for thread priority */
#include <string.h>

/* for alloca */
#ifdef MSW
#include <malloc.h>
#else
#include "alloca.h"
#endif

#if (_POSIX_MEMLOCK - 0) >=  200112L
#include <sys/mman.h>
#endif /* _POSIX_MEMLOCK */


/************ forward declarations **************/

#ifdef UNIX
/* real-time flag, true if priority boosted */
extern int sys_hipriority;
#endif

/* get a garray's "array" structure. */
t_array *h_garray_getarray(t_garray *x)
{
    int zonset, ztype;
    t_symbol *zarraytype;
    t_scalar *sc = x->x_scalar;
    t_symbol *templatesym = sc->sc_template;
    t_template *_template = template_findbyname(templatesym);
    if (!_template)
    {
        error("array: couldn't find template %s", templatesym->s_name);
        return (0);
    }
    if (!template_find_field(_template, gensym("z"), 
            &zonset, &ztype, &zarraytype))
    {
        error("array: template %s has no 'z' field", templatesym->s_name);
        return (0);
    }
    if (ztype != DT_ARRAY)
    {
        error("array: template %s, 'z' field is not an array",
            templatesym->s_name);
        return (0);
    }
    return (sc->sc_vec[zonset].w_array);
}


/************ sndfiler **************/

static t_class *sndfiler_class;

typedef struct _sndfiler
{
    t_object x_obj;
    t_canvas *x_canvas;
} t_sndfiler;

typedef struct _sfprocess
{
    void* padding;
    /* callback function */
    void (* process) (t_sndfiler *, int, t_atom *);
    t_sndfiler * x;   /* soundfiler */
    int argc;
    t_atom * argv;
} t_sfprocess;

/* this is the queue for all soundfiler objects */
typedef struct _sfqueue
{
    t_fifo* x_jobs;
    sem_t sem;
} t_sfqueue;

typedef struct _syncdata
{
    t_garray** arrays;
    t_float** helper_arrays;
    int argc;
    t_int frames;
} t_syncdata;

static t_sfqueue sndfiler_queue; 
static pthread_t sf_thread_id; /* id of soundfiler thread */

static t_sndfiler *sndfiler_new(void)
{
    t_sndfiler *x = (t_sndfiler *)pd_new(sndfiler_class);
    x->x_canvas = canvas_getcurrent();
    outlet_new(&x->x_obj, &s_float);
    return (x);
}

/* global soundfiler thread ... sleeping until signaled */
static void sndfiler_thread(void)
{
    while (1)
    {
        t_sfprocess * me;
        sem_wait(&sndfiler_queue.sem);
        
        while (me = (t_sfprocess *)fifo_get(sndfiler_queue.x_jobs))
        {
            (me->process)(me->x, me->argc, me->argv);

            /* freeing the argument vector */
            freebytes(me->argv, sizeof(t_atom) * me->argc);
            freebytes(me, sizeof(t_sfprocess));
        }
    }
}

static void sndfiler_start_thread(void)
{
    pthread_attr_t sf_attr;
    struct sched_param sf_param;
    int status;

    //initialize queue
    sndfiler_queue.x_jobs = fifo_init();
    sem_init (&sndfiler_queue.sem,0,0);
    
    // initialize thread
    pthread_attr_init(&sf_attr);
    
    sf_param.sched_priority=sched_get_priority_min(SCHED_OTHER);
    pthread_attr_setschedparam(&sf_attr,&sf_param);
	
#ifdef UNIX
    if (sys_hipriority == 1/*  && getuid() == 0 */)
    {
        sf_param.sched_priority=sched_get_priority_min(SCHED_RR);
        pthread_attr_setschedpolicy(&sf_attr,SCHED_RR);
    }
#endif /* UNIX */

    //start thread
    status = pthread_create(&sf_thread_id, &sf_attr, 
        (void *) sndfiler_thread,NULL);
  
    if (status != 0)
        error("Couldn't create sndfiler thread: %d",status);
    else
        post("Global sndfiler thread launched, priority: %d", 
            sf_param.sched_priority);
}

static void sndfiler_read_cb(t_sndfiler * x, int argc, t_atom* argv);

/* syntax:
 * read soundfile array0..n
 * if the soundfile has less channels than arrays are given, these arrays are
 * set to zero
 * if there are too little arrays given, only the first n channels will be used
 * */
static void sndfiler_read(t_sndfiler * x, t_symbol *s, int argc, t_atom* argv)
{
    t_sfprocess * process = getbytes(sizeof(t_sfprocess));

    process->process = &sndfiler_read_cb;
    process->x = x;
    process->argc = argc;
    process->argv = (t_atom*) copybytes(argv, sizeof(t_atom) * argc);

    fifo_put(sndfiler_queue.x_jobs, process);

    sem_post(&sndfiler_queue.sem);
}

static t_int sndfiler_synchonize(t_int * w);

static void sndfiler_read_cb(t_sndfiler * x, int argc, t_atom* argv)
{
    int i, j;
    int channel_count;
    t_float** helper_arrays;
    int resize = 0;
    int seek = 0, arraysize = 0, writesize;

    t_symbol* file;
    t_garray ** arrays;

    SNDFILE* sndfile;
    SF_INFO info;
    
    // parse flags
    while (argc > 0 && argv->a_type == A_SYMBOL &&
        *argv->a_w.w_symbol->s_name == '-')
    {
        char *flag = argv->a_w.w_symbol->s_name + 1;
        if (!strcmp(flag, "resize"))
        {
            resize = 1;
            argc -= 1; argv += 1;
        }
        else if (!strcmp(flag, "skip"))
        {
            if (argc < 2 || argv[1].a_type != A_FLOAT ||
                ((seek = argv[1].a_w.w_float) == 0))
                goto usage;
            argc -= 2; argv += 2;
        }
        else goto usage;
    }
    
    if (argc < 2)
        goto usage;

    file = atom_getsymbolarg(0, argc, argv);

    channel_count = argc - 1;
    helper_arrays = getbytes(channel_count * sizeof(t_float*));

    arrays = getbytes(channel_count * sizeof(t_garray*));
    for (i = 0; i != channel_count; ++i)
    {
        t_float *dummy;
        int size;
        t_garray *array;
         
        if(!(array = (t_garray *)pd_findbyclass(
                                                atom_getsymbolarg(i+1, argc, argv), garray_class)))
        {
            pd_error(x, "%s: no such array", atom_getsymbolarg(i+1, 
                         argc, argv)->s_name);
            return;
        }
	
        if(garray_getfloatarray(array, &size, &dummy))
            arrays[i] = array;
        else
        {
            pd_error(x, "%s: bad template for sndfiler", atom_getsymbolarg(i+1, 
                         argc, argv)->s_name);
            return;
        }
	
        // in multichannel mode: check if arrays have different length
        if (arraysize && arraysize != size && !resize)
        {
            post("sndfiler: arrays have different lengths, resizing to last one ...");
        }
        arraysize = size;
    }
  
    sndfile = sf_open(file->s_name, SFM_READ, &info);

    if (sndfile)
    {
        int pos = 0;
        int maxchannels = (channel_count < info.channels) ?
            channel_count : info.channels;

        t_float * item = alloca(maxchannels * sizeof(t_float));
    
        t_int ** syncdata = getbytes(sizeof(t_int*) * 5);
	
        // negative seek: offset from the end of the file
        if(seek<0)
        {
            pos = sf_seek(sndfile, seek, SEEK_END);
        }
        if(seek>0)
        {
            pos = sf_seek(sndfile, seek, SEEK_SET);
        }
        if(pos == -1)
        {
            pd_error(x, "invalid seek in soundfile");
            return;
        }
	
        if(resize)
        {
            writesize = (info.frames-pos);
            arraysize = writesize;
        }
        else
            writesize = (arraysize>(info.frames-pos)) ? 
                info.frames-pos : arraysize;

#if (_POSIX_MEMLOCK - 0) >=  200112L
        munlockall();
#endif

        for (i = 0; i != channel_count; ++i)
        {
            helper_arrays[i] = getalignedbytes(arraysize * sizeof(t_float));
        }

        for (i = 0; i != writesize; ++i)
        {
            sf_read_float(sndfile, item, info.channels);

            for (j = 0; j != info.channels; ++j)
            {
                if (j < channel_count)
                {
                    helper_arrays[j][i] = item[j];
                }
            }
        }
	
        // fill remaining elements with zero
        if(!resize && (arraysize>(info.frames-pos)))
        {
            for (i = writesize; i != arraysize; ++i)
            {
                for (j = 0; j != info.channels; ++j)
                {
                    if (j < channel_count)
                    {
                        helper_arrays[j][i] = 0;
                    }
                }
            }
        }

#if (_POSIX_MEMLOCK - 0) >=  200112L
        mlockall(MCL_FUTURE);
#endif

        sf_close(sndfile);

        syncdata[0] = (t_int*)arrays;
        syncdata[1] = (t_int*)helper_arrays;
        syncdata[2] = (t_int*)channel_count;
        syncdata[3] = (t_int*)arraysize;
        syncdata[4] = (t_int*)x;

        sys_callback(sndfiler_synchonize, (t_int*)syncdata, 5);
        return;
    }
    else
    {
        pd_error(x, "Error opening file");
        return;
    }
    
 usage:
	pd_error(x, "usage: read [flags] filename array1 array2 ...");
    post("flags: -skip <n> -resize ");
}

static t_int sndfiler_synchonize(t_int * w)
{
    int i;
    t_garray** arrays = (t_garray**) w[0];
    t_float** helper_arrays = (t_float**) w[1];
    t_int channel_count = (t_int)w[2];
    t_int frames = (t_int)w[3];
    t_sndfiler* x = (t_sndfiler*)w[4];

    for (i = 0; i != channel_count; ++i)
    {
        t_garray * garray = arrays[i];
        t_array * array = h_garray_getarray(garray);
        t_glist * gl = garray->x_glist;;

        freealignedbytes(array->a_vec, array->a_n);
        array->a_vec = (char*)helper_arrays[i];
        array->a_n = frames;

        if (gl->gl_list == &garray->x_gobj && !garray->x_gobj.g_next)
        {
            vmess(&gl->gl_pd, gensym("bounds"), "ffff", 0., gl->gl_y1,
                (double)(frames > 1 ? frames-1 : 1), gl->gl_y2);

            /* close any dialogs that might have the wrong info now... */
            gfxstub_deleteforkey(gl);
        }
        else 
            garray_redraw(garray);
    }

    free(arrays);
    free(helper_arrays);

    canvas_update_dsp();

    outlet_float(x->x_obj.ob_outlet, frames);

    return 0;
}

static void sndfiler_t_resize(t_sndfiler * y, int argc, t_atom* argv);

/* syntax:
 * resize table size
 * */
static void sndfiler_resize(t_sndfiler * x, t_symbol *s, int argc, t_atom* argv)
{
    t_sfprocess * process = getbytes(sizeof(t_sfprocess));

    process->process = &sndfiler_t_resize;
    process->x = x;
    process->argc = argc;
    process->argv = (t_atom*) copybytes(argv, sizeof(t_atom) * argc);

    fifo_put(sndfiler_queue.x_jobs, process);

    sem_post(&sndfiler_queue.sem);
}

static void sndfiler_t_resize(t_sndfiler *y, int argc, t_atom *argv)
{
    int was, elemsize;       /* array contains was elements of size elemsize */
    t_float * vec;           /* old array */ 
    t_glist *gl;
    int n;                   /* resize of n elements */
    char *nvec;              /* new array */ 

    t_garray * x = (t_garray *)pd_findbyclass(argv[0].a_w.w_symbol, garray_class);
    t_array *data = h_garray_getarray(x);

    if (!(x))
    {
        pd_error(y, "%s: no such table", argv[0].a_w.w_symbol->s_name);
        goto usage;
    }
    
    vec = (t_float*) data->a_vec;
    was = data->a_n;

    if ((argv+1)->a_type == A_FLOAT)
        n = (int) (argv+1)->a_w.w_float;
    else
        goto usage;

    if (n == was) return;

    if (n < 1) n = 1;
    elemsize = template_findbyname(data->a_templatesym)->t_n * sizeof(t_word);

#if (_POSIX_MEMLOCK - 0) >=  200112L
    munlockall();
#endif

    if (was > n)
        nvec = (char*)copybytes(data->a_vec, was * elemsize);
    else
    {
        nvec = getbytes(n * elemsize);
        memcpy (nvec, data->a_vec, was * elemsize);

        /* LATER should check t_resizebytes result */
        memset(nvec + was*elemsize, 0, (n - was) * elemsize);
    }
  
    if (!nvec)
    {
        pd_error(x, "array resize failed: out of memory");

#if (_POSIX_MEMLOCK - 0) >=  200112L
        mlockall(MCL_FUTURE);
#endif

        return;
    }

    /* TB: we'll have to be sure that no one is accessing the array */
    sys_lock();

#ifdef GARRAY_THREAD_LOCK
    garray_lock(x);
#endif

    data->a_vec = nvec;
    data->a_n = n;

#ifdef GARRAY_THREAD_LOCK
    garray_unlock(x);
#endif
    
    if (x->x_usedindsp) canvas_update_dsp();
    sys_unlock();

    /* if this is the only array in the graph,
       reset the graph's coordinates */
    gl = x->x_glist;
    if (gl->gl_list == &x->x_gobj && !x->x_gobj.g_next)
    {
        vmess(&gl->gl_pd, gensym("bounds"), "ffff",
            0., gl->gl_y1, (double)(n > 1 ? n-1 : 1), gl->gl_y2);
        /* close any dialogs that might have the wrong info now... */
        gfxstub_deleteforkey(gl);
    }
    else garray_redraw(x);

    freebytes (vec, was * elemsize);

#if (_POSIX_MEMLOCK - 0) >= 200112L
    mlockall(MCL_FUTURE);
#endif

    sys_lock();
    outlet_float(y->x_obj.ob_outlet, (float)atom_getintarg(1,argc,argv)); 
    sys_unlock();
  
    return;
  
 usage:
    pd_error(x, "usage: resize tablename size");
}

#ifdef _MSC_VER
__declspec(dllexport)
#endif
void sndfiler_setup(void)
{
    sndfiler_class = class_new(gensym("sndfiler"), 
        (t_newmethod)sndfiler_new, 0,
        sizeof(t_sndfiler), 0, 0);

    class_addmethod(sndfiler_class, (t_method)sndfiler_read,
        gensym("read"), A_GIMME, 0);
    class_addmethod(sndfiler_class, (t_method)sndfiler_resize,
        gensym("resize"), A_GIMME, 0);

#ifdef USE_PD_MAIN
    // needed to start thread callback system
    threadlib_setup();
#endif
  
    // starts helper thread
    sndfiler_start_thread();
}