/[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

revision 1082 by schoenebeck, Thu Mar 8 01:43:18 2007 UTC 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) {  template<class T> inline std::string ToString(T o) {
# Line 516  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 530  MainWindow::MainWindow() : Line 534  MainWindow::MainWindow() :
534          sigc::mem_fun(*this, &MainWindow::on_button_release));          sigc::mem_fun(*this, &MainWindow::on_button_release));
535    
536      // Add the TreeView tab, inside a ScrolledWindow, with the button underneath:      // Add the TreeView tab, inside a ScrolledWindow, with the button underneath:
537      m_ScrolledWindow.add(m_TreeViewNotebook);      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 783  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_TreeViewSamples, "Samples");      m_TreeViewNotebook.append_page(m_ScrolledWindowSamples, "Samples");
802      m_TreeViewNotebook.append_page(m_TreeView, "Instruments");      m_TreeViewNotebook.append_page(m_ScrolledWindow, "Instruments");
803    
804    
805      actionGroup = Gtk::ActionGroup::create();      actionGroup = Gtk::ActionGroup::create();
# Line 851  MainWindow::MainWindow() : Line 860  MainWindow::MainWindow() :
860          sigc::mem_fun(*this, &MainWindow::on_action_add_group)          sigc::mem_fun(*this, &MainWindow::on_action_add_group)
861      );      );
862      actionGroup->add(      actionGroup->add(
863          Gtk::Action::create("AddSample", _("Add _Sample")),          Gtk::Action::create("AddSample", _("Add _Sample(s)")),
864          sigc::mem_fun(*this, &MainWindow::on_action_add_sample)          sigc::mem_fun(*this, &MainWindow::on_action_add_sample)
865      );      );
866      actionGroup->add(      actionGroup->add(
# Line 1276  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 1298  void MainWindow::on_action_file_open() Line 1308  void MainWindow::on_action_file_open()
1308          }          }
1309          instrument_menu->get_submenu()->items().clear();          instrument_menu->get_submenu()->items().clear();
1310    
1311            m_SampleImportQueue.clear();
1312          m_refTreeModel->clear();          m_refTreeModel->clear();
1313          m_refSamplesTreeModel->clear();          m_refSamplesTreeModel->clear();
1314          if (file) delete file;          if (file) delete file;
# Line 1337  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 1350  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 1557  void MainWindow::load_gig(gig::File* gig Line 1647  void MainWindow::load_gig(gig::File* gig
1647      instrument_menu->get_submenu()->show_all_children();      instrument_menu->get_submenu()->show_all_children();
1648    
1649      for (gig::Group* group = gig->GetFirstGroup(); group; group = gig->GetNextGroup()) {      for (gig::Group* group = gig->GetFirstGroup(); group; group = gig->GetNextGroup()) {
1650          Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();          if (group->Name != "") {
1651          Gtk::TreeModel::Row rowGroup = *iterGroup;              Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();
1652          rowGroup[m_SamplesModel.m_col_name]   = group->Name.c_str();              Gtk::TreeModel::Row rowGroup = *iterGroup;
1653          rowGroup[m_SamplesModel.m_col_group]  = group;              rowGroup[m_SamplesModel.m_col_name]   = group->Name.c_str();
1654          rowGroup[m_SamplesModel.m_col_sample] = NULL;              rowGroup[m_SamplesModel.m_col_group]  = group;
1655          for (gig::Sample* sample = group->GetFirstSample(); sample; sample = group->GetNextSample()) {              rowGroup[m_SamplesModel.m_col_sample] = NULL;
1656              Gtk::TreeModel::iterator iterSample = m_refSamplesTreeModel->append(rowGroup.children());              for (gig::Sample* sample = group->GetFirstSample(); sample; sample = group->GetNextSample()) {
1657              Gtk::TreeModel::Row rowSample = *iterSample;                  Gtk::TreeModel::iterator iterSample = m_refSamplesTreeModel->append(rowGroup.children());
1658              rowSample[m_SamplesModel.m_col_name]   = sample->pInfo->Name.c_str();                  Gtk::TreeModel::Row rowSample = *iterSample;
1659              rowSample[m_SamplesModel.m_col_sample] = sample;                  rowSample[m_SamplesModel.m_col_name]   = sample->pInfo->Name.c_str();
1660              rowSample[m_SamplesModel.m_col_group]  = NULL;                  rowSample[m_SamplesModel.m_col_sample] = sample;
1661                    rowSample[m_SamplesModel.m_col_group]  = NULL;
1662                }
1663          }          }
1664      }      }
1665  }  }
# Line 1639  void MainWindow::on_action_add_group() { Line 1731  void MainWindow::on_action_add_group() {
1731  }  }
1732    
1733  void MainWindow::on_action_add_sample() {  void MainWindow::on_action_add_sample() {
1734      //TODO: open browse for file dialog for adding new samples      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() {  void MainWindow::on_action_remove_sample() {
1848      //TODO: remove the selected group or sample      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.1082  
changed lines
  Added in v.1091

  ViewVC Help
Powered by ViewVC