1 /*
2   ZynAddSubFX - a software synthesizer
3 
4   JackEngine.h - Jack Driver
5   Copyright (C) 2009 Alan Calvert
6   Copyright (C) 2014 Mark McCurry
7 
8   This program is free software; you can redistribute it and/or
9   modify it under the terms of the GNU General Public License
10   as published by the Free Software Foundation; either version 2
11   of the License, or (at your option) any later version.
12 */
13 
14 #ifndef JACK_ENGINE_H
15 #define JACK_ENGINE_H
16 
17 #include <string>
18 #include <pthread.h>
19 #include <semaphore.h>
20 #include <jack/jack.h>
21 #include <pthread.h>
22 
23 #include "MidiIn.h"
24 #include "AudioOut.h"
25 
26 namespace zyn {
27 
28 typedef jack_default_audio_sample_t jsample_t;
29 
30 class JackEngine:public AudioOut, MidiIn
31 {
32     public:
33         JackEngine(const SYNTH_T &synth);
~JackEngine()34         ~JackEngine() { }
35 
36         bool Start();
37         void Stop();
38 
39         void setMidiEn(bool nval);
40         bool getMidiEn() const;
41 
42         void setAudioEn(bool nval);
43         bool getAudioEn() const;
44 
getBuffersize()45         int getBuffersize() { return audio.jackNframes; }
46 
47         std::string clientName();
48         int clientId();
49 
50     protected:
51 
52         int processCallback(jack_nframes_t nframes);
53         static int _processCallback(jack_nframes_t nframes, void *arg);
54         int bufferSizeCallback(jack_nframes_t nframes);
55         static int _bufferSizeCallback(jack_nframes_t nframes, void *arg);
56         static void _errorCallback(const char *msg);
57         static void _infoCallback(const char *msg);
58         static int _xrunCallback(void *arg);
59 
60     private:
61         bool connectServer(std::string server);
62         bool connectJack();
63         void disconnectJack();
64         bool openAudio();
65         void stopAudio();
66         bool processAudio(jack_nframes_t nframes);
67         bool openMidi();
68         void stopMidi();
69 
70         jack_client_t *jackClient;
71         struct audio {
72             unsigned int jackSamplerate;
73             unsigned int jackNframes;
74             jack_port_t *ports[2];
75             jsample_t   *portBuffs[2];
76             float peaks[1];
77         } audio;
78         struct osc {
79             jack_port_t *oscport;
80         } osc;
81         struct midi {
82             jack_port_t *inport;
83             bool         jack_sync;
84         } midi;
85 
86         void handleMidi(unsigned long frames);
87 };
88 
89 }
90 
91 #endif
92