Merge branch 'ripple-mode-cc' into cairocanvas
[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         init_post_engine ();
49
50         if (engine->start () != 0) {
51                 std::cerr << "Cannot start Audio/MIDI engine\n";
52                 ::exit (1);
53         }
54
55         Session* session = new Session (*engine, dir, state);
56         engine->set_session (session);
57         return session;
58 }
59
60 string session_name = "";
61 string backend_client_name = "ardour";
62 string backend_session_uuid;
63 bool just_version = false;
64 bool use_vst = true;
65 bool try_hw_optimization = true;
66 bool no_connect_ports = false;
67
68 void
69 print_help ()
70 {
71         cout << "Usage: hardour [OPTIONS]... DIR SNAPSHOT_NAME\n\n"
72              << "  DIR                         Directory/Folder to load session from\n"
73              << "  SNAPSHOT_NAME               Name of session/snapshot to load (without .ardour at end\n"
74              << "  -v, --version               Show version information\n"
75              << "  -h, --help                  Print this message\n"
76              << "  -c, --name <name>           Use a specific backend client name, default is ardour\n"
77              << "  -d, --disable-plugins       Disable all plugins in an existing session\n"
78              << "  -D, --debug <options>       Set debug flags. Use \"-D list\" to see available options\n"
79              << "  -O, --no-hw-optimizations   Disable h/w specific optimizations\n"
80              << "  -P, --no-connect-ports      Do not connect any ports at startup\n"
81 #ifdef WINDOWS_VST_SUPPORT
82              << "  -V, --novst                 Do not use VST support\n"
83 #endif
84                 ;
85 }
86
87 int main (int argc, char* argv[])
88 {
89         const char *optstring = "vhdD:c:VOU:P";
90
91         const struct option longopts[] = {
92                 { "version", 0, 0, 'v' },
93                 { "help", 0, 0, 'h' },
94                 { "disable-plugins", 1, 0, 'd' },
95                 { "debug", 1, 0, 'D' },
96                 { "name", 1, 0, 'c' },
97                 { "novst", 0, 0, 'V' },
98                 { "no-hw-optimizations", 0, 0, 'O' },
99                 { "uuid", 1, 0, 'U' },
100                 { "no-connect-ports", 0, 0, 'P' },
101                 { 0, 0, 0, 0 }
102         };
103
104         int option_index = 0;
105         int c = 0;
106
107         while (1) {
108                 c = getopt_long (argc, argv, optstring, longopts, &option_index);
109
110                 if (c == -1) {
111                         break;
112                 }
113
114                 switch (c) {
115                 case 0:
116                         break;
117
118                 case 'v':
119                         just_version = true;
120                         break;
121
122                 case 'h':
123                         print_help ();
124                         exit (0);
125                         break;
126
127                 case 'c':
128                         backend_client_name = optarg;
129                         break;
130
131                 case 'd':
132                         ARDOUR::Session::set_disable_all_loaded_plugins (true);
133                         break;
134
135                 case 'D':
136                         if (PBD::parse_debug_options (optarg)) {
137                                 ::exit (1);
138                         }
139                         break;
140
141                 case 'O':
142                         try_hw_optimization = false;
143                         break;
144
145                 case 'P':
146                         no_connect_ports = true;
147                         break;
148
149                 case 'V':
150 #ifdef WINDOWS_VST_SUPPORT
151                         use_vst = false;
152 #endif /* WINDOWS_VST_SUPPORT */
153                         break;
154
155                 case 'U':
156                         backend_session_uuid = optarg;
157                         break;
158
159                 default:
160                         print_help ();
161                         ::exit (1);
162                 }
163         }
164
165         if (argc < 3) {
166                 print_help ();
167                 ::exit (1);
168         }
169
170         if (!ARDOUR::init (false, true, localedir)) {
171                 cerr << "Ardour failed to initialize\n" << endl;
172                 ::exit (1);
173         }
174
175         Session* s = 0;
176         
177         try {
178                 s = load_session (argv[optind], argv[optind+1]);
179         } catch (failed_constructor& e) {
180                 cerr << "failed_constructor: " << e.what() << "\n";
181                 exit (EXIT_FAILURE);
182         } catch (AudioEngine::PortRegistrationFailure& e) {
183                 cerr << "PortRegistrationFailure: " << e.what() << "\n";
184                 exit (EXIT_FAILURE);
185         } catch (exception& e) {
186                 cerr << "exception: " << e.what() << "\n";
187                 exit (EXIT_FAILURE);
188         } catch (...) {
189                 cerr << "unknown exception.\n";
190                 exit (EXIT_FAILURE);
191         }
192
193         s->request_transport_speed (1.0);
194         
195         sleep (-1);
196
197         AudioEngine::instance()->remove_session ();
198         delete s;
199         AudioEngine::instance()->stop ();
200
201         AudioEngine::destroy ();
202
203         return 0;
204 }