~ubuntu-branches/ubuntu/trusty/python-pyo/trusty-proposed

« back to all changes in this revision

Viewing changes to src/objects/granulatormodule.c

  • Committer: Package Import Robot
  • Author(s): Tiago Bortoletto Vaz
  • Date: 2013-09-02 17:25:57 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20130902172557-9mmcx9g59vc031sa
Tags: 0.6.6+svn1132-1
* Added new objects:
  - PVMix, Mix the most prominent components from two phase vocoder streaming
    objects.
  - TableScale, Scales values from a table and writes them into another table.
  - Granule, another granular synthesis generator.
  - PVBufTabLoops, phase vocoder buffer with bin independent speed playback.
  - PVBufLoops, phase vocoder buffer with bin independent speed playback.
  - PVShift, spectral frequency shifter. PVAmpMod and PVFreqMod, frequency
    independent modulations.
  - PVDelay, spectral delays and PVBuffer, pv recorder and playback.
  - PVFilter. Spectral filtering.
  - PVCross, PVMult, PVMorph. Spectral morphing.
  - PVAddSynth, Phase Vocoder additive synthesis object.
* Added E-Pyo binary to the package, accessible via Sound & Video menu.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
    PyObject *dur;
44
44
    Stream *dur_stream;
45
45
    int ngrains;
46
 
    int init;
47
46
    MYFLT basedur;
48
47
    MYFLT pointerPos;
49
48
    MYFLT *startPos;
2305
2304
    Looper_new,                 /* tp_new */
2306
2305
};
2307
2306
 
 
2307
static const MYFLT Granule_MAX_GRAINS = 1024; 
 
2308
typedef struct {
 
2309
    pyo_audio_HEAD
 
2310
    PyObject *table;
 
2311
    PyObject *env;
 
2312
    PyObject *dens;
 
2313
    Stream *dens_stream;
 
2314
    PyObject *pitch;
 
2315
    Stream *pitch_stream;
 
2316
    PyObject *pos;
 
2317
    Stream *pos_stream;
 
2318
    PyObject *dur;
 
2319
    Stream *dur_stream;
 
2320
    MYFLT *gpos;
 
2321
    MYFLT *glen;
 
2322
    MYFLT *inc;
 
2323
    MYFLT *phase;
 
2324
    int *flags;
 
2325
    int num;
 
2326
    double timer;
 
2327
    MYFLT oneOnSr;
 
2328
    int modebuffer[6];
 
2329
} Granule;
 
2330
 
 
2331
static void
 
2332
Granule_transform_i(Granule *self) {
 
2333
    MYFLT dens, inc, index, amp, phase;
 
2334
    int i, j, ipart;
 
2335
    int flag = 0; 
 
2336
    MYFLT pit = 0, pos = 0, dur = 0; 
 
2337
    
 
2338
    MYFLT *tablelist = TableStream_getData(self->table);
 
2339
    int size = TableStream_getSize(self->table);
 
2340
    
 
2341
    MYFLT *envlist = TableStream_getData(self->env);
 
2342
    int envsize = TableStream_getSize(self->env);
 
2343
    
 
2344
    dens = PyFloat_AS_DOUBLE(self->dens);
 
2345
    if (dens < 0.0)
 
2346
        dens = -dens;
 
2347
    
 
2348
    inc = dens * self->oneOnSr;
 
2349
    
 
2350
    for (i=0; i<self->bufsize; i++) {
 
2351
        self->data[i] = 0.0;
 
2352
 
 
2353
        /* clocker */
 
2354
        self->timer += inc;
 
2355
        if (self->timer >= 1.0) {
 
2356
            self->timer -= 1.0;
 
2357
            flag = 1;
 
2358
        }
 
2359
 
 
2360
        /* need to start a new grain */
 
2361
        if (flag) {
 
2362
            for (j=0; j<Granule_MAX_GRAINS; j++) {
 
2363
                if (self->flags[j] == 0) {
 
2364
                    self->flags[j] = 1;
 
2365
                    if (j >= self->num)
 
2366
                        self->num = j + 1;
 
2367
                    if (self->modebuffer[3] == 0)
 
2368
                        pit = PyFloat_AS_DOUBLE(self->pitch);
 
2369
                    else
 
2370
                        pit = Stream_getData((Stream *)self->pitch_stream)[i];
 
2371
                    if (self->modebuffer[4] == 0)
 
2372
                        pos = PyFloat_AS_DOUBLE(self->pos);
 
2373
                    else
 
2374
                        pos = Stream_getData((Stream *)self->pos_stream)[i];
 
2375
                    if (self->modebuffer[5] == 0)
 
2376
                        dur = PyFloat_AS_DOUBLE(self->dur);
 
2377
                    else
 
2378
                        dur = Stream_getData((Stream *)self->dur_stream)[i];
 
2379
                    if (pos < 0.0)
 
2380
                        pos = 0.0;
 
2381
                    else if (pos >= size)
 
2382
                        pos = (MYFLT)size;
 
2383
                    self->gpos[j] = pos;
 
2384
                    self->glen[j] = dur * self->sr * pit;
 
2385
                    if ((pos + self->glen[j]) >= size || (pos + self->glen[j]) < 0)
 
2386
                        self->flags[j] = 0;
 
2387
                    self->phase[j] = 0.0;
 
2388
                    self->inc[j] = 1.0 / (dur * self->sr);
 
2389
                    break;
 
2390
                }
 
2391
            }
 
2392
        }
 
2393
 
 
2394
        /* compute active grains */
 
2395
        for (j=0; j<self->num; j++) {
 
2396
            if (self->flags[j]) {
 
2397
                phase = self->phase[j];
 
2398
                if (phase >= 0.0 && phase < 1.0) {
 
2399
                    // compute envelope
 
2400
                    index = phase * envsize;
 
2401
                    ipart = (int)index;
 
2402
                    amp = envlist[ipart] + (envlist[ipart+1] - envlist[ipart]) * (index - ipart);
 
2403
 
 
2404
                    // compute sampling
 
2405
                    index = phase * self->glen[j] + self->gpos[j];
 
2406
                    ipart = (int)index;
 
2407
                    self->data[i] += (tablelist[ipart] + (tablelist[ipart+1] - tablelist[ipart]) * (index - ipart)) * amp;
 
2408
 
 
2409
                    phase += self->inc[j];
 
2410
                    if (phase >= 1.0)
 
2411
                        self->flags[j] = 0;
 
2412
                    else
 
2413
                        self->phase[j] = phase;
 
2414
                }
 
2415
            }
 
2416
        }
 
2417
        flag = 0;
 
2418
    }    
 
2419
}
 
