Prefer using RAII to suspend signals
[ardour.git] / headless / load_session.cc
1 #include <iostream>
2 #include <cstdlib>
3 #include <getopt.h>
4
5 #include "pbd/failed_constructor.h"
6 #include "pbd/error.h"
7 #include "pbd/debug.h"
8
9 #include "ardour/ardour.h"
10 #include "ardour/audioengine.h"
11 #include "ardour/session.h"
12
13 #include "misc.h"
14
15 using namespace std;
16 using namespace ARDOUR;
17 using namespace PBD;
18
19 #ifdef PLATFORM_WINDOWS
20 #include <windows.h>
21 #define sleep(X) Sleep((X) * 1000)
22 #endif
23
24 static const char* localedir = LOCALEDIR;
25
26 TestReceiver test_receiver;
27
28 /** @param dir Session directory.
29  *  @param state Session state file, without .ardour suffix.
30  */
31 Session *
32 load_session (string dir, string state)
33 {
34         SessionEvent::create_per_thread_pool ("test", 512);
35
36         test_receiver.listen_to (error);
37         test_receiver.listen_to (info);
38         test_receiver.listen_to (fatal);
39         test_receiver.listen_to (warning);
40
41         AudioEngine* engine = AudioEngine::create ();
42
43         if (!engine->set_default_backend ()) {
44                 std::cerr << "Cannot create Audio/MIDI engine\n";
45                 ::exit (1);
46         }
47
48         if (engine->start () != 0) {
49                 std::cerr << "Cannot start Audio/MIDI engine\n";
50                 ::exit (1);
51         }
52
53         Session* session = new Session (*engine, dir, state);
54         engine->set_session (session);
55         return session;
56 }
57
58 string session_name = "";
59 string backend_client_name = "ardour";
60 string backend_session_uuid;
61 bool just_version = false;
62 bool use_vst = true;
63 bool try_hw_optimization = true;
64 bool no_connect_ports = false;
65
66 void
67 print_help ()
68 {
69         cout << "Usage: hardour [OPTIONS]... DIR SNAPSHOT_NAME\n\n"
70              << "  DIR                         Directory/Folder to load session from\n"
71              << "  SNAPSHOT_NAME               Name of session/snapshot to load (without .ardour at end\n"
72              << "  -v, --version               Show version information\n"
73              << "  -h, --help                  Print this message\n"
74              << "  -c, --name <name>           Use a specific backend client name, default is ardour\n"
75              << "  -d, --disable-plugins       Disable all plugins in an existing session\n"
76              << "  -D, --debug <options>       Set debug flags. Use \"-D list\" to see available options\n"
77              << "  -O, --no-hw-optimizations   Disable h/w specific optimizations\n"
78              << "  -P, --no-connect-ports      Do not connect any ports at startup\n"
79 #ifdef WINDOWS_VST_SUPPORT
80              << "  -V, --novst                 Do not use VST support\n"
81 #endif
82                 ;
83 }
84
85 int main (int argc, char* argv[])
86 {
87         const char *optstring = "vhBdD:c:VOU:P";
88
89         const struct option longopts[] = {
90                 { "version", 0, 0, 'v' },
91                 { "help", 0, 0, 'h' },
92                 { "bypass-plugins", 1, 0, 'B' },
93                 { "disable-plugins", 1, 0, 'd' },
94                 { "debug", 1, 0, 'D' },
95                 { "name", 1, 0, 'c' },
96                 { "novst", 0, 0, 'V' },
97                 { "no-hw-optimizations", 0, 0, 'O' },
98                 { "uuid", 1, 0, 'U' },
99                 { "no-connect-ports", 0, 0, 'P' },
100                 { 0, 0, 0, 0 }
101         };
102
103         int option_index = 0;
104         int c = 0;
105
106         while (1) {
107                 c = getopt_long (argc, argv, optstring, longopts, &option_index);
108
109                 if (c == -1) {
110                         break;
111                 }
112
113                 switch (c) {
114                 case 0:
115                         break;
116
117                 case 'v':
118                         just_version = true;
119                         break;
120
121                 case 'h':
122                         print_help ();
123                         exit (0);
124                         break;
125
126                 case 'c':
127                         backend_client_name = optarg;
128                         break;
129
130                 case 'B':
131                         ARDOUR::Session::set_bypass_all_loaded_plugins (true);
132                         break;
133
134                 case 'd':
135                         ARDOUR::Session::set_disable_all_loaded_plugins (true);
136                         break;
137
138                 case 'D':
139                         if (PBD::parse_debug_options (optarg)) {
140                                 ::exit (1);
141                         }
142                         break;
143
144                 case 'O':
145                         try_hw_optimization = false;
146                         break;
147
148                 case 'P':
149                         no_connect_ports = true;
150                         break;
151
152                 case 'V':
153 #ifdef WINDOWS_VST_SUPPORT
154                         use_vst = false;
155 #endif /* WINDOWS_VST_SUPPORT */
156                         break;
157
158                 case 'U':
159                         backend_session_uuid = optarg;
160                         break;
161
162                 default:
163                         print_help ();
164                         ::exit (1);
165                 }
166         }
167
168         if (argc < 3) {
169                 print_help ();
170                 ::exit (1);
171         }
172
173         if (!ARDOUR::init (false, true, localedir)) {
174                 cerr << "Ardour failed to initialize\n" << endl;
175                 ::exit (1);
176         }
177
178         Session* s = 0;
179
180         try {
181                 s = load_session (argv[optind], argv[optind+1]);
182         } catch (failed_constructor& e) {
183                 cerr << "failed_constructor: " << e.what() << "\n";
184                 exit (EXIT_FAILURE);
185         } catch (AudioEngine::PortRegistrationFailure& e) {
186                 cerr << "PortRegistrationFailure: " << e.what() << "\n";
187                 exit (EXIT_FAILURE);
188         } catch (exception& e) {
189                 cerr << "exception: " << e.what() << "\n";
190                 exit (EXIT_FAILURE);
191         } catch (...) {
192                 cerr << "unknown exception.\n";
193                 exit (EXIT_FAILURE);
194         }
195
196         s->request_transport_speed (1.0);
197
198         sleep (-1);
199
200         AudioEngine::instance()->remove_session ();
201         delete s;
202         AudioEngine::instance()->stop ();
203
204         AudioEngine::destroy ();
205
206         return 0;
207 }