/[svn]/libgig/trunk/src/gig.cpp
ViewVC logotype

Diff of /libgig/trunk/src/gig.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2394 by schoenebeck, Mon Jan 7 23:23:58 2013 UTC revision 2482 by schoenebeck, Mon Nov 25 02:22:38 2013 UTC
# Line 454  namespace { Line 454  namespace {
454      }      }
455    
456      /**      /**
457         * Make a (semi) deep copy of the Sample object given by @a orig (without
458         * the actual waveform data) and assign it to this object.
459         *
460         * Discussion: copying .gig samples is a bit tricky. It requires three
461         * steps:
462         * 1. Copy sample's meta informations (done by CopyAssignMeta()) including
463         *    its new sample waveform data size.
464         * 2. Saving the file (done by File::Save()) so that it gains correct size
465         *    and layout for writing the actual wave form data directly to disc
466         *    in next step.
467         * 3. Copy the waveform data with disk streaming (done by CopyAssignWave()).
468         *
469         * @param orig - original Sample object to be copied from
470         */
471        void Sample::CopyAssignMeta(const Sample* orig) {
472            // handle base classes
473            DLS::Sample::CopyAssignCore(orig);
474            
475            // handle actual own attributes of this class
476            Manufacturer = orig->Manufacturer;
477            Product = orig->Product;
478            SamplePeriod = orig->SamplePeriod;
479            MIDIUnityNote = orig->MIDIUnityNote;
480            FineTune = orig->FineTune;
481            SMPTEFormat = orig->SMPTEFormat;
482            SMPTEOffset = orig->SMPTEOffset;
483            Loops = orig->Loops;
484            LoopID = orig->LoopID;
485            LoopType = orig->LoopType;
486            LoopStart = orig->LoopStart;
487            LoopEnd = orig->LoopEnd;
488            LoopSize = orig->LoopSize;
489            LoopFraction = orig->LoopFraction;
490            LoopPlayCount = orig->LoopPlayCount;
491            
492            // schedule resizing this sample to the given sample's size
493            Resize(orig->GetSize());
494        }
495    
496        /**
497         * Should be called after CopyAssignMeta() and File::Save() sequence.
498         * Read more about it in the discussion of CopyAssignMeta(). This method
499         * copies the actual waveform data by disk streaming.
500         *
501         * @e CAUTION: this method is currently not thread safe! During this
502         * operation the sample must not be used for other purposes by other
503         * threads!
504         *
505         * @param orig - original Sample object to be copied from
506         */
507        void Sample::CopyAssignWave(const Sample* orig) {
508            const int iReadAtOnce = 32*1024;
509            char* buf = new char[iReadAtOnce * orig->FrameSize];
510            Sample* pOrig = (Sample*) orig; //HACK: remove constness for now
511            unsigned long restorePos = pOrig->GetPos();
512            pOrig->SetPos(0);
513            SetPos(0);
514            for (unsigned long n = pOrig->Read(buf, iReadAtOnce); n;
515                               n = pOrig->Read(buf, iReadAtOnce))
516            {
517                Write(buf, n);
518            }
519            pOrig->SetPos(restorePos);
520            delete [] buf;
521        }
522    
523        /**
524       * Apply sample and its settings to the respective RIFF chunks. You have       * Apply sample and its settings to the respective RIFF chunks. You have
525       * to call File::Save() to make changes persistent.       * to call File::Save() to make changes persistent.
526       *       *
# Line 808  namespace { Line 875  namespace {
875      /**      /**
876       * Returns the current position in the sample (in sample points).       * Returns the current position in the sample (in sample points).
877       */       */
878      unsigned long Sample::GetPos() {      unsigned long Sample::GetPos() const {
879          if (Compressed) return SamplePos;          if (Compressed) return SamplePos;
880          else            return pCkData->GetPos() / FrameSize;          else            return pCkData->GetPos() / FrameSize;
881      }      }
# Line 1433  namespace { Line 1500  namespace {
1500                                                          : vcf_res_ctrl_none;                                                          : vcf_res_ctrl_none;
1501              uint16_t eg3depth = _3ewa->ReadUint16();              uint16_t eg3depth = _3ewa->ReadUint16();
1502              EG3Depth = (eg3depth <= 1200) ? eg3depth /* positives */              EG3Depth = (eg3depth <= 1200) ? eg3depth /* positives */
1503                                          : (-1) * (int16_t) ((eg3depth ^ 0xffff) + 1); /* binary complementary for negatives */                                          : (-1) * (int16_t) ((eg3depth ^ 0xfff) + 1); /* binary complementary for negatives */
1504              _3ewa->ReadInt16(); // unknown              _3ewa->ReadInt16(); // unknown
1505              ChannelOffset = _3ewa->ReadUint8() / 4;              ChannelOffset = _3ewa->ReadUint8() / 4;
1506              uint8_t regoptions = _3ewa->ReadUint8();              uint8_t regoptions = _3ewa->ReadUint8();
# Line 1608  namespace { Line 1675  namespace {
1675       * @param orig - original DimensionRegion object to be copied from       * @param orig - original DimensionRegion object to be copied from
1676       */       */
1677      void DimensionRegion::CopyAssign(const DimensionRegion* orig) {      void DimensionRegion::CopyAssign(const DimensionRegion* orig) {
1678            CopyAssign(orig, NULL);
1679        }
1680    
1681        /**
1682         * Make a (semi) deep copy of the DimensionRegion object given by @a orig
1683         * and assign it to this object.
1684         *
1685         * @param orig - original DimensionRegion object to be copied from
1686         * @param mSamples - crosslink map between the foreign file's samples and
1687         *                   this file's samples
1688         */
1689        void DimensionRegion::CopyAssign(const DimensionRegion* orig, const std::map<Sample*,Sample*>* mSamples) {
1690          // delete all allocated data first          // delete all allocated data first
1691          if (VelocityTable) delete [] VelocityTable;          if (VelocityTable) delete [] VelocityTable;
1692          if (pSampleLoops) delete [] pSampleLoops;          if (pSampleLoops) delete [] pSampleLoops;
# Line 1615  namespace { Line 1694  namespace {
1694          // backup parent list pointer          // backup parent list pointer
1695          RIFF::List* p = pParentList;          RIFF::List* p = pParentList;
1696                    
1697            gig::Sample* pOriginalSample = pSample;
1698            gig::Region* pOriginalRegion = pRegion;
1699            
1700          //NOTE: copy code copied from assignment constructor above, see comment there as well          //NOTE: copy code copied from assignment constructor above, see comment there as well
1701                    
1702          *this = *orig; // default memberwise shallow copy of all parameters          *this = *orig; // default memberwise shallow copy of all parameters
1703          pParentList = p; // restore the chunk pointer          pParentList = p; // restore the chunk pointer
1704            
1705            // only take the raw sample reference & parent region reference if the
1706            // two DimensionRegion objects are part of the same file
1707            if (pOriginalRegion->GetParent()->GetParent() != orig->pRegion->GetParent()->GetParent()) {
1708                pRegion = pOriginalRegion;
1709                pSample = pOriginalSample;
1710            }
1711            
1712            if (mSamples && mSamples->count(orig->pSample)) {
1713                pSample = mSamples->find(orig->pSample)->second;
1714            }
1715    
1716          // deep copy of owned structures          // deep copy of owned structures
1717          if (orig->VelocityTable) {          if (orig->VelocityTable) {
# Line 1873  namespace { Line 1966  namespace {
1966          }          }
1967    
1968          const uint16_t eg3depth = (EG3Depth >= 0) ? EG3Depth          const uint16_t eg3depth = (EG3Depth >= 0) ? EG3Depth
1969                                                    : uint16_t(((-EG3Depth) - 1) ^ 0xffff); /* binary complementary for negatives */                                                    : uint16_t(((-EG3Depth) - 1) ^ 0xfff); /* binary complementary for negatives */
1970          store16(&pData[116], eg3depth);          store16(&pData[116], eg3depth);
1971    
1972          // next 2 bytes unknown          // next 2 bytes unknown
# Line 2972  namespace { Line 3065  namespace {
3065       * @param orig - original Region object to be copied from       * @param orig - original Region object to be copied from
3066       */       */
3067      void Region::CopyAssign(const Region* orig) {      void Region::CopyAssign(const Region* orig) {
3068            CopyAssign(orig, NULL);
3069        }
3070        
3071        /**
3072         * Make a (semi) deep copy of the Region object given by @a orig and
3073         * assign it to this object
3074         *
3075         * @param mSamples - crosslink map between the foreign file's samples and
3076         *                   this file's samples
3077         */
3078        void Region::CopyAssign(const Region* orig, const std::map<Sample*,Sample*>* mSamples) {
3079          // handle base classes          // handle base classes
3080          DLS::Region::CopyAssign(orig);          DLS::Region::CopyAssign(orig);
3081                    
3082            if (mSamples && mSamples->count((gig::Sample*)orig->pSample)) {
3083                pSample = mSamples->find((gig::Sample*)orig->pSample)->second;
3084            }
3085            
3086          // handle own member variables          // handle own member variables
3087          for (int i = Dimensions - 1; i >= 0; --i) {          for (int i = Dimensions - 1; i >= 0; --i) {
3088              DeleteDimension(&pDimensionDefinitions[i]);              DeleteDimension(&pDimensionDefinitions[i]);
# Line 2989  namespace { Line 3097  namespace {
3097          for (int i = 0; i < 256; i++) {          for (int i = 0; i < 256; i++) {
3098              if (pDimensionRegions[i] && orig->pDimensionRegions[i]) {              if (pDimensionRegions[i] && orig->pDimensionRegions[i]) {
3099                  pDimensionRegions[i]->CopyAssign(                  pDimensionRegions[i]->CopyAssign(
3100                      orig->pDimensionRegions[i]                      orig->pDimensionRegions[i],
3101                        mSamples
3102                  );                  );
3103              }              }
3104          }          }
# Line 3000  namespace { Line 3109  namespace {
3109  // *************** MidiRule ***************  // *************** MidiRule ***************
3110  // *  // *
3111    
3112  MidiRuleCtrlTrigger::MidiRuleCtrlTrigger(RIFF::Chunk* _3ewg) {      MidiRuleCtrlTrigger::MidiRuleCtrlTrigger(RIFF::Chunk* _3ewg) {
3113      _3ewg->SetPos(36);          _3ewg->SetPos(36);
3114      Triggers = _3ewg->ReadUint8();          Triggers = _3ewg->ReadUint8();
3115      _3ewg->SetPos(40);          _3ewg->SetPos(40);
3116      ControllerNumber = _3ewg->ReadUint8();          ControllerNumber = _3ewg->ReadUint8();
3117      _3ewg->SetPos(46);          _3ewg->SetPos(46);
3118      for (int i = 0 ; i < Triggers ; i++) {          for (int i = 0 ; i < Triggers ; i++) {
3119          pTriggers[i].TriggerPoint = _3ewg->ReadUint8();              pTriggers[i].TriggerPoint = _3ewg->ReadUint8();
3120          pTriggers[i].Descending = _3ewg->ReadUint8();              pTriggers[i].Descending = _3ewg->ReadUint8();
3121          pTriggers[i].VelSensitivity = _3ewg->ReadUint8();              pTriggers[i].VelSensitivity = _3ewg->ReadUint8();
3122          pTriggers[i].Key = _3ewg->ReadUint8();              pTriggers[i].Key = _3ewg->ReadUint8();
3123          pTriggers[i].NoteOff = _3ewg->ReadUint8();              pTriggers[i].NoteOff = _3ewg->ReadUint8();
3124          pTriggers[i].Velocity = _3ewg->ReadUint8();              pTriggers[i].Velocity = _3ewg->ReadUint8();
3125          pTriggers[i].OverridePedal = _3ewg->ReadUint8();              pTriggers[i].OverridePedal = _3ewg->ReadUint8();
3126          _3ewg->ReadUint8();              _3ewg->ReadUint8();
3127            }
3128        }
3129    
3130        MidiRuleCtrlTrigger::MidiRuleCtrlTrigger() :
3131            ControllerNumber(0),
3132            Triggers(0) {
3133        }
3134    
3135        void MidiRuleCtrlTrigger::UpdateChunks(uint8_t* pData) const {
3136            pData[32] = 4;
3137            pData[33] = 16;
3138            pData[36] = Triggers;
3139            pData[40] = ControllerNumber;
3140            for (int i = 0 ; i < Triggers ; i++) {
3141                pData[46 + i * 8] = pTriggers[i].TriggerPoint;
3142                pData[47 + i * 8] = pTriggers[i].Descending;
3143                pData[48 + i * 8] = pTriggers[i].VelSensitivity;
3144                pData[49 + i * 8] = pTriggers[i].Key;
3145                pData[50 + i * 8] = pTriggers[i].NoteOff;
3146                pData[51 + i * 8] = pTriggers[i].Velocity;
3147                pData[52 + i * 8] = pTriggers[i].OverridePedal;
3148            }
3149        }
3150    
3151        MidiRuleLegato::MidiRuleLegato(RIFF::Chunk* _3ewg) {
3152            _3ewg->SetPos(36);
3153            LegatoSamples = _3ewg->ReadUint8(); // always 12
3154            _3ewg->SetPos(40);
3155            BypassUseController = _3ewg->ReadUint8();
3156            BypassKey = _3ewg->ReadUint8();
3157            BypassController = _3ewg->ReadUint8();
3158            ThresholdTime = _3ewg->ReadUint16();
3159            _3ewg->ReadInt16();
3160            ReleaseTime = _3ewg->ReadUint16();
3161            _3ewg->ReadInt16();
3162            KeyRange.low = _3ewg->ReadUint8();
3163            KeyRange.high = _3ewg->ReadUint8();
3164            _3ewg->SetPos(64);
3165            ReleaseTriggerKey = _3ewg->ReadUint8();
3166            AltSustain1Key = _3ewg->ReadUint8();
3167            AltSustain2Key = _3ewg->ReadUint8();
3168        }
3169    
3170        MidiRuleLegato::MidiRuleLegato() :
3171            LegatoSamples(12),
3172            BypassUseController(false),
3173            BypassKey(0),
3174            BypassController(1),
3175            ThresholdTime(20),
3176            ReleaseTime(20),
3177            ReleaseTriggerKey(0),
3178            AltSustain1Key(0),
3179            AltSustain2Key(0)
3180        {
3181            KeyRange.low = KeyRange.high = 0;
3182        }
3183    
3184        void MidiRuleLegato::UpdateChunks(uint8_t* pData) const {
3185            pData[32] = 0;
3186            pData[33] = 16;
3187            pData[36] = LegatoSamples;
3188            pData[40] = BypassUseController;
3189            pData[41] = BypassKey;
3190            pData[42] = BypassController;
3191            store16(&pData[43], ThresholdTime);
3192            store16(&pData[47], ReleaseTime);
3193            pData[51] = KeyRange.low;
3194            pData[52] = KeyRange.high;
3195            pData[64] = ReleaseTriggerKey;
3196            pData[65] = AltSustain1Key;
3197            pData[66] = AltSustain2Key;
3198        }
3199    
3200        MidiRuleAlternator::MidiRuleAlternator(RIFF::Chunk* _3ewg) {
3201            _3ewg->SetPos(36);
3202            Articulations = _3ewg->ReadUint8();
3203            int flags = _3ewg->ReadUint8();
3204            Polyphonic = flags & 8;
3205            Chained = flags & 4;
3206            Selector = (flags & 2) ? selector_controller :
3207                (flags & 1) ? selector_key_switch : selector_none;
3208            Patterns = _3ewg->ReadUint8();
3209            _3ewg->ReadUint8(); // chosen row
3210            _3ewg->ReadUint8(); // unknown
3211            _3ewg->ReadUint8(); // unknown
3212            _3ewg->ReadUint8(); // unknown
3213            KeySwitchRange.low = _3ewg->ReadUint8();
3214            KeySwitchRange.high = _3ewg->ReadUint8();
3215            Controller = _3ewg->ReadUint8();
3216            PlayRange.low = _3ewg->ReadUint8();
3217            PlayRange.high = _3ewg->ReadUint8();
3218    
3219            int n = std::min(int(Articulations), 32);
3220            for (int i = 0 ; i < n ; i++) {
3221                _3ewg->ReadString(pArticulations[i], 32);
3222            }
3223            _3ewg->SetPos(1072);
3224            n = std::min(int(Patterns), 32);
3225            for (int i = 0 ; i < n ; i++) {
3226                _3ewg->ReadString(pPatterns[i].Name, 16);
3227                pPatterns[i].Size = _3ewg->ReadUint8();
3228                _3ewg->Read(&pPatterns[i][0], 1, 32);
3229            }
3230        }
3231    
3232        MidiRuleAlternator::MidiRuleAlternator() :
3233            Articulations(0),
3234            Patterns(0),
3235            Selector(selector_none),
3236            Controller(0),
3237            Polyphonic(false),
3238            Chained(false)
3239        {
3240            PlayRange.low = PlayRange.high = 0;
3241            KeySwitchRange.low = KeySwitchRange.high = 0;
3242      }      }
 }  
3243    
3244        void MidiRuleAlternator::UpdateChunks(uint8_t* pData) const {
3245            pData[32] = 3;
3246            pData[33] = 16;
3247            pData[36] = Articulations;
3248            pData[37] = (Polyphonic ? 8 : 0) | (Chained ? 4 : 0) |
3249                (Selector == selector_controller ? 2 :
3250                 (Selector == selector_key_switch ? 1 : 0));
3251            pData[38] = Patterns;
3252    
3253            pData[43] = KeySwitchRange.low;
3254            pData[44] = KeySwitchRange.high;
3255            pData[45] = Controller;
3256            pData[46] = PlayRange.low;
3257            pData[47] = PlayRange.high;
3258    
3259            char* str = reinterpret_cast<char*>(pData);
3260            int pos = 48;
3261            int n = std::min(int(Articulations), 32);
3262            for (int i = 0 ; i < n ; i++, pos += 32) {
3263                strncpy(&str[pos], pArticulations[i].c_str(), 32);
3264            }
3265    
3266            pos = 1072;
3267            n = std::min(int(Patterns), 32);
3268            for (int i = 0 ; i < n ; i++, pos += 49) {
3269                strncpy(&str[pos], pPatterns[i].Name.c_str(), 16);
3270                pData[pos + 16] = pPatterns[i].Size;
3271                memcpy(&pData[pos + 16], &(pPatterns[i][0]), 32);
3272            }
3273        }
3274    
3275  // *************** Instrument ***************  // *************** Instrument ***************
3276  // *  // *
# Line 3063  MidiRuleCtrlTrigger::MidiRuleCtrlTrigger Line 3316  MidiRuleCtrlTrigger::MidiRuleCtrlTrigger
3316                      uint8_t id1 = _3ewg->ReadUint8();                      uint8_t id1 = _3ewg->ReadUint8();
3317                      uint8_t id2 = _3ewg->ReadUint8();                      uint8_t id2 = _3ewg->ReadUint8();
3318    
3319                      if (id1 == 4 && id2 == 16) {                      if (id2 == 16) {
3320                          pMidiRules[i++] = new MidiRuleCtrlTrigger(_3ewg);                          if (id1 == 4) {
3321                                pMidiRules[i++] = new MidiRuleCtrlTrigger(_3ewg);
3322                            } else if (id1 == 0) {
3323                                pMidiRules[i++] = new MidiRuleLegato(_3ewg);
3324                            } else if (id1 == 3) {
3325                                pMidiRules[i++] = new MidiRuleAlternator(_3ewg);
3326                            } else {
3327                                pMidiRules[i++] = new MidiRuleUnknown;
3328                            }
3329                        }
3330                        else if (id1 != 0 || id2 != 0) {
3331                            pMidiRules[i++] = new MidiRuleUnknown;
3332                      }                      }
3333                      //TODO: all the other types of rules                      //TODO: all the other types of rules
3334    
# Line 3156  MidiRuleCtrlTrigger::MidiRuleCtrlTrigger Line 3420  MidiRuleCtrlTrigger::MidiRuleCtrlTrigger
3420                                      DimensionKeyRange.low << 1;                                      DimensionKeyRange.low << 1;
3421          pData[10] = dimkeystart;          pData[10] = dimkeystart;
3422          pData[11] = DimensionKeyRange.high;          pData[11] = DimensionKeyRange.high;
3423    
3424            if (pMidiRules[0] == 0 && _3ewg->GetSize() >= 34) {
3425                pData[32] = 0;
3426                pData[33] = 0;
3427            } else {
3428                for (int i = 0 ; pMidiRules[i] ; i++) {
3429                    pMidiRules[i]->UpdateChunks(pData);
3430                }
3431            }
3432      }      }
3433    
3434      /**      /**
# Line 3237  MidiRuleCtrlTrigger::MidiRuleCtrlTrigger Line 3510  MidiRuleCtrlTrigger::MidiRuleCtrlTrigger
3510      MidiRule* Instrument::GetMidiRule(int i) {      MidiRule* Instrument::GetMidiRule(int i) {
3511          return pMidiRules[i];          return pMidiRules[i];
3512      }      }
3513        
3514        /**
3515         * Adds the "controller trigger" MIDI rule to the instrument.
3516         *
3517         * @returns the new MIDI rule
3518         */
3519        MidiRuleCtrlTrigger* Instrument::AddMidiRuleCtrlTrigger() {
3520            delete pMidiRules[0];
3521            MidiRuleCtrlTrigger* r = new MidiRuleCtrlTrigger;
3522            pMidiRules[0] = r;
3523            pMidiRules[1] = 0;
3524            return r;
3525        }
3526    
3527        /**
3528         * Adds the legato MIDI rule to the instrument.
3529         *
3530         * @returns the new MIDI rule
3531         */
3532        MidiRuleLegato* Instrument::AddMidiRuleLegato() {
3533            delete pMidiRules[0];
3534            MidiRuleLegato* r = new MidiRuleLegato;
3535            pMidiRules[0] = r;
3536            pMidiRules[1] = 0;
3537            return r;
3538        }
3539    
3540        /**
3541         * Adds the alternator MIDI rule to the instrument.
3542         *
3543         * @returns the new MIDI rule
3544         */
3545        MidiRuleAlternator* Instrument::AddMidiRuleAlternator() {
3546            delete pMidiRules[0];
3547            MidiRuleAlternator* r = new MidiRuleAlternator;
3548            pMidiRules[0] = r;
3549            pMidiRules[1] = 0;
3550            return r;
3551        }
3552    
3553        /**
3554         * Deletes a MIDI rule from the instrument.
3555         *
3556         * @param i - MIDI rule number
3557         */
3558        void Instrument::DeleteMidiRule(int i) {
3559            delete pMidiRules[i];
3560            pMidiRules[i] = 0;
3561        }
3562    
3563      /**      /**
3564       * Make a (semi) deep copy of the Instrument object given by @a orig       * Make a (semi) deep copy of the Instrument object given by @a orig
3565       * and assign it to this object.       * and assign it to this object.
# Line 3248  MidiRuleCtrlTrigger::MidiRuleCtrlTrigger Line 3570  MidiRuleCtrlTrigger::MidiRuleCtrlTrigger
3570       * @param orig - original Instrument object to be copied from       * @param orig - original Instrument object to be copied from
3571       */       */
3572      void Instrument::CopyAssign(const Instrument* orig) {      void Instrument::CopyAssign(const Instrument* orig) {
3573            CopyAssign(orig, NULL);
3574        }
3575            
3576        /**
3577         * Make a (semi) deep copy of the Instrument object given by @a orig
3578         * and assign it to this object.
3579         *
3580         * @param orig - original Instrument object to be copied from
3581         * @param mSamples - crosslink map between the foreign file's samples and
3582         *                   this file's samples
3583         */
3584        void Instrument::CopyAssign(const Instrument* orig, const std::map<Sample*,Sample*>* mSamples) {
3585          // handle base class          // handle base class
3586          // (without copying DLS region stuff)          // (without copying DLS region stuff)
3587          DLS::Instrument::CopyAssignCore(orig);          DLS::Instrument::CopyAssignCore(orig);
# Line 3276  MidiRuleCtrlTrigger::MidiRuleCtrlTrigger Line 3610  MidiRuleCtrlTrigger::MidiRuleCtrlTrigger
3610                  Region* dstRgn = AddRegion();                  Region* dstRgn = AddRegion();
3611                  //NOTE: Region does semi-deep copy !                  //NOTE: Region does semi-deep copy !
3612                  dstRgn->CopyAssign(                  dstRgn->CopyAssign(
3613                      static_cast<gig::Region*>(*it)                      static_cast<gig::Region*>(*it),
3614                        mSamples
3615                  );                  );
3616              }              }
3617          }          }
# Line 3485  MidiRuleCtrlTrigger::MidiRuleCtrlTrigger Line 3820  MidiRuleCtrlTrigger::MidiRuleCtrlTrigger
3820          SamplesIterator++;          SamplesIterator++;
3821          return static_cast<gig::Sample*>( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL );          return static_cast<gig::Sample*>( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL );
3822      }      }
3823        
3824        /**
3825         * Returns Sample object of @a index.
3826         *
3827         * @returns sample object or NULL if index is out of bounds
3828         */
3829        Sample* File::GetSample(uint index) {
3830            if (!pSamples) LoadSamples();
3831            if (!pSamples) return NULL;
3832            DLS::File::SampleList::iterator it = pSamples->begin();
3833            for (int i = 0; i < index; ++i) {
3834                ++it;
3835                if (it == pSamples->end()) return NULL;
3836            }
3837            if (it == pSamples->end()) return NULL;
3838            return static_cast<gig::Sample*>( *it );
3839        }
3840    
3841      /** @brief Add a new sample.      /** @brief Add a new sample.
3842       *       *
# Line 3703  MidiRuleCtrlTrigger::MidiRuleCtrlTrigger Line 4055  MidiRuleCtrlTrigger::MidiRuleCtrlTrigger
4055          instr->CopyAssign(orig);          instr->CopyAssign(orig);
4056          return instr;          return instr;
4057      }      }
4058        
4059        /** @brief Add content of another existing file.
4060         *
4061         * Duplicates the samples, groups and instruments of the original file
4062         * given by @a pFile and adds them to @c this File. In case @c this File is
4063         * a new one that you haven't saved before, then you have to call
4064         * SetFileName() before calling AddContentOf(), because this method will
4065         * automatically save this file during operation, which is required for
4066         * writing the sample waveform data by disk streaming.
4067         *
4068         * @param pFile - original file whose's content shall be copied from
4069         */
4070        void File::AddContentOf(File* pFile) {
4071            static int iCallCount = -1;
4072            iCallCount++;
4073            std::map<Group*,Group*> mGroups;
4074            std::map<Sample*,Sample*> mSamples;
4075            
4076            // clone sample groups
4077            for (int i = 0; pFile->GetGroup(i); ++i) {
4078                Group* g = AddGroup();
4079                g->Name =
4080                    "COPY" + ToString(iCallCount) + "_" + pFile->GetGroup(i)->Name;
4081                mGroups[pFile->GetGroup(i)] = g;
4082            }
4083            
4084            // clone samples (not waveform data here yet)
4085            for (int i = 0; pFile->GetSample(i); ++i) {
4086                Sample* s = AddSample();
4087                s->CopyAssignMeta(pFile->GetSample(i));
4088                mGroups[pFile->GetSample(i)->GetGroup()]->AddSample(s);
4089                mSamples[pFile->GetSample(i)] = s;
4090            }
4091            
4092            //BUG: For some reason this method only works with this additional
4093            //     Save() call in between here.
4094            //
4095            // Important: The correct one of the 2 Save() methods has to be called
4096            // here, depending on whether the file is completely new or has been
4097            // saved to disk already, otherwise it will result in data corruption.
4098            if (pRIFF->IsNew())
4099                Save(GetFileName());
4100            else
4101                Save();
4102            
4103            // clone instruments
4104            // (passing the crosslink table here for the cloned samples)
4105            for (int i = 0; pFile->GetInstrument(i); ++i) {
4106                Instrument* instr = AddInstrument();
4107                instr->CopyAssign(pFile->GetInstrument(i), &mSamples);
4108            }
4109            
4110            // Mandatory: file needs to be saved to disk at this point, so this
4111            // file has the correct size and data layout for writing the samples'
4112            // waveform data to disk.
4113            Save();
4114            
4115            // clone samples' waveform data
4116            // (using direct read & write disk streaming)
4117            for (int i = 0; pFile->GetSample(i); ++i) {
4118                mSamples[pFile->GetSample(i)]->CopyAssignWave(pFile->GetSample(i));
4119            }
4120        }
4121    
4122      /** @brief Delete an instrument.      /** @brief Delete an instrument.
4123       *       *
# Line 3915  MidiRuleCtrlTrigger::MidiRuleCtrlTrigger Line 4330  MidiRuleCtrlTrigger::MidiRuleCtrlTrigger
4330    
4331          // update group's chunks          // update group's chunks
4332          if (pGroups) {          if (pGroups) {
4333              std::list<Group*>::iterator iter = pGroups->begin();              // make sure '3gri' and '3gnl' list chunks exist
4334              std::list<Group*>::iterator end  = pGroups->end();              // (before updating the Group chunks)
4335              for (; iter != end; ++iter) {              RIFF::List* _3gri = pRIFF->GetSubList(LIST_TYPE_3GRI);
4336                  (*iter)->UpdateChunks();              if (!_3gri) {
4337                    _3gri = pRIFF->AddSubList(LIST_TYPE_3GRI);
4338                    pRIFF->MoveSubChunk(_3gri, pRIFF->GetSubChunk(CHUNK_ID_PTBL));
4339              }              }
4340                RIFF::List* _3gnl = _3gri->GetSubList(LIST_TYPE_3GNL);
4341                if (!_3gnl) _3gnl = _3gri->AddSubList(LIST_TYPE_3GNL);
4342    
4343              // v3: make sure the file has 128 3gnm chunks              // v3: make sure the file has 128 3gnm chunks
4344                // (before updating the Group chunks)
4345              if (pVersion && pVersion->major == 3) {              if (pVersion && pVersion->major == 3) {
                 RIFF::List* _3gnl = pRIFF->GetSubList(LIST_TYPE_3GRI)->GetSubList(LIST_TYPE_3GNL);  
4346                  RIFF::Chunk* _3gnm = _3gnl->GetFirstSubChunk();                  RIFF::Chunk* _3gnm = _3gnl->GetFirstSubChunk();
4347                  for (int i = 0 ; i < 128 ; i++) {                  for (int i = 0 ; i < 128 ; i++) {
4348                      if (i >= pGroups->size()) ::SaveString(CHUNK_ID_3GNM, _3gnm, _3gnl, "", "", true, 64);                      if (i >= pGroups->size()) ::SaveString(CHUNK_ID_3GNM, _3gnm, _3gnl, "", "", true, 64);
4349                      if (_3gnm) _3gnm = _3gnl->GetNextSubChunk();                      if (_3gnm) _3gnm = _3gnl->GetNextSubChunk();
4350                  }                  }
4351              }              }
4352    
4353                std::list<Group*>::iterator iter = pGroups->begin();
4354                std::list<Group*>::iterator end  = pGroups->end();
4355                for (; iter != end; ++iter) {
4356                    (*iter)->UpdateChunks();
4357                }
4358          }          }
4359    
4360          // update einf chunk          // update einf chunk

Legend:
Removed from v.2394  
changed lines
  Added in v.2482

  ViewVC Help
Powered by ViewVC