2420
 
 
2421
static void
 
2422
Granule_transform_a(Granule *self) {
 
2423
    MYFLT index, amp, phase;
 
2424
    int i, j, ipart;
 
2425
    int flag = 0;
 
2426
    MYFLT pit = 0, pos = 0, dur = 0; 
 
2427
    
 
2428
    MYFLT *tablelist = TableStream_getData(self->table);
 
2429
    int size = TableStream_getSize(self->table);
 
2430
    
 
2431
    MYFLT *envlist = TableStream_getData(self->env);
 
2432
    int envsize = TableStream_getSize(self->env);
 
2433
 
 
2434
    MYFLT *density = Stream_getData((Stream *)self->dens_stream);
 
2435
 
 
2436
    for (i=0; i<self->bufsize; i++) {
 
2437
        self->data[i] = 0.0;
 
2438
 
 
2439
        /* clocker */
 
2440
        self->timer += density[i] * self->oneOnSr;
 
2441
        if (self->timer >= 1.0) {
 
2442
            self->timer -= 1.0;
 
2443
            flag = 1;
 
2444
        }
 
2445
 
 
2446
        /* need to start a new grain */
 
2447
        if (flag) {
 
2448
            for (j=0; j<Granule_MAX_GRAINS; j++) {
 
2449
                if (self->flags[j] == 0) {
 
2450
                    self->flags[j] = 1;
 
2451
                    if (j >= self->num)
 
2452
                        self->num = j + 1;
 
2453
                    if (self->modebuffer[3] == 0)
 
2454
                        pit = PyFloat_AS_DOUBLE(self->pitch);
 
2455
                    else
 
2456
                        pit = Stream_getData((Stream *)self->pitch_stream)[i];
 
2457
                    if (self->modebuffer[4] == 0)
 
2458
                        pos = PyFloat_AS_DOUBLE(self->pos);
 
2459
                    else
 
2460
                        pos = Stream_getData((Stream *)self->pos_stream)[i];
 
2461
                    if (self->modebuffer[5] == 0)
 
2462
                        dur = PyFloat_AS_DOUBLE(self->dur);
 
2463
                    else
 
2464
                        dur = Stream_getData((Stream *)self->dur_stream)[i];
 
2465
                    if (pos < 0.0)
 
2466
                        pos = 0.0;
 
2467
                    else if (pos >= size)
 
2468
                        pos = (MYFLT)size;
 
2469
                    self->gpos[j] = pos;
 
2470
                    self->glen[j] = dur * self->sr * pit;
 
2471
                    if ((pos + self->glen[j]) >= size || (pos + self->glen[j]) < 0)
 
2472
                        self->flags[j] = 0;
 
2473
                    self->phase[j] = 0.0;
 
2474
                    self->inc[j] = 1.0 / (dur * self->sr);
 
2475
                    break;
 
2476
                }
 
2477
            }
 
2478
        }
 
2479
 
 
2480
        /* compute active grains */
 
2481
        for (j=0; j<self->num; j++) {
 
2482
            if (self->flags[j]) {
 
2483
                phase = self->phase[j];
 
2484
                if (phase >= 0.0 && phase < 1.0) {
 
2485
                    // compute envelope
 
2486
                    index = phase * envsize;
 
2487
                    ipart = (int)index;
 
2488
                    amp = envlist[ipart] + (envlist[ipart+1] - envlist[ipart]) * (index - ipart);
 
2489
 
 
2490
                    // compute sampling
 
2491
                    index = phase * self->glen[j] + self->gpos[j];
 
2492
                    ipart = (int)index;
 
2493
                    self->data[i] += (tablelist[ipart] + (tablelist[ipart+1] - tablelist[ipart]) * (index - ipart)) * amp;
 
2494
 
 
2495
                    phase += self->inc[j];
 
2496
                    if (phase >= 1.0)
 
2497
                        self->flags[j] = 0;
 
2498
                    else
 
2499
                        self->phase[j] = phase;
 
2500
                }
 
2501
            }
 
2502
        }
 
2503
        flag = 0;
 
2504
    }    
 
2505
}
 
