Advertisement
Guest User

Untitled

a guest
Oct 19th, 2014
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <jack/jack.h>
  5. #include <unistd.h>
  6.  
  7. //gcc -o register_jack_ports register_jack_ports.c `pkg-config --cflags --libs jack`
  8.  
  9. int input_port_count=100; //param
  10.  
  11. //Array of pointers to input or output ports
  12. jack_port_t **ioPortArray;
  13. jack_client_t *client;
  14.  
  15. jack_options_t jack_opts = JackNoStartServer;
  16.  
  17. int process_enabled=0;
  18.  
  19. int process(jack_nframes_t nframes, void *arg)
  20. {
  21. if(process_enabled==1)
  22. {
  23. fprintf(stderr,".");
  24. }
  25. return 0;
  26. }
  27.  
  28. //================================================================
  29. int main (int argc, char *argv[])
  30. {
  31. fprintf(stderr,"first param is # of channels.\n");
  32.  
  33. input_port_count=atoi(argv[1]);
  34.  
  35. const char **ports;
  36. jack_status_t status;
  37.  
  38. const char *client_name="testme";
  39.  
  40. //create an array of input ports
  41. ioPortArray = (jack_port_t**) malloc(input_port_count * sizeof(jack_port_t*));
  42.  
  43. //open a client connection to the JACK server
  44. client = jack_client_open (client_name, jack_opts, &status, NULL);
  45. if (client == NULL)
  46. {
  47. fprintf (stderr, "jack_client_open() failed, status = 0x%2.0x\n", status);
  48. exit(1);
  49. }
  50. jack_set_process_callback (client, process, NULL);
  51.  
  52. // Register each output port
  53. int port=0;
  54. for (port=0 ; port<input_port_count ; port ++)
  55. {
  56. // Create port name
  57. char* portName;
  58. if (asprintf(&portName, "input_%d", (port+1)) < 0)
  59. {
  60. fprintf(stderr, "Could not create portname for port %d\n", port);
  61. exit(1);
  62. }
  63.  
  64. // Register the input port
  65. ioPortArray[port] = jack_port_register(client, portName, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  66. if (ioPortArray[port] == NULL)
  67. {
  68. fprintf(stderr, "Could not create input port %d\n", (port+1));
  69. exit(1);
  70. }
  71. }
  72.  
  73. //now activate client in JACK, starting with process() cycles
  74. if (jack_activate (client))
  75. {
  76. fprintf (stderr, "cannot activate client\n\n");
  77. exit(1);
  78. }
  79.  
  80. //prevent to get physical midi ports
  81. const char* pat="audio";
  82.  
  83. ports = jack_get_ports (client, NULL, pat,
  84. JackPortIsPhysical|JackPortIsOutput);
  85. if (ports == NULL)
  86. {
  87. fprintf(stderr, "no physical capture ports found\n");
  88. exit(1);
  89. }
  90.  
  91. int autoconnect=1;
  92. if(autoconnect==1)
  93. {
  94. int j=0;
  95. int i=0;
  96. for(i=0;i<input_port_count;i++)
  97. {
  98. if (ports[i]!=NULL
  99. && ioPortArray[j]!=NULL
  100. && jack_port_name(ioPortArray[j])!=NULL)
  101. {
  102. if(!jack_connect (client, ports[i],jack_port_name(ioPortArray[j])))
  103. {
  104. //used variabled can't be NULL here
  105. fprintf (stderr, "autoconnect: %s -> %s\n",
  106. ports[i],jack_port_name(ioPortArray[j])
  107. );
  108. j++;
  109. }
  110. else
  111. {
  112. fprintf (stderr, "autoconnect: failed: %s -> %s\n",
  113. ports[i],jack_port_name(ioPortArray[j])
  114. );
  115. }
  116. }
  117. }
  118. }
  119.  
  120. free (ports);
  121.  
  122. process_enabled=1;
  123.  
  124. //run possibly forever until not interrupted by any means
  125. while (1)
  126. {
  127. sleep(1);
  128. }
  129. exit(0);
  130. }//end main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement