summaryrefslogtreecommitdiff
path: root/gtk2_ardour/editor_pt_import.cc
blob: f83a76e29541155363ba8feeb2bb4697de1240da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
    Copyright (C) 2000-2006 Paul Davis

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <errno.h>
#include <unistd.h>
#include <algorithm>

#include "pbd/pthread_utils.h"
#include "pbd/basename.h"
#include "pbd/shortpath.h"
#include "pbd/stateful_diff_command.h"

#include "ardour/audio_track.h"
#include "ardour/audiofilesource.h"
#include "ardour/audioregion.h"
#include "ardour/midi_region.h"
#include "ardour/midi_track.h"
#include "ardour/midi_model.h"
#include "ardour/operations.h"
#include "ardour/region_factory.h"
#include "ardour/smf_source.h"
#include "ardour/source_factory.h"
#include "ardour/utils.h"
#include "ardour/playlist.h"
#include "ardour/session.h"
#include "pbd/memento_command.h"

#include "ptformat/ptfformat.h"

#include "ardour_ui.h"
#include "cursor_context.h"
#include "editor.h"
#include "sfdb_ui.h"
#include "editing.h"
#include "audio_time_axis.h"
#include "midi_time_axis.h"
#include "session_import_dialog.h"
#include "gui_thread.h"
#include "interthread_progress_window.h"
#include "mouse_cursors.h"
#include "editor_cursors.h"

#include "pbd/i18n.h"

using namespace std;
using namespace ARDOUR;
using namespace PBD;
using namespace Gtk;
using namespace Gtkmm2ext;
using namespace Editing;
using std::string;

/* Editor dialogs supporting the incorporation of PT sessions into ardour */

void
Editor::external_pt_dialog ()
{
	std::string ptpath;

	if (_session == 0) {
		MessageDialog msg (_("You can't import a PT session until you have a session loaded."));
		msg.run ();
		return;
	}

	Gtk::FileChooserDialog dialog (_("Import PT Session"), FILE_CHOOSER_ACTION_OPEN);
	dialog.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
	dialog.add_button (Gtk::Stock::OK, Gtk::RESPONSE_OK);

	while (true) {
		int result = dialog.run ();

		if (result == Gtk::RESPONSE_OK) {
			ptpath = dialog.get_filename ();

			if (!Glib::file_test (ptpath, Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
				Gtk::MessageDialog msg (string_compose (_("%1: this is only the directory/folder name, not the filename.\n"), ptpath));
				msg.run ();
				continue;
			}
		}

		if (ptpath.length ()) {
			uint32_t srate = _session->sample_rate ();

			if (import_ptf.load (ptpath, srate) == -1) {
				MessageDialog msg (_("Doesn't seem to be a valid PT session file"));
				msg.run ();
				return;
			} else {
				MessageDialog msg (string_compose (_("PT v%1 Session @ %2Hz\n\n%3 audio files\n%4 audio regions\n%5 active audio regions\n%6 midi regions\n%7 active midi regions\n\nContinue..."), (int)import_ptf.version, import_ptf.sessionrate, import_ptf.audiofiles.size (), import_ptf.regions.size (), import_ptf.tracks.size (), import_ptf.midiregions.size (), import_ptf.miditracks.size ()));
				msg.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);

				int result = msg.run ();
				if (result != Gtk::RESPONSE_OK) {
					return;
				}
			}

			ImportProgressWindow ipw (&import_pt_status, _("PT Import"), _("Cancel Import"));
			pthread_create_and_store ("import_pt", &import_pt_status.thread, _import_pt_thread, this);
			pthread_detach (import_pt_status.thread);

			ipw.show();

			import_pt_status.all_done = false;

			while (!import_pt_status.done && !import_pt_status.cancel) {
				gtk_main_iteration ();
			}

			// wait for thread to terminate
			while (!import_pt_status.all_done) {
				gtk_main_iteration ();
			}

			if (import_pt_status.cancel) {
				MessageDialog msg (_("PT import was cancelled,\ncheck session for incomplete import"));
				msg.run ();
			} else {
				MessageDialog msg (_("PT import complete!"));
				msg.run ();
			}
			break;
		}

		if (result == Gtk::RESPONSE_CANCEL) {
			break;
		}
	}
}

void *
Editor::_import_pt_thread (void *arg)
{
	SessionEvent::create_per_thread_pool ("import pt events", 2048);

	Editor *ed = (Editor *) arg;
	return ed->import_pt_thread ();
}

void *
Editor::import_pt_thread ()
{
	_session->import_pt (import_ptf, import_pt_status);
	return 0;
}