Fix astate setting for combo parameters when setting all astate
[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 = "vhBdD:c:VOU:P";
90
91         const struct option longopts[] = {
92                 { "version", 0, 0, 'v' },
93                 { "help", 0, 0, 'h' },
94                 { "bypass-plugins", 1, 0, 'B' },
95                 { "disable-plugins", 1, 0, 'd' },
96                 { "debug", 1, 0, 'D' },
97                 { "name", 1, 0, 'c' },
98                 { "novst", 0, 0, 'V' },
99                 { "no-hw-optimizations", 0, 0, 'O' },
100                 { "uuid", 1, 0, 'U' },
101                 { "no-connect-ports", 0, 0, 'P' },
102                 { 0, 0, 0, 0 }
103         };
104
105         int option_index = 0;
106         int c = 0;
107
108         while (1) {
109                 c = getopt_long (argc, argv, optstring, longopts, &option_index);
110
111                 if (c == -1) {
112                         break;
113                 }
114
115                 switch (c) {
116                 case 0:
117                         break;
118
119                 case 'v':
120                         just_version = true;
121                         break;
122
123                 case 'h':
124                         print_help ();
125                         exit (0);
126                         break;
127
128                 case 'c':
129                         backend_client_name = optarg;
130                         break;
131
132                 case 'B':
133                         ARDOUR::Session::set_bypass_all_loaded_plugins (true);
134                         break;
135
136                 case 'd':
137                         ARDOUR::Session::set_disable_all_loaded_plugins (true);
138                         break;
139
140                 case 'D':
141                         if (PBD::parse_debug_options (optarg)) {
142                                 ::exit (1);
143                         }
144                         break;
145
146                 case 'O':
147                         try_hw_optimization = false;
148                         break;
149
150                 case 'P':
151                         no_connect_ports = true;
152                         break;
153
154                 case 'V':
155 #ifdef WINDOWS_VST_SUPPORT
156                         use_vst = false;
157 #endif /* WINDOWS_VST_SUPPORT */
158                         break;
159
160                 case 'U':
161                         backend_session_uuid = optarg;
162                         break;
163
164                 default:
165                         print_help ();
166                         ::exit (1);
167                 }
168         }
169
170         if (argc < 3) {
171                 print_help ();
172                 ::exit (1);
173         }
174
175         if (!ARDOUR::init (false, true, localedir)) {
176                 cerr << "Ardour failed to initialize\n" << endl;
177                 ::exit (1);
178         }
179
180         Session* s = 0;
181
182         try {
183                 s = load_session (argv[optind], argv[optind+1]);
184         } catch (failed_constructor& e) {
185                 cerr << "failed_constructor: " << e.what() << "\n";
186                 exit (EXIT_FAILURE);
187         } catch (AudioEngine::PortRegistrationFailure& e) {
188                 cerr << "PortRegistrationFailure: " << e.what() << "\n";
189                 exit (EXIT_FAILURE);
190         } catch (exception& e) {
191                 cerr << "exception: " << e.what() << "\n";
192                 exit (EXIT_FAILURE);
193         } catch (...) {
194                 cerr << "unknown exception.\n";
195                 exit (EXIT_FAILURE);
196         }
197
198         s->request_transport_speed (1.0);
199
200         sleep (-1);
201
202         AudioEngine::instance()->remove_session ();
203         delete s;
204         AudioEngine::instance()->stop ();
205
206         AudioEngine::destroy ();
207
208         return 0;
209 }