int setup_maps() { mapper_map map = mapper_map_new(num_sources, sendsigs, 1, &recvsig); if (!map) { eprintf("Failed to create map\n"); return 1; } mapper_map_set_mode(map, MAPPER_MODE_EXPRESSION); // build expression string int i, offset = 2, len = num_sources * 3 + 3; char expr[len]; snprintf(expr, 3, "y="); for (i = 0; i < num_sources; i++) { mapper_slot slot = mapper_map_slot_by_signal(map, sendsigs[i]); snprintf(expr + offset, len - offset, "-x%d", mapper_slot_index(slot)); if (i > 0) mapper_slot_set_causes_update(slot, 0); offset += 3; } mapper_map_set_expression(map, expr); mapper_map_push(map); // wait until mappings have been established while (!done && !mapper_map_ready(map)) { for (i = 0; i < num_sources; i++) mapper_device_poll(sources[i], 10); mapper_device_poll(destination, 10); } return 0; }
void map_signals() { mapper_map map = mapper_map_new(1, &sendsig, 1, &recvsig); mapper_map_set_mode(map, MAPPER_MODE_EXPRESSION); mapper_map_set_expression(map, "y{-1}=-10;y=y{-1}+1"); mapper_map_push(map); // wait until mapping has been established while (!done && !mapper_map_ready(map)) { mapper_device_poll(source, 10); mapper_device_poll(destination, 10); } }
int setup_maps() { mapper_map map = mapper_map_new(1, &sendsig, 1, &recvsig); mapper_map_set_mode(map, MAPPER_MODE_EXPRESSION); mapper_map_set_expression(map, "y=x*10"); mapper_map_push(map); // wait until mapping has been established while (!done && !mapper_map_ready(map)) { mapper_device_poll(source, 10); mapper_device_poll(destination, 10); } return 0; }
// ********************************************************* // -(connection handler)------------------------------------ void impmap_on_map(mapper_device dev, mapper_map map, mapper_record_event e) { // if connected involves current generic signal, create a new generic signal impmap *x = (void*)mapper_device_user_data(dev); if (!x) { post("error in connect handler: user_data is NULL"); return; } if (!x->ready) { post("error in connect handler: device not ready"); return; } // retrieve devices and signals mapper_slot slot = mapper_map_slot(map, MAPPER_LOC_SOURCE, 0); mapper_signal src_sig = mapper_slot_signal(slot); mapper_device src_dev = mapper_signal_device(src_sig); slot = mapper_map_slot(map, MAPPER_LOC_DESTINATION, 0); mapper_signal dst_sig = mapper_slot_signal(slot); mapper_device dst_dev = mapper_signal_device(dst_sig); // sanity check: don't allow self-connections if (src_dev == dst_dev) { mapper_map_release(map); return; } char full_name[256]; if (e == MAPPER_ADDED) { if (src_dev == x->device) { snprintf(full_name, 256, "%s/%s", mapper_device_name(dst_dev), mapper_signal_name(dst_sig)); if (strcmp(mapper_signal_name(src_sig), full_name) == 0) { // <thisDev>:<dstDevName>/<dstSigName> -> <dstDev>:<dstSigName> return; } if (mapper_device_num_signals(x->device, MAPPER_DIR_OUTGOING) >= MAX_LIST) { post("Max outputs reached!"); return; } // unmap the generic signal mapper_map_release(map); // add a matching output signal int i, length = mapper_signal_length(dst_sig); char type = mapper_signal_type(dst_sig); void *min = mapper_signal_minimum(dst_sig); void *max = mapper_signal_maximum(dst_sig); float *minf = 0, *maxf = 0; if (type == 'f') { minf = (float*)min; maxf = (float*)max; } else { if (min) { minf = alloca(length * sizeof(float)); if (type == 'i') { int *mini = (int*)min; for (i = 0; i < length; i++) minf[i] = (float)mini[i]; } else if (type == 'd') { double *mind = (double*)min; for (i = 0; i < length; i++) minf[i] = (float)mind[i]; } else minf = 0; } if (max) { maxf = alloca(length * sizeof(float)); if (type == 'i') { int *maxi = (int*)max; for (i = 0; i < length; i++) maxf[i] = (float)maxi[i]; } else if (type == 'd') { double *maxd = (double*)max; for (i = 0; i < length; i++) maxf[i] = (float)maxd[i]; } else maxf = 0; } } src_sig = mapper_device_add_output_signal(x->device, full_name, length, 'f', 0, minf, maxf); if (!src_sig) { post("error creating new source signal!"); return; } mapper_signal_set_callback(src_sig, impmap_on_query); // map the new signal map = mapper_map_new(1, &src_sig, 1, &dst_sig); mapper_map_set_mode(map, MAPPER_MODE_EXPRESSION); mapper_map_set_expression(map, "y=x"); mapper_map_push(map); impmap_update_output_vector_positions(x); //output numOutputs maxpd_atom_set_int(&x->msg_buffer, mapper_device_num_signals(x->device, MAPPER_DIR_OUTGOING) - 1); outlet_anything(x->outlet3, gensym("numOutputs"), 1, &x->msg_buffer); } else if (dst_dev == x->device) { snprintf(full_name, 256, "%s/%s", mapper_device_name(src_dev), mapper_signal_name(src_sig)); if (strcmp(mapper_signal_name(dst_sig), full_name) == 0) { // <srcDevName>:<srcSigName> -> <thisDev>:<srcDevName>/<srcSigName> return; } if (mapper_device_num_signals(x->device, MAPPER_DIR_INCOMING) >= MAX_LIST) { post("Max inputs reached!"); return; } // unmap the generic signal mapper_map_release(map); // add a matching input signal int i, length = mapper_signal_length(src_sig); char type = mapper_signal_type(src_sig); void *min = mapper_signal_minimum(src_sig); void *max = mapper_signal_maximum(src_sig); float *minf = 0, *maxf = 0; if (type == 'f') { minf = (float*)min; maxf = (float*)max; } else { if (min) { minf = alloca(length * sizeof(float)); if (type == 'i') { int *mini = (int*)min; for (i = 0; i < length; i++) minf[i] = (float)mini[i]; } else if (type == 'd') { double *mind = (double*)min; for (i = 0; i < length; i++) minf[i] = (float)mind[i]; } else minf = 0; } if (max) { maxf = alloca(length * sizeof(float)); if (type == 'i') { int *maxi = (int*)max; for (i = 0; i < length; i++) maxf[i] = (float)maxi[i]; } else if (type == 'd') { double *maxd = (double*)max; for (i = 0; i < length; i++) maxf[i] = (float)maxd[i]; } else maxf = 0; } } dst_sig = mapper_device_add_input_signal(x->device, full_name, length, 'f', 0, minf, maxf, impmap_on_input, 0); if (!dst_sig) { post("error creating new destination signal!"); return; } // map the new signal map = mapper_map_new(1, &src_sig, 1, &dst_sig); mapper_map_set_mode(map, MAPPER_MODE_EXPRESSION); mapper_map_set_expression(map, "y=x"); mapper_map_push(map); impmap_update_input_vector_positions(x); //output numInputs maxpd_atom_set_int(&x->msg_buffer, mapper_device_num_signals(x->device, MAPPER_DIR_INCOMING) - 1); outlet_anything(x->outlet3, gensym("numInputs"), 1, &x->msg_buffer); } } else if (e == MAPPER_REMOVED) { if (src_sig == x->dummy_input || src_sig == x->dummy_output || dst_sig == x->dummy_input || dst_sig == x->dummy_output) return; if (src_dev == x->device) { snprintf(full_name, 256, "%s/%s", mapper_device_name(dst_dev), mapper_signal_name(dst_sig)); if (strcmp(mapper_signal_name(src_sig), full_name) != 0) return; // remove signal mapper_device_remove_signal(x->device, src_sig); impmap_update_input_vector_positions(x); //output numOutputs maxpd_atom_set_int(&x->msg_buffer, mapper_device_num_signals(x->device, MAPPER_DIR_OUTGOING) - 1); outlet_anything(x->outlet3, gensym("numOutputs"), 1, &x->msg_buffer); } else if (dst_dev == x->device) { snprintf(full_name, 256, "%s/%s", mapper_device_name(src_dev), mapper_signal_name(src_sig)); if (strcmp(mapper_signal_name(dst_sig), full_name) != 0) return; // remove signal mapper_device_remove_signal(x->device, dst_sig); impmap_update_input_vector_positions(x); //output numInputs maxpd_atom_set_int(&x->msg_buffer, mapper_device_num_signals(x->device, MAPPER_DIR_INCOMING) - 1); outlet_anything(x->outlet3, gensym("numInputs"), 1, &x->msg_buffer); } } }