2506
 
 
2507
static void Granule_postprocessing_ii(Granule *self) { POST_PROCESSING_II };
 
2508
static void Granule_postprocessing_ai(Granule *self) { POST_PROCESSING_AI };
 
2509
static void Granule_postprocessing_ia(Granule *self) { POST_PROCESSING_IA };
 
2510
static void Granule_postprocessing_aa(Granule *self) { POST_PROCESSING_AA };
 
2511
static void Granule_postprocessing_ireva(Granule *self) { POST_PROCESSING_IREVA };
 
2512
static void Granule_postprocessing_areva(Granule *self) { POST_PROCESSING_AREVA };
 
2513
static void Granule_postprocessing_revai(Granule *self) { POST_PROCESSING_REVAI };
 
2514
static void Granule_postprocessing_revaa(Granule *self) { POST_PROCESSING_REVAA };
 
2515
static void Granule_postprocessing_revareva(Granule *self) { POST_PROCESSING_REVAREVA };
 
2516
 
 
2517
static void
 
2518
Granule_setProcMode(Granule *self)
 
2519
{
 
2520
    int procmode, muladdmode;
 
2521
    procmode = self->modebuffer[2];
 
2522
    muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
 
2523
 
 
2524
        switch (procmode) {
 
2525
        case 0:    
 
2526
            self->proc_func_ptr = Granule_transform_i;
 
2527
            break;
 
2528
        case 1:    
 
2529
            self->proc_func_ptr = Granule_transform_a;
 
2530
            break;
 
2531
    } 
 
2532
        switch (muladdmode) {
 
2533
        case 0:        
 
2534
            self->muladd_func_ptr = Granule_postprocessing_ii;
 
2535
            break;
 
2536
        case 1:    
 
2537
            self->muladd_func_ptr = Granule_postprocessing_ai;
 
2538
            break;
 
2539
        case 2:    
 
2540
            self->muladd_func_ptr = Granule_postprocessing_revai;
 
2541
            break;
 
2542
        case 10:        
 
2543
            self->muladd_func_ptr = Granule_postprocessing_ia;
 
2544
            break;
 
2545
        case 11:    
 
2546
            self->muladd_func_ptr = Granule_postprocessing_aa;
 
2547
            break;
 
2548
        case 12:    
 
2549
            self->muladd_func_ptr = Granule_postprocessing_revaa;
 
2550
            break;
 
2551
        case 20:        
 
2552
            self->muladd_func_ptr = Granule_postprocessing_ireva;
 
2553
            break;
 
2554
        case 21:    
 
2555
            self->muladd_func_ptr = Granule_postprocessing_areva;
 
2556
            break;
 
2557
        case 22:    
 
2558
            self->muladd_func_ptr = Granule_postprocessing_revareva;
 
2559
            break;
 
2560
    }   
 
2561
}
 
2562
 
 
2563
static void
 
2564
Granule_compute_next_data_frame(Granule *self)
 
2565
{
 
2566
    (*self->proc_func_ptr)(self); 
 
2567
    (*self->muladd_func_ptr)(self);
 
2568
}
 
2569
 
 
2570
static int
 
2571
Granule_traverse(Granule *self, visitproc visit, void *arg)
 
2572
{
 
2573
    pyo_VISIT
 
2574
    Py_VISIT(self->table);
 
2575
    Py_VISIT(self->env);
 
2576
    Py_VISIT(self->dens);    
 
2577
    Py_VISIT(self->dens_stream);    
 
2578
    Py_VISIT(self->pitch);    
 
2579
    Py_VISIT(self->pitch_stream);    
 
2580
    Py_VISIT(self->pos);    
 
2581
    Py_VISIT(self->pos_stream);    
 
2582
    Py_VISIT(self->dur);    
 
2583
    Py_VISIT(self->dur_stream);    
 
2584
    return 0;
 
2585
}
 
