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

Annotation of /gigedit/trunk/src/gigedit/CombineInstrumentsDialog.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2548 - (hide annotations) (download)
Tue May 13 12:17:43 2014 UTC (9 years, 10 months ago) by schoenebeck
File size: 28908 byte(s)
* Combine instruments: a new feature that allows to merge a selection of
  instruments to one new single instrument. It uses the 'layer' dimension
  to stack up the instruments. This feature is available from the main menu
  under 'Tools' -> 'Combine Instruments'.

1 schoenebeck 2548 /*
2     Copyright (c) 2014 Christian Schoenebeck
3    
4     This file is part of "gigedit" and released under the terms of the
5     GNU General Public License version 2.
6     */
7    
8     #include "CombineInstrumentsDialog.h"
9    
10     // enable this for debug messages being printed while combining the instruments
11     #define DEBUG_COMBINE_INSTRUMENTS 0
12    
13     #include "global.h"
14    
15     #include <set>
16     #include <iostream>
17     #include <assert.h>
18    
19     #include <glibmm/ustring.h>
20     #include <gtkmm/stock.h>
21     #include <gtkmm/messagedialog.h>
22    
23     Glib::ustring gig_to_utf8(const gig::String& gig_string);
24    
25     typedef std::map<gig::Instrument*, gig::Region*> RegionGroup;
26     typedef std::map<DLS::range_t,RegionGroup> RegionGroups;
27    
28     typedef std::vector<DLS::range_t> DimensionZones;
29     typedef std::map<gig::dimension_t,DimensionZones> Dimensions;
30    
31     typedef std::map<gig::dimension_t,int> DimensionCase;
32    
33     typedef std::map<gig::dimension_t, int> DimensionRegionUpperLimits;
34    
35     ///////////////////////////////////////////////////////////////////////////
36     // private functions
37    
38     #if DEBUG_COMBINE_INSTRUMENTS
39     static void printRanges(const RegionGroups& regions) {
40     std::cout << "{ ";
41     for (RegionGroups::const_iterator it = regions.begin(); it != regions.end(); ++it) {
42     if (it != regions.begin()) std::cout << ", ";
43     std::cout << (int)it->first.low << ".." << (int)it->first.high;
44     }
45     std::cout << " }" << std::flush;
46     }
47     #endif
48    
49     /**
50     * If the two ranges overlap, then this function returns the smallest point
51     * within that overlapping zone. If the two ranges do not overlap, then this
52     * function will return -1 instead.
53     */
54     inline int smallestOverlapPoint(const DLS::range_t& r1, const DLS::range_t& r2) {
55     if (r1.overlaps(r2.low)) return r2.low;
56     if (r2.overlaps(r1.low)) return r1.low;
57     return -1;
58     }
59    
60     /**
61     * Get the most smallest region point (not necessarily its region start point)
62     * of all regions of the given instruments, start searching at keyboard
63     * position @a iStart.
64     *
65     * @returns very first region point >= iStart, or -1 if no region could be
66     * found with a range member point >= iStart
67     */
68     static int findLowestRegionPoint(std::vector<gig::Instrument*>& instruments, int iStart) {
69     DLS::range_t searchRange = { iStart, 127 };
70     int result = -1;
71     for (uint i = 0; i < instruments.size(); ++i) {
72     gig::Instrument* instr = instruments[i];
73     for (gig::Region* rgn = instr->GetFirstRegion(); rgn; rgn = instr->GetNextRegion()) {
74     if (rgn->KeyRange.overlaps(searchRange)) {
75     int lowest = smallestOverlapPoint(rgn->KeyRange, searchRange);
76     if (result == -1 || lowest < result) result = lowest;
77     }
78     }
79     }
80     return result;
81     }
82    
83     /**
84     * Get the most smallest region end of all regions of the given instruments,
85     * start searching at keyboard position @a iStart.
86     *
87     * @returns very first region end >= iStart, or -1 if no region could be found
88     * with a range end >= iStart
89     */
90     static int findFirstRegionEnd(std::vector<gig::Instrument*>& instruments, int iStart) {
91     DLS::range_t searchRange = { iStart, 127 };
92     int result = -1;
93     for (uint i = 0; i < instruments.size(); ++i) {
94     gig::Instrument* instr = instruments[i];
95     for (gig::Region* rgn = instr->GetFirstRegion(); rgn; rgn = instr->GetNextRegion()) {
96     if (rgn->KeyRange.overlaps(searchRange)) {
97     if (result == -1 || rgn->KeyRange.high < result)
98     result = rgn->KeyRange.high;
99     }
100     }
101     }
102     return result;
103     }
104    
105     /**
106     * Returns a list of all regions of the given @a instrument where the respective
107     * region's key range overlaps the given @a range.
108     */
109     static std::vector<gig::Region*> getAllRegionsWhichOverlapRange(gig::Instrument* instrument, DLS::range_t range) {
110     //std::cout << "All regions which overlap { " << (int)range.low << ".." << (int)range.high << " } : " << std::flush;
111     std::vector<gig::Region*> v;
112     for (gig::Region* rgn = instrument->GetFirstRegion(); rgn; rgn = instrument->GetNextRegion()) {
113     if (rgn->KeyRange.overlaps(range)) {
114     v.push_back(rgn);
115     //std::cout << (int)rgn->KeyRange.low << ".." << (int)rgn->KeyRange.high << ", " << std::flush;
116     }
117     }
118     //std::cout << " END." << std::endl;
119     return v;
120     }
121    
122     /**
123     * Returns all regions of the given @a instruments where the respective region's
124     * key range overlaps the given @a range. The regions returned are ordered (in a
125     * map) by their instrument pointer.
126     */
127     static RegionGroup getAllRegionsWhichOverlapRange(std::vector<gig::Instrument*>& instruments, DLS::range_t range) {
128     RegionGroup group;
129     for (uint i = 0; i < instruments.size(); ++i) {
130     gig::Instrument* instr = instruments[i];
131     std::vector<gig::Region*> v = getAllRegionsWhichOverlapRange(instr, range);
132     if (v.empty()) continue;
133     if (v.size() > 1) {
134     std::cerr << "WARNING: More than one region found!" << std::endl;
135     }
136     group[instr] = v[0];
137     }
138     return group;
139     }
140    
141     /** @brief Identify required regions.
142     *
143     * Takes a list of @a instruments as argument (which are planned to be combined
144     * as layers in one single new instrument) and fulfills the following tasks:
145     *
146     * - 1. Identification of total amount of regions required to create a new
147     * instrument to become a layered version of the given instruments.
148     * - 2. Precise key range of each of those identified required regions to be
149     * created in that new instrument.
150     * - 3. Grouping the original source regions of the given original instruments
151     * to the respective target key range (new region) of the instrument to be
152     * created.
153     *
154     * @param instruments - list of instruments that are planned to be combined
155     * @returns structured result of the tasks described above
156     */
157     static RegionGroups groupByRegionIntersections(std::vector<gig::Instrument*>& instruments) {
158     RegionGroups groups;
159    
160     // find all region intersections of all instruments
161     std::vector<DLS::range_t> intersections;
162     for (int iStart = 0; iStart <= 127; ) {
163     iStart = findLowestRegionPoint(instruments, iStart);
164     if (iStart < 0) break;
165     const int iEnd = findFirstRegionEnd(instruments, iStart);
166     DLS::range_t range = { iStart, iEnd };
167     intersections.push_back(range);
168     iStart = iEnd + 1;
169     }
170    
171     // now sort all regions to those found intersections
172     for (uint i = 0; i < intersections.size(); ++i) {
173     const DLS::range_t& range = intersections[i];
174     RegionGroup group = getAllRegionsWhichOverlapRange(instruments, range);
175     if (!group.empty())
176     groups[range] = group;
177     else
178     std::cerr << "WARNING: empty region group!" << std::endl;
179     }
180    
181     return groups;
182     }
183    
184     /** @brief Identify required dimensions.
185     *
186     * Takes a planned new region (@a regionGroup) as argument and identifies which
187     * precise dimensions would have to be created for that new region, along with
188     * the amount of dimension zones and their precise individual zone sizes.
189     *
190     * @param regionGroup - planned new region for a new instrument
191     * @returns set of dimensions that shall be created for the given planned region
192     */
193     static Dimensions getDimensionsForRegionGroup(RegionGroup& regionGroup) {
194     std::map<gig::dimension_t, std::set<int> > dimUpperLimits;
195    
196     // collect all dimension region zones' upper limits
197     for (RegionGroup::iterator it = regionGroup.begin();
198     it != regionGroup.end(); ++it)
199     {
200     gig::Region* rgn = it->second;
201     int previousBits = 0;
202     for (uint d = 0; d < rgn->Dimensions; ++d) {
203     const gig::dimension_def_t& def = rgn->pDimensionDefinitions[d];
204     for (uint z = 0; z < def.zones; ++z) {
205     int dr = z << previousBits;
206     gig::DimensionRegion* dimRgn = rgn->pDimensionRegions[dr];
207     // Store the individual dimension zone sizes (or actually their
208     // upper limits here) for each dimension.
209     // HACK: Note that the velocity dimension is specially handled
210     // here. Instead of taking over custom velocity split sizes
211     // here, only a bogus number (zone index number) is stored for
212     // each velocity zone, that way only the maxiumum amount of
213     // velocity splits of all regions is stored here, and when their
214     // individual DimensionRegions are finally copied (later), the
215     // individual velocity split size are copied by that.
216     dimUpperLimits[def.dimension].insert(
217     (def.dimension == gig::dimension_velocity) ?
218     z : (def.split_type == gig::split_type_bit) ?
219     ((z+1) * 128/def.zones - 1) : dimRgn->DimensionUpperLimits[dr]
220     );
221     }
222     previousBits += def.bits;
223     }
224     }
225    
226     // convert upper limit set to range vector
227     Dimensions dims;
228     for (std::map<gig::dimension_t, std::set<int> >::const_iterator it = dimUpperLimits.begin();
229     it != dimUpperLimits.end(); ++it)
230     {
231     gig::dimension_t type = it->first;
232     int iLow = 0;
233     for (std::set<int>::const_iterator itNums = it->second.begin();
234     itNums != it->second.end(); ++itNums)
235     {
236     const int iUpperLimit = *itNums;
237     DLS::range_t range = { iLow, iUpperLimit };
238     dims[type].push_back(range);
239     iLow = iUpperLimit + 1;
240     }
241     }
242    
243     return dims;
244     }
245    
246     inline int getDimensionIndex(gig::dimension_t type, gig::Region* rgn) {
247     for (uint i = 0; i < rgn->Dimensions; ++i)
248     if (rgn->pDimensionDefinitions[i].dimension == type)
249     return i;
250     return -1;
251     }
252    
253     static void fillDimValues(uint* values/*[8]*/, DimensionCase dimCase, gig::Region* rgn, bool bShouldHaveAllDimensionsPassed) {
254     for (DimensionCase::iterator it = dimCase.begin(); it != dimCase.end(); ++it) {
255     gig::dimension_t type = it->first;
256     int iDimIndex = getDimensionIndex(type, rgn);
257     if (bShouldHaveAllDimensionsPassed) assert(iDimIndex >= 0);
258     else if (iDimIndex < 0) continue;
259     values[iDimIndex] = it->second;
260     }
261     }
262    
263     static DimensionRegionUpperLimits getDimensionRegionUpperLimits(gig::DimensionRegion* dimRgn) {
264     DimensionRegionUpperLimits limits;
265     gig::Region* rgn = dimRgn->GetParent();
266     for (int d = 0; d < rgn->Dimensions; ++d) {
267     const gig::dimension_def_t& def = rgn->pDimensionDefinitions[d];
268     limits[def.dimension] = dimRgn->DimensionUpperLimits[d];
269     }
270     return limits;
271     }
272    
273     static void restoreDimensionRegionUpperLimits(gig::DimensionRegion* dimRgn, const DimensionRegionUpperLimits& limits) {
274     gig::Region* rgn = dimRgn->GetParent();
275     for (DimensionRegionUpperLimits::const_iterator it = limits.begin();
276     it != limits.end(); ++it)
277     {
278     int index = getDimensionIndex(it->first, rgn);
279     assert(index >= 0);
280     dimRgn->DimensionUpperLimits[index] = it->second;
281     }
282     }
283    
284     /**
285     * Returns the sum of all bits of all dimensions defined before the given
286     * dimensions (@a type). This allows to access cases of that particular
287     * dimension directly.
288     *
289     * @param type - dimension that shall be used
290     * @param rgn - parent region of that dimension
291     */
292     inline int baseBits(gig::dimension_t type, gig::Region* rgn) {
293     int previousBits = 0;
294     for (int i = 0; i < rgn->Dimensions; ++i) {
295     if (rgn->pDimensionDefinitions[i].dimension == type) break;
296     previousBits += rgn->pDimensionDefinitions[i].bits;
297     }
298     return previousBits;
299     }
300    
301     inline int dimensionRegionIndex(gig::DimensionRegion* dimRgn) {
302     gig::Region* rgn = dimRgn->GetParent();
303     int sz = sizeof(rgn->pDimensionRegions) / sizeof(gig::DimensionRegion*);
304     for (int i = 0; i < sz; ++i)
305     if (rgn->pDimensionRegions[i] == dimRgn)
306     return i;
307     return -1;
308     }
309    
310     /** @brief Get exact zone ranges of given dimension.
311     *
312     * This function is useful for the velocity type dimension. In contrast to other
313     * dimension types, this dimension can have different zone ranges (that is
314     * different individual start and end points of its dimension zones) depending
315     * on which zones of other dimensions (on that gig::Region) are currently
316     * selected.
317     *
318     * @param type - dimension where the zone ranges should be retrieved for
319     * (usually the velocity dimension in this context)
320     * @param dimRgn - reflects the exact cases (zone selections) of all other
321     * dimensions than the given one in question
322     * @returns individual ranges for each zone of the questioned dimension type,
323     * it returns an empty result on errors instead
324     */
325     static DimensionZones preciseDimensionZonesFor(gig::dimension_t type, gig::DimensionRegion* dimRgn) {
326     DimensionZones zones;
327     gig::Region* rgn = dimRgn->GetParent();
328     int iDimension = getDimensionIndex(type, rgn);
329     if (iDimension < 0) return zones;
330     const gig::dimension_def_t& def = rgn->pDimensionDefinitions[iDimension];
331     int iDimRgn = dimensionRegionIndex(dimRgn);
332     int iBaseBits = baseBits(type, rgn);
333     int mask = ~(((1 << def.bits) - 1) << iBaseBits);
334    
335     int iLow = 0;
336     for (int z = 0; z < def.zones; ++z) {
337     gig::DimensionRegion* dimRgn2 =
338     rgn->pDimensionRegions[ (iDimRgn & mask) | ( z << iBaseBits) ];
339     int iHigh = dimRgn2->DimensionUpperLimits[iDimension];
340     DLS::range_t range = { iLow, iHigh};
341     zones.push_back(range);
342     iLow = iHigh + 1;
343     }
344     return zones;
345     }
346    
347     /** @brief Copy all DimensionRegions from source Region to target Region.
348     *
349     * Copies the entire articulation informations (including sample reference of
350     * course) from all individual DimensionRegions of source Region @a inRgn to
351     * target Region @a outRgn. There are no dimension regions created during this
352     * task. It is expected that the required dimensions (thus the required
353     * dimension regions) were already created before calling this function.
354     *
355     * To be precise, it does the task above only for the layer selected by
356     * @a iSrcLayer and @a iDstLayer. All dimensions regions of other layers that
357     * may exist, will not be copied by one single call of this function. So if
358     * there is a layer dimension, this function needs to be called several times.
359     *
360     * @param outRgn - where the dimension regions shall be copied to
361     * @param inRgn - all dimension regions that shall be copied from
362     * @param dims - dimension definitions of target region
363     * @param iDstLayer - layer number of destination region where the dimension
364     * regions shall be copied to
365     * @param iSrcLayer - layer number of the source region where the dimension
366     * regions shall be copied from
367     * @param dimCase - just for internal purpose (function recursion), don't pass
368     * anything here, this function will call itself recursively
369     * will fill this container with concrete dimension values for
370     * selecting the precise dimension regions during its task
371     */
372     static void copyDimensionRegions(gig::Region* outRgn, gig::Region* inRgn, Dimensions dims, int iDstLayer, int iSrcLayer, DimensionCase dimCase = DimensionCase()) {
373     if (dims.empty()) {
374     // resolve the respective source & destination DimensionRegion ...
375     uint srcDimValues[8] = {};
376     uint dstDimValues[8] = {};
377     DimensionCase srcDimCase = dimCase;
378     DimensionCase dstDimCase = dimCase;
379     srcDimCase[gig::dimension_layer] = iSrcLayer;
380     dstDimCase[gig::dimension_layer] = iDstLayer;
381    
382     // first select source & target dimension region with an arbitrary
383     // velocity split zone, to get access to the precise individual velocity
384     // split zone sizes (if there is actually a velocity dimension at all,
385     // otherwise we already select the desired source & target dimension
386     // region here)
387     fillDimValues(srcDimValues, srcDimCase, inRgn, false);
388     fillDimValues(dstDimValues, dstDimCase, outRgn, true);
389     gig::DimensionRegion* srcDimRgn = inRgn->GetDimensionRegionByValue(srcDimValues);
390     gig::DimensionRegion* dstDimRgn = outRgn->GetDimensionRegionByValue(dstDimValues);
391    
392     // now that we have access to the precise velocity split zone upper
393     // limits, we can select the actual source & destination dimension
394     // regions we need to copy (assuming that source or target region has
395     // a velocity dimension)
396     if (outRgn->GetDimensionDefinition(gig::dimension_velocity)) {
397     // re-select target dimension region
398     DimensionZones zones = preciseDimensionZonesFor(gig::dimension_velocity, dstDimRgn);
399     assert(zones.size() > 1);
400     const int iZoneIndex = dstDimCase[gig::dimension_velocity];
401     assert(iZoneIndex <= zones.size());
402     dstDimCase[gig::dimension_velocity] = zones[iZoneIndex].low; // arbitrary value between low and high
403     fillDimValues(dstDimValues, dstDimCase, outRgn, true);
404     dstDimRgn = outRgn->GetDimensionRegionByValue(dstDimValues);
405    
406     // re-select source dimension region
407     // (if it has a velocity dimension)
408     if (inRgn->GetDimensionDefinition(gig::dimension_velocity)) {
409     srcDimCase[gig::dimension_velocity] = zones[iZoneIndex].low; // same value as used above for target dimension region
410     fillDimValues(srcDimValues, srcDimCase, inRgn, false);
411     srcDimRgn = inRgn->GetDimensionRegionByValue(srcDimValues);
412     }
413     }
414    
415     // backup the target DimensionRegion's current dimension zones upper
416     // limits (because the target DimensionRegion's upper limits are already
417     // defined correctly since calling AddDimension(), and the CopyAssign()
418     // call next, will overwrite those upper limits unfortunately
419     DimensionRegionUpperLimits dstUpperLimits = getDimensionRegionUpperLimits(dstDimRgn);
420     DimensionRegionUpperLimits srcUpperLimits = getDimensionRegionUpperLimits(srcDimRgn);
421    
422     // copy over the selected DimensionRegion
423     const gig::Region* const origRgn = dstDimRgn->GetParent(); // just for sanity check below
424     dstDimRgn->CopyAssign(srcDimRgn);
425     assert(origRgn == dstDimRgn->GetParent());
426    
427     // restore all original dimension zone upper limits except of the
428     // velocity dimension, because the velocity dimension zone sizes are
429     // allowed to differ for individual DimensionRegions in gig v3 format
430     if (srcUpperLimits.count(gig::dimension_velocity)) {
431     assert(dstUpperLimits.count(gig::dimension_velocity));
432     dstUpperLimits[gig::dimension_velocity] = srcUpperLimits[gig::dimension_velocity];
433     }
434     restoreDimensionRegionUpperLimits(dstDimRgn, dstUpperLimits);
435    
436     return; // end of recursion
437     }
438    
439     Dimensions::iterator itDimension = dims.begin();
440    
441     gig::dimension_t type = itDimension->first;
442     DimensionZones zones = itDimension->second;
443    
444     dims.erase(itDimension);
445    
446     int iZone = 0;
447     for (DimensionZones::iterator itZone = zones.begin();
448     itZone != zones.end(); ++itZone, ++iZone)
449     {
450     DLS::range_t zoneRange = *itZone;
451     gig::dimension_def_t* def = outRgn->GetDimensionDefinition(type);
452     dimCase[type] = (def->split_type == gig::split_type_bit) ? iZone : zoneRange.low;
453     // recurse until 'dims' is exhausted (and dimCase filled up with concrete value)
454     copyDimensionRegions(outRgn, inRgn, dims, iDstLayer, iSrcLayer, dimCase);
455     }
456     }
457    
458     /** @brief Combine given list of instruments to one instrument.
459     *
460     * Takes a list of @a instruments as argument and combines them to one single
461     * new @a output instrument. For this task, it will create a 'layer' dimension
462     * in the new instrument and copies the source instruments to those layers.
463     *
464     * @param instruments - (input) list of instruments that shall be combined,
465     * they will only be read, so they will be left untouched
466     * @param gig - (input/output) .gig file where the new combined instrument shall
467     * be created
468     * @param output - (output) on success this pointer will be set to the new
469     * instrument being created
470     * @throw RIFF::Exception on any kinds of errors
471     */
472     static void combineInstruments(std::vector<gig::Instrument*>& instruments, gig::File* gig, gig::Instrument*& output) {
473     output = NULL;
474    
475     // divide the individual regions to (probably even smaller) groups of
476     // regions, coping with the fact that the source regions of the instruments
477     // might have quite different range sizes and start and end points
478     RegionGroups groups = groupByRegionIntersections(instruments);
479     #if DEBUG_COMBINE_INSTRUMENTS
480     std::cout << std::endl << "New regions: " << std::flush;
481     printRanges(groups);
482     std::cout << std::endl;
483     #endif
484    
485     if (groups.empty())
486     throw gig::Exception(_("No regions found to create a new instrument with."));
487    
488     // create a new output instrument
489     gig::Instrument* outInstr = gig->AddInstrument();
490     outInstr->pInfo->Name = "NEW COMBINATION";
491    
492     // Distinguishing in the following code block between 'horizontal' and
493     // 'vertical' regions. The 'horizontal' ones are meant to be the key ranges
494     // in the output instrument, while the 'vertical' regions are meant to be
495     // the set of source regions that shall be layered to that 'horizontal'
496     // region / key range. It is important to know, that the key ranges defined
497     // in the 'horizontal' and 'vertical' regions might differ.
498    
499     // merge the instruments to the new output instrument
500     for (RegionGroups::iterator itGroup = groups.begin();
501     itGroup != groups.end(); ++itGroup) // iterate over 'horizontal' / target regions ...
502     {
503     gig::Region* outRgn = outInstr->AddRegion();
504     outRgn->SetKeyRange(itGroup->first.low, itGroup->first.high);
505    
506     // detect the total amount of layers required to build up this combi
507     // for current key range
508     int iTotalLayers = 0;
509     for (RegionGroup::iterator itRgn = itGroup->second.begin();
510     itRgn != itGroup->second.end(); ++itRgn)
511     {
512     gig::Region* inRgn = itRgn->second;
513     iTotalLayers += inRgn->Layers;
514     }
515    
516     // create all required dimensions for this output region
517     // (except the layer dimension, which we create as next step)
518     Dimensions dims = getDimensionsForRegionGroup(itGroup->second);
519     for (Dimensions::iterator itDim = dims.begin();
520     itDim != dims.end(); ++itDim)
521     {
522     if (itDim->first == gig::dimension_layer) continue;
523    
524     gig::dimension_def_t def;
525     def.dimension = itDim->first; // dimension type
526     def.zones = itDim->second.size();
527     def.bits = zoneCountToBits(def.zones);
528     #if DEBUG_COMBINE_INSTRUMENTS
529     std::cout << "Adding new regular dimension type=" << std::hex << (int)def.dimension << std::dec << ", zones=" << (int)def.zones << ", bits=" << (int)def.bits << " ... " << std::flush;
530     #endif
531     outRgn->AddDimension(&def);
532     #if DEBUG_COMBINE_INSTRUMENTS
533     std::cout << "OK" << std::endl << std::flush;
534     #endif
535     }
536    
537     // create the layer dimension (if necessary for current key range)
538     if (iTotalLayers > 1) {
539     gig::dimension_def_t def;
540     def.dimension = gig::dimension_layer; // dimension type
541     def.zones = iTotalLayers;
542     def.bits = zoneCountToBits(def.zones);
543     #if DEBUG_COMBINE_INSTRUMENTS
544     std::cout << "Adding new (layer) dimension type=" << std::hex << (int)def.dimension << std::dec << ", zones=" << (int)def.zones << ", bits=" << (int)def.bits << " ... " << std::flush;
545     #endif
546     outRgn->AddDimension(&def);
547     #if DEBUG_COMBINE_INSTRUMENTS
548     std::cout << "OK" << std::endl << std::flush;
549     #endif
550     }
551    
552     // now copy the source dimension regions to the target dimension regions
553     int iDstLayer = 0;
554     for (RegionGroup::iterator itRgn = itGroup->second.begin();
555     itRgn != itGroup->second.end(); ++itRgn) // iterate over 'vertical' / source regions ...
556     {
557     gig::Region* inRgn = itRgn->second;
558     for (uint iSrcLayer = 0; iSrcLayer < inRgn->Layers; ++iSrcLayer, ++iDstLayer) {
559     copyDimensionRegions(outRgn, inRgn, dims, iDstLayer, iSrcLayer);
560     }
561     }
562     }
563    
564     // success
565     output = outInstr;
566     }
567    
568     ///////////////////////////////////////////////////////////////////////////
569     // class 'CombineInstrumentsDialog'
570    
571     CombineInstrumentsDialog::CombineInstrumentsDialog(Gtk::Window& parent, gig::File* gig)
572     : Gtk::Dialog(_("Combine Instruments"), parent, true),
573     m_gig(gig), m_fileWasChanged(false), m_newCombinedInstrument(NULL),
574     m_cancelButton(Gtk::Stock::CANCEL), m_OKButton(Gtk::Stock::OK),
575     m_descriptionLabel()
576     {
577     get_vbox()->pack_start(m_descriptionLabel, Gtk::PACK_SHRINK);
578     get_vbox()->pack_start(m_treeView);
579     get_vbox()->pack_start(m_buttonBox, Gtk::PACK_SHRINK);
580    
581     #if GTKMM_MAJOR_VERSION >= 3
582     description.set_line_wrap();
583     #endif
584     m_descriptionLabel.set_text(_(
585     "Select at least two instruments below that shall be combined "
586     "as layers (using a \"Layer\" dimension) to a new instrument. The "
587     "original instruments remain untouched.")
588     );
589    
590     m_refTreeModel = Gtk::ListStore::create(m_columns);
591     m_treeView.set_model(m_refTreeModel);
592     //m_treeView.set_tooltip_text(_("asdf"));
593     m_treeView.append_column("Instrument", m_columns.m_col_name);
594     m_treeView.set_headers_visible(false);
595     m_treeView.get_selection()->set_mode(Gtk::SELECTION_MULTIPLE);
596     m_treeView.get_selection()->signal_changed().connect(
597     sigc::mem_fun(*this, &CombineInstrumentsDialog::onSelectionChanged)
598     );
599     m_treeView.show();
600    
601     for (int i = 0; true; ++i) {
602     gig::Instrument* instr = gig->GetInstrument(i);
603     if (!instr) break;
604    
605     #if DEBUG_COMBINE_INSTRUMENTS
606     {
607     std::cout << "Instrument (" << i << ") '" << instr->pInfo->Name << "' Regions: " << std::flush;
608     for (gig::Region* rgn = instr->GetFirstRegion(); rgn; rgn = instr->GetNextRegion()) {
609     std::cout << rgn->KeyRange.low << ".." << rgn->KeyRange.high << ", " << std::flush;
610     }
611     std::cout << std::endl;
612     }
613     std::cout << std::endl;
614     #endif
615    
616     Glib::ustring name(gig_to_utf8(instr->pInfo->Name));
617     Gtk::TreeModel::iterator iter = m_refTreeModel->append();
618     Gtk::TreeModel::Row row = *iter;
619     row[m_columns.m_col_name] = name;
620     row[m_columns.m_col_instr] = instr;
621     }
622    
623     m_buttonBox.set_layout(Gtk::BUTTONBOX_END);
624     m_buttonBox.set_border_width(5);
625     m_buttonBox.pack_start(m_cancelButton, Gtk::PACK_SHRINK);
626     m_buttonBox.pack_start(m_OKButton, Gtk::PACK_SHRINK);
627     m_buttonBox.show();
628    
629     m_cancelButton.show();
630     m_OKButton.set_sensitive(false);
631     m_OKButton.show();
632    
633     m_cancelButton.signal_clicked().connect(
634     sigc::mem_fun(*this, &CombineInstrumentsDialog::hide)
635     );
636    
637     m_OKButton.signal_clicked().connect(
638     sigc::mem_fun(*this, &CombineInstrumentsDialog::combineSelectedInstruments)
639     );
640    
641     show_all_children();
642     }
643    
644     void CombineInstrumentsDialog::combineSelectedInstruments() {
645     std::vector<gig::Instrument*> instruments;
646     std::vector<Gtk::TreeModel::Path> v = m_treeView.get_selection()->get_selected_rows();
647     for (uint i = 0; i < v.size(); ++i) {
648     Gtk::TreeModel::iterator it = m_refTreeModel->get_iter(v[i]);
649     Gtk::TreeModel::Row row = *it;
650     Glib::ustring name = row[m_columns.m_col_name];
651     gig::Instrument* instrument = row[m_columns.m_col_instr];
652     #if DEBUG_COMBINE_INSTRUMENTS
653     printf("Selection '%s' 0x%lx\n\n", name.c_str(), int64_t((void*)instrument));
654     #endif
655     instruments.push_back(instrument);
656     }
657    
658     try {
659     combineInstruments(instruments, m_gig, m_newCombinedInstrument);
660     } catch (RIFF::Exception e) {;
661     Gtk::MessageDialog msg(*this, e.Message, false, Gtk::MESSAGE_ERROR);
662     msg.run();
663     return;
664     }
665    
666     // no error occurred
667     m_fileWasChanged = true;
668     hide();
669     }
670    
671     void CombineInstrumentsDialog::onSelectionChanged() {
672     std::vector<Gtk::TreeModel::Path> v = m_treeView.get_selection()->get_selected_rows();
673     m_OKButton.set_sensitive(v.size() >= 2);
674     }
675    
676     bool CombineInstrumentsDialog::fileWasChanged() const {
677     return m_fileWasChanged;
678     }
679    
680     gig::Instrument* CombineInstrumentsDialog::newCombinedInstrument() const {
681     return m_newCombinedInstrument;
682     }

  ViewVC Help
Powered by ViewVC