--- gigedit/trunk/src/gigedit/mainwindow.cpp 2007/07/22 15:07:08 1262 +++ gigedit/trunk/src/gigedit/mainwindow.cpp 2007/08/26 09:29:52 1303 @@ -31,6 +31,18 @@ #include #endif +#if (GLIBMM_MAJOR_VERSION == 2 && GLIBMM_MINOR_VERSION < 6) || GLIBMM_MAJOR_VERSION < 2 +namespace Glib { +Glib::ustring filename_display_basename(const std::string& filename) +{ + gchar* gstr = g_path_get_basename(filename.c_str()); + Glib::ustring str(gstr); + g_free(gstr); + return Glib::filename_to_utf8(str); +} +} +#endif + #include #include @@ -234,6 +246,9 @@ std::list drag_target_gig_sample; drag_target_gig_sample.push_back( Gtk::TargetEntry("gig::Sample") ); m_TreeViewSamples.drag_source_set(drag_target_gig_sample); + m_TreeViewSamples.signal_drag_begin().connect( + sigc::mem_fun(*this, &MainWindow::on_sample_treeview_drag_begin) + ); m_TreeViewSamples.signal_drag_data_get().connect( sigc::mem_fun(*this, &MainWindow::on_sample_treeview_drag_data_get) ); @@ -253,6 +268,9 @@ file_is_changed = false; show_all_children(); + + // start with a new gig file by default + on_action_file_new(); } MainWindow::~MainWindow() @@ -407,12 +425,15 @@ Glib::filename_display_basename(filename).c_str()); Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE); g_free(msg); +#if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 6) || GTKMM_MAJOR_VERSION > 2 dialog.set_secondary_text(_("If you close without saving, your changes will be lost.")); +#endif dialog.add_button(_("Close _Without Saving"), Gtk::RESPONSE_NO); dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); dialog.add_button(file_has_name ? Gtk::Stock::SAVE : Gtk::Stock::SAVE_AS, Gtk::RESPONSE_YES); dialog.set_default_response(Gtk::RESPONSE_YES); int response = dialog.run(); + dialog.hide(); if (response == Gtk::RESPONSE_YES) return file_save(); return response != Gtk::RESPONSE_CANCEL; } @@ -434,7 +455,6 @@ if (dialog.run() == Gtk::RESPONSE_OK) { std::string filename = dialog.get_filename(); printf("filename=%s\n", filename.c_str()); - __clear(); printf("on_action_file_open self=%x\n", Glib::Thread::self()); load_file(filename.c_str()); current_dir = Glib::path_get_dirname(filename); @@ -443,6 +463,7 @@ void MainWindow::load_file(const char* name) { + __clear(); load_dialog = new LoadDialog("Loading...", *this); load_dialog->show_all(); loader = new Loader(strdup(name)); @@ -483,9 +504,33 @@ file_save(); } -bool MainWindow::file_save() +bool MainWindow::check_if_savable() { if (!file) return false; + + if (!file->GetFirstSample()) { + Gtk::MessageDialog(*this, _("The file could not be saved " + "because it contains no samples"), + false, Gtk::MESSAGE_ERROR).run(); + return false; + } + + for (gig::Instrument* instrument = file->GetFirstInstrument() ; instrument ; + instrument = file->GetNextInstrument()) { + if (!instrument->GetFirstRegion()) { + Gtk::MessageDialog(*this, _("The file could not be saved " + "because there are instruments " + "that have no regions"), + false, Gtk::MESSAGE_ERROR).run(); + return false; + } + } + return true; +} + +bool MainWindow::file_save() +{ + if (!check_if_savable()) return false; if (!file_has_name) return file_save_as(); std::cout << "Saving file\n" << std::flush; @@ -508,12 +553,12 @@ void MainWindow::on_action_file_save_as() { + if (!check_if_savable()) return; file_save_as(); } bool MainWindow::file_save_as() { - if (!file) return false; Gtk::FileChooserDialog dialog(*this, _("Save as"), Gtk::FILE_CHOOSER_ACTION_SAVE); dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK); @@ -576,51 +621,60 @@ int bitdepth; switch (info.format & 0xff) { case SF_FORMAT_PCM_S8: - bitdepth = 16; // we simply convert to 16 bit for now - break; case SF_FORMAT_PCM_16: + case SF_FORMAT_PCM_U8: bitdepth = 16; break; case SF_FORMAT_PCM_24: - bitdepth = 32; // we simply convert to 32 bit for now - break; case SF_FORMAT_PCM_32: - bitdepth = 32; - break; - case SF_FORMAT_PCM_U8: - bitdepth = 16; // we simply convert to 16 bit for now - break; case SF_FORMAT_FLOAT: - bitdepth = 32; - break; case SF_FORMAT_DOUBLE: - bitdepth = 32; // I guess we will always truncate this to 32 bit + bitdepth = 24; break; default: sf_close(hFile); // close sound file throw std::string("format not supported"); // unsupported subformat (yet?) } - // allocate appropriate copy buffer (TODO: for now we copy - // it in one piece, might be tough for very long samples) - // and copy sample data into buffer - int8_t* buffer = NULL; + + const int bufsize = 10000; switch (bitdepth) { - case 16: - buffer = new int8_t[2 * info.channels * info.frames]; - // libsndfile does the conversion for us (if needed) - sf_readf_short(hFile, (short*) buffer, info.frames); + case 16: { + short* buffer = new short[bufsize * info.channels]; + sf_count_t cnt = info.frames; + while (cnt) { + // libsndfile does the conversion for us (if needed) + int n = sf_readf_short(hFile, buffer, bufsize); + // write from buffer directly (physically) into .gig file + iter->gig_sample->Write(buffer, n); + cnt -= n; + } + delete[] buffer; break; - case 32: - buffer = new int8_t[4 * info.channels * info.frames]; - // libsndfile does the conversion for us (if needed) - sf_readf_int(hFile, (int*) buffer, info.frames); + } + case 24: { + int* srcbuf = new int[bufsize * info.channels]; + uint8_t* dstbuf = new uint8_t[bufsize * 3 * info.channels]; + sf_count_t cnt = info.frames; + while (cnt) { + // libsndfile returns 32 bits, convert to 24 + int n = sf_readf_int(hFile, srcbuf, bufsize); + int j = 0; + for (int i = 0 ; i < n * info.channels ; i++) { + dstbuf[j++] = srcbuf[i] >> 8; + dstbuf[j++] = srcbuf[i] >> 16; + dstbuf[j++] = srcbuf[i] >> 24; + } + // write from buffer directly (physically) into .gig file + iter->gig_sample->Write(dstbuf, n); + cnt -= n; + } + delete[] srcbuf; + delete[] dstbuf; break; + } } - // write from buffer directly (physically) into .gig file - (*iter).gig_sample->Write(buffer, info.frames); // cleanup sf_close(hFile); - delete[] buffer; // on success we remove the sample from the import queue, // otherwise keep it, maybe it works the next time ? std::list::iterator cur = iter; @@ -1073,25 +1127,15 @@ int bitdepth; switch (info.format & 0xff) { case SF_FORMAT_PCM_S8: - bitdepth = 16; // we simply convert to 16 bit for now - break; case SF_FORMAT_PCM_16: + case SF_FORMAT_PCM_U8: bitdepth = 16; break; case SF_FORMAT_PCM_24: - bitdepth = 32; // we simply convert to 32 bit for now - break; case SF_FORMAT_PCM_32: - bitdepth = 32; - break; - case SF_FORMAT_PCM_U8: - bitdepth = 16; // we simply convert to 16 bit for now - break; case SF_FORMAT_FLOAT: - bitdepth = 32; - break; case SF_FORMAT_DOUBLE: - bitdepth = 32; // I guess we will always truncate this to 32 bit + bitdepth = 24; break; default: sf_close(hFile); // close sound file @@ -1113,6 +1157,39 @@ sample->BitDepth = bitdepth; sample->FrameSize = bitdepth / 8/*1 byte are 8 bits*/ * info.channels; sample->SamplesPerSecond = info.samplerate; + sample->AverageBytesPerSecond = sample->FrameSize * sample->SamplesPerSecond; + sample->BlockAlign = sample->FrameSize; + sample->SamplesTotal = info.frames; + + SF_INSTRUMENT instrument; + if (sf_command(hFile, SFC_GET_INSTRUMENT, + &instrument, sizeof(instrument)) != SF_FALSE) + { + sample->MIDIUnityNote = instrument.basenote; + +#if HAVE_SF_INSTRUMENT_LOOPS + if (instrument.loop_count && instrument.loops[0].mode != SF_LOOP_NONE) { + sample->Loops = 1; + + switch (instrument.loops[0].mode) { + case SF_LOOP_FORWARD: + sample->LoopType = gig::loop_type_normal; + break; + case SF_LOOP_BACKWARD: + sample->LoopType = gig::loop_type_backward; + break; + case SF_LOOP_ALTERNATING: + sample->LoopType = gig::loop_type_bidirectional; + break; + } + sample->LoopStart = instrument.loops[0].start; + sample->LoopEnd = instrument.loops[0].end; + sample->LoopPlayCount = instrument.loops[0].count; + sample->LoopSize = sample->LoopEnd - sample->LoopStart + 1; + } +#endif + } + // schedule resizing the sample (which will be done // physically when File::Save() is called) sample->Resize(info.frames); @@ -1199,6 +1276,7 @@ break; } } + dimreg_changed(); file_changed(); } // remove respective row(s) from samples tree view @@ -1210,9 +1288,21 @@ } } +// For some reason drag_data_get gets called two times for each +// drag'n'drop (at least when target is an Entry). This work-around +// makes sure the code in drag_data_get and drop_drag_data_received is +// only executed once, as drag_begin only gets called once. +void MainWindow::on_sample_treeview_drag_begin(const Glib::RefPtr& context) +{ + first_call_to_drag_data_get = true; +} + void MainWindow::on_sample_treeview_drag_data_get(const Glib::RefPtr&, Gtk::SelectionData& selection_data, guint, guint) { + if (!first_call_to_drag_data_get) return; + first_call_to_drag_data_get = false; + // get selected sample gig::Sample* sample = NULL; Glib::RefPtr sel = m_TreeViewSamples.get_selection(); @@ -1230,20 +1320,55 @@ const Glib::RefPtr& context, int, int, const Gtk::SelectionData& selection_data, guint, guint time) { - gig::DimensionRegion* dimregion = m_DimRegionChooser.get_dimregion(); gig::Sample* sample = *((gig::Sample**) selection_data.get_data()); - if (sample && dimregion && selection_data.get_length() == sizeof(gig::Sample*)) { - if (sample != dimregion->pSample) { - dimregion->pSample = sample; - dimreg_edit.wSample->set_text(dimregion->pSample->pInfo->Name.c_str()); - std::cout << "Drop received sample \"" << - dimregion->pSample->pInfo->Name.c_str() << "\"" << std::endl; - // drop success - context->drop_reply(true, time); - file_changed(); - return; + if (sample && selection_data.get_length() == sizeof(gig::Sample*)) { + std::cout << "Drop received sample \"" << + sample->pInfo->Name << "\"" << std::endl; + // drop success + context->drop_reply(true, time); + + // find the samplechannel dimension + gig::Region* region = m_RegionChooser.get_region(); + gig::dimension_def_t* stereo_dimension = 0; + for (int i = 0 ; i < region->Dimensions ; i++) { + if (region->pDimensionDefinitions[i].dimension == + gig::dimension_samplechannel) { + stereo_dimension = ®ion->pDimensionDefinitions[i]; + break; + } } + bool channels_changed = false; + if (sample->Channels == 1 && stereo_dimension) { + // remove the samplechannel dimension + region->DeleteDimension(stereo_dimension); + channels_changed = true; + region_changed(); + } + dimreg_edit.set_sample(sample); + + if (sample->Channels == 2 && !stereo_dimension) { + // add samplechannel dimension + gig::dimension_def_t dim; + dim.dimension = gig::dimension_samplechannel; + dim.bits = 1; + dim.zones = 2; + region->AddDimension(&dim); + channels_changed = true; + region_changed(); + } + if (channels_changed) { + // unmap all samples with wrong number of channels + // TODO: maybe there should be a warning dialog for this + for (int i = 0 ; i < region->DimensionRegions ; i++) { + gig::DimensionRegion* d = region->pDimensionRegions[i]; + if (d->pSample && d->pSample->Channels != sample->Channels) { + d->pSample = 0; + } + } + } + + return; } // drop failed context->drop_reply(false, time);