2586
 
 
2587
static int 
 
2588
Granule_clear(Granule *self)
 
2589
{
 
2590
    pyo_CLEAR
 
2591
    Py_CLEAR(self->table);
 
2592
    Py_CLEAR(self->env);
 
2593
    Py_CLEAR(self->dens);    
 
2594
    Py_CLEAR(self->dens_stream);    
 
2595
    Py_CLEAR(self->pitch);    
 
2596
    Py_CLEAR(self->pitch_stream);    
 
2597
    Py_CLEAR(self->pos);    
 
2598
    Py_CLEAR(self->pos_stream);    
 
2599
    Py_CLEAR(self->dur);    
 
2600
    Py_CLEAR(self->dur_stream);    
 
2601
    return 0;
 
2602
}
 
2603
 
 
2604
static void
 
2605
Granule_dealloc(Granule* self)
 
2606
{
 
2607
    pyo_DEALLOC
 
2608
    free(self->gpos);   
 
2609
    free(self->glen);
 
2610
    free(self->inc);
 
2611
    free(self->flags);
 
2612
    free(self->phase);
 
2613
    Granule_clear(self);
 
2614
    self->ob_type->tp_free((PyObject*)self);
 
2615
}
 
2616
 
 
2617
static PyObject *
 
2618
Granule_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 
2619
{
 
2620
    int i;
 
2621
    PyObject *tabletmp, *envtmp, *denstmp=NULL, *pitchtmp=NULL, *postmp=NULL, *durtmp=NULL, *multmp=NULL, *addtmp=NULL;
 
2622
    Granule *self;
 
2623
    self = (Granule *)type->tp_alloc(type, 0);
 
2624
 
 
2625
    self->dens = PyFloat_FromDouble(50);
 
2626
    self->pitch = PyFloat_FromDouble(1);
 
2627
    self->pos = PyFloat_FromDouble(0.0);
 
2628
    self->dur = PyFloat_FromDouble(0.1);
 
2629
    self->timer = 1.0;
 
2630
    self->num = 0;
 
2631
        self->modebuffer[0] = 0;
 
2632
        self->modebuffer[1] = 0;
 
2633
        self->modebuffer[2] = 0;
 
2634
        self->modebuffer[3] = 0;
 
2635
        self->modebuffer[4] = 0;
 
2636
        self->modebuffer[5] = 0;
 
2637
 
 
2638
    INIT_OBJECT_COMMON
 
2639
    
 
2640
    self->oneOnSr = 1.0 / self->sr;
 
2641
 
 
2642
    Stream_setFunctionPtr(self->stream, Granule_compute_next_data_frame);
 
2643
    self->mode_func_ptr = Granule_setProcMode;
 
2644
 
 
2645
    static char *kwlist[] = {"table", "env", "dens", "pitch", "pos", "dur", "mul", "add", NULL};
 
2646
 
 
2647
    if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO|OOOOOO", kwlist, &tabletmp, &envtmp, &denstmp, &pitchtmp, &postmp, &durtmp, &multmp, &addtmp))
 
2648
        Py_RETURN_NONE;
 
2649
 
 
2650
    if ( PyObject_HasAttrString((PyObject *)tabletmp, "getTableStream") == 0 ) {
 
2651
        PySys_WriteStderr("TypeError: \"table\" argument of Granule must be a PyoTableObject.\n");
 
2652
        if (PyInt_AsLong(PyObject_CallMethod(self->server, "getIsBooted", NULL))) {
 
2653
            PyObject_CallMethod(self->server, "shutdown", NULL);
 
2654
        }
 
2655
        Py_Exit(1);
 
2656
    }
 
2657
    Py_XDECREF(self->table);
 
2658
    self->table = PyObject_CallMethod((PyObject *)tabletmp, "getTableStream", "");
 
2659
 
 
2660
    if ( PyObject_HasAttrString((PyObject *)envtmp, "getTableStream") == 0 ) {
 
2661
        PySys_WriteStderr("TypeError: \"env\" argument of Granule must be a PyoTableObject.\n");
 
2662
        if (PyInt_AsLong(PyObject_CallMethod(self->server, "getIsBooted", NULL))) {
 
2663
            PyObject_CallMethod(self->server, "shutdown", NULL);
 
2664
        }
 
2665
        Py_Exit(1);
 
2666
    }
 
2667
    Py_XDECREF(self->env);
 
2668
    self->env = PyObject_CallMethod((PyObject *)envtmp, "getTableStream", "");
 
2669
 
 
2670
    if (denstmp) {
 
2671
        PyObject_CallMethod((PyObject *)self, "setDens", "O", denstmp);
 
2672
    }
 
2673
    
 
2674
    if (pitchtmp) {
 
2675
        PyObject_CallMethod((PyObject *)self, "setPitch", "O", pitchtmp);
 
2676
    }
 
2677
 
 
2678
    if (postmp) {
 
2679
        PyObject_CallMethod((PyObject *)self, "setPos", "O", postmp);
 
2680
    }
 
2681
 
 
2682
    if (durtmp) {
 
2683
        PyObject_CallMethod((PyObject *)self, "setDur", "O", durtmp);
 
2684
    }
 
2685
    
 
2686
    if (multmp) {
 
2687
        PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
 
2688
    }
 
2689
 
 
2690
    if (addtmp) {
 
2691
        PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
 
2692
    }
 
2693
 
 
2694
    PyObject_CallMethod(self->server, "addStream", "O", self->stream);
 
2695
 
 
2696
    self->gpos = (MYFLT *)realloc(self->gpos, Granule_MAX_GRAINS * sizeof(MYFLT));
 
2697
    self->glen = (MYFLT *)realloc(self->glen, Granule_MAX_GRAINS * sizeof(MYFLT));
 
2698
    self->inc = (MYFLT *)realloc(self->inc, Granule_MAX_GRAINS * sizeof(MYFLT));
 
2699
    self->phase = (MYFLT *)realloc(self->phase, Granule_MAX_GRAINS * sizeof(MYFLT));
 
2700
    self->flags = (int *)realloc(self->flags, Granule_MAX_GRAINS * sizeof(int));
 
2701
    
 
2702
    for (i=0; i<Granule_MAX_GRAINS; i++) {
 
2703
        self->gpos[i] = self->glen[i] = self->inc[i] = self->phase[i] = 0.0;
 
2704
        self->flags[i] = 0;
 
2705
    }
 
2706
    
 
2707
    (*self->mode_func_ptr)(self);
 
2708
    
 
2709
    return (PyObject *)self;
 
2710
}
 
