fix issues with recording while synced to JACK (non-pure-virtual method added to...
[ardour.git] / libs / backends / jack / jack_session.cc
1 /*
2   Copyright (C) 2013 Paul Davis
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20
21 #include <time.h>
22
23 #include <glibmm/miscutils.h>
24
25 #include <jack/jack.h>
26 #include <jack/transport.h>
27
28 #include "ardour/audioengine.h"
29 #include "ardour/filename_extensions.h"
30 #include "ardour/session.h"
31 #include "ardour/session_directory.h"
32 #include "ardour/tempo.h"
33
34 #include "jack_session.h"
35
36 using namespace ARDOUR;
37 using std::string;
38
39 JACKSession::JACKSession (Session* s)
40         : SessionHandlePtr (s)
41 {
42 }
43
44 JACKSession::~JACKSession ()
45 {
46 }
47
48 void
49 JACKSession::session_event (jack_session_event_t* event)
50 {
51         char timebuf[128], *tmp;
52         time_t n;
53         struct tm local_time;
54
55         time (&n);
56         localtime_r (&n, &local_time);
57         strftime (timebuf, sizeof(timebuf), "JS_%FT%T", &local_time);
58
59         while ((tmp = strchr(timebuf, ':'))) { *tmp = '.'; }
60
61         if (event->type == JackSessionSaveTemplate)
62         {
63                 if (_session->save_template( timebuf )) {
64                         event->flags = JackSessionSaveError;
65                 } else {
66                         string cmd ("ardour3 -P -U ");
67                         cmd += event->client_uuid;
68                         cmd += " -T ";
69                         cmd += timebuf;
70
71                         event->command_line = strdup (cmd.c_str());
72                 }
73         }
74         else
75         {
76                 if (_session->save_state (timebuf)) {
77                         event->flags = JackSessionSaveError;
78                 } else {
79                         std::string xml_path (_session->session_directory().root_path());
80                         std::string legalized_filename = legalize_for_path (timebuf) + statefile_suffix;
81                         xml_path = Glib::build_filename (xml_path, legalized_filename);
82
83                         string cmd ("ardour3 -P -U ");
84                         cmd += event->client_uuid;
85                         cmd += " \"";
86                         cmd += xml_path;
87                         cmd += '\"';
88
89                         event->command_line = strdup (cmd.c_str());
90                 }
91         }
92
93         /* this won't be called if the port engine in use is not JACK, so we do 
94            not have to worry about the type of PortEngine::private_handle()
95         */
96
97         jack_client_t* jack_client = (jack_client_t*) AudioEngine::instance()->port_engine().private_handle();
98         
99         if (jack_client) {
100                 jack_session_reply (jack_client, event);
101         }
102
103         if (event->type == JackSessionSaveAndQuit) {
104                 _session->Quit (); /* EMIT SIGNAL */
105         }
106
107         jack_session_event_free (event);
108 }
109
110 void
111 JACKSession::timebase_callback (jack_transport_state_t /*state*/,
112                                  pframes_t /*nframes*/,
113                                  jack_position_t* pos,
114                                  int /*new_position*/)
115 {
116         Timecode::BBT_Time bbt;
117         TempoMap& tempo_map (_session->tempo_map());
118         framepos_t tf = _session->transport_frame ();
119
120         /* BBT info */
121
122         TempoMetric metric (tempo_map.metric_at (tf));
123         
124         try {
125                 tempo_map.bbt_time_rt (tf, bbt);
126                 
127                 pos->bar = bbt.bars;
128                 pos->beat = bbt.beats;
129                 pos->tick = bbt.ticks;
130                 
131                 // XXX still need to set bar_start_tick
132                 
133                 pos->beats_per_bar = metric.meter().divisions_per_bar();
134                 pos->beat_type = metric.meter().note_divisor();
135                 pos->ticks_per_beat = Timecode::BBT_Time::ticks_per_beat;
136                 pos->beats_per_minute = metric.tempo().beats_per_minute();
137                 
138                 pos->valid = jack_position_bits_t (pos->valid | JackPositionBBT);
139                 
140         } catch (...) {
141                 /* no message */
142         }
143
144 #ifdef HAVE_JACK_VIDEO_SUPPORT
145         //poke audio video ratio so Ardour can track Video Sync
146         pos->audio_frames_per_video_frame = _session->frame_rate() / _session->timecode_frames_per_second();
147         pos->valid = jack_position_bits_t (pos->valid | JackAudioVideoRatio);
148 #endif
149
150 #ifdef HAVE_JACK_TIMCODE_SUPPORT
151         /* This is not yet defined in JACK */
152
153         /* Timecode info */
154
155         pos->timecode_offset = _session->config.get_timecode_offset();
156         t.timecode_frame_rate = _session->timecode_frames_per_second();
157         pos->valid = jack_position_bits_t (pos->valid | JackPositionTimecode);
158 #endif
159
160 #ifdef HAVE_JACK_LOOPING_SUPPORT
161         /* This is not yet defined in JACK */
162         if (_transport_speed) {
163
164                 if (play_loop) {
165
166                         Location* location = _session->locations()->auto_loop_location();
167
168                         if (location) {
169
170                                 t.transport_state = JackTransportLooping;
171                                 t.loop_start = location->start();
172                                 t.loop_end = location->end();
173                                 t.valid = jack_transport_bits_t (t.valid | JackTransportLoop);
174
175                         } else {
176
177                                 t.loop_start = 0;
178                                 t.loop_end = 0;
179                                 t.transport_state = JackTransportRolling;
180
181                         }
182
183                 } else {
184
185                         t.loop_start = 0;
186                         t.loop_end = 0;
187                         t.transport_state = JackTransportRolling;
188
189                 }
190
191         }
192 #endif
193 }
194