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

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

  ViewVC Help
Powered by ViewVC