2711
 
 
2712
static PyObject * Granule_getServer(Granule* self) { GET_SERVER };
 
2713
static PyObject * Granule_getStream(Granule* self) { GET_STREAM };
 
2714
static PyObject * Granule_setMul(Granule *self, PyObject *arg) { SET_MUL };     
 
2715
static PyObject * Granule_setAdd(Granule *self, PyObject *arg) { SET_ADD };     
 
2716
static PyObject * Granule_setSub(Granule *self, PyObject *arg) { SET_SUB };     
 
2717
static PyObject * Granule_setDiv(Granule *self, PyObject *arg) { SET_DIV };     
 
2718
 
 
2719
static PyObject * Granule_play(Granule *self, PyObject *args, PyObject *kwds) { PLAY };
 
2720
static PyObject * Granule_out(Granule *self, PyObject *args, PyObject *kwds) { OUT };
 
2721
static PyObject * Granule_stop(Granule *self) { STOP };
 
2722
 
 
2723
static PyObject * Granule_multiply(Granule *self, PyObject *arg) { MULTIPLY };
 
2724
static PyObject * Granule_inplace_multiply(Granule *self, PyObject *arg) { INPLACE_MULTIPLY };
 
2725
static PyObject * Granule_add(Granule *self, PyObject *arg) { ADD };
 
2726
static PyObject * Granule_inplace_add(Granule *self, PyObject *arg) { INPLACE_ADD };
 
2727
static PyObject * Granule_sub(Granule *self, PyObject *arg) { SUB };
 
2728
static PyObject * Granule_inplace_sub(Granule *self, PyObject *arg) { INPLACE_SUB };
 
2729
static PyObject * Granule_div(Granule *self, PyObject *arg) { DIV };
 
2730
static PyObject * Granule_inplace_div(Granule *self, PyObject *arg) { INPLACE_DIV };
 
2731
 
 
2732
static PyObject *
 
2733
Granule_setDens(Granule *self, PyObject *arg)
 
2734
{
 
2735
        PyObject *tmp, *streamtmp;
 
2736
        
 
2737
        if (arg == NULL) {
 
2738
                Py_INCREF(Py_None);
 
2739
                return Py_None;
 
2740
        }
 
2741
    
 
2742
        int isNumber = PyNumber_Check(arg);
 
2743
        
 
2744
        tmp = arg;
 
2745
        Py_INCREF(tmp);
 
2746
        Py_DECREF(self->dens);
 
2747
        if (isNumber == 1) {
 
2748
                self->dens = PyNumber_Float(tmp);
 
2749
        self->modebuffer[2] = 0;
 
2750
        }
 
2751
        else {
 
2752
                self->dens = tmp;
 
2753
        streamtmp = PyObject_CallMethod((PyObject *)self->dens, "_getStream", NULL);
 
2754
        Py_INCREF(streamtmp);
 
2755
        Py_XDECREF(self->dens_stream);
 
2756
        self->dens_stream = (Stream *)streamtmp;
 
2757
                self->modebuffer[2] = 1;
 
2758
        }
 
2759
    
 
2760
    (*self->mode_func_ptr)(self);
 
2761
    
 
2762
        Py_INCREF(Py_None);
 
2763
        return Py_None;
 
2764
}       
 
