/[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 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) {  template<class T> inline std::string ToString(T o) {
# Line 386  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 516  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 530  MainWindow::MainWindow() : Line 532  MainWindow::MainWindow() :
532          sigc::mem_fun(*this, &MainWindow::on_button_release));          sigc::mem_fun(*this, &MainWindow::on_button_release));
533    
534      // Add the TreeView tab, inside a ScrolledWindow, with the button underneath:      // Add the TreeView tab, inside a ScrolledWindow, with the button underneath:
535      m_ScrolledWindow.add(m_TreeViewNotebook);      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 544  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 783  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_TreeViewSamples, "Samples");      m_TreeViewNotebook.append_page(m_ScrolledWindowSamples, "Samples");
799      m_TreeViewNotebook.append_page(m_TreeView, "Instruments");      m_TreeViewNotebook.append_page(m_ScrolledWindow, "Instruments");
800    
801    
802      actionGroup = Gtk::ActionGroup::create();      actionGroup = Gtk::ActionGroup::create();
# Line 851  MainWindow::MainWindow() : Line 857  MainWindow::MainWindow() :
857          sigc::mem_fun(*this, &MainWindow::on_action_add_group)          sigc::mem_fun(*this, &MainWindow::on_action_add_group)
858      );      );
859      actionGroup->add(      actionGroup->add(
860          Gtk::Action::create("AddSample", _("Add _Sample")),          Gtk::Action::create("AddSample", _("Add _Sample(s)")),
861          sigc::mem_fun(*this, &MainWindow::on_action_add_sample)          sigc::mem_fun(*this, &MainWindow::on_action_add_sample)
862      );      );
863      actionGroup->add(      actionGroup->add(
# Line 968  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 1276  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 1298  void MainWindow::on_action_file_open() Line 1304  void MainWindow::on_action_file_open()
1304          }          }
1305          instrument_menu->get_submenu()->items().clear();          instrument_menu->get_submenu()->items().clear();
1306    
1307            m_SampleImportQueue.clear();
1308          m_refTreeModel->clear();          m_refTreeModel->clear();
1309          m_refSamplesTreeModel->clear();          m_refSamplesTreeModel->clear();
1310          if (file) delete file;          if (file) delete file;
# Line 1337  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 1350  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 1557  void MainWindow::load_gig(gig::File* gig Line 1643  void MainWindow::load_gig(gig::File* gig
1643      instrument_menu->get_submenu()->show_all_children();      instrument_menu->get_submenu()->show_all_children();
1644    
1645      for (gig::Group* group = gig->GetFirstGroup(); group; group = gig->GetNextGroup()) {      for (gig::Group* group = gig->GetFirstGroup(); group; group = gig->GetNextGroup()) {
1646          Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();          if (group->Name != "") {
1647          Gtk::TreeModel::Row rowGroup = *iterGroup;              Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();
1648          rowGroup[m_SamplesModel.m_col_name]   = group->Name.c_str();              Gtk::TreeModel::Row rowGroup = *iterGroup;
1649          rowGroup[m_SamplesModel.m_col_group]  = group;              rowGroup[m_SamplesModel.m_col_name]   = group->Name.c_str();
1650          rowGroup[m_SamplesModel.m_col_sample] = NULL;              rowGroup[m_SamplesModel.m_col_group]  = group;
1651          for (gig::Sample* sample = group->GetFirstSample(); sample; sample = group->GetNextSample()) {              rowGroup[m_SamplesModel.m_col_sample] = NULL;
1652              Gtk::TreeModel::iterator iterSample = m_refSamplesTreeModel->append(rowGroup.children());              for (gig::Sample* sample = group->GetFirstSample(); sample; sample = group->GetNextSample()) {
1653              Gtk::TreeModel::Row rowSample = *iterSample;                  Gtk::TreeModel::iterator iterSample = m_refSamplesTreeModel->append(rowGroup.children());
1654              rowSample[m_SamplesModel.m_col_name]   = sample->pInfo->Name.c_str();                  Gtk::TreeModel::Row rowSample = *iterSample;
1655              rowSample[m_SamplesModel.m_col_sample] = sample;                  rowSample[m_SamplesModel.m_col_name]   = sample->pInfo->Name.c_str();
1656              rowSample[m_SamplesModel.m_col_group]  = NULL;                  rowSample[m_SamplesModel.m_col_sample] = sample;
1657                    rowSample[m_SamplesModel.m_col_group]  = NULL;
1658                }
1659          }          }
1660      }      }
1661  }  }
# Line 1639  void MainWindow::on_action_add_group() { Line 1727  void MainWindow::on_action_add_group() {
1727  }  }
1728    
1729  void MainWindow::on_action_add_sample() {  void MainWindow::on_action_add_sample() {
1730      //TODO: open browse for file dialog for adding new samples      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() {  void MainWindow::on_action_remove_sample() {
1844      //TODO: remove the selected group or sample      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.1082  
changed lines
  Added in v.1092

  ViewVC Help
Powered by ViewVC