Advertisement
SoapSuds

Jack Loop Back Microphone to Speaker.

Dec 12th, 2012
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.58 KB | None | 0 0
  1. /*
  2.     Programmer: Shane B.
  3.     Date: 12/12/12
  4.     Purpose: Learn how to minipulate sound packets before looping into the headphone
  5. */
  6.  
  7.  
  8.  
  9. /*
  10.     === Load Libraries===
  11. */
  12. #include <stdio.h>
  13. //#include <errno.h>
  14. //#include <unistd.h>
  15. //#include <stdlib.h>
  16. //#include <string.h>
  17.  
  18. #include <jack/jack.h>
  19.  
  20.  
  21. /*
  22.     ===Define Globally set variables/pointers===
  23. */
  24. jack_port_t *input_port;
  25. jack_port_t *output_port;
  26.  
  27.  
  28.  
  29.  
  30. /*
  31.     ===Begin Other Helpful Functions===
  32. */
  33.  
  34.  
  35. int
  36. process (jack_nframes_t nframes, void *arg)
  37.  
  38. {
  39.     jack_default_audio_sample_t *out = (jack_default_audio_sample_t *)
  40.     jack_port_get_buffer (output_port, nframes);
  41.  
  42.     jack_default_audio_sample_t *in = (jack_default_audio_sample_t *)
  43.     fprintf("%lu", in);
  44.         jack_port_get_buffer (input_port, nframes);
  45.  
  46.     memcpy (out, in, sizeof (jack_default_audio_sample_t) * nframes);
  47.    
  48.     return 0;      
  49. }
  50.  
  51. int
  52. srate (jack_nframes_t nframes, void *arg)
  53. {
  54.     printf ("the sample rate is now %lu/sec\n", nframes);
  55.     return 0;
  56. }
  57.  
  58. void
  59. jack_shutdown (void *arg)
  60. {
  61.     exit (1);
  62. }
  63.  
  64.  
  65.  
  66.  
  67.  
  68. /*
  69.  
  70.     ===Begin Logic Run-time===
  71. */
  72.  
  73. int
  74. main (int argc, char *argv[])
  75. {
  76.     /*
  77.         ---------------------------------------------------------------------------------------------------------------
  78.         Here we are attempting to open up a client connectiong with the JACK server
  79.     */
  80.    
  81.     //Define pointer variable for connecting to the JACK server
  82.     jack_client_t *client;
  83.    
  84.     //Check if the client can connect to the local jack server
  85.     if ((client = jack_client_new("crypt")) == 0) {
  86.         fprintf (stderr, "jack server not running?\n");
  87.         return 1;
  88.     }
  89.    
  90.    
  91.     //Assuming we have connected to the JACK server, estiablish input/output connection ports to open up
  92.     input_port = jack_port_register (client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  93.     output_port = jack_port_register (client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  94.    
  95.     //Lets define some Call back functions that can be triggered during audio runtime
  96.         //When there is work (audio) to be proccessed call the function proccess()
  97.         jack_set_process_callback (client, process, 0);
  98.    
  99.         //When the sample rate of the audio stream changes call the function srate()
  100.         jack_set_sample_rate_callback (client, srate, 0);
  101.    
  102.         //When the JACK server stops talking or shuts down, tell the application(local) to close
  103.         jack_on_shutdown (client, jack_shutdown, 0);
  104.    
  105.    
  106.     //If we can activate the JACK client then we have a connection, continue running...
  107.     if (jack_activate (client)) {
  108.         fprintf (stderr, "cannot activate client");
  109.         return 1;
  110.     }
  111.    
  112.     //Define port pointer variable
  113.     const char **ports;
  114.    
  115.     //Check if physical output ports exist...
  116.     if ((ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput)) == NULL) {
  117.         fprintf(stderr, "Cannot find any physical capture ports\n");
  118.         exit(1);
  119.     }
  120.    
  121.     //Check if we can connect to physical input ports with the client....
  122.     if (jack_connect (client, ports[0], jack_port_name (input_port))) {
  123.         fprintf (stderr, "cannot connect input ports\n");
  124.     }
  125.    
  126.     //Free port pointer
  127.     free (ports);
  128.    
  129.     //
  130.     if ((ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsInput)) == NULL) {
  131.         fprintf(stderr, "Cannot find any physical playback ports\n");
  132.         exit(1);
  133.     }
  134.    
  135.     //Connect all known ports....
  136.     int i=0;
  137.     while(ports[i]!=NULL){
  138.       if (jack_connect (client, jack_port_name (output_port), ports[i])) {
  139.         fprintf (stderr, "cannot connect output ports\n");
  140.       }
  141.       i++;
  142.     }
  143.  
  144.     free (ports);
  145.    
  146.     for(;;)
  147.      
  148.  
  149.     /*KIRBY: Close the client*/
  150.     jack_client_close (client);
  151.     exit (0);
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement