comparison modules-and-plug-ins/max-external/btrack~.cpp @ 78:866024f9f95a

Added initial basic Max external and implemented getCurrentTempoEstimate() in BTrack.cpp
author Adam Stark <adamstark.uk@gmail.com>
date Mon, 24 Nov 2014 11:48:08 +0000
parents
children 9fd4075b8f9e
comparison
equal deleted inserted replaced
75:7d3622243e9a 78:866024f9f95a
1 //===========================================================================
2 /** @file btrack~.cpp
3 * @brief The btrack~ Max external
4 * @author Adam Stark
5 * @copyright Copyright (C) 2008-2014 Queen Mary University of London
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 //===========================================================================
21
22 //===========================================================================
23 #include "ext.h" // standard Max include, always required (except in Jitter)
24 #include "ext_obex.h" // required for "new" style objects
25 #include "z_dsp.h" // required for MSP objects
26
27 //===========================================================================
28 // BTrack includes
29 #include "../../src/BTrack.h"
30 #include "../../src/OnsetDetectionFunction.h"
31
32 //===========================================================================
33 // struct to represent the object's state
34 typedef struct _btrack {
35
36 // The object itself (t_pxobject in MSP instead of t_object)
37 t_pxobject ob;
38
39 // An instance of the BTrack beat tracker
40 BTrack *b;
41
42 // An outlet for beats
43 void *beat_outlet;
44
45 // An outlet for tempo estimates
46 void *tempo_outlet;
47
48 } t_btrack;
49
50
51 //===========================================================================
52 // method prototypes
53 void *btrack_new(t_symbol *s, long argc, t_atom *argv);
54 void btrack_free(t_btrack *x);
55 void btrack_assist(t_btrack *x, void *b, long m, long a, char *s);
56 void btrack_float(t_btrack *x, double f);
57 void btrack_dsp(t_btrack *x, t_signal **sp, short *count);
58 void btrack_dsp64(t_btrack *x, t_object *dsp64, short *count, double samplerate, long maxvectorsize, long flags);
59 t_int *btrack_perform(t_int *w);
60 void btrack_perform64(t_btrack *x, t_object *dsp64, double **ins, long numins, double **outs, long numouts, long sampleframes, long flags, void *userparam);
61
62 //===========================================================================
63 void btrack_process(t_btrack *x,double* audioFrame);
64 void outlet_beat(t_btrack *x, t_symbol *s, long argc, t_atom *argv);
65
66 // global class pointer variable
67 static t_class *btrack_class = NULL;
68
69
70
71
72 //===========================================================================
73 int C74_EXPORT main(void)
74 {
75 // object initialization, note the use of dsp_free for the freemethod, which is required
76 // unless you need to free allocated memory, in which case you should call dsp_free from
77 // your custom free function.
78
79 t_class *c = class_new("btrack~", (method)btrack_new, (method)dsp_free, (long)sizeof(t_btrack), 0L, A_GIMME, 0);
80
81 class_addmethod(c, (method)btrack_float, "float", A_FLOAT, 0);
82 class_addmethod(c, (method)btrack_dsp, "dsp", A_CANT, 0); // Old 32-bit MSP dsp chain compilation for Max 5 and earlier
83 class_addmethod(c, (method)btrack_dsp64, "dsp64", A_CANT, 0); // New 64-bit MSP dsp chain compilation for Max 6
84 class_addmethod(c, (method)btrack_assist, "assist", A_CANT, 0);
85
86 class_dspinit(c);
87 class_register(CLASS_BOX, c);
88 btrack_class = c;
89
90 return 0;
91 }
92
93
94 //===========================================================================
95 void *btrack_new(t_symbol *s, long argc, t_atom *argv)
96 {
97 t_btrack *x = (t_btrack *)object_alloc(btrack_class);
98
99 if (x) {
100 dsp_setup((t_pxobject *)x, 1); // MSP inlets: arg is # of inlets and is REQUIRED!
101 // use 0 if you don't need inlets
102
103 object_post((t_object *) x,"v1.0 designed by Adam Stark and Matthew Davies at Queen Mary University of London");
104
105 // create detection function and beat tracking objects
106 x->b = new BTrack();
107
108 x->tempo_outlet = floatout(x);
109 x->beat_outlet = bangout(x);
110
111 /*
112
113
114 x->mode = 0;
115 x->lastbang = 0;
116
117 x->dobeats = 1;
118 x->countin = 4;
119
120 x->counttempi[0] = 120;
121 x->counttempi[1] = 120;
122 x->counttempi[2] = 120;
123 */
124 }
125 return (x);
126 }
127
128
129 //===========================================================================
130 // NOT CALLED!, we use dsp_free for a generic free function
131 void btrack_free(t_btrack *x)
132 {
133 ;
134 }
135
136
137 //===========================================================================
138 void btrack_assist(t_btrack *x, void *b, long m, long a, char *s)
139 {
140 if (m == ASSIST_INLET) { //inlet
141 sprintf(s, "I am inlet %ld", a);
142 }
143 else { // outlet
144 sprintf(s, "I am outlet %ld", a);
145 }
146 }
147
148
149 //===========================================================================
150 void btrack_float(t_btrack *x, double f)
151 {
152
153
154 }
155
156 //===========================================================================
157 // this function is called when the DAC is enabled, and "registers" a function for the signal chain in Max 5 and earlier.
158 // In this case we register the 32-bit, "btrack_perform" method.
159 void btrack_dsp(t_btrack *x, t_signal **sp, short *count)
160 {
161 int hopSize = (int) sp[0]->s_n;
162 int frameSize = hopSize*2;
163
164 x->b->updateHopAndFrameSize(hopSize, frameSize);
165
166 dsp_add(btrack_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
167 }
168
169
170 //===========================================================================
171 // this is the Max 6 version of the dsp method -- it registers a function for the signal chain in Max 6,
172 // which operates on 64-bit audio signals.
173 void btrack_dsp64(t_btrack *x, t_object *dsp64, short *count, double samplerate, long maxvectorsize, long flags)
174 {
175 int hopSize = (int) maxvectorsize;
176 int frameSize = hopSize*2;
177
178 x->b->updateHopAndFrameSize(hopSize, frameSize);
179
180 object_method(dsp64, gensym("dsp_add64"), x, btrack_perform64, 0, NULL);
181 }
182
183
184 //===========================================================================
185 // this is the 32-bit perform method for Max 5 and earlier
186 t_int *btrack_perform(t_int *w)
187 {
188 t_btrack *x = (t_btrack *)(w[1]);
189 t_float *inL = (t_float *)(w[2]);
190 int n = (int)w[3];
191
192 double audioFrame[n];
193
194 for (int i = 0;i < n;i++)
195 {
196 audioFrame[i] = (double) inL[i];
197 }
198
199 btrack_process(x,audioFrame);
200
201 // you have to return the NEXT pointer in the array OR MAX WILL CRASH
202 return w + 4;
203 }
204
205 //===========================================================================
206 // this is 64-bit perform method for Max 6
207 void btrack_perform64(t_btrack *x, t_object *dsp64, double **ins, long numins, double **outs, long numouts, long sampleframes, long flags, void *userparam)
208 {
209 t_double *inL = ins[0]; // we get audio for each inlet of the object from the **ins argument
210 int n = sampleframes;
211
212 double audioFrame[n];
213
214 for (int i = 0;i < n;i++)
215 {
216 audioFrame[i] = (double) inL[i];
217 }
218
219 btrack_process(x,audioFrame);
220 }
221
222 //===========================================================================
223 void btrack_process(t_btrack *x,double* audioFrame)
224 {
225 // process the audio frame
226 x->b->processAudioFrame(audioFrame);
227
228
229 // if there is a beat in this frame
230 if (x->b->beatDueInCurrentFrame())
231 {
232 // outlet a beat
233 defer_low((t_object *)x, (method)outlet_beat, NULL, 0, NULL);
234 }
235 }
236
237 //===========================================================================
238 void outlet_beat(t_btrack *x, t_symbol *s, long argc, t_atom *argv)
239 {
240 // send a bang out of the beat outlet
241 outlet_bang(x->beat_outlet);
242
243 // send the tempo out of the tempo outlet
244 outlet_float(x->tempo_outlet, (float) x->b->getCurrentTempoEstimate());
245 }
246
247
248
249
250
251
252
253
254
255
256