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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1265 - (hide annotations) (download)
Sun Jul 29 13:44:59 2007 UTC (16 years, 8 months ago) by persson
File size: 48754 byte(s)
* added support for 24 bit sample import
* when importing samples, settings for loops and root note are copied
  from the sample files (if they have such settings)

1 schoenebeck 1225 /*
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 persson 1261 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 6) || GTKMM_MAJOR_VERSION > 2
30 schoenebeck 1225 #define ABOUT_DIALOG
31     #include <gtkmm/aboutdialog.h>
32     #endif
33    
34     #include <stdio.h>
35     #include <sndfile.h>
36    
37     #include "mainwindow.h"
38    
39     #define _(String) gettext(String)
40    
41     template<class T> inline std::string ToString(T o) {
42     std::stringstream ss;
43     ss << o;
44     return ss.str();
45     }
46    
47     MainWindow::MainWindow()
48     {
49     // set_border_width(5);
50     // set_default_size(400, 200);
51    
52    
53     add(m_VBox);
54    
55     // Handle selection
56     Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();
57     tree_sel_ref->signal_changed().connect(
58     sigc::mem_fun(*this, &MainWindow::on_sel_change));
59    
60     // m_TreeView.set_reorderable();
61    
62     m_TreeView.signal_button_press_event().connect_notify(
63     sigc::mem_fun(*this, &MainWindow::on_button_release));
64    
65     // Add the TreeView tab, inside a ScrolledWindow, with the button underneath:
66     m_ScrolledWindow.add(m_TreeView);
67     // m_ScrolledWindow.set_size_request(200, 600);
68     m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
69    
70     m_ScrolledWindowSamples.add(m_TreeViewSamples);
71     m_ScrolledWindowSamples.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
72    
73    
74     m_TreeViewNotebook.set_size_request(300);
75    
76     m_HPaned.add1(m_TreeViewNotebook);
77     m_HPaned.add2(dimreg_edit);
78    
79    
80     m_TreeViewNotebook.append_page(m_ScrolledWindowSamples, "Samples");
81     m_TreeViewNotebook.append_page(m_ScrolledWindow, "Instruments");
82    
83    
84     actionGroup = Gtk::ActionGroup::create();
85    
86     actionGroup->add(Gtk::Action::create("MenuFile", _("_File")));
87     actionGroup->add(Gtk::Action::create("New", Gtk::Stock::NEW),
88     sigc::mem_fun(
89     *this, &MainWindow::on_action_file_new));
90     Glib::RefPtr<Gtk::Action> action =
91     Gtk::Action::create("Open", Gtk::Stock::OPEN);
92     action->property_label() = action->property_label() + "...";
93     actionGroup->add(action,
94     sigc::mem_fun(
95     *this, &MainWindow::on_action_file_open));
96     actionGroup->add(Gtk::Action::create("Save", Gtk::Stock::SAVE),
97     sigc::mem_fun(
98     *this, &MainWindow::on_action_file_save));
99     action = Gtk::Action::create("SaveAs", Gtk::Stock::SAVE_AS);
100     action->property_label() = action->property_label() + "...";
101     actionGroup->add(action,
102 persson 1261 Gtk::AccelKey("<shift><control>s"),
103 schoenebeck 1225 sigc::mem_fun(
104 persson 1261 *this, &MainWindow::on_action_file_save_as));
105 schoenebeck 1225 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 persson 1261 *this, &MainWindow::on_action_quit));
116 schoenebeck 1225 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 persson 1261 add_accel_group(uiManager->get_accel_group());
156 schoenebeck 1225
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 persson 1261 m_RegionChooser.signal_region_selected().connect(
204 schoenebeck 1225 sigc::mem_fun(*this, &MainWindow::region_changed) );
205 persson 1261 m_DimRegionChooser.signal_dimregion_selected().connect(
206 schoenebeck 1225 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 persson 1261 dimreg_edit.signal_dimreg_changed().connect(
245     sigc::mem_fun(*this, &MainWindow::file_changed));
246     m_RegionChooser.signal_instrument_changed().connect(
247     sigc::mem_fun(*this, &MainWindow::file_changed));
248     m_DimRegionChooser.signal_region_changed().connect(
249     sigc::mem_fun(*this, &MainWindow::file_changed));
250     instrumentProps.signal_instrument_changed().connect(
251     sigc::mem_fun(*this, &MainWindow::file_changed));
252 schoenebeck 1225 file = 0;
253 persson 1261 file_is_changed = false;
254 schoenebeck 1225
255     show_all_children();
256     }
257    
258     MainWindow::~MainWindow()
259     {
260     }
261    
262 persson 1261 bool MainWindow::on_delete_event(GdkEventAny* event)
263     {
264     return file_is_changed && !close_confirmation_dialog();
265     }
266    
267     void MainWindow::on_action_quit()
268     {
269     if (file_is_changed && !close_confirmation_dialog()) return;
270     hide();
271     }
272    
273 schoenebeck 1225 void MainWindow::region_changed()
274     {
275     m_DimRegionChooser.set_region(m_RegionChooser.get_region());
276     }
277    
278     void MainWindow::dimreg_changed()
279     {
280     dimreg_edit.set_dim_region(m_DimRegionChooser.get_dimregion());
281     }
282    
283     void MainWindow::on_sel_change()
284     {
285     Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();
286    
287     Gtk::TreeModel::iterator it = tree_sel_ref->get_selected();
288     if (it) {
289     Gtk::TreeModel::Row row = *it;
290     std::cout << row[m_Columns.m_col_name] << std::endl;
291    
292     m_RegionChooser.set_instrument(row[m_Columns.m_col_instr]);
293     } else {
294     m_RegionChooser.set_instrument(0);
295     }
296     }
297    
298     void loader_progress_callback(gig::progress_t* progress)
299     {
300     Loader* loader = static_cast<Loader*>(progress->custom);
301     loader->progress_callback(progress->factor);
302     }
303    
304     void Loader::progress_callback(float fraction)
305     {
306     {
307     Glib::Mutex::Lock lock(progressMutex);
308     progress = fraction;
309     }
310     progress_dispatcher();
311     }
312    
313     void Loader::thread_function()
314     {
315     printf("thread_function self=%x\n", Glib::Thread::self());
316     printf("Start %s\n", filename);
317     RIFF::File* riff = new RIFF::File(filename);
318     gig = new gig::File(riff);
319     gig::progress_t progress;
320     progress.callback = loader_progress_callback;
321     progress.custom = this;
322    
323     gig->GetInstrument(0, &progress);
324     printf("End\n");
325     finished_dispatcher();
326     }
327    
328     Loader::Loader(const char* filename)
329     : thread(0), filename(filename)
330     {
331     }
332    
333     void Loader::launch()
334     {
335     thread = Glib::Thread::create(sigc::mem_fun(*this, &Loader::thread_function), true);
336     printf("launch thread=%x\n", thread);
337     }
338    
339     float Loader::get_progress()
340     {
341     float res;
342     {
343     Glib::Mutex::Lock lock(progressMutex);
344     res = progress;
345     }
346     return res;
347     }
348    
349     Glib::Dispatcher& Loader::signal_progress()
350     {
351     return progress_dispatcher;
352     }
353    
354     Glib::Dispatcher& Loader::signal_finished()
355     {
356     return finished_dispatcher;
357     }
358    
359     LoadDialog::LoadDialog(const Glib::ustring& title, Gtk::Window& parent)
360     : Gtk::Dialog(title, parent, true)
361     {
362     get_vbox()->pack_start(progressBar);
363     show_all_children();
364     }
365    
366     // Clear all GUI elements / controls. This method is typically called
367     // before a new .gig file is to be created or to be loaded.
368     void MainWindow::__clear() {
369     // remove all entries from "Instrument" menu
370     Gtk::MenuItem* instrument_menu =
371     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuInstrument"));
372     instrument_menu->hide();
373     for (int i = 0; i < instrument_menu->get_submenu()->items().size(); i++) {
374     delete &instrument_menu->get_submenu()->items()[i];
375     }
376     instrument_menu->get_submenu()->items().clear();
377     // forget all samples that ought to be imported
378     m_SampleImportQueue.clear();
379     // clear the samples and instruments tree views
380     m_refTreeModel->clear();
381     m_refSamplesTreeModel->clear();
382     // free libgig's gig::File instance
383     if (file) {
384     delete file;
385     file = NULL;
386     }
387     }
388    
389     void MainWindow::on_action_file_new()
390     {
391 persson 1261 if (file_is_changed && !close_confirmation_dialog()) return;
392    
393 schoenebeck 1225 // clear all GUI elements
394     __clear();
395     // create a new .gig file (virtually yet)
396     gig::File* pFile = new gig::File;
397     // already add one new instrument by default
398     gig::Instrument* pInstrument = pFile->AddInstrument();
399     pInstrument->pInfo->Name = "Unnamed Instrument";
400     // update GUI with that new gig::File
401 persson 1261 load_gig(pFile, 0 /*no file name yet*/);
402 schoenebeck 1225 }
403    
404 persson 1261 bool MainWindow::close_confirmation_dialog()
405     {
406     gchar* msg = g_strdup_printf(_("Save changes to \"%s\" before closing?"),
407     Glib::filename_display_basename(filename).c_str());
408     Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
409     g_free(msg);
410     dialog.set_secondary_text(_("If you close without saving, your changes will be lost."));
411     dialog.add_button(_("Close _Without Saving"), Gtk::RESPONSE_NO);
412     dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
413     dialog.add_button(file_has_name ? Gtk::Stock::SAVE : Gtk::Stock::SAVE_AS, Gtk::RESPONSE_YES);
414     dialog.set_default_response(Gtk::RESPONSE_YES);
415     int response = dialog.run();
416     if (response == Gtk::RESPONSE_YES) return file_save();
417     return response != Gtk::RESPONSE_CANCEL;
418     }
419    
420 schoenebeck 1225 void MainWindow::on_action_file_open()
421     {
422 persson 1261 if (file_is_changed && !close_confirmation_dialog()) return;
423    
424 schoenebeck 1225 Gtk::FileChooserDialog dialog(*this, _("Open file"));
425     dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
426     dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
427 persson 1261 dialog.set_default_response(Gtk::RESPONSE_OK);
428 schoenebeck 1225 Gtk::FileFilter filter;
429     filter.add_pattern("*.gig");
430     dialog.set_filter(filter);
431 persson 1261 if (current_dir != "") {
432     dialog.set_current_folder(current_dir);
433     }
434 schoenebeck 1225 if (dialog.run() == Gtk::RESPONSE_OK) {
435 persson 1261 std::string filename = dialog.get_filename();
436     printf("filename=%s\n", filename.c_str());
437 schoenebeck 1225 __clear();
438     printf("on_action_file_open self=%x\n", Glib::Thread::self());
439 persson 1261 load_file(filename.c_str());
440     current_dir = Glib::path_get_dirname(filename);
441 schoenebeck 1225 }
442     }
443    
444     void MainWindow::load_file(const char* name)
445     {
446     load_dialog = new LoadDialog("Loading...", *this);
447     load_dialog->show_all();
448     loader = new Loader(strdup(name));
449     loader->signal_progress().connect(
450     sigc::mem_fun(*this, &MainWindow::on_loader_progress));
451     loader->signal_finished().connect(
452     sigc::mem_fun(*this, &MainWindow::on_loader_finished));
453     loader->launch();
454     }
455    
456     void MainWindow::load_instrument(gig::Instrument* instr) {
457     if (!instr) {
458     Glib::ustring txt = "Provided instrument is NULL!\n";
459     Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
460     msg.run();
461     Gtk::Main::quit();
462     }
463     gig::File* pFile = (gig::File*) instr->GetParent();
464 persson 1261 load_gig(pFile, 0 /*file name*/);
465 schoenebeck 1225 //TODO: automatically select the given instrument
466     }
467    
468     void MainWindow::on_loader_progress()
469     {
470     load_dialog->set_fraction(loader->get_progress());
471     }
472    
473     void MainWindow::on_loader_finished()
474     {
475     printf("Loader finished!\n");
476     printf("on_loader_finished self=%x\n", Glib::Thread::self());
477     load_gig(loader->gig, loader->filename);
478     load_dialog->hide();
479     }
480    
481     void MainWindow::on_action_file_save()
482     {
483 persson 1261 file_save();
484     }
485    
486     bool MainWindow::file_save()
487     {
488     if (!file) return false;
489     if (!file_has_name) return file_save_as();
490    
491 schoenebeck 1225 std::cout << "Saving file\n" << std::flush;
492     try {
493     file->Save();
494 persson 1261 if (file_is_changed) {
495     set_title(get_title().substr(1));
496     file_is_changed = false;
497     }
498 schoenebeck 1225 } catch (RIFF::Exception e) {
499     Glib::ustring txt = "Could not save file: " + e.Message;
500     Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
501     msg.run();
502 persson 1261 return false;
503 schoenebeck 1225 }
504     std::cout << "Saving file done\n" << std::flush;
505     __import_queued_samples();
506 persson 1261 return true;
507 schoenebeck 1225 }
508    
509     void MainWindow::on_action_file_save_as()
510     {
511 persson 1261 file_save_as();
512     }
513    
514     bool MainWindow::file_save_as()
515     {
516     if (!file) return false;
517     Gtk::FileChooserDialog dialog(*this, _("Save as"), Gtk::FILE_CHOOSER_ACTION_SAVE);
518 schoenebeck 1225 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
519     dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);
520 persson 1261 dialog.set_default_response(Gtk::RESPONSE_OK);
521    
522     #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 8) || GTKMM_MAJOR_VERSION > 2
523     dialog.set_do_overwrite_confirmation();
524     // TODO: an overwrite dialog for gtkmm < 2.8
525     #endif
526 schoenebeck 1225 Gtk::FileFilter filter;
527     filter.add_pattern("*.gig");
528     dialog.set_filter(filter);
529 persson 1261
530     if (Glib::path_is_absolute(filename)) {
531     dialog.set_filename(filename);
532     } else if (current_dir != "") {
533     dialog.set_current_folder(current_dir);
534     }
535     dialog.set_current_name(Glib::filename_display_basename(filename));
536    
537 schoenebeck 1225 if (dialog.run() == Gtk::RESPONSE_OK) {
538     try {
539 persson 1261 std::string filename = dialog.get_filename();
540     if (!Glib::str_has_suffix(filename, ".gig")) {
541     filename += ".gig";
542     }
543     printf("filename=%s\n", filename.c_str());
544     file->Save(filename);
545     this->filename = filename;
546     current_dir = Glib::path_get_dirname(filename);
547     set_title(Glib::filename_display_basename(filename));
548     file_has_name = true;
549     file_is_changed = false;
550 schoenebeck 1225 } catch (RIFF::Exception e) {
551     Glib::ustring txt = "Could not save file: " + e.Message;
552     Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
553     msg.run();
554 persson 1261 return false;
555 schoenebeck 1225 }
556     __import_queued_samples();
557 persson 1261 return true;
558 schoenebeck 1225 }
559 persson 1261 return false;
560 schoenebeck 1225 }
561    
562     // actually write the sample(s)' data to the gig file
563     void MainWindow::__import_queued_samples() {
564     std::cout << "Starting sample import\n" << std::flush;
565     Glib::ustring error_files;
566     printf("Samples to import: %d\n", m_SampleImportQueue.size());
567     for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
568     iter != m_SampleImportQueue.end(); ) {
569     printf("Importing sample %s\n",(*iter).sample_path.c_str());
570     SF_INFO info;
571     info.format = 0;
572     SNDFILE* hFile = sf_open((*iter).sample_path.c_str(), SFM_READ, &info);
573     try {
574     if (!hFile) throw std::string("could not open file");
575     // determine sample's bit depth
576     int bitdepth;
577     switch (info.format & 0xff) {
578     case SF_FORMAT_PCM_S8:
579     case SF_FORMAT_PCM_16:
580 persson 1265 case SF_FORMAT_PCM_U8:
581 schoenebeck 1225 bitdepth = 16;
582     break;
583     case SF_FORMAT_PCM_24:
584     case SF_FORMAT_PCM_32:
585     case SF_FORMAT_FLOAT:
586     case SF_FORMAT_DOUBLE:
587 persson 1265 bitdepth = 24;
588 schoenebeck 1225 break;
589     default:
590     sf_close(hFile); // close sound file
591     throw std::string("format not supported"); // unsupported subformat (yet?)
592     }
593 persson 1265
594     const int bufsize = 10000;
595 schoenebeck 1225 switch (bitdepth) {
596 persson 1265 case 16: {
597     short* buffer = new short[bufsize * info.channels];
598     sf_count_t cnt = info.frames;
599     while (cnt) {
600     // libsndfile does the conversion for us (if needed)
601     int n = sf_readf_short(hFile, buffer, bufsize);
602     // write from buffer directly (physically) into .gig file
603     iter->gig_sample->Write(buffer, n);
604     cnt -= n;
605     }
606     delete[] buffer;
607 schoenebeck 1225 break;
608 persson 1265 }
609     case 24: {
610     int* srcbuf = new int[bufsize * info.channels];
611     uint8_t* dstbuf = new uint8_t[bufsize * 3 * info.channels];
612     sf_count_t cnt = info.frames;
613     while (cnt) {
614     // libsndfile returns 32 bits, convert to 24
615     int n = sf_readf_int(hFile, srcbuf, bufsize);
616     int j = 0;
617     for (int i = 0 ; i < n * info.channels ; i++) {
618     dstbuf[j++] = srcbuf[i] >> 8;
619     dstbuf[j++] = srcbuf[i] >> 16;
620     dstbuf[j++] = srcbuf[i] >> 24;
621     }
622     // write from buffer directly (physically) into .gig file
623     iter->gig_sample->Write(dstbuf, n);
624     cnt -= n;
625     }
626     delete[] srcbuf;
627     delete[] dstbuf;
628 schoenebeck 1225 break;
629 persson 1265 }
630 schoenebeck 1225 }
631     // cleanup
632     sf_close(hFile);
633     // on success we remove the sample from the import queue,
634     // otherwise keep it, maybe it works the next time ?
635     std::list<SampleImportItem>::iterator cur = iter;
636     ++iter;
637     m_SampleImportQueue.erase(cur);
638     } catch (std::string what) {
639     // remember the files that made trouble (and their cause)
640     if (error_files.size()) error_files += "\n";
641     error_files += (*iter).sample_path += " (" + what + ")";
642     ++iter;
643     }
644     }
645     // show error message box when some sample(s) could not be imported
646     if (error_files.size()) {
647     Glib::ustring txt = "Could not import the following sample(s):\n" + error_files;
648     Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
649     msg.run();
650     }
651     }
652    
653     void MainWindow::on_action_file_properties()
654     {
655     propDialog.show();
656     propDialog.deiconify();
657     }
658    
659     void MainWindow::on_action_help_about()
660     {
661     #ifdef ABOUT_DIALOG
662     Gtk::AboutDialog dialog;
663     dialog.set_version(VERSION);
664     dialog.run();
665     #endif
666     }
667    
668     PropDialog::PropDialog()
669     : table(2,1)
670     {
671     table.set_col_spacings(5);
672     const char* propLabels[] = {
673     "Name:",
674     "CreationDate:",
675     "Comments:", // TODO: multiline
676     "Product:",
677     "Copyright:",
678     "Artists:",
679     "Genre:",
680     "Keywords:",
681     "Engineer:",
682     "Technician:",
683     "Software:", // TODO: readonly
684     "Medium:",
685     "Source:",
686     "SourceForm:",
687     "Commissioned:",
688     "Subject:"
689     };
690     for (int i = 0 ; i < sizeof(propLabels) / sizeof(char*) ; i++) {
691     label[i].set_text(propLabels[i]);
692     label[i].set_alignment(Gtk::ALIGN_LEFT);
693     table.attach(label[i], 0, 1, i, i + 1, Gtk::FILL, Gtk::SHRINK);
694     table.attach(entry[i], 1, 2, i, i + 1, Gtk::FILL | Gtk::EXPAND,
695     Gtk::SHRINK);
696     }
697    
698     add(table);
699     // add_button(Gtk::Stock::CANCEL, 0);
700     // add_button(Gtk::Stock::OK, 1);
701     show_all_children();
702     }
703    
704     void PropDialog::set_info(DLS::Info* info)
705     {
706     entry[0].set_text(info->Name);
707     entry[1].set_text(info->CreationDate);
708     entry[2].set_text(Glib::convert(info->Comments, "UTF-8", "ISO-8859-1"));
709     entry[3].set_text(info->Product);
710     entry[4].set_text(info->Copyright);
711     entry[5].set_text(info->Artists);
712     entry[6].set_text(info->Genre);
713     entry[7].set_text(info->Keywords);
714     entry[8].set_text(info->Engineer);
715     entry[9].set_text(info->Technician);
716     entry[10].set_text(info->Software);
717     entry[11].set_text(info->Medium);
718     entry[12].set_text(info->Source);
719     entry[13].set_text(info->SourceForm);
720     entry[14].set_text(info->Commissioned);
721     entry[15].set_text(info->Subject);
722     }
723    
724 persson 1262 void InstrumentProps::add_prop(BoolEntry& boolentry)
725     {
726     table.attach(boolentry.widget, 0, 2, rowno, rowno + 1,
727     Gtk::FILL, Gtk::SHRINK);
728     rowno++;
729     boolentry.signal_changed_by_user().connect(instrument_changed.make_slot());
730     }
731    
732     void InstrumentProps::add_prop(BoolEntryPlus6& boolentry)
733     {
734     table.attach(boolentry.widget, 0, 2, rowno, rowno + 1,
735     Gtk::FILL, Gtk::SHRINK);
736     rowno++;
737     boolentry.signal_changed_by_user().connect(instrument_changed.make_slot());
738     }
739    
740 schoenebeck 1225 void InstrumentProps::add_prop(LabelWidget& prop)
741     {
742     table.attach(prop.label, 0, 1, rowno, rowno + 1,
743     Gtk::FILL, Gtk::SHRINK);
744     table.attach(prop.widget, 1, 2, rowno, rowno + 1,
745     Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
746     rowno++;
747 persson 1261 prop.signal_changed_by_user().connect(instrument_changed.make_slot());
748 schoenebeck 1225 }
749    
750     InstrumentProps::InstrumentProps()
751     : table(2,1),
752     quitButton(Gtk::Stock::CLOSE),
753     eName("Name"),
754 persson 1262 eIsDrum("Is drum"),
755     eMIDIBank("MIDI bank", 0, 16383),
756     eMIDIProgram("MIDI program"),
757 schoenebeck 1225 eAttenuation("Attenuation", 0, 96, 0, 1),
758     eGainPlus6("Gain +6dB", eAttenuation, -6),
759 persson 1262 eEffectSend("Effect send", 0, 65535),
760     eFineTune("Fine tune", -8400, 8400),
761     ePitchbendRange("Pitchbend range", 0, 12),
762     ePianoReleaseMode("Piano release mode"),
763     eDimensionKeyRangeLow("Dimension key range low"),
764     eDimensionKeyRangeHigh("Dimension key range high")
765 schoenebeck 1225 {
766     set_title("Instrument properties");
767    
768     rowno = 0;
769     table.set_col_spacings(5);
770    
771     add_prop(eName);
772     add_prop(eIsDrum);
773     add_prop(eMIDIBank);
774     add_prop(eMIDIProgram);
775     add_prop(eAttenuation);
776     add_prop(eGainPlus6);
777     add_prop(eEffectSend);
778     add_prop(eFineTune);
779     add_prop(ePitchbendRange);
780     add_prop(ePianoReleaseMode);
781     add_prop(eDimensionKeyRangeLow);
782     add_prop(eDimensionKeyRangeHigh);
783    
784 persson 1261 eDimensionKeyRangeLow.signal_changed_by_user().connect(
785 schoenebeck 1225 sigc::mem_fun(*this, &InstrumentProps::key_range_low_changed));
786 persson 1261 eDimensionKeyRangeHigh.signal_changed_by_user().connect(
787 schoenebeck 1225 sigc::mem_fun(*this, &InstrumentProps::key_range_high_changed));
788    
789     add(vbox);
790     table.set_border_width(5);
791     vbox.pack_start(table);
792     table.show();
793     vbox.pack_start(buttonBox, Gtk::PACK_SHRINK);
794     buttonBox.set_layout(Gtk::BUTTONBOX_END);
795     buttonBox.set_border_width(5);
796     buttonBox.show();
797     buttonBox.pack_start(quitButton);
798     quitButton.set_flags(Gtk::CAN_DEFAULT);
799     quitButton.grab_focus();
800    
801     quitButton.signal_clicked().connect(
802     sigc::mem_fun(*this, &InstrumentProps::hide));
803    
804     quitButton.show();
805     vbox.show();
806     show_all_children();
807     }
808    
809     void InstrumentProps::set_instrument(gig::Instrument* instrument)
810     {
811     eName.set_ptr(&instrument->pInfo->Name);
812     eIsDrum.set_ptr(&instrument->IsDrum);
813     eMIDIBank.set_ptr(&instrument->MIDIBank);
814     eMIDIProgram.set_ptr(&instrument->MIDIProgram);
815     eAttenuation.set_ptr(&instrument->Attenuation);
816     eGainPlus6.set_ptr(&instrument->Attenuation);
817     eEffectSend.set_ptr(&instrument->EffectSend);
818     eFineTune.set_ptr(&instrument->FineTune);
819     ePitchbendRange.set_ptr(&instrument->PitchbendRange);
820     ePianoReleaseMode.set_ptr(&instrument->PianoReleaseMode);
821 persson 1261 eDimensionKeyRangeLow.set_ptr(0);
822     eDimensionKeyRangeHigh.set_ptr(0);
823 schoenebeck 1225 eDimensionKeyRangeLow.set_ptr(&instrument->DimensionKeyRange.low);
824     eDimensionKeyRangeHigh.set_ptr(&instrument->DimensionKeyRange.high);
825     }
826    
827     void InstrumentProps::key_range_low_changed()
828     {
829     double l = eDimensionKeyRangeLow.get_value();
830     double h = eDimensionKeyRangeHigh.get_value();
831     if (h < l) eDimensionKeyRangeHigh.set_value(l);
832     }
833    
834     void InstrumentProps::key_range_high_changed()
835     {
836     double l = eDimensionKeyRangeLow.get_value();
837     double h = eDimensionKeyRangeHigh.get_value();
838     if (h < l) eDimensionKeyRangeLow.set_value(h);
839     }
840    
841 persson 1261 sigc::signal<void> InstrumentProps::signal_instrument_changed()
842 schoenebeck 1225 {
843 persson 1261 return instrument_changed;
844     }
845 schoenebeck 1225
846 persson 1261 void MainWindow::file_changed()
847     {
848     if (file && !file_is_changed) {
849     set_title("*" + get_title());
850     file_is_changed = true;
851 schoenebeck 1225 }
852 persson 1261 }
853 schoenebeck 1225
854 persson 1261 void MainWindow::load_gig(gig::File* gig, const char* filename)
855     {
856     file = 0;
857    
858     this->filename = filename ? filename : _("Unsaved Gig File");
859     set_title(Glib::filename_display_basename(this->filename));
860     file_has_name = filename;
861     file_is_changed = false;
862    
863 schoenebeck 1225 propDialog.set_info(gig->pInfo);
864    
865     Gtk::MenuItem* instrument_menu =
866     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuInstrument"));
867    
868     int instrument_index = 0;
869     Gtk::RadioMenuItem::Group instrument_group;
870     for (gig::Instrument* instrument = gig->GetFirstInstrument() ; instrument ;
871     instrument = gig->GetNextInstrument()) {
872     Gtk::TreeModel::iterator iter = m_refTreeModel->append();
873     Gtk::TreeModel::Row row = *iter;
874     row[m_Columns.m_col_name] = instrument->pInfo->Name.c_str();
875     row[m_Columns.m_col_instr] = instrument;
876     // create a menu item for this instrument
877     Gtk::RadioMenuItem* item =
878     new Gtk::RadioMenuItem(instrument_group, instrument->pInfo->Name.c_str());
879     instrument_menu->get_submenu()->append(*item);
880     item->signal_activate().connect(
881     sigc::bind(
882     sigc::mem_fun(*this, &MainWindow::on_instrument_selection_change),
883     instrument_index
884     )
885     );
886     instrument_index++;
887     }
888     instrument_menu->show();
889     instrument_menu->get_submenu()->show_all_children();
890    
891     for (gig::Group* group = gig->GetFirstGroup(); group; group = gig->GetNextGroup()) {
892     if (group->Name != "") {
893     Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();
894     Gtk::TreeModel::Row rowGroup = *iterGroup;
895     rowGroup[m_SamplesModel.m_col_name] = group->Name.c_str();
896     rowGroup[m_SamplesModel.m_col_group] = group;
897     rowGroup[m_SamplesModel.m_col_sample] = NULL;
898     for (gig::Sample* sample = group->GetFirstSample();
899     sample; sample = group->GetNextSample()) {
900     Gtk::TreeModel::iterator iterSample =
901     m_refSamplesTreeModel->append(rowGroup.children());
902     Gtk::TreeModel::Row rowSample = *iterSample;
903     rowSample[m_SamplesModel.m_col_name] = sample->pInfo->Name.c_str();
904     rowSample[m_SamplesModel.m_col_sample] = sample;
905     rowSample[m_SamplesModel.m_col_group] = NULL;
906     }
907     }
908     }
909    
910 persson 1261 file = gig;
911    
912 schoenebeck 1225 // select the first instrument
913     Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();
914     tree_sel_ref->select(Gtk::TreePath("0"));
915     }
916    
917     void MainWindow::show_instr_props()
918     {
919     Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();
920     Gtk::TreeModel::iterator it = tree_sel_ref->get_selected();
921     if (it)
922     {
923     Gtk::TreeModel::Row row = *it;
924     if (row[m_Columns.m_col_instr])
925     {
926     instrumentProps.set_instrument(row[m_Columns.m_col_instr]);
927     instrumentProps.show();
928     instrumentProps.deiconify();
929     }
930     }
931     }
932    
933     void MainWindow::on_button_release(GdkEventButton* button)
934     {
935     if (button->type == GDK_2BUTTON_PRESS) {
936     show_instr_props();
937     } else if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
938     popup_menu->popup(button->button, button->time);
939     }
940     }
941    
942     void MainWindow::on_instrument_selection_change(int index) {
943     m_RegionChooser.set_instrument(file->GetInstrument(index));
944     }
945    
946     void MainWindow::on_sample_treeview_button_release(GdkEventButton* button) {
947     if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
948     Gtk::Menu* sample_popup =
949     dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/SamplePopupMenu"));
950     // update enabled/disabled state of sample popup items
951     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
952     Gtk::TreeModel::iterator it = sel->get_selected();
953     bool group_selected = false;
954     bool sample_selected = false;
955     if (it) {
956     Gtk::TreeModel::Row row = *it;
957     group_selected = row[m_SamplesModel.m_col_group];
958     sample_selected = row[m_SamplesModel.m_col_sample];
959     }
960     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/SampleProperties"))->
961     set_sensitive(group_selected || sample_selected);
962     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddSample"))->
963     set_sensitive(group_selected || sample_selected);
964     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddGroup"))->
965     set_sensitive(file);
966     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/RemoveSample"))->
967     set_sensitive(group_selected || sample_selected);
968     // show sample popup
969     sample_popup->popup(button->button, button->time);
970     }
971     }
972    
973     void MainWindow::on_action_add_instrument() {
974     static int __instrument_indexer = 0;
975     if (!file) return;
976     gig::Instrument* instrument = file->AddInstrument();
977     __instrument_indexer++;
978     instrument->pInfo->Name =
979     "Unnamed Instrument " + ToString(__instrument_indexer);
980     // update instrument tree view
981     Gtk::TreeModel::iterator iterInstr = m_refTreeModel->append();
982     Gtk::TreeModel::Row rowInstr = *iterInstr;
983     rowInstr[m_Columns.m_col_name] = instrument->pInfo->Name.c_str();
984     rowInstr[m_Columns.m_col_instr] = instrument;
985 persson 1261 file_changed();
986 schoenebeck 1225 }
987    
988     void MainWindow::on_action_remove_instrument() {
989     if (!file) return;
990     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeView.get_selection();
991     Gtk::TreeModel::iterator it = sel->get_selected();
992     if (it) {
993     Gtk::TreeModel::Row row = *it;
994     gig::Instrument* instr = row[m_Columns.m_col_instr];
995     try {
996     // remove instrument from the gig file
997     if (instr) file->DeleteInstrument(instr);
998     // remove respective row from instruments tree view
999     m_refTreeModel->erase(it);
1000 persson 1261 file_changed();
1001 schoenebeck 1225 } catch (RIFF::Exception e) {
1002     Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);
1003     msg.run();
1004     }
1005     }
1006     }
1007    
1008     void MainWindow::on_action_sample_properties() {
1009     //TODO: show a dialog where the selected sample's properties can be edited
1010     Gtk::MessageDialog msg(
1011     *this, "Sorry, yet to be implemented!", false, Gtk::MESSAGE_INFO
1012     );
1013     msg.run();
1014     }
1015    
1016     void MainWindow::on_action_add_group() {
1017     static int __sample_indexer = 0;
1018     if (!file) return;
1019     gig::Group* group = file->AddGroup();
1020     group->Name = "Unnamed Group";
1021     if (__sample_indexer) group->Name += " " + ToString(__sample_indexer);
1022     __sample_indexer++;
1023     // update sample tree view
1024     Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();
1025     Gtk::TreeModel::Row rowGroup = *iterGroup;
1026     rowGroup[m_SamplesModel.m_col_name] = group->Name.c_str();
1027     rowGroup[m_SamplesModel.m_col_sample] = NULL;
1028     rowGroup[m_SamplesModel.m_col_group] = group;
1029 persson 1261 file_changed();
1030 schoenebeck 1225 }
1031    
1032     void MainWindow::on_action_add_sample() {
1033     if (!file) return;
1034     // get selected group
1035     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
1036     Gtk::TreeModel::iterator it = sel->get_selected();
1037     if (!it) return;
1038     Gtk::TreeModel::Row row = *it;
1039     gig::Group* group = row[m_SamplesModel.m_col_group];
1040     if (!group) { // not a group, but a sample is selected (probably)
1041     gig::Sample* sample = row[m_SamplesModel.m_col_sample];
1042     if (!sample) return;
1043     it = row.parent(); // resolve parent (that is the sample's group)
1044     if (!it) return;
1045     row = *it;
1046     group = row[m_SamplesModel.m_col_group];
1047     if (!group) return;
1048     }
1049     // show 'browse for file' dialog
1050     Gtk::FileChooserDialog dialog(*this, _("Add Sample(s)"));
1051     dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1052     dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
1053     dialog.set_select_multiple(true);
1054     Gtk::FileFilter soundfilter; // matches all file types supported by libsndfile
1055 persson 1262 const char* const supportedFileTypes[] = {
1056 schoenebeck 1225 "*.wav", "*.WAV", "*.aiff", "*.AIFF", "*.aifc", "*.AIFC", "*.snd",
1057     "*.SND", "*.au", "*.AU", "*.paf", "*.PAF", "*.iff", "*.IFF",
1058     "*.svx", "*.SVX", "*.sf", "*.SF", "*.voc", "*.VOC", "*.w64",
1059     "*.W64", "*.pvf", "*.PVF", "*.xi", "*.XI", "*.htk", "*.HTK",
1060     "*.caf", "*.CAF", NULL
1061     };
1062     for (int i = 0; supportedFileTypes[i]; i++)
1063     soundfilter.add_pattern(supportedFileTypes[i]);
1064     soundfilter.set_name("Sound Files");
1065     Gtk::FileFilter allpassfilter; // matches every file
1066     allpassfilter.add_pattern("*.*");
1067     allpassfilter.set_name("All Files");
1068     dialog.add_filter(soundfilter);
1069     dialog.add_filter(allpassfilter);
1070     if (dialog.run() == Gtk::RESPONSE_OK) {
1071     Glib::ustring error_files;
1072     Glib::SListHandle<Glib::ustring> filenames = dialog.get_filenames();
1073     for (Glib::SListHandle<Glib::ustring>::iterator iter = filenames.begin();
1074     iter != filenames.end(); ++iter) {
1075     printf("Adding sample %s\n",(*iter).c_str());
1076     // use libsndfile to retrieve file informations
1077     SF_INFO info;
1078     info.format = 0;
1079     SNDFILE* hFile = sf_open((*iter).c_str(), SFM_READ, &info);
1080     try {
1081     if (!hFile) throw std::string("could not open file");
1082     int bitdepth;
1083     switch (info.format & 0xff) {
1084     case SF_FORMAT_PCM_S8:
1085     case SF_FORMAT_PCM_16:
1086 persson 1265 case SF_FORMAT_PCM_U8:
1087 schoenebeck 1225 bitdepth = 16;
1088     break;
1089     case SF_FORMAT_PCM_24:
1090     case SF_FORMAT_PCM_32:
1091     case SF_FORMAT_FLOAT:
1092     case SF_FORMAT_DOUBLE:
1093 persson 1265 bitdepth = 24;
1094 schoenebeck 1225 break;
1095     default:
1096     sf_close(hFile); // close sound file
1097     throw std::string("format not supported"); // unsupported subformat (yet?)
1098     }
1099     // add a new sample to the .gig file
1100     gig::Sample* sample = file->AddSample();
1101     // file name without path
1102 persson 1262 Glib::ustring filename = Glib::filename_display_basename(*iter);
1103     // remove file extension if there is one
1104     for (int i = 0; supportedFileTypes[i]; i++) {
1105     if (Glib::str_has_suffix(filename, supportedFileTypes[i] + 1)) {
1106     filename.erase(filename.length() - strlen(supportedFileTypes[i] + 1));
1107     break;
1108     }
1109     }
1110     sample->pInfo->Name = filename;
1111 schoenebeck 1225 sample->Channels = info.channels;
1112     sample->BitDepth = bitdepth;
1113     sample->FrameSize = bitdepth / 8/*1 byte are 8 bits*/ * info.channels;
1114     sample->SamplesPerSecond = info.samplerate;
1115 persson 1265 sample->AverageBytesPerSecond = sample->FrameSize * sample->SamplesPerSecond;
1116     sample->BlockAlign = sample->FrameSize;
1117     sample->SamplesTotal = info.frames;
1118    
1119     SF_INSTRUMENT instrument;
1120     if (sf_command(hFile, SFC_GET_INSTRUMENT,
1121     &instrument, sizeof(instrument)) != SF_FALSE)
1122     {
1123     sample->MIDIUnityNote = instrument.basenote;
1124    
1125     if (instrument.loop_count && instrument.loops[0].mode != SF_LOOP_NONE) {
1126     sample->Loops = 1;
1127    
1128     switch (instrument.loops[0].mode) {
1129     case SF_LOOP_FORWARD:
1130     sample->LoopType = gig::loop_type_normal;
1131     break;
1132     case SF_LOOP_BACKWARD:
1133     sample->LoopType = gig::loop_type_backward;
1134     break;
1135     case SF_LOOP_ALTERNATING:
1136     sample->LoopType = gig::loop_type_bidirectional;
1137     break;
1138     }
1139     sample->LoopStart = instrument.loops[0].start;
1140     sample->LoopEnd = instrument.loops[0].end;
1141     sample->LoopPlayCount = instrument.loops[0].count;
1142     sample->LoopSize = sample->LoopEnd - sample->LoopStart + 1;
1143     }
1144     }
1145    
1146 schoenebeck 1225 // schedule resizing the sample (which will be done
1147     // physically when File::Save() is called)
1148     sample->Resize(info.frames);
1149     // make sure sample is part of the selected group
1150     group->AddSample(sample);
1151     // schedule that physical resize and sample import
1152     // (data copying), performed when "Save" is requested
1153     SampleImportItem sched_item;
1154     sched_item.gig_sample = sample;
1155     sched_item.sample_path = *iter;
1156     m_SampleImportQueue.push_back(sched_item);
1157     // add sample to the tree view
1158     Gtk::TreeModel::iterator iterSample =
1159     m_refSamplesTreeModel->append(row.children());
1160     Gtk::TreeModel::Row rowSample = *iterSample;
1161 persson 1262 rowSample[m_SamplesModel.m_col_name] = filename;
1162 schoenebeck 1225 rowSample[m_SamplesModel.m_col_sample] = sample;
1163     rowSample[m_SamplesModel.m_col_group] = NULL;
1164     // close sound file
1165     sf_close(hFile);
1166 persson 1261 file_changed();
1167 schoenebeck 1225 } catch (std::string what) { // remember the files that made trouble (and their cause)
1168     if (error_files.size()) error_files += "\n";
1169     error_files += *iter += " (" + what + ")";
1170     }
1171     }
1172     // show error message box when some file(s) could not be opened / added
1173     if (error_files.size()) {
1174     Glib::ustring txt = "Could not add the following sample(s):\n" + error_files;
1175     Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
1176     msg.run();
1177     }
1178     }
1179     }
1180    
1181     void MainWindow::on_action_remove_sample() {
1182     if (!file) return;
1183     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
1184     Gtk::TreeModel::iterator it = sel->get_selected();
1185     if (it) {
1186     Gtk::TreeModel::Row row = *it;
1187     gig::Group* group = row[m_SamplesModel.m_col_group];
1188     gig::Sample* sample = row[m_SamplesModel.m_col_sample];
1189     Glib::ustring name = row[m_SamplesModel.m_col_name];
1190     try {
1191     // remove group or sample from the gig file
1192     if (group) {
1193     // temporarily remember the samples that bolong to
1194     // that group (we need that to clean the queue)
1195     std::list<gig::Sample*> members;
1196     for (gig::Sample* pSample = group->GetFirstSample();
1197     pSample; pSample = group->GetNextSample()) {
1198     members.push_back(pSample);
1199     }
1200     // delete the group in the .gig file including the
1201     // samples that belong to the group
1202     file->DeleteGroup(group);
1203     // if sample(s) were just previously added, remove
1204     // them from the import queue
1205     for (std::list<gig::Sample*>::iterator member = members.begin();
1206     member != members.end(); ++member) {
1207     for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
1208     iter != m_SampleImportQueue.end(); ++iter) {
1209     if ((*iter).gig_sample == *member) {
1210     printf("Removing previously added sample '%s' from group '%s'\n",
1211     (*iter).sample_path.c_str(), name.c_str());
1212     m_SampleImportQueue.erase(iter);
1213     break;
1214     }
1215     }
1216     }
1217 persson 1261 file_changed();
1218 schoenebeck 1225 } else if (sample) {
1219     // remove sample from the .gig file
1220     file->DeleteSample(sample);
1221     // if sample was just previously added, remove it from
1222     // the import queue
1223     for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
1224     iter != m_SampleImportQueue.end(); ++iter) {
1225     if ((*iter).gig_sample == sample) {
1226     printf("Removing previously added sample '%s'\n",
1227     (*iter).sample_path.c_str());
1228     m_SampleImportQueue.erase(iter);
1229     break;
1230     }
1231     }
1232 persson 1261 file_changed();
1233 schoenebeck 1225 }
1234     // remove respective row(s) from samples tree view
1235     m_refSamplesTreeModel->erase(it);
1236     } catch (RIFF::Exception e) {
1237     Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);
1238     msg.run();
1239     }
1240     }
1241     }
1242    
1243     void MainWindow::on_sample_treeview_drag_data_get(const Glib::RefPtr<Gdk::DragContext>&,
1244     Gtk::SelectionData& selection_data, guint, guint)
1245     {
1246     // get selected sample
1247     gig::Sample* sample = NULL;
1248     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
1249     Gtk::TreeModel::iterator it = sel->get_selected();
1250     if (it) {
1251     Gtk::TreeModel::Row row = *it;
1252     sample = row[m_SamplesModel.m_col_sample];
1253     }
1254     // pass the gig::Sample as pointer
1255     selection_data.set(selection_data.get_target(), 0/*unused*/, (const guchar*)&sample,
1256     sizeof(sample)/*length of data in bytes*/);
1257     }
1258    
1259     void MainWindow::on_sample_label_drop_drag_data_received(
1260     const Glib::RefPtr<Gdk::DragContext>& context, int, int,
1261     const Gtk::SelectionData& selection_data, guint, guint time)
1262     {
1263     gig::Sample* sample = *((gig::Sample**) selection_data.get_data());
1264    
1265 persson 1265 if (sample && selection_data.get_length() == sizeof(gig::Sample*)) {
1266     if (dimreg_edit.set_sample(sample)) {
1267 schoenebeck 1225 std::cout << "Drop received sample \"" <<
1268 persson 1265 sample->pInfo->Name << "\"" << std::endl;
1269 schoenebeck 1225 // drop success
1270     context->drop_reply(true, time);
1271     return;
1272     }
1273     }
1274     // drop failed
1275     context->drop_reply(false, time);
1276     }
1277    
1278     void MainWindow::sample_name_changed(const Gtk::TreeModel::Path& path,
1279     const Gtk::TreeModel::iterator& iter) {
1280     if (!iter) return;
1281     Gtk::TreeModel::Row row = *iter;
1282     Glib::ustring name = row[m_SamplesModel.m_col_name];
1283     gig::Group* group = row[m_SamplesModel.m_col_group];
1284     gig::Sample* sample = row[m_SamplesModel.m_col_sample];
1285     if (group) {
1286 persson 1261 if (group->Name != name) {
1287     group->Name = name;
1288     printf("group name changed\n");
1289     file_changed();
1290     }
1291 schoenebeck 1225 } else if (sample) {
1292 persson 1261 if (sample->pInfo->Name != name.raw()) {
1293     sample->pInfo->Name = name.raw();
1294     printf("sample name changed\n");
1295     file_changed();
1296     }
1297 schoenebeck 1225 }
1298     }
1299    
1300     void MainWindow::instrument_name_changed(const Gtk::TreeModel::Path& path,
1301     const Gtk::TreeModel::iterator& iter) {
1302     if (!iter) return;
1303     Gtk::TreeModel::Row row = *iter;
1304     Glib::ustring name = row[m_Columns.m_col_name];
1305     gig::Instrument* instrument = row[m_Columns.m_col_instr];
1306 persson 1261 if (instrument && instrument->pInfo->Name != name.raw()) {
1307     instrument->pInfo->Name = name.raw();
1308     file_changed();
1309     }
1310 schoenebeck 1225 }

  ViewVC Help
Powered by ViewVC