~ubuntu-branches/ubuntu/natty/pd-zexy/natty

« back to all changes in this revision

Viewing changes to src/repack.c

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard, IOhannes m zmölnig, Jonas Smedegaard
  • Date: 2010-08-20 12:17:41 UTC
  • mfrom: (2.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100820121741-4kxozn8b9rhee9fr
Tags: 2.2.3-1
* New upstream version

[ IOhannes m zmölnig ]
* Adopt package, on behalf of Multimedia Team.
  Closes: #546964
* Simply debian/rules with CDBS, and don't unconditionally strip
  binaries.
  Closes: #437763
* Install into /usr/lib/pd/extra/zexy/. Document usage in REAME.Debian
  and warn about change in NEWS.
* git'ify package. Add Vcs-* stanzas to control file.
* Use dpkg source format 3.0 (quilt). Drop build-dependency on quilt.

[ Jonas Smedegaard ]
* Enable CDBS copyright-check routine.
* Add copyright and licensing header to debian/rules.
* Add myself as uploader.
* Rewrite debian/copyright using rev. 135 of draft DEP5 format.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************
 
2
 *
 
3
 * zexy - implementation file
 
4
 *
 
5
 * copyleft (c) IOhannes m zm�lnig
 
6
 *
 
7
 *   1999:forum::f�r::uml�ute:2004
 
8
 *
 
9
 *   institute of electronic music and acoustics (iem)
 
10
 *
 
11
 ******************************************************
 
12
 *
 
13
 * license: GNU General Public License v.2
 
14
 *
 
15
 ******************************************************/
 
16
 
 
17
/* 3108:forum::f�r::uml�ute:2000 */
 
18
 
 
19
/* objects for manipulating packages*/
 
20
 
 
21
/*
 
22
  repack    : (re)pack floats/symbols/pointers to fixed-length packages
 
23
*/
 
24
 
 
25
#include "zexy.h"
 
26
#include <string.h>
 
27
 
 
28
/* -------------------- repack ------------------------------ */
 
29
 
 
30
/*
 
31
(re)pack a sequence of (packages of) atoms into a package of given length
 
32
 
 
33
"bang" gives out the current package immediately
 
34
the second inlet lets you change the default package size
 
35
*/
 
36
 
 
37
static t_class *repack_class;
 
38
 
 
39
typedef struct _repack
 
40
{
 
41
  t_object x_obj;
 
42
  t_atom  *buffer;
 
43
  int      bufsize;
 
44
 
 
45
  int      outputsize;
 
46
  int      current;
 
47
} t_repack;
 
48
 
 
49
 
 
50
static void repack_set(t_repack *x, t_float f)
 
51
{
 
52
  /* set the new default size */
 
53
  int n = f;
 
54
 
 
55
  if (n > 0) {
 
56
 
 
57
    /* flush all the newsized packages that are in the buffer */
 
58
    t_atom *dumbuf = x->buffer;
 
59
    int     dumcur = x->current;
 
60
 
 
61
    while (n <= dumcur) {
 
62
      outlet_list(x->x_obj.ob_outlet, gensym("list"), n, dumbuf);
 
63
      dumcur -= n;
 
64
      dumbuf += n;
 
65
    }
 
66
 
 
67
    if (dumcur < 0) error("this should never happen :: dumcur = %d < 0", dumcur);
 
68
    else {
 
69
      memcpy(x->buffer, dumbuf, dumcur * sizeof(t_atom));
 
70
      x->current = dumcur;
 
71
    }
 
72
         
 
73
    if (n > x->bufsize) {
 
74
      dumbuf = (t_atom *)getbytes(n * sizeof(t_atom));
 
75
      memcpy(dumbuf, x->buffer, x->current * sizeof(t_atom));
 
76
      freebytes(x->buffer, x->bufsize * sizeof(t_atom));
 
77
      x->buffer =  dumbuf;
 
78
      x->bufsize = n;
 
79
    }
 
80
    
 
81
    x->outputsize = n;
 
82
  }
 
83
}
 
84
 
 
85
static void repack_bang(t_repack *x)
 
86
{
 
87
  /* output the list as far as we are now */
 
88
  outlet_list(x->x_obj.ob_outlet, gensym("list"), x->current, x->buffer);
 
89
  x->current = 0;
 
90
}
 
91
 
 
92
static void repack_float(t_repack *x, t_float f)
 
93
{
 
94
  /* add a float-atom to the list */
 
95
  SETFLOAT(&x->buffer[x->current], f);
 
96
  x->current++;
 
97
  if (x->current >= x->outputsize) repack_bang(x);
 
98
}
 
99
 
 
100
static void repack_symbol(t_repack *x, t_symbol *s)
 
101
{
 
102
  /* add a sym-atom to the list */
 
103
  SETSYMBOL(&x->buffer[x->current], s);
 
104
  x->current++;
 
105
  if (x->current >= x->outputsize) repack_bang(x);
 
106
}
 
107
static void repack_pointer(t_repack *x, t_gpointer *p)
 
108
{
 
109
  /* add a pointer-atom to the list */
 
110
  SETPOINTER(&x->buffer[x->current], p);
 
111
  x->current++;
 
112
  if (x->current >= x->outputsize) repack_bang(x);
 
113
}
 
114
static void repack_list(t_repack *x, t_symbol *s, int argc, t_atom *argv)
 
115
{
 
116
  int remain = x->outputsize - x->current;
 
117
  t_atom *ap = argv;
 
118
  ZEXY_USEVAR(s);
 
119
 
 
120
  if (argc >= remain) {
 
121
    memcpy(x->buffer+x->current, ap, remain * sizeof(t_atom));
 
122
    ap   += remain;
 
123
    argc -= remain;
 
124
    outlet_list(x->x_obj.ob_outlet, gensym("list"), x->outputsize, x->buffer);
 
125
    x->current = 0;
 
126
  }
 
127
 
 
128
  while (argc >= x->outputsize) {
 
129
    outlet_list(x->x_obj.ob_outlet, gensym("list"), x->outputsize, ap);
 
130
    ap += x->outputsize;
 
131
    argc -= x->outputsize;
 
132
  }
 
133
 
 
134
  memcpy(x->buffer + x->current, ap, argc * sizeof(t_atom));
 
135
  x->current += argc;
 
136
}
 
137
static void repack_anything(t_repack *x, t_symbol *s, int argc, t_atom *argv)
 
138
{
 
139
  SETSYMBOL(&x->buffer[x->current], s);
 
140
  x->current++;
 
141
 
 
142
  if (x->current >= x->outputsize) {
 
143
    repack_bang(x);
 
144
  }
 
145
  repack_list(x, gensym("list"), argc, argv);
 
146
}
 
147
 
 
148
static void *repack_new(t_floatarg f)
 
149
{
 
150
  t_repack *x = (t_repack *)pd_new(repack_class);
 
151
 
 
152
 
 
153
 
 
154
  x->outputsize = x->bufsize = (f>0.f)?f:2 ;
 
155
  x->current = 0;
 
156
 
 
157
 
 
158
  x->buffer = (t_atom *)getbytes(x->bufsize * sizeof(t_atom));
 
159
 
 
160
  inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym(""));
 
161
  outlet_new(&x->x_obj, 0);
 
162
 
 
163
  return (x);
 
164
}
 
165
 
 
166
void repack_setup(void)
 
167
{
 
168
  repack_class = class_new(gensym("repack"), (t_newmethod)repack_new, 
 
169
                           0, sizeof(t_repack), 0, A_DEFFLOAT, 0);
 
170
  
 
171
  class_addbang    (repack_class, repack_bang);
 
172
  class_addfloat   (repack_class, repack_float);
 
173
  class_addsymbol  (repack_class, repack_symbol);
 
174
  class_addpointer (repack_class, repack_pointer);
 
175
  class_addlist    (repack_class, repack_list);
 
176
  class_addanything(repack_class, repack_anything);
 
177
  class_addmethod  (repack_class, (t_method)repack_set, gensym(""), A_DEFFLOAT, 0);
 
178
 
 
179
  zexy_register("repack");
 
180
}