Advertisement
SoapSuds

Solution to seeing the time/frequency with JACK Audio in C

Dec 13th, 2012
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.81 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. //#include <errno.h>
  4. //#include <unistd.h>
  5. //#include <stdlib.h>
  6. //#include <string.h>
  7.  
  8. #include <jack/jack.h>
  9.  
  10. jack_port_t *input_port;
  11. jack_port_t *output_port;
  12.  
  13. int
  14. process (jack_nframes_t nframes, void *arg)
  15.  
  16. {
  17.     jack_default_audio_sample_t *out = (jack_default_audio_sample_t *)
  18.     jack_port_get_buffer (output_port, nframes);
  19.    
  20.     jack_default_audio_sample_t *in = (jack_default_audio_sample_t *)
  21.         jack_port_get_buffer (input_port, nframes);
  22.  
  23.     memcpy (out, in, sizeof (jack_default_audio_sample_t) * nframes);
  24.    
  25.     float mult = 5.2;
  26.     int i = 0;
  27.     for(i=0;i < nframes;i++){
  28.         //Analog signal at this frame
  29.         printf("%f \n", out[i]);
  30.         //out[i]=atan(out[i]*mult)/atan(mult);
  31.         //printf("%f \n\n", out[i]);
  32.     }
  33.    
  34.     //Buffer is over...
  35.     //printf("-------------------------------");
  36.    
  37.     return 0;      
  38. }
  39.  
  40. int
  41. srate (jack_nframes_t nframes, void *arg)
  42.  
  43. {
  44.     printf ("the sample rate is now %lu/sec\n", nframes);
  45.     return 0;
  46. }
  47.  
  48. void
  49. jack_shutdown (void *arg)
  50. {
  51.     exit (1);
  52. }
  53.  
  54. int
  55. main (int argc, char *argv[])
  56. {
  57.         /*KIRBY: Create a JACK client.  
  58.          This is our connection to the JACK daemon.*/
  59.  
  60.     jack_client_t *client;
  61.  
  62.     /*KIRBY: A pointer for an array of ports.  Remember, we
  63.           already saw this being
  64.       used to hold the array of available ports
  65.       See the previous chapter*/
  66.  
  67.     const char **ports;
  68.  
  69.  
  70.     /*KIRBY: This doesn't really need an explanation*/
  71.  
  72.     if (argc < 2) {
  73.         fprintf (stderr, "usage: jack_simple_client \n");
  74.         return 1;
  75.     }
  76.  
  77.     /* tell the JACK server to call error() whenever it
  78.        experiences an error.  Notice that this callback is
  79.        global to this process, not specific to each client.
  80.    
  81.        This is set here so that it can catch errors in the
  82.        connection process
  83.     */
  84.     /*KIRBY: Nuff said.*/
  85.     //jack_set_error_function (error);
  86.  
  87.     /* try to become a client of the JACK server */
  88.     /*KIRBY:  This is where our pointer "client"
  89.           gets something to point to.  
  90.       You will notice that the functions called later take a client as
  91.       a parameter - this is what we pass.*/
  92.     if ((client = jack_client_new (argv[1])) == 0) {
  93.         fprintf (stderr, "jack server not running?\n");
  94.         return 1;
  95.     }
  96.    
  97.     fprintf(stderr, argv[0]);
  98.  
  99.     /* tell the JACK server to call `process()' whenever
  100.        there is work to be done.
  101.     */
  102.  
  103.     jack_set_process_callback (client, process, 0);
  104.  
  105.     /* tell the JACK server to call `srate()' whenever
  106.        the sample rate of the system changes.
  107.     */
  108.  
  109.     jack_set_sample_rate_callback (client, srate, 0);
  110.  
  111.     /* tell the JACK server to call `jack_shutdown()' if
  112.        it ever shuts down, either entirely, or if it
  113.        just decides to stop calling us.
  114.     */
  115.  
  116.     jack_on_shutdown (client, jack_shutdown, 0);
  117.  
  118.     /* display the current sample rate. once the client is activated
  119.        (see below), you should rely on your own sample rate
  120.        callback (see above) for this value.
  121.     */
  122.     printf ("engine sample rate: %lu\n", jack_get_sample_rate (client));
  123.  
  124.     /* create two ports */
  125.  
  126.     input_port = jack_port_register (client, "input",
  127.                      JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  128.     output_port = jack_port_register (client, "output",
  129.                      JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  130.  
  131.     /* tell the JACK server that we are ready to roll */
  132.     /*KIRBY: So, once we are in a position to start
  133.           doing whatever it is we do, this is how we announce that.*/
  134.     if (jack_activate (client)) {
  135.         fprintf (stderr, "cannot activate client");
  136.         return 1;
  137.     }
  138.  
  139.     /* connect the ports. Note: you can't do this before
  140.        the client is activated, because we can't allow
  141.        connections to be made to clients that aren't
  142.        running.
  143.     */
  144.     /*KIRBY: We already discussed this.  Go back a chapter
  145.                  if you missed it.*/
  146.    
  147.     if ((ports = jack_get_ports (client, NULL, NULL,
  148.                                JackPortIsPhysical|JackPortIsOutput)) == NULL) {
  149.         fprintf(stderr, "Cannot find any physical capture ports\n");
  150.         exit(1);
  151.     }
  152.  
  153.     if (jack_connect (client, ports[0], jack_port_name (input_port))) {
  154.         fprintf (stderr, "cannot connect input ports\n");
  155.     }
  156.  
  157.     free (ports);
  158.    
  159.     if ((ports = jack_get_ports (client, NULL, NULL,
  160.                                JackPortIsPhysical|JackPortIsInput)) == NULL) {
  161.         fprintf(stderr, "Cannot find any physical playback ports\n");
  162.         exit(1);
  163.     }
  164.    
  165.     /*KIRBY: This is our modified bit.  Groovy, eh?*/
  166.    
  167.     int i=0;
  168.     while(ports[i]!=NULL){
  169.       if (jack_connect (client, jack_port_name (output_port), ports[i])) {
  170.         fprintf (stderr, "cannot connect output ports\n");
  171.       }
  172.       i++;
  173.     }
  174.  
  175.     free (ports);
  176.  
  177.     /* Since this is just a toy, run for a few seconds, then finish */
  178.     /*KIRBY: We changed that, too.  Now we run until we get killed.*/
  179.     for(;;)
  180.       sleep (1);
  181.  
  182.     /*KIRBY: Close the client*/
  183.     jack_client_close (client);
  184.     exit (0);
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement