1 // jack.h
2 // LiVES (lives-exe)
3 // (c) G. Finch 2005 - 2017
4 // Released under the GPL 3 or later
5 // see file ../COPYING for licensing details
6 
7 #ifndef HAS_LIVES_JACK_H
8 #define HAS_LIVES_JACK_H
9 
10 #ifdef ENABLE_JACK
11 
12 /////////////////////////////////////////////////////////////////
13 // Transport
14 
15 #include <jack/jack.h>
16 #include <jack/transport.h>
17 #include <jack/control.h>
18 
19 boolean lives_jack_init(void);  /** start up server on LiVES init */
20 boolean lives_jack_poll(void); /** poll function to check transport state */
21 void lives_jack_end(void);
22 
23 int lives_start_ready_callback(jack_transport_state_t state, jack_position_t *pos, void *arg);
24 
25 void jack_pb_start(double pbtime);  /** start playback transport master */
26 void jack_pb_stop(void);  /** pause playback transport master */
27 
28 ////////////////////////////////////////////////////////////////////////////
29 // Audio
30 
31 #include "audio.h"
32 
33 #ifndef IS_DARWIN
34 #define JACK_DRIVER_NAME "alsa"
35 #else
36 #ifdef IS_SOLARIS
37 // use OSS on Solaris
38 #define JACK_DRIVER_NAME "oss"
39 #else
40 // use coreaudio on Darwin
41 #define JACK_DRIVER_NAME "coreaudio"
42 #endif
43 #endif
44 
45 #define JACK_MAX_OUTPUT_PORTS 10
46 #define JACK_MAX_INPUT_PORTS 10
47 
48 #define ERR_PORT_NOT_FOUND 10
49 
50 #define JACK_DEFAULT_SERVER_NAME "default"
51 
52 typedef jack_nframes_t nframes_t;
53 
54 #define USE_JACKCTL 1
55 
56 // let's hope these are well above the standard jack transport states...
57 #define JackTClosed 1024
58 #define JackTReset 1025
59 #define JackTStopped 1026
60 
61 typedef struct {
62   int      dev_idx;                      /**< id of this device ??? */
63   int     sample_out_rate;                   /**< samples(frames) per second */
64   volatile int     sample_in_rate;                   /**< samples(frames) per second */
65   uint64_t    num_input_channels;            /**< number of input channels(1 is mono, 2 stereo etc..) */
66   uint64_t    num_output_channels;           /**< number of output channels(1 is mono, 2 stereo etc..) */
67   uint64_t    bytes_per_channel;
68 
69   uint64_t    num_calls;                     /**< count of process_audio() calls */
70 
71   jack_port_t     *output_port[JACK_MAX_OUTPUT_PORTS]; /**< output ports */
72   jack_port_t     *input_port[JACK_MAX_INPUT_PORTS]; /**< input ports */
73   jack_client_t   *client;                        /**< pointer to jack client */
74 
75   char             **jack_port_name;              /**< user given strings for the port names, can be NULL */
76   unsigned int     jack_port_name_count;          /**< the number of port names given */
77   uint64_t    jack_port_flags;               /**< flags to be passed to jack when opening the output ports */
78 
79   lives_audio_loop_t loop;
80 
81   jack_transport_state_t state;
82 
83   float     volume[JACK_MAX_OUTPUT_PORTS];      ///< amount volume, 1.0 is full volume
84 
85   boolean          in_use;                        /**< true if this device is currently in use */
86   boolean mute;
87 
88   volatile aserver_message_t   *msgq;          /**< linked list of messages we are sending to the callback process */
89 
90   off_t seek_pos;
91   volatile off_t real_seek_pos;
92   off_t seek_end;
93   boolean usigned;
94   boolean reverse_endian;
95 
96   volatile lives_whentostop_t *whentostop; ///< pointer to mainw->whentostop
97   volatile lives_cancel_t *cancelled; ///< pointer to mainw->cancelled
98 
99   /* variables used for trying to restart the connection to jack */
100   boolean             jackd_died;                    /**< true if jackd has died and we should try to restart it */
101 
102   boolean play_when_stopped; ///< if we should play audio even when jack transport is stopped
103 
104   volatile jack_nframes_t nframes_start;
105   volatile uint64_t frames_written;
106 
107   int out_chans_available;
108   int in_chans_available;
109 
110   boolean is_paused;
111 
112   boolean is_output; ///< is output FROM host to jack
113 
114   boolean is_silent;
115 
116   boolean is_active;
117 
118   int playing_file;
119 
120   volatile float jack_pulse[1024];
121 
122   lives_audio_buf_t **abufs;
123   volatile int read_abuf;
124 
125   volatile int astream_fd;
126 
127   volatile float abs_maxvol_heard;
128 } jack_driver_t;
129 
130 #define JACK_MAX_OUTDEVICES 10
131 #define JACK_MAX_INDEVICES 10
132 
133 ////////////////////////////////////////////////////////////////////////////
134 
135 jack_driver_t *jack_get_driver(int dev_idx, boolean is_output); ///< get driver
136 
137 int jack_audio_init(void); ///< init jack for host output
138 int jack_audio_read_init(void); ///< init jack for host input
139 
140 boolean jack_create_client_writer(jack_driver_t *); ///< open device for host output
141 boolean jack_create_client_reader(jack_driver_t *); ///< open device for host input
142 
143 boolean jack_write_driver_activate(jack_driver_t *);  ///< activate for host playback
144 boolean jack_read_driver_activate(jack_driver_t *, boolean autocon);  ///< activate for host recording
145 
146 void jack_close_device(jack_driver_t *);
147 
148 boolean jack_try_reconnect(void);
149 
150 void jack_aud_pb_ready(int fileno);
151 void jack_pb_end(void);
152 
153 size_t jack_flush_read_data(size_t rbytes, void *data);
154 
155 // utils
156 volatile aserver_message_t *jack_get_msgq(jack_driver_t *); ///< pull last msg from msgq, or return NULL
157 void jack_time_reset(jack_driver_t *, int64_t offset);
158 ticks_t lives_jack_get_time(jack_driver_t *); ///< get time from jack, in 10^-8 seconds
159 boolean jack_audio_seek_frame(jack_driver_t *, double frame);  ///< seek to (video) frame
160 int64_t jack_audio_seek_bytes(jack_driver_t *, int64_t bytes, lives_clip_t *sfile);  ///< seek to byte position
161 size_t jack_get_buffsize(jack_driver_t *);
162 
163 void jack_get_rec_avals(jack_driver_t *);
164 
165 ticks_t jack_transport_get_current_ticks(void);
166 
167 double lives_jack_get_pos(jack_driver_t *);
168 
169 #endif
170 
171 #endif
172