/[svn]/gigedit/trunk/src/gigedit/mainwindow.cpp
ViewVC logotype

Contents of /gigedit/trunk/src/gigedit/mainwindow.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1322 - (show annotations) (download)
Tue Sep 4 11:04:56 2007 UTC (16 years, 6 months ago) by schoenebeck
File size: 55938 byte(s)
* as counterpart to latest LS commit: added experimental support to
  synchronize gigedit with LinuxSampler to avoid race conditions / crash
  while modifying data structures and playing the instrument with LS at
  the same time
* packaging fixes: don't use a hard coded path to install the LS plugin
  DLL, trying to substitute the given LS plugin directory by the
  '${libdir}' automake variable (mandatory i.e. for Gentoo ebuild) and
  include plugin/linuxsamplerplugin.h into the release tarball
  ('make dist')
* updated German translation (po/de.po)

1 /*
2 * Copyright (C) 2006, 2007 Andreas Persson
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2, or (at
7 * your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with program; see the file COPYING. If not, write to the Free
16 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17 * 02110-1301 USA.
18 */
19
20 #include <libintl.h>
21 #include <iostream>
22
23 #include <gtkmm/filechooserdialog.h>
24 #include <gtkmm/messagedialog.h>
25 #include <gtkmm/stock.h>
26 #include <gtkmm/targetentry.h>
27 #include <gtkmm/main.h>
28
29 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 6) || GTKMM_MAJOR_VERSION > 2
30 #define ABOUT_DIALOG
31 #include <gtkmm/aboutdialog.h>
32 #endif
33
34 #if (GLIBMM_MAJOR_VERSION == 2 && GLIBMM_MINOR_VERSION < 6) || GLIBMM_MAJOR_VERSION < 2
35 namespace Glib {
36 Glib::ustring filename_display_basename(const std::string& filename)
37 {
38 gchar* gstr = g_path_get_basename(filename.c_str());
39 Glib::ustring str(gstr);
40 g_free(gstr);
41 return Glib::filename_to_utf8(str);
42 }
43 }
44 #endif
45
46 #include <stdio.h>
47 #include <sndfile.h>
48
49 #include "mainwindow.h"
50
51 #define _(String) gettext(String)
52
53 template<class T> inline std::string ToString(T o) {
54 std::stringstream ss;
55 ss << o;
56 return ss.str();
57 }
58
59 MainWindow::MainWindow()
60 {
61 // set_border_width(5);
62 // set_default_size(400, 200);
63
64
65 add(m_VBox);
66
67 // Handle selection
68 Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();
69 tree_sel_ref->signal_changed().connect(
70 sigc::mem_fun(*this, &MainWindow::on_sel_change));
71
72 // m_TreeView.set_reorderable();
73
74 m_TreeView.signal_button_press_event().connect_notify(
75 sigc::mem_fun(*this, &MainWindow::on_button_release));
76
77 // Add the TreeView tab, inside a ScrolledWindow, with the button underneath:
78 m_ScrolledWindow.add(m_TreeView);
79 // m_ScrolledWindow.set_size_request(200, 600);
80 m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
81
82 m_ScrolledWindowSamples.add(m_TreeViewSamples);
83 m_ScrolledWindowSamples.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
84
85
86 m_TreeViewNotebook.set_size_request(300);
87
88 m_HPaned.add1(m_TreeViewNotebook);
89 m_HPaned.add2(dimreg_edit);
90
91
92 m_TreeViewNotebook.append_page(m_ScrolledWindowSamples, "Samples");
93 m_TreeViewNotebook.append_page(m_ScrolledWindow, "Instruments");
94
95
96 actionGroup = Gtk::ActionGroup::create();
97
98 actionGroup->add(Gtk::Action::create("MenuFile", _("_File")));
99 actionGroup->add(Gtk::Action::create("New", Gtk::Stock::NEW),
100 sigc::mem_fun(
101 *this, &MainWindow::on_action_file_new));
102 Glib::RefPtr<Gtk::Action> action =
103 Gtk::Action::create("Open", Gtk::Stock::OPEN);
104 action->property_label() = action->property_label() + "...";
105 actionGroup->add(action,
106 sigc::mem_fun(
107 *this, &MainWindow::on_action_file_open));
108 actionGroup->add(Gtk::Action::create("Save", Gtk::Stock::SAVE),
109 sigc::mem_fun(
110 *this, &MainWindow::on_action_file_save));
111 action = Gtk::Action::create("SaveAs", Gtk::Stock::SAVE_AS);
112 action->property_label() = action->property_label() + "...";
113 actionGroup->add(action,
114 Gtk::AccelKey("<shift><control>s"),
115 sigc::mem_fun(
116 *this, &MainWindow::on_action_file_save_as));
117 actionGroup->add(Gtk::Action::create("Properties",
118 Gtk::Stock::PROPERTIES),
119 sigc::mem_fun(
120 *this, &MainWindow::on_action_file_properties));
121 actionGroup->add(Gtk::Action::create("InstrProperties",
122 Gtk::Stock::PROPERTIES),
123 sigc::mem_fun(
124 *this, &MainWindow::show_instr_props));
125 actionGroup->add(Gtk::Action::create("Quit", Gtk::Stock::QUIT),
126 sigc::mem_fun(
127 *this, &MainWindow::on_action_quit));
128 actionGroup->add(Gtk::Action::create("MenuInstrument", _("_Instrument")));
129
130 action = Gtk::Action::create("MenuHelp", Gtk::Stock::HELP);
131 actionGroup->add(Gtk::Action::create("MenuHelp",
132 action->property_label()));
133 #ifdef ABOUT_DIALOG
134 actionGroup->add(Gtk::Action::create("About", Gtk::Stock::ABOUT),
135 sigc::mem_fun(
136 *this, &MainWindow::on_action_help_about));
137 #endif
138 actionGroup->add(
139 Gtk::Action::create("AddInstrument", _("Add _Instrument")),
140 sigc::mem_fun(*this, &MainWindow::on_action_add_instrument)
141 );
142 actionGroup->add(
143 Gtk::Action::create("RemoveInstrument", Gtk::Stock::REMOVE),
144 sigc::mem_fun(*this, &MainWindow::on_action_remove_instrument)
145 );
146
147 // sample right-click popup actions
148 actionGroup->add(
149 Gtk::Action::create("SampleProperties", Gtk::Stock::PROPERTIES),
150 sigc::mem_fun(*this, &MainWindow::on_action_sample_properties)
151 );
152 actionGroup->add(
153 Gtk::Action::create("AddGroup", _("Add _Group")),
154 sigc::mem_fun(*this, &MainWindow::on_action_add_group)
155 );
156 actionGroup->add(
157 Gtk::Action::create("AddSample", _("Add _Sample(s)")),
158 sigc::mem_fun(*this, &MainWindow::on_action_add_sample)
159 );
160 actionGroup->add(
161 Gtk::Action::create("RemoveSample", Gtk::Stock::REMOVE),
162 sigc::mem_fun(*this, &MainWindow::on_action_remove_sample)
163 );
164
165 uiManager = Gtk::UIManager::create();
166 uiManager->insert_action_group(actionGroup);
167 add_accel_group(uiManager->get_accel_group());
168
169 Glib::ustring ui_info =
170 "<ui>"
171 " <menubar name='MenuBar'>"
172 " <menu action='MenuFile'>"
173 " <menuitem action='New'/>"
174 " <menuitem action='Open'/>"
175 " <separator/>"
176 " <menuitem action='Save'/>"
177 " <menuitem action='SaveAs'/>"
178 " <separator/>"
179 " <menuitem action='Properties'/>"
180 " <separator/>"
181 " <menuitem action='Quit'/>"
182 " </menu>"
183 " <menu action='MenuInstrument'>"
184 " </menu>"
185 #ifdef ABOUT_DIALOG
186 " <menu action='MenuHelp'>"
187 " <menuitem action='About'/>"
188 " </menu>"
189 #endif
190 " </menubar>"
191 " <popup name='PopupMenu'>"
192 " <menuitem action='InstrProperties'/>"
193 " <menuitem action='AddInstrument'/>"
194 " <separator/>"
195 " <menuitem action='RemoveInstrument'/>"
196 " </popup>"
197 " <popup name='SamplePopupMenu'>"
198 " <menuitem action='SampleProperties'/>"
199 " <menuitem action='AddGroup'/>"
200 " <menuitem action='AddSample'/>"
201 " <separator/>"
202 " <menuitem action='RemoveSample'/>"
203 " </popup>"
204 "</ui>";
205 uiManager->add_ui_from_string(ui_info);
206
207 popup_menu = dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/PopupMenu"));
208
209 Gtk::Widget* menuBar = uiManager->get_widget("/MenuBar");
210 m_VBox.pack_start(*menuBar, Gtk::PACK_SHRINK);
211 m_VBox.pack_start(m_HPaned);
212 m_VBox.pack_start(m_RegionChooser, Gtk::PACK_SHRINK);
213 m_VBox.pack_start(m_DimRegionChooser, Gtk::PACK_SHRINK);
214
215 m_RegionChooser.signal_region_selected().connect(
216 sigc::mem_fun(*this, &MainWindow::region_changed) );
217 m_DimRegionChooser.signal_dimregion_selected().connect(
218 sigc::mem_fun(*this, &MainWindow::dimreg_changed) );
219
220
221 // Create the Tree model:
222 m_refTreeModel = Gtk::ListStore::create(m_Columns);
223 m_TreeView.set_model(m_refTreeModel);
224 m_refTreeModel->signal_row_changed().connect(
225 sigc::mem_fun(*this, &MainWindow::instrument_name_changed)
226 );
227
228 // Add the TreeView's view columns:
229 m_TreeView.append_column_editable("Instrument", m_Columns.m_col_name);
230 m_TreeView.set_headers_visible(false);
231
232 // create samples treeview (including its data model)
233 m_refSamplesTreeModel = SamplesTreeStore::create(m_SamplesModel);
234 m_TreeViewSamples.set_model(m_refSamplesTreeModel);
235 // m_TreeViewSamples.set_reorderable();
236 m_TreeViewSamples.append_column_editable("Samples", m_SamplesModel.m_col_name);
237 m_TreeViewSamples.set_headers_visible(false);
238 m_TreeViewSamples.signal_button_press_event().connect_notify(
239 sigc::mem_fun(*this, &MainWindow::on_sample_treeview_button_release)
240 );
241 m_refSamplesTreeModel->signal_row_changed().connect(
242 sigc::mem_fun(*this, &MainWindow::sample_name_changed)
243 );
244
245 // establish drag&drop between samples tree view and dimension region 'Sample' text entry
246 std::list<Gtk::TargetEntry> drag_target_gig_sample;
247 drag_target_gig_sample.push_back( Gtk::TargetEntry("gig::Sample") );
248 m_TreeViewSamples.drag_source_set(drag_target_gig_sample);
249 m_TreeViewSamples.signal_drag_begin().connect(
250 sigc::mem_fun(*this, &MainWindow::on_sample_treeview_drag_begin)
251 );
252 m_TreeViewSamples.signal_drag_data_get().connect(
253 sigc::mem_fun(*this, &MainWindow::on_sample_treeview_drag_data_get)
254 );
255 dimreg_edit.wSample->drag_dest_set(drag_target_gig_sample);
256 dimreg_edit.wSample->signal_drag_data_received().connect(
257 sigc::mem_fun(*this, &MainWindow::on_sample_label_drop_drag_data_received)
258 );
259 dimreg_edit.signal_dimreg_changed().connect(
260 sigc::hide(sigc::mem_fun(*this, &MainWindow::file_changed)));
261 m_RegionChooser.signal_instrument_changed().connect(
262 sigc::mem_fun(*this, &MainWindow::file_changed));
263 m_DimRegionChooser.signal_region_changed().connect(
264 sigc::mem_fun(*this, &MainWindow::file_changed));
265 instrumentProps.signal_instrument_changed().connect(
266 sigc::mem_fun(*this, &MainWindow::file_changed));
267
268 dimreg_edit.signal_dimreg_to_be_changed().connect(
269 dimreg_to_be_changed_signal.make_slot());
270 dimreg_edit.signal_dimreg_changed().connect(
271 dimreg_changed_signal.make_slot());
272 dimreg_edit.signal_sample_ref_changed().connect(
273 sample_ref_changed_signal.make_slot());
274
275 m_RegionChooser.signal_instrument_struct_to_be_changed().connect(
276 sigc::hide(
277 sigc::bind(
278 file_structure_to_be_changed_signal.make_slot(),
279 sigc::ref(this->file)
280 )
281 )
282 );
283 m_RegionChooser.signal_instrument_struct_changed().connect(
284 sigc::hide(
285 sigc::bind(
286 file_structure_changed_signal.make_slot(),
287 sigc::ref(this->file)
288 )
289 )
290 );
291 m_RegionChooser.signal_region_to_be_changed().connect(
292 region_to_be_changed_signal.make_slot());
293 m_RegionChooser.signal_region_changed_signal().connect(
294 region_changed_signal.make_slot());
295
296 file = 0;
297 file_is_changed = false;
298
299 show_all_children();
300
301 // start with a new gig file by default
302 on_action_file_new();
303 }
304
305 MainWindow::~MainWindow()
306 {
307 }
308
309 bool MainWindow::on_delete_event(GdkEventAny* event)
310 {
311 return file_is_changed && !close_confirmation_dialog();
312 }
313
314 void MainWindow::on_action_quit()
315 {
316 if (file_is_changed && !close_confirmation_dialog()) return;
317 hide();
318 }
319
320 void MainWindow::region_changed()
321 {
322 m_DimRegionChooser.set_region(m_RegionChooser.get_region());
323 }
324
325 void MainWindow::dimreg_changed()
326 {
327 dimreg_edit.set_dim_region(m_DimRegionChooser.get_dimregion());
328 }
329
330 void MainWindow::on_sel_change()
331 {
332 Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();
333
334 Gtk::TreeModel::iterator it = tree_sel_ref->get_selected();
335 if (it) {
336 Gtk::TreeModel::Row row = *it;
337 std::cout << row[m_Columns.m_col_name] << std::endl;
338
339 m_RegionChooser.set_instrument(row[m_Columns.m_col_instr]);
340 } else {
341 m_RegionChooser.set_instrument(0);
342 }
343 }
344
345 void loader_progress_callback(gig::progress_t* progress)
346 {
347 Loader* loader = static_cast<Loader*>(progress->custom);
348 loader->progress_callback(progress->factor);
349 }
350
351 void Loader::progress_callback(float fraction)
352 {
353 {
354 Glib::Mutex::Lock lock(progressMutex);
355 progress = fraction;
356 }
357 progress_dispatcher();
358 }
359
360 void Loader::thread_function()
361 {
362 printf("thread_function self=%x\n", Glib::Thread::self());
363 printf("Start %s\n", filename);
364 RIFF::File* riff = new RIFF::File(filename);
365 gig = new gig::File(riff);
366 gig::progress_t progress;
367 progress.callback = loader_progress_callback;
368 progress.custom = this;
369
370 gig->GetInstrument(0, &progress);
371 printf("End\n");
372 finished_dispatcher();
373 }
374
375 Loader::Loader(const char* filename)
376 : thread(0), filename(filename)
377 {
378 }
379
380 void Loader::launch()
381 {
382 thread = Glib::Thread::create(sigc::mem_fun(*this, &Loader::thread_function), true);
383 printf("launch thread=%x\n", thread);
384 }
385
386 float Loader::get_progress()
387 {
388 float res;
389 {
390 Glib::Mutex::Lock lock(progressMutex);
391 res = progress;
392 }
393 return res;
394 }
395
396 Glib::Dispatcher& Loader::signal_progress()
397 {
398 return progress_dispatcher;
399 }
400
401 Glib::Dispatcher& Loader::signal_finished()
402 {
403 return finished_dispatcher;
404 }
405
406 LoadDialog::LoadDialog(const Glib::ustring& title, Gtk::Window& parent)
407 : Gtk::Dialog(title, parent, true)
408 {
409 get_vbox()->pack_start(progressBar);
410 show_all_children();
411 }
412
413 // Clear all GUI elements / controls. This method is typically called
414 // before a new .gig file is to be created or to be loaded.
415 void MainWindow::__clear() {
416 // remove all entries from "Instrument" menu
417 Gtk::MenuItem* instrument_menu =
418 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuInstrument"));
419 instrument_menu->hide();
420 for (int i = 0; i < instrument_menu->get_submenu()->items().size(); i++) {
421 delete &instrument_menu->get_submenu()->items()[i];
422 }
423 instrument_menu->get_submenu()->items().clear();
424 // forget all samples that ought to be imported
425 m_SampleImportQueue.clear();
426 // clear the samples and instruments tree views
427 m_refTreeModel->clear();
428 m_refSamplesTreeModel->clear();
429 // free libgig's gig::File instance
430 if (file) {
431 delete file;
432 file = NULL;
433 }
434 }
435
436 void MainWindow::on_action_file_new()
437 {
438 if (file_is_changed && !close_confirmation_dialog()) return;
439
440 // clear all GUI elements
441 __clear();
442 // create a new .gig file (virtually yet)
443 gig::File* pFile = new gig::File;
444 // already add one new instrument by default
445 gig::Instrument* pInstrument = pFile->AddInstrument();
446 pInstrument->pInfo->Name = "Unnamed Instrument";
447 // update GUI with that new gig::File
448 load_gig(pFile, 0 /*no file name yet*/);
449 }
450
451 bool MainWindow::close_confirmation_dialog()
452 {
453 gchar* msg = g_strdup_printf(_("Save changes to \"%s\" before closing?"),
454 Glib::filename_display_basename(filename).c_str());
455 Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
456 g_free(msg);
457 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 6) || GTKMM_MAJOR_VERSION > 2
458 dialog.set_secondary_text(_("If you close without saving, your changes will be lost."));
459 #endif
460 dialog.add_button(_("Close _Without Saving"), Gtk::RESPONSE_NO);
461 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
462 dialog.add_button(file_has_name ? Gtk::Stock::SAVE : Gtk::Stock::SAVE_AS, Gtk::RESPONSE_YES);
463 dialog.set_default_response(Gtk::RESPONSE_YES);
464 int response = dialog.run();
465 dialog.hide();
466 if (response == Gtk::RESPONSE_YES) return file_save();
467 return response != Gtk::RESPONSE_CANCEL;
468 }
469
470 void MainWindow::on_action_file_open()
471 {
472 if (file_is_changed && !close_confirmation_dialog()) return;
473
474 Gtk::FileChooserDialog dialog(*this, _("Open file"));
475 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
476 dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
477 dialog.set_default_response(Gtk::RESPONSE_OK);
478 Gtk::FileFilter filter;
479 filter.add_pattern("*.gig");
480 dialog.set_filter(filter);
481 if (current_dir != "") {
482 dialog.set_current_folder(current_dir);
483 }
484 if (dialog.run() == Gtk::RESPONSE_OK) {
485 std::string filename = dialog.get_filename();
486 printf("filename=%s\n", filename.c_str());
487 printf("on_action_file_open self=%x\n", Glib::Thread::self());
488 load_file(filename.c_str());
489 current_dir = Glib::path_get_dirname(filename);
490 }
491 }
492
493 void MainWindow::load_file(const char* name)
494 {
495 __clear();
496 load_dialog = new LoadDialog("Loading...", *this);
497 load_dialog->show_all();
498 loader = new Loader(strdup(name));
499 loader->signal_progress().connect(
500 sigc::mem_fun(*this, &MainWindow::on_loader_progress));
501 loader->signal_finished().connect(
502 sigc::mem_fun(*this, &MainWindow::on_loader_finished));
503 loader->launch();
504 }
505
506 void MainWindow::load_instrument(gig::Instrument* instr) {
507 if (!instr) {
508 Glib::ustring txt = "Provided instrument is NULL!\n";
509 Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
510 msg.run();
511 Gtk::Main::quit();
512 }
513 gig::File* pFile = (gig::File*) instr->GetParent();
514 load_gig(pFile, 0 /*file name*/);
515 //TODO: automatically select the given instrument
516 }
517
518 void MainWindow::on_loader_progress()
519 {
520 load_dialog->set_fraction(loader->get_progress());
521 }
522
523 void MainWindow::on_loader_finished()
524 {
525 printf("Loader finished!\n");
526 printf("on_loader_finished self=%x\n", Glib::Thread::self());
527 load_gig(loader->gig, loader->filename);
528 load_dialog->hide();
529 }
530
531 void MainWindow::on_action_file_save()
532 {
533 file_save();
534 }
535
536 bool MainWindow::check_if_savable()
537 {
538 if (!file) return false;
539
540 if (!file->GetFirstSample()) {
541 Gtk::MessageDialog(*this, _("The file could not be saved "
542 "because it contains no samples"),
543 false, Gtk::MESSAGE_ERROR).run();
544 return false;
545 }
546
547 for (gig::Instrument* instrument = file->GetFirstInstrument() ; instrument ;
548 instrument = file->GetNextInstrument()) {
549 if (!instrument->GetFirstRegion()) {
550 Gtk::MessageDialog(*this, _("The file could not be saved "
551 "because there are instruments "
552 "that have no regions"),
553 false, Gtk::MESSAGE_ERROR).run();
554 return false;
555 }
556 }
557 return true;
558 }
559
560 bool MainWindow::file_save()
561 {
562 if (!check_if_savable()) return false;
563 if (!file_has_name) return file_save_as();
564
565 std::cout << "Saving file\n" << std::flush;
566 file_structure_to_be_changed_signal.emit(this->file);
567 try {
568 file->Save();
569 if (file_is_changed) {
570 set_title(get_title().substr(1));
571 file_is_changed = false;
572 }
573 } catch (RIFF::Exception e) {
574 file_structure_changed_signal.emit(this->file);
575 Glib::ustring txt = "Could not save file: " + e.Message;
576 Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
577 msg.run();
578 return false;
579 }
580 std::cout << "Saving file done\n" << std::flush;
581 __import_queued_samples();
582 file_structure_changed_signal.emit(this->file);
583 return true;
584 }
585
586 void MainWindow::on_action_file_save_as()
587 {
588 if (!check_if_savable()) return;
589 file_save_as();
590 }
591
592 bool MainWindow::file_save_as()
593 {
594 Gtk::FileChooserDialog dialog(*this, _("Save as"), Gtk::FILE_CHOOSER_ACTION_SAVE);
595 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
596 dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);
597 dialog.set_default_response(Gtk::RESPONSE_OK);
598
599 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 8) || GTKMM_MAJOR_VERSION > 2
600 dialog.set_do_overwrite_confirmation();
601 // TODO: an overwrite dialog for gtkmm < 2.8
602 #endif
603 Gtk::FileFilter filter;
604 filter.add_pattern("*.gig");
605 dialog.set_filter(filter);
606
607 if (Glib::path_is_absolute(filename)) {
608 dialog.set_filename(filename);
609 } else if (current_dir != "") {
610 dialog.set_current_folder(current_dir);
611 }
612 dialog.set_current_name(Glib::filename_display_basename(filename));
613
614 if (dialog.run() == Gtk::RESPONSE_OK) {
615 file_structure_to_be_changed_signal.emit(this->file);
616 try {
617 std::string filename = dialog.get_filename();
618 if (!Glib::str_has_suffix(filename, ".gig")) {
619 filename += ".gig";
620 }
621 printf("filename=%s\n", filename.c_str());
622 file->Save(filename);
623 this->filename = filename;
624 current_dir = Glib::path_get_dirname(filename);
625 set_title(Glib::filename_display_basename(filename));
626 file_has_name = true;
627 file_is_changed = false;
628 } catch (RIFF::Exception e) {
629 file_structure_changed_signal.emit(this->file);
630 Glib::ustring txt = "Could not save file: " + e.Message;
631 Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
632 msg.run();
633 return false;
634 }
635 __import_queued_samples();
636 file_structure_changed_signal.emit(this->file);
637 return true;
638 }
639 return false;
640 }
641
642 // actually write the sample(s)' data to the gig file
643 void MainWindow::__import_queued_samples() {
644 std::cout << "Starting sample import\n" << std::flush;
645 Glib::ustring error_files;
646 printf("Samples to import: %d\n", m_SampleImportQueue.size());
647 for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
648 iter != m_SampleImportQueue.end(); ) {
649 printf("Importing sample %s\n",(*iter).sample_path.c_str());
650 SF_INFO info;
651 info.format = 0;
652 SNDFILE* hFile = sf_open((*iter).sample_path.c_str(), SFM_READ, &info);
653 try {
654 if (!hFile) throw std::string("could not open file");
655 // determine sample's bit depth
656 int bitdepth;
657 switch (info.format & 0xff) {
658 case SF_FORMAT_PCM_S8:
659 case SF_FORMAT_PCM_16:
660 case SF_FORMAT_PCM_U8:
661 bitdepth = 16;
662 break;
663 case SF_FORMAT_PCM_24:
664 case SF_FORMAT_PCM_32:
665 case SF_FORMAT_FLOAT:
666 case SF_FORMAT_DOUBLE:
667 bitdepth = 24;
668 break;
669 default:
670 sf_close(hFile); // close sound file
671 throw std::string("format not supported"); // unsupported subformat (yet?)
672 }
673
674 const int bufsize = 10000;
675 switch (bitdepth) {
676 case 16: {
677 short* buffer = new short[bufsize * info.channels];
678 sf_count_t cnt = info.frames;
679 while (cnt) {
680 // libsndfile does the conversion for us (if needed)
681 int n = sf_readf_short(hFile, buffer, bufsize);
682 // write from buffer directly (physically) into .gig file
683 iter->gig_sample->Write(buffer, n);
684 cnt -= n;
685 }
686 delete[] buffer;
687 break;
688 }
689 case 24: {
690 int* srcbuf = new int[bufsize * info.channels];
691 uint8_t* dstbuf = new uint8_t[bufsize * 3 * info.channels];
692 sf_count_t cnt = info.frames;
693 while (cnt) {
694 // libsndfile returns 32 bits, convert to 24
695 int n = sf_readf_int(hFile, srcbuf, bufsize);
696 int j = 0;
697 for (int i = 0 ; i < n * info.channels ; i++) {
698 dstbuf[j++] = srcbuf[i] >> 8;
699 dstbuf[j++] = srcbuf[i] >> 16;
700 dstbuf[j++] = srcbuf[i] >> 24;
701 }
702 // write from buffer directly (physically) into .gig file
703 iter->gig_sample->Write(dstbuf, n);
704 cnt -= n;
705 }
706 delete[] srcbuf;
707 delete[] dstbuf;
708 break;
709 }
710 }
711 // cleanup
712 sf_close(hFile);
713 // on success we remove the sample from the import queue,
714 // otherwise keep it, maybe it works the next time ?
715 std::list<SampleImportItem>::iterator cur = iter;
716 ++iter;
717 m_SampleImportQueue.erase(cur);
718 } catch (std::string what) {
719 // remember the files that made trouble (and their cause)
720 if (error_files.size()) error_files += "\n";
721 error_files += (*iter).sample_path += " (" + what + ")";
722 ++iter;
723 }
724 }
725 // show error message box when some sample(s) could not be imported
726 if (error_files.size()) {
727 Glib::ustring txt = "Could not import the following sample(s):\n" + error_files;
728 Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
729 msg.run();
730 }
731 }
732
733 void MainWindow::on_action_file_properties()
734 {
735 propDialog.show();
736 propDialog.deiconify();
737 }
738
739 void MainWindow::on_action_help_about()
740 {
741 #ifdef ABOUT_DIALOG
742 Gtk::AboutDialog dialog;
743 dialog.set_version(VERSION);
744 dialog.run();
745 #endif
746 }
747
748 PropDialog::PropDialog()
749 : table(2,1)
750 {
751 table.set_col_spacings(5);
752 const char* propLabels[] = {
753 "Name:",
754 "CreationDate:",
755 "Comments:", // TODO: multiline
756 "Product:",
757 "Copyright:",
758 "Artists:",
759 "Genre:",
760 "Keywords:",
761 "Engineer:",
762 "Technician:",
763 "Software:", // TODO: readonly
764 "Medium:",
765 "Source:",
766 "SourceForm:",
767 "Commissioned:",
768 "Subject:"
769 };
770 for (int i = 0 ; i < sizeof(propLabels) / sizeof(char*) ; i++) {
771 label[i].set_text(propLabels[i]);
772 label[i].set_alignment(Gtk::ALIGN_LEFT);
773 table.attach(label[i], 0, 1, i, i + 1, Gtk::FILL, Gtk::SHRINK);
774 table.attach(entry[i], 1, 2, i, i + 1, Gtk::FILL | Gtk::EXPAND,
775 Gtk::SHRINK);
776 }
777
778 add(table);
779 // add_button(Gtk::Stock::CANCEL, 0);
780 // add_button(Gtk::Stock::OK, 1);
781 show_all_children();
782 }
783
784 void PropDialog::set_info(DLS::Info* info)
785 {
786 entry[0].set_text(info->Name);
787 entry[1].set_text(info->CreationDate);
788 entry[2].set_text(Glib::convert(info->Comments, "UTF-8", "ISO-8859-1"));
789 entry[3].set_text(info->Product);
790 entry[4].set_text(info->Copyright);
791 entry[5].set_text(info->Artists);
792 entry[6].set_text(info->Genre);
793 entry[7].set_text(info->Keywords);
794 entry[8].set_text(info->Engineer);
795 entry[9].set_text(info->Technician);
796 entry[10].set_text(info->Software);
797 entry[11].set_text(info->Medium);
798 entry[12].set_text(info->Source);
799 entry[13].set_text(info->SourceForm);
800 entry[14].set_text(info->Commissioned);
801 entry[15].set_text(info->Subject);
802 }
803
804 void InstrumentProps::add_prop(BoolEntry& boolentry)
805 {
806 table.attach(boolentry.widget, 0, 2, rowno, rowno + 1,
807 Gtk::FILL, Gtk::SHRINK);
808 rowno++;
809 boolentry.signal_changed_by_user().connect(instrument_changed.make_slot());
810 }
811
812 void InstrumentProps::add_prop(BoolEntryPlus6& boolentry)
813 {
814 table.attach(boolentry.widget, 0, 2, rowno, rowno + 1,
815 Gtk::FILL, Gtk::SHRINK);
816 rowno++;
817 boolentry.signal_changed_by_user().connect(instrument_changed.make_slot());
818 }
819
820 void InstrumentProps::add_prop(LabelWidget& prop)
821 {
822 table.attach(prop.label, 0, 1, rowno, rowno + 1,
823 Gtk::FILL, Gtk::SHRINK);
824 table.attach(prop.widget, 1, 2, rowno, rowno + 1,
825 Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
826 rowno++;
827 prop.signal_changed_by_user().connect(instrument_changed.make_slot());
828 }
829
830 InstrumentProps::InstrumentProps()
831 : table(2,1),
832 quitButton(Gtk::Stock::CLOSE),
833 eName("Name"),
834 eIsDrum("Is drum"),
835 eMIDIBank("MIDI bank", 0, 16383),
836 eMIDIProgram("MIDI program"),
837 eAttenuation("Attenuation", 0, 96, 0, 1),
838 eGainPlus6("Gain +6dB", eAttenuation, -6),
839 eEffectSend("Effect send", 0, 65535),
840 eFineTune("Fine tune", -8400, 8400),
841 ePitchbendRange("Pitchbend range", 0, 12),
842 ePianoReleaseMode("Piano release mode"),
843 eDimensionKeyRangeLow("Dimension key range low"),
844 eDimensionKeyRangeHigh("Dimension key range high")
845 {
846 set_title("Instrument properties");
847
848 rowno = 0;
849 table.set_col_spacings(5);
850
851 add_prop(eName);
852 add_prop(eIsDrum);
853 add_prop(eMIDIBank);
854 add_prop(eMIDIProgram);
855 add_prop(eAttenuation);
856 add_prop(eGainPlus6);
857 add_prop(eEffectSend);
858 add_prop(eFineTune);
859 add_prop(ePitchbendRange);
860 add_prop(ePianoReleaseMode);
861 add_prop(eDimensionKeyRangeLow);
862 add_prop(eDimensionKeyRangeHigh);
863
864 eDimensionKeyRangeLow.signal_changed_by_user().connect(
865 sigc::mem_fun(*this, &InstrumentProps::key_range_low_changed));
866 eDimensionKeyRangeHigh.signal_changed_by_user().connect(
867 sigc::mem_fun(*this, &InstrumentProps::key_range_high_changed));
868
869 add(vbox);
870 table.set_border_width(5);
871 vbox.pack_start(table);
872 table.show();
873 vbox.pack_start(buttonBox, Gtk::PACK_SHRINK);
874 buttonBox.set_layout(Gtk::BUTTONBOX_END);
875 buttonBox.set_border_width(5);
876 buttonBox.show();
877 buttonBox.pack_start(quitButton);
878 quitButton.set_flags(Gtk::CAN_DEFAULT);
879 quitButton.grab_focus();
880
881 quitButton.signal_clicked().connect(
882 sigc::mem_fun(*this, &InstrumentProps::hide));
883
884 quitButton.show();
885 vbox.show();
886 show_all_children();
887 }
888
889 void InstrumentProps::set_instrument(gig::Instrument* instrument)
890 {
891 eName.set_ptr(&instrument->pInfo->Name);
892 eIsDrum.set_ptr(&instrument->IsDrum);
893 eMIDIBank.set_ptr(&instrument->MIDIBank);
894 eMIDIProgram.set_ptr(&instrument->MIDIProgram);
895 eAttenuation.set_ptr(&instrument->Attenuation);
896 eGainPlus6.set_ptr(&instrument->Attenuation);
897 eEffectSend.set_ptr(&instrument->EffectSend);
898 eFineTune.set_ptr(&instrument->FineTune);
899 ePitchbendRange.set_ptr(&instrument->PitchbendRange);
900 ePianoReleaseMode.set_ptr(&instrument->PianoReleaseMode);
901 eDimensionKeyRangeLow.set_ptr(0);
902 eDimensionKeyRangeHigh.set_ptr(0);
903 eDimensionKeyRangeLow.set_ptr(&instrument->DimensionKeyRange.low);
904 eDimensionKeyRangeHigh.set_ptr(&instrument->DimensionKeyRange.high);
905 }
906
907 void InstrumentProps::key_range_low_changed()
908 {
909 double l = eDimensionKeyRangeLow.get_value();
910 double h = eDimensionKeyRangeHigh.get_value();
911 if (h < l) eDimensionKeyRangeHigh.set_value(l);
912 }
913
914 void InstrumentProps::key_range_high_changed()
915 {
916 double l = eDimensionKeyRangeLow.get_value();
917 double h = eDimensionKeyRangeHigh.get_value();
918 if (h < l) eDimensionKeyRangeLow.set_value(h);
919 }
920
921 sigc::signal<void> InstrumentProps::signal_instrument_changed()
922 {
923 return instrument_changed;
924 }
925
926 void MainWindow::file_changed()
927 {
928 if (file && !file_is_changed) {
929 set_title("*" + get_title());
930 file_is_changed = true;
931 }
932 }
933
934 void MainWindow::load_gig(gig::File* gig, const char* filename)
935 {
936 file = 0;
937
938 this->filename = filename ? filename : _("Unsaved Gig File");
939 set_title(Glib::filename_display_basename(this->filename));
940 file_has_name = filename;
941 file_is_changed = false;
942
943 propDialog.set_info(gig->pInfo);
944
945 Gtk::MenuItem* instrument_menu =
946 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuInstrument"));
947
948 int instrument_index = 0;
949 Gtk::RadioMenuItem::Group instrument_group;
950 for (gig::Instrument* instrument = gig->GetFirstInstrument() ; instrument ;
951 instrument = gig->GetNextInstrument()) {
952 Gtk::TreeModel::iterator iter = m_refTreeModel->append();
953 Gtk::TreeModel::Row row = *iter;
954 row[m_Columns.m_col_name] = instrument->pInfo->Name.c_str();
955 row[m_Columns.m_col_instr] = instrument;
956 // create a menu item for this instrument
957 Gtk::RadioMenuItem* item =
958 new Gtk::RadioMenuItem(instrument_group, instrument->pInfo->Name.c_str());
959 instrument_menu->get_submenu()->append(*item);
960 item->signal_activate().connect(
961 sigc::bind(
962 sigc::mem_fun(*this, &MainWindow::on_instrument_selection_change),
963 instrument_index
964 )
965 );
966 instrument_index++;
967 }
968 instrument_menu->show();
969 instrument_menu->get_submenu()->show_all_children();
970
971 for (gig::Group* group = gig->GetFirstGroup(); group; group = gig->GetNextGroup()) {
972 if (group->Name != "") {
973 Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();
974 Gtk::TreeModel::Row rowGroup = *iterGroup;
975 rowGroup[m_SamplesModel.m_col_name] = group->Name.c_str();
976 rowGroup[m_SamplesModel.m_col_group] = group;
977 rowGroup[m_SamplesModel.m_col_sample] = NULL;
978 for (gig::Sample* sample = group->GetFirstSample();
979 sample; sample = group->GetNextSample()) {
980 Gtk::TreeModel::iterator iterSample =
981 m_refSamplesTreeModel->append(rowGroup.children());
982 Gtk::TreeModel::Row rowSample = *iterSample;
983 rowSample[m_SamplesModel.m_col_name] = sample->pInfo->Name.c_str();
984 rowSample[m_SamplesModel.m_col_sample] = sample;
985 rowSample[m_SamplesModel.m_col_group] = NULL;
986 }
987 }
988 }
989
990 file = gig;
991
992 // select the first instrument
993 Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();
994 tree_sel_ref->select(Gtk::TreePath("0"));
995 }
996
997 void MainWindow::show_instr_props()
998 {
999 Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();
1000 Gtk::TreeModel::iterator it = tree_sel_ref->get_selected();
1001 if (it)
1002 {
1003 Gtk::TreeModel::Row row = *it;
1004 if (row[m_Columns.m_col_instr])
1005 {
1006 instrumentProps.set_instrument(row[m_Columns.m_col_instr]);
1007 instrumentProps.show();
1008 instrumentProps.deiconify();
1009 }
1010 }
1011 }
1012
1013 void MainWindow::on_button_release(GdkEventButton* button)
1014 {
1015 if (button->type == GDK_2BUTTON_PRESS) {
1016 show_instr_props();
1017 } else if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
1018 popup_menu->popup(button->button, button->time);
1019 }
1020 }
1021
1022 void MainWindow::on_instrument_selection_change(int index) {
1023 m_RegionChooser.set_instrument(file->GetInstrument(index));
1024 }
1025
1026 void MainWindow::on_sample_treeview_button_release(GdkEventButton* button) {
1027 if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
1028 Gtk::Menu* sample_popup =
1029 dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/SamplePopupMenu"));
1030 // update enabled/disabled state of sample popup items
1031 Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
1032 Gtk::TreeModel::iterator it = sel->get_selected();
1033 bool group_selected = false;
1034 bool sample_selected = false;
1035 if (it) {
1036 Gtk::TreeModel::Row row = *it;
1037 group_selected = row[m_SamplesModel.m_col_group];
1038 sample_selected = row[m_SamplesModel.m_col_sample];
1039 }
1040 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/SampleProperties"))->
1041 set_sensitive(group_selected || sample_selected);
1042 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddSample"))->
1043 set_sensitive(group_selected || sample_selected);
1044 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddGroup"))->
1045 set_sensitive(file);
1046 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/RemoveSample"))->
1047 set_sensitive(group_selected || sample_selected);
1048 // show sample popup
1049 sample_popup->popup(button->button, button->time);
1050 }
1051 }
1052
1053 void MainWindow::on_action_add_instrument() {
1054 static int __instrument_indexer = 0;
1055 if (!file) return;
1056 gig::Instrument* instrument = file->AddInstrument();
1057 __instrument_indexer++;
1058 instrument->pInfo->Name =
1059 "Unnamed Instrument " + ToString(__instrument_indexer);
1060 // update instrument tree view
1061 Gtk::TreeModel::iterator iterInstr = m_refTreeModel->append();
1062 Gtk::TreeModel::Row rowInstr = *iterInstr;
1063 rowInstr[m_Columns.m_col_name] = instrument->pInfo->Name.c_str();
1064 rowInstr[m_Columns.m_col_instr] = instrument;
1065 file_changed();
1066 }
1067
1068 void MainWindow::on_action_remove_instrument() {
1069 if (!file) return;
1070 Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeView.get_selection();
1071 Gtk::TreeModel::iterator it = sel->get_selected();
1072 if (it) {
1073 Gtk::TreeModel::Row row = *it;
1074 gig::Instrument* instr = row[m_Columns.m_col_instr];
1075 try {
1076 // remove instrument from the gig file
1077 if (instr) file->DeleteInstrument(instr);
1078 // remove respective row from instruments tree view
1079 m_refTreeModel->erase(it);
1080 file_changed();
1081 } catch (RIFF::Exception e) {
1082 Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);
1083 msg.run();
1084 }
1085 }
1086 }
1087
1088 void MainWindow::on_action_sample_properties() {
1089 //TODO: show a dialog where the selected sample's properties can be edited
1090 Gtk::MessageDialog msg(
1091 *this, "Sorry, yet to be implemented!", false, Gtk::MESSAGE_INFO
1092 );
1093 msg.run();
1094 }
1095
1096 void MainWindow::on_action_add_group() {
1097 static int __sample_indexer = 0;
1098 if (!file) return;
1099 gig::Group* group = file->AddGroup();
1100 group->Name = "Unnamed Group";
1101 if (__sample_indexer) group->Name += " " + ToString(__sample_indexer);
1102 __sample_indexer++;
1103 // update sample tree view
1104 Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();
1105 Gtk::TreeModel::Row rowGroup = *iterGroup;
1106 rowGroup[m_SamplesModel.m_col_name] = group->Name.c_str();
1107 rowGroup[m_SamplesModel.m_col_sample] = NULL;
1108 rowGroup[m_SamplesModel.m_col_group] = group;
1109 file_changed();
1110 }
1111
1112 void MainWindow::on_action_add_sample() {
1113 if (!file) return;
1114 // get selected group
1115 Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
1116 Gtk::TreeModel::iterator it = sel->get_selected();
1117 if (!it) return;
1118 Gtk::TreeModel::Row row = *it;
1119 gig::Group* group = row[m_SamplesModel.m_col_group];
1120 if (!group) { // not a group, but a sample is selected (probably)
1121 gig::Sample* sample = row[m_SamplesModel.m_col_sample];
1122 if (!sample) return;
1123 it = row.parent(); // resolve parent (that is the sample's group)
1124 if (!it) return;
1125 row = *it;
1126 group = row[m_SamplesModel.m_col_group];
1127 if (!group) return;
1128 }
1129 // show 'browse for file' dialog
1130 Gtk::FileChooserDialog dialog(*this, _("Add Sample(s)"));
1131 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1132 dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
1133 dialog.set_select_multiple(true);
1134 Gtk::FileFilter soundfilter; // matches all file types supported by libsndfile
1135 const char* const supportedFileTypes[] = {
1136 "*.wav", "*.WAV", "*.aiff", "*.AIFF", "*.aifc", "*.AIFC", "*.snd",
1137 "*.SND", "*.au", "*.AU", "*.paf", "*.PAF", "*.iff", "*.IFF",
1138 "*.svx", "*.SVX", "*.sf", "*.SF", "*.voc", "*.VOC", "*.w64",
1139 "*.W64", "*.pvf", "*.PVF", "*.xi", "*.XI", "*.htk", "*.HTK",
1140 "*.caf", "*.CAF", NULL
1141 };
1142 for (int i = 0; supportedFileTypes[i]; i++)
1143 soundfilter.add_pattern(supportedFileTypes[i]);
1144 soundfilter.set_name("Sound Files");
1145 Gtk::FileFilter allpassfilter; // matches every file
1146 allpassfilter.add_pattern("*.*");
1147 allpassfilter.set_name("All Files");
1148 dialog.add_filter(soundfilter);
1149 dialog.add_filter(allpassfilter);
1150 if (dialog.run() == Gtk::RESPONSE_OK) {
1151 Glib::ustring error_files;
1152 Glib::SListHandle<Glib::ustring> filenames = dialog.get_filenames();
1153 for (Glib::SListHandle<Glib::ustring>::iterator iter = filenames.begin();
1154 iter != filenames.end(); ++iter) {
1155 printf("Adding sample %s\n",(*iter).c_str());
1156 // use libsndfile to retrieve file informations
1157 SF_INFO info;
1158 info.format = 0;
1159 SNDFILE* hFile = sf_open((*iter).c_str(), SFM_READ, &info);
1160 try {
1161 if (!hFile) throw std::string("could not open file");
1162 int bitdepth;
1163 switch (info.format & 0xff) {
1164 case SF_FORMAT_PCM_S8:
1165 case SF_FORMAT_PCM_16:
1166 case SF_FORMAT_PCM_U8:
1167 bitdepth = 16;
1168 break;
1169 case SF_FORMAT_PCM_24:
1170 case SF_FORMAT_PCM_32:
1171 case SF_FORMAT_FLOAT:
1172 case SF_FORMAT_DOUBLE:
1173 bitdepth = 24;
1174 break;
1175 default:
1176 sf_close(hFile); // close sound file
1177 throw std::string("format not supported"); // unsupported subformat (yet?)
1178 }
1179 // add a new sample to the .gig file
1180 gig::Sample* sample = file->AddSample();
1181 // file name without path
1182 Glib::ustring filename = Glib::filename_display_basename(*iter);
1183 // remove file extension if there is one
1184 for (int i = 0; supportedFileTypes[i]; i++) {
1185 if (Glib::str_has_suffix(filename, supportedFileTypes[i] + 1)) {
1186 filename.erase(filename.length() - strlen(supportedFileTypes[i] + 1));
1187 break;
1188 }
1189 }
1190 sample->pInfo->Name = filename;
1191 sample->Channels = info.channels;
1192 sample->BitDepth = bitdepth;
1193 sample->FrameSize = bitdepth / 8/*1 byte are 8 bits*/ * info.channels;
1194 sample->SamplesPerSecond = info.samplerate;
1195 sample->AverageBytesPerSecond = sample->FrameSize * sample->SamplesPerSecond;
1196 sample->BlockAlign = sample->FrameSize;
1197 sample->SamplesTotal = info.frames;
1198
1199 SF_INSTRUMENT instrument;
1200 if (sf_command(hFile, SFC_GET_INSTRUMENT,
1201 &instrument, sizeof(instrument)) != SF_FALSE)
1202 {
1203 sample->MIDIUnityNote = instrument.basenote;
1204
1205 #if HAVE_SF_INSTRUMENT_LOOPS
1206 if (instrument.loop_count && instrument.loops[0].mode != SF_LOOP_NONE) {
1207 sample->Loops = 1;
1208
1209 switch (instrument.loops[0].mode) {
1210 case SF_LOOP_FORWARD:
1211 sample->LoopType = gig::loop_type_normal;
1212 break;
1213 case SF_LOOP_BACKWARD:
1214 sample->LoopType = gig::loop_type_backward;
1215 break;
1216 case SF_LOOP_ALTERNATING:
1217 sample->LoopType = gig::loop_type_bidirectional;
1218 break;
1219 }
1220 sample->LoopStart = instrument.loops[0].start;
1221 sample->LoopEnd = instrument.loops[0].end;
1222 sample->LoopPlayCount = instrument.loops[0].count;
1223 sample->LoopSize = sample->LoopEnd - sample->LoopStart + 1;
1224 }
1225 #endif
1226 }
1227
1228 // schedule resizing the sample (which will be done
1229 // physically when File::Save() is called)
1230 sample->Resize(info.frames);
1231 // make sure sample is part of the selected group
1232 group->AddSample(sample);
1233 // schedule that physical resize and sample import
1234 // (data copying), performed when "Save" is requested
1235 SampleImportItem sched_item;
1236 sched_item.gig_sample = sample;
1237 sched_item.sample_path = *iter;
1238 m_SampleImportQueue.push_back(sched_item);
1239 // add sample to the tree view
1240 Gtk::TreeModel::iterator iterSample =
1241 m_refSamplesTreeModel->append(row.children());
1242 Gtk::TreeModel::Row rowSample = *iterSample;
1243 rowSample[m_SamplesModel.m_col_name] = filename;
1244 rowSample[m_SamplesModel.m_col_sample] = sample;
1245 rowSample[m_SamplesModel.m_col_group] = NULL;
1246 // close sound file
1247 sf_close(hFile);
1248 file_changed();
1249 } catch (std::string what) { // remember the files that made trouble (and their cause)
1250 if (error_files.size()) error_files += "\n";
1251 error_files += *iter += " (" + what + ")";
1252 }
1253 }
1254 // show error message box when some file(s) could not be opened / added
1255 if (error_files.size()) {
1256 Glib::ustring txt = "Could not add the following sample(s):\n" + error_files;
1257 Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
1258 msg.run();
1259 }
1260 }
1261 }
1262
1263 void MainWindow::on_action_remove_sample() {
1264 if (!file) return;
1265 Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
1266 Gtk::TreeModel::iterator it = sel->get_selected();
1267 if (it) {
1268 Gtk::TreeModel::Row row = *it;
1269 gig::Group* group = row[m_SamplesModel.m_col_group];
1270 gig::Sample* sample = row[m_SamplesModel.m_col_sample];
1271 Glib::ustring name = row[m_SamplesModel.m_col_name];
1272 try {
1273 // remove group or sample from the gig file
1274 if (group) {
1275 // temporarily remember the samples that bolong to
1276 // that group (we need that to clean the queue)
1277 std::list<gig::Sample*> members;
1278 for (gig::Sample* pSample = group->GetFirstSample();
1279 pSample; pSample = group->GetNextSample()) {
1280 members.push_back(pSample);
1281 }
1282 // notify everybody that we're going to remove these samples
1283 samples_to_be_removed_signal.emit(members);
1284 // delete the group in the .gig file including the
1285 // samples that belong to the group
1286 file->DeleteGroup(group);
1287 // notify that we're done with removal
1288 samples_removed_signal.emit();
1289 // if sample(s) were just previously added, remove
1290 // them from the import queue
1291 for (std::list<gig::Sample*>::iterator member = members.begin();
1292 member != members.end(); ++member) {
1293 for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
1294 iter != m_SampleImportQueue.end(); ++iter) {
1295 if ((*iter).gig_sample == *member) {
1296 printf("Removing previously added sample '%s' from group '%s'\n",
1297 (*iter).sample_path.c_str(), name.c_str());
1298 m_SampleImportQueue.erase(iter);
1299 break;
1300 }
1301 }
1302 }
1303 file_changed();
1304 } else if (sample) {
1305 // notify everybody that we're going to remove this sample
1306 std::list<gig::Sample*> lsamples;
1307 lsamples.push_back(sample);
1308 samples_to_be_removed_signal.emit(lsamples);
1309 // remove sample from the .gig file
1310 file->DeleteSample(sample);
1311 // notify that we're done with removal
1312 samples_removed_signal.emit();
1313 // if sample was just previously added, remove it from
1314 // the import queue
1315 for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
1316 iter != m_SampleImportQueue.end(); ++iter) {
1317 if ((*iter).gig_sample == sample) {
1318 printf("Removing previously added sample '%s'\n",
1319 (*iter).sample_path.c_str());
1320 m_SampleImportQueue.erase(iter);
1321 break;
1322 }
1323 }
1324 dimreg_changed();
1325 file_changed();
1326 }
1327 // remove respective row(s) from samples tree view
1328 m_refSamplesTreeModel->erase(it);
1329 } catch (RIFF::Exception e) {
1330 // pretend we're done with removal (i.e. to avoid dead locks)
1331 samples_removed_signal.emit();
1332 // show error message
1333 Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);
1334 msg.run();
1335 }
1336 }
1337 }
1338
1339 // For some reason drag_data_get gets called two times for each
1340 // drag'n'drop (at least when target is an Entry). This work-around
1341 // makes sure the code in drag_data_get and drop_drag_data_received is
1342 // only executed once, as drag_begin only gets called once.
1343 void MainWindow::on_sample_treeview_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
1344 {
1345 first_call_to_drag_data_get = true;
1346 }
1347
1348 void MainWindow::on_sample_treeview_drag_data_get(const Glib::RefPtr<Gdk::DragContext>&,
1349 Gtk::SelectionData& selection_data, guint, guint)
1350 {
1351 if (!first_call_to_drag_data_get) return;
1352 first_call_to_drag_data_get = false;
1353
1354 // get selected sample
1355 gig::Sample* sample = NULL;
1356 Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
1357 Gtk::TreeModel::iterator it = sel->get_selected();
1358 if (it) {
1359 Gtk::TreeModel::Row row = *it;
1360 sample = row[m_SamplesModel.m_col_sample];
1361 }
1362 // pass the gig::Sample as pointer
1363 selection_data.set(selection_data.get_target(), 0/*unused*/, (const guchar*)&sample,
1364 sizeof(sample)/*length of data in bytes*/);
1365 }
1366
1367 void MainWindow::on_sample_label_drop_drag_data_received(
1368 const Glib::RefPtr<Gdk::DragContext>& context, int, int,
1369 const Gtk::SelectionData& selection_data, guint, guint time)
1370 {
1371 gig::Sample* sample = *((gig::Sample**) selection_data.get_data());
1372
1373 if (sample && selection_data.get_length() == sizeof(gig::Sample*)) {
1374 std::cout << "Drop received sample \"" <<
1375 sample->pInfo->Name << "\"" << std::endl;
1376 // drop success
1377 context->drop_reply(true, time);
1378
1379 //TODO: we should better move most of the following code to DimRegionEdit::set_sample()
1380
1381 // notify everybody that we're going to alter the region
1382 gig::Region* region = m_RegionChooser.get_region();
1383 region_to_be_changed_signal.emit(region);
1384
1385 // find the samplechannel dimension
1386 gig::dimension_def_t* stereo_dimension = 0;
1387 for (int i = 0 ; i < region->Dimensions ; i++) {
1388 if (region->pDimensionDefinitions[i].dimension ==
1389 gig::dimension_samplechannel) {
1390 stereo_dimension = &region->pDimensionDefinitions[i];
1391 break;
1392 }
1393 }
1394 bool channels_changed = false;
1395 if (sample->Channels == 1 && stereo_dimension) {
1396 // remove the samplechannel dimension
1397 region->DeleteDimension(stereo_dimension);
1398 channels_changed = true;
1399 region_changed();
1400 }
1401 dimreg_edit.set_sample(sample);
1402
1403 if (sample->Channels == 2 && !stereo_dimension) {
1404 // add samplechannel dimension
1405 gig::dimension_def_t dim;
1406 dim.dimension = gig::dimension_samplechannel;
1407 dim.bits = 1;
1408 dim.zones = 2;
1409 region->AddDimension(&dim);
1410 channels_changed = true;
1411 region_changed();
1412 }
1413 if (channels_changed) {
1414 // unmap all samples with wrong number of channels
1415 // TODO: maybe there should be a warning dialog for this
1416 for (int i = 0 ; i < region->DimensionRegions ; i++) {
1417 gig::DimensionRegion* d = region->pDimensionRegions[i];
1418 if (d->pSample && d->pSample->Channels != sample->Channels) {
1419 gig::Sample* oldref = d->pSample;
1420 d->pSample = NULL;
1421 sample_ref_changed_signal.emit(oldref, NULL);
1422 }
1423 }
1424 }
1425
1426 // notify we're done with altering
1427 region_changed_signal.emit(region);
1428
1429 return;
1430 }
1431 // drop failed
1432 context->drop_reply(false, time);
1433 }
1434
1435 void MainWindow::sample_name_changed(const Gtk::TreeModel::Path& path,
1436 const Gtk::TreeModel::iterator& iter) {
1437 if (!iter) return;
1438 Gtk::TreeModel::Row row = *iter;
1439 Glib::ustring name = row[m_SamplesModel.m_col_name];
1440 gig::Group* group = row[m_SamplesModel.m_col_group];
1441 gig::Sample* sample = row[m_SamplesModel.m_col_sample];
1442 if (group) {
1443 if (group->Name != name) {
1444 group->Name = name;
1445 printf("group name changed\n");
1446 file_changed();
1447 }
1448 } else if (sample) {
1449 if (sample->pInfo->Name != name.raw()) {
1450 sample->pInfo->Name = name.raw();
1451 printf("sample name changed\n");
1452 file_changed();
1453 }
1454 }
1455 }
1456
1457 void MainWindow::instrument_name_changed(const Gtk::TreeModel::Path& path,
1458 const Gtk::TreeModel::iterator& iter) {
1459 if (!iter) return;
1460 Gtk::TreeModel::Row row = *iter;
1461 Glib::ustring name = row[m_Columns.m_col_name];
1462 gig::Instrument* instrument = row[m_Columns.m_col_instr];
1463 if (instrument && instrument->pInfo->Name != name.raw()) {
1464 instrument->pInfo->Name = name.raw();
1465 file_changed();
1466 }
1467 }
1468
1469 sigc::signal<void, gig::File*> MainWindow::signal_file_structure_to_be_changed() {
1470 return file_structure_to_be_changed_signal;
1471 }
1472
1473 sigc::signal<void, gig::File*> MainWindow::signal_file_structure_changed() {
1474 return file_structure_changed_signal;
1475 }
1476
1477 sigc::signal<void, std::list<gig::Sample*> > MainWindow::signal_samples_to_be_removed() {
1478 return samples_to_be_removed_signal;
1479 }
1480
1481 sigc::signal<void> MainWindow::signal_samples_removed() {
1482 return samples_removed_signal;
1483 }
1484
1485 sigc::signal<void, gig::Region*> MainWindow::signal_region_to_be_changed() {
1486 return region_to_be_changed_signal;
1487 }
1488
1489 sigc::signal<void, gig::Region*> MainWindow::signal_region_changed() {
1490 return region_changed_signal;
1491 }
1492
1493 sigc::signal<void, gig::Sample*/*old*/, gig::Sample*/*new*/> MainWindow::signal_sample_ref_changed() {
1494 return sample_ref_changed_signal;
1495 }
1496
1497 sigc::signal<void, gig::DimensionRegion*> MainWindow::signal_dimreg_to_be_changed() {
1498 return dimreg_to_be_changed_signal;
1499 }
1500
1501 sigc::signal<void, gig::DimensionRegion*> MainWindow::signal_dimreg_changed() {
1502 return dimreg_changed_signal;
1503 }

  ViewVC Help
Powered by ViewVC