visibility macros and flush() added to SrcFileSource; merge with master
[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 "pbd/localtime_r.h"
29
30 #include "ardour/audioengine.h"
31 #include "ardour/filename_extensions.h"
32 #include "ardour/session.h"
33 #include "ardour/session_directory.h"
34 #include "ardour/tempo.h"
35
36 #include "jack_session.h"
37
38 using namespace ARDOUR;
39 using std::string;
40
41 JACKSession::JACKSession (Session* s)
42         : SessionHandlePtr (s)
43 {
44 }
45
46 JACKSession::~JACKSession ()
47 {
48 }
49
50 void
51 JACKSession::session_event (jack_session_event_t* event)
52 {
53         char timebuf[128], *tmp;
54         time_t n;
55         struct tm local_time;
56
57         time (&n);
58         localtime_r (&n, &local_time);
59         strftime (timebuf, sizeof(timebuf), "JS_%FT%T", &local_time);
60
61         while ((tmp = strchr(timebuf, ':'))) { *tmp = '.'; }
62
63         if (event->type == JackSessionSaveTemplate)
64         {
65                 if (_session->save_template( timebuf )) {
66                         event->flags = JackSessionSaveError;
67                 } else {
68                         string cmd ("ardour3 -P -U ");
69                         cmd += event->client_uuid;
70                         cmd += " -T ";
71                         cmd += timebuf;
72
73                         event->command_line = strdup (cmd.c_str());
74                 }
75         }
76         else
77         {
78                 if (_session->save_state (timebuf)) {
79                         event->flags = JackSessionSaveError;
80                 } else {
81                         std::string xml_path (_session->session_directory().root_path());
82                         std::string legalized_filename = legalize_for_path (timebuf) + statefile_suffix;
83                         xml_path = Glib::build_filename (xml_path, legalized_filename);
84
85                         string cmd ("ardour3 -P -U ");
86                         cmd += event->client_uuid;
87                         cmd += " \"";
88                         cmd += xml_path;
89                         cmd += '\"';
90
91                         event->command_line = strdup (cmd.c_str());
92                 }
93         }
94
95         /* this won't be called if the port engine in use is not JACK, so we do 
96            not have to worry about the type of PortEngine::private_handle()
97         */
98
99         jack_client_t* jack_client = (jack_client_t*) AudioEngine::instance()->port_engine().private_handle();
100         
101         if (jack_client) {
102                 jack_session_reply (jack_client, event);
103         }
104
105         if (event->type == JackSessionSaveAndQuit) {
106                 _session->Quit (); /* EMIT SIGNAL */
107         }
108
109         jack_session_event_free (event);
110 }
111
112 void
113 JACKSession::timebase_callback (jack_transport_state_t /*state*/,
114                                  pframes_t /*nframes*/,
115                                  jack_position_t* pos,
116                                  int /*new_position*/)
117 {
118         Timecode::BBT_Time bbt;
119         TempoMap& tempo_map (_session->tempo_map());
120         framepos_t tf = _session->transport_frame ();
121
122         /* BBT info */
123
124         TempoMetric metric (tempo_map.metric_at (tf));
125         
126         try {
127                 tempo_map.bbt_time_rt (tf, bbt);
128                 
129                 pos->bar = bbt.bars;
130                 pos->beat = bbt.beats;
131                 pos->tick = bbt.ticks;
132                 
133                 // XXX still need to set bar_start_tick
134                 
135                 pos->beats_per_bar = metric.meter().divisions_per_bar();
136                 pos->beat_type = metric.meter().note_divisor();
137                 pos->ticks_per_beat = Timecode::BBT_Time::ticks_per_beat;
138                 pos->beats_per_minute = metric.tempo().beats_per_minute();
139                 
140                 pos->valid = jack_position_bits_t (pos->valid | JackPositionBBT);
141                 
142         } catch (...) {
143                 /* no message */
144         }
145
146 #ifdef HAVE_JACK_VIDEO_SUPPORT
147         //poke audio video ratio so Ardour can track Video Sync
148         pos->audio_frames_per_video_frame = _session->frame_rate() / _session->timecode_frames_per_second();
149         pos->valid = jack_position_bits_t (pos->valid | JackAudioVideoRatio);
150 #endif
151
152 #ifdef HAVE_JACK_TIMCODE_SUPPORT
153         /* This is not yet defined in JACK */
154
155         /* Timecode info */
156
157         pos->timecode_offset = _session->config.get_timecode_offset();
158         t.timecode_frame_rate = _session->timecode_frames_per_second();
159         pos->valid = jack_position_bits_t (pos->valid | JackPositionTimecode);
160 #endif
161
162 #ifdef HAVE_JACK_LOOPING_SUPPORT
163         /* This is not yet defined in JACK */
164         if (_transport_speed) {
165
166                 if (play_loop) {
167
168                         Location* location = _session->locations()->auto_loop_location();
169
170                         if (location) {
171
172                                 t.transport_state = JackTransportLooping;
173                                 t.loop_start = location->start();
174                                 t.loop_end = location->end();
175                                 t.valid = jack_transport_bits_t (t.valid | JackTransportLoop);
176
177                         } else {
178
179                                 t.loop_start = 0;
180                                 t.loop_end = 0;
181                                 t.transport_state = JackTransportRolling;
182
183                         }
184
185                 } else {
186
187                         t.loop_start = 0;
188                         t.loop_end = 0;
189                         t.transport_state = JackTransportRolling;
190
191                 }
192
193         }
194 #endif
195 }
196