2765
 
 
2766
static PyObject *
 
2767
Granule_setPitch(Granule *self, PyObject *arg)
 
2768
{
 
2769
        PyObject *tmp, *streamtmp;
 
2770
        
 
2771
        if (arg == NULL) {
 
2772
                Py_INCREF(Py_None);
 
2773
                return Py_None;
 
2774
        }
 
2775
    
 
2776
        int isNumber = PyNumber_Check(arg);
 
2777
        
 
2778
        tmp = arg;
 
2779
        Py_INCREF(tmp);
 
2780
        Py_DECREF(self->pitch);
 
2781
        if (isNumber == 1) {
 
2782
                self->pitch = PyNumber_Float(tmp);
 
2783
        self->modebuffer[3] = 0;
 
2784
        }
 
2785
        else {
 
2786
                self->pitch = tmp;
 
2787
        streamtmp = PyObject_CallMethod((PyObject *)self->pitch, "_getStream", NULL);
 
2788
        Py_INCREF(streamtmp);
 
2789
        Py_XDECREF(self->pitch_stream);
 
2790
        self->pitch_stream = (Stream *)streamtmp;
 
2791
                self->modebuffer[3] = 1;
 
2792
        }
 
2793
 
 
2794
        Py_INCREF(Py_None);
 
2795
        return Py_None;
 
2796
}       
 
2797
 
 
2798
static PyObject *
 
2799
Granule_setPos(Granule *self, PyObject *arg)
 
2800
{
 
2801
        PyObject *tmp, *streamtmp;
 
2802
        
 
2803
        if (arg == NULL) {
 
2804
                Py_INCREF(Py_None);
 
2805
                return Py_None;
 
2806
        }
 
2807
    
 
2808
        int isNumber = PyNumber_Check(arg);
 
2809
        
 
2810
        tmp = arg;
 
2811
        Py_INCREF(tmp);
 
2812
        Py_DECREF(self->pos);
 
2813
        if (isNumber == 1) {
 
2814
                self->pos = PyNumber_Float(tmp);
 
2815
        self->modebuffer[4] = 0;
 
2816
        }
 
2817
        else {
 
2818
                self->pos = tmp;
 
2819
        streamtmp = PyObject_CallMethod((PyObject *)self->pos, "_getStream", NULL);
 
2820
        Py_INCREF(streamtmp);
 
2821
        Py_XDECREF(self->pos_stream);
 
2822
        self->pos_stream = (Stream *)streamtmp;
 
2823
                self->modebuffer[4] = 1;
 
2824
        }
 
2825
 
 
2826
        Py_INCREF(Py_None);
 
2827
        return Py_None;
 
2828
}       
 
2829
 
 
2830
static PyObject *
 
2831
Granule_setDur(Granule *self, PyObject *arg)
 
2832
{
 
2833
        PyObject *tmp, *streamtmp;
 
2834
        
 
2835
        if (arg == NULL) {
 
2836
                Py_INCREF(Py_None);
 
2837
                return Py_None;
 
2838
        }
 
2839
    
 
2840
        int isNumber = PyNumber_Check(arg);
 
2841
        
 
2842
        tmp = arg;
 
2843
        Py_INCREF(tmp);
 
2844
        Py_DECREF(self->dur);
 
2845
        if (isNumber == 1) {
 
2846
                self->dur = PyNumber_Float(tmp);
 
2847
        self->modebuffer[5] = 0;
 
2848
        }
 
2849
        else {
 
2850
                self->dur = tmp;
 
2851
        streamtmp = PyObject_CallMethod((PyObject *)self->dur, "_getStream", NULL);
 
2852
        Py_INCREF(streamtmp);
 
2853
        Py_XDECREF(self->dur_stream);
 
2854
        self->dur_stream = (Stream *)streamtmp;
 
2855
                self->modebuffer[5] = 1;
 
2856
        }
 
2857
 
 
2858
        Py_INCREF(Py_None);
 
2859
        return Py_None;
 
2860
}
 
2861
 
 
2862
static PyObject *
 
2863
Granule_getTable(Granule* self)
 
2864
{
 
2865
    Py_INCREF(self->table);
 
2866
    return self->table;
 
2867
};
 
2868
 
 
2869
static PyObject *
 
