File: piperead~.c

package info (click to toggle)
pd-ext13 0.17.1-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 392 kB
  • ctags: 638
  • sloc: ansic: 3,618; makefile: 251
file content (165 lines) | stat: -rw-r--r-- 3,717 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
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
#include "m_pd.h"

#ifdef __gnu_linux__
#include <sys/mman.h>
#endif
#include <sys/types.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

/* ------------------------ piperead_tilde~ ----------------------------- */

#ifdef _WIN32
#define BINREADMODE "rb"
#else
#define BINREADMODE "r"
#endif

static t_class *piperead_tilde_class;


typedef struct _piperead_tilde
{
     t_object x_obj;
     void*     x_mapaddr;
     int       x_fd;
     short x_sample;
     t_int   x_play;
     t_int   x_channels;
     int buflen;
     short  buf[32768];
     int  readpointer;
     int writepointer;
} t_piperead_tilde;


void piperead_tilde_open(t_piperead_tilde *x,t_symbol *filename)
{
/*     struct stat  fstate;*/
     char fname[MAXPDSTRING];
     canvas_makefilename(canvas_getcurrent(), filename->s_name,
			 fname, MAXPDSTRING);

     /* close the old file */
     if (x->x_fd >= 0) close(x->x_fd);

     if ((x->x_fd = open(fname,( O_NONBLOCK | O_RDONLY))) < 0)
     {
	  error("can't open %s",fname);
	  return;
     }

    for (x->writepointer=0;x->writepointer<(x->buflen*0.9);x->writepointer++){
      read (x->x_fd,&x->buf[x->writepointer],2);
    }
/*    post ("prebuffering done");*/
    
}

#define MAX_CHANS 4

t_int *piperead_tilde_perform(t_int *w)
{
    t_piperead_tilde*  x = (t_piperead_tilde*)(w[1]);
    int c = x->x_channels;
    int i;
    int erg=0;
    int n;
    t_float* out[MAX_CHANS];
    for (i=0;i<c;i++)      
      out[i] = (t_float *)(w[3+i]);
    n = (int)(w[3+c]);
        
    while (n--) 
    if (x->x_play){ 
      for (i=0;i<c;i++)  
      { 
        if (++x->readpointer>x->buflen){
          x->readpointer=0;
        } 
        if (erg != EAGAIN){
          if (++x->writepointer>x->buflen){
            x->writepointer=0;
          } 
        }
        *out[i]++ = x->buf[x->readpointer]/32768.;
        erg = read (x->x_fd,&x->buf[x->writepointer],2);         
      }
    }
    else
    {
      for (i=0;i<c;i++)  *out[i]++= 0.;
    }
    return (w+c+4);
}


static void piperead_tilde_float(t_piperead_tilde *x, t_floatarg f)
{
     int t = f;
     if (t) {
	  x->x_play=1;
     }
     else {
	  x->x_play=0;
     }

}

static void piperead_tilde_dsp(t_piperead_tilde *x, t_signal **sp)
{
     switch (x->x_channels) {
     case 1:
	  dsp_add(piperead_tilde_perform, 4, x, sp[0]->s_vec, 
		  sp[1]->s_vec, sp[0]->s_n);
	  break;
     case 2:
	  dsp_add(piperead_tilde_perform, 5, x, sp[0]->s_vec, 
		  sp[1]->s_vec,sp[2]->s_vec, sp[0]->s_n);
	  break;
     case 4:
	  dsp_add(piperead_tilde_perform, 6, x, sp[0]->s_vec, 
		  sp[1]->s_vec,sp[2]->s_vec,
		  sp[3]->s_vec,sp[4]->s_vec,
		  sp[0]->s_n);
	  break;
     }
}


static void *piperead_tilde_new(t_floatarg chan, t_floatarg buflen)
{
    t_piperead_tilde *x = (t_piperead_tilde *)pd_new(piperead_tilde_class);
    t_int c = chan;
    t_int bl = buflen;

    if (c<1 || c > MAX_CHANS) c = 1;
    if (bl<8 || bl > 32767) bl = 256;
    x->x_fd = -1;
    x->x_channels = c;
    x->buflen=bl;
    x->x_play = 0;

    while (c--) {
	 outlet_new(&x->x_obj, gensym("signal"));
    }
    return (x);
}

void piperead_tilde_setup(void)
{
    piperead_tilde_class = class_new(gensym("piperead~"), (t_newmethod)piperead_tilde_new, 0,
    	sizeof(t_piperead_tilde), 0,A_DEFFLOAT,A_DEFFLOAT,0);
    class_addmethod(piperead_tilde_class, nullfn, gensym("signal"), 0);
    
    class_addmethod(piperead_tilde_class, (t_method) piperead_tilde_dsp, gensym("dsp"), 0);
    class_addmethod(piperead_tilde_class, (t_method) piperead_tilde_open, gensym("open"), A_SYMBOL,A_NULL);
    class_addfloat(piperead_tilde_class, piperead_tilde_float);
}