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