Exemplo n.º 1
0
int osc_send( const char *port, const char *msg, const char *format, ...)
{
	lo_address t = lo_address_new(NULL, port);
	lo_message message = lo_message_new();

	va_list args;
	va_start( args, format);

	while( *format != '\0')
	{
		switch( *format)
		{
			case 's': lo_message_add_string( message, va_arg( args, const char *)); break;
			case 'i': lo_message_add_int32( message, va_arg( args, int)); break;
			case 'f': lo_message_add_float( message, va_arg( args, double)); break;
		}

		++format;
	}

	va_end( args);
	

	if( !lo_send_message( t, msg, message))
	{
		printf("OSC error %d: %s\n", lo_address_errno(t),
		lo_address_errstr(t));
	}

	if( osc_log) osc_log_print( msg, message);
	lo_message_free( message);
	lo_address_free( t);

	return 0;
}
Exemplo n.º 2
0
int osc_send_track_load(struct deck *de)
{
    struct player *pl;
    struct track *tr;
    pl = &de->player;
    tr = pl->track;
    
    if(tr) {
        /* send a message to /xwax/track_load with two arguments, report any
         * errors */
        int c;
        for(c = 0; c < osc_nclient%3; ++c) { 
            if (lo_send(address[c], "/xwax/track_load", "iissi",
                    de->ncontrol,
                    (int) tr,
                    de->record->artist, 
                    de->record->title,
                    tr->rate
                ) == -1) {
                printf("OSC error %d: %s\n", lo_address_errno(address[c]),
                       lo_address_errstr(address[c]));
            }
            printf("osc_send_track_load: sent track_load to %s\n", lo_address_get_url(address[c]));
            sleep(1); // Wierd bug in liblo that makes second track load not catched by client's track_load_handler if sent too fast
            
        }
    }
    
    return 0;
}
Exemplo n.º 3
0
int osc_send_status(lo_address a, int d)
{
    struct deck *de;
    struct player *pl;
    struct track *tr;
    de = &osc_deck[d];
    pl = &de->player;
    tr = pl->track;
    
    char *path;
    if(tr->path)
        path = tr->path;
    else
        path = "";
    
    if(tr) {
        /* send a message to /xwax/status */
        if (lo_send(a, "/xwax/status", "isssfffi",
                de->ncontrol,           // deck number (int)
                path,               // track path (string)
                de->record->artist,     // artist name (string)
                de->record->title,      // track title (string)
                (float) tr->length / (float) tr->rate,  // track length in seconds (float)
                player_get_elapsed(pl),           // player position in seconds (float)
                pl->pitch,              // player pitch (float)
                pl->timecode_control)    // timecode activated or not (int)
            == -1) {
            printf("OSC error %d: %s\n", lo_address_errno(a),
                   lo_address_errstr(a));
        }
        printf("osc_send_status: sent deck %i status to %s\n", d, lo_address_get_url(a));            
    }
    
    return 0;    
}
Exemplo n.º 4
0
int main(int argc, char *argv[])
{
    /* build a blob object from some data */
    lo_blob btest = lo_blob_new(sizeof(testdata), testdata);

    /* an address to send messages to. sometimes it is better to let the server
     * pick a port number for you by passing NULL as the last argument */
    lo_address t = lo_address_new(NULL, "7770");

    if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'q') {
      /* send a message with no arguments to the path /quit */
      lo_send(t, "/quit", NULL);
    } else {
      /* send a message to /foo/bar with two float arguments, report any
       * errors */
      if (lo_send(t, "/foo/bar", "ff", 0.12345678f, 23.0f) == -1) {
	printf("OSC error %d: %s\n", lo_address_errno(t), lo_address_errstr(t));
      }

      /* send a message to /a/b/c/d with a mixtrure of float and string
       * arguments */
      lo_send(t, "/a/b/c/d", "sfsff", "one", 0.12345678f, "three",
	      -0.00000023001f, 1.0);

      /* send a 'blob' object to /a/b/c/d */
      lo_send(t, "/a/b/c/d", "b", btest);

      /* send a jamin scene change instruction with a 32bit integer argument */
      lo_send(t, "/jamin/scene", "i", 2);
    }

    return 0;
}
Exemplo n.º 5
0
SLBus::~SLBus()
{
	if ( lo_send( sl_target_, "/quit", "" ) == -1 ) {
		spdlog::get( "main" )->error(
			"OSC error {}: {}", lo_address_errno(sl_target_), lo_address_errstr(sl_target_)
		);
	}

	while (clipset_.size())
		clipset_.pop_back();
}
Exemplo n.º 6
0
void unreg_cb(int loop, const char* monitor, const char* cb_addr)
{
  char addr[29];
  if (loop == -2)
    sprintf(addr, "/unregister_auto_update");
  else
    sprintf(addr, "/sl/%d/unregister_auto_update", loop);
  if (lo_send(sl, addr, "siss", monitor, 200, osc_host, cb_addr) == -1) {
    printf("OSC error %d: %s\n", lo_address_errno(sl), lo_address_errstr(sl));
  }
}
Exemplo n.º 7
0
int osc_send_quit( const char *port)
{
	lo_address t = lo_address_new(NULL, port);

	if( !lo_send( t, "/quit" , ""))
	{
		printf("OSC error %d: %s\n", lo_address_errno(t),
		lo_address_errstr(t));
	}

	return 0;
}
Exemplo n.º 8
0
int osc_send_vector( const char *port, const char *msg)
{
	lo_address t = lo_address_new(NULL, port);

	if( !lo_send( t, "/a/b/c" , "fff" , 10.0f, 10.0f, 10.0f))
	{
		printf("OSC error %d: %s\n", lo_address_errno(t),
		lo_address_errstr(t));
	}

	return 0;
}
Exemplo n.º 9
0
int subtest_handler(const char *path, const char *types, lo_arg **argv,
                    int argc, lo_message data, void *user_data)
{
    int i;
    lo_address a = lo_message_get_source(data);
    static char *uri = NULL;

    printf("subtest: got reply (%s)\n", path);
    if (!uri) {
	uri = lo_address_get_url(a);
    } else {
	char *new_uri = lo_address_get_url(a);

	if (strcmp(uri, new_uri)) {
	    printf("ERROR: %s != %s\n", uri, new_uri);

	    exit(1);
	}
	free(new_uri);
    }
    lo_send(a, "/subtest-reply", "i", 0xbaa);
    if (lo_address_errno(a)) {
	fprintf(stderr, "subtest error %d: %s\n", lo_address_errno(a),
		lo_address_errstr(a));

	exit(1);
    }

    for (i=0; i<10; i++) {
#ifdef WIN32
        /* TODO: Wait time of 2.233 not easily doable in Windows */
        Sleep(2);
#else
        usleep(2233);
#endif
	lo_send(a, "/subtest-reply", "i", 0xbaa+i);
    }

    return 0;
}
Exemplo n.º 10
0
Arquivo: osc.c Projeto: cmeon/touchwax
int osc_send_position(int d, const float position)
{
    lo_address t = lo_address_new(ADDRESS, "7770");
        
    if (lo_send(t, "/xwax/position", "if", d, position) == -1) {
#ifdef __ANDROID__
    __android_log_print(ANDROID_LOG_DEBUG, "osc.c", "OSC error %d: %s\n", lo_address_errno(t),
        lo_address_errstr(t));
#endif
    }  
    
    return 1;
}
Exemplo n.º 11
0
void get_control(int loop, const char* control, const char* cb_addr)
{
  char addr[27];
  if (loop < -5) {
    printf("get_control invalid loop %d\n", loop);
  }
  else {
    sprintf(addr, "/sl/%d/get", loop);
    if (lo_send(sl, addr, "sss", control, osc_host, cb_addr) == -1) {
      printf("OSC error %d: %s\n", lo_address_errno(sl), lo_address_errstr(sl));
    }
  }
}
Exemplo n.º 12
0
int osc_send_scale(int scale)
{
    /* send a message to /xwax/track_load with two arguments, report any
     * errors */
    int c;
    for(c = 0; c < osc_nclient%3; ++c) {
        if (lo_send(address[c], "/xwax/scale", "i", scale) == -1) {
            printf("OSC error %d: %s\n", lo_address_errno(address[c]),
                   lo_address_errstr(address[c]));
        }
    }
    
    return 0;
}
Exemplo n.º 13
0
static int send_msg(lo_address a, char *path, term_t args)
{
	lo_message msg=lo_message_new();

	if (add_msg_args(msg,args)) {
		if (lo_send_message(a,path,msg)==-1) {
			lo_message_free(msg);
			return osc_error(lo_address_errno(a),lo_address_errstr(a),path);
		} else {
			lo_message_free(msg);
			return TRUE;
		}
	} else return FALSE;
}
Exemplo n.º 14
0
int main(int argc, char *argv[])
{
	lo_address t = lo_address_new(NULL, "7770");
	sleep(5);
	smallIncr = (TWOPI)/360.0;
	for (theta = TWOPI ; theta >= 0.0 ; theta = theta-smallIncr) {
		int jsVal = (int) round(sin(theta)*32767.0) ;
		if (lo_send(t, path, types, jsNum , jsAxis , jsVal) == -1) {
			printf("OSC error %d: %s\n", lo_address_errno(t), lo_address_errstr(t));
		}
		usleep(40000);
		printf("path=%s , types=%s argslist = %d %d %d\n",path , types , jsNum , jsAxis , jsVal) ;
	}
}
Exemplo n.º 15
0
int main(int argc, char *argv[])
{
    /* an address to send messages to. sometimes it is better to let the server
     * pick a port number for you by passing NULL as the last argument */
    lo_address t = lo_address_new(NULL, "7770");
    char mode = 't';
    float x = 0.0;
    float delta = 0.1;
    int n = 0; /* slider number */

    if (argc == 2) {
        mode = argv[1][0];
        if (mode == '?' || mode == 'h' ||
                (mode != 'i' && mode != 't')) {
            printf("usage: test-client [?hti]\n");
            printf("    default (t) is triangle, (i)nteractive sends msg after each return\n");
            exit(1);
        }
    }

    printf("lo_address_new done\n");

    /* send messages to /slider with two arguments, report any
     * errors */
    while (1) {
        if (lo_send(t, "/slider", "if", n, x) == -1) {
            printf("OSC error %d: %s\n", lo_address_errno(t), lo_address_errstr(t));
            break;
        } else {
            printf("/slider %d %g\n", n, x);
        }
        x = x + delta;
        if (x > 1.0 - delta * 0.5) {
            x = 1.0;
            delta = -delta;
        } else if (x < -delta * 0.5) {
            x = 0.0;
            delta = -delta;
        }
        if (mode == 'i') {
            while (getchar() != '\n') ;
        } else {
            usleep(100000);
        }
    }

    printf("done calling lo_send\n");

    return 0;
}
Exemplo n.º 16
0
void SLClip::send2SL(const char* pathend, lo_message msg)
{
	auto mainlog = spdlog::get( "main" );
	mainlog->info( "SLClip::send2SL");
	lo_message_pp( msg );
	lo_address target = ((SLBus*)parent_)->getSLTarget();
	mainlog->info( "* address : {}", target);
	std::string path = "/sl/" + std::to_string( sl_id_ ) + pathend;
	mainlog->info( "* path : {}", path);
	if ( lo_send_message( target, path.c_str(), msg ) == -1 ) {
		mainlog->error(
			"OSC error {}: {}", lo_address_errno(target), lo_address_errstr(target)
		);
	}
	mainlog->info( "/SLClip::send2SL");
}
Exemplo n.º 17
0
int osc_send_pos(int d, const float pos, const float pitch)
{
    //lo_address t = lo_address_new(NULL, "7771");
    //lo_address t = lo_address_new(address, "7771");

    /* send a message to /xwax/position with one float argument, report any
     * errors */
    int c;
    for(c = 0; c < osc_nclient%3; ++c) {
        if (lo_send(address[c], "/xwax/position", "iff", d, pos, pitch) == -1) {
            printf("OSC error %d: %s\n", lo_address_errno(address[c]),
                   lo_address_errstr(address[c]));
        }
    }
        
    return 0;
}
Exemplo n.º 18
0
void test_multicast(lo_server_thread st)
{
    /* test multicast server and sender */
    /* message is sent from st otherwise reply doesn't work */
    lo_server ms = lo_server_new_multicast("224.0.1.1", "15432", error);
    lo_address ma = lo_address_new("224.0.1.1", "15432");
    lo_address_set_ttl(ma, 1);
    lo_server_add_method(ms, "/foo/bar", "fi", foo_handler, ms);
    lo_server_add_method(ms, "/reply", "s", reply_handler, NULL);
    if (lo_send_from(ma, lo_server_thread_get_server(st), LO_TT_IMMEDIATE,
                     "/foo/bar", "ff", 0.12345678f, 23.0f) == -1) {
        printf("multicast send error %d: %s\n", lo_address_errno(ma), lo_address_errstr(ma));
        exit(1);
    }
    TEST(lo_server_recv(ms)==24);
    lo_server_free(ms);
    lo_address_free(ma);
}
Exemplo n.º 19
0
static int send_msg_timestamped(lo_address a, lo_timetag *ts, char *path, term_t args)
{
	lo_message msg=lo_message_new();
	lo_bundle  bun=lo_bundle_new(*ts);

	if (add_msg_args(msg,args)) {
		int ret;

		lo_bundle_add_message(bun,path,msg);
		ret = lo_send_bundle(a,bun);
		lo_message_free(msg);
		lo_bundle_free(bun);
		if (ret==-1) {
			return osc_error(lo_address_errno(a),lo_address_errstr(a),path);
		} else {
			return TRUE;
		}
	} else return FALSE;
}
Exemplo n.º 20
0
int foo_handler(const char *path, const char *types, lo_arg **argv, int argc,
		 lo_message data, void *user_data)
{
    lo_server serv = (lo_server)user_data;
    lo_address src = lo_message_get_source(data);
    char *url = lo_address_get_url(src);
    char *server_url = lo_server_get_url(serv);
    printf("Address of us: %s\n", server_url);
    printf("%s <- f:%f, i:%d\n", path, argv[0]->f, argv[1]->i);
    if (lo_send_from(src, serv, LO_TT_IMMEDIATE, "/reply", "s", "a reply") == -1) {
	printf("OSC reply error %d: %s\nSending to %s\n", lo_address_errno(src), lo_address_errstr(src), url);
	exit(1);
    } else {
	printf("Reply sent to %s\n\n", url);
    }
    free(server_url);
    free(url);

    return 0;
}
Exemplo n.º 21
0
void SLBus::addClip( std::shared_ptr<SLClip> clip )
{
	stk::FileWvIn f( clip->getURI() );
	float duration = f.getSize() / f.getFileRate();
	
	if ( lo_send( sl_target_, "/loop_add", "if", f.channelsOut(), duration ) == -1 ) {
		spdlog::get( "main" )->error(
			"OSC error {}: {}", lo_address_errno(sl_target_), lo_address_errstr(sl_target_)
		);
	}
	else {
		clip->setParent( this );
		clip->setSLid( clipset_.size() );
		clip->SLget( "state" );
		sleep(1);
		clip->SLget( "state" );
		clip->SLload();
		clipset_.push_back( clip );
		Waiter::getInstance()->selectClip( clip );
	}
}
Exemplo n.º 22
0
int main(int argc, char *argv[])
{
  lo_address ad;

  if (argc > 1) {
    printf("Connecting to %s\n", argv[1]);
    ad = lo_address_new_from_url(argv[1]);
  } else {
    printf("Connecting to %s\n", MONOHORN_URL);
    ad = lo_address_new_from_url(MONOHORN_URL);
  }

  if (lo_address_errno(ad) < 0) {
    fprintf(stderr, "LO Error: %s\n", lo_address_errstr(ad));
    return 1;
  }

  int ret = 0;

  while (1)
  {
    rb_raise();
    rb_bottom();
    rb_render(ad);

    if (lo_send(ad, "/render", NULL) < 0)
    {
      fprintf(stderr, "LO Error: %s\n", lo_address_errstr(ad));
      ret = 1;
      break;
    }

    // 15 frames /sec
    usleep(1000000 / 15);
  }

  lo_send(ad, "/clear", NULL);

  return ret;
}
Exemplo n.º 23
0
int info_handler(const char *path, const char *types, lo_arg **argv, int argc,
		 void *data, void *user_data)
{
//      printf("info message\n");
//      fflush(stdout);

  lo_bundle b = lo_bundle_new( LO_TT_IMMEDIATE );

      int num_joysticks = SDL_NumJoysticks();
//       printf("Found %d joystick(s)\n\n", num_joysticks);
	lo_message m1 = lo_message_new();
	lo_message_add_int32( m1, num_joysticks );
	lo_bundle_add_message( b, "/joystick/number", m1 );
	int joy_idx;
        for(joy_idx = 0; joy_idx < num_joysticks; ++joy_idx)
        {
          SDL_Joystick* joy = SDL_JoystickOpen(joy_idx);
          if (!joy)
          {
            fprintf(stderr, "Unable to open joystick %d\n", joy_idx);
          }
          else
          {
	    lo_message m2 = get_joystick_info_msg( joy_idx, joy );
	    lo_bundle_add_message( b, "/joystick/info", m2 );
//             print_joystick_info(joy_idx, joy);
            SDL_JoystickClose(joy);
          }
//         }
      }

 	if ( lo_send_bundle_from( t, s, b )  == -1 ){
 		{ printf("sd2osc/info: OSC error %d: %s\n", lo_address_errno(t), lo_address_errstr(t)); }
 	}
 	lo_bundle_free( b );
	fflush(stdout);

    return 0;
}
Exemplo n.º 24
0
int osc_send_ppm_block(struct track *tr)
{
    if(tr) {
        int c;
        for(c = 0; c < osc_nclient%3; ++c) {
            int i = 0;
            while(i < tr->length) {
                int j = 0;
                unsigned char ppm_block[24575];
                
                while(j < 24575 && i < tr->length) {
                    ppm_block[j] = track_get_ppm(tr, i);
                    ++j;
                    i += 64;
                }
                
                /* build a blob object from some data */
                lo_blob blob = lo_blob_new(j, &ppm_block);

                //lo_server server = lo_server_thread_get_server(st_tcp);
                if (lo_send(address[c], "/xwax/ppm", "ibi", 
                    (int) tr, 
                    blob,
                    tr->length
                ) == -1) {
                    printf("OSC error %d: %s\n", lo_address_errno(address[c]),
                        lo_address_errstr(address[c]));
                }
                lo_blob_free(blob);
            }
            lo_send(address[c], "/xwax/ppm_end", "i", (int) tr);
            
            printf("Sent %p blocks to %s\n", tr, lo_address_get_url(address[c]));
            sleep(1); // Wierd bug in liblo that makes second track load not catched by client's track_load_handler if sent too fast
        }

    }
    return 0;
}
Exemplo n.º 25
0
int osc_send_ints( const char *port, const char *msg, int count, int *val)
{
	lo_address t = lo_address_new(NULL, port);
	lo_message message = lo_message_new();

	int i;
	for( i = 0; i < count; i++)
	{
		lo_message_add_int32( message, val[i]);
	}

	if( !lo_send_message( t, msg, message))
	{
		printf("OSC error %d: %s\n", lo_address_errno(t),
		lo_address_errstr(t));
	}

	if( osc_log) osc_log_print( msg, message);
	lo_message_free( message);
	lo_address_free( t);

	return 0;
}
Exemplo n.º 26
0
/* catch any osc incoming messages. */
int
OscCtrlServer::osc_handler(const char *path,
                           const char *types,
                           lo_arg ** argv, int argc, void * /*data */ ,
                           void *user_data) {
  OscCtrlServer *context = static_cast<OscCtrlServer *>(user_data);
  std::shared_ptr<QuiddityManager> manager =
      context->get_quiddity_manager();
  if (!(bool) manager) {
    g_warning("OscCtrlServer: cannot get quiddity manager");
    return 0;
  }

  // create
  if (g_str_has_prefix(path, "/c") || g_str_has_prefix(path, "/C")) {
    if (argc == 1) {
      gchar *class_name = string_from_osc_arg(types[0], argv[0]);
      manager->create(class_name);
      g_free(class_name);
    }
    else if (argc == 2) {
      gchar *class_name = string_from_osc_arg(types[0], argv[0]);
      gchar *quid_name = string_from_osc_arg(types[1], argv[1]);
      manager->create(class_name, quid_name);
      g_free(class_name);
      g_free(quid_name);
    }
    else
      g_warning("OSCctl: wrong arg number for create");
    return 0;
  }

  // remove
  if (g_str_has_prefix(path, "/r") || g_str_has_prefix(path, "/R")) {
    if (argc == 1) {
      gchar *quid_name = string_from_osc_arg(types[0], argv[0]);
      manager->remove(quid_name);
      g_free(quid_name);
    }
    else
      g_warning("OSCctl: wrong arg number for remove");
    return 0;
  }

  // set_property
  if (g_str_has_prefix(path, "/s") || g_str_has_prefix(path, "/S")) {
    if (argc == 3) {
      gchar *quid_name = string_from_osc_arg(types[0], argv[0]);
      gchar *prop_name = string_from_osc_arg(types[1], argv[1]);
      gchar *value = string_from_osc_arg(types[2], argv[2]);
      manager->set_property(quid_name, prop_name, value);
      g_free(quid_name);
      g_free(prop_name);
      g_free(value);
    }
    else
      g_warning("OSCctl: wrong arg number for set_property");
    return 0;
  }

  // invoke
  if (g_str_has_prefix(path, "/i") || g_str_has_prefix(path, "/I")) {
    if (argc >= 2) {
      gchar *quid_name = string_from_osc_arg(types[0], argv[0]);
      gchar *method_name = string_from_osc_arg(types[1], argv[1]);
      int i;
      std::vector<std::string> args;
      for (i = 2; i < argc; i++) {
        gchar *val = string_from_osc_arg(types[i], argv[i]);
        args.push_back(val);
        g_free(val);
      }
      manager->invoke(quid_name, method_name, nullptr, args);
      g_free(quid_name);
      g_free(method_name);
    }
    else
      g_warning("OSCctl: wrong arg number for invoke");
    return 0;
  }

  // add an osc subscriber
  if (g_str_has_prefix(path, "/a") || g_str_has_prefix(path, "/A")) {
    if (argc == 3) {
      gchar *subscriber_name = string_from_osc_arg(types[0], argv[0]);
      gchar *host = string_from_osc_arg(types[1], argv[1]);
      gchar *port = string_from_osc_arg(types[2], argv[2]);
      gchar *port_int = string_float_to_string_int(port);
      gchar *internal_subscriber_name =
          context->make_internal_subscriber_name(subscriber_name);
      if (context->osc_subscribers_.end() ==
          context->osc_subscribers_.find(internal_subscriber_name)) {
        g_warning
            ("OscCtrlServer: a subscriber named %s is already registered",
             subscriber_name);
        return 0;
      }
      if (host == nullptr || port_int == nullptr) {
        g_warning("OscCtrlServer: issue with host name or port");
        return 0;
      }
      context->osc_subscribers_[internal_subscriber_name] =
          std::make_pair(host, port_int);
      if (!manager->make_property_subscriber(internal_subscriber_name,
                                             OscCtrlServer::prop_cb,
                                             context))
        return 0;

      g_debug("subscriber %s, %s, %s", subscriber_name, host, port_int);

      g_free(internal_subscriber_name);
      g_free(subscriber_name);
      g_free(host);
      g_free(port);
      g_free(port_int);
    }
    else
      g_warning("OSCctl: add subscriber needs 3 args (name, host, port)");
    return 0;
  }

  // delete an osc subscriber
  if (g_str_has_prefix(path, "/d") || g_str_has_prefix(path, "/D")) {
    if (argc == 1) {
      gchar *subscriber_name = string_from_osc_arg(types[0], argv[0]);
      gchar *internal_subscriber_name =
          context->make_internal_subscriber_name(subscriber_name);
      auto it = context->osc_subscribers_.find(internal_subscriber_name);
      if (context->osc_subscribers_.end() == it) {
        g_warning
            ("OscCtrlServer: cannot delete non existing subscriber named %s",
             subscriber_name);
        return 0;
      }
      manager->remove_property_subscriber(internal_subscriber_name);
      context->osc_subscribers_.erase(it);
      g_free(internal_subscriber_name);
      g_free(subscriber_name);
    }
    else
      g_warning("OSCctl: delete subscriber needs 1 args (name)");
    return 0;
  }

  // subscribe to a property
  if (g_strcmp0(path, "/get") == 0) {
    if (argc == 3) {
      gchar *quiddity_name = string_from_osc_arg(types[0], argv[0]);
      gchar *property_name = string_from_osc_arg(types[1], argv[1]);
      gchar *response_url = string_from_osc_arg(types[2], argv[2]);

      if (quiddity_name == nullptr || property_name == nullptr
          || response_url == nullptr) {
        g_warning
            ("OscCtrlServer: issue with quiddity name or property name or response url");
        return 0;
      }

      std::string value = manager->get_property(quiddity_name,
                                                property_name);
      lo_address response_lo_address =
          lo_address_new_from_url(response_url);

      if (response_lo_address != nullptr
          && !lo_address_errno(response_lo_address)) {
        gchar *message = g_strdup_printf("/%s/%s",
                                         quiddity_name,
                                         property_name);
        lo_send(response_lo_address, message, "s", value.c_str());
        lo_address_free(response_lo_address);
        g_free(message);
      }
      else
        g_debug("url osc error in get");

      g_free(quiddity_name);
      g_free(property_name);
      g_free(response_url);
    }
    else
      g_warning
          ("OSCctl: subscribe property needs 3 args (name, quiddity, property)");
    return 0;
  }

  // subscribe to a property
  if (g_str_has_prefix(path, "/get_property_")
      || g_str_has_prefix(path, "/G")) {
    if (argc == 3) {
      gchar *subscriber_name = string_from_osc_arg(types[0], argv[0]);
      gchar *quiddity_name = string_from_osc_arg(types[1], argv[1]);
      gchar *property_name = string_from_osc_arg(types[2], argv[2]);
      gchar *internal_subscriber_name =
          context->make_internal_subscriber_name(subscriber_name);
      if (context->osc_subscribers_.end() ==
          context->osc_subscribers_.find(internal_subscriber_name)) {
        g_warning
            ("OscCtrlServer: a subscriber named %s does not exist",
             subscriber_name);
        return 0;
      }
      if (quiddity_name == nullptr || property_name == nullptr) {
        g_warning
            ("OscCtrlServer: issue with quiddity name or property name");
        return 0;
      }

      if (!manager->subscribe_property(internal_subscriber_name,
                                       quiddity_name, property_name))
        g_warning("OscCtrlServer: pb subscribing to %s, %s",
                  quiddity_name, property_name);

      g_free(internal_subscriber_name);
      g_free(subscriber_name);
      g_free(quiddity_name);
      g_free(property_name);
    }
    else
      g_warning
          ("OSCctl: subscribe property needs 3 args (name, quiddity, property)");
    return 0;
  }

  // unsubscribe to a property
  if (g_str_has_prefix(path, "/u") || g_str_has_prefix(path, "/U")) {
    if (argc == 3) {
      gchar *subscriber_name = string_from_osc_arg(types[0], argv[0]);
      gchar *quiddity_name = string_from_osc_arg(types[1], argv[1]);
      gchar *property_name = string_from_osc_arg(types[2], argv[2]);
      gchar *internal_subscriber_name =
          context->make_internal_subscriber_name(subscriber_name);
      if (context->osc_subscribers_.end() ==
          context->osc_subscribers_.find(internal_subscriber_name)) {
        g_warning
            ("OscCtrlServer: a subscriber named %s does not exist",
             subscriber_name);
        return 0;
      }
      if (quiddity_name == nullptr || property_name == nullptr) {
        g_warning
            ("OscCtrlServer: issue with quiddity name or property name");
        return 0;
      }

      if (!manager->unsubscribe_property(internal_subscriber_name,
                                         quiddity_name, property_name))
        g_warning("OscCtrlServer: pb unsubscribing to %s, %s",
                  quiddity_name, property_name);

      g_free(internal_subscriber_name);
      g_free(subscriber_name);
      g_free(quiddity_name);
      g_free(property_name);
    }
    else
      g_warning
          ("OSCctl: unsubscribe property needs 3 args (name, quiddity, property)");
    return 0;
  }

  g_debug("unknown osc path %s", path);
  return 0;
}
Exemplo n.º 27
0
int main()
{
    lo_blob btest = lo_blob_new(sizeof(testdata), testdata);
    lo_server_thread st, sta, stb;
    lo_server s = lo_server_new(NULL, error);
    lo_bundle b;
    lo_message m1, m2;
    char *server_url, *path, *protocol, *host, *port;
    const char *host2, *port2;
    lo_address a;
    uint8_t midi_data[4] = {0xff, 0xf7, 0xAA, 0x00};
    union end_test32 et32;
    union end_test64 et64;
    lo_timetag tt = {0x1, 0x80000000}, sched;
    int count;
    int proto;
    char cmd[256];

    test_deserialise();

    sta = lo_server_thread_new("7591", error);
    stb = lo_server_thread_new("7591", rep_error);
    if (stb) {
	fprintf(stderr, "FAILED: create bad server thread object!\n");
	exit(1);
    }
    lo_server_thread_free(sta);

    /* leak check */
    st = lo_server_thread_new(NULL, error);
    lo_server_thread_start(st);
#ifdef WIN32
    Sleep(4);
#else
    usleep(4000);
#endif
    lo_server_thread_stop(st);
    lo_server_thread_free(st);
    st = lo_server_thread_new(NULL, error);
    lo_server_thread_start(st);
    lo_server_thread_stop(st);
    lo_server_thread_free(st);
    st = lo_server_thread_new(NULL, error);
    lo_server_thread_free(st);
    st = lo_server_thread_new(NULL, error);
    lo_server_thread_free(st);
    st = lo_server_thread_new(NULL, error);

    a = lo_address_new_from_url("osc://localhost/");
    TEST(a != NULL);
    lo_address_free(a);

    a = lo_address_new_from_url("osc.://localhost/");
    TEST(a == NULL);


    atexit(exitcheck);

    printf("type tests\n");
    TEST(sizeof(float) == sizeof(int32_t));
    TEST(sizeof(double) == sizeof(int64_t));

    et32.i = 0x23242526U;
    et32.i = lo_htoo32(et32.i);
    if (et32.c[0] != 0x23 || et32.c[1] != 0x24 || et32.c[2] != 0x25 ||
	et32.c[3] != 0x26) {
	fprintf(stderr, "failed 32bit endian conversion test\n");
	fprintf(stderr, "0x23242526 -> %X\n", et32.i);
	exit(1);
    } else {
	printf("passed 32bit endian conversion test\n");
    }

    et64.i = 0x232425262728292AULL;
    et64.i = lo_htoo64(et64.i);
    if (et64.c[0] != 0x23 || et64.c[1] != 0x24 || et64.c[2] != 0x25 ||
	et64.c[3] != 0x26 || et64.c[4] != 0x27 || et64.c[5] != 0x28 ||
	et64.c[6] != 0x29 || et64.c[7] != 0x2A) {
	fprintf(stderr, "failed 64bit endian conversion\n");
	fprintf(stderr, "0x232425262728292A -> %llX\n", (long long unsigned int)et64.i);
	exit(1);
    } else {
	printf("passed 64bit endian conversion\n");
    }
    printf("\n");

    /* OSC URL tests */
    path = lo_url_get_path("osc.udp://localhost:9999/a/path/is/here");
    if (strcmp(path, "/a/path/is/here")) {
	printf("failed lo_url_get_path() test1\n");
	printf("'%s' != '/a/path/is/here'\n", path);
	exit(1);
    } else {
	printf("passed lo_url_get_path() test1\n");
    }
    free(path);

    protocol = lo_url_get_protocol("osc.udp://localhost:9999/a/path/is/here");
    if (strcmp(protocol, "udp")) {
	printf("failed lo_url_get_protocol() test1\n");
	printf("'%s' != 'udp'\n", protocol);
	exit(1);
    } else {
	printf("passed lo_url_get_protocol() test1\n");
    }
    free(protocol);

    protocol = lo_url_get_protocol("osc.tcp://localhost:9999/a/path/is/here");
    if (strcmp(protocol, "tcp")) {
	printf("failed lo_url_get_protocol() test2\n");
	printf("'%s' != 'tcp'\n", protocol);
	exit(1);
    } else {
	printf("passed lo_url_get_protocol() test2\n");
    }
    free(protocol);
    
    protocol = lo_url_get_protocol("osc.udp://[::ffff:localhost]:9999/a/path/is/here");
    if (strcmp(protocol, "udp")) {
	printf("failed lo_url_get_protocol() test1 (IPv6)\n");
	printf("'%s' != 'udp'\n", protocol);
	exit(1);
    } else {
	printf("passed lo_url_get_protocol() test1 (IPv6)\n");
    }
    free(protocol);

    proto = lo_url_get_protocol_id("osc.udp://localhost:9999/a/path/is/here");
    if (proto != LO_UDP) {
	printf("failed lo_url_get_protocol_id() test1\n");
	printf("'%d' != LO_UDP\n", proto);
	exit(1);
    } else {
	printf("passed lo_url_get_protocol_id() test1\n");
    }

    proto = lo_url_get_protocol_id("osc.tcp://localhost:9999/a/path/is/here");
    if (proto != LO_TCP) {
	printf("failed lo_url_get_protocol_id() test2\n");
	printf("'%d' != LO_TCP\n", proto);
	exit(1);
    } else {
	printf("passed lo_url_get_protocol_id() test2\n");
    }
    
    proto = lo_url_get_protocol_id("osc.invalid://localhost:9999/a/path/is/here");
    if (proto != -1) {
	printf("failed lo_url_get_protocol_id() test3\n");
	printf("'%d' != -1\n", proto);
	exit(1);
    } else {
	printf("passed lo_url_get_protocol_id() test3\n");
    }
    
    proto = lo_url_get_protocol_id("osc.udp://[::ffff:localhost]:9999/a/path/is/here");
    if (proto != LO_UDP) {
	printf("failed lo_url_get_protocol_id() test1 (IPv6)\n");
	printf("'%d' != LO_UDP\n", proto);
	exit(1);
    } else {
	printf("passed lo_url_get_protocol_id() test1 (IPv6)\n");
    }

    host = lo_url_get_hostname("osc.udp://foo.example.com:9999/a/path/is/here");
    if (strcmp(host, "foo.example.com")) {
	printf("failed lo_url_get_hostname() test1\n");
	printf("'%s' != 'foo.example.com'\n", host);
	exit(1);
    } else {
	printf("passed lo_url_get_hostname() test1\n");
    }
    free(host);

    host = lo_url_get_hostname("osc.udp://[0000::::0001]:9999/a/path/is/here");
    if (strcmp(host, "0000::::0001")) {
	printf("failed lo_url_get_hostname() test2 (IPv6)\n");
	printf("'%s' != '0000::::0001'\n", host);
	exit(1);
    } else {
	printf("passed lo_url_get_hostname() test2 (IPv6)\n");
    }
    free(host);

    port = lo_url_get_port("osc.udp://localhost:9999/a/path/is/here");
    if (strcmp(port, "9999")) {
	printf("failed lo_url_get_port() test1\n");
	printf("'%s' != '9999'\n", port);
	exit(1);
    } else {
	printf("passed lo_url_get_port() test1\n");
    }
    free(port);
    
    port = lo_url_get_port("osc.udp://[::ffff:127.0.0.1]:9999/a/path/is/here");
    if (strcmp(port, "9999")) {
	printf("failed lo_url_get_port() test1 (IPv6)\n");
	printf("'%s' != '9999'\n", port);
	exit(1);
    } else {
	printf("passed lo_url_get_port() test1 (IPv6)\n");
    }
    free(port);
    printf("\n");
    
    
    
    
    
    a = lo_address_new_from_url("osc.tcp://foo.example.com:9999/");
    host2 = lo_address_get_hostname(a);
    if (strcmp(host2, "foo.example.com")) {
	printf("failed lo_address_get_hostname() test\n");
	printf("'%s' != 'foo.example.com'\n", host2);
	exit(1);
    } else {
	printf("passed lo_address_get_hostname() test\n");
    }

    port2 = lo_address_get_port(a);
    if (strcmp(port2, "9999")) {
	printf("failed lo_address_get_port() test\n");
	printf("'%s' != '9999'\n", port2);
	exit(1);
    } else {
	printf("passed lo_address_get_port() test\n");
    }

    proto = lo_address_get_protocol(a);
    if (proto != LO_TCP) {
	printf("failed lo_address_get_protocol() test\n");
	printf("'%d' != '%d'\n", proto, LO_TCP);
	exit(1);
    } else {
	printf("passed lo_address_get_protocol() test\n");
    }

    server_url = lo_address_get_url(a);
    if (strcmp(server_url, "osc.tcp://foo.example.com:9999/")) {
	printf("failed lo_address_get_url() test\n");
	printf("'%s' != '%s'\n", server_url, "osc.tcp://foo.example.com:9999/");
	exit(1);
    } else {
	printf("passed lo_address_get_url() test\n");
    }
    free(server_url);
    lo_address_free( a );
    printf("\n");
    

    /* Test blod sizes */
    if (lo_blob_datasize(btest) != 5 || lo_blobsize(btest) != 12) {
	printf("blob is %d (%d) bytes long, should be 5 (12)\n",
               lo_blob_datasize(btest), lo_blobsize(btest));
	lo_arg_pp(LO_BLOB, btest);
	printf(" <- blob\n");
	exit(1);
    }
    
    
    
    /* Server method handler tests */
    server_url = lo_server_thread_get_url(st);
    a = lo_address_new_from_url(server_url);
    printf("Server URL: %s\n", server_url);
    free(server_url);

    /* add method that will match the path /foo/bar, with two numbers, coerced
     * to float and int */

    lo_server_thread_add_method(st, "/foo/bar", "fi", foo_handler, lo_server_thread_get_server(st));

    lo_server_thread_add_method(st, "/reply", "s", reply_handler, NULL);

    lo_server_thread_add_method(st, "/lotsofformats", "fisbmhtdSccTFNI",
				lots_handler, NULL);

    lo_server_thread_add_method(st, "/coerce", "dfhiSs",
				coerce_handler, NULL);

    lo_server_thread_add_method(st, "/bundle", NULL,
				bundle_handler, NULL);
    lo_server_thread_add_method(st, "/timestamp", NULL,
				timestamp_handler, NULL);
    lo_server_thread_add_method(st, "/jitter", "ti",
				jitter_handler, NULL);

    lo_server_thread_add_method(st, "/pattern/foo", NULL,
				pattern_handler, "foo");
    lo_server_thread_add_method(st, "/pattern/bar", NULL,
				pattern_handler, "bar");
    lo_server_thread_add_method(st, "/pattern/baz", NULL,
				pattern_handler, "baz");

    lo_server_thread_add_method(st, "/subtest", "i",
				subtest_handler, st);

    lo_server_thread_add_method(st, "/subtest-reply", "i",
				subtest_reply_handler, NULL);

    /* add method that will match any path and args */
    lo_server_thread_add_method(st, NULL, NULL, generic_handler, NULL);

    /* add method that will match the path /quit with no args */
    lo_server_thread_add_method(st, "/quit", "", quit_handler, NULL);

    /* check that the thread restarts */
    lo_server_thread_start(st);
    lo_server_thread_stop(st);
    lo_server_thread_start(st);

    if (lo_send(a, "/foo/bar", "ff", 0.12345678f, 23.0f) == -1) {
	printf("OSC error A %d: %s\n", lo_address_errno(a), lo_address_errstr(a));
	exit(1);
    }

    if (lo_send(a, "/foo/bar", "ff", 0.12345678f, 23.0f) == -1) {
	printf("OSC error B %d: %s\n", lo_address_errno(a), lo_address_errstr(a));
	exit(1);
    }

    test_validation(a);
    test_multicast(st);

    lo_send(a, "/", "i", 242);
    lo_send(a, "/pattern/", "i", 243);

#ifndef _MSC_VER  /* MS compiler refuses to compile this case */
    lo_send(a, "/bar", "ff", 0.12345678f, 1.0/0.0);
#endif
    lo_send(a, "/lotsofformats", "fisbmhtdSccTFNI", 0.12345678f, 123, "123",
	    btest, midi_data, 0x0123456789abcdefULL, tt, 0.9999, "sym",
	    'X', 'Y');
    lo_send(a, "/coerce", "fdihsS", 0.1f, 0.2, 123, 124LL, "aaa", "bbb");
    lo_send(a, "/coerce", "ffffss", 0.1f, 0.2f, 123.0, 124.0, "aaa", "bbb");
    lo_send(a, "/coerce", "ddddSS", 0.1, 0.2, 123.0, 124.0, "aaa", "bbb");
    lo_send(a, "/a/b/c/d", "sfsff", "one", 0.12345678f, "three",
	    -0.00000023001f, 1.0);
    lo_send(a, "/a/b/c/d", "b", btest);

    TEST(test_varargs(a, "/lotsofformats", "fisbmhtdSccTFNI", 0.12345678f, 123,
                      "123", btest, midi_data, 0x0123456789abcdefULL, tt,
                      0.9999, "sym", 'X', 'Y', LO_ARGS_END) == 0);

#ifdef __GNUC__
    // Note: Lack of support for variable-argument macros in non-GCC compilers
    //       does not allow us to test for these conditions.

    // too many args
    TEST(test_varargs(a, "/lotsofformats", "f", 0.12345678f, 123,
                      "123", btest, midi_data, 0x0123456789abcdefULL, tt,
                      0.9999, "sym", 'X', 'Y', LO_ARGS_END) != 0);
    // too many types
    TEST(test_varargs(a, "/lotsofformats", "fisbmhtdSccTFNI", 0.12345678f, 123,
                      "123", btest, midi_data, 0x0123456789abcdefULL, tt, 0.5,
                      LO_ARGS_END) != 0);
#endif

    // test lo_message_add
    m1 = lo_message_new();
    TEST(lo_message_add(m1, "fisbmhtdSccTFNI", 0.12345678f, 123, "123",
                        btest, midi_data, 0x0123456789abcdefULL, tt, 0.9999, "sym",
                        'X', 'Y') == 0);
    lo_send_message(a, "/lotsofformats", m1);
    lo_message_free(m1);
 
    lo_blob_free(btest);

    lo_send(a, "/pattern/*", "s", "a");
    lo_send(a, "/pattern/ba[rz]", "s", "b");

    server_url = lo_server_thread_get_url(st);
    sprintf(cmd, "." PATHDELIM "subtest %s &", server_url);
    if (system(cmd) != 0) {
	fprintf(stderr, "Cannot execute subtest command\n");
	exit(1);
    }
    system(cmd);
    free(server_url);

#ifdef WIN32
    Sleep(2000);
#else
    sleep(2);
#endif
    TEST(reply_count == 3);
    TEST(pattern_count == 5);
    TEST(subtest_count == 2);
    TEST(subtest_reply_count == 22);
    printf("\n");

    {
        lo_timetag t = {10,0xFFFFFFFC};
        b = lo_bundle_new(t);
    }
    m1 = lo_message_new();
    lo_message_add_string(m1, "abcdefghijklmnopqrstuvwxyz");
    lo_message_add_string(m1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    lo_bundle_add_message(b, "/bundle", m1);
    lo_send_bundle(a, b);

    /* This should be safe for multiple copies of the same message. */
    lo_bundle_free_messages(b);

    {
        lo_timetag t = {1,2};
        b = lo_bundle_new(t);
    }
    m1 = lo_message_new();
    lo_message_add_int32(m1, 23);
    lo_message_add_string(m1, "23");
    lo_bundle_add_message(b, "/bundle", m1);
    m2 = lo_message_new();
    lo_message_add_string(m2, "24");
    lo_message_add_int32(m2, 24);
    lo_bundle_add_message(b, "/bundle", m2);
    lo_bundle_add_message(b, "/bundle", m1);

/* 
    lo_send_bundle(a, b);
    if (a->errnum) {
	printf("error %d: %s\n", a->errnum, a->errstr);
	exit(1);
    }
*/
    TEST(lo_send_bundle(a, b) == 88);

    /* Test freeing out-of-order copies of messages in a bundle. */
    lo_bundle_free_messages(b);

    {
        lo_timetag t = {10,0xFFFFFFFE};
        b = lo_bundle_new(t);
    }
    m1 = lo_message_new();
    lo_message_add_string(m1, "abcdefghijklmnopqrstuvwxyz");
    lo_message_add_string(m1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    lo_bundle_add_message(b, "/bundle", m1);
    lo_send_bundle(a, b);
    lo_message_free(m1);
    lo_bundle_free(b);

    lo_timetag_now(&sched);

    sched.sec += 5;
    b = lo_bundle_new(sched);
    m1 = lo_message_new();
    lo_message_add_string(m1, "future");
    lo_message_add_string(m1, "time");
    lo_message_add_string(m1, "test");
    lo_bundle_add_message(b, "/bundle", m1);

    lo_send_bundle(a, b);
    lo_message_free(m1);
    lo_bundle_free(b);

    lo_send_timestamped(a, sched, "/bundle", "s", "lo_send_timestamped() test");

    /* test bundle timestamp ends up in message struct (and doesn't end up in
       unbundled messages) */
    lo_timetag_now(&sched);    
    lo_send_timestamped(a, sched, "/timestamp", "it", 1, sched);
    lo_send(a, "/timestamp", "it", 0, sched);

#define JITTER_ITS 25
    /* jitter tests */
    {
	lo_timetag stamps[JITTER_ITS];
	lo_timetag now;
	int i;

	for (i=0; i<JITTER_ITS; i++) {
	    lo_timetag_now(&now);
	    stamps[i] = now;
	    stamps[i].sec += 1;
	    stamps[i].frac = rand();
	    lo_send_timestamped(a, stamps[i], "/jitter", "ti", stamps[i], i);
	}
    }

#ifdef WIN32
    Sleep(2000);
#else
    sleep(2);
#endif

    lo_address_free(a);

    TEST(lo_server_thread_events_pending(st));

    while (lo_server_thread_events_pending(st)) {
	printf("pending events, wait...\n");
#ifdef WIN32
	fflush(stdout);
	Sleep(1000);
#else
	sleep(1);
#endif
    }
    
    TEST(bundle_count == 7);
    printf("\n");

    printf("bundle timing jitter results:\n"
	   "max jitter = %fs\n"
	   "avg jitter = %fs\n"
           "min jitter = %fs\n\n",
           jitter_max, jitter_total/(float)jitter_count, jitter_min);

    server_url = lo_server_get_url(s);

    lo_server_add_method(s, NULL, NULL, generic_handler, NULL);
    a = lo_address_new_from_url(server_url);
    TEST(lo_server_recv_noblock(s, 0) == 0);
    printf("Testing noblock API on %s\n", server_url);
    lo_send(a, "/non-block-test", "f", 23.0);

    count = 0;
    while (!lo_server_recv_noblock(s, 10) && count++ < 1000) { }
    if (count >= 1000) {
	printf("lo_server_recv_noblock() test failed\n");

	exit(1);
    }

    /* Delete methods */
    lo_server_thread_del_method(st, "/coerce", "dfhiSs");
    lo_server_del_method(s, NULL, NULL);

    lo_address_free(a);
    lo_server_free(s);
    free(server_url);

#ifndef WIN32
    { /* UNIX domain tests */
	lo_address ua;
	lo_server us;
	char *addr;

	unlink("/tmp/testlo.osc");
	us = lo_server_new_with_proto("/tmp/testlo.osc", LO_UNIX, error);
	ua = lo_address_new_from_url("osc.unix:///tmp/testlo.osc");
	TEST(lo_server_get_protocol(us) == LO_UNIX);
	TEST(lo_send(ua, "/unix", "f", 23.0) == 16);
	TEST(lo_server_recv(us) == 16);
	addr = lo_server_get_url(us);
	TEST(!strcmp("osc.unix:////tmp/testlo.osc", addr));
	free(addr);
	lo_address_free(ua);
	ua = lo_address_new_with_proto(LO_UNIX, NULL, "/tmp/testlo.osc");
	TEST(lo_send(ua, "/unix", "f", 23.0) == 16);
	TEST(lo_server_recv(us) == 16);
	lo_server_free(us);
	lo_address_free(ua);
    }
#endif

    { /* TCP tests */
	lo_address ta;
	lo_server ts;
	char *addr;

	ts = lo_server_new_with_proto(NULL, LO_TCP, error);
	addr = lo_server_get_url(ts);
	ta = lo_address_new_from_url(addr);
	if (lo_address_errno(ta)) {
	    printf("err: %s\n", lo_address_errstr(ta));
	    exit(1);
	}
	TEST(lo_server_get_protocol(ts) == LO_TCP);
	TEST(lo_send(ta, "/tcp", "f", 23.0) == 16);
	TEST(lo_send(ta, "/tcp", "f", 23.0) == 16);
	TEST(lo_server_recv(ts) == 16);
	TEST(lo_server_recv(ts) == 16);
	free(addr);
	lo_server_free(ts);
	lo_address_free(ta);
    }

    server_url = lo_server_thread_get_url(st);
    a = lo_address_new_from_url(server_url);
    /* exit */
    lo_send(a, "/quit", NULL);
    lo_address_free(a);

    while (!done) {
#ifdef WIN32
    Sleep(1);
#else
	usleep(1000);
#endif
    }

    lo_server_thread_free(st);
    free(server_url);


    return 0;
}
Exemplo n.º 28
0
void printPacketInfo(const MIDIPacket* packet) {
   double timeinsec = packet->timeStamp / (double)1e9;
//    printf("%9.3lf\t", timeinsec);
   int i;
   if (packet->length == 3) {
       if ((packet->data[0] & 0xf0) == 0x90) {
           // Note down
           int channel = packet->data[0] & 0x0f;
           int note = packet->data[1];
           int velocity = packet->data[2];
           printf("Down chan=%d %d vel=%d\n", channel, note, velocity);
           char keyboard[16], trigger[16];
           if (!channel) {
               sprintf(keyboard, "keyboard");
               sprintf(trigger, "trigger");
           } else {
               sprintf(keyboard, "keyboard%d", channel+1);
               sprintf(trigger, "trigger%d", channel+1);
           }
           if (lo_send(t, "/set", "ssf", keyboard, "result", 0.1*(note-48)/12.0) == -1) {
               printf("OSC error %d: %s\n", lo_address_errno(t),
                   lo_address_errstr(t));
           }
           if (lo_send(t, "/set", "ssf", trigger, "result", velocity/127.0) == -1) {
               printf("OSC error %d: %s\n", lo_address_errno(t),
                   lo_address_errstr(t));
           }
       } else if ((packet->data[0] & 0xf0) == 0x80) {
           // Note up
           int channel = packet->data[0] & 0x0f;
           int note = packet->data[1];
           int velocity = 0;//packet->data[2];
           printf("Up chan=%d %d vel=%d\n", channel, note, velocity);
           char keyboard[16], trigger[16];
           if (!channel) {
               sprintf(keyboard, "keyboard");
               sprintf(trigger, "trigger");
           } else {
               sprintf(keyboard, "keyboard%d", channel+1);
               sprintf(trigger, "trigger%d", channel+1);
           }
           if (lo_send(t, "/set", "ssf", keyboard, "result", 0.1*(note-48)/12.0) == -1) {
               printf("OSC error %d: %s\n", lo_address_errno(t),
                   lo_address_errstr(t));
           }
           if (lo_send(t, "/set", "ssf", trigger, "result", velocity/127.0) == -1) {
               printf("OSC error %d: %s\n", lo_address_errno(t),
                   lo_address_errstr(t));
           }
       } else if ((packet->data[0] & 0xf0) == 0xe0) {
           // Pitch bend
           int channel = packet->data[0] & 0x0f;
           int lo_byte = packet->data[1];
           int hi_byte = packet->data[2];
           printf("bend chan=%d value=%d %d\n", channel, hi_byte, lo_byte);
           int value = hi_byte*0x80+lo_byte;
           char bend[16];
           if (!channel) {
               sprintf(bend, "bend");
           } else {
               sprintf(bend, "bend%d", channel+1);
           }
           if (lo_send(t, "/set", "ssf", bend, "result", (float)value/0x4000) == -1) {
               printf("OSC error %d: %s\n", lo_address_errno(t),
                   lo_address_errstr(t));
           }
       } else if ((packet->data[0] & 0xf0) == 0xb0 && packet->data[1] == 1) {
           // Pitch modulation
           int channel = packet->data[0] & 0x0f;
           int value = packet->data[2];
           printf("modulation chan=%d value=%d\n", channel, value);
           char modulation[16];
           if (!channel) {
               sprintf(modulation, "modulation");
           } else {
               sprintf(modulation, "modulation%d", channel+1);
           }
           if (lo_send(t, "/set", "ssf", modulation, "result", (float)value/0x80) == -1) {
               printf("OSC error %d: %s\n", lo_address_errno(t),
                   lo_address_errstr(t));
           }
       }
   }
#if 0
   for (i=0; i<packet->length; i++) {
      if (packet->data[i] < 0x7f) {
         printf("%d ", packet->data[i]);
      } else {
         printf("0x%x ", packet->data[i]);
      }
   }
   printf("\n");
#endif
}