*** empty log message ***
[chuck-blob.git] / v2 / util_opsc.h
blob6a3b99e0d04b56c2437027b0b58d36e7b1578699
1 /*----------------------------------------------------------------------------
2 ChucK Concurrent, On-the-fly Audio Programming Language
3 Compiler and Virtual Machine
5 Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.cs.princeton.edu/
7 http://soundlab.cs.princeton.edu/
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 U.S.A.
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
26 // file: util_opsc.h
27 // desc: ...
29 // author: Philip L. Davidson (philipd@alumni.princeton.edu)
30 // Ge Wang (gewang@cs.princeton.edu)
31 // Perry R. Cook (prc@cs.princeton.edu)
32 // date: Spring 2005
33 //-----------------------------------------------------------------------------
34 #ifndef _OSC_H_INCLUDED_
35 #define _OSC_H_INCLUDED_
37 // chuck high-style artistry
38 // let's just cat included files into an enormous code-wad.
39 // keep that bubble gum in your hair.
41 // the included files were from the CNMAT OSC-Kit distrib.
43 #ifdef __PLATFORM_WIN32__
44 #include <windows.h>
45 #endif
47 //#include "OSC-pattern-match.h"
48 // OSC-pattern-match.h
51 Copyright © 1998. The Regents of the University of California (Regents).
52 All Rights Reserved.
54 Written by Matt Wright, The Center for New Music and Audio Technologies,
55 University of California, Berkeley.
57 Permission to use, copy, modify, distribute, and distribute modified versions
58 of this software and its documentation without fee and without a signed
59 licensing agreement, is hereby granted, provided that the above copyright
60 notice, this paragraph and the following two paragraphs appear in all copies,
61 modifications, and distributions.
63 IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
64 SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
65 OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
66 BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68 REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
69 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
70 PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
71 HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
72 MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
74 The OpenSound Control WWW page is
75 http://www.cnmat.berkeley.edu/OpenSoundControl
79 #ifndef __OSC_PATTERN_MATCH_H_INCLUDED__
80 #define __OSC_PATTERN_MATCH_H_INCLUDED__
82 #include <cstdio>
84 OSC-pattern-match.h
86 #ifndef TRUE
87 #define TRUE true
88 #define FALSE false
89 #endif
91 bool PatternMatch (const char *pattern, const char *test);
93 #endif //__OSC_PATTERN_MATCH_H_INCLUDED__
95 // - end OSC-pattern-match.h
97 //#inclue OSC-TimeTag.h
100 Copyright (c) 1998. The Regents of the University of California (Regents).
101 All Rights Reserved.
103 Permission to use, copy, modify, and distribute this software and its
104 documentation for educational, research, and not-for-profit purposes, without
105 fee and without a signed licensing agreement, is hereby granted, provided that
106 the above copyright notice, this paragraph and the following two paragraphs
107 appear in all copies, modifications, and distributions. Contact The Office of
108 Technology Licensing, UC Berkeley, 2150 Shattuck Avenue, Suite 510, Berkeley,
109 CA 94720-1620, (510) 643-7201, for commercial licensing opportunities.
111 Written by Matt Wright, The Center for New Music and Audio Technologies,
112 University of California, Berkeley.
114 IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
115 SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
116 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
117 REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
119 REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
120 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
121 FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING
122 DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
123 REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
124 ENHANCEMENTS, OR MODIFICATIONS.
126 The OpenSound Control WWW page is
127 http://www.cnmat.berkeley.edu/OpenSoundControl
131 OSC_timeTag.h: library for manipulating OSC time tags
132 Matt Wright, 5/29/97
134 Time tags in OSC have the same format as in NTP: 64 bit fixed point, with the
135 top 32 bits giving number of seconds sinve midnight 1/1/1900 and the bottom
136 32 bits giving fractional parts of a second. We represent this by a 64-bit
137 unsigned long if possible, or else a struct.
139 NB: On many architectures with 64-bit ints, it's illegal (like maybe a bus error)
140 to dereference a pointer to a 64-bit int that's not 64-bit aligned.
143 #ifndef OSC_TIMETAG
144 #define OSC_TIMETAG
146 #ifdef __sgi
147 #define HAS8BYTEINT
148 /* You may have to change this typedef if there's some other
149 way to specify 64 bit ints on your system */
150 typedef long long int64;
151 typedef unsigned long long uint64;
152 typedef unsigned long uint32;
153 #else
154 /* You may have to redefine this typedef if ints on your system
155 aren't 32 bits. */
156 typedef unsigned int uint32;
157 #endif
160 #ifdef HAS8BYTEINT
161 typedef uint64 OSCTimeTag;
162 #else
163 typedef struct {
164 uint32 seconds;
165 uint32 fraction;
166 } OSCTimeTag;
167 #endif
171 /* Return a time tag representing the current time (as of when this
172 procedure is called). */
173 OSCTimeTag OSCTT_CurrentTime(void);
175 /* Return the time tag 0x0000000000000001, indicating to the receiving device
176 that it should process the message immediately. */
177 OSCTimeTag OSCTT_Immediately(void);
179 /* Return the time tag 0xffffffffffffffff, a time so far in the future that
180 it's effectively infinity. */
181 OSCTimeTag OSCTT_BiggestPossibleTimeTag(void);
183 /* Given a time tag and a number of seconds to add to the time tag, return
184 the new time tag */
185 OSCTimeTag OSCTT_PlusSeconds(OSCTimeTag original, float secondsOffset);
187 /* Compare two time tags. Return negative if first is < second, 0 if
188 they're equal, and positive if first > second. */
189 int OSCTT_Compare(OSCTimeTag left, OSCTimeTag right);
191 #endif /* OSC_TIMETAG */
194 //#include "OSC-client.h"
195 // OSC-client.h
197 Copyright (c) 1996,1997. The Regents of the University of California (Regents).
198 All Rights Reserved.
200 Permission to use, copy, modify, and distribute this software and its
201 documentation for educational, research, and not-for-profit purposes, without
202 fee and without a signed licensing agreement, is hereby granted, provided that
203 the above copyright notice, this paragraph and the following two paragraphs
204 appear in all copies, modifications, and distributions. Contact The Office of
205 Technology Licensing, UC Berkeley, 2150 Shattuck Avenue, Suite 510, Berkeley,
206 CA 94720-1620, (510) 643-7201, for commercial licensing opportunities.
208 Written by Matt Wright, The Center for New Music and Audio Technologies,
209 University of California, Berkeley.
211 IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
212 SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
213 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
214 REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
216 REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
217 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
218 FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING
219 DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
220 REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
221 ENHANCEMENTS, OR MODIFICATIONS.
226 OSC-client.h: library for constructing OpenSoundControl messages.
227 Derived from SynthControl.h
228 Author: Matt Wright
229 Version 0.1: 6/13/97
230 Version 0.2: 7/21/2000: Support for type-tagged messages
233 General notes:
235 This library abstracts away the data format for the OpenSoundControl
236 protocol. Users of this library can construct OpenSoundControl packets
237 with a function call interface instead of knowing how to lay out the bits.
239 All issues of memory allocation are deferred to the user of this library.
240 There are two data structures that the user must allocate. The first
241 is the actual buffer that the message will be written into. This buffer
242 can be any size, but if it's too small there's a possibility that it
243 will become overfull. The other data structure is called an OSCbuf,
244 and it holds all the state used by the library as it's constructing
245 a buffer.
247 All procedures that have the possibility of an error condition return int,
248 with 0 indicating no error and nonzero indicating an error. The variable
249 OSC_errorMessage will be set to point to a string containing an error
250 message explaining what the problem is.
254 #include <cstdio>
255 //#include "sys.h"
257 //#include "OSC-timetag.h"
259 /* The int4byte type has to be a 4-byte integer. You may have to
260 change this to long or something else on your system. */
261 #ifdef __MWERKS__
262 /* In Metrowerks you can set ints to be 2 or 4 bytes on 68K, but long is
263 always 4 bytes */
264 typedef long int4byte;
265 // ISSUE: 64-bit
266 #else
267 typedef int int4byte;
268 // ISSUE: 64-bit
269 #endif
271 /* The maximum depth of bundles within bundles within bundles within...
272 This is the size of a static array. If you exceed this limit you'll
273 get an error message. */
274 #define MAX_BUNDLE_NESTING 32
277 /* Don't ever manipulate the data in the OSCbuf struct directly. (It's
278 declared here in the header file only so your program will be able to
279 declare variables of type OSCbuf and have the right amount of memory
280 be allocated.) */
282 typedef struct OSCbuf_struct {
283 char *buffer; /* The buffer to hold the OSC packet */
284 int size; /* Size of the buffer */
285 char *bufptr; /* Current position as we fill the buffer */
286 int state; /* State of partially-constructed message */
287 int4byte *thisMsgSize; /* Pointer to count field before
288 currently-being-written message */
289 int4byte *prevCounts[MAX_BUNDLE_NESTING];
290 /* Pointers to count field before each currently
291 open bundle */
292 int bundleDepth; /* How many sub-sub-bundles are we in now? */
293 char *typeStringPtr; /* This pointer advances through the type
294 tag string as you add arguments. */
295 int gettingFirstUntypedArg; /* nonzero if this message doesn't have
296 a type tag and we're waiting for the 1st arg */
297 } OSCbuf;
301 /* Initialize the given OSCbuf. The user of this module must pass in the
302 block of memory that this OSCbuf will use for a buffer, and the number of
303 bytes in that block. (It's the user's job to allocate the memory because
304 you do it differently in different systems.) */
305 void OSC_initBuffer(OSCbuf *buf, int size, char *byteArray);
308 /* Reset the given OSCbuf. Do this after you send out the contents of
309 the buffer and want to start writing new data into it. */
310 void OSC_resetBuffer(OSCbuf *buf);
313 /* Is the buffer empty? (I.e., would it be stupid to send the buffer
314 contents to the synth?) */
315 int OSC_isBufferEmpty(OSCbuf *buf);
318 /* How much space is left in the buffer? */
319 int OSC_freeSpaceInBuffer(OSCbuf *buf);
321 /* Does the buffer contain a valid OSC packet? (Returns nonzero if yes.) */
322 int OSC_isBufferDone(OSCbuf *buf);
324 /* When you're ready to send out the buffer (i.e., when OSC_isBufferDone()
325 returns true), call these two procedures to get the OSC packet that's been
326 assembled and its size in bytes. (And then call OSC_resetBuffer() if you
327 want to re-use this OSCbuf for the next packet.) */
328 char *OSC_getPacket(OSCbuf *buf);
329 int OSC_packetSize(OSCbuf *buf);
333 /* Here's the basic model for building up OSC messages in an OSCbuf:
335 - Make sure the OSCbuf has been initialized with OSC_initBuffer().
337 - To open a bundle, call OSC_openBundle(). You can then write
338 messages or open new bundles within the bundle you opened.
339 Call OSC_closeBundle() to close the bundle. Note that a packet
340 does not have to have a bundle; it can instead consist of just a
341 single message.
344 - For each message you want to send:
346 - Call OSC_writeAddress() with the name of your message. (In
347 addition to writing your message name into the buffer, this
348 procedure will also leave space for the size count of this message.)
350 - Alternately, call OSC_writeAddressAndTypes() with the name of
351 your message and with a type string listing the types of all the
352 arguments you will be putting in this message.
354 - Now write each of the arguments into the buffer, by calling one of:
355 OSC_writeFloatArg()
356 OSC_writeFloatArgs()
357 OSC_writeIntArg()
358 OSC_writeStringArg()
360 - Now your message is complete; you can send out the buffer or you can
361 add another message to it.
364 int OSC_openBundle(OSCbuf *buf, OSCTimeTag tt);
365 int OSC_closeBundle(OSCbuf *buf);
366 int OSC_closeAllBundles(OSCbuf *buf);
368 int OSC_writeAddress(OSCbuf *buf, char *name);
369 int OSC_writeAddressAndTypes(OSCbuf *buf, char *name, char *types);
370 int OSC_writeFloatArg(OSCbuf *buf, float arg);
371 int OSC_writeFloatArgs(OSCbuf *buf, int numFloats, float *args);
372 int OSC_writeIntArg(OSCbuf *buf, int4byte arg);
373 int OSC_writeStringArg(OSCbuf *buf, char *arg);
375 extern const char * OSC_errorMessage;
377 /* How many bytes will be needed in the OSC format to hold the given
378 string? The length of the string, plus the null char, plus any padding
379 needed for 4-byte alignment. */
380 int OSC_effectiveStringLength(char *string);
382 // end OSC-client.h
384 #include "stdlib.h"
385 #include "string.h"
387 // from veldt:platform.h - UDP Transmitter / Receiver Pair
389 #include "chuck_oo.h"
391 class OSC_Address_Space;
392 class UDP_Transmitter;
393 class UDP_Receiver;
395 class OSC_Transmitter
397 protected:
398 char _buffer[2048];
399 OSCbuf _osc;
400 UDP_Transmitter * _out;
401 bool _holdMessage;
403 public:
404 OSC_Transmitter();
405 OSC_Transmitter( UDP_Transmitter * out) ;
406 virtual ~OSC_Transmitter();
408 void init();
409 void setHost( char * host, int port);
410 void addMessage( char * address, char * args, ...);
412 void openBundle( OSCTimeTag t);
413 void closeBundle();
415 void startMessage( char * spec );
416 void startMessage( char * address, char * args );
417 void addInt( int4byte i ); // gewang: changed from int
418 void addFloat( float f );
419 void addString( char * s);
420 bool packetReady();
421 void holdMessage( bool arg );
422 void tryMessage();
423 void kickMessage();
425 void presend( char * buffer , int size );
429 #define OSCINBUFSIZE 8192
430 #define OSCMESGSIZE 8192
432 //default size for array of osc buffers ( ~8k each )
433 #define OSCINBOXDEF 2
434 //maximum size for the array ( ~2mb is enough, no )
435 #define OSCINBOXMAX 256
437 struct OSCMesg {
438 char *address;
439 char *types;
440 char *data;
441 char *payload;
442 int len;
443 double recvtime;
444 long int timetag;
447 struct XMutex;
448 struct XThread;
450 class UDP_Subscriber
452 public:
453 virtual ~UDP_Subscriber() {}
455 public:
456 virtual int& port() = 0; // get/set the value of the subscriber's current port.
457 virtual void onReceive( char * mesg, int mesgLen ) = 0;
459 protected:
460 virtual bool subscribe( int port );
461 virtual bool unsubscribe();
465 class OSC_Receiver : private UDP_Subscriber
467 protected:
468 // UDP_Receiver* _in;
470 // bool _listening;
471 // char _inbuf[OSCINBUFSIZE];
472 // int _inbufsize;
473 // int _mesglen;
474 int _port;
475 int _tmp_port;
476 void onReceive( char * mesg, int mesgLen);
477 int & port();
479 XMutex* _io_mutex;
480 // XThread* _io_thread;
481 OSCMesg _meep;
482 OSCMesg *_inbox;
483 int _inbox_size;
484 bool _started;
485 int _in_read; //space that is being read
486 int _in_write; //space that is being written
488 OSC_Address_Space ** _address_space;
489 int _address_size;
490 int _address_num;
492 public:
494 OSC_Receiver();
495 OSC_Receiver(UDP_Receiver* in);
496 virtual ~OSC_Receiver();
498 //setup //takedown
499 void init();
500 void bind_to_port(int port = 0);
501 void close_sock();
503 void recv_mesg();
505 bool listen( int port );
506 bool listen();
507 void stopListening(); //unsubscribe;
509 void parse(char *, int len);
510 void handle_mesg(char *, int len);
511 void handle_bundle(char *, int len);
512 void set_mesg(OSCMesg *m, char * buf, int len );
514 OSCMesg *write() { return _inbox + _in_write;}
515 void next_write();
517 OSCMesg *read() { return _inbox + _in_read;}
518 OSCMesg *next_read();
521 bool has_mesg();
522 bool get_mesg(OSCMesg* bucket);
524 void add_address ( OSC_Address_Space * o );
525 void remove_address ( OSC_Address_Space * o );
526 OSC_Address_Space * new_event ( char * spec );
527 OSC_Address_Space * new_event ( char * addr, char * type );
528 void distribute_message( OSCMesg * msg);
531 enum osc_datatype { OSC_UNTYPED, OSC_NOARGS, OSC_INT, OSC_FLOAT, OSC_STRING, OSC_BLOB, OSC_NTYPE };
533 struct opsc_data
535 osc_datatype t;
536 OSCTimeTag timetag;
538 union {
539 int4byte i; // gewang: changed from int
540 unsigned long u; // gewang: changed from unsigned int
541 float f;
544 char * s;
546 opsc_data() {
547 t = OSC_UNTYPED;
548 s = NULL;
550 ~opsc_data() {
551 s = NULL;
555 #define OSC_ADDRESS_QUEUE_MAX 32768
556 //if it's bigger than this, something's not doing it's job...
557 //so we'll drop the message and send a friendly warning via EM_log
559 typedef class OSC_Address_Space OSCSrc;
562 class OSC_Address_Space : public Chuck_Event
564 protected:
565 OSC_Receiver * _receiver;
566 XMutex* _buffer_mutex;
567 char _spec[512];
568 bool _needparse;
569 char _address[512];
570 char _type[512];
571 opsc_data * _queue;
572 opsc_data * _current_data;
573 int _qread;
574 int _qwrite;
575 int _queueSize;
576 int _cur_value;
577 opsc_data *_cur_mesg;
578 opsc_data *_vals;
579 int _dataSize;
580 bool _noArgs;
581 void resizeData(int n);
582 void resizeQueue(int n);
583 void parseSpec();
584 void scanSpec();
586 public:
587 Chuck_Object * SELF;
588 Chuck_String p_str;
589 OSC_Address_Space();
590 OSC_Address_Space( const char * spec );
591 OSC_Address_Space( const char * addr, const char * type );
592 virtual ~OSC_Address_Space();
594 // initialization
595 void init();
596 void setSpec( const char * c );
597 void setSpec( const char * addr, const char * type );
598 void setReceiver( OSC_Receiver * recv );
600 // distribution
601 bool try_queue_mesg ( OSCMesg * o );
602 bool message_matches ( OSCMesg * o );
603 void queue_mesg ( OSCMesg * o );
605 // loop functions
606 bool has_mesg();
607 bool next_mesg();
609 void wait ( Chuck_VM_Shred * shred, Chuck_VM * vm );
611 // get functions
612 bool vcheck( osc_datatype tt );
613 int4byte next_int(); // gewang: changed from int
614 float next_float();
615 char * next_string();
616 char * next_string_dup();
617 char * next_blob();
621 #endif // _OSC_H_INCLUDED_