From a91bc7566f231d0a8acf8d7d7b024344f1600df0 Mon Sep 17 00:00:00 2001 From: Jonathan Moore Liles Date: Wed, 20 Jan 2010 16:33:02 -0800 Subject: [PATCH] Mixer: Add basic OSC support. --- mixer/configure | 3 + mixer/makefile.inc | 2 +- mixer/src/Mixer.C | 134 +++++++++++++++++++++++++ mixer/src/Mixer.H | 10 ++ mixer/src/main.C | 4 +- nonlib/OSC/Endpoint.C | 227 ++++++++++++++++++++++++++++++++++++++++++ nonlib/OSC/Endpoint.H | 158 +++++++++++++++++++++++++++++ nonlib/makefile.inc | 2 +- scripts/config-funcs | 18 +++- 9 files changed, 549 insertions(+), 9 deletions(-) create mode 100644 nonlib/OSC/Endpoint.C create mode 100644 nonlib/OSC/Endpoint.H diff --git a/mixer/configure b/mixer/configure index 1f25912..d87659c 100755 --- a/mixer/configure +++ b/mixer/configure @@ -20,5 +20,8 @@ require_command ar ar require_command makedepend makedepend require_package JACK 0.103.0 jack require_package lrdf 0.4.0 lrdf +require_package liblo 0.23 liblo + +test_version `version_of liblo` 0.26 || warn "Version $(version_of liblo) of liblo is slow to create servers. Consider upgrading to 0.26 or later" end diff --git a/mixer/makefile.inc b/mixer/makefile.inc index b1a498a..fda3f27 100644 --- a/mixer/makefile.inc +++ b/mixer/makefile.inc @@ -8,7 +8,7 @@ Mixer_SRCS:=$(Mixer_SRCS:.fl=.C) Mixer_SRCS:=$(sort $(Mixer_SRCS)) Mixer_OBJS:=$(Mixer_SRCS:.C=.o) -Mixer_LIBS := $(FLTK_LIBS) $(JACK_LIBS) $(LASH_LIBS) $(LRDF_LIBS) +Mixer_LIBS := $(FLTK_LIBS) $(JACK_LIBS) $(LASH_LIBS) $(LRDF_LIBS) $(LIBLO_LIBS) src/mixer: $(Mixer_OBJS) FL/libfl_widgets.a nonlib/libnonlib.a @ echo -n Linking mixer... diff --git a/mixer/src/Mixer.C b/mixer/src/Mixer.C index 2e86ea3..ce15e68 100644 --- a/mixer/src/Mixer.C +++ b/mixer/src/Mixer.C @@ -42,9 +42,13 @@ #include "debug.h" #include "FL/color_scheme.H" +#include "OSC/Endpoint.H" +#include const double STATUS_UPDATE_FREQ = 0.2f; +const double OSC_INTERVAL = 0.1f; + extern char *user_config_dir; #include "debug.h" @@ -55,6 +59,108 @@ extern char *user_config_dir; /* ((Mixer*)v)->update(); */ /* } */ + + +/************************/ +/* OSC Message Handlers */ +/************************/ + +OSC_HANDLER( generic ) +{ + OSC_DMSG(); + + return 0; +} + +OSC_HANDLER( quit ) +{ + OSC_DMSG(); + + ((Mixer*)user_data)->command_quit(); + + return 0; +} + +OSC_HANDLER( save ) +{ + OSC_DMSG(); + + if ( ((Mixer*)user_data)->command_save() ) + OSC_REPLY_OK(); + else + OSC_REPLY_ERR(); + + return 0; +} + +OSC_HANDLER( load ) +{ + OSC_DMSG(); + + const char *project_path = &argv[0]->s; + const char *project_display_name = &argv[1]->s; + + if ( ((Mixer*)user_data)->command_load( project_path, project_display_name ) ) + OSC_REPLY_OK(); + else + OSC_REPLY_ERR(); + + return 0; +} + +OSC_HANDLER( new ) +{ + OSC_DMSG(); + + const char *project_path = &argv[0]->s; + const char *project_display_name = &argv[1]->s; + + if ( ((Mixer*)user_data)->command_new( project_path, project_display_name ) ) + OSC_REPLY_OK(); + else + OSC_REPLY_ERR(); + + return 0; +} + +OSC_HANDLER( root ) +{ + OSC_DMSG(); + + OSC_REPLY( "load\nsave\nquit\nnew\n"); + + return 0; +} + +OSC_HANDLER( add_strip ) +{ + OSC_DMSG(); + + ((Mixer*)user_data)->command_add_strip(); + + OSC_REPLY_OK(); + + return 0; +} + +OSC_HANDLER( finger ) +{ + OSC_DMSG(); + + lo_address src = lo_message_get_source( msg ); + + const char *s = "APP_TITLE\n"; + +/* if ( 1 >= lo_send_from( src, ((Mixer*)user_data)->osc_endpoint, LO_TT_IMMEDIATE, "/finger-reply", "s", s ) ) */ +/* { */ +/* DMESSAGE( "Failed to send reply" ); */ +/* } */ + + return 0; +} + + + void Mixer::cb_menu(Fl_Widget* o) { Fl_Menu_Bar *menu = (Fl_Menu_Bar*)o; @@ -276,6 +382,34 @@ Mixer::Mixer ( int X, int Y, int W, int H, const char *L ) : update_menu(); load_options(); + + osc_endpoint = new OSC::Endpoint(); + + osc_endpoint->url(); + + // if ( 1 >= lo_send_from( src, ((Mixer*)user_data)->osc_endpoint, LO_TT_IMMEDIATE, "/finger-reply", "s", s ) ) + + osc_endpoint->add_method( "/nsm/quit", "", OSC_NAME( quit ), this ); + osc_endpoint->add_method( "/nsm/load", "ss", OSC_NAME( load ), this ); + osc_endpoint->add_method( "/nsm/save", "", OSC_NAME( save ), this ); + osc_endpoint->add_method( "/nsm/new", "ss", OSC_NAME( new ), this ); + osc_endpoint->add_method( "/nsm/", "", OSC_NAME( root ), this ); + osc_endpoint->add_method( "/finger", "", OSC_NAME( finger ), this ); + osc_endpoint->add_method( "/mixer/add_strip", "", OSC_NAME( add_strip ), this ); +// osc_endpoint->add_method( NULL, "", osc_generic, this ); + +// osc_endpoint->start(); + + /* poll so we can keep OSC handlers running in the GUI thread and avoid extra sync */ + Fl::add_timeout( OSC_INTERVAL, check_osc, this ); +} + +void +Mixer::check_osc ( void * v ) +{ + ((Mixer*)v)->osc_endpoint->check(); + Fl::repeat_timeout( OSC_INTERVAL, check_osc, v ); + } Mixer::~Mixer ( ) diff --git a/mixer/src/Mixer.H b/mixer/src/Mixer.H index 8c05fc4..70119b0 100644 --- a/mixer/src/Mixer.H +++ b/mixer/src/Mixer.H @@ -29,9 +29,15 @@ class Fl_Flowpack; class Fl_Menu_Bar; +#include "OSC/Endpoint.H" + class Mixer : public Fl_Group { +public: + + OSC::Endpoint *osc_endpoint; + private: int _rows; @@ -85,6 +91,10 @@ public: Mixer ( int X, int Y, int W, int H, const char *L ); virtual ~Mixer(); + static void check_osc ( void * v ); + + void announce ( const char *nash_url ); + public: bool command_save ( void ); diff --git a/mixer/src/main.C b/mixer/src/main.C index 3f95547..d159288 100644 --- a/mixer/src/main.C +++ b/mixer/src/main.C @@ -155,8 +155,8 @@ main ( int argc, char **argv ) else { FATAL( "Missing instance name" ); - } - } + } + } else if ( !strncmp( argv[i], "--", 2 ) ) { WARNING( "Unrecognized option: %s", argv[i] ); diff --git a/nonlib/OSC/Endpoint.C b/nonlib/OSC/Endpoint.C new file mode 100644 index 0000000..346877f --- /dev/null +++ b/nonlib/OSC/Endpoint.C @@ -0,0 +1,227 @@ + +/*******************************************************************************/ +/* Copyright (C) 2010 Jonathan Moore Liles */ +/* */ +/* This program is free software; you can redistribute it and/or modify it */ +/* under the terms of the GNU General Public License as published by the */ +/* Free Software Foundation; either version 2 of the License, or (at your */ +/* option) any later version. */ +/* */ +/* This program 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 General Public License for */ +/* more details. */ +/* */ +/* You should have received a copy of the GNU General Public License along */ +/* with This program; see the file COPYING. If not,write to the Free Software */ +/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*******************************************************************************/ + +#include +#include "debug.h" +#include +#include + +#include "Endpoint.H" + +namespace OSC +{ + + void + Endpoint::error_handler(int num, const char *msg, const char *path) + { + WARNING( "LibLO server error %d in path %s: %s\n", num, path, msg); + } + + Endpoint::Endpoint ( const char *port ) + { + DMESSAGE( "Creating OSC server" ); + +// _st = lo_server_thread_new( s, error_handler ); + // _server = lo_server_thread_get_server( _st ); + + _server = lo_server_new( port, error_handler ); + + if ( ! _server ) + FATAL( "Error creating OSC server" ); + + char *url = lo_server_get_url(_server); + printf("OSC: %s\n",url); + free(url); + } + + Endpoint::~Endpoint ( ) + { +// lo_server_thread_free( _st ); + lo_server_free( _server ); + } + + void + Endpoint::add_method ( const char *path, const char *typespec, lo_method_handler handler, void *user_data ) + { + lo_server_add_method( _server, path, typespec, handler, user_data ); + } + + void + Endpoint::del_method ( const char *path, const char *typespec ) + { + lo_server_del_method( _server, path, typespec ); + } + +/* void * */ +/* Endpoint::osc_thread ( void * arg ) */ +/* { */ +/* ((Endpoint*)arg)->osc_thread(); */ + +/* return NULL; */ +/* } */ + +/* void */ +/* Endpoint::osc_thread ( void ) */ +/* { */ +/* _thread.name( "OSC" ); */ + +/* DMESSAGE( "OSC Thread running" ); */ + +/* for ( ;; ) */ +/* { */ +/* lo_server_recv( _sever ); */ +/* } */ +/* } */ + + void + Endpoint::start ( void ) + { + +/* if ( !_thread.clone( &Endpoint::osc_thread, this ) ) */ +/* FATAL( "Could not create OSC thread" ); */ + +/* lo_server_thread_start( _st ); */ + + } + + void + Endpoint::stop ( void ) + { +// lo_server_thread_stop( _st ); + } + + int + Endpoint::port ( void ) const + { + return lo_server_get_port( _server ); + } + + char * + Endpoint::url ( void ) const + { + return lo_server_get_url( _server ); + } + +/** Process any waiting events and return immediately */ + void + Endpoint::check ( void ) const + { + wait( 0 ); + } + +/** Process any waiting events and return immediately */ + void + Endpoint::wait ( int timeout ) const + { + lo_server_recv_noblock( _server, timeout ); + } + +/** Process events forever */ + void + Endpoint::run ( void ) const + { + for ( ;; ) + { + lo_server_recv( _server ); + } + } + + + int + Endpoint::send ( lo_address to, const char *path, std::list< OSC_Value > values ) + { + + lo_message m = lo_message_new(); + + for ( std::list< OSC_Value >::const_iterator i = values.begin(); + i != values.end(); + ++i ) + { + const OSC_Value *ov = &(*i); + + switch ( ov->type() ) + { + case 'f': + lo_message_add_float( m, ((OSC_Float*)ov)->value() ); + break; + case 'i': + lo_message_add_int32( m, ((OSC_Int*)ov)->value() ); + break; + case 's': + DMESSAGE( "Adding string %s", ((OSC_String*)ov)->value() ); + lo_message_add_string( m, ((OSC_String*)ov)->value() ); + break; + default: + FATAL( "Unknown format: %c", ov->type() ); + break; + } + } + + DMESSAGE( "Path: %s", path ); + + lo_bundle b = lo_bundle_new( LO_TT_IMMEDIATE ); + + lo_bundle_add_message(b, path, m ); + + int r = lo_send_bundle_from( to, _server, b ); + +// int r = lo_send_message_from( to, _server, path, m ); + +// lo_message_free( m ); + + return r; + } + + int + Endpoint::send ( lo_address to, const char *path ) + { + return lo_send_from( to, _server, LO_TT_IMMEDIATE, path, "" ); + } + + int + Endpoint::send ( lo_address to, const char *path, int v ) + { + return lo_send_from( to, _server, LO_TT_IMMEDIATE, path, "i", v ); + } + + int + Endpoint::send ( lo_address to, const char *path, float v ) + { + return lo_send_from( to, _server, LO_TT_IMMEDIATE, path, "f", v ); + } + + int + Endpoint::send ( lo_address to, const char *path, double v ) + { + return lo_send_from( to, _server, LO_TT_IMMEDIATE, path, "d", v ); + } + + int + Endpoint::send ( lo_address to, const char *path, const char * v ) + { + return lo_send_from( to, _server, LO_TT_IMMEDIATE, path, "s", v ); + } + + int + Endpoint::send ( lo_address to, const char *path, const char * v1, const char *v2 ) + { + return lo_send_from( to, _server, LO_TT_IMMEDIATE, path, "ss", v1, v2 ); + } + +} diff --git a/nonlib/OSC/Endpoint.H b/nonlib/OSC/Endpoint.H new file mode 100644 index 0000000..43f5b9f --- /dev/null +++ b/nonlib/OSC/Endpoint.H @@ -0,0 +1,158 @@ + +/*******************************************************************************/ +/* Copyright (C) 2010 Jonathan Moore Liles */ +/* */ +/* This program is free software; you can redistribute it and/or modify it */ +/* under the terms of the GNU General Public License as published by the */ +/* Free Software Foundation; either version 2 of the License, or (at your */ +/* option) any later version. */ +/* */ +/* This program 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 General Public License for */ +/* more details. */ +/* */ +/* You should have received a copy of the GNU General Public License along */ +/* with This program; see the file COPYING. If not,write to the Free Software */ +/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*******************************************************************************/ + +#pragma once + +#include +//#include "util/Thread.H" +#include + +namespace OSC +{ + + class OSC_Value + { + + protected: + + char _type; + + float f; + double d; + int i; + const char *s; + + public: + + OSC_Value ( const OSC_Value &rhs ) + { + _type = rhs._type; + + f =rhs.f; + d = rhs.d; + i = rhs.i; + s = rhs.s; + } + + OSC_Value ( ) + { + _type = 0; + + f = 0; + d = 0; + i = 0; + s = 0; + } + + virtual ~OSC_Value ( ) { } + virtual char type ( void ) const { return _type; } + }; + + class OSC_Float : public OSC_Value + { + + public: + + float value ( void ) const { return f; } + + OSC_Float ( float v ) + { + _type = 'f'; + f = v; + } + }; + + class OSC_Int : public OSC_Value + { + + public: + + int value ( void ) const { return i; } + + OSC_Int ( int v ) + { + _type = 'i'; + i = v; + } + }; + + class OSC_String : public OSC_Value + { + public: + + const char * value ( void ) const { return s; } + + OSC_String ( const char *v ) + { + _type = 's'; + s = v; + } + }; + + class Endpoint + { + static void error_handler(int num, const char *msg, const char *path); + +// Thread _thread; + +// lo_server_thread _st; + lo_server _server; + + public: + + Endpoint ( const char *port = 0 ); + + ~Endpoint ( ); + + void add_method ( const char *path, const char *typespec, lo_method_handler handler, void *user_data ); + void del_method ( const char *path, const char *typespec ); + void start ( void ); + void stop ( void ); + int port ( void ) const; + char * url ( void ) const; + + void check ( void ) const; + void wait ( int timeout ) const; + void run ( void ) const; + + int send ( lo_address to, const char *path, std::list< OSC_Value > values ); + + /* overloads for common message formats */ + int send ( lo_address to, const char *path ); + int send ( lo_address to, const char *path, float v ); + int send ( lo_address to, const char *path, double v ); + int send ( lo_address to, const char *path, int v ); + int send ( lo_address to, const char *path, long v ); + int send ( lo_address to, const char *path, const char *v ); + int send ( lo_address to, const char *path, const char *v1, const char *v2 ); + + }; + +}; + + +/* helper macros for defining OSC handlers */ +#define OSC_NAME( name ) osc_ ## name +#define OSC_DMSG() DMESSAGE( "Got OSC message: %s", path ); +#define OSC_HANDLER( name ) static int OSC_NAME( name ) ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data ) + + +#define OSC_REPLY_OK() ((Mixer*)user_data)->osc_endpoint->send( lo_message_get_source( msg ), "/reply", path, "ok" ) +#define OSC_REPLY( msg_str ) ((Mixer*)user_data)->osc_endpoint->send( lo_message_get_source( msg ), "/reply", path, msg_str ) +#define OSC_REPLY_ERR() ((Mixer*)user_data)->osc_endpoint->send( lo_message_get_source( msg ), "/reply", path, "err" ) diff --git a/nonlib/makefile.inc b/nonlib/makefile.inc index 5bc3f07..22489e6 100644 --- a/nonlib/makefile.inc +++ b/nonlib/makefile.inc @@ -1,6 +1,6 @@ # -*- mode: makefile; -*- -nonlib_SRCS := $(wildcard nonlib/*.C nonlib/*.c nonlib/JACK/*.C nonlib/LASH/*.C) +nonlib_SRCS := $(wildcard nonlib/*.C nonlib/JACK/*.C nonlib/LASH/*.C nonlib/OSC/*.C) nonlib_SRCS:=$(sort $(nonlib_SRCS)) nonlib_OBJS:=$(nonlib_SRCS:.C=.o) diff --git a/scripts/config-funcs b/scripts/config-funcs index a9dbf52..9f4bf5b 100644 --- a/scripts/config-funcs +++ b/scripts/config-funcs @@ -282,11 +282,19 @@ require_package () _test_version () { - [ $1 -gt $4 ] && return 0 - [ $1 -eq $4 ] && [ $2 -gt $5 ] && return 0 - [ $1 -eq $4 ] && [ $2 -eq $5 ] && [ $3 -gt $6 ] && return 0 - [ $1 -eq $4 ] && [ $2 -eq $5 ] && [ $3 -eq $6 ] && return 0 - return 1 + if [ $# == 6 ] + then + [ $1 -gt $4 ] && return 0 + [ $1 -eq $4 ] && [ $2 -gt $5 ] && return 0 + [ $1 -eq $4 ] && [ $2 -eq $5 ] && [ $3 -gt $6 ] && return 0 + [ $1 -eq $4 ] && [ $2 -eq $5 ] && [ $3 -eq $6 ] && return 0 + return 1 + elif [ $# == 4 ] + then + [ $1 -gt $3 ] && return 0 + [ $1 -eq $3 ] && [ $2 -eq $4 ] && return 0 + return 1; + fi } # return true if #1 is greater than or equal to $2