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