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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1117 - (show annotations) (download)
Sat Mar 24 13:05:58 2007 UTC (13 years, 2 months ago) by persson
File size: 43115 byte(s)
* added +6dB parameter to DimRegionEdit
* preparations for improved instrument properties dialog

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 namespace {
617 uint16_t& access_MIDIBank(gig::Instrument* instr)
618 {
619 // TODO: update MIDIBankCoarse/Fine too?
620 return instr->MIDIBank;
621 }
622 uint32_t& access_MIDIProgram(gig::Instrument* instr)
623 {
624 return instr->MIDIProgram;
625 }
626 }
627
628 InstrumentProps::InstrumentProps()
629 : table(2,1),
630 quitButton(Gtk::Stock::CLOSE),
631 eMIDIBank("MIDIBank", &access_MIDIBank, 0, 16383),
632 eMIDIProgram("MIDIProgram", &access_MIDIProgram),
633 eAttenuation("Attenuation", &gig::Instrument::Attenuation),
634 eEffectSend("EffectSend", &gig::Instrument::EffectSend, 0, 65536),
635 eFineTune("FineTune", &gig::Instrument::FineTune, -8400, 8400),
636 ePianoReleaseMode("PianoReleaseMode", &gig::Instrument::PianoReleaseMode)
637 {
638 table.set_col_spacings(5);
639 char* propLabels[] = {
640 "Name:",
641 "IsDrum:",
642 "MIDIBank:",
643 "MIDIProgram:",
644 "Attenuation:",
645 "EffectSend:",
646 "FineTune:",
647 "PitchbendRange:",
648 "PianoReleaseMode:",
649 "DimensionKeyRange:",
650 };
651 int entryIdx = 0, checkIdx = 0;
652 for (int i = 0 ; i < sizeof(propLabels) / sizeof(char*) ; i++) {
653 #if 0
654 if (i == 3) {
655 table.attach(eMIDIProgram.label, 0, 1, i, i + 1,
656 Gtk::FILL, Gtk::SHRINK);
657 table.attach(eMIDIProgram.widget, 1, 2, i, i + 1,
658 Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
659 continue;
660 }
661 #endif
662 label[i].set_text(propLabels[i]);
663 label[i].set_alignment(Gtk::ALIGN_LEFT);
664 table.attach(label[i], 0, 1, i, i + 1, Gtk::FILL, Gtk::SHRINK);
665 if (i == 1 || i == 8)
666 table.attach(check[checkIdx++], 1, 2, i, i + 1,
667 Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
668 else
669 table.attach(entry[entryIdx++], 1, 2, i, i + 1,
670 Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
671 }
672
673 // vbox { table buttonBox { quitButton } }
674
675 //get_vbox()->pack_start(table);
676 // set_border_width(6);
677 add(vbox);
678 table.set_border_width(2);
679 vbox.pack_start(table);
680 table.show();
681 vbox.pack_start(buttonBox);
682 buttonBox.set_layout(Gtk::BUTTONBOX_END);
683 buttonBox.set_border_width(5);
684 buttonBox.show();
685 buttonBox.pack_start(quitButton);
686 quitButton.set_flags(Gtk::CAN_DEFAULT);
687 quitButton.grab_focus();
688
689 quitButton.signal_clicked().connect(
690 sigc::mem_fun(*this, &InstrumentProps::hide));
691
692 // quitButton.grab_default();
693 quitButton.show();
694 // add(table);
695 vbox.show();
696 show_all_children();
697 }
698
699 extern char* notes[];
700
701 void InstrumentProps::set_instrument(gig::Instrument* instrument)
702 {
703 char buf[100];
704
705 int entryIdx = 0, checkIdx = 0;
706 entry[entryIdx++].set_text(instrument->pInfo->Name);
707 check[checkIdx++].set_active(instrument->IsDrum);
708 sprintf(buf, "%d", instrument->MIDIBank);
709 entry[entryIdx++].set_text(buf);
710 sprintf(buf, "%d", instrument->MIDIProgram);
711 entry[entryIdx++].set_text(buf);
712 sprintf(buf, "%d", instrument->Attenuation);
713 entry[entryIdx++].set_text(buf);
714 sprintf(buf, "%d", instrument->EffectSend);
715 entry[entryIdx++].set_text(buf);
716 sprintf(buf, "%d", instrument->FineTune);
717 entry[entryIdx++].set_text(buf);
718 sprintf(buf, "%d", instrument->PitchbendRange);
719 entry[entryIdx++].set_text(buf);
720 check[checkIdx++].set_active(instrument->PianoReleaseMode);
721 sprintf(buf, "%s%d (%d)..%s%d (%d)",
722 notes[instrument->DimensionKeyRange.low % 12],
723 instrument->DimensionKeyRange.low / 12 - 1,
724 instrument->DimensionKeyRange.low,
725 notes[instrument->DimensionKeyRange.high % 12],
726 instrument->DimensionKeyRange.high / 12 - 1,
727 instrument->DimensionKeyRange.high);
728 entry[entryIdx].set_text(buf);
729 }
730
731 void MainWindow::load_gig(gig::File* gig, const char* filename)
732 {
733 file = gig;
734
735 if (filename) {
736 const char *basename = strrchr(filename, '/');
737 basename = basename ? basename + 1 : filename;
738 set_title(basename);
739 } else {
740 set_title("unnamed");
741 }
742
743 propDialog.set_info(gig->pInfo);
744
745 Gtk::MenuItem* instrument_menu =
746 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuInstrument"));
747
748 int instrument_index = 0;
749 Gtk::RadioMenuItem::Group instrument_group;
750 for (gig::Instrument* instrument = gig->GetFirstInstrument() ; instrument ;
751 instrument = gig->GetNextInstrument()) {
752 Gtk::TreeModel::iterator iter = m_refTreeModel->append();
753 Gtk::TreeModel::Row row = *iter;
754 row[m_Columns.m_col_name] = instrument->pInfo->Name.c_str();
755 row[m_Columns.m_col_instr] = instrument;
756 // create a menu item for this instrument
757 Gtk::RadioMenuItem* item =
758 new Gtk::RadioMenuItem(instrument_group, instrument->pInfo->Name.c_str());
759 instrument_menu->get_submenu()->append(*item);
760 item->signal_activate().connect(
761 sigc::bind(
762 sigc::mem_fun(*this, &MainWindow::on_instrument_selection_change),
763 instrument_index
764 )
765 );
766 instrument_index++;
767 }
768 instrument_menu->show();
769 instrument_menu->get_submenu()->show_all_children();
770
771 for (gig::Group* group = gig->GetFirstGroup(); group; group = gig->GetNextGroup()) {
772 if (group->Name != "") {
773 Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();
774 Gtk::TreeModel::Row rowGroup = *iterGroup;
775 rowGroup[m_SamplesModel.m_col_name] = group->Name.c_str();
776 rowGroup[m_SamplesModel.m_col_group] = group;
777 rowGroup[m_SamplesModel.m_col_sample] = NULL;
778 for (gig::Sample* sample = group->GetFirstSample();
779 sample; sample = group->GetNextSample()) {
780 Gtk::TreeModel::iterator iterSample =
781 m_refSamplesTreeModel->append(rowGroup.children());
782 Gtk::TreeModel::Row rowSample = *iterSample;
783 rowSample[m_SamplesModel.m_col_name] = sample->pInfo->Name.c_str();
784 rowSample[m_SamplesModel.m_col_sample] = sample;
785 rowSample[m_SamplesModel.m_col_group] = NULL;
786 }
787 }
788 }
789
790 // select the first instrument
791 Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();
792 tree_sel_ref->select(Gtk::TreePath("0"));
793 }
794
795 void MainWindow::show_instr_props()
796 {
797 Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();
798 Gtk::TreeModel::iterator it = tree_sel_ref->get_selected();
799 if (it)
800 {
801 Gtk::TreeModel::Row row = *it;
802 if (row[m_Columns.m_col_instr])
803 {
804 instrumentProps.set_instrument(row[m_Columns.m_col_instr]);
805 instrumentProps.show();
806 instrumentProps.deiconify();
807 }
808 }
809 }
810
811 void MainWindow::on_button_release(GdkEventButton* button)
812 {
813 if (button->type == GDK_2BUTTON_PRESS) {
814 show_instr_props();
815 } else if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
816 popup_menu->popup(button->button, button->time);
817 }
818 }
819
820 void MainWindow::on_instrument_selection_change(int index) {
821 m_RegionChooser.set_instrument(file->GetInstrument(index));
822 }
823
824 void MainWindow::on_sample_treeview_button_release(GdkEventButton* button) {
825 if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
826 Gtk::Menu* sample_popup =
827 dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/SamplePopupMenu"));
828 // update enabled/disabled state of sample popup items
829 Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
830 Gtk::TreeModel::iterator it = sel->get_selected();
831 bool group_selected = false;
832 bool sample_selected = false;
833 if (it) {
834 Gtk::TreeModel::Row row = *it;
835 group_selected = row[m_SamplesModel.m_col_group];
836 sample_selected = row[m_SamplesModel.m_col_sample];
837 }
838 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/SampleProperties"))->
839 set_sensitive(group_selected || sample_selected);
840 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddSample"))->
841 set_sensitive(group_selected || sample_selected);
842 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddGroup"))->
843 set_sensitive(file);
844 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/RemoveSample"))->
845 set_sensitive(group_selected || sample_selected);
846 // show sample popup
847 sample_popup->popup(button->button, button->time);
848 }
849 }
850
851 void MainWindow::on_action_add_instrument() {
852 static int __instrument_indexer = 0;
853 if (!file) return;
854 gig::Instrument* instrument = file->AddInstrument();
855 __instrument_indexer++;
856 instrument->pInfo->Name =
857 "Unnamed Instrument " + ToString(__instrument_indexer);
858 // update instrument tree view
859 Gtk::TreeModel::iterator iterInstr = m_refTreeModel->append();
860 Gtk::TreeModel::Row rowInstr = *iterInstr;
861 rowInstr[m_Columns.m_col_name] = instrument->pInfo->Name.c_str();
862 rowInstr[m_Columns.m_col_instr] = instrument;
863 }
864
865 void MainWindow::on_action_remove_instrument() {
866 if (!file) return;
867 Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeView.get_selection();
868 Gtk::TreeModel::iterator it = sel->get_selected();
869 if (it) {
870 Gtk::TreeModel::Row row = *it;
871 gig::Instrument* instr = row[m_Columns.m_col_instr];
872 try {
873 // remove instrument from the gig file
874 if (instr) file->DeleteInstrument(instr);
875 // remove respective row from instruments tree view
876 m_refTreeModel->erase(it);
877 } catch (RIFF::Exception e) {
878 Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);
879 msg.run();
880 }
881 }
882 }
883
884 void MainWindow::on_action_sample_properties() {
885 //TODO: show a dialog where the selected sample's properties can be edited
886 Gtk::MessageDialog msg(
887 *this, "Sorry, yet to be implemented!", false, Gtk::MESSAGE_INFO
888 );
889 msg.run();
890 }
891
892 void MainWindow::on_action_add_group() {
893 static int __sample_indexer = 0;
894 if (!file) return;
895 gig::Group* group = file->AddGroup();
896 group->Name = "Unnamed Group";
897 if (__sample_indexer) group->Name += " " + ToString(__sample_indexer);
898 __sample_indexer++;
899 // update sample tree view
900 Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();
901 Gtk::TreeModel::Row rowGroup = *iterGroup;
902 rowGroup[m_SamplesModel.m_col_name] = group->Name.c_str();
903 rowGroup[m_SamplesModel.m_col_sample] = NULL;
904 rowGroup[m_SamplesModel.m_col_group] = group;
905 }
906
907 void MainWindow::on_action_add_sample() {
908 if (!file) return;
909 // get selected group
910 Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
911 Gtk::TreeModel::iterator it = sel->get_selected();
912 if (!it) return;
913 Gtk::TreeModel::Row row = *it;
914 gig::Group* group = row[m_SamplesModel.m_col_group];
915 if (!group) { // not a group, but a sample is selected (probably)
916 gig::Sample* sample = row[m_SamplesModel.m_col_sample];
917 if (!sample) return;
918 it = row.parent(); // resolve parent (that is the sample's group)
919 if (!it) return;
920 row = *it;
921 group = row[m_SamplesModel.m_col_group];
922 if (!group) return;
923 }
924 // show 'browse for file' dialog
925 Gtk::FileChooserDialog dialog(*this, _("Add Sample(s)"));
926 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
927 dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
928 dialog.set_select_multiple(true);
929 Gtk::FileFilter soundfilter; // matches all file types supported by libsndfile
930 const char* supportedFileTypes[] = {
931 "*.wav", "*.WAV", "*.aiff", "*.AIFF", "*.aifc", "*.AIFC", "*.snd",
932 "*.SND", "*.au", "*.AU", "*.paf", "*.PAF", "*.iff", "*.IFF",
933 "*.svx", "*.SVX", "*.sf", "*.SF", "*.voc", "*.VOC", "*.w64",
934 "*.W64", "*.pvf", "*.PVF", "*.xi", "*.XI", "*.htk", "*.HTK",
935 "*.caf", "*.CAF", NULL
936 };
937 for (int i = 0; supportedFileTypes[i]; i++)
938 soundfilter.add_pattern(supportedFileTypes[i]);
939 soundfilter.set_name("Sound Files");
940 Gtk::FileFilter allpassfilter; // matches every file
941 allpassfilter.add_pattern("*.*");
942 allpassfilter.set_name("All Files");
943 dialog.add_filter(soundfilter);
944 dialog.add_filter(allpassfilter);
945 if (dialog.run() == Gtk::RESPONSE_OK) {
946 Glib::ustring error_files;
947 Glib::SListHandle<Glib::ustring> filenames = dialog.get_filenames();
948 for (Glib::SListHandle<Glib::ustring>::iterator iter = filenames.begin();
949 iter != filenames.end(); ++iter) {
950 printf("Adding sample %s\n",(*iter).c_str());
951 // use libsndfile to retrieve file informations
952 SF_INFO info;
953 info.format = 0;
954 SNDFILE* hFile = sf_open((*iter).c_str(), SFM_READ, &info);
955 try {
956 if (!hFile) throw std::string("could not open file");
957 int bitdepth;
958 switch (info.format & 0xff) {
959 case SF_FORMAT_PCM_S8:
960 bitdepth = 16; // we simply convert to 16 bit for now
961 break;
962 case SF_FORMAT_PCM_16:
963 bitdepth = 16;
964 break;
965 case SF_FORMAT_PCM_24:
966 bitdepth = 32; // we simply convert to 32 bit for now
967 break;
968 case SF_FORMAT_PCM_32:
969 bitdepth = 32;
970 break;
971 case SF_FORMAT_PCM_U8:
972 bitdepth = 16; // we simply convert to 16 bit for now
973 break;
974 case SF_FORMAT_FLOAT:
975 bitdepth = 32;
976 break;
977 case SF_FORMAT_DOUBLE:
978 bitdepth = 32; // I guess we will always truncate this to 32 bit
979 break;
980 default:
981 sf_close(hFile); // close sound file
982 throw std::string("format not supported"); // unsupported subformat (yet?)
983 }
984 // add a new sample to the .gig file
985 gig::Sample* sample = file->AddSample();
986 // file name without path
987 sample->pInfo->Name = (*iter).substr((*iter).rfind('/') + 1).raw();
988 sample->Channels = info.channels;
989 sample->BitDepth = bitdepth;
990 sample->FrameSize = bitdepth / 8/*1 byte are 8 bits*/ * info.channels;
991 sample->SamplesPerSecond = info.samplerate;
992 // schedule resizing the sample (which will be done
993 // physically when File::Save() is called)
994 sample->Resize(info.frames);
995 // make sure sample is part of the selected group
996 group->AddSample(sample);
997 // schedule that physical resize and sample import
998 // (data copying), performed when "Save" is requested
999 SampleImportItem sched_item;
1000 sched_item.gig_sample = sample;
1001 sched_item.sample_path = *iter;
1002 m_SampleImportQueue.push_back(sched_item);
1003 // add sample to the tree view
1004 Gtk::TreeModel::iterator iterSample =
1005 m_refSamplesTreeModel->append(row.children());
1006 Gtk::TreeModel::Row rowSample = *iterSample;
1007 rowSample[m_SamplesModel.m_col_name] = sample->pInfo->Name.c_str();
1008 rowSample[m_SamplesModel.m_col_sample] = sample;
1009 rowSample[m_SamplesModel.m_col_group] = NULL;
1010 // close sound file
1011 sf_close(hFile);
1012 } catch (std::string what) { // remember the files that made trouble (and their cause)
1013 if (error_files.size()) error_files += "\n";
1014 error_files += *iter += " (" + what + ")";
1015 }
1016 }
1017 // show error message box when some file(s) could not be opened / added
1018 if (error_files.size()) {
1019 Glib::ustring txt = "Could not add the following sample(s):\n" + error_files;
1020 Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
1021 msg.run();
1022 }
1023 }
1024 }
1025
1026 void MainWindow::on_action_remove_sample() {
1027 if (!file) return;
1028 Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
1029 Gtk::TreeModel::iterator it = sel->get_selected();
1030 if (it) {
1031 Gtk::TreeModel::Row row = *it;
1032 gig::Group* group = row[m_SamplesModel.m_col_group];
1033 gig::Sample* sample = row[m_SamplesModel.m_col_sample];
1034 Glib::ustring name = row[m_SamplesModel.m_col_name];
1035 try {
1036 // remove group or sample from the gig file
1037 if (group) {
1038 // temporarily remember the samples that bolong to
1039 // that group (we need that to clean the queue)
1040 std::list<gig::Sample*> members;
1041 for (gig::Sample* pSample = group->GetFirstSample();
1042 pSample; pSample = group->GetNextSample()) {
1043 members.push_back(pSample);
1044 }
1045 // delete the group in the .gig file including the
1046 // samples that belong to the group
1047 file->DeleteGroup(group);
1048 // if sample(s) were just previously added, remove
1049 // them from the import queue
1050 for (std::list<gig::Sample*>::iterator member = members.begin();
1051 member != members.end(); ++member) {
1052 for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
1053 iter != m_SampleImportQueue.end(); ++iter) {
1054 if ((*iter).gig_sample == *member) {
1055 printf("Removing previously added sample '%s' from group '%s'\n",
1056 (*iter).sample_path.c_str(), name.c_str());
1057 m_SampleImportQueue.erase(iter);
1058 break;
1059 }
1060 }
1061 }
1062 } else if (sample) {
1063 // remove sample from the .gig file
1064 file->DeleteSample(sample);
1065 // if sample was just previously added, remove it from
1066 // the import queue
1067 for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
1068 iter != m_SampleImportQueue.end(); ++iter) {
1069 if ((*iter).gig_sample == sample) {
1070 printf("Removing previously added sample '%s'\n",
1071 (*iter).sample_path.c_str());
1072 m_SampleImportQueue.erase(iter);
1073 break;
1074 }
1075 }
1076 }
1077 // remove respective row(s) from samples tree view
1078 m_refSamplesTreeModel->erase(it);
1079 } catch (RIFF::Exception e) {
1080 Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);
1081 msg.run();
1082 }
1083 }
1084 }
1085
1086 void MainWindow::on_sample_treeview_drag_data_get(const Glib::RefPtr<Gdk::DragContext>&,
1087 Gtk::SelectionData& selection_data, guint, guint)
1088 {
1089 // get selected sample
1090 gig::Sample* sample = NULL;
1091 Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
1092 Gtk::TreeModel::iterator it = sel->get_selected();
1093 if (it) {
1094 Gtk::TreeModel::Row row = *it;
1095 sample = row[m_SamplesModel.m_col_sample];
1096 }
1097 // pass the gig::Sample as pointer
1098 selection_data.set(selection_data.get_target(), 0/*unused*/, (const guchar*)&sample,
1099 sizeof(sample)/*length of data in bytes*/);
1100 }
1101
1102 void MainWindow::on_sample_label_drop_drag_data_received(
1103 const Glib::RefPtr<Gdk::DragContext>& context, int, int,
1104 const Gtk::SelectionData& selection_data, guint, guint time)
1105 {
1106 gig::DimensionRegion* dimregion = m_DimRegionChooser.get_dimregion();
1107 gig::Sample* sample = *((gig::Sample**) selection_data.get_data());
1108
1109 if (sample && dimregion && selection_data.get_length() == sizeof(gig::Sample*)) {
1110 if (sample != dimregion->pSample) {
1111 dimregion->pSample = sample;
1112 dimreg_edit.wSample->set_text(dimregion->pSample->pInfo->Name.c_str());
1113 std::cout << "Drop received sample \"" <<
1114 dimregion->pSample->pInfo->Name.c_str() << "\"" << std::endl;
1115 // drop success
1116 context->drop_reply(true, time);
1117 return;
1118 }
1119 }
1120 // drop failed
1121 context->drop_reply(false, time);
1122 }
1123
1124 void MainWindow::sample_name_changed(const Gtk::TreeModel::Path& path,
1125 const Gtk::TreeModel::iterator& iter) {
1126 if (!iter) return;
1127 Gtk::TreeModel::Row row = *iter;
1128 Glib::ustring name = row[m_SamplesModel.m_col_name];
1129 gig::Group* group = row[m_SamplesModel.m_col_group];
1130 gig::Sample* sample = row[m_SamplesModel.m_col_sample];
1131 if (group) {
1132 group->Name = name;
1133 std::cout << "Group name changed\n" << std::flush;
1134 } else if (sample) {
1135 sample->pInfo->Name = name.raw();
1136 std::cout << "Sample name changed\n" << std::flush;
1137 }
1138 }
1139
1140 void MainWindow::instrument_name_changed(const Gtk::TreeModel::Path& path,
1141 const Gtk::TreeModel::iterator& iter) {
1142 std::cout << "Instrument name changed\n" << std::flush;
1143 if (!iter) return;
1144 Gtk::TreeModel::Row row = *iter;
1145 Glib::ustring name = row[m_Columns.m_col_name];
1146 gig::Instrument* instrument = row[m_Columns.m_col_instr];
1147 if (instrument) instrument->pInfo->Name = name.raw();
1148 }

  ViewVC Help
Powered by ViewVC