File: test_libpd.c

package info (click to toggle)
puredata 0.55.2%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 20,508 kB
  • sloc: ansic: 118,824; tcl: 10,221; cpp: 9,327; makefile: 1,632; sh: 1,476; python: 152; xml: 98; awk: 13
file content (94 lines) | stat: -rw-r--r-- 2,588 bytes parent folder | download | duplicates (3)
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
/*
    test_libpd: test libpd by creating two Pd instances and running them
    simultaneously.  The test patch has a phasor~ object - in one instance
    it runs forward, and in the other, backward.
*/

#include <stdio.h>
#include "z_libpd.h"

void pdprint(const char *s) {
  printf("%s", s);
}

void pdnoteon(int ch, int pitch, int vel) {
  printf("noteon: %d %d %d\n", ch, pitch, vel);
}

int main(int argc, char **argv) {
    t_pdinstance *pd1, *pd2;
    int srate = 48000;
        /* one input channel, two output channels,
           block size 64, one tick per buffer: */
    float inbuf[64], outbuf[128];
    char *filename = "test_libpd.pd", *dirname = ".";

    /* accept overrides from the commandline:
       $ pdtest_multi file.pd ../dir */
    if (argc > 1) filename = argv[1];
    if (argc > 2) dirname = argv[2];

        /* maybe these two calls should be available per-instance somehow: */
    libpd_set_printhook(pdprint);
    libpd_set_noteonhook(pdnoteon);

    libpd_init();

    pd1 = libpd_new_instance();
    pd2 = libpd_new_instance();

    libpd_set_instance(pd1); /* talk to first pd instance */

    libpd_init_audio(1, 2, srate);
        /* send message:  [; pd dsp 1(   */
    libpd_start_message(1); /* one entry in list */
    libpd_add_float(1.0f);
    libpd_finish_message("pd", "dsp");

        /* open the file  */
    libpd_openfile(filename, dirname);

        /* repeat this all for the second instance */
    libpd_set_instance(pd2);
    libpd_init_audio(1, 2, srate);
    libpd_start_message(1);
    libpd_add_float(1.0f);
    libpd_finish_message("pd", "dsp");
    libpd_openfile(filename, dirname);

    libpd_set_instance(pd1);
    /* [; pd frequency 480 ( */
    libpd_start_message(1);
    libpd_add_float(480.0f);
    libpd_finish_message("frequency", "float");

    libpd_set_instance(pd2);
    /* [; pd frequency -480 ( */
    libpd_start_message(1);
    libpd_add_float(-480.0f);
    libpd_finish_message("frequency", "float");

    /* now run pd for 3 ticks */
    int i, j;
    for (i = 0; i < 3; i++)
    {
        libpd_set_instance(pd1);
        libpd_process_float(1, inbuf, outbuf);
        printf("instance 1, tick %d:\n", i);
        for (j = 0; j < 8; j++)
            printf("%f ", outbuf[j]);
        printf("... \n");

        libpd_set_instance(pd2);
        libpd_process_float(1, inbuf, outbuf);
        printf("instance 2, tick %d:\n", i);
        for (j = 0; j < 8; j++)
            printf("%f ", outbuf[j]);
        printf("... \n");
    }

    libpd_free_instance(pd1);
    libpd_free_instance(pd2);

    return 0;
}