2870
Granule_setTable(Granule *self, PyObject *arg)
 
2871
{
 
2872
        PyObject *tmp;
 
2873
        
 
2874
        if (arg == NULL) {
 
2875
                Py_INCREF(Py_None);
 
2876
                return Py_None;
 
2877
        }
 
2878
    
 
2879
        tmp = arg;
 
2880
        Py_DECREF(self->table);
 
2881
    self->table = PyObject_CallMethod((PyObject *)tmp, "getTableStream", "");
 
2882
    
 
2883
        Py_INCREF(Py_None);
 
2884
        return Py_None;
 
2885
}       
 
2886
 
 
2887
static PyObject *
 
2888
Granule_getEnv(Granule* self)
 
2889
{
 
2890
    Py_INCREF(self->env);
 
2891
    return self->env;
 
2892
};
 
2893
 
 
2894
static PyObject *
 
2895
Granule_setEnv(Granule *self, PyObject *arg)
 
2896
{
 
2897
        PyObject *tmp;
 
2898
        
 
2899
        if (arg == NULL) {
 
2900
                Py_INCREF(Py_None);
 
2901
                return Py_None;
 
2902
        }
 
2903
    
 
2904
        tmp = arg;
 
2905
        Py_DECREF(self->env);
 
2906
    self->env = PyObject_CallMethod((PyObject *)tmp, "getTableStream", "");
 
2907
    
 
2908
        Py_INCREF(Py_None);
 
2909
        return Py_None;
 
2910
}       
 
2911
 
 
2912
static PyMemberDef Granule_members[] = {
 
2913
    {"server", T_OBJECT_EX, offsetof(Granule, server), 0, "Pyo server."},
 
2914
    {"stream", T_OBJECT_EX, offsetof(Granule, stream), 0, "Stream object."},
 
2915
    {"table", T_OBJECT_EX, offsetof(Granule, table), 0, "Sound table."},
 
2916
    {"env", T_OBJECT_EX, offsetof(Granule, env), 0, "Envelope table."},
 
2917
    {"dens", T_OBJECT_EX, offsetof(Granule, dens), 0, "Density of grains per second."},
 
2918
    {"pitch", T_OBJECT_EX, offsetof(Granule, pitch), 0, "Speed of the reading pointer."},
 
2919
    {"pos", T_OBJECT_EX, offsetof(Granule, pos), 0, "Position in the sound table."},
 
2920
    {"dur", T_OBJECT_EX, offsetof(Granule, dur), 0, "Duration of each grains."},
 
2921
    {"mul", T_OBJECT_EX, offsetof(Granule, mul), 0, "Mul factor."},
 
2922
    {"add", T_OBJECT_EX, offsetof(Granule, add), 0, "Add factor."},
 
2923
    {NULL}  /* Sentinel */
 
2924
};
 
