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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

gigedit/branches/linuxsampler_org/src/mainwindow.cpp revision 1052 by persson, Sat Mar 3 12:20:01 2007 UTC gigedit/trunk/src/mainwindow.cpp revision 1092 by persson, Sun Mar 11 15:21:58 2007 UTC
# Line 27  Line 27 
27  #if GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 6  #if GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 6
28  #define ABOUT_DIALOG  #define ABOUT_DIALOG
29  #include <gtkmm/aboutdialog.h>  #include <gtkmm/aboutdialog.h>
30    #include <gtkmm/messagedialog.h>
31  #endif  #endif
32    
33    #include <stdio.h>
34    #include <sndfile.h>
35    
36  #define _(String) gettext(String)  #define _(String) gettext(String)
37    
38    template<class T> inline std::string ToString(T o) {
39        std::stringstream ss;
40        ss << o;
41        return ss.str();
42    }
43    
44  bool update_gui;  bool update_gui;
45    
# Line 381  void MainWindow::addProp(LabelWidget& pr Line 390  void MainWindow::addProp(LabelWidget& pr
390    
391  MainWindow::MainWindow() :  MainWindow::MainWindow() :
392  //    eSample("Sample", wSample),  //    eSample("Sample", wSample),
     eVelocityUpperLimit("VelocityUpperLimit",  
                         &gig::DimensionRegion::VelocityUpperLimit),  
