Debian Codegraph @

src/core/ulib_ai.h

raw symbols SCIP
1 /*----------------------------------------------------------------------------
2 ChucK Strongly-timed Audio Programming Language
3 Compiler, Virtual Machine, and Synthesis Engine
4
5 Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.stanford.edu/
7 http://chuck.cs.princeton.edu/
8
9 This program is free software; you can redistribute it and/or modify
10 it under the dual-license terms of EITHER the MIT License OR the GNU
11 General Public License (the latter as published by the Free Software
12 Foundation; either version 2 of the License or, at your option, any
13 later version).
14
15 This program is distributed in the hope that it will be useful and/or
16 interesting, but WITHOUT ANY WARRANTY; without even the implied warranty
17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 MIT Licence and/or the GNU General Public License for details.
19
20 You should have received a copy of the MIT License and the GNU General
21 Public License (GPL) along with this program; a copy of the GPL can also
22 be obtained by writing to the Free Software Foundation, Inc., 59 Temple
23 Place, Suite 330, Boston, MA 02111-1307 U.S.A.
24 -----------------------------------------------------------------------------*/
25
26 //-----------------------------------------------------------------------------
27 // file: ulib_ai.h
28 // desc: Music and AI class library; part of ChAI (ChucK for AI)
29 //
30 // date: Fall 2022 -- present
31 // authors: Ge Wang ([email protected])
32 // Yikai Li ([email protected])
33 //
34 // (HISTORICAL NOTE)
35 // Much of ChAI and its ways of thinking and doing can be foundationally
36 // attributed to the work of Dr. Rebecca Fiebrink, her landmark Ph.D. Thesis,
37 // /Real-time Human Interaction With Supervised Learning Algorithms for Music
38 // Composition and Performance/ (2011) [1], the Wekinator framework [2], her
39 // teaching at the intersections of AI, HCI, and Music [3], as well as earlier
40 // collaborations between Rebecca and Ge [4,5] including SMIRK [6] (Small Musical
41 // Information Retrieval toolKit; 2008; MARSYAS and the MIR work of Dr. George
42 // Tzanetakis; early efforts in on-the-fly learning), the unit analyzer
43 // framework [7] (2007; afford real-time audio feature extraction). All of
44 // these have directly or indirectly contributed the creation of ChAI.
45 // Additionally, ChAI benefitted from the teaching and design philosophy of
46 // Dr. Perry R. Cook (Ph.D. advisor to Rebecca, George, and Ge at Princeton,
47 // and ChucK co-author), who argued for real-time human interaction, parametric
48 // sound synthesis, and the importance of play in the design of musical tools.
49 //
50 // [1] Fiebrink, Rebecca. 2011. /Real-time Human Interaction With Supervised
51 // Learning Algorithms for Music Composition and Performance/ Ph.D. Thesis.
52 // Princeton University. https://www.cs.princeton.edu/techreports/2010/891.pdf
53 //
54 // [2] http://www.wekinator.org/
55 // Wekinator | Software for real-time, interactive machine learning
56 //
57 // [3] Fiebrink, Rebecca. "Machine Learning for Musicians and Artists" MOOC
58 // https://www.kadenze.com/courses/machine-learning-for-musicians-and-artists/info
59 //
60 // [4] Fiebrink, R., G. Wang, P. R. Cook. 2008. "Foundations for On-the-fly
61 // Learning in the ChucK Programming Language." /International Computer Music
62 // Conference/. https://mcd.stanford.edu/publish/files/2008-icmc-learning.pdf
63 //
64 // [5] Fiebrink, R., G. Wang, P. R. Cook. 2008. "Support for Music Information
65 // Retrieval in the ChucK Programming Language." /International Conference
66 // on Music Information Retrieval/ (ISMIR).
67 // https://mcd.stanford.edu/publish/files/2008-ismir-proto.pdf
68 //
69 // [6] http://smirk.cs.princeton.edu/
70 // sMIRk | Small Music Information Retrieval toolKit
71 //
72 // [7] Tzanetakis, G. and P. R. Cook. 2000. "MARSYAS: A Framework for Audio
73 // Analysis." Organised Sound. 4:(3)
74 //
75 // [8] Tzanetakis, G. and P. R. Cook. 2002. "Musical Genre Classification of
76 // Audio Signals." IEEE Transaction on Speech and Audio Processing. 10(5).
77 //
78 // [9] Wang, G., R. Fiebrink, P. R. Cook. 2007. "Combining Analysis and
79 // Synthesis in the ChucK Programming Language." /International Computer
80 // Music Conference/. https://mcd.stanford.edu/publish/files/2007-icmc-uana.pdf
81 //-----------------------------------------------------------------------------
82 #ifndef __ULIB_AI_H__
83 #define __ULIB_AI_H__
84
85 #include "chuck_dl.h"
86 #include "chuck_errmsg.h"
87 #include "util_math.h"
88
89 #ifndef __CHUNREAL_ENGINE__
90 #define CK_TYPEID_CSTR(t) ( typeid( t ).name() )
91 #else
92 #define CK_TYPEID_CSTR(t) ( "[chunreal type info disabled]" )
93 #endif
94
95
96 // query
97 DLL_QUERY libai_query( Chuck_DL_Query * QUERY );
98
99 //-----------------------------------------------------------------------------
100 // name: class ChaiMatrixFast | 1.4.2.0 (ge) added
101 // desc: a templated fast access but not bound-checked 2D matrix
102 // allows arbitrary 2D arrays of arbitrary and non-constant dimensions
103 // to be allocated locally, e.g., int matrix[xDim][yDim] -- which is not
104 // valid syntax with some compilers.
105 //-----------------------------------------------------------------------------
106 template<class T>
107 class ChaiMatrixFast
108 {
109 public:
110 ChaiMatrixFast()
111 {
112 // zero out
113 m_matrix = NULL;
114 // zero out
115 m_xDim = m_yDim = 0;
116 }
117
118 ChaiMatrixFast( t_CKINT xDim, t_CKINT yDim )
119 {
120 // zero out
121 m_matrix = NULL;
122 // allocate
123 allocate( xDim, yDim );
124 }
125
126 ~ChaiMatrixFast()
127 {
128 // deallocate
129 cleanup();
130 }
131
132 t_CKBOOL allocate( t_CKINT xDim, t_CKINT yDim )
133 {
134 // delete if necessary
135 if( m_matrix != NULL ) cleanup();
136
137 // allocate
138 m_matrix = new T[xDim * yDim];
139 // check
140 if( !m_matrix )
141 {
142 // error
143 CK_FPRINTF_STDERR( "[chuck]: ChaiMatrixFast allocation failure: %s, %d x %d...\n",
144 CK_TYPEID_CSTR( T ), xDim, yDim );
145 // bail
146 return FALSE;
147 }
148 // remember
149 m_xDim = xDim;
150 m_yDim = yDim;
151
152 // done
153 return TRUE;
154 }
155
156 // get element value
157 T & v( t_CKINT x, t_CKINT y )
158 {
159 // at least do a check
160 if( x < 0 || x >= m_xDim || y < 0 || y >= m_yDim )
161 {
162 // error
163 CK_FPRINTF_STDERR( "[chuck]: ChaiMatrixFast out of bound: %s[%d][%d]...\n",
164 CK_TYPEID_CSTR( T ), x, y );
165 // halt
166 // TODO: this should throw (a shred-level, probably) exception
167 assert( FALSE );
168 }
169
170 return m_matrix[x * m_yDim + y];
171 }
172
173 // overloaded operator
174 T & operator()( t_CKINT x, t_CKINT y )
175 {
176 return v( x, y );
177 }
178
179 // get dimensions
180 t_CKINT xDim() const
181 { return m_xDim; }
182 t_CKINT yDim() const
183 { return m_yDim; }
184
185 void cleanup()
186 {
187 // reclaim array memory and zero the pointer
188 CK_SAFE_DELETE_ARRAY( m_matrix );
189 // zero out
190 m_xDim = m_yDim = 0;
191 }
192
193 protected:
194 T * m_matrix;
195 t_CKINT m_xDim;
196 t_CKINT m_yDim;
197 };
198
199 //-----------------------------------------------------------------------------
200 // name: class ChaiVectorFast | 1.4.2.0 (ge) added
201 // desc: a templated fast access but not bound-checked 1D array
202 // allows arbitrary arrays of arbitrary and non-constant dimension
203 // to be allocated locally, e.g., int array[dim] -- which is not
204 // valid syntax with some compilers.
205 //-----------------------------------------------------------------------------
206 template<class T>
207 class ChaiVectorFast
208 {
209 public:
210 ChaiVectorFast()
211 {
212 // zero out
213 m_vector = NULL;
214 // zero out
215 m_length = 0;
216 }
217
218 ChaiVectorFast( t_CKINT length )
219 {
220 // zero out
221 m_vector = NULL;
222 // allocate
223 allocate( length );
224 }
225
226 ~ChaiVectorFast()
227 {
228 // deallocate
229 cleanup();
230 }
231
232 t_CKBOOL allocate( t_CKINT length )
233 {
234 // delete if necessary
235 if( m_vector != NULL ) cleanup();
236
237 // allocate
238 m_vector = new T[length];
239 // check
240 if( !m_vector )
241 {
242 // error
243 CK_FPRINTF_STDERR( "[chuck]: ChaiVectorFast allocation failure: %s[%d]...\n",
244 CK_TYPEID_CSTR( T ), length );
245 // bail
246 return FALSE;
247 }
248 // remember
249 m_length = length;
250
251 // done
252 return TRUE;
253 }
254
255 void shuffle()
256 {
257 T temp;
258 t_CKINT j;
259 for( t_CKINT i = m_length - 1; i > 0; i-- )
260 {
261 j = ck_random() % ( i + 1 );
262 temp = m_vector[i];
263 m_vector[i] = m_vector[j];
264 m_vector[j] = temp;
265 }
266 }
267
268 t_CKUINT length()
269 { return m_length; }
270
271 t_CKINT size()
272 { return m_length; }
273
274 T getMax()
275 {
276 T max = m_vector[0];
277 for( t_CKINT i = 1; i < m_length; i++ )
278 {
279 if( m_vector[i] > max )
280 {
281 max = m_vector[i];
282 }
283 }
284 return max;
285 }
286
287 void setAll( T value )
288 {
289 // iterate
290 for( t_CKINT i = 0; i < m_length; i++ )
291 {
292 // set to value
293 m_vector[i] = value;
294 }
295 }
296
297 T & operator[]( t_CKINT i )
298 {
299 // at least do a check
300 if( i < 0 || i >= m_length )
301 {
302 // error
303 CK_FPRINTF_STDERR( "[chuck]: ChaiVectorFast out of bound: %s[%d] (capacity=%d)...\n",
304 CK_TYPEID_CSTR( T ), i, m_length );
305 // TODO: this should throw (a shred-level, probably) exception
306 assert( FALSE );
307 }
308
309 return m_vector[i];
310 }
311
312 // get element value
313 T & v( t_CKINT i )
314 {
315 return ( *this )[i];
316 }
317
318 void cleanup()
319 {
320 // reclaim array memory and zero the pointer
321 CK_SAFE_DELETE_ARRAY( m_vector );
322 // zero out
323 m_length = 0;
324 }
325
326 public:
327 T * m_vector;
328 t_CKINT m_length;
329 };
330
331 #endif
332
333 #ifndef _KD_TREE_H
334 #define _KD_TREE_H
335
336 #include <float.h>
337 // #define DBL_EPSILON 2.2204460492503131e-16
338
339 #define KDTREE_MAX_LEVEL 64
340 #define KDTREE_LEFT_INDEX 0
341 #define KDTREE_RIGHT_INDEX 1
342
343 typedef struct knn_list
344 {
345 struct kdnode * node;
346 double distance;
347 struct knn_list * prev;
348 struct knn_list * next;
349 } knn_list_t;
350
351 typedef struct kdnode
352 {
353 long coord_index;
354 double * coord;
355 struct kdnode * left;
356 struct kdnode * right;
357 t_CKINT r;
358 } kdnode_t;
359
360 typedef struct kdtree
361 {
362 struct kdnode * root;
363 size_t count;
364 size_t capacity;
365 double * coords;
366 double ** coord_table;
367 long * coord_indexes;
368 unsigned char * coord_deleted;
369 unsigned char * coord_passed;
370 struct knn_list knn_list_head;
371 t_CKINT dim;
372 t_CKINT knn_num;
373 } kdtree_t;
374
375 struct kdtree * kdtree_init( t_CKINT dim );
376 void kdtree_insert( struct kdtree * tree, double * coord );
377 void kdtree_rebuild( struct kdtree * tree );
378 void kdtree_knn_search( struct kdtree * tree, double * coord, t_CKINT k );
379 void kdtree_destroy( struct kdtree * tree );
380 void kdtree_dump( struct kdtree * tree );
381
382 #endif /* _KD_TREE_H */