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