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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1138 - (show annotations) (download)
Sat Mar 31 09:33:40 2007 UTC (17 years ago) by persson
File size: 42351 byte(s)
* reworked instrument properties dialog - properties can now be edited
* code cleanup: removed the pointer to member usage in paramedit as it
  just made things more complicated

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
28 #if GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 6
29 #define ABOUT_DIALOG
30 #include <gtkmm/aboutdialog.h>
31 #endif
32
33 #include <stdio.h>
34 #include <sndfile.h>
35
36 #include "mainwindow.h"
37
38 #define _(String) gettext(String)
39
40 template<class T> inline std::string ToString(T o) {
41 std::stringstream ss;
42 ss << o;
43 return ss.str();
44 }
45
46 MainWindow::MainWindow()
47 {
48 // set_border_width(5);
49 // set_default_size(400, 200);
50
51
52 add(m_VBox);
53
54 // Handle selection
55 Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();
56 tree_sel_ref->signal_changed().connect(
57 sigc::mem_fun(*this, &MainWindow::on_sel_change));
58
59 // m_TreeView.set_reorderable();
60
61 m_TreeView.signal_button_press_event().connect_notify(
62 sigc::mem_fun(*this, &MainWindow::on_button_release));
63
64 // Add the TreeView tab, inside a ScrolledWindow, with the button underneath:
65 m_ScrolledWindow.add(m_TreeView);
66 // m_ScrolledWindow.set_size_request(200, 600);
67 m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
68
69 m_ScrolledWindowSamples.add(m_TreeViewSamples);
70 m_ScrolledWindowSamples.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
71
72
73 m_TreeViewNotebook.set_size_request(300);
74
75 m_HPaned.add1(m_TreeViewNotebook);
76 m_HPaned.add2(dimreg_edit);
77
78
79 m_TreeViewNotebook.append_page(m_ScrolledWindowSamples, "Samples");
80 m_TreeViewNotebook.append_page(m_ScrolledWindow, "Instruments");
81
82
83 actionGroup = Gtk::ActionGroup::create();
84
85 actionGroup->add(Gtk::Action::create("MenuFile", _("_File")));
86 actionGroup->add(Gtk::Action::create("New", Gtk::Stock::NEW),
87 sigc::mem_fun(
88 *this, &MainWindow::on_action_file_new));
89 Glib::RefPtr<Gtk::Action> action =
90 Gtk::Action::create("Open", Gtk::Stock::OPEN);
91 action->property_label() = action->property_label() + "...";
92 actionGroup->add(action,
93 sigc::mem_fun(
94 *this, &MainWindow::on_action_file_open));
95 actionGroup->add(Gtk::Action::create("Save", Gtk::Stock::SAVE),
96 sigc::mem_fun(
97 *this, &MainWindow::on_action_file_save));
98 action = Gtk::Action::create("SaveAs", Gtk::Stock::SAVE_AS);
99 action->property_label() = action->property_label() + "...";
100 actionGroup->add(action,
101 *(new Gtk::AccelKey("<shift><control>s")),
102 sigc::mem_fun(
103 *this, &MainWindow::on_action_file_save_as)
104 );
105 actionGroup->add(Gtk::Action::create("Properties",
106 Gtk::Stock::PROPERTIES),
107 sigc::mem_fun(
108 *this, &MainWindow::on_action_file_properties));
109 actionGroup->add(Gtk::Action::create("InstrProperties",
110 Gtk::Stock::PROPERTIES),
111 sigc::mem_fun(
112 *this, &MainWindow::show_instr_props));
113 actionGroup->add(Gtk::Action::create("Quit", Gtk::Stock::QUIT),
114 sigc::mem_fun(
115 *this, &MainWindow::hide));
116 actionGroup->add(Gtk::Action::create("MenuInstrument", _("_Instrument")));
117
118 action = Gtk::Action::create("MenuHelp", Gtk::Stock::HELP);
119 actionGroup->add(Gtk::Action::create("MenuHelp",
120 action->property_label()));
121 #ifdef ABOUT_DIALOG
122 actionGroup->add(Gtk::Action::create("About", Gtk::Stock::ABOUT),
123 sigc::mem_fun(
124 *this, &MainWindow::on_action_help_about));
125 #endif
126 actionGroup->add(
127 Gtk::Action::create("AddInstrument", _("Add _Instrument")),
128 sigc::mem_fun(*this, &MainWindow::on_action_add_instrument)
129 );
130 actionGroup->add(
131 Gtk::Action::create("RemoveInstrument", Gtk::Stock::REMOVE),
132 sigc::mem_fun(*this, &MainWindow::on_action_remove_instrument)
133 );
134
135 // sample right-click popup actions
136 actionGroup->add(
137 Gtk::Action::create("SampleProperties", Gtk::Stock::PROPERTIES),
138 sigc::mem_fun(*this, &MainWindow::on_action_sample_properties)
139 );
140 actionGroup->add(
141 Gtk::Action::create("AddGroup", _("Add _Group")),
142 sigc::mem_fun(*this, &MainWindow::on_action_add_group)
143 );
144 actionGroup->add(
145 Gtk::Action::create("AddSample", _("Add _Sample(s)")),
146 sigc::mem_fun(*this, &MainWindow::on_action_add_sample)
147 );
148 actionGroup->add(
149 Gtk::Action::create("RemoveSample", Gtk::Stock::REMOVE),
150 sigc::mem_fun(*this, &MainWindow::on_action_remove_sample)
151 );
152
153 uiManager = Gtk::UIManager::create();
154 uiManager->insert_action_group(actionGroup);
155 // add_accel_group(uiManager->get_accel_group());
156
157 Glib::ustring ui_info =
158 "<ui>"
159 " <menubar name='MenuBar'>"
160 " <menu action='MenuFile'>"
161 " <menuitem action='New'/>"
162 " <menuitem action='Open'/>"
163 " <separator/>"
164 " <menuitem action='Save'/>"
165 " <menuitem action='SaveAs'/>"
166 " <separator/>"
167 " <menuitem action='Properties'/>"
168 " <separator/>"
169 " <menuitem action='Quit'/>"
170 " </menu>"
171 " <menu action='MenuInstrument'>"
172 " </menu>"
173 #ifdef ABOUT_DIALOG
174 " <menu action='MenuHelp'>"
175 " <menuitem action='About'/>"
176 " </menu>"
177 #endif
178 " </menubar>"
179 " <popup name='PopupMenu'>"
180 " <menuitem action='InstrProperties'/>"
181 " <menuitem action='AddInstrument'/>"
182 " <separator/>"
183 " <menuitem action='RemoveInstrument'/>"
184 " </popup>"
185 " <popup name='SamplePopupMenu'>"
186 " <menuitem action='SampleProperties'/>"
187 " <menuitem action='AddGroup'/>"
188 " <menuitem action='AddSample'/>"
189 " <separator/>"
190 " <menuitem action='RemoveSample'/>"
191 " </popup>"
192 "</ui>";
193 uiManager->add_ui_from_string(ui_info);
194
195 popup_menu = dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/PopupMenu"));
196
197 Gtk::Widget* menuBar = uiManager->get_widget("/MenuBar");
198 m_VBox.pack_start(*menuBar, Gtk::PACK_SHRINK);
199 m_VBox.pack_start(m_HPaned);
200 m_VBox.pack_start(m_RegionChooser, Gtk::PACK_SHRINK);
201 m_VBox.pack_start(m_DimRegionChooser, Gtk::PACK_SHRINK);
202
203 m_RegionChooser.signal_sel_changed().connect(
204 sigc::mem_fun(*this, &MainWindow::region_changed) );
205 m_DimRegionChooser.signal_sel_changed().connect(
206 sigc::mem_fun(*this, &MainWindow::dimreg_changed) );
207
208
209 // Create the Tree model:
210 m_refTreeModel = Gtk::ListStore::create(m_Columns);
211 m_TreeView.set_model(m_refTreeModel);
212 m_refTreeModel->signal_row_changed().connect(
213 sigc::mem_fun(*this, &MainWindow::instrument_name_changed)
214 );
215
216 // Add the TreeView's view columns:
217 m_TreeView.append_column_editable("Instrument", m_Columns.m_col_name);
218 m_TreeView.set_headers_visible(false);
219
220 // create samples treeview (including its data model)
221 m_refSamplesTreeModel = SamplesTreeStore::create(m_SamplesModel);
222 m_TreeViewSamples.set_model(m_refSamplesTreeModel);
223 // m_TreeViewSamples.set_reorderable();
224 m_TreeViewSamples.append_column_editable("Samples", m_SamplesModel.m_col_name);
225 m_TreeViewSamples.set_headers_visible(false);
226 m_TreeViewSamples.signal_button_press_event().connect_notify(
227 sigc::mem_fun(*this, &MainWindow::on_sample_treeview_button_release)
228 );
229 m_refSamplesTreeModel->signal_row_changed().connect(
230 sigc::mem_fun(*this, &MainWindow::sample_name_changed)
231 );
232
233 // establish drag&drop between samples tree view and dimension region 'Sample' text entry
234 std::list<Gtk::TargetEntry> drag_target_gig_sample;
235 drag_target_gig_sample.push_back( Gtk::TargetEntry("gig::Sample") );
236 m_TreeViewSamples.drag_source_set(drag_target_gig_sample);
237 m_TreeViewSamples.signal_drag_data_get().connect(
238 sigc::mem_fun(*this, &MainWindow::on_sample_treeview_drag_data_get)
239 );
240 dimreg_edit.wSample->drag_dest_set(drag_target_gig_sample);
241 dimreg_edit.wSample->signal_drag_data_received().connect(
242 sigc::mem_fun(*this, &MainWindow::on_sample_label_drop_drag_data_received)
243 );
244
245 file = 0;
246
247 show_all_children();
248 }
249
250 MainWindow::~MainWindow()
251 {
252 }
253
254 void MainWindow::region_changed()
255 {
256 m_DimRegionChooser.set_region(m_RegionChooser.get_region());
257 }
258
259 void MainWindow::dimreg_changed()
260 {
261 dimreg_edit.set_dim_region(m_DimRegionChooser.get_dimregion());
262 }
263
264 void MainWindow::on_sel_change()
265 {
266 Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();
267
268 Gtk::TreeModel::iterator it = tree_sel_ref->get_selected();
269 if (it) {
270 Gtk::TreeModel::Row row = *it;
271 std::cout << row[m_Columns.m_col_name] << std::endl;
272
273 m_RegionChooser.set_instrument(row[m_Columns.m_col_instr]);
274 } else {
275 m_RegionChooser.set_instrument(0);
276 }
277 }
278
279 void loader_progress_callback(gig::progress_t* progress)
280 {
281 Loader* loader = static_cast<Loader*>(progress->custom);
282 loader->progress_callback(progress->factor);
283 }
284
285 void Loader::progress_callback(float fraction)
286 {
287 {
288 Glib::Mutex::Lock lock(progressMutex);
289 progress = fraction;
290 }
291 progress_dispatcher();
292 }
293
294 void Loader::thread_function()
295 {
296 printf("thread_function self=%x\n", Glib::Thread::self());
297 printf("Start %s\n", filename);
298 RIFF::File* riff = new RIFF::File(filename);
299 gig = new gig::File(riff);
300 gig::progress_t progress;
301 progress.callback = loader_progress_callback;
302 progress.custom = this;
303
304 gig->GetInstrument(0, &progress);
305 printf("End\n");
306 finished_dispatcher();
307 }
308
309 Loader::Loader(const char* filename)
310 : thread(0), filename(filename)
311 {
312 }
313
314 void Loader::launch()
315 {
316 thread = Glib::Thread::create(sigc::mem_fun(*this, &Loader::thread_function), true);
317 printf("launch thread=%x\n", thread);
318 }
319
320 float Loader::get_progress()
321 {
322 float res;
323 {
324 Glib::Mutex::Lock lock(progressMutex);
325 res = progress;
326 }
327 return res;
328 }
329
330 Glib::Dispatcher& Loader::signal_progress()
331 {
332 return progress_dispatcher;
333 }
334
335 Glib::Dispatcher& Loader::signal_finished()
336 {
337 return finished_dispatcher;
338 }
339
340 LoadDialog::LoadDialog(const Glib::ustring& title, Gtk::Window& parent)
341 : Gtk::Dialog(title, parent, true)
342 {
343 get_vbox()->pack_start(progressBar);
344 show_all_children();
345 }
346
347 // Clear all GUI elements / controls. This method is typically called
348 // before a new .gig file is to be created or to be loaded.
349 void MainWindow::__clear() {
350 // remove all entries from "Instrument" menu
351 Gtk::MenuItem* instrument_menu =
352 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuInstrument"));
353 instrument_menu->hide();
354 for (int i = 0; i < instrument_menu->get_submenu()->items().size(); i++) {
355 delete &instrument_menu->get_submenu()->items()[i];
356 }
357 instrument_menu->get_submenu()->items().clear();
358 // forget all samples that ought to be imported
359 m_SampleImportQueue.clear();
360 // clear the samples and instruments tree views
361 m_refTreeModel->clear();
362 m_refSamplesTreeModel->clear();
363 // free libgig's gig::File instance
364 if (file) {
365 delete file;
366 file = NULL;
367 }
368 }
369
370 void MainWindow::on_action_file_new()
371 {
372 // clear all GUI elements
373 __clear();
374 // create a new .gig file (virtually yet)
375 gig::File* pFile = new gig::File;
376 // already add one new instrument by default
377 gig::Instrument* pInstrument = pFile->AddInstrument();
378 pInstrument->pInfo->Name = "Unnamed Instrument";
379 // update GUI with that new gig::File
380 load_gig(pFile, NULL /*no file name yet*/);
381 }
382
383 void MainWindow::on_action_file_open()
384 {
385 Gtk::FileChooserDialog dialog(*this, _("Open file"));
386 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
387 dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
388 Gtk::FileFilter filter;
389 filter.add_pattern("*.gig");
390 dialog.set_filter(filter);
391 if (dialog.run() == Gtk::RESPONSE_OK) {
392 printf("filename=%s\n", dialog.get_filename().c_str());
393 __clear();
394 printf("on_action_file_open self=%x\n", Glib::Thread::self());
395 load_file(dialog.get_filename().c_str());
396 }
397 }
398
399 void MainWindow::load_file(const char* name)
400 {
401 load_dialog = new LoadDialog("Loading...", *this);
402 load_dialog->show_all();
403 loader = new Loader(strdup(name));
404 loader->signal_progress().connect(
405 sigc::mem_fun(*this, &MainWindow::on_loader_progress));
406 loader->signal_finished().connect(
407 sigc::mem_fun(*this, &MainWindow::on_loader_finished));
408 loader->launch();
409 }
410
411 void MainWindow::on_loader_progress()
412 {
413 load_dialog->set_fraction(loader->get_progress());
414 }
415
416 void MainWindow::on_loader_finished()
417 {
418 printf("Loader finished!\n");
419 printf("on_loader_finished self=%x\n", Glib::Thread::self());
420 load_gig(loader->gig, loader->filename);
421 load_dialog->hide();
422 }
423
424 void MainWindow::on_action_file_save()
425 {
426 if (!file) return;
427 std::cout << "Saving file\n" << std::flush;
428 try {
429 file->Save();
430 } catch (RIFF::Exception e) {
431 Glib::ustring txt = "Could not save file: " + e.Message;
432 Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
433 msg.run();
434 return;
435 }
436 std::cout << "Saving file done\n" << std::flush;
437 __import_queued_samples();
438 }
439
440 void MainWindow::on_action_file_save_as()
441 {
442 if (!file) return;
443 Gtk::FileChooserDialog dialog(*this, "Open", Gtk::FILE_CHOOSER_ACTION_SAVE);
444 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
445 dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);
446 Gtk::FileFilter filter;
447 filter.add_pattern("*.gig");
448 dialog.set_filter(filter);
449 if (dialog.run() == Gtk::RESPONSE_OK) {
450 printf("filename=%s\n", dialog.get_filename().c_str());
451 try {
452 file->Save(dialog.get_filename());
453 } catch (RIFF::Exception e) {
454 Glib::ustring txt = "Could not save file: " + e.Message;
455 Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
456 msg.run();
457 return;
458 }
459 __import_queued_samples();
460 }
461 }
462
463 // actually write the sample(s)' data to the gig file
464 void MainWindow::__import_queued_samples() {
465 std::cout << "Starting sample import\n" << std::flush;
466 Glib::ustring error_files;
467 printf("Samples to import: %d\n", m_SampleImportQueue.size());
468 for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
469 iter != m_SampleImportQueue.end(); ) {
470 printf("Importing sample %s\n",(*iter).sample_path.c_str());
471 SF_INFO info;
472 info.format = 0;
473 SNDFILE* hFile = sf_open((*iter).sample_path.c_str(), SFM_READ, &info);
474 try {
475 if (!hFile) throw std::string("could not open file");
476 // determine sample's bit depth
477 int bitdepth;
478 switch (info.format & 0xff) {
479 case SF_FORMAT_PCM_S8:
480 bitdepth = 16; // we simply convert to 16 bit for now
481 break;
482 case SF_FORMAT_PCM_16:
483 bitdepth = 16;
484 break;
485 case SF_FORMAT_PCM_24:
486 bitdepth = 32; // we simply convert to 32 bit for now
487 break;
488 case SF_FORMAT_PCM_32:
489 bitdepth = 32;
490 break;
491 case SF_FORMAT_PCM_U8:
492 bitdepth = 16; // we simply convert to 16 bit for now
493 break;
494 case SF_FORMAT_FLOAT:
495 bitdepth = 32;
496 break;
497 case SF_FORMAT_DOUBLE:
498 bitdepth = 32; // I guess we will always truncate this to 32 bit
499 break;
500 default:
501 sf_close(hFile); // close sound file
502 throw std::string("format not supported"); // unsupported subformat (yet?)
503 }
504 // allocate appropriate copy buffer (TODO: for now we copy
505 // it in one piece, might be tough for very long samples)
506 // and copy sample data into buffer
507 int8_t* buffer = NULL;
508 switch (bitdepth) {
509 case 16:
510 buffer = new int8_t[2 * info.channels * info.frames];
511 // libsndfile does the conversion for us (if needed)
512 sf_readf_short(hFile, (short*) buffer, info.frames);
513 break;
514 case 32:
515 buffer = new int8_t[4 * info.channels * info.frames];
516 // libsndfile does the conversion for us (if needed)
517 sf_readf_int(hFile, (int*) buffer, info.frames);
518 break;
519 }
520 // write from buffer directly (physically) into .gig file
521 (*iter).gig_sample->Write(buffer, info.frames);
522 // cleanup
523 sf_close(hFile);
524 delete[] buffer;
525 // on success we remove the sample from the import queue,
526 // otherwise keep it, maybe it works the next time ?
527 std::list<SampleImportItem>::iterator cur = iter;
528 ++iter;
529 m_SampleImportQueue.erase(cur);
530 } catch (std::string what) {
531 // remember the files that made trouble (and their cause)
532 if (error_files.size()) error_files += "\n";
533 error_files += (*iter).sample_path += " (" + what + ")";
534 ++iter;
535 }
536 }
537 // show error message box when some sample(s) could not be imported
538 if (error_files.size()) {
539 Glib::ustring txt = "Could not import the following sample(s):\n" + error_files;
540 Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
541 msg.run();
542 }
543 }
544
545 void MainWindow::on_action_file_properties()
546 {
547 propDialog.show();
548 propDialog.deiconify();
549 }
550
551 void MainWindow::on_action_help_about()
552 {
553 #ifdef ABOUT_DIALOG
554 Gtk::AboutDialog dialog;
555 dialog.set_version(VERSION);
556 dialog.run();
557 #endif
558 }
559
560 PropDialog::PropDialog()
561 : table(2,1)
562 {
563 table.set_col_spacings(5);
564 char* propLabels[] = {
565 "Name:",
566 "CreationDate:",
567 "Comments:", // TODO: multiline
568 "Product:",
569 "Copyright:",
570 "Artists:",
571 "Genre:",
572 "Keywords:",
573 "Engineer:",
574 "Technician:",
575 "Software:", // TODO: readonly
576 "Medium:",
577 "Source:",
578 "SourceForm:",
579 "Commissioned:",
580 "Subject:"
581 };
582 for (int i = 0 ; i < sizeof(propLabels) / sizeof(char*) ; i++) {
583 label[i].set_text(propLabels[i]);
584 label[i].set_alignment(Gtk::ALIGN_LEFT);
585 table.attach(label[i], 0, 1, i, i + 1, Gtk::FILL, Gtk::SHRINK);
586 table.attach(entry[i], 1, 2, i, i + 1, Gtk::FILL | Gtk::EXPAND,
587 Gtk::SHRINK);
588 }
589
590 add(table);
591 // add_button(Gtk::Stock::CANCEL, 0);
592 // add_button(Gtk::Stock::OK, 1);
593 show_all_children();
594 }
595
596 void PropDialog::set_info(DLS::Info* info)
597 {
598 entry[0].set_text(info->Name);
599 entry[1].set_text(info->CreationDate);
600 entry[2].set_text(Glib::convert(info->Comments, "UTF-8", "ISO-8859-1"));
601 entry[3].set_text(info->Product);
602 entry[4].set_text(info->Copyright);
603 entry[5].set_text(info->Artists);
604 entry[6].set_text(info->Genre);
605 entry[7].set_text(info->Keywords);
606 entry[8].set_text(info->Engineer);
607 entry[9].set_text(info->Technician);
608 entry[10].set_text(info->Software);
609 entry[11].set_text(info->Medium);
610 entry[12].set_text(info->Source);
611 entry[13].set_text(info->SourceForm);
612 entry[14].set_text(info->Commissioned);
613 entry[15].set_text(info->Subject);
614 }
615
616 void InstrumentProps::add_prop(LabelWidget& prop)
617 {
618 table.attach(prop.label, 0, 1, rowno, rowno + 1,
619 Gtk::FILL, Gtk::SHRINK);
620 table.attach(prop.widget, 1, 2, rowno, rowno + 1,
621 Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
622 rowno++;
623 }
624
625 InstrumentProps::InstrumentProps()
626 : table(2,1),
627 quitButton(Gtk::Stock::CLOSE),
628 eName("Name"),
629 eIsDrum("IsDrum"),
630 eMIDIBank("MIDIBank", 0, 16383),
631 eMIDIProgram("MIDIProgram"),
632 eAttenuation("Attenuation", 0, 96, 0, 1),
633 eGainPlus6("Gain +6dB", eAttenuation, -6),
634 eEffectSend("EffectSend", 0, 65535),
635 eFineTune("FineTune", -8400, 8400),
636 ePitchbendRange("PitchbendRange", 0, 12),
637 ePianoReleaseMode("PianoReleaseMode"),
638 eDimensionKeyRangeLow("DimensionKeyRangeLow"),
639 eDimensionKeyRangeHigh("DimensionKeyRangeHigh")
640 {
641 set_title("Instrument properties");
642
643 rowno = 0;
644 table.set_col_spacings(5);
645
646 add_prop(eName);
647 add_prop(eIsDrum);
648 add_prop(eMIDIBank);
649 add_prop(eMIDIProgram);
650 add_prop(eAttenuation);
651 add_prop(eGainPlus6);
652 add_prop(eEffectSend);
653 add_prop(eFineTune);
654 add_prop(ePitchbendRange);
655 add_prop(ePianoReleaseMode);
656 add_prop(eDimensionKeyRangeLow);
657 add_prop(eDimensionKeyRangeHigh);
658
659 eDimensionKeyRangeLow.signal_value_changed().connect(
660 sigc::mem_fun(*this, &InstrumentProps::key_range_low_changed));
661 eDimensionKeyRangeHigh.signal_value_changed().connect(
662 sigc::mem_fun(*this, &InstrumentProps::key_range_high_changed));
663
664 add(vbox);
665 table.set_border_width(5);
666 vbox.pack_start(table);
667 table.show();
668 vbox.pack_start(buttonBox, Gtk::PACK_SHRINK);
669 buttonBox.set_layout(Gtk::BUTTONBOX_END);
670 buttonBox.set_border_width(5);
671 buttonBox.show();
672 buttonBox.pack_start(quitButton);
673 quitButton.set_flags(Gtk::CAN_DEFAULT);
674 quitButton.grab_focus();
675
676 quitButton.signal_clicked().connect(
677 sigc::mem_fun(*this, &InstrumentProps::hide));
678
679 quitButton.show();
680 vbox.show();
681 show_all_children();
682 }
683
684 void InstrumentProps::set_instrument(gig::Instrument* instrument)
685 {
686 update_gui = false;
687 eName.set_ptr(&instrument->pInfo->Name);
688 eIsDrum.set_ptr(&instrument->IsDrum);
689 eMIDIBank.set_ptr(&instrument->MIDIBank);
690 eMIDIProgram.set_ptr(&instrument->MIDIProgram);
691 eAttenuation.set_ptr(&instrument->Attenuation);
692 eGainPlus6.set_ptr(&instrument->Attenuation);
693 eEffectSend.set_ptr(&instrument->EffectSend);
694 eFineTune.set_ptr(&instrument->FineTune);
695 ePitchbendRange.set_ptr(&instrument->PitchbendRange);
696 ePianoReleaseMode.set_ptr(&instrument->PianoReleaseMode);
697 eDimensionKeyRangeLow.set_ptr(&instrument->DimensionKeyRange.low);
698 eDimensionKeyRangeHigh.set_ptr(&instrument->DimensionKeyRange.high);
699 update_gui = true;
700 }
701
702 void InstrumentProps::key_range_low_changed()
703 {
704 double l = eDimensionKeyRangeLow.get_value();
705 double h = eDimensionKeyRangeHigh.get_value();
706 if (h < l) eDimensionKeyRangeHigh.set_value(l);
707 }
708
709 void InstrumentProps::key_range_high_changed()
710 {
711 double l = eDimensionKeyRangeLow.get_value();
712 double h = eDimensionKeyRangeHigh.get_value();
713 if (h < l) eDimensionKeyRangeLow.set_value(h);
714 }
715
716 void MainWindow::load_gig(gig::File* gig, const char* filename)
717 {
718 file = gig;
719
720 if (filename) {
721 const char *basename = strrchr(filename, '/');
722 basename = basename ? basename + 1 : filename;
723 set_title(basename);
724 } else {
725 set_title("unnamed");
726 }
727
728 propDialog.set_info(gig->pInfo);
729
730 Gtk::MenuItem* instrument_menu =
731 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuInstrument"));
732
733 int instrument_index = 0;
734 Gtk::RadioMenuItem::Group instrument_group;
735 for (gig::Instrument* instrument = gig->GetFirstInstrument() ; instrument ;
736 instrument = gig->GetNextInstrument()) {
737 Gtk::TreeModel::iterator iter = m_refTreeModel->append();
738 Gtk::TreeModel::Row row = *iter;
739 row[m_Columns.m_col_name] = instrument->pInfo->Name.c_str();
740 row[m_Columns.m_col_instr] = instrument;
741 // create a menu item for this instrument
742 Gtk::RadioMenuItem* item =
743 new Gtk::RadioMenuItem(instrument_group, instrument->pInfo->Name.c_str());
744 instrument_menu->get_submenu()->append(*item);
745 item->signal_activate().connect(
746 sigc::bind(
747 sigc::mem_fun(*this, &MainWindow::on_instrument_selection_change),
748 instrument_index
749 )
750 );
751 instrument_index++;
752 }
753 instrument_menu->show();
754 instrument_menu->get_submenu()->show_all_children();
755
756 for (gig::Group* group = gig->GetFirstGroup(); group; group = gig->GetNextGroup()) {
757 if (group->Name != "") {
758 Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();
759 Gtk::TreeModel::Row rowGroup = *iterGroup;
760 rowGroup[m_SamplesModel.m_col_name] = group->Name.c_str();
761 rowGroup[m_SamplesModel.m_col_group] = group;
762 rowGroup[m_SamplesModel.m_col_sample] = NULL;
763 for (gig::Sample* sample = group->GetFirstSample();
764 sample; sample = group->GetNextSample()) {
765 Gtk::TreeModel::iterator iterSample =
766 m_refSamplesTreeModel->append(rowGroup.children());
767 Gtk::TreeModel::Row rowSample = *iterSample;
768 rowSample[m_SamplesModel.m_col_name] = sample->pInfo->Name.c_str();
769 rowSample[m_SamplesModel.m_col_sample] = sample;
770 rowSample[m_SamplesModel.m_col_group] = NULL;
771 }
772 }
773 }
774
775 // select the first instrument
776 Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();
777 tree_sel_ref->select(Gtk::TreePath("0"));
778 }
779
780 void MainWindow::show_instr_props()
781 {
782 Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();
783 Gtk::TreeModel::iterator it = tree_sel_ref->get_selected();
784 if (it)
785 {
786 Gtk::TreeModel::Row row = *it;
787 if (row[m_Columns.m_col_instr])
788 {
789 instrumentProps.set_instrument(row[m_Columns.m_col_instr]);
790 instrumentProps.show();
791 instrumentProps.deiconify();
792 }
793 }
794 }
795
796 void MainWindow::on_button_release(GdkEventButton* button)
797 {
798 if (button->type == GDK_2BUTTON_PRESS) {
799 show_instr_props();
800 } else if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
801 popup_menu->popup(button->button, button->time);
802 }
803 }
804
805 void MainWindow::on_instrument_selection_change(int index) {
806 m_RegionChooser.set_instrument(file->GetInstrument(index));
807 }
808
809 void MainWindow::on_sample_treeview_button_release(GdkEventButton* button) {
810 if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
811 Gtk::Menu* sample_popup =
812 dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/SamplePopupMenu"));
813 // update enabled/disabled state of sample popup items
814 Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
815 Gtk::TreeModel::iterator it = sel->get_selected();
816 bool group_selected = false;
817 bool sample_selected = false;
818 if (it) {
819 Gtk::TreeModel::Row row = *it;
820 group_selected = row[m_SamplesModel.m_col_group];
821 sample_selected = row[m_SamplesModel.m_col_sample];
822 }
823 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/SampleProperties"))->
824 set_sensitive(group_selected || sample_selected);
825 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddSample"))->
826 set_sensitive(group_selected || sample_selected);
827 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddGroup"))->
828 set_sensitive(file);
829 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/RemoveSample"))->
830 set_sensitive(group_selected || sample_selected);
831 // show sample popup
832 sample_popup->popup(button->button, button->time);
833 }
834 }
835
836 void MainWindow::on_action_add_instrument() {
837 static int __instrument_indexer = 0;
838 if (!file) return;
839 gig::Instrument* instrument = file->AddInstrument();
840 __instrument_indexer++;
841 instrument->pInfo->Name =
842 "Unnamed Instrument " + ToString(__instrument_indexer);
843 // update instrument tree view
844 Gtk::TreeModel::iterator iterInstr = m_refTreeModel->append();
845 Gtk::TreeModel::Row rowInstr = *iterInstr;
846 rowInstr[m_Columns.m_col_name] = instrument->pInfo->Name.c_str();
847 rowInstr[m_Columns.m_col_instr] = instrument;
848 }
849
850 void MainWindow::on_action_remove_instrument() {
851 if (!file) return;
852 Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeView.get_selection();
853 Gtk::TreeModel::iterator it = sel->get_selected();
854 if (it) {
855 Gtk::TreeModel::Row row = *it;
856 gig::Instrument* instr = row[m_Columns.m_col_instr];
857 try {
858 // remove instrument from the gig file
859 if (instr) file->DeleteInstrument(instr);
860 // remove respective row from instruments tree view
861 m_refTreeModel->erase(it);
862 } catch (RIFF::Exception e) {
863 Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);
864 msg.run();
865 }
866 }
867 }
868
869 void MainWindow::on_action_sample_properties() {
870 //TODO: show a dialog where the selected sample's properties can be edited
871 Gtk::MessageDialog msg(
872 *this, "Sorry, yet to be implemented!", false, Gtk::MESSAGE_INFO
873 );
874 msg.run();
875 }
876
877 void MainWindow::on_action_add_group() {
878 static int __sample_indexer = 0;
879 if (!file) return;
880 gig::Group* group = file->AddGroup();
881 group->Name = "Unnamed Group";
882 if (__sample_indexer) group->Name += " " + ToString(__sample_indexer);
883 __sample_indexer++;
884 // update sample tree view
885 Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();
886 Gtk::TreeModel::Row rowGroup = *iterGroup;
887 rowGroup[m_SamplesModel.m_col_name] = group->Name.c_str();
888 rowGroup[m_SamplesModel.m_col_sample] = NULL;
889 rowGroup[m_SamplesModel.m_col_group] = group;
890 }
891
892 void MainWindow::on_action_add_sample() {
893 if (!file) return;
894 // get selected group
895 Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
896 Gtk::TreeModel::iterator it = sel->get_selected();
897 if (!it) return;
898 Gtk::TreeModel::Row row = *it;
899 gig::Group* group = row[m_SamplesModel.m_col_group];
900 if (!group) { // not a group, but a sample is selected (probably)
901 gig::Sample* sample = row[m_SamplesModel.m_col_sample];
902 if (!sample) return;
903 it = row.parent(); // resolve parent (that is the sample's group)
904 if (!it) return;
905 row = *it;
906 group = row[m_SamplesModel.m_col_group];
907 if (!group) return;
908 }
909 // show 'browse for file' dialog
910 Gtk::FileChooserDialog dialog(*this, _("Add Sample(s)"));
911 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
912 dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
913 dialog.set_select_multiple(true);
914 Gtk::FileFilter soundfilter; // matches all file types supported by libsndfile
915 const char* supportedFileTypes[] = {
916 "*.wav", "*.WAV", "*.aiff", "*.AIFF", "*.aifc", "*.AIFC", "*.snd",
917 "*.SND", "*.au", "*.AU", "*.paf", "*.PAF", "*.iff", "*.IFF",
918 "*.svx", "*.SVX", "*.sf", "*.SF", "*.voc", "*.VOC", "*.w64",
919 "*.W64", "*.pvf", "*.PVF", "*.xi", "*.XI", "*.htk", "*.HTK",
920 "*.caf", "*.CAF", NULL
921 };
922 for (int i = 0; supportedFileTypes[i]; i++)
923 soundfilter.add_pattern(supportedFileTypes[i]);
924 soundfilter.set_name("Sound Files");
925 Gtk::FileFilter allpassfilter; // matches every file
926 allpassfilter.add_pattern("*.*");
927 allpassfilter.set_name("All Files");
928 dialog.add_filter(soundfilter);
929 dialog.add_filter(allpassfilter);
930 if (dialog.run() == Gtk::RESPONSE_OK) {
931 Glib::ustring error_files;
932 Glib::SListHandle<Glib::ustring> filenames = dialog.get_filenames();
933 for (Glib::SListHandle<Glib::ustring>::iterator iter = filenames.begin();
934 iter != filenames.end(); ++iter) {
935 printf("Adding sample %s\n",(*iter).c_str());
936 // use libsndfile to retrieve file informations
937 SF_INFO info;
938 info.format = 0;
939 SNDFILE* hFile = sf_open((*iter).c_str(), SFM_READ, &info);
940 try {
941 if (!hFile) throw std::string("could not open file");
942 int bitdepth;
943 switch (info.format & 0xff) {
944 case SF_FORMAT_PCM_S8:
945 bitdepth = 16; // we simply convert to 16 bit for now
946 break;
947 case SF_FORMAT_PCM_16:
948 bitdepth = 16;
949 break;
950 case SF_FORMAT_PCM_24:
951 bitdepth = 32; // we simply convert to 32 bit for now
952 break;
953 case SF_FORMAT_PCM_32:
954 bitdepth = 32;
955 break;
956 case SF_FORMAT_PCM_U8:
957 bitdepth = 16; // we simply convert to 16 bit for now
958 break;
959 case SF_FORMAT_FLOAT:
960 bitdepth = 32;
961 break;
962 case SF_FORMAT_DOUBLE:
963 bitdepth = 32; // I guess we will always truncate this to 32 bit
964 break;
965 default:
966 sf_close(hFile); // close sound file
967 throw std::string("format not supported"); // unsupported subformat (yet?)
968 }
969 // add a new sample to the .gig file
970 gig::Sample* sample = file->AddSample();
971 // file name without path
972 sample->pInfo->Name = (*iter).substr((*iter).rfind('/') + 1).raw();
973 sample->Channels = info.channels;
974 sample->BitDepth = bitdepth;
975 sample->FrameSize = bitdepth / 8/*1 byte are 8 bits*/ * info.channels;
976 sample->SamplesPerSecond = info.samplerate;
977 // schedule resizing the sample (which will be done
978 // physically when File::Save() is called)
979 sample->Resize(info.frames);
980 // make sure sample is part of the selected group
981 group->AddSample(sample);
982 // schedule that physical resize and sample import
983 // (data copying), performed when "Save" is requested
984 SampleImportItem sched_item;
985 sched_item.gig_sample = sample;
986 sched_item.sample_path = *iter;
987 m_SampleImportQueue.push_back(sched_item);
988 // add sample to the tree view
989 Gtk::TreeModel::iterator iterSample =
990 m_refSamplesTreeModel->append(row.children());
991 Gtk::TreeModel::Row rowSample = *iterSample;
992 rowSample[m_SamplesModel.m_col_name] = sample->pInfo->Name.c_str();
993 rowSample[m_SamplesModel.m_col_sample] = sample;
994 rowSample[m_SamplesModel.m_col_group] = NULL;
995 // close sound file
996 sf_close(hFile);
997 } catch (std::string what) { // remember the files that made trouble (and their cause)
998 if (error_files.size()) error_files += "\n";
999 error_files += *iter += " (" + what + ")";
1000 }
1001 }
1002 // show error message box when some file(s) could not be opened / added
1003 if (error_files.size()) {
1004 Glib::ustring txt = "Could not add the following sample(s):\n" + error_files;
1005 Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
1006 msg.run();
1007 }
1008 }
1009 }
1010
1011 void MainWindow::on_action_remove_sample() {
1012 if (!file) return;
1013 Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
1014 Gtk::TreeModel::iterator it = sel->get_selected();
1015 if (it) {
1016 Gtk::TreeModel::Row row = *it;
1017 gig::Group* group = row[m_SamplesModel.m_col_group];
1018 gig::Sample* sample = row[m_SamplesModel.m_col_sample];
1019 Glib::ustring name = row[m_SamplesModel.m_col_name];
1020 try {
1021 // remove group or sample from the gig file
1022 if (group) {
1023 // temporarily remember the samples that bolong to
1024 // that group (we need that to clean the queue)
1025 std::list<gig::Sample*> members;
1026 for (gig::Sample* pSample = group->GetFirstSample();
1027 pSample; pSample = group->GetNextSample()) {
1028 members.push_back(pSample);
1029 }
1030 // delete the group in the .gig file including the
1031 // samples that belong to the group
1032 file->DeleteGroup(group);
1033 // if sample(s) were just previously added, remove
1034 // them from the import queue
1035 for (std::list<gig::Sample*>::iterator member = members.begin();
1036 member != members.end(); ++member) {
1037 for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
1038 iter != m_SampleImportQueue.end(); ++iter) {
1039 if ((*iter).gig_sample == *member) {
1040 printf("Removing previously added sample '%s' from group '%s'\n",
1041 (*iter).sample_path.c_str(), name.c_str());
1042 m_SampleImportQueue.erase(iter);
1043 break;
1044 }
1045 }
1046 }
1047 } else if (sample) {
1048 // remove sample from the .gig file
1049 file->DeleteSample(sample);
1050 // if sample was just previously added, remove it from
1051 // the import queue
1052 for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
1053 iter != m_SampleImportQueue.end(); ++iter) {
1054 if ((*iter).gig_sample == sample) {
1055 printf("Removing previously added sample '%s'\n",
1056 (*iter).sample_path.c_str());
1057 m_SampleImportQueue.erase(iter);
1058 break;
1059 }
1060 }
1061 }
1062 // remove respective row(s) from samples tree view
1063 m_refSamplesTreeModel->erase(it);
1064 } catch (RIFF::Exception e) {
1065 Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);
1066 msg.run();
1067 }
1068 }
1069 }
1070
1071 void MainWindow::on_sample_treeview_drag_data_get(const Glib::RefPtr<Gdk::DragContext>&,
1072 Gtk::SelectionData& selection_data, guint, guint)
1073 {
1074 // get selected sample
1075 gig::Sample* sample = NULL;
1076 Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
1077 Gtk::TreeModel::iterator it = sel->get_selected();
1078 if (it) {
1079 Gtk::TreeModel::Row row = *it;
1080 sample = row[m_SamplesModel.m_col_sample];
1081 }
1082 // pass the gig::Sample as pointer
1083 selection_data.set(selection_data.get_target(), 0/*unused*/, (const guchar*)&sample,
1084 sizeof(sample)/*length of data in bytes*/);
1085 }
1086
1087 void MainWindow::on_sample_label_drop_drag_data_received(
1088 const Glib::RefPtr<Gdk::DragContext>& context, int, int,
1089 const Gtk::SelectionData& selection_data, guint, guint time)
1090 {
1091 gig::DimensionRegion* dimregion = m_DimRegionChooser.get_dimregion();
1092 gig::Sample* sample = *((gig::Sample**) selection_data.get_data());
1093
1094 if (sample && dimregion && selection_data.get_length() == sizeof(gig::Sample*)) {
1095 if (sample != dimregion->pSample) {
1096 dimregion->pSample = sample;
1097 dimreg_edit.wSample->set_text(dimregion->pSample->pInfo->Name.c_str());
1098 std::cout << "Drop received sample \"" <<
1099 dimregion->pSample->pInfo->Name.c_str() << "\"" << std::endl;
1100 // drop success
1101 context->drop_reply(true, time);
1102 return;
1103 }
1104 }
1105 // drop failed
1106 context->drop_reply(false, time);
1107 }
1108
1109 void MainWindow::sample_name_changed(const Gtk::TreeModel::Path& path,
1110 const Gtk::TreeModel::iterator& iter) {
1111 if (!iter) return;
1112 Gtk::TreeModel::Row row = *iter;
1113 Glib::ustring name = row[m_SamplesModel.m_col_name];
1114 gig::Group* group = row[m_SamplesModel.m_col_group];
1115 gig::Sample* sample = row[m_SamplesModel.m_col_sample];
1116 if (group) {
1117 group->Name = name;
1118 } else if (sample) {
1119 sample->pInfo->Name = name.raw();
1120 }
1121 }
1122
1123 void MainWindow::instrument_name_changed(const Gtk::TreeModel::Path& path,
1124 const Gtk::TreeModel::iterator& iter) {
1125 if (!iter) return;
1126 Gtk::TreeModel::Row row = *iter;
1127 Glib::ustring name = row[m_Columns.m_col_name];
1128 gig::Instrument* instrument = row[m_Columns.m_col_instr];
1129 if (instrument) instrument->pInfo->Name = name.raw();
1130 }

  ViewVC Help
Powered by ViewVC