aboutsummaryrefslogtreecommitdiff
path: root/control/qread.c
blob: 28112df3025fb8f04656a81fcf1f928d98d31c48 (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
/* (C) Guenter Geiger <geiger@epy.co.at> */


#include <m_pd.h>

/* ------------------------ qread ----------------------------- */

#include <stdio.h>

static t_class *qread_class;


#define MAXLINE 255

typedef struct _qread
{
     t_object x_obj;
     FILE* x_file;
     int x_size;
     t_clock*  x_clock;
     t_glist * x_glist;
     int x_num;
     t_symbol* x_name;
} t_qread;



static void qread_open(t_qread *x,t_symbol *filename)
{
     char fname[MAXPDSTRING];

     if (filename == &s_) {
	  post("sfread: open without filename");
	  return;
     }

     canvas_makefilename((void*)glist_getcanvas(x->x_glist), filename->s_name,
			 fname, MAXPDSTRING);


     /* close the old file */

     if (x->x_file) fclose(x->x_file);

     if (!(x->x_file = fopen(fname,"r")))
     {
	  error("can't open %s",fname);
	  return;
     }


}

void qread_next(t_qread *x)
{
     int i;
     float delay;
     char name[MAXLINE];
     t_atom at[20];
     int ac=0;
     t_floatarg ff;

     if (!x->x_file) return;

     fscanf(x->x_file,"%f",&delay);
     if (feof(x->x_file)) {
	  clock_unset(x->x_clock);
	  return;
     }

     fscanf(x->x_file,"%s",name);
#ifdef DEBUG
     post("next: name = %s delay = %f",name,delay);
#endif

     for (i=0;i<=x->x_num  && !feof(x->x_file);i++) {
	  fscanf(x->x_file,"%f",&ff);
	  SETFLOAT(at+i,ff);
     }
     ac = i-1;
     fscanf(x->x_file,";");


     clock_delay(x->x_clock,delay);
     
     outlet_list(x->x_obj.ob_outlet, gensym(name), ac, at);
}

static void qread_bang(t_qread *x)
{
     if (!x->x_file) return;
     
     fseek(x->x_file,0,SEEK_SET);
     clock_delay(x->x_clock,0);

#ifdef DEBUG
     post("bang");
#endif
}

static void qread_stop(t_qread *x)
{
     clock_unset(x->x_clock);
}

static void *qread_new(t_floatarg n)
{
    t_qread *x = (t_qread *)pd_new(qread_class);
    outlet_new(&x->x_obj, &s_float);

    x->x_name = gensym("qread");
    x->x_glist = (t_glist*) canvas_getcurrent();
    x->x_clock = clock_new(x, (t_method)qread_next);
    x->x_file = NULL;
    x->x_num = n;
    return (x);
}

void qread_setup(void)
{
    qread_class = class_new(gensym("qread"), (t_newmethod)qread_new, 0,
				sizeof(t_qread), 0,A_DEFFLOAT,A_NULL);
    class_addbang(qread_class,qread_bang);
    class_addmethod(qread_class,(t_method)qread_next,gensym("next"),A_NULL);
    class_addmethod(qread_class,(t_method)qread_open,gensym("open"),A_SYMBOL,A_NULL);
    class_addmethod(qread_class,(t_method)qread_stop,gensym("stop"),A_NULL);
}