393      eEG1PreAttack("PreAttack", &gig::DimensionRegion::EG1PreAttack, 0, 100, 2),      eEG1PreAttack("PreAttack", &gig::DimensionRegion::EG1PreAttack, 0, 100, 2),
394      eEG1Attack("Attack", &gig::DimensionRegion::EG1Attack, 0, 60, 3),      eEG1Attack("Attack", &gig::DimensionRegion::EG1Attack, 0, 60, 3),
395      eEG1Decay1("Decay1", &gig::DimensionRegion::EG1Decay1, 0.005, 60, 3),      eEG1Decay1("Decay1", &gig::DimensionRegion::EG1Decay1, 0.005, 60, 3),
# Line 511  MainWindow::MainWindow() : Line 518  MainWindow::MainWindow() :
518      eSampleLoops("SampleLoops", &access_SampleLoops, 0, 1)      eSampleLoops("SampleLoops", &access_SampleLoops, 0, 1)
519  {  {
520  //    set_border_width(5);  //    set_border_width(5);
521      set_default_size(400, 200);  //    set_default_size(400, 200);
522    
523    
524      add(m_VBox);      add(m_VBox);
# Line 524  MainWindow::MainWindow() : Line 531  MainWindow::MainWindow() :
531      m_TreeView.signal_button_press_event().connect_notify(      m_TreeView.signal_button_press_event().connect_notify(
532          sigc::mem_fun(*this, &MainWindow::on_button_release));          sigc::mem_fun(*this, &MainWindow::on_button_release));
533    
534      // Add the TreeView, inside a ScrolledWindow, with the button underneath:      // Add the TreeView tab, inside a ScrolledWindow, with the button underneath:
535      m_ScrolledWindow.add(m_TreeView);      m_ScrolledWindow.add(m_TreeView);
536      m_ScrolledWindow.set_size_request(400, 600);  //    m_ScrolledWindow.set_size_request(200, 600);
537      m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);      m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
538    
539        m_ScrolledWindowSamples.add(m_TreeViewSamples);
540        m_ScrolledWindowSamples.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
541    
542    
543      for (int i = 0 ; i < 5 ; i++) {      for (int i = 0 ; i < 5 ; i++) {
544          table[i] = new Gtk::Table(3, 1);          table[i] = new Gtk::Table(3, 1);
545          table[i]->set_col_spacings(5);          table[i]->set_col_spacings(5);
# Line 539  MainWindow::MainWindow() : Line 550  MainWindow::MainWindow() :
550      firstRowInBlock = 0;      firstRowInBlock = 0;
551    
552      addString("Sample", lSample, wSample);      addString("Sample", lSample, wSample);
     addProp(eVelocityUpperLimit);  
553      addHeader("EG1");      addHeader("EG1");
554      addProp(eEG1PreAttack);      addProp(eEG1PreAttack);
555      addProp(eEG1Attack);      addProp(eEG1Attack);
# Line 778  MainWindow::MainWindow() : Line 788  MainWindow::MainWindow() :
788      m_Notebook.append_page(*table[2], "EG3");      m_Notebook.append_page(*table[2], "EG3");
789      m_Notebook.append_page(*table[3], "Velocity");      m_Notebook.append_page(*table[3], "Velocity");
790      m_Notebook.append_page(*table[4], "Misc");      m_Notebook.append_page(*table[4], "Misc");
791      m_Notebook.set_size_request(400, 500);  //    m_Notebook.set_size_request(400, 500);
792    
793      m_HPaned.add1(m_ScrolledWindow);      m_TreeViewNotebook.set_size_request(300);
794        m_HPaned.add1(m_TreeViewNotebook);
795      m_HPaned.add2(m_Notebook);      m_HPaned.add2(m_Notebook);
796    
797    
798        m_TreeViewNotebook.append_page(m_ScrolledWindowSamples, "Samples");
799        m_TreeViewNotebook.append_page(m_ScrolledWindow, "Instruments");
800    
801    
802      actionGroup = Gtk::ActionGroup::create();      actionGroup = Gtk::ActionGroup::create();
803    
804      actionGroup->add(Gtk::Action::create("MenuFile", _("_File")));      actionGroup->add(Gtk::Action::create("MenuFile", _("_File")));
# Line 817  MainWindow::MainWindow() : Line 832  MainWindow::MainWindow() :
832      actionGroup->add(Gtk::Action::create("Quit", Gtk::Stock::QUIT),      actionGroup->add(Gtk::Action::create("Quit", Gtk::Stock::QUIT),
833                       sigc::mem_fun(                       sigc::mem_fun(
834                           *this, &MainWindow::hide));                           *this, &MainWindow::hide));
835        actionGroup->add(Gtk::Action::create("MenuInstrument", _("_Instrument")));
836    
837      action = Gtk::Action::create("MenuHelp", Gtk::Stock::HELP);      action = Gtk::Action::create("MenuHelp", Gtk::Stock::HELP);
838      actionGroup->add(Gtk::Action::create("MenuHelp",      actionGroup->add(Gtk::Action::create("MenuHelp",
839                                           action->property_label()));                                           action->property_label()));
# Line 825  MainWindow::MainWindow() : Line 842  MainWindow::MainWindow() :
842                       sigc::mem_fun(                       sigc::mem_fun(
843                           *this, &MainWindow::on_action_help_about));                           *this, &MainWindow::on_action_help_about));
844  #endif  #endif
845      action = Gtk::Action::create("Remove", "Ta bort");      action = Gtk::Action::create("Remove", Gtk::Stock::REMOVE);
846      actionGroup->add(action,      actionGroup->add(action,
847                       sigc::mem_fun(                       sigc::mem_fun(
848                           *this, &MainWindow::hide));                           *this, &MainWindow::hide));
849    
850        // sample right-click popup actions
851        actionGroup->add(
852            Gtk::Action::create("SampleProperties", Gtk::Stock::PROPERTIES),
853            sigc::mem_fun(*this, &MainWindow::on_action_sample_properties)
854        );
855        actionGroup->add(
856            Gtk::Action::create("AddGroup", _("Add _Group")),
857            sigc::mem_fun(*this, &MainWindow::on_action_add_group)
858        );
859        actionGroup->add(
860            Gtk::Action::create("AddSample", _("Add _Sample(s)")),
861            sigc::mem_fun(*this, &MainWindow::on_action_add_sample)
862        );
863        actionGroup->add(
864            Gtk::Action::create("RemoveSample", Gtk::Stock::REMOVE),
865            sigc::mem_fun(*this, &MainWindow::on_action_remove_sample)
866        );
867    
868      uiManager = Gtk::UIManager::create();      uiManager = Gtk::UIManager::create();
869      uiManager->insert_action_group(actionGroup);      uiManager->insert_action_group(actionGroup);
870      // add_accel_group(uiManager->get_accel_group());      // add_accel_group(uiManager->get_accel_group());
# Line 848  MainWindow::MainWindow() : Line 883  MainWindow::MainWindow() :
883          "      <separator/>"          "      <separator/>"
884          "      <menuitem action='Quit'/>"          "      <menuitem action='Quit'/>"
885          "    </menu>"          "    </menu>"
886            "    <menu action='MenuInstrument'>"
887            "    </menu>"
888  #ifdef ABOUT_DIALOG  #ifdef ABOUT_DIALOG
889          "    <menu action='MenuHelp'>"          "    <menu action='MenuHelp'>"
890          "      <menuitem action='About'/>"          "      <menuitem action='About'/>"
# Line 858  MainWindow::MainWindow() : Line 895  MainWindow::MainWindow() :
895          "    <menuitem action='InstrProperties'/>"          "    <menuitem action='InstrProperties'/>"
896          "    <menuitem action='Remove'/>"          "    <menuitem action='Remove'/>"
897          "  </popup>"          "  </popup>"
898            "  <popup name='SamplePopupMenu'>"
899            "    <menuitem action='SampleProperties'/>"
900            "    <menuitem action='AddGroup'/>"
901            "    <menuitem action='AddSample'/>"
902            "    <separator/>"
903            "    <menuitem action='RemoveSample'/>"
904            "  </popup>"
905          "</ui>";          "</ui>";
906      uiManager->add_ui_from_string(ui_info);      uiManager->add_ui_from_string(ui_info);
907    
# Line 883  MainWindow::MainWindow() : Line 927  MainWindow::MainWindow() :
927      m_TreeView.append_column("Instrument", m_Columns.m_col_name);      m_TreeView.append_column("Instrument", m_Columns.m_col_name);
928      m_TreeView.set_headers_visible(false);      m_TreeView.set_headers_visible(false);
929    
930        // create samples treeview (including its data model)
931        m_refSamplesTreeModel = Gtk::TreeStore::create(m_SamplesModel);
932        m_TreeViewSamples.set_model(m_refSamplesTreeModel);
933        m_TreeViewSamples.append_column("Samples", m_SamplesModel.m_col_name);
934        m_TreeViewSamples.set_headers_visible(false);
935        m_TreeViewSamples.signal_button_press_event().connect_notify(
936            sigc::mem_fun(*this, &MainWindow::on_sample_treeview_button_release)
937        );
938    
939      file = 0;      file = 0;
940    
941      show_all_children();      show_all_children();
# Line 921  void MainWindow::set_dim_region(gig::Dim Line 974  void MainWindow::set_dim_region(gig::Dim
974  {  {
975      update_gui = false;      update_gui = false;
976      wSample->set_text(d->pSample ? d->pSample->pInfo->Name.c_str() : "NULL");      wSample->set_text(d->pSample ? d->pSample->pInfo->Name.c_str() : "NULL");
     eVelocityUpperLimit.set_dimreg(d);  
977      eEG1PreAttack.set_dimreg(d);      eEG1PreAttack.set_dimreg(d);
978      eEG1Attack.set_dimreg(d);      eEG1Attack.set_dimreg(d);
979      eEG1Decay1.set_dimreg(d);      eEG1Decay1.set_dimreg(d);
# Line 1229  LoadDialog::LoadDialog() Line 1281  LoadDialog::LoadDialog()
1281    
1282  void MainWindow::on_action_file_new()  void MainWindow::on_action_file_new()
1283  {  {
1284        m_SampleImportQueue.clear();
1285  }  }
1286    
1287  void MainWindow::on_action_file_open()  void MainWindow::on_action_file_open()
# Line 1242  void MainWindow::on_action_file_open() Line 1295  void MainWindow::on_action_file_open()
1295      if (dialog.run() == Gtk::RESPONSE_OK) {      if (dialog.run() == Gtk::RESPONSE_OK) {
1296          printf("filename=%s\n", dialog.get_filename().c_str());          printf("filename=%s\n", dialog.get_filename().c_str());
1297    
1298            // remove all entries from "Instrument" menu
1299            Gtk::MenuItem* instrument_menu =
1300                dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuInstrument"));
1301            instrument_menu->hide();
1302            for (int i = 0; i < instrument_menu->get_submenu()->items().size(); i++) {
1303                delete &instrument_menu->get_submenu()->items()[i];
1304            }
1305            instrument_menu->get_submenu()->items().clear();
1306    
1307            m_SampleImportQueue.clear();
1308          m_refTreeModel->clear();          m_refTreeModel->clear();
1309            m_refSamplesTreeModel->clear();
1310          if (file) delete file;          if (file) delete file;
1311    
1312          // getInfo(dialog.get_filename().c_str(), *this);          // getInfo(dialog.get_filename().c_str(), *this);
# Line 1280  void MainWindow::on_loader_finished() Line 1344  void MainWindow::on_loader_finished()
1344    
1345  void MainWindow::on_action_file_save()  void MainWindow::on_action_file_save()
1346  {  {
1347        if (!file) return;
1348        file->Save();
1349        __import_queued_samples();
1350  }  }
1351    
1352  void MainWindow::on_action_file_save_as()  void MainWindow::on_action_file_save_as()
1353  {  {
1354        if (!file) return;
1355      Gtk::FileChooserDialog dialog(*this, "Open", Gtk::FILE_CHOOSER_ACTION_SAVE);      Gtk::FileChooserDialog dialog(*this, "Open", Gtk::FILE_CHOOSER_ACTION_SAVE);
1356      dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);      dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1357      dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);      dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);
# Line 1293  void MainWindow::on_action_file_save_as( Line 1361  void MainWindow::on_action_file_save_as(
1361      if (dialog.run() == Gtk::RESPONSE_OK) {      if (dialog.run() == Gtk::RESPONSE_OK) {
1362          printf("filename=%s\n", dialog.get_filename().c_str());          printf("filename=%s\n", dialog.get_filename().c_str());
1363          file->Save(dialog.get_filename());          file->Save(dialog.get_filename());
1364            __import_queued_samples();
1365        }
1366    }
1367    
1368    // actually write the sample(s)' data to the gig file
1369    void MainWindow::__import_queued_samples() {
1370        Glib::ustring error_files;
1371        for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin(); iter != m_SampleImportQueue.end(); ) {
1372            printf("Importing sample %s\n",(*iter).sample_path.c_str());
1373            SF_INFO info;
1374            info.format = 0;
1375            SNDFILE* hFile = sf_open((*iter).sample_path.c_str(), SFM_READ, &info);
1376            try {
1377                if (!hFile) throw std::string("could not open file");
1378                // determine sample's bit depth
1379                int bitdepth;
1380                switch (info.format & 0xff) {
1381                    case SF_FORMAT_PCM_S8:
1382                        bitdepth = 16; // we simply convert to 16 bit for now
1383                        break;
1384                    case SF_FORMAT_PCM_16:
1385                        bitdepth = 16;
1386                        break;
1387                    case SF_FORMAT_PCM_24:
1388                        bitdepth = 32; // we simply convert to 32 bit for now
1389                        break;
1390                    case SF_FORMAT_PCM_32:
1391                        bitdepth = 32;
1392                        break;
1393                    case SF_FORMAT_PCM_U8:
1394                        bitdepth = 16; // we simply convert to 16 bit for now
1395                        break;
1396                    case SF_FORMAT_FLOAT:
1397                        bitdepth = 32;
1398                        break;
1399                    case SF_FORMAT_DOUBLE:
1400                        bitdepth = 32; // I guess we will always truncate this to 32 bit
1401                        break;
1402                    default:
1403                        sf_close(hFile); // close sound file
1404                        throw std::string("format not supported"); // unsupported subformat (yet?)
1405                }
1406                // allocate appropriate copy buffer (TODO: for now we copy it in one piece, might be tough for very long samples)
1407                // and copy sample data into buffer
1408                int8_t* buffer = NULL;
1409                switch (bitdepth) {
1410                    case 16:
1411                        buffer = new int8_t[2 * info.channels * info.frames];
1412                        sf_readf_short(hFile, (short*) buffer, info.frames); // libsndfile does the conversion for us (if needed)
1413                        break;
1414                    case 32:
1415                        buffer = new int8_t[4 * info.channels * info.frames];
1416                        sf_readf_int(hFile, (int*) buffer, info.frames); // libsndfile does the conversion for us (if needed)
1417                        break;
1418                }
1419                // write from buffer directly (physically) into .gig file
1420                (*iter).gig_sample->Write(buffer, info.frames);
1421                // cleanup
1422                sf_close(hFile);
1423                delete buffer;
1424                // on success we remove the sample from the import queue, otherwise keep it, maybe it works the next time ?
1425                std::list<SampleImportItem>::iterator cur = iter;
1426                ++iter;
1427                m_SampleImportQueue.erase(cur);
1428            } catch (std::string what) { // remember the files that made trouble (and their cause)
1429                if (error_files.size()) error_files += "\n";
1430                error_files += (*iter).sample_path += " (" + what + ")";
1431                ++iter;
1432            }
1433        }
1434        // show error message box when some sample(s) could not be imported
1435        if (error_files.size()) {
1436            Glib::ustring txt = "Could not import the following sample(s):\n" + error_files;
1437            Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
1438            msg.run();
1439      }      }
1440  }  }
1441    
# Line 1474  void MainWindow::load_gig(gig::File* gig Line 1617  void MainWindow::load_gig(gig::File* gig
1617    
1618      propDialog.set_info(gig->pInfo);      propDialog.set_info(gig->pInfo);
1619    
1620        Gtk::MenuItem* instrument_menu =
1621            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuInstrument"));
1622    
1623        int instrument_index = 0;
1624        Gtk::RadioMenuItem::Group instrument_group;
1625      for (gig::Instrument* instrument = gig->GetFirstInstrument() ; instrument ;      for (gig::Instrument* instrument = gig->GetFirstInstrument() ; instrument ;
1626           instrument = gig->GetNextInstrument()) {           instrument = gig->GetNextInstrument()) {
1627          Gtk::TreeModel::iterator iter = m_refTreeModel->append();          Gtk::TreeModel::iterator iter = m_refTreeModel->append();
1628          Gtk::TreeModel::Row row = *iter;          Gtk::TreeModel::Row row = *iter;
1629          row[m_Columns.m_col_name] = instrument->pInfo->Name.c_str();          row[m_Columns.m_col_name] = instrument->pInfo->Name.c_str();
1630          row[m_Columns.m_col_instr] = instrument;          row[m_Columns.m_col_instr] = instrument;
1631            // create a menu item for this instrument
1632            Gtk::RadioMenuItem* item= new Gtk::RadioMenuItem(instrument_group, instrument->pInfo->Name.c_str());
1633            instrument_menu->get_submenu()->append(*item);
1634            item->signal_activate().connect(
1635                sigc::bind(
1636                    sigc::mem_fun(*this, &MainWindow::on_instrument_selection_change),
1637                    instrument_index
1638                )
1639            );
1640            instrument_index++;
1641        }
1642        instrument_menu->show();
1643        instrument_menu->get_submenu()->show_all_children();
1644    
1645        for (gig::Group* group = gig->GetFirstGroup(); group; group = gig->GetNextGroup()) {
1646            if (group->Name != "") {
1647                Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();
1648                Gtk::TreeModel::Row rowGroup = *iterGroup;
1649                rowGroup[m_SamplesModel.m_col_name]   = group->Name.c_str();
1650                rowGroup[m_SamplesModel.m_col_group]  = group;
1651                rowGroup[m_SamplesModel.m_col_sample] = NULL;
1652                for (gig::Sample* sample = group->GetFirstSample(); sample; sample = group->GetNextSample()) {
1653                    Gtk::TreeModel::iterator iterSample = m_refSamplesTreeModel->append(rowGroup.children());
1654                    Gtk::TreeModel::Row rowSample = *iterSample;
1655                    rowSample[m_SamplesModel.m_col_name]   = sample->pInfo->Name.c_str();
1656                    rowSample[m_SamplesModel.m_col_sample] = sample;
1657                    rowSample[m_SamplesModel.m_col_group]  = NULL;
1658                }
1659            }
1660      }      }
1661  }  }
1662    
# Line 1502  void MainWindow::on_button_release(GdkEv Line 1679  void MainWindow::on_button_release(GdkEv
1679          popup_menu->popup(button->button, button->time);          popup_menu->popup(button->button, button->time);
1680      }      }
1681  }  }
1682    
1683    void MainWindow::on_instrument_selection_change(int index) {
1684        m_RegionChooser.set_instrument(file->GetInstrument(index));
1685    }
1686    
1687    void MainWindow::on_sample_treeview_button_release(GdkEventButton* button) {
1688        if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
1689            Gtk::Menu* sample_popup =
1690                dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/SamplePopupMenu"));
1691            // update enabled/disabled state of sample popup items
1692            Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
1693            Gtk::TreeModel::iterator it = sel->get_selected();
1694            bool group_selected  = false;
1695            bool sample_selected = false;
1696            if (it) {
1697                Gtk::TreeModel::Row row = *it;
1698                group_selected  = row[m_SamplesModel.m_col_group];
1699                sample_selected = row[m_SamplesModel.m_col_sample];
1700            }
1701            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/SampleProperties"))->set_sensitive(group_selected || sample_selected);
1702            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddSample"))->set_sensitive(group_selected || sample_selected);
1703            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddGroup"))->set_sensitive(file);
1704            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/RemoveSample"))->set_sensitive(group_selected || sample_selected);
1705            // show sample popup
1706            sample_popup->popup(button->button, button->time);
1707        }
1708    }
1709    
1710    void MainWindow::on_action_sample_properties() {
1711         //TODO: show a dialog where the selected sample's properties can be edited
1712    }
1713    
1714    void MainWindow::on_action_add_group() {
1715        static int __sample_indexer = 0;
1716        if (!file) return;
1717        gig::Group* group = file->AddGroup();
1718        group->Name = "Unnamed Group";
1719        if (__sample_indexer) group->Name += " " + ToString(__sample_indexer);
1720        __sample_indexer++;
1721        // update sample tree view
1722        Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();
1723        Gtk::TreeModel::Row rowGroup = *iterGroup;
1724        rowGroup[m_SamplesModel.m_col_name] = group->Name.c_str();
1725        rowGroup[m_SamplesModel.m_col_sample] = NULL;
1726        rowGroup[m_SamplesModel.m_col_group] = group;
1727    }
1728    
1729    void MainWindow::on_action_add_sample() {
1730        if (!file) return;
1731        // get selected group
1732        Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
1733        Gtk::TreeModel::iterator it = sel->get_selected();
1734        if (!it) return;
1735        Gtk::TreeModel::Row row = *it;
1736        gig::Group* group = row[m_SamplesModel.m_col_group];
1737        if (!group) { // not a group, but a sample is selected (probably)
1738            gig::Sample* sample = row[m_SamplesModel.m_col_sample];
1739            if (!sample) return;
1740            it = row.parent(); // resolve parent (that is the sample's group)
1741            if (!it) return;
1742            row = *it;
1743            group = row[m_SamplesModel.m_col_group];
1744            if (!group) return;
1745        }
1746        // show 'browse for file' dialog
1747        Gtk::FileChooserDialog dialog(*this, _("Add Sample(s)"));
1748        dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1749        dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
1750        dialog.set_select_multiple(true);
1751        Gtk::FileFilter soundfilter; // matches all file types supported by libsndfile
1752        const char* supportedFileTypes[] = {
1753            "*.wav", "*.WAV", "*.aiff", "*.AIFF", "*.aifc", "*.AIFC", "*.snd",
1754            "*.SND", "*.au", "*.AU", "*.paf", "*.PAF", "*.iff", "*.IFF",
1755            "*.svx", "*.SVX", "*.sf", "*.SF", "*.voc", "*.VOC", "*.w64",
1756            "*.W64", "*.pvf", "*.PVF", "*.xi", "*.XI", "*.htk", "*.HTK",
1757            "*.caf", "*.CAF", NULL
1758        };
1759        for (int i = 0; supportedFileTypes[i]; i++)
1760            soundfilter.add_pattern(supportedFileTypes[i]);
1761        soundfilter.set_name("Sound Files");
1762        Gtk::FileFilter allpassfilter; // matches every file
1763        allpassfilter.add_pattern("*.*");
1764        allpassfilter.set_name("All Files");
1765        dialog.add_filter(soundfilter);
1766        dialog.add_filter(allpassfilter);
1767        if (dialog.run() == Gtk::RESPONSE_OK) {
1768            Glib::ustring error_files;
1769            Glib::SListHandle<Glib::ustring> filenames = dialog.get_filenames();
1770            for (Glib::SListHandle<Glib::ustring>::iterator iter = filenames.begin(); iter != filenames.end(); ++iter) {
1771                printf("Adding sample %s\n",(*iter).c_str());
1772                // use libsndfile to retrieve file informations
1773                SF_INFO info;
1774                info.format = 0;
1775                SNDFILE* hFile = sf_open((*iter).c_str(), SFM_READ, &info);
1776                try {
1777                    if (!hFile) throw std::string("could not open file");
1778                    int bitdepth;
1779                    switch (info.format & 0xff) {
1780                        case SF_FORMAT_PCM_S8:
1781                            bitdepth = 16; // we simply convert to 16 bit for now
1782                            break;
1783                        case SF_FORMAT_PCM_16:
1784                            bitdepth = 16;
1785                            break;
1786                        case SF_FORMAT_PCM_24:
1787                            bitdepth = 32; // we simply convert to 32 bit for now
1788                            break;
1789                        case SF_FORMAT_PCM_32:
1790                            bitdepth = 32;
1791                            break;
1792                        case SF_FORMAT_PCM_U8:
1793                            bitdepth = 16; // we simply convert to 16 bit for now
1794                            break;
1795                        case SF_FORMAT_FLOAT:
1796                            bitdepth = 32;
1797                            break;
1798                        case SF_FORMAT_DOUBLE:
1799                            bitdepth = 32; // I guess we will always truncate this to 32 bit
1800                            break;
1801                        default:
1802                            sf_close(hFile); // close sound file
1803                            throw std::string("format not supported"); // unsupported subformat (yet?)
1804                    }
1805                    // add a new sample to the .gig file
1806                    gig::Sample* sample = file->AddSample();
1807                    sample->pInfo->Name = (*iter).substr((*iter).rfind('/') + 1).raw(); // file name without path
1808                    sample->Channels = info.channels;
1809                    sample->BitDepth = bitdepth;
1810                    sample->FrameSize = bitdepth / 8/*1 byte are 8 bits*/ * info.channels;
1811                    sample->SamplesPerSecond = info.samplerate;
1812                    // schedule resizing the sample (which will be done physically when File::Save() is called)
1813                    sample->Resize(info.frames);
1814                    // make sure sample is part of the selected group
1815                    group->AddSample(sample);
1816                    // schedule that physical resize and sample import (data copying), performed when "Save" is requested
1817                    SampleImportItem sched_item;
1818                    sched_item.gig_sample  = sample;
1819                    sched_item.sample_path = *iter;
1820                    m_SampleImportQueue.push_back(sched_item);
1821                    // add sample to the tree view
1822                    Gtk::TreeModel::iterator iterSample = m_refSamplesTreeModel->append(row.children());
1823                    Gtk::TreeModel::Row rowSample = *iterSample;
1824                    rowSample[m_SamplesModel.m_col_name]   = sample->pInfo->Name.c_str();
1825                    rowSample[m_SamplesModel.m_col_sample] = sample;
1826                    rowSample[m_SamplesModel.m_col_group]  = NULL;
1827                    // close sound file
1828                    sf_close(hFile);
1829                } catch (std::string what) { // remember the files that made trouble (and their cause)
1830                    if (error_files.size()) error_files += "\n";
1831                    error_files += *iter += " (" + what + ")";
1832                }
1833            }
1834            // show error message box when some file(s) could not be opened / added
1835            if (error_files.size()) {
1836                Glib::ustring txt = "Could not add the following sample(s):\n" + error_files;
1837                Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
1838                msg.run();
1839            }
1840        }
1841    }
1842    
1843    void MainWindow::on_action_remove_sample() {
1844        if (!file) return;
1845        Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
1846        Gtk::TreeModel::iterator it = sel->get_selected();
1847        if (it) {
1848            Gtk::TreeModel::Row row = *it;
1849            gig::Group* group   = row[m_SamplesModel.m_col_group];
1850            gig::Sample* sample = row[m_SamplesModel.m_col_sample];
1851            Glib::ustring name  = row[m_SamplesModel.m_col_name];
1852            try {
1853                // remove group or sample from the gig file
1854                if (group) {
1855                    // temporarily remember the samples that bolong to that group (we need that to clean the queue)
1856                    std::list<gig::Sample*> members;
1857                    for (gig::Sample* pSample = group->GetFirstSample(); pSample; pSample = group->GetNextSample()) {
1858                        members.push_back(pSample);
1859                    }
1860                    // delete the group in the .gig file including the samples that belong to the group
1861                    file->DeleteGroup(group);
1862                    // if sample(s) were just previously added, remove them from the import queue
1863                    for (std::list<gig::Sample*>::iterator member = members.begin(); member != members.end(); ++member) {
1864                        for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin(); iter != m_SampleImportQueue.end(); ++iter) {
1865                            if ((*iter).gig_sample == *member) {
1866                                printf("Removing previously added sample '%s' from group '%s'\n", (*iter).sample_path.c_str(), name.c_str());
1867                                m_SampleImportQueue.erase(iter);
1868                                break;
1869                            }
1870                        }
1871                    }
1872                } else if (sample) {
1873                    // remove sample from the .gig file
1874                    file->DeleteSample(sample);
1875                    // if sample was just previously added, remove it from the import queue
1876                    for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin(); iter != m_SampleImportQueue.end(); ++iter) {
1877                        if ((*iter).gig_sample == sample) {
1878                            printf("Removing previously added sample '%s'\n", (*iter).sample_path.c_str());
1879                            m_SampleImportQueue.erase(iter);
1880                            break;
1881                        }
1882                    }
1883                }
1884                // remove respective row(s) from samples tree view
1885                m_refSamplesTreeModel->erase(it);
1886            } catch (RIFF::Exception e) {
1887                Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);
1888                msg.run();
1889            }
1890        }
1891    }

Legend:
Removed from v.1052  
changed lines
  Added in v.1092

  ViewVC Help
Powered by ViewVC