Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.41 KB | None | 0 0
  1. // Contact: Harry van Haaren <harryhaaren@gmail.com>
  2. // Compile: g++ jackClient.cpp `pkg-config --cflags --libs jack`
  3.  
  4. #include <iostream>
  5. #include <unistd.h>
  6. #include <jack/jack.h>
  7.  
  8. // declare two "jack_port_t" pointers, which will each represent a port
  9. // in the JACK graph (ie: Connections tab in QJackCtl)
  10. jack_port_t* inputPort = 0;
  11. jack_port_t* outputPortL = 0;
  12. jack_port_t* outputPortR = 0;
  13.  
  14.  
  15. // this function is the main audio processing loop, JACK calls this function
  16. // every time that it wants "nframes" number of frames to be processed.
  17. // nframes is usually between 64 and 256, but you should always program
  18. // so that you can work with any amount of frames per process() call!
  19. int process(jack_nframes_t nframes, void* )
  20. {
  21.   // this is a little tricky, port_get_buffer() will return a pointer to
  22.   // the data that we will use, so cast it to (float*), so that we
  23.   // can use the data as floating point numbers. JACK will always pass
  24.   // floating point samples around, the reason that we have to cast it
  25.   // ourselves is so that it could be changed in the future... don't worry
  26.   // about it too much.
  27.   float* inputBuffer = (float*)jack_port_get_buffer ( inputPort , nframes);
  28.   float* outputBufferL= (float*)jack_port_get_buffer ( outputPortL, nframes);
  29.   float* outputBufferR= (float*)jack_port_get_buffer ( outputPortR, nframes);
  30.  
  31.  
  32.   // this is the intresting part, we work with each sample of audio data
  33.   // one by one, copying them across. Try multiplying the input by 0.5,
  34.   // it will decrease the volume...
  35.   for ( int i = 0; i < (int) nframes; i++)
  36.   {
  37.     // copy data from input to output. Note this is not optimized for speed!
  38.     outputBufferL[i] = inputBuffer[i];
  39.     outputBufferR[i] = inputBuffer[i];
  40.   }
  41.  
  42.   return 0;
  43. }
  44.  
  45. int main()
  46. {
  47.   std::cout << "JACK client tutorial" << std::endl;
  48.  
  49.   // create a JACK client and activate
  50.   jack_client_t* client = jack_client_open ("JackClientTutorial",
  51.                                             JackNullOption,
  52.                                             0,
  53.                                             0 );
  54.  
  55.   // register the process callback : JACK "calls us back" when there is
  56.   // work to be done, and the "process" function does that work.
  57.   jack_set_process_callback  (client, process , 0);
  58.  
  59.   // register two ports, one input one output, both of AUDIO type
  60.   inputPort = jack_port_register ( client,
  61.                                     "input",
  62.                                     JACK_DEFAULT_AUDIO_TYPE,
  63.                                     JackPortIsInput,
  64.                                     0 );
  65.  
  66.   outputPortL = jack_port_register ( client,
  67.                                     "outputL",
  68.                                     JACK_DEFAULT_AUDIO_TYPE,
  69.                                     JackPortIsOutput,
  70.                                     0 );
  71.  
  72.   outputPortR = jack_port_register ( client,
  73.                                     "outputR",
  74.                                     JACK_DEFAULT_AUDIO_TYPE,
  75.                                     JackPortIsOutput,
  76.                                     0 );
  77.  
  78.   // activate the client, ie: enable it for processing
  79.   jack_activate(client);
  80.  
  81.   // pause for 30 seconds, letting process() do it's thing
  82.   sleep(300);
  83.  
  84.   // tell JACK to stop processing the client
  85.   jack_deactivate(client);
  86.  
  87.   // close the client
  88.   jack_client_close(client);
  89.  
  90.   return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement