finish unfinished work at basing all install paths on the program name
[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 #define sleep(X) Sleep((X) * 1000)
21 #endif
22
23 static const char* localedir = LOCALEDIR;
24
25 TestReceiver test_receiver;
26
27 /** @param dir Session directory.
28  *  @param state Session state file, without .ardour suffix.
29  */
30 Session *
31 load_session (string dir, string state)
32 {
33         SessionEvent::create_per_thread_pool ("test", 512);
34
35         test_receiver.listen_to (error);
36         test_receiver.listen_to (info);
37         test_receiver.listen_to (fatal);
38         test_receiver.listen_to (warning);
39
40         AudioEngine* engine = AudioEngine::create ();
41
42         if (!engine->set_default_backend ()) {
43                 std::cerr << "Cannot create Audio/MIDI engine\n";
44                 ::exit (1);
45         }
46
47         init_post_engine ();
48
49         if (engine->start () != 0) {
50                 std::cerr << "Cannot start Audio/MIDI engine\n";
51                 ::exit (1);
52         }
53
54         Session* session = new Session (*engine, dir, state);
55         engine->set_session (session);
56         return session;
57 }
58
59 string session_name = "";
60 string backend_client_name = "ardour";
61 string backend_session_uuid;
62 bool just_version = false;
63 bool use_vst = true;
64 bool try_hw_optimization = true;
65 bool no_connect_ports = false;
66
67 void
68 print_help ()
69 {
70         cout << "Usage: hardour [OPTIONS]... DIR SNAPSHOT_NAME\n\n"
71              << "  DIR                         Directory/Folder to load session from\n"
72              << "  SNAPSHOT_NAME               Name of session/snapshot to load (without .ardour at end\n"
73              << "  -v, --version               Show version information\n"
74              << "  -h, --help                  Print this message\n"
75              << "  -c, --name <name>           Use a specific backend client name, default is ardour\n"
76              << "  -d, --disable-plugins       Disable all plugins in an existing session\n"
77              << "  -D, --debug <options>       Set debug flags. Use \"-D list\" to see available options\n"
78              << "  -O, --no-hw-optimizations   Disable h/w specific optimizations\n"
79              << "  -P, --no-connect-ports      Do not connect any ports at startup\n"
80 #ifdef WINDOWS_VST_SUPPORT
81              << "  -V, --novst                 Do not use VST support\n"
82 #endif
83                 ;
84 }
85
86 int main (int argc, char* argv[])
87 {
88         const char *optstring = "vhdD:c:VOU:P";
89
90         const struct option longopts[] = {
91                 { "version", 0, 0, 'v' },
92                 { "help", 0, 0, 'h' },
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 'd':
131                         ARDOUR::Session::set_disable_all_loaded_plugins (true);
132                         break;
133
134                 case 'D':
135                         if (PBD::parse_debug_options (optarg)) {
136                                 ::exit (1);
137                         }
138                         break;
139
140                 case 'O':
141                         try_hw_optimization = false;
142                         break;
143
144                 case 'P':
145                         no_connect_ports = true;
146                         break;
147
148                 case 'V':
149 #ifdef WINDOWS_VST_SUPPORT
150                         use_vst = false;
151 #endif /* WINDOWS_VST_SUPPORT */
152                         break;
153
154                 case 'U':
155                         backend_session_uuid = optarg;
156                         break;
157
158                 default:
159                         print_help ();
160                         ::exit (1);
161                 }
162         }
163
164         if (argc < 3) {
165                 print_help ();
166                 ::exit (1);
167         }
168
169         if (!ARDOUR::init (false, true, localedir)) {
170                 cerr << "Ardour failed to initialize\n" << endl;
171                 ::exit (1);
172         }
173
174         Session* s = 0;
175         
176         try {
177                 s = load_session (argv[optind], argv[optind+1]);
178         } catch (failed_constructor& e) {
179                 cerr << "failed_constructor: " << e.what() << "\n";
180                 exit (EXIT_FAILURE);
181         } catch (AudioEngine::PortRegistrationFailure& e) {
182                 cerr << "PortRegistrationFailure: " << e.what() << "\n";
183                 exit (EXIT_FAILURE);
184         } catch (exception& e) {
185                 cerr << "exception: " << e.what() << "\n";
186                 exit (EXIT_FAILURE);
187         } catch (...) {
188                 cerr << "unknown exception.\n";
189                 exit (EXIT_FAILURE);
190         }
191
192         s->request_transport_speed (1.0);
193         
194         sleep (-1);
195
196         AudioEngine::instance()->remove_session ();
197         delete s;
198         AudioEngine::instance()->stop ();
199
200         AudioEngine::destroy ();
201
202         return 0;
203 }