/[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 2484 by schoenebeck, Tue Dec 31 00:13:20 2013 UTC revision 2584 by schoenebeck, Sat May 31 20:54:39 2014 UTC
# Line 2  Line 2 
2   *                                                                         *   *                                                                         *
3   *   libgig - C++ cross-platform Gigasampler format file access library    *   *   libgig - C++ cross-platform Gigasampler format file access library    *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003-2013 by Christian Schoenebeck                      *   *   Copyright (C) 2003-2014 by Christian Schoenebeck                      *
6   *                              <cuse@users.sourceforge.net>               *   *                              <cuse@users.sourceforge.net>               *
7   *                                                                         *   *                                                                         *
8   *   This library is free software; you can redistribute it and/or modify  *   *   This library is free software; you can redistribute it and/or modify  *
# Line 28  Line 28 
28  #include <algorithm>  #include <algorithm>
29  #include <math.h>  #include <math.h>
30  #include <iostream>  #include <iostream>
31    #include <assert.h>
32    
33  /// Initial size of the sample buffer which is used for decompression of  /// Initial size of the sample buffer which is used for decompression of
34  /// compressed sample wave streams - this value should always be bigger than  /// compressed sample wave streams - this value should always be bigger than
# Line 1708  namespace { Line 1709  namespace {
1709          //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
1710                    
1711          *this = *orig; // default memberwise shallow copy of all parameters          *this = *orig; // default memberwise shallow copy of all parameters
1712            
1713            // restore members that shall not be altered
1714          pParentList = p; // restore the chunk pointer          pParentList = p; // restore the chunk pointer
1715            pRegion = pOriginalRegion;
1716                    
1717          // only take the raw sample reference & parent region reference if the          // only take the raw sample reference reference if the
1718          // two DimensionRegion objects are part of the same file          // two DimensionRegion objects are part of the same file
1719          if (pOriginalRegion->GetParent()->GetParent() != orig->pRegion->GetParent()->GetParent()) {          if (pOriginalRegion->GetParent()->GetParent() != orig->pRegion->GetParent()->GetParent()) {
             pRegion = pOriginalRegion;  
1720              pSample = pOriginalSample;              pSample = pOriginalSample;
1721          }          }
1722                    
# Line 2087  namespace { Line 2090  namespace {
2090          return pRegion;          return pRegion;
2091      }      }
2092    
2093    // show error if some _lev_ctrl_* enum entry is not listed in the following function
2094    // (commented out for now, because "diagnostic push" not supported prior GCC 4.6)
2095    // TODO: uncomment and add a GCC version check (see also commented "#pragma GCC diagnostic pop" below)
2096    //#pragma GCC diagnostic push
2097    //#pragma GCC diagnostic error "-Wswitch"
2098    
2099      leverage_ctrl_t DimensionRegion::DecodeLeverageController(_lev_ctrl_t EncodedController) {      leverage_ctrl_t DimensionRegion::DecodeLeverageController(_lev_ctrl_t EncodedController) {
2100          leverage_ctrl_t decodedcontroller;          leverage_ctrl_t decodedcontroller;
2101          switch (EncodedController) {          switch (EncodedController) {
# Line 2198  namespace { Line 2207  namespace {
2207                  decodedcontroller.controller_number = 95;                  decodedcontroller.controller_number = 95;
2208                  break;                  break;
2209    
2210                // format extension (these controllers are so far only supported by
2211                // LinuxSampler & gigedit) they will *NOT* work with
2212                // Gigasampler/GigaStudio !
2213                case _lev_ctrl_CC3_EXT:
2214                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2215                    decodedcontroller.controller_number = 3;
2216                    break;
2217                case _lev_ctrl_CC6_EXT:
2218                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2219                    decodedcontroller.controller_number = 6;
2220                    break;
2221                case _lev_ctrl_CC7_EXT:
2222                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2223                    decodedcontroller.controller_number = 7;
2224                    break;
2225                case _lev_ctrl_CC8_EXT:
2226                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2227                    decodedcontroller.controller_number = 8;
2228                    break;
2229                case _lev_ctrl_CC9_EXT:
2230                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2231                    decodedcontroller.controller_number = 9;
2232                    break;
2233                case _lev_ctrl_CC10_EXT:
2234                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2235                    decodedcontroller.controller_number = 10;
2236                    break;
2237                case _lev_ctrl_CC11_EXT:
2238                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2239                    decodedcontroller.controller_number = 11;
2240                    break;
2241                case _lev_ctrl_CC14_EXT:
2242                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2243                    decodedcontroller.controller_number = 14;
2244                    break;
2245                case _lev_ctrl_CC15_EXT:
2246                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2247                    decodedcontroller.controller_number = 15;
2248                    break;
2249                case _lev_ctrl_CC20_EXT:
2250                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2251                    decodedcontroller.controller_number = 20;
2252                    break;
2253                case _lev_ctrl_CC21_EXT:
2254                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2255                    decodedcontroller.controller_number = 21;
2256                    break;
2257                case _lev_ctrl_CC22_EXT:
2258                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2259                    decodedcontroller.controller_number = 22;
2260                    break;
2261                case _lev_ctrl_CC23_EXT:
2262                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2263                    decodedcontroller.controller_number = 23;
2264                    break;
2265                case _lev_ctrl_CC24_EXT:
2266                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2267                    decodedcontroller.controller_number = 24;
2268                    break;
2269                case _lev_ctrl_CC25_EXT:
2270                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2271                    decodedcontroller.controller_number = 25;
2272                    break;
2273                case _lev_ctrl_CC26_EXT:
2274                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2275                    decodedcontroller.controller_number = 26;
2276                    break;
2277                case _lev_ctrl_CC27_EXT:
2278                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2279                    decodedcontroller.controller_number = 27;
2280                    break;
2281                case _lev_ctrl_CC28_EXT:
2282                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2283                    decodedcontroller.controller_number = 28;
2284                    break;
2285                case _lev_ctrl_CC29_EXT:
2286                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2287                    decodedcontroller.controller_number = 29;
2288                    break;
2289                case _lev_ctrl_CC30_EXT:
2290                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2291                    decodedcontroller.controller_number = 30;
2292                    break;
2293                case _lev_ctrl_CC31_EXT:
2294                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2295                    decodedcontroller.controller_number = 31;
2296                    break;
2297                case _lev_ctrl_CC68_EXT:
2298                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2299                    decodedcontroller.controller_number = 68;
2300                    break;
2301                case _lev_ctrl_CC69_EXT:
2302                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2303                    decodedcontroller.controller_number = 69;
2304                    break;
2305                case _lev_ctrl_CC70_EXT:
2306                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2307                    decodedcontroller.controller_number = 70;
2308                    break;
2309                case _lev_ctrl_CC71_EXT:
2310                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2311                    decodedcontroller.controller_number = 71;
2312                    break;
2313                case _lev_ctrl_CC72_EXT:
2314                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2315                    decodedcontroller.controller_number = 72;
2316                    break;
2317                case _lev_ctrl_CC73_EXT:
2318                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2319                    decodedcontroller.controller_number = 73;
2320                    break;
2321                case _lev_ctrl_CC74_EXT:
2322                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2323                    decodedcontroller.controller_number = 74;
2324                    break;
2325                case _lev_ctrl_CC75_EXT:
2326                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2327                    decodedcontroller.controller_number = 75;
2328                    break;
2329                case _lev_ctrl_CC76_EXT:
2330                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2331                    decodedcontroller.controller_number = 76;
2332                    break;
2333                case _lev_ctrl_CC77_EXT:
2334                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2335                    decodedcontroller.controller_number = 77;
2336                    break;
2337                case _lev_ctrl_CC78_EXT:
2338                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2339                    decodedcontroller.controller_number = 78;
2340                    break;
2341                case _lev_ctrl_CC79_EXT:
2342                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2343                    decodedcontroller.controller_number = 79;
2344                    break;
2345                case _lev_ctrl_CC84_EXT:
2346                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2347                    decodedcontroller.controller_number = 84;
2348                    break;
2349                case _lev_ctrl_CC85_EXT:
2350                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2351                    decodedcontroller.controller_number = 85;
2352                    break;
2353                case _lev_ctrl_CC86_EXT:
2354                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2355                    decodedcontroller.controller_number = 86;
2356                    break;
2357                case _lev_ctrl_CC87_EXT:
2358                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2359                    decodedcontroller.controller_number = 87;
2360                    break;
2361                case _lev_ctrl_CC89_EXT:
2362                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2363                    decodedcontroller.controller_number = 89;
2364                    break;
2365                case _lev_ctrl_CC90_EXT:
2366                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2367                    decodedcontroller.controller_number = 90;
2368                    break;
2369                case _lev_ctrl_CC96_EXT:
2370                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2371                    decodedcontroller.controller_number = 96;
2372                    break;
2373                case _lev_ctrl_CC97_EXT:
2374                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2375                    decodedcontroller.controller_number = 97;
2376                    break;
2377                case _lev_ctrl_CC102_EXT:
2378                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2379                    decodedcontroller.controller_number = 102;
2380                    break;
2381                case _lev_ctrl_CC103_EXT:
2382                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2383                    decodedcontroller.controller_number = 103;
2384                    break;
2385                case _lev_ctrl_CC104_EXT:
2386                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2387                    decodedcontroller.controller_number = 104;
2388                    break;
2389                case _lev_ctrl_CC105_EXT:
2390                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2391                    decodedcontroller.controller_number = 105;
2392                    break;
2393                case _lev_ctrl_CC106_EXT:
2394                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2395                    decodedcontroller.controller_number = 106;
2396                    break;
2397                case _lev_ctrl_CC107_EXT:
2398                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2399                    decodedcontroller.controller_number = 107;
2400                    break;
2401                case _lev_ctrl_CC108_EXT:
2402                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2403                    decodedcontroller.controller_number = 108;
2404                    break;
2405                case _lev_ctrl_CC109_EXT:
2406                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2407                    decodedcontroller.controller_number = 109;
2408                    break;
2409                case _lev_ctrl_CC110_EXT:
2410                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2411                    decodedcontroller.controller_number = 110;
2412                    break;
2413                case _lev_ctrl_CC111_EXT:
2414                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2415                    decodedcontroller.controller_number = 111;
2416                    break;
2417                case _lev_ctrl_CC112_EXT:
2418                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2419                    decodedcontroller.controller_number = 112;
2420                    break;
2421                case _lev_ctrl_CC113_EXT:
2422                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2423                    decodedcontroller.controller_number = 113;
2424                    break;
2425                case _lev_ctrl_CC114_EXT:
2426                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2427                    decodedcontroller.controller_number = 114;
2428                    break;
2429                case _lev_ctrl_CC115_EXT:
2430                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2431                    decodedcontroller.controller_number = 115;
2432                    break;
2433                case _lev_ctrl_CC116_EXT:
2434                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2435                    decodedcontroller.controller_number = 116;
2436                    break;
2437                case _lev_ctrl_CC117_EXT:
2438                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2439                    decodedcontroller.controller_number = 117;
2440                    break;
2441                case _lev_ctrl_CC118_EXT:
2442                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2443                    decodedcontroller.controller_number = 118;
2444                    break;
2445                case _lev_ctrl_CC119_EXT:
2446                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2447                    decodedcontroller.controller_number = 119;
2448                    break;
2449    
2450              // unknown controller type              // unknown controller type
2451              default:              default:
2452                  throw gig::Exception("Unknown leverage controller type.");                  throw gig::Exception("Unknown leverage controller type.");
2453          }          }
2454          return decodedcontroller;          return decodedcontroller;
2455      }      }
2456        
2457    // see above (diagnostic push not supported prior GCC 4.6)
2458    //#pragma GCC diagnostic pop
2459    
2460      DimensionRegion::_lev_ctrl_t DimensionRegion::EncodeLeverageController(leverage_ctrl_t DecodedController) {      DimensionRegion::_lev_ctrl_t DimensionRegion::EncodeLeverageController(leverage_ctrl_t DecodedController) {
2461          _lev_ctrl_t encodedcontroller;          _lev_ctrl_t encodedcontroller;
# Line 2291  namespace { Line 2543  namespace {
2543                      case 95:                      case 95:
2544                          encodedcontroller = _lev_ctrl_effect5depth;                          encodedcontroller = _lev_ctrl_effect5depth;
2545                          break;                          break;
2546    
2547                        // format extension (these controllers are so far only
2548                        // supported by LinuxSampler & gigedit) they will *NOT*
2549                        // work with Gigasampler/GigaStudio !
2550                        case 3:
2551                            encodedcontroller = _lev_ctrl_CC3_EXT;
2552                            break;
2553                        case 6:
2554                            encodedcontroller = _lev_ctrl_CC6_EXT;
2555                            break;
2556                        case 7:
2557                            encodedcontroller = _lev_ctrl_CC7_EXT;
2558                            break;
2559                        case 8:
2560                            encodedcontroller = _lev_ctrl_CC8_EXT;
2561                            break;
2562                        case 9:
2563                            encodedcontroller = _lev_ctrl_CC9_EXT;
2564                            break;
2565                        case 10:
2566                            encodedcontroller = _lev_ctrl_CC10_EXT;
2567                            break;
2568                        case 11:
2569                            encodedcontroller = _lev_ctrl_CC11_EXT;
2570                            break;
2571                        case 14:
2572                            encodedcontroller = _lev_ctrl_CC14_EXT;
2573                            break;
2574                        case 15:
2575                            encodedcontroller = _lev_ctrl_CC15_EXT;
2576                            break;
2577                        case 20:
2578                            encodedcontroller = _lev_ctrl_CC20_EXT;
2579                            break;
2580                        case 21:
2581                            encodedcontroller = _lev_ctrl_CC21_EXT;
2582                            break;
2583                        case 22:
2584                            encodedcontroller = _lev_ctrl_CC22_EXT;
2585                            break;
2586                        case 23:
2587                            encodedcontroller = _lev_ctrl_CC23_EXT;
2588                            break;
2589                        case 24:
2590                            encodedcontroller = _lev_ctrl_CC24_EXT;
2591                            break;
2592                        case 25:
2593                            encodedcontroller = _lev_ctrl_CC25_EXT;
2594                            break;
2595                        case 26:
2596                            encodedcontroller = _lev_ctrl_CC26_EXT;
2597                            break;
2598                        case 27:
2599                            encodedcontroller = _lev_ctrl_CC27_EXT;
2600                            break;
2601                        case 28:
2602                            encodedcontroller = _lev_ctrl_CC28_EXT;
2603                            break;
2604                        case 29:
2605                            encodedcontroller = _lev_ctrl_CC29_EXT;
2606                            break;
2607                        case 30:
2608                            encodedcontroller = _lev_ctrl_CC30_EXT;
2609                            break;
2610                        case 31:
2611                            encodedcontroller = _lev_ctrl_CC31_EXT;
2612                            break;
2613                        case 68:
2614                            encodedcontroller = _lev_ctrl_CC68_EXT;
2615                            break;
2616                        case 69:
2617                            encodedcontroller = _lev_ctrl_CC69_EXT;
2618                            break;
2619                        case 70:
2620                            encodedcontroller = _lev_ctrl_CC70_EXT;
2621                            break;
2622                        case 71:
2623                            encodedcontroller = _lev_ctrl_CC71_EXT;
2624                            break;
2625                        case 72:
2626                            encodedcontroller = _lev_ctrl_CC72_EXT;
2627                            break;
2628                        case 73:
2629                            encodedcontroller = _lev_ctrl_CC73_EXT;
2630                            break;
2631                        case 74:
2632                            encodedcontroller = _lev_ctrl_CC74_EXT;
2633                            break;
2634                        case 75:
2635                            encodedcontroller = _lev_ctrl_CC75_EXT;
2636                            break;
2637                        case 76:
2638                            encodedcontroller = _lev_ctrl_CC76_EXT;
2639                            break;
2640                        case 77:
2641                            encodedcontroller = _lev_ctrl_CC77_EXT;
2642                            break;
2643                        case 78:
2644                            encodedcontroller = _lev_ctrl_CC78_EXT;
2645                            break;
2646                        case 79:
2647                            encodedcontroller = _lev_ctrl_CC79_EXT;
2648                            break;
2649                        case 84:
2650                            encodedcontroller = _lev_ctrl_CC84_EXT;
2651                            break;
2652                        case 85:
2653                            encodedcontroller = _lev_ctrl_CC85_EXT;
2654                            break;
2655                        case 86:
2656                            encodedcontroller = _lev_ctrl_CC86_EXT;
2657                            break;
2658                        case 87:
2659                            encodedcontroller = _lev_ctrl_CC87_EXT;
2660                            break;
2661                        case 89:
2662                            encodedcontroller = _lev_ctrl_CC89_EXT;
2663                            break;
2664                        case 90:
2665                            encodedcontroller = _lev_ctrl_CC90_EXT;
2666                            break;
2667                        case 96:
2668                            encodedcontroller = _lev_ctrl_CC96_EXT;
2669                            break;
2670                        case 97:
2671                            encodedcontroller = _lev_ctrl_CC97_EXT;
2672                            break;
2673                        case 102:
2674                            encodedcontroller = _lev_ctrl_CC102_EXT;
2675                            break;
2676                        case 103:
2677                            encodedcontroller = _lev_ctrl_CC103_EXT;
2678                            break;
2679                        case 104:
2680                            encodedcontroller = _lev_ctrl_CC104_EXT;
2681                            break;
2682                        case 105:
2683                            encodedcontroller = _lev_ctrl_CC105_EXT;
2684                            break;
2685                        case 106:
2686                            encodedcontroller = _lev_ctrl_CC106_EXT;
2687                            break;
2688                        case 107:
2689                            encodedcontroller = _lev_ctrl_CC107_EXT;
2690                            break;
2691                        case 108:
2692                            encodedcontroller = _lev_ctrl_CC108_EXT;
2693                            break;
2694                        case 109:
2695                            encodedcontroller = _lev_ctrl_CC109_EXT;
2696                            break;
2697                        case 110:
2698                            encodedcontroller = _lev_ctrl_CC110_EXT;
2699                            break;
2700                        case 111:
2701                            encodedcontroller = _lev_ctrl_CC111_EXT;
2702                            break;
2703                        case 112:
2704                            encodedcontroller = _lev_ctrl_CC112_EXT;
2705                            break;
2706                        case 113:
2707                            encodedcontroller = _lev_ctrl_CC113_EXT;
2708                            break;
2709                        case 114:
2710                            encodedcontroller = _lev_ctrl_CC114_EXT;
2711                            break;
2712                        case 115:
2713                            encodedcontroller = _lev_ctrl_CC115_EXT;
2714                            break;
2715                        case 116:
2716                            encodedcontroller = _lev_ctrl_CC116_EXT;
2717                            break;
2718                        case 117:
2719                            encodedcontroller = _lev_ctrl_CC117_EXT;
2720                            break;
2721                        case 118:
2722                            encodedcontroller = _lev_ctrl_CC118_EXT;
2723                            break;
2724                        case 119:
2725                            encodedcontroller = _lev_ctrl_CC119_EXT;
2726                            break;
2727    
2728                      default:                      default:
2729                          throw gig::Exception("leverage controller number is not supported by the gig format");                          throw gig::Exception("leverage controller number is not supported by the gig format");
2730                  }                  }
# Line 2622  namespace { Line 3056  namespace {
3056              memset(_3lnk->LoadChunkData(), 0, _3lnkChunkSize);              memset(_3lnk->LoadChunkData(), 0, _3lnkChunkSize);
3057    
3058              // move 3prg to last position              // move 3prg to last position
3059              pCkRegion->MoveSubChunk(pCkRegion->GetSubList(LIST_TYPE_3PRG), 0);              pCkRegion->MoveSubChunk(pCkRegion->GetSubList(LIST_TYPE_3PRG), (RIFF::Chunk*)NULL);
3060          }          }
3061    
3062          // update dimension definitions in '3lnk' chunk          // update dimension definitions in '3lnk' chunk
# Line 2768  namespace { Line 3202  namespace {
3202       *                        dimension bits limit is violated       *                        dimension bits limit is violated
3203       */       */
3204      void Region::AddDimension(dimension_def_t* pDimDef) {      void Region::AddDimension(dimension_def_t* pDimDef) {
3205            // some initial sanity checks of the given dimension definition
3206            if (pDimDef->zones < 2)
3207                throw gig::Exception("Could not add new dimension, amount of requested zones must always be at least two");
3208            if (pDimDef->bits < 1)
3209                throw gig::Exception("Could not add new dimension, amount of requested requested zone bits must always be at least one");
3210            if (pDimDef->dimension == dimension_samplechannel) {
3211                if (pDimDef->zones != 2)
3212                    throw gig::Exception("Could not add new 'sample channel' dimensions, the requested amount of zones must always be 2 for this dimension type");
3213                if (pDimDef->bits != 1)
3214                    throw gig::Exception("Could not add new 'sample channel' dimensions, the requested amount of zone bits must always be 1 for this dimension type");
3215            }
3216    
3217          // check if max. amount of dimensions reached          // check if max. amount of dimensions reached
3218          File* file = (File*) GetParent()->GetParent();          File* file = (File*) GetParent()->GetParent();
3219          const int iMaxDimensions = (file->pVersion && file->pVersion->major == 3) ? 8 : 5;          const int iMaxDimensions = (file->pVersion && file->pVersion->major == 3) ? 8 : 5;
# Line 2943  namespace { Line 3389  namespace {
3389          if (pDimDef->dimension == dimension_layer) Layers = 1;          if (pDimDef->dimension == dimension_layer) Layers = 1;
3390      }      }
3391    
3392        /** @brief Delete one split zone of a dimension (decrement zone amount).
3393         *
3394         * Instead of deleting an entire dimensions, this method will only delete
3395         * one particular split zone given by @a zone of the Region's dimension
3396         * given by @a type. So this method will simply decrement the amount of
3397         * zones by one of the dimension in question. To be able to do that, the
3398         * respective dimension must exist on this Region and it must have at least
3399         * 3 zones. All DimensionRegion objects associated with the zone will be
3400         * deleted.
3401         *
3402         * @param type - identifies the dimension where a zone shall be deleted
3403         * @param zone - index of the dimension split zone that shall be deleted
3404         * @throws gig::Exception if requested zone could not be deleted
3405         */
3406        void Region::DeleteDimensionZone(dimension_t type, int zone) {
3407            dimension_def_t* oldDef = GetDimensionDefinition(type);
3408            if (!oldDef)
3409                throw gig::Exception("Could not delete dimension zone, no such dimension of given type");
3410            if (oldDef->zones <= 2)
3411                throw gig::Exception("Could not delete dimension zone, because it would end up with only one zone.");
3412            if (zone < 0 || zone >= oldDef->zones)
3413                throw gig::Exception("Could not delete dimension zone, requested zone index out of bounds.");
3414    
3415            const int newZoneSize = oldDef->zones - 1;
3416    
3417            // create a temporary Region which just acts as a temporary copy
3418            // container and will be deleted at the end of this function and will
3419            // also not be visible through the API during this process
3420            gig::Region* tempRgn = NULL;
3421            {
3422                // adding these temporary chunks is probably not even necessary
3423                Instrument* instr = static_cast<Instrument*>(GetParent());
3424                RIFF::List* pCkInstrument = instr->pCkInstrument;
3425                RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);
3426                if (!lrgn)  lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN);
3427                RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN);
3428                tempRgn = new Region(instr, rgn);
3429            }
3430    
3431            // copy this region's dimensions (with already the dimension split size
3432            // requested by the arguments of this method call) to the temporary
3433            // region, and don't use Region::CopyAssign() here for this task, since
3434            // it would also alter fast lookup helper variables here and there
3435            dimension_def_t newDef;
3436            for (int i = 0; i < Dimensions; ++i) {
3437                dimension_def_t def = pDimensionDefinitions[i]; // copy, don't reference
3438                // is this the dimension requested by the method arguments? ...
3439                if (def.dimension == type) { // ... if yes, decrement zone amount by one
3440                    def.zones = newZoneSize;
3441                    if ((1 << (def.bits - 1)) == def.zones) def.bits--;
3442                    newDef = def;
3443                }
3444                tempRgn->AddDimension(&def);
3445            }
3446    
3447            // find the dimension index in the tempRegion which is the dimension
3448            // type passed to this method (paranoidly expecting different order)
3449            int tempReducedDimensionIndex = -1;
3450            for (int d = 0; d < tempRgn->Dimensions; ++d) {
3451                if (tempRgn->pDimensionDefinitions[d].dimension == type) {
3452                    tempReducedDimensionIndex = d;
3453                    break;
3454                }
3455            }
3456    
3457            // copy dimension regions from this region to the temporary region
3458            for (int iDst = 0; iDst < 256; ++iDst) {
3459                DimensionRegion* dstDimRgn = tempRgn->pDimensionRegions[iDst];
3460                if (!dstDimRgn) continue;
3461                std::map<dimension_t,int> dimCase;
3462                bool isValidZone = true;
3463                for (int d = 0, baseBits = 0; d < tempRgn->Dimensions; ++d) {
3464                    const int dstBits = tempRgn->pDimensionDefinitions[d].bits;
3465                    dimCase[tempRgn->pDimensionDefinitions[d].dimension] =
3466                        (iDst >> baseBits) & ((1 << dstBits) - 1);
3467                    baseBits += dstBits;
3468                    // there are also DimensionRegion objects of unused zones, skip them
3469                    if (dimCase[tempRgn->pDimensionDefinitions[d].dimension] >= tempRgn->pDimensionDefinitions[d].zones) {
3470                        isValidZone = false;
3471                        break;
3472                    }
3473                }
3474                if (!isValidZone) continue;
3475                // a bit paranoid: cope with the chance that the dimensions would
3476                // have different order in source and destination regions
3477                const bool isLastZone = (dimCase[type] == newZoneSize - 1);
3478                if (dimCase[type] >= zone) dimCase[type]++;
3479                DimensionRegion* srcDimRgn = GetDimensionRegionByBit(dimCase);
3480                dstDimRgn->CopyAssign(srcDimRgn);
3481                // if this is the upper most zone of the dimension passed to this
3482                // method, then correct (raise) its upper limit to 127
3483                if (newDef.split_type == split_type_normal && isLastZone)
3484                    dstDimRgn->DimensionUpperLimits[tempReducedDimensionIndex] = 127;
3485            }
3486    
3487            // now tempRegion's dimensions and DimensionRegions basically reflect
3488            // what we wanted to get for this actual Region here, so we now just
3489            // delete and recreate the dimension in question with the new amount
3490            // zones and then copy back from tempRegion      
3491            DeleteDimension(oldDef);
3492            AddDimension(&newDef);
3493            for (int iSrc = 0; iSrc < 256; ++iSrc) {
3494                DimensionRegion* srcDimRgn = tempRgn->pDimensionRegions[iSrc];
3495                if (!srcDimRgn) continue;
3496                std::map<dimension_t,int> dimCase;
3497                for (int d = 0, baseBits = 0; d < tempRgn->Dimensions; ++d) {
3498                    const int srcBits = tempRgn->pDimensionDefinitions[d].bits;
3499                    dimCase[tempRgn->pDimensionDefinitions[d].dimension] =
3500                        (iSrc >> baseBits) & ((1 << srcBits) - 1);
3501                    baseBits += srcBits;
3502                }
3503                // a bit paranoid: cope with the chance that the dimensions would
3504                // have different order in source and destination regions
3505                DimensionRegion* dstDimRgn = GetDimensionRegionByBit(dimCase);
3506                if (!dstDimRgn) continue;
3507                dstDimRgn->CopyAssign(srcDimRgn);
3508            }
3509    
3510            // delete temporary region
3511            delete tempRgn;
3512    
3513            UpdateVelocityTable();
3514        }
3515    
3516        /** @brief Divide split zone of a dimension in two (increment zone amount).
3517         *
3518         * This will increment the amount of zones for the dimension (given by
3519         * @a type) by one. It will do so by dividing the zone (given by @a zone)
3520         * in the middle of its zone range in two. So the two zones resulting from
3521         * the zone being splitted, will be an equivalent copy regarding all their
3522         * articulation informations and sample reference. The two zones will only
3523         * differ in their zone's upper limit
3524         * (DimensionRegion::DimensionUpperLimits).
3525         *
3526         * @param type - identifies the dimension where a zone shall be splitted
3527         * @param zone - index of the dimension split zone that shall be splitted
3528         * @throws gig::Exception if requested zone could not be splitted
3529         */
3530        void Region::SplitDimensionZone(dimension_t type, int zone) {
3531            dimension_def_t* oldDef = GetDimensionDefinition(type);
3532            if (!oldDef)
3533                throw gig::Exception("Could not split dimension zone, no such dimension of given type");
3534            if (zone < 0 || zone >= oldDef->zones)
3535                throw gig::Exception("Could not split dimension zone, requested zone index out of bounds.");
3536    
3537            const int newZoneSize = oldDef->zones + 1;
3538    
3539            // create a temporary Region which just acts as a temporary copy
3540            // container and will be deleted at the end of this function and will
3541            // also not be visible through the API during this process
3542            gig::Region* tempRgn = NULL;
3543            {
3544                // adding these temporary chunks is probably not even necessary
3545                Instrument* instr = static_cast<Instrument*>(GetParent());
3546                RIFF::List* pCkInstrument = instr->pCkInstrument;
3547                RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);
3548                if (!lrgn)  lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN);
3549                RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN);
3550                tempRgn = new Region(instr, rgn);
3551            }
3552    
3553            // copy this region's dimensions (with already the dimension split size
3554            // requested by the arguments of this method call) to the temporary
3555            // region, and don't use Region::CopyAssign() here for this task, since
3556            // it would also alter fast lookup helper variables here and there
3557            dimension_def_t newDef;
3558            for (int i = 0; i < Dimensions; ++i) {
3559                dimension_def_t def = pDimensionDefinitions[i]; // copy, don't reference
3560                // is this the dimension requested by the method arguments? ...
3561                if (def.dimension == type) { // ... if yes, increment zone amount by one
3562                    def.zones = newZoneSize;
3563                    if ((1 << oldDef->bits) < newZoneSize) def.bits++;
3564                    newDef = def;
3565                }
3566                tempRgn->AddDimension(&def);
3567            }
3568    
3569            // find the dimension index in the tempRegion which is the dimension
3570            // type passed to this method (paranoidly expecting different order)
3571            int tempIncreasedDimensionIndex = -1;
3572            for (int d = 0; d < tempRgn->Dimensions; ++d) {
3573                if (tempRgn->pDimensionDefinitions[d].dimension == type) {
3574                    tempIncreasedDimensionIndex = d;
3575                    break;
3576                }
3577            }
3578    
3579            // copy dimension regions from this region to the temporary region
3580            for (int iSrc = 0; iSrc < 256; ++iSrc) {
3581                DimensionRegion* srcDimRgn = pDimensionRegions[iSrc];
3582                if (!srcDimRgn) continue;
3583                std::map<dimension_t,int> dimCase;
3584                bool isValidZone = true;
3585                for (int d = 0, baseBits = 0; d < Dimensions; ++d) {
3586                    const int srcBits = pDimensionDefinitions[d].bits;
3587                    dimCase[pDimensionDefinitions[d].dimension] =
3588                        (iSrc >> baseBits) & ((1 << srcBits) - 1);
3589                    // there are also DimensionRegion objects for unused zones, skip them
3590                    if (dimCase[pDimensionDefinitions[d].dimension] >= pDimensionDefinitions[d].zones) {
3591                        isValidZone = false;
3592                        break;
3593                    }
3594                    baseBits += srcBits;
3595                }
3596                if (!isValidZone) continue;
3597                // a bit paranoid: cope with the chance that the dimensions would
3598                // have different order in source and destination regions            
3599                if (dimCase[type] > zone) dimCase[type]++;
3600                DimensionRegion* dstDimRgn = tempRgn->GetDimensionRegionByBit(dimCase);
3601                dstDimRgn->CopyAssign(srcDimRgn);
3602                // if this is the requested zone to be splitted, then also copy
3603                // the source DimensionRegion to the newly created target zone
3604                // and set the old zones upper limit lower
3605                if (dimCase[type] == zone) {
3606                    // lower old zones upper limit
3607                    if (newDef.split_type == split_type_normal) {
3608                        const int high =
3609                            dstDimRgn->DimensionUpperLimits[tempIncreasedDimensionIndex];
3610                        int low = 0;
3611                        if (zone > 0) {
3612                            std::map<dimension_t,int> lowerCase = dimCase;
3613                            lowerCase[type]--;
3614                            DimensionRegion* dstDimRgnLow = tempRgn->GetDimensionRegionByBit(lowerCase);
3615                            low = dstDimRgnLow->DimensionUpperLimits[tempIncreasedDimensionIndex];
3616                        }
3617                        dstDimRgn->DimensionUpperLimits[tempIncreasedDimensionIndex] = low + (high - low) / 2;
3618                    }
3619                    // fill the newly created zone of the divided zone as well
3620                    dimCase[type]++;
3621                    dstDimRgn = tempRgn->GetDimensionRegionByBit(dimCase);
3622                    dstDimRgn->CopyAssign(srcDimRgn);
3623                }
3624            }
3625    
3626            // now tempRegion's dimensions and DimensionRegions basically reflect
3627            // what we wanted to get for this actual Region here, so we now just
3628            // delete and recreate the dimension in question with the new amount
3629            // zones and then copy back from tempRegion      
3630            DeleteDimension(oldDef);
3631            AddDimension(&newDef);
3632            for (int iSrc = 0; iSrc < 256; ++iSrc) {
3633                DimensionRegion* srcDimRgn = tempRgn->pDimensionRegions[iSrc];
3634                if (!srcDimRgn) continue;
3635                std::map<dimension_t,int> dimCase;
3636                for (int d = 0, baseBits = 0; d < tempRgn->Dimensions; ++d) {
3637                    const int srcBits = tempRgn->pDimensionDefinitions[d].bits;
3638                    dimCase[tempRgn->pDimensionDefinitions[d].dimension] =
3639                        (iSrc >> baseBits) & ((1 << srcBits) - 1);
3640                    baseBits += srcBits;
3641                }
3642                // a bit paranoid: cope with the chance that the dimensions would
3643                // have different order in source and destination regions
3644                DimensionRegion* dstDimRgn = GetDimensionRegionByBit(dimCase);
3645                if (!dstDimRgn) continue;
3646                dstDimRgn->CopyAssign(srcDimRgn);
3647            }
3648    
3649            // delete temporary region
3650            delete tempRgn;
3651    
3652            UpdateVelocityTable();
3653        }
3654    
3655        DimensionRegion* Region::GetDimensionRegionByBit(const std::map<dimension_t,int>& DimCase) {
3656            uint8_t bits[8] = {};
3657            for (std::map<dimension_t,int>::const_iterator it = DimCase.begin();
3658                 it != DimCase.end(); ++it)
3659            {
3660                for (int d = 0; d < Dimensions; ++d) {
3661                    if (pDimensionDefinitions[d].dimension == it->first) {
3662                        bits[d] = it->second;
3663                        goto nextDimCaseSlice;
3664                    }
3665                }
3666                assert(false); // do crash ... too harsh maybe ? ignore it instead ?
3667                nextDimCaseSlice:
3668                ; // noop
3669            }
3670            return GetDimensionRegionByBit(bits);
3671        }
3672    
3673        /**
3674         * Searches in the current Region for a dimension of the given dimension
3675         * type and returns the precise configuration of that dimension in this
3676         * Region.
3677         *
3678         * @param type - dimension type of the sought dimension
3679         * @returns dimension definition or NULL if there is no dimension with
3680         *          sought type in this Region.
3681         */
3682        dimension_def_t* Region::GetDimensionDefinition(dimension_t type) {
3683            for (int i = 0; i < Dimensions; ++i)
3684                if (pDimensionDefinitions[i].dimension == type)
3685                    return &pDimensionDefinitions[i];
3686            return NULL;
3687        }
3688    
3689      Region::~Region() {      Region::~Region() {
3690          for (int i = 0; i < 256; i++) {          for (int i = 0; i < 256; i++) {
3691              if (pDimensionRegions[i]) delete pDimensionRegions[i];              if (pDimensionRegions[i]) delete pDimensionRegions[i];
# Line 3000  namespace { Line 3743  namespace {
3743              }              }
3744              bitpos += pDimensionDefinitions[i].bits;              bitpos += pDimensionDefinitions[i].bits;
3745          }          }
3746          DimensionRegion* dimreg = pDimensionRegions[dimregidx];          DimensionRegion* dimreg = pDimensionRegions[dimregidx & 255];
3747            if (!dimreg) return NULL;
3748          if (veldim != -1) {          if (veldim != -1) {
3749              // (dimreg is now the dimension region for the lowest velocity)              // (dimreg is now the dimension region for the lowest velocity)
3750              if (dimreg->VelocityTable) // custom defined zone ranges              if (dimreg->VelocityTable) // custom defined zone ranges
3751                  bits = dimreg->VelocityTable[DimValues[veldim]];                  bits = dimreg->VelocityTable[DimValues[veldim] & 127];
3752              else // normal split type              else // normal split type
3753                  bits = uint8_t(DimValues[veldim] / pDimensionDefinitions[veldim].zone_size);                  bits = uint8_t((DimValues[veldim] & 127) / pDimensionDefinitions[veldim].zone_size);
3754    
3755              dimregidx |= bits << velbitpos;              const uint8_t limiter_mask = (1 << pDimensionDefinitions[veldim].bits) - 1;
3756              dimreg = pDimensionRegions[dimregidx];              dimregidx |= (bits & limiter_mask) << velbitpos;
3757                dimreg = pDimensionRegions[dimregidx & 255];
3758          }          }
3759          return dimreg;          return dimreg;
3760      }      }
# Line 3280  namespace { Line 4025  namespace {
4025          }          }
4026      }      }
4027    
4028    // *************** Script ***************
4029    // *
4030    
4031        Script::Script(ScriptGroup* group, RIFF::Chunk* ckScri) {
4032            pGroup = group;
4033            pChunk = ckScri;
4034            if (ckScri) { // object is loaded from file ...
4035                // read header
4036                uint32_t headerSize = ckScri->ReadUint32();
4037                Compression = (Compression_t) ckScri->ReadUint32();
4038                Encoding    = (Encoding_t) ckScri->ReadUint32();
4039                Language    = (Language_t) ckScri->ReadUint32();
4040                Bypass      = (Language_t) ckScri->ReadUint32() & 1;
4041                crc         = ckScri->ReadUint32();
4042                uint32_t nameSize = ckScri->ReadUint32();
4043                Name.resize(nameSize, ' ');
4044                for (int i = 0; i < nameSize; ++i)
4045                    Name[i] = ckScri->ReadUint8();
4046                // to handle potential future extensions of the header
4047                ckScri->SetPos(headerSize - 6*sizeof(int32_t) + nameSize, RIFF::stream_curpos);
4048                // read actual script data
4049                uint32_t scriptSize = ckScri->GetSize() - ckScri->GetPos();
4050                data.resize(scriptSize);
4051                for (int i = 0; i < scriptSize; ++i)
4052                    data[i] = ckScri->ReadUint8();
4053            } else { // this is a new script object, so just initialize it as such ...
4054                Compression = COMPRESSION_NONE;
4055                Encoding = ENCODING_ASCII;
4056                Language = LANGUAGE_NKSP;
4057                Bypass   = false;
4058                crc      = 0;
4059                Name     = "Unnamed Script";
4060            }
4061        }
4062    
4063        Script::~Script() {
4064        }
4065    
4066        /**
4067         * Returns the current script (i.e. as source code) in text format.
4068         */
4069        String Script::GetScriptAsText() {
4070            String s;
4071            s.resize(data.size(), ' ');
4072            memcpy(&s[0], &data[0], data.size());
4073            return s;
4074        }
4075    
4076        /**
4077         * Replaces the current script with the new script source code text given
4078         * by @a text.
4079         *
4080         * @param text - new script source code
4081         */
4082        void Script::SetScriptAsText(const String& text) {
4083            data.resize(text.size());
4084            memcpy(&data[0], &text[0], text.size());
4085        }
4086    
4087        void Script::UpdateChunks() {
4088            // recalculate CRC32 check sum
4089            __resetCRC(crc);
4090            __calculateCRC(&data[0], data.size(), crc);
4091            __encodeCRC(crc);
4092            // make sure chunk exists and has the required size
4093            const int chunkSize = 7*sizeof(int32_t) + Name.size() + data.size();
4094            if (!pChunk) pChunk = pGroup->pList->AddSubChunk(CHUNK_ID_SCRI, chunkSize);
4095            else pChunk->Resize(chunkSize);
4096            // fill the chunk data to be written to disk
4097            uint8_t* pData = (uint8_t*) pChunk->LoadChunkData();
4098            int pos = 0;
4099            store32(&pData[pos], 6*sizeof(int32_t) + Name.size()); // total header size
4100            pos += sizeof(int32_t);
4101            store32(&pData[pos], Compression);
4102            pos += sizeof(int32_t);
4103            store32(&pData[pos], Encoding);
4104            pos += sizeof(int32_t);
4105            store32(&pData[pos], Language);
4106            pos += sizeof(int32_t);
4107            store32(&pData[pos], Bypass ? 1 : 0);
4108            pos += sizeof(int32_t);
4109            store32(&pData[pos], crc);
4110            pos += sizeof(int32_t);
4111            store32(&pData[pos], Name.size());
4112            pos += sizeof(int32_t);
4113            for (int i = 0; i < Name.size(); ++i, ++pos)
4114                pData[pos] = Name[i];
4115            for (int i = 0; i < data.size(); ++i, ++pos)
4116                pData[pos] = data[i];
4117        }
4118    
4119        /**
4120         * Move this script from its current ScriptGroup to another ScriptGroup
4121         * given by @a pGroup.
4122         *
4123         * @param pGroup - script's new group
4124         */
4125        void Script::SetGroup(ScriptGroup* pGroup) {
4126            if (this->pGroup = pGroup) return;
4127            if (pChunk)
4128                pChunk->GetParent()->MoveSubChunk(pChunk, pGroup->pList);
4129            this->pGroup = pGroup;
4130        }
4131    
4132        void Script::RemoveAllScriptReferences() {
4133            File* pFile = pGroup->pFile;
4134            for (int i = 0; pFile->GetInstrument(i); ++i) {
4135                Instrument* instr = pFile->GetInstrument(i);
4136                instr->RemoveScript(this);
4137            }
4138        }
4139    
4140    // *************** ScriptGroup ***************
4141    // *
4142    
4143        ScriptGroup::ScriptGroup(File* file, RIFF::List* lstRTIS) {
4144            pFile = file;
4145            pList = lstRTIS;
4146            pScripts = NULL;
4147            if (lstRTIS) {
4148                RIFF::Chunk* ckName = lstRTIS->GetSubChunk(CHUNK_ID_LSNM);
4149                ::LoadString(ckName, Name);
4150            } else {
4151                Name = "Default Group";
4152            }
4153        }
4154    
4155        ScriptGroup::~ScriptGroup() {
4156            if (pScripts) {
4157                std::list<Script*>::iterator iter = pScripts->begin();
4158                std::list<Script*>::iterator end  = pScripts->end();
4159                while (iter != end) {
4160                    delete *iter;
4161                    ++iter;
4162                }
4163                delete pScripts;
4164            }
4165        }
4166    
4167        void ScriptGroup::UpdateChunks() {
4168            if (pScripts) {
4169                if (!pList)
4170                    pList = pFile->pRIFF->GetSubList(LIST_TYPE_3LS)->AddSubList(LIST_TYPE_RTIS);
4171    
4172                // now store the name of this group as <LSNM> chunk as subchunk of the <RTIS> list chunk
4173                ::SaveString(CHUNK_ID_LSNM, NULL, pList, Name, String("Unnamed Group"), true, 64);
4174    
4175                for (std::list<Script*>::iterator it = pScripts->begin();
4176                     it != pScripts->end(); ++it)
4177                {
4178                    (*it)->UpdateChunks();
4179                }
4180            }
4181        }
4182    
4183        /** @brief Get instrument script.
4184         *
4185         * Returns the real-time instrument script with the given index.
4186         *
4187         * @param index - number of the sought script (0..n)
4188         * @returns sought script or NULL if there's no such script
4189         */
4190        Script* ScriptGroup::GetScript(uint index) {
4191            if (!pScripts) LoadScripts();
4192            std::list<Script*>::iterator it = pScripts->begin();
4193            for (uint i = 0; it != pScripts->end(); ++i, ++it)
4194                if (i == index) return *it;
4195            return NULL;
4196        }
4197    
4198        /** @brief Add new instrument script.
4199         *
4200         * Adds a new real-time instrument script to the file. The script is not
4201         * actually used / executed unless it is referenced by an instrument to be
4202         * used. This is similar to samples, which you can add to a file, without
4203         * an instrument necessarily actually using it.
4204         *
4205         * You have to call Save() to make this persistent to the file.
4206         *
4207         * @return new empty script object
4208         */
4209        Script* ScriptGroup::AddScript() {
4210            if (!pScripts) LoadScripts();
4211            Script* pScript = new Script(this, NULL);
4212            pScripts->push_back(pScript);
4213            return pScript;
4214        }
4215    
4216        /** @brief Delete an instrument script.
4217         *
4218         * This will delete the given real-time instrument script. References of
4219         * instruments that are using that script will be removed accordingly.
4220         *
4221         * You have to call Save() to make this persistent to the file.
4222         *
4223         * @param pScript - script to delete
4224         * @throws gig::Exception if given script could not be found
4225         */
4226        void ScriptGroup::DeleteScript(Script* pScript) {
4227            if (!pScripts) LoadScripts();
4228            std::list<Script*>::iterator iter =
4229                find(pScripts->begin(), pScripts->end(), pScript);
4230            if (iter == pScripts->end())
4231                throw gig::Exception("Could not delete script, could not find given script");
4232            pScripts->erase(iter);
4233            pScript->RemoveAllScriptReferences();
4234            if (pScript->pChunk)
4235                pScript->pChunk->GetParent()->DeleteSubChunk(pScript->pChunk);
4236            delete pScript;
4237        }
4238    
4239        void ScriptGroup::LoadScripts() {
4240            if (pScripts) return;
4241            pScripts = new std::list<Script*>;
4242            if (!pList) return;
4243    
4244            for (RIFF::Chunk* ck = pList->GetFirstSubChunk(); ck;
4245                 ck = pList->GetNextSubChunk())
4246            {
4247                if (ck->GetChunkID() == CHUNK_ID_SCRI) {
4248                    pScripts->push_back(new Script(this, ck));
4249                }
4250            }
4251        }
4252    
4253  // *************** Instrument ***************  // *************** Instrument ***************
4254  // *  // *
4255    
# Line 3302  namespace { Line 4272  namespace {
4272          DimensionKeyRange.high = 0;          DimensionKeyRange.high = 0;
4273          pMidiRules = new MidiRule*[3];          pMidiRules = new MidiRule*[3];
4274          pMidiRules[0] = NULL;          pMidiRules[0] = NULL;
4275            pScriptRefs = NULL;
4276    
4277          // Loading          // Loading
4278          RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART);          RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART);
# Line 3362  namespace { Line 4333  namespace {
4333              }              }
4334          }          }
4335    
4336            // own gig format extensions
4337            RIFF::List* lst3LS = insList->GetSubList(LIST_TYPE_3LS);
4338            if (lst3LS) {
4339                RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL);
4340                if (ckSCSL) {
4341                    int slotCount = ckSCSL->ReadUint32();
4342                    int slotSize  = ckSCSL->ReadUint32();
4343                    int unknownSpace = slotSize - 2*sizeof(uint32_t); // in case of future extensions
4344                    for (int i = 0; i < slotCount; ++i) {
4345                        _ScriptPooolEntry e;
4346                        e.fileOffset = ckSCSL->ReadUint32();
4347                        e.bypass     = ckSCSL->ReadUint32() & 1;
4348                        if (unknownSpace) ckSCSL->SetPos(unknownSpace, RIFF::stream_curpos); // in case of future extensions
4349                        scriptPoolFileOffsets.push_back(e);
4350                    }
4351                }
4352            }
4353    
4354          __notify_progress(pProgress, 1.0f); // notify done          __notify_progress(pProgress, 1.0f); // notify done
4355      }      }
4356    
# Line 3382  namespace { Line 4371  namespace {
4371              delete pMidiRules[i];              delete pMidiRules[i];
4372          }          }
4373          delete[] pMidiRules;          delete[] pMidiRules;
4374            if (pScriptRefs) delete pScriptRefs;
4375      }      }
4376    
4377      /**      /**
# Line 3437  namespace { Line 4427  namespace {
4427                  pMidiRules[i]->UpdateChunks(pData);                  pMidiRules[i]->UpdateChunks(pData);
4428              }              }
4429          }          }
4430    
4431            // own gig format extensions
4432           if (pScriptRefs) {
4433               RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS);
4434               if (!lst3LS) lst3LS = pCkInstrument->AddSubList(LIST_TYPE_3LS);
4435               const int totalSize = pScriptRefs->size() * 2*sizeof(uint32_t);
4436               RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL);
4437               if (!ckSCSL) ckSCSL = lst3LS->AddSubChunk(CHUNK_ID_SCSL, totalSize);
4438               else ckSCSL->Resize(totalSize);
4439               uint8_t* pData = (uint8_t*) ckSCSL->LoadChunkData();
4440               for (int i = 0, pos = 0; i < pScriptRefs->size(); ++i) {
4441                   int fileOffset =
4442                        (*pScriptRefs)[i].script->pChunk->GetFilePos() -
4443                        (*pScriptRefs)[i].script->pChunk->GetPos() -
4444                        CHUNK_HEADER_SIZE;
4445                   store32(&pData[pos], fileOffset);
4446                   pos += sizeof(uint32_t);
4447                   store32(&pData[pos], (*pScriptRefs)[i].bypass ? 1 : 0);
4448                   pos += sizeof(uint32_t);
4449               }
4450           }
4451      }      }
4452    
4453      /**      /**
# Line 3568  namespace { Line 4579  namespace {
4579          pMidiRules[i] = 0;          pMidiRules[i] = 0;
4580      }      }
4581    
4582        void Instrument::LoadScripts() {
4583            if (pScriptRefs) return;
4584            pScriptRefs = new std::vector<_ScriptPooolRef>;
4585            if (scriptPoolFileOffsets.empty()) return;
4586            File* pFile = (File*) GetParent();
4587            for (uint k = 0; k < scriptPoolFileOffsets.size(); ++k) {
4588                uint32_t offset = scriptPoolFileOffsets[k].fileOffset;
4589                for (uint i = 0; pFile->GetScriptGroup(i); ++i) {
4590                    ScriptGroup* group = pFile->GetScriptGroup(i);
4591                    for (uint s = 0; group->GetScript(s); ++s) {
4592                        Script* script = group->GetScript(s);
4593                        if (script->pChunk) {
4594                            script->pChunk->SetPos(0);
4595                            if (script->pChunk->GetFilePos() -
4596                                script->pChunk->GetPos() -
4597                                CHUNK_HEADER_SIZE == offset)
4598                            {
4599                                _ScriptPooolRef ref;
4600                                ref.script = script;
4601                                ref.bypass = scriptPoolFileOffsets[k].bypass;
4602                                pScriptRefs->push_back(ref);
4603                                break;
4604                            }
4605                        }
4606                    }
4607                }
4608            }
4609            // we don't need that anymore
4610            scriptPoolFileOffsets.clear();
4611        }
4612    
4613        /** @brief Add new instrument script slot (gig format extension)
4614         *
4615         * Add the given real-time instrument script reference to this instrument,
4616         * which shall be executed by the sampler for for this instrument. The
4617         * script will be added to the end of the script list of this instrument.
4618         * The positions of the scripts in the Instrument's Script list are
4619         * relevant, because they define in which order they shall be executed by
4620         * the sampler. For this reason it is also legal to add the same script
4621         * twice to an instrument, for example you might have a script called
4622         * "MyFilter" which performs an event filter task, and you might have
4623         * another script called "MyNoteTrigger" which triggers new notes, then you
4624         * might for example have the following list of scripts on the instrument:
4625         *
4626         * 1. Script "MyFilter"
4627         * 2. Script "MyNoteTrigger"
4628         * 3. Script "MyFilter"
4629         *
4630         * Which would make sense, because the 2nd script launched new events, which
4631         * you might need to filter as well.
4632         *
4633         * There are two ways to disable / "bypass" scripts. You can either disable
4634         * a script locally for the respective script slot on an instrument (i.e. by
4635         * passing @c false to the 2nd argument of this method, or by calling
4636         * SetScriptBypassed()). Or you can disable a script globally for all slots
4637         * and all instruments by setting Script::Bypass.
4638         *
4639         * @note This is an own format extension which did not exist i.e. in the
4640         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4641         * gigedit.
4642         *
4643         * @param pScript - script that shall be executed for this instrument
4644         * @param bypass  - if enabled, the sampler shall skip executing this
4645         *                  script (in the respective list position)
4646         * @see SetScriptBypassed()
4647         */
4648        void Instrument::AddScriptSlot(Script* pScript, bool bypass) {
4649            LoadScripts();
4650            _ScriptPooolRef ref = { pScript, bypass };
4651            pScriptRefs->push_back(ref);
4652        }
4653    
4654        /** @brief Flip two script slots with each other (gig format extension).
4655         *
4656         * Swaps the position of the two given scripts in the Instrument's Script
4657         * list. The positions of the scripts in the Instrument's Script list are
4658         * relevant, because they define in which order they shall be executed by
4659         * the sampler.
4660         *
4661         * @note This is an own format extension which did not exist i.e. in the
4662         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4663         * gigedit.
4664         *
4665         * @param index1 - index of the first script slot to swap
4666         * @param index2 - index of the second script slot to swap
4667         */
4668        void Instrument::SwapScriptSlots(uint index1, uint index2) {
4669            LoadScripts();
4670            if (index1 >= pScriptRefs->size() || index2 >= pScriptRefs->size())
4671                return;
4672            _ScriptPooolRef tmp = (*pScriptRefs)[index1];
4673            (*pScriptRefs)[index1] = (*pScriptRefs)[index2];
4674            (*pScriptRefs)[index2] = tmp;
4675        }
4676    
4677        /** @brief Remove script slot.
4678         *
4679         * Removes the script slot with the given slot index.
4680         *
4681         * @param index - index of script slot to remove
4682         */
4683        void Instrument::RemoveScriptSlot(uint index) {
4684            LoadScripts();
4685            if (index >= pScriptRefs->size()) return;
4686            pScriptRefs->erase( pScriptRefs->begin() + index );
4687        }
4688    
4689        /** @brief Remove reference to given Script (gig format extension).
4690         *
4691         * This will remove all script slots on the instrument which are referencing
4692         * the given script.
4693         *
4694         * @note This is an own format extension which did not exist i.e. in the
4695         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4696         * gigedit.
4697         *
4698         * @param pScript - script reference to remove from this instrument
4699         * @see RemoveScriptSlot()
4700         */
4701        void Instrument::RemoveScript(Script* pScript) {
4702            LoadScripts();
4703            for (int i = pScriptRefs->size() - 1; i >= 0; --i) {
4704                if ((*pScriptRefs)[i].script == pScript) {
4705                    pScriptRefs->erase( pScriptRefs->begin() + i );
4706                }
4707            }
4708        }
4709    
4710        /** @brief Instrument's amount of script slots.
4711         *
4712         * This method returns the amount of script slots this instrument currently
4713         * uses.
4714         *
4715         * A script slot is a reference of a real-time instrument script to be
4716         * executed by the sampler. The scripts will be executed by the sampler in
4717         * sequence of the slots. One (same) script may be referenced multiple
4718         * times in different slots.
4719         *
4720         * @note This is an own format extension which did not exist i.e. in the
4721         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4722         * gigedit.
4723         */
4724        uint Instrument::ScriptSlotCount() const {
4725            return pScriptRefs ? pScriptRefs->size() : scriptPoolFileOffsets.size();
4726        }
4727    
4728        /** @brief Whether script execution shall be skipped.
4729         *
4730         * Defines locally for the Script reference slot in the Instrument's Script
4731         * list, whether the script shall be skipped by the sampler regarding
4732         * execution.
4733         *
4734         * It is also possible to ignore exeuction of the script globally, for all
4735         * slots and for all instruments by setting Script::Bypass.
4736         *
4737         * @note This is an own format extension which did not exist i.e. in the
4738         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4739         * gigedit.
4740         *
4741         * @param index - index of the script slot on this instrument
4742         * @see Script::Bypass
4743         */
4744        bool Instrument::IsScriptSlotBypassed(uint index) {
4745            if (index >= ScriptSlotCount()) return false;
4746            return pScriptRefs ? pScriptRefs->at(index).bypass
4747                               : scriptPoolFileOffsets.at(index).bypass;
4748            
4749        }
4750    
4751        /** @brief Defines whether execution shall be skipped.
4752         *
4753         * You can call this method to define locally whether or whether not the
4754         * given script slot shall be executed by the sampler.
4755         *
4756         * @note This is an own format extension which did not exist i.e. in the
4757         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4758         * gigedit.
4759         *
4760         * @param index - script slot index on this instrument
4761         * @param bBypass - if true, the script slot will be skipped by the sampler
4762         * @see Script::Bypass
4763         */
4764        void Instrument::SetScriptSlotBypassed(uint index, bool bBypass) {
4765            if (index >= ScriptSlotCount()) return;
4766            if (pScriptRefs)
4767                pScriptRefs->at(index).bypass = bBypass;
4768            else
4769                scriptPoolFileOffsets.at(index).bypass = bBypass;
4770        }
4771    
4772      /**      /**
4773       * 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
4774       * and assign it to this object.       * and assign it to this object.
# Line 3601  namespace { Line 4802  namespace {
4802          PitchbendRange = orig->PitchbendRange;          PitchbendRange = orig->PitchbendRange;
4803          PianoReleaseMode = orig->PianoReleaseMode;          PianoReleaseMode = orig->PianoReleaseMode;
4804          DimensionKeyRange = orig->DimensionKeyRange;          DimensionKeyRange = orig->DimensionKeyRange;
4805            scriptPoolFileOffsets = orig->scriptPoolFileOffsets;
4806            pScriptRefs = orig->pScriptRefs;
4807                    
4808          // free old midi rules          // free old midi rules
4809          for (int i = 0 ; pMidiRules[i] ; i++) {          for (int i = 0 ; pMidiRules[i] ; i++) {
# Line 3786  namespace { Line 4989  namespace {
4989          bAutoLoad = true;          bAutoLoad = true;
4990          *pVersion = VERSION_3;          *pVersion = VERSION_3;
4991          pGroups = NULL;          pGroups = NULL;
4992            pScriptGroups = NULL;
4993          pInfo->SetFixedStringLengths(_FileFixedStringLengths);          pInfo->SetFixedStringLengths(_FileFixedStringLengths);
4994          pInfo->ArchivalLocation = String(256, ' ');          pInfo->ArchivalLocation = String(256, ' ');
4995    
# Line 3801  namespace { Line 5005  namespace {
5005      File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) {      File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) {
5006          bAutoLoad = true;          bAutoLoad = true;
5007          pGroups = NULL;          pGroups = NULL;
5008            pScriptGroups = NULL;
5009          pInfo->SetFixedStringLengths(_FileFixedStringLengths);          pInfo->SetFixedStringLengths(_FileFixedStringLengths);
5010      }      }
5011    
# Line 3814  namespace { Line 5019  namespace {
5019              }              }
5020              delete pGroups;              delete pGroups;
5021          }          }
5022            if (pScriptGroups) {
5023                std::list<ScriptGroup*>::iterator iter = pScriptGroups->begin();
5024                std::list<ScriptGroup*>::iterator end  = pScriptGroups->end();
5025                while (iter != end) {
5026                    delete *iter;
5027                    ++iter;
5028                }
5029                delete pScriptGroups;
5030            }
5031      }      }
5032    
5033      Sample* File::GetFirstSample(progress_t* pProgress) {      Sample* File::GetFirstSample(progress_t* pProgress) {
# Line 4228  namespace { Line 5442  namespace {
5442          return NULL;          return NULL;
5443      }      }
5444    
5445        /**
5446         * Returns the group with the given group name.
5447         *
5448         * Note: group names don't have to be unique in the gig format! So there
5449         * can be multiple groups with the same name. This method will simply
5450         * return the first group found with the given name.
5451         *
5452         * @param name - name of the sought group
5453         * @returns sought group or NULL if there's no group with that name
5454         */
5455        Group* File::GetGroup(String name) {
5456            if (!pGroups) LoadGroups();
5457            GroupsIterator = pGroups->begin();
5458            for (uint i = 0; GroupsIterator != pGroups->end(); ++GroupsIterator, ++i)
5459                if ((*GroupsIterator)->Name == name) return *GroupsIterator;
5460            return NULL;
5461        }
5462    
5463      Group* File::AddGroup() {      Group* File::AddGroup() {
5464          if (!pGroups) LoadGroups();          if (!pGroups) LoadGroups();
5465          // there must always be at least one group          // there must always be at least one group
# Line 4308  namespace { Line 5540  namespace {
5540          }          }
5541      }      }
5542    
5543        /** @brief Get instrument script group (by index).
5544         *
5545         * Returns the real-time instrument script group with the given index.
5546         *
5547         * @param index - number of the sought group (0..n)
5548         * @returns sought script group or NULL if there's no such group
5549         */
5550        ScriptGroup* File::GetScriptGroup(uint index) {
5551            if (!pScriptGroups) LoadScriptGroups();
5552            std::list<ScriptGroup*>::iterator it = pScriptGroups->begin();
5553            for (uint i = 0; it != pScriptGroups->end(); ++i, ++it)
5554                if (i == index) return *it;
5555            return NULL;
5556        }
5557    
5558        /** @brief Get instrument script group (by name).
5559         *
5560         * Returns the first real-time instrument script group found with the given
5561         * group name. Note that group names may not necessarily be unique.
5562         *
5563         * @param name - name of the sought script group
5564         * @returns sought script group or NULL if there's no such group
5565         */
5566        ScriptGroup* File::GetScriptGroup(const String& name) {
5567            if (!pScriptGroups) LoadScriptGroups();
5568            std::list<ScriptGroup*>::iterator it = pScriptGroups->begin();
5569            for (uint i = 0; it != pScriptGroups->end(); ++i, ++it)
5570                if ((*it)->Name == name) return *it;
5571            return NULL;
5572        }
5573    
5574        /** @brief Add new instrument script group.
5575         *
5576         * Adds a new, empty real-time instrument script group to the file.
5577         *
5578         * You have to call Save() to make this persistent to the file.
5579         *
5580         * @return new empty script group
5581         */
5582        ScriptGroup* File::AddScriptGroup() {
5583            if (!pScriptGroups) LoadScriptGroups();
5584            ScriptGroup* pScriptGroup = new ScriptGroup(this, NULL);
5585            pScriptGroups->push_back(pScriptGroup);
5586            return pScriptGroup;
5587        }
5588    
5589        /** @brief Delete an instrument script group.
5590         *
5591         * This will delete the given real-time instrument script group and all its
5592         * instrument scripts it contains. References inside instruments that are
5593         * using the deleted scripts will be removed from the respective instruments
5594         * accordingly.
5595         *
5596         * You have to call Save() to make this persistent to the file.
5597         *
5598         * @param pScriptGroup - script group to delete
5599         * @throws gig::Exception if given script group could not be found
5600         */
5601        void File::DeleteScriptGroup(ScriptGroup* pScriptGroup) {
5602            if (!pScriptGroups) LoadScriptGroups();
5603            std::list<ScriptGroup*>::iterator iter =
5604                find(pScriptGroups->begin(), pScriptGroups->end(), pScriptGroup);
5605            if (iter == pScriptGroups->end())
5606                throw gig::Exception("Could not delete script group, could not find given script group");
5607            pScriptGroups->erase(iter);
5608            for (int i = 0; pScriptGroup->GetScript(i); ++i)
5609                pScriptGroup->DeleteScript(pScriptGroup->GetScript(i));
5610            if (pScriptGroup->pList)
5611                pScriptGroup->pList->GetParent()->DeleteSubChunk(pScriptGroup->pList);
5612            delete pScriptGroup;
5613        }
5614    
5615        void File::LoadScriptGroups() {
5616            if (pScriptGroups) return;
5617            pScriptGroups = new std::list<ScriptGroup*>;
5618            RIFF::List* lstLS = pRIFF->GetSubList(LIST_TYPE_3LS);
5619            if (lstLS) {
5620                for (RIFF::List* lst = lstLS->GetFirstSubList(); lst;
5621                     lst = lstLS->GetNextSubList())
5622                {
5623                    if (lst->GetListType() == LIST_TYPE_RTIS) {
5624                        pScriptGroups->push_back(new ScriptGroup(this, lst));
5625                    }
5626                }
5627            }
5628        }
5629    
5630      /**      /**
5631       * Apply all the gig file's current instruments, samples, groups and settings       * Apply all the gig file's current instruments, samples, groups and settings
5632       * to the respective RIFF chunks. You have to call Save() to make changes       * to the respective RIFF chunks. You have to call Save() to make changes
# Line 4323  namespace { Line 5642  namespace {
5642    
5643          b64BitWavePoolOffsets = pVersion && pVersion->major == 3;          b64BitWavePoolOffsets = pVersion && pVersion->major == 3;
5644    
5645            // update own gig format extension chunks
5646            // (not part of the GigaStudio 4 format)
5647            //
5648            // This must be performed before writing the chunks for instruments,
5649            // because the instruments' script slots will write the file offsets
5650            // of the respective instrument script chunk as reference.
5651            if (pScriptGroups) {
5652                RIFF::List* lst3LS = pRIFF->GetSubList(LIST_TYPE_3LS);
5653                if (pScriptGroups->empty()) {
5654                    if (lst3LS) pRIFF->DeleteSubChunk(lst3LS);
5655                } else {
5656                    if (!lst3LS) lst3LS = pRIFF->AddSubList(LIST_TYPE_3LS);
5657    
5658                    // Update instrument script (group) chunks.
5659    
5660                    for (std::list<ScriptGroup*>::iterator it = pScriptGroups->begin();
5661                         it != pScriptGroups->end(); ++it)
5662                    {
5663                        (*it)->UpdateChunks();
5664                    }
5665                }
5666            }
5667    
5668          // first update base class's chunks          // first update base class's chunks
5669          DLS::File::UpdateChunks();          DLS::File::UpdateChunks();
5670    

Legend:
Removed from v.2484  
changed lines
  Added in v.2584

  ViewVC Help
Powered by ViewVC