Commit e1d31d1f authored by sletz's avatar sletz
Browse files

Add jack_midi_dump client.

git-svn-id: http://subversion.jackaudio.org/jack/jack2/trunk/jackmp@4170 0c269be4-1314-0410-8aa9-9f06e86f4224
parent 2eeb1f0a
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <jack/jack.h>
#include <jack/midiport.h>
static jack_port_t* port;
static void
describe (jack_midi_event_t* event, char* buffer, size_t buflen)
{
assert (buflen > 0);
buffer[0] = '\0';
if (event->size == 0) {
return;
}
int type = event->buffer[0] & 0xf0;
int channel = event->buffer[0] & 0xf;
switch (type) {
case 0x90:
assert (event->size == 3);
snprintf (buffer, buflen, "note on (channel %d): pitch %d, velocity %d", channel, event->buffer[1], event->buffer[2]);
break;
case 0x80:
assert (event->size == 3);
snprintf (buffer, buflen, "note off (channel %d): pitch %d, velocity %d", channel, event->buffer[1], event->buffer[2]);
break;
case 0xb0:
assert (event->size == 3);
snprintf (buffer, buflen, "control change (channel %d): controller %d, value %d", channel, event->buffer[1], event->buffer[2]);
break;
default:
break;
}
}
int
process (jack_nframes_t frames, void* arg)
{
void* buffer;
jack_nframes_t N;
jack_nframes_t i;
char description[256];
buffer = jack_port_get_buffer (port, frames);
assert (buffer);
N = jack_midi_get_event_count (buffer);
for (i = 0; i < N; ++i) {
jack_midi_event_t event;
int r;
r = jack_midi_event_get (&event, buffer, i);
if (r == 0) {
size_t j;
printf ("%d:", event.time);
for (j = 0; j < event.size; ++j) {
printf (" %x", event.buffer[j]);
}
describe (&event, description, sizeof (description));
printf (" %s", description);
printf ("\n");
}
}
return 0;
}
int
main (int argc, char* argv[])
{
jack_client_t* client;
char const default_name[] = "midi-monitor";
char const * client_name;
int r;
if (argc == 2) {
client_name = argv[1];
} else {
client_name = default_name;
}
client = jack_client_open (client_name, JackNullOption, NULL);
if (client == NULL) {
fprintf (stderr, "Could not create JACK client.\n");
exit (EXIT_FAILURE);
}
jack_set_process_callback (client, process, 0);
port = jack_port_register (client, "input", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
if (port == NULL) {
fprintf (stderr, "Could not register port.\n");
exit (EXIT_FAILURE);
}
r = jack_activate (client);
if (r != 0) {
fprintf (stderr, "Could not activate client.\n");
exit (EXIT_FAILURE);
}
sleep (-1);
return 0;
}
......@@ -25,12 +25,11 @@
#include <stdlib.h>
#include <string.h>
#include <jack/jack.h>
#include <jack/types.h>
#include <jack/jslist.h>
#include <jack/transport.h>
#include <jack/session.h>
char *package; /* program name */
char *package; /* program name */
jack_client_t *client;
jack_session_event_type_t notify_type;
......@@ -38,147 +37,145 @@ char *save_path = NULL;
void jack_shutdown(void *arg)
{
fprintf(stderr, "JACK shut down, exiting ...\n");
exit(1);
fprintf(stderr, "JACK shut down, exiting ...\n");
exit(1);
}
void signal_handler(int sig)
{
jack_client_close(client);
fprintf(stderr, "signal received, exiting ...\n");
exit(0);
jack_client_close(client);
fprintf(stderr, "signal received, exiting ...\n");
exit(0);
}
void parse_arguments(int argc, char *argv[])
{
/* basename $0 */
package = strrchr(argv[0], '/');
if (package == 0)
package = argv[0];
else
package++;
if (argc==2) {
if( !strcmp( argv[1], "quit" ) ) {
notify_type = JackSessionSaveAndQuit;
return;
}
}
if (argc==3) {
if( !strcmp( argv[1], "save" ) ) {
notify_type = JackSessionSave;
save_path = argv[2];
return;
}
}
fprintf(stderr, "usage: %s quit|save [path]\n", package);
exit(9);
/* basename $0 */
package = strrchr(argv[0], '/');
if (package == 0)
package = argv[0];
else
package++;
if (argc==2) {
if( !strcmp( argv[1], "quit" ) ) {
notify_type = JackSessionSaveAndQuit;
return;
}
}
if (argc==3) {
if( !strcmp( argv[1], "save" ) ) {
notify_type = JackSessionSave;
save_path = argv[2];
return;
}
}
fprintf(stderr, "usage: %s quit|save [path]\n", package);
exit(9);
}
typedef struct {
char name[32];
char uuid[16];
char name[32];
char uuid[16];
} uuid_map_t;
JSList *uuid_map = NULL;
void add_uuid_mapping( const char *uuid ) {
char *clientname = jack_get_client_name_by_uuid( client, uuid );
if( !clientname ) {
printf( "error... cant find client for uuid %s", uuid );
return;
}
uuid_map_t *mapping = malloc( sizeof(uuid_map_t) );
snprintf( mapping->uuid, sizeof(mapping->uuid), "%s", uuid );
snprintf( mapping->name, sizeof(mapping->name), "%s", clientname );
uuid_map = jack_slist_append( uuid_map, mapping );
char *clientname = jack_get_client_name_by_uuid( client, uuid );
if( !clientname ) {
printf( "error... cant find client for uuid" );
return;
}
uuid_map_t *mapping = malloc( sizeof(uuid_map_t) );
snprintf( mapping->uuid, sizeof(mapping->uuid), "%s", uuid );
snprintf( mapping->name, sizeof(mapping->name), "%s", clientname );
uuid_map = jack_slist_append( uuid_map, mapping );
}
char *map_port_name_to_uuid_port( const char *port_name )
{
JSList *node;
char retval[300];
char *port_component = strchr( port_name,':' );
char *client_component = strdup( port_name );
strchr( client_component, ':' )[0] = '\0';
sprintf( retval, "%s", port_name );
for( node=uuid_map; node; node=jack_slist_next(node) ) {
uuid_map_t *mapping = node->data;
if( !strcmp( mapping->name, client_component ) ) {
sprintf( retval, "%s%s", mapping->uuid, port_component );
break;
}
}
return strdup(retval);
JSList *node;
char retval[300];
char *port_component = strchr( port_name,':' );
char *client_component = strdup( port_name );
strchr( client_component, ':' )[0] = '\0';
sprintf( retval, "%s", port_name );
for( node=uuid_map; node; node=jack_slist_next(node) ) {
uuid_map_t *mapping = node->data;
if( !strcmp( mapping->name, client_component ) ) {
sprintf( retval, "%s%s", mapping->uuid, port_component );
break;
}
}
return strdup(retval);
}
int main(int argc, char *argv[])
{
parse_arguments(argc, argv);
jack_session_command_t *retval;
int k,i,j;
/* become a JACK client */
if ((client = jack_client_open(package, JackNullOption, NULL)) == 0) {
fprintf(stderr, "JACK server not running?\n");
exit(1);
}
signal(SIGQUIT, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGHUP, signal_handler);
signal(SIGINT, signal_handler);
jack_on_shutdown(client, jack_shutdown, 0);
jack_activate(client);
retval = jack_session_notify( client, NULL, notify_type, save_path );
printf( "retval = %p\n", retval );
for(i=0; retval[i].uuid; i++ ) {
printf( "export SESSION_DIR=\"%s%s/\"\n", save_path, retval[i].client_name );
printf( "%s &\n", retval[i].command );
add_uuid_mapping(retval[i].uuid);
}
printf( "sleep 10\n" );
for(k=0; retval[k].uuid; k++ ) {
char* port_regexp = alloca( jack_client_name_size()+3 );
char* client_name = jack_get_client_name_by_uuid( client, retval[k].uuid );
snprintf( port_regexp, jack_client_name_size()+3, "%s:.*", client_name );
jack_free(client_name);
const char **ports = jack_get_ports( client, port_regexp, NULL, 0 );
if( !ports ) {
continue;
}
for (i = 0; ports[i]; ++i) {
const char **connections;
if ((connections = jack_port_get_all_connections (client, jack_port_by_name(client, ports[i]))) != 0) {
for (j = 0; connections[j]; j++) {
char *src = map_port_name_to_uuid_port( ports[i] );
char *dst = map_port_name_to_uuid_port( connections[j] );
printf( "jack_connect -u \"%s\" \"%s\"\n", src, dst );
}
jack_free (connections);
}
}
jack_free(ports);
}
jack_session_commands_free(retval);
jack_client_close(client);
return 0;
parse_arguments(argc, argv);
jack_session_command_t *retval;
int k,i,j;
/* become a JACK client */
if ((client = jack_client_open(package, JackNullOption, NULL)) == 0) {
fprintf(stderr, "JACK server not running?\n");
exit(1);
}
signal(SIGQUIT, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGHUP, signal_handler);
signal(SIGINT, signal_handler);
jack_on_shutdown(client, jack_shutdown, 0);
jack_activate(client);
retval = jack_session_notify( client, NULL, notify_type, save_path );
for(i=0; retval[i].uuid; i++ ) {
printf( "export SESSION_DIR=\"%s%s/\"\n", save_path, retval[i].client_name );
printf( "%s &\n", retval[i].command );
add_uuid_mapping(retval[i].uuid);
}
printf( "sleep 10\n" );
for(k=0; retval[k].uuid; k++ ) {
char* port_regexp = alloca( jack_client_name_size()+3 );
char* client_name = jack_get_client_name_by_uuid( client, retval[k].uuid );
snprintf( port_regexp, jack_client_name_size()+3, "%s:.*", client_name );
jack_free(client_name);
const char **ports = jack_get_ports( client, port_regexp, NULL, 0 );
if( !ports ) {
continue;
}
for (i = 0; ports[i]; ++i) {
const char **connections;
if ((connections = jack_port_get_all_connections (client, jack_port_by_name(client, ports[i]))) != 0) {
for (j = 0; connections[j]; j++) {
char *src = map_port_name_to_uuid_port( ports[i] );
char *dst = map_port_name_to_uuid_port( connections[j] );
printf( "jack_connect -u \"%s\" \"%s\"\n", src, dst );
}
jack_free (connections);
}
}
jack_free(ports);
}
jack_session_commands_free(retval);
jack_client_close(client);
return 0;
}
......@@ -26,6 +26,7 @@ example_programs = {
'jack_session_notify' : 'session_notify.c',
'jack_server_control' : 'server_control.cpp',
'jack_latent_client' : 'latent_client.c',
'jack_midi_dump' : 'midi_dump.c',
}
example_libs = {
......
......@@ -49,6 +49,7 @@
4B32258F10A31AB400838A8E /* PBXTargetDependency */,
4B66550E127C356E00753A79 /* PBXTargetDependency */,
4B38120313269CCB00C61B14 /* PBXTargetDependency */,
4B8F16FC1329169F0002AD73 /* PBXTargetDependency */,
);
name = "All Universal 32/64 bits";
productName = All;
......@@ -96,6 +97,7 @@
4B363F780DEB0D85001F72D9 /* PBXTargetDependency */,
4B32258B10A31A9000838A8E /* PBXTargetDependency */,
4B38120113269CB600C61B14 /* PBXTargetDependency */,
4B8F16FA132916910002AD73 /* PBXTargetDependency */,
);
name = "All Universal 32 bits";
productName = All;
......@@ -581,6 +583,8 @@
4B8A38F1117B827E00664E07 /* JackSocketClientChannel.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4BC3B6AF0E703B8D0066E42F /* JackSocketClientChannel.cpp */; };
4B8A38F6117B82AB00664E07 /* JackSocket.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4BC3B6AD0E703B8D0066E42F /* JackSocket.cpp */; };
4B8A38F7117B82B200664E07 /* JackSocketClientChannel.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BC3B6B00E703B8D0066E42F /* JackSocketClientChannel.h */; };
4B8F16F51329161E0002AD73 /* midi_dump.c in Sources */ = {isa = PBXBuildFile; fileRef = 4B8F16F41329161E0002AD73 /* midi_dump.c */; };
4B8F16F61329161E0002AD73 /* midi_dump.c in Sources */ = {isa = PBXBuildFile; fileRef = 4B8F16F41329161E0002AD73 /* midi_dump.c */; };
4B93F1990E87992100E4ECCD /* JackPosixThread.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4BC3B6A20E703B2E0066E42F /* JackPosixThread.cpp */; };
4B93F19A0E87992200E4ECCD /* JackPosixThread.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BC3B6A30E703B2E0066E42F /* JackPosixThread.h */; };
4B93F19C0E87998200E4ECCD /* JackPosixServerLaunch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4BF5FBBA0E878B9C003D2374 /* JackPosixServerLaunch.cpp */; };
......@@ -1195,6 +1199,20 @@
remoteGlobalIDString = 4B699D03097D421600A18468;
remoteInfo = "jack_external_metro Universal";
};
4B8F16F9132916910002AD73 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 08FB7793FE84155DC02AAC07 /* Project object */;
proxyType = 1;
remoteGlobalIDString = 4B8F16DB13290DC80002AD73 /* jack_midi_dump Universal */;
remoteInfo = "jack_midi_dump Universal";
};
4B8F16FB1329169F0002AD73 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 08FB7793FE84155DC02AAC07 /* Project object */;
proxyType = 1;
remoteGlobalIDString = 4B8F16E813290E0E0002AD73 /* jack_midi_dump 64 bits */;
remoteInfo = "jack_midi_dump 64 bits";
};
4BA693E80CBE5BBA00EAD520 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 08FB7793FE84155DC02AAC07 /* Project object */;
......@@ -1569,6 +1587,9 @@
4B869D7F08C9CB00001CF041 /* JackDriverLoader.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = JackDriverLoader.cpp; path = ../common/JackDriverLoader.cpp; sourceTree = SOURCE_ROOT; };
4B88D03911298BEE007A87C1 /* weakjack.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = weakjack.h; path = ../common/jack/weakjack.h; sourceTree = SOURCE_ROOT; };
4B88D03A11298BEE007A87C1 /* weakmacros.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = weakmacros.h; path = ../common/jack/weakmacros.h; sourceTree = SOURCE_ROOT; };
4B8F16E513290DC80002AD73 /* jack_midi_dump */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = jack_midi_dump; sourceTree = BUILT_PRODUCTS_DIR; };
4B8F16F213290E0E0002AD73 /* jack_midi_dump */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = jack_midi_dump; sourceTree = BUILT_PRODUCTS_DIR; };
4B8F16F41329161E0002AD73 /* midi_dump.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = midi_dump.c; path = "../example-clients/midi_dump.c"; sourceTree = SOURCE_ROOT; };
4B940B9B06DDDE5B00D77F60 /* AudioHardware.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = AudioHardware.h; path = /System/Library/Frameworks/CoreAudio.framework/Versions/A/Headers/AudioHardware.h; sourceTree = "<absolute>"; };
4B94334910A5E666002A187F /* systemdeps.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = systemdeps.h; path = ../common/jack/systemdeps.h; sourceTree = SOURCE_ROOT; };
4B95BCAD0D913073000F7695 /* control.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = control.h; path = ../common/jack/control.h; sourceTree = SOURCE_ROOT; };
......@@ -2212,6 +2233,20 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
4B8F16DF13290DC80002AD73 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
4B8F16EC13290E0E0002AD73 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
4B978DB60A31CF4A009E2DD1 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
......@@ -2521,6 +2556,8 @@
4B6654F7127C34AE00753A79 /* jack_server_control */,
4B38115F1326878E00C61B14 /* jack_latent_client */,
4B3811971326884E00C61B14 /* jack_latent_client */,
4B8F16E513290DC80002AD73 /* jack_midi_dump */,
4B8F16F213290E0E0002AD73 /* jack_midi_dump */,
);
name = Products;
sourceTree = "<group>";
......@@ -2528,6 +2565,7 @@
4B03383E0797E19900686131 /* Simple clients */ = {
isa = PBXGroup;
children = (
4B8F16F41329161E0002AD73 /* midi_dump.c */,
4B3811FA13269C8300C61B14 /* latent_client.c */,
4B6654FB127C350100753A79 /* server_control.cpp */,
4B363F750DEB0D7D001F72D9 /* impulse_grabber.c */,
......@@ -3779,6 +3817,20 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
4B8F16DC13290DC80002AD73 /* Headers */ = {
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
4B8F16E913290E0E0002AD73 /* Headers */ = {
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
4B978DB20A31CF4A009E2DD1 /* Headers */ = {
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
......@@ -5260,6 +5312,44 @@
productReference = 4B699DB0097D421700A18468 /* jack_dummy.so */;
productType = "com.apple.product-type.library.dynamic";
};
4B8F16DB13290DC80002AD73 /* jack_midi_dump Universal */ = {
isa = PBXNativeTarget;
buildConfigurationList = 4B8F16E113290DC80002AD73 /* Build configuration list for PBXNativeTarget "jack_midi_dump Universal" */;
buildPhases = (
4B8F16DC13290DC80002AD73 /* Headers */,
4B8F16DD13290DC80002AD73 /* Sources */,
4B8F16DF13290DC80002AD73 /* Frameworks */,
4B8F16E013290DC80002AD73 /* Rez */,
);
buildRules = (
);
dependencies = (
);
name = "jack_midi_dump Universal";
productInstallPath = /usr/local/bin;
productName = jack_metro;
productReference = 4B8F16E513290DC80002AD73 /* jack_midi_dump */;
productType = "com.apple.product-type.tool";
};
4B8F16E813290E0E0002AD73 /* jack_midi_dump 64 bits */ = {
isa = PBXNativeTarget;
buildConfigurationList = 4B8F16EE13290E0E0002AD73 /* Build configuration list for PBXNativeTarget "jack_midi_dump 64 bits" */;
buildPhases = (
4B8F16E913290E0E0002AD73 /* Headers */,
4B8F16EA13290E0E0002AD73 /* Sources */,
4B8F16EC13290E0E0002AD73 /* Frameworks */,
4B8F16ED13290E0E0002AD73 /* Rez */,
);
buildRules = (
);
dependencies = (
);
name = "jack_midi_dump 64 bits";
productInstallPath = /usr/local/bin;
productName = jack_metro;
productReference = 4B8F16F213290E0E0002AD73 /* jack_midi_dump */;
productType = "com.apple.product-type.tool";
};
4B978DB10A31CF4A009E2DD1 /* jack_portaudio Universal */ = {
isa = PBXNativeTarget;
buildConfigurationList = 4B978DB70A31CF4A009E2DD1 /* Build configuration list for PBXNativeTarget "jack_portaudio Universal" */;
......@@ -5723,6 +5813,7 @@
4B699C4C097D421600A18468 /* Jackservermp.framework Universal */,
4B5A1BB10CD1CB9E0005BF74 /* jack_midiseq Universal */,
4B5A1BD00CD1CCE10005BF74 /* jack_midisine Universal */,
4B8F16DB13290DC80002AD73 /* jack_midi_dump Universal */,
4B363DCE0DEB02F6001F72D9 /* jack_alias Universal */,
4B699CB1097D421600A18468 /* jack_metro Universal */,
4B699CC1097D421600A18468 /* jack_lsp Universal */,
......@@ -5770,6 +5861,7 @@
4BA3393310B2E36800190E3B /* Jackservermp.framework 64 bits profiling */,
4B35C50A0D4731D1000DE7AE /* jack_midiseq 64 bits */,
4B35C5160D4731D1000DE7AE /* jack_midisine 64 bits */,
4B8F16E813290E0E0002AD73 /* jack_midi_dump 64 bits */,
4B35C5220D4731D1000DE7AE /* jack_metro 64 bits */,
4B35C52E0D4731D1000DE7AE /* jack_lsp 64 bits */,
4B35C53A0D4731D1000DE7AE /* jack_connect 64 bits */,
......@@ -6236,6 +6328,20 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
4B8F16E013290DC80002AD73 /* Rez */ = {
isa = PBXRezBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
4B8F16ED13290E0E0002AD73 /* Rez */ = {
isa = PBXRezBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
4BA339A510B2E36800190E3B /* Rez */ = {
isa = PBXRezBuildPhase;
buildActionMask = 2147483647;
......@@ -7078,6 +7184,22 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
4B8F16DD13290DC80002AD73 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
4B8F16F51329161E0002AD73 /* midi_dump.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
4B8F16EA13290E0E0002AD73 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
4B8F16F61329161E0002AD73 /* midi_dump.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
4B978DB40A31CF4A009E2DD1 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
......@@ -7576,6 +7698,16 @@
target = 4B699D03097D421600A18468 /* jack_external_metro Universal */;
targetProxy = 4B699DBF097D421700A18468 /* PBXContainerItemProxy */;
};
4B8F16FA132916910002AD73 /* PBXTargetDependency */ = {