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