File: ndmetro.c

package info (click to toggle)
pd-moonlib 0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 828 kB
  • sloc: ansic: 5,454; makefile: 64; tcl: 22
file content (198 lines) | stat: -rw-r--r-- 5,044 bytes parent folder | download
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* ------------------------- ndndmetro   -----------------------------------------*/
/*                                                                              */
/*  Antoine Rousseau 2005-2016                                                       */
/*                                                                              */
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/
/* ---------------------------------------------------------------------------- */

#include "m_pd.h"
#include <stdio.h>
#include <string.h>
#ifdef UNIX
#include <stdlib.h>
#endif
#include <math.h>

static t_class *ndmetro_class;


typedef struct _ndmetro
{
    t_object x_obj;
    t_clock *x_clock;
    double x_tempo;
    double x_newtempo;
    double x_num;
    double x_denum;
    int x_tick;
    int x_hit;
    int x_on;
    double x_lasttime;
    double x_lastbeat;
    t_outlet *x_floatoutlet;
} t_ndmetro;

static void ndmetro_tick(t_ndmetro *x)
{
    double dtime,nextime,beat,tick=x->x_tick,tickk,fracpart=0;
    double intpart;

    x->x_hit = 0;

    if(tick>=0){
	outlet_float(x->x_obj.ob_outlet,fmod(tick,x->x_num));
	if (!x->x_hit) {
		clock_delay(x->x_clock, x->x_tempo/x->x_denum);
    		x->x_tick=tick+1;
	}
    }
    else{
    	dtime=clock_gettimesince(x->x_lasttime);
    	beat=x->x_lastbeat+dtime/x->x_tempo;
    	tick=beat*x->x_denum;
    	if(fabs((tickk=rint(tick))-tick)<1e-6) tick=tickk;

    	fracpart=modf(tick,&intpart);

   //fprintf(stderr,"metro_tick; beat=%.15g tick=%.15g fracpart=%.15g\n",beat,tick,fracpart);
    	if(!fracpart) outlet_float(x->x_obj.ob_outlet,fmod(tick,x->x_num));
	else  outlet_float(x->x_floatoutlet,fmod(tick,x->x_num));

    	if (!x->x_hit) {
    		x->x_tempo=x->x_newtempo;
    		x->x_lastbeat=beat;
    		x->x_lasttime=clock_getsystime();
    		nextime=x->x_tempo*(1.0-fracpart)/x->x_denum;
    		if(nextime<0.01) nextime=0.01;
		clock_delay(x->x_clock, nextime);
    		x->x_tick=floor(tick)+1;
	}
    }
}

static void ndmetro_bang(t_ndmetro *x)
{
    //post("metro_bang");
    x->x_tick=-1;
    if(x->x_on) ndmetro_tick(x);
    x->x_hit = 1;
}

static void ndmetro_list(t_ndmetro *x, t_symbol *s,  int ac, t_atom *av)
{
    double beat,tempo;
    t_atom *ap=av;

    //post("metro_list");

    if(ac<1) {
	ndmetro_bang(x);
	return;
    }

    if(ap->a_type!=A_FLOAT) {
	ndmetro_bang(x);
	return;
    }

    beat=atom_getfloat(ap);ap++;


    //fprintf(stderr,"beat=%g\n",beat);
    if(beat==-1) {
    	clock_unset(x->x_clock);
	x->x_on=0;
	x->x_hit = 1;
	//x->x_tick=0;
	return;
    }

    if((ac>1)&&(ap->a_type==A_FLOAT)) {
    	tempo=atom_getfloat(ap);
    	if(tempo<1) tempo=1;
    	x->x_newtempo=tempo;
    //fprintf(stderr,"tempo=%f\n",tempo);
    }

    x->x_lastbeat=beat;
    x->x_lasttime=clock_getsystime();
    x->x_on=1;

    ndmetro_bang(x);
}


static void ndmetro_nd(t_ndmetro *x, t_floatarg n,t_floatarg d)
{
    if(d<0.01) d=0.01;
    if(n<0.01) n=0.01;
    x->x_num=n;
    x->x_denum=d;
    x->x_tick=-1;
    //if(x->x_on) ndmetro_tick(x);
    ndmetro_bang(x);

}

static void ndmetro_tempo(t_ndmetro *x, t_floatarg t)
{
    if(t<1) t=1;
    x->x_newtempo=t;
    x->x_tick=-1;
    //if(x->x_on) ndmetro_tick(x);
    ndmetro_bang(x);
}

static void ndmetro_free(t_ndmetro *x)
{
    clock_free(x->x_clock);
}

static void *ndmetro_new(t_floatarg n, t_floatarg d)
{
    t_ndmetro *x = (t_ndmetro *)pd_new(ndmetro_class);

    if(d==0) d=1;
    if(n==0) n=1;
    if(d<0.01) d=0.01;
    if(n<0.01) n=0.01;
    x->x_num=n;
    x->x_denum=d;
    x->x_hit = 0;
    x->x_on = 0;
    x->x_clock = clock_new(x, (t_method)ndmetro_tick);
    x->x_tempo=x->x_newtempo=1000;
    x->x_lasttime=clock_getsystime();
    x->x_lastbeat=0;
    outlet_new(&x->x_obj, gensym("float"));
    x->x_floatoutlet=outlet_new(&x->x_obj, gensym("float"));
    //inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("ft1"));
    return (x);
}

void ndmetro_setup(void)
{
    ndmetro_class = class_new(gensym("ndmetro"), (t_newmethod)ndmetro_new,
        (t_method)ndmetro_free, sizeof(t_ndmetro), 0, A_DEFFLOAT,  A_DEFFLOAT, 0);
    class_addbang(ndmetro_class, (t_method)ndmetro_bang);
    class_addlist(ndmetro_class, (t_method)ndmetro_list);
    class_addmethod(ndmetro_class, (t_method)ndmetro_nd, gensym("nd"),
        A_FLOAT,A_FLOAT, 0);
    class_addmethod(ndmetro_class, (t_method)ndmetro_tempo, gensym("tempo"),
        A_FLOAT, 0);
}