2925
 
 
2926
static PyMethodDef Granule_methods[] = {
 
2927
    {"getTable", (PyCFunction)Granule_getTable, METH_NOARGS, "Returns sound table object."},
 
2928
    {"setTable", (PyCFunction)Granule_setTable, METH_O, "Sets sound table."},
 
2929
    {"getEnv", (PyCFunction)Granule_getEnv, METH_NOARGS, "Returns envelope table object."},
 
2930
    {"setEnv", (PyCFunction)Granule_setEnv, METH_O, "Sets envelope table."},
 
2931
    {"getServer", (PyCFunction)Granule_getServer, METH_NOARGS, "Returns server object."},
 
2932
    {"_getStream", (PyCFunction)Granule_getStream, METH_NOARGS, "Returns stream object."},
 
2933
    {"play", (PyCFunction)Granule_play, METH_VARARGS|METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
 
2934
    {"out", (PyCFunction)Granule_out, METH_VARARGS|METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
 
2935
    {"stop", (PyCFunction)Granule_stop, METH_NOARGS, "Stops computing."},
 
2936
        {"setDens", (PyCFunction)Granule_setDens, METH_O, "Sets the density of grains per second."},
 
2937
        {"setPitch", (PyCFunction)Granule_setPitch, METH_O, "Sets global pitch factor."},
 
2938
    {"setPos", (PyCFunction)Granule_setPos, METH_O, "Sets position in the sound table."},
 
2939
    {"setDur", (PyCFunction)Granule_setDur, METH_O, "Sets the grain duration."},
 
2940
        {"setMul", (PyCFunction)Granule_setMul, METH_O, "Sets Granule mul factor."},
 
2941
        {"setAdd", (PyCFunction)Granule_setAdd, METH_O, "Sets Granule add factor."},
 
2942
    {"setSub", (PyCFunction)Granule_setSub, METH_O, "Sets inverse add factor."},
 
2943
    {"setDiv", (PyCFunction)Granule_setDiv, METH_O, "Sets inverse mul factor."},
 
2944
    {NULL}  /* Sentinel */
 
2945
};
 
2946
 
 
2947
static PyNumberMethods Granule_as_number = {
 
2948
    (binaryfunc)Granule_add,                      /*nb_add*/
 
2949
    (binaryfunc)Granule_sub,                 /*nb_subtract*/
 
2950
    (binaryfunc)Granule_multiply,                 /*nb_multiply*/
 
2951
    (binaryfunc)Granule_div,                   /*nb_divide*/
 
2952
    0,                /*nb_remainder*/
 
2953
    0,                   /*nb_divmod*/
 
2954
    0,                   /*nb_power*/
 
2955
    0,                  /*nb_neg*/
 
2956
    0,                /*nb_pos*/
 
2957
    0,                  /*(unaryfunc)array_abs,*/
 
2958
    0,                    /*nb_nonzero*/
 
2959
    0,                    /*nb_invert*/
 
2960
    0,               /*nb_lshift*/
 
2961
    0,              /*nb_rshift*/
 
2962
    0,              /*nb_and*/
 
2963
    0,              /*nb_xor*/
 
2964
    0,               /*nb_or*/
 
2965
    0,                                          /*nb_coerce*/
 
2966
    0,                       /*nb_int*/
 
2967
    0,                      /*nb_long*/
 
2968
    0,                     /*nb_float*/
 
2969
    0,                       /*nb_oct*/
 
2970
    0,                       /*nb_hex*/
 
2971
    (binaryfunc)Granule_inplace_add,              /*inplace_add*/
 
2972
    (binaryfunc)Granule_inplace_sub,         /*inplace_subtract*/
 
2973
    (binaryfunc)Granule_inplace_multiply,         /*inplace_multiply*/
 
2974
    (binaryfunc)Granule_inplace_div,           /*inplace_divide*/
 
2975
    0,        /*inplace_remainder*/
 
2976
    0,           /*inplace_power*/
 
2977
    0,       /*inplace_lshift*/
 
2978
    0,      /*inplace_rshift*/
 
2979
    0,      /*inplace_and*/
 
2980
    0,      /*inplace_xor*/
 
2981
    0,       /*inplace_or*/
 
2982
    0,             /*nb_floor_divide*/
 
2983
    0,              /*nb_true_divide*/
 
2984
    0,     /*nb_inplace_floor_divide*/
 
2985
    0,      /*nb_inplace_true_divide*/
 
2986
    0,                     /* nb_index */
 
2987
};
 
2988
 
 
2989
PyTypeObject GranuleType = {
 
2990
    PyObject_HEAD_INIT(NULL)
 
2991
    0,                         /*ob_pitch*/
 
2992
    "_pyo.Granule_base",         /*tp_name*/
 
2993
    sizeof(Granule),         /*tp_basicpitch*/
 
2994
    0,                         /*tp_itempitch*/
 
2995
    (destructor)Granule_dealloc, /*tp_dealloc*/
 
2996
    0,                         /*tp_print*/
 
2997
    0,                         /*tp_getattr*/
 
2998
    0,                         /*tp_setattr*/
 
2999
    0,                         /*tp_compare*/
 
3000
    0,                         /*tp_repr*/
 
3001
    &Granule_as_number,             /*tp_as_number*/
 
3002
    0,                         /*tp_as_sequence*/
 
3003
    0,                         /*tp_as_mapping*/
 
3004
    0,                         /*tp_hash */
 
3005
    0,                         /*tp_call*/
 
3006
    0,                         /*tp_str*/
 
3007
    0,                         /*tp_getattro*/
 
3008
    0,                         /*tp_setattro*/
 
3009
    0,                         /*tp_as_buffer*/
 
3010
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/
 
3011
    "Granule objects. Accumulation of multiples grains of sound.",           /* tp_doc */
 
3012
    (traverseproc)Granule_traverse,   /* tp_traverse */
 
3013
    (inquiry)Granule_clear,           /* tp_clear */
 
3014
    0,                         /* tp_richcompare */
 
3015
    0,                         /* tp_weaklistoffset */
 
3016
    0,                         /* tp_iter */
 
3017
    0,                         /* tp_iternext */
 
3018
    Granule_methods,             /* tp_methods */
 
3019
    Granule_members,             /* tp_members */
 
3020
    0,                      /* tp_getset */
 
3021
    0,                         /* tp_base */
 
3022
    0,                         /* tp_dict */
 
3023
    0,                         /* tp_descr_get */
 
3024
    0,                         /* tp_descr_set */
 
3025
    0,                         /* tp_dictoffset */
 
3026
    0,      /* tp_init */
 
3027
    0,                         /* tp_alloc */
 
3028
    Granule_new,                 /* tp_new */
 
3029
};
2308
3030