Beispiel #1
0
  private void writePdtaChunk(RIFFWriter writer) throws IOException {

    RIFFWriter phdr_chunk = writer.writeChunk("phdr");
    int phdr_zone_count = 0;
    for (SF2Instrument preset : this.instruments) {
      phdr_chunk.writeString(preset.name, 20);
      phdr_chunk.writeUnsignedShort(preset.preset);
      phdr_chunk.writeUnsignedShort(preset.bank);
      phdr_chunk.writeUnsignedShort(phdr_zone_count);
      if (preset.getGlobalRegion() != null) phdr_zone_count += 1;
      phdr_zone_count += preset.getRegions().size();
      phdr_chunk.writeUnsignedInt(preset.library);
      phdr_chunk.writeUnsignedInt(preset.genre);
      phdr_chunk.writeUnsignedInt(preset.morphology);
    }
    phdr_chunk.writeString("EOP", 20);
    phdr_chunk.writeUnsignedShort(0);
    phdr_chunk.writeUnsignedShort(0);
    phdr_chunk.writeUnsignedShort(phdr_zone_count);
    phdr_chunk.writeUnsignedInt(0);
    phdr_chunk.writeUnsignedInt(0);
    phdr_chunk.writeUnsignedInt(0);

    RIFFWriter pbag_chunk = writer.writeChunk("pbag");
    int pbag_gencount = 0;
    int pbag_modcount = 0;
    for (SF2Instrument preset : this.instruments) {
      if (preset.getGlobalRegion() != null) {
        pbag_chunk.writeUnsignedShort(pbag_gencount);
        pbag_chunk.writeUnsignedShort(pbag_modcount);
        pbag_gencount += preset.getGlobalRegion().getGenerators().size();
        pbag_modcount += preset.getGlobalRegion().getModulators().size();
      }
      for (SF2InstrumentRegion region : preset.getRegions()) {
        pbag_chunk.writeUnsignedShort(pbag_gencount);
        pbag_chunk.writeUnsignedShort(pbag_modcount);
        if (layers.indexOf(region.layer) != -1) {
          // One generator is used to reference to instrument record
          pbag_gencount += 1;
        }
        pbag_gencount += region.getGenerators().size();
        pbag_modcount += region.getModulators().size();
      }
    }
    pbag_chunk.writeUnsignedShort(pbag_gencount);
    pbag_chunk.writeUnsignedShort(pbag_modcount);

    RIFFWriter pmod_chunk = writer.writeChunk("pmod");
    for (SF2Instrument preset : this.instruments) {
      if (preset.getGlobalRegion() != null) {
        writeModulators(pmod_chunk, preset.getGlobalRegion().getModulators());
      }
      for (SF2InstrumentRegion region : preset.getRegions())
        writeModulators(pmod_chunk, region.getModulators());
    }
    pmod_chunk.write(new byte[10]);

    RIFFWriter pgen_chunk = writer.writeChunk("pgen");
    for (SF2Instrument preset : this.instruments) {
      if (preset.getGlobalRegion() != null) {
        writeGenerators(pgen_chunk, preset.getGlobalRegion().getGenerators());
      }
      for (SF2InstrumentRegion region : preset.getRegions()) {
        writeGenerators(pgen_chunk, region.getGenerators());
        int ix = (int) layers.indexOf(region.layer);
        if (ix != -1) {
          pgen_chunk.writeUnsignedShort(SF2Region.GENERATOR_INSTRUMENT);
          pgen_chunk.writeShort((short) ix);
        }
      }
    }
    pgen_chunk.write(new byte[4]);

    RIFFWriter inst_chunk = writer.writeChunk("inst");
    int inst_zone_count = 0;
    for (SF2Layer instrument : this.layers) {
      inst_chunk.writeString(instrument.name, 20);
      inst_chunk.writeUnsignedShort(inst_zone_count);
      if (instrument.getGlobalRegion() != null) inst_zone_count += 1;
      inst_zone_count += instrument.getRegions().size();
    }
    inst_chunk.writeString("EOI", 20);
    inst_chunk.writeUnsignedShort(inst_zone_count);

    RIFFWriter ibag_chunk = writer.writeChunk("ibag");
    int ibag_gencount = 0;
    int ibag_modcount = 0;
    for (SF2Layer instrument : this.layers) {
      if (instrument.getGlobalRegion() != null) {
        ibag_chunk.writeUnsignedShort(ibag_gencount);
        ibag_chunk.writeUnsignedShort(ibag_modcount);
        ibag_gencount += instrument.getGlobalRegion().getGenerators().size();
        ibag_modcount += instrument.getGlobalRegion().getModulators().size();
      }
      for (SF2LayerRegion region : instrument.getRegions()) {
        ibag_chunk.writeUnsignedShort(ibag_gencount);
        ibag_chunk.writeUnsignedShort(ibag_modcount);
        if (samples.indexOf(region.sample) != -1) {
          // One generator is used to reference to instrument record
          ibag_gencount += 1;
        }
        ibag_gencount += region.getGenerators().size();
        ibag_modcount += region.getModulators().size();
      }
    }
    ibag_chunk.writeUnsignedShort(ibag_gencount);
    ibag_chunk.writeUnsignedShort(ibag_modcount);

    RIFFWriter imod_chunk = writer.writeChunk("imod");
    for (SF2Layer instrument : this.layers) {
      if (instrument.getGlobalRegion() != null) {
        writeModulators(imod_chunk, instrument.getGlobalRegion().getModulators());
      }
      for (SF2LayerRegion region : instrument.getRegions())
        writeModulators(imod_chunk, region.getModulators());
    }
    imod_chunk.write(new byte[10]);

    RIFFWriter igen_chunk = writer.writeChunk("igen");
    for (SF2Layer instrument : this.layers) {
      if (instrument.getGlobalRegion() != null) {
        writeGenerators(igen_chunk, instrument.getGlobalRegion().getGenerators());
      }
      for (SF2LayerRegion region : instrument.getRegions()) {
        writeGenerators(igen_chunk, region.getGenerators());
        int ix = samples.indexOf(region.sample);
        if (ix != -1) {
          igen_chunk.writeUnsignedShort(SF2Region.GENERATOR_SAMPLEID);
          igen_chunk.writeShort((short) ix);
        }
      }
    }
    igen_chunk.write(new byte[4]);

    RIFFWriter shdr_chunk = writer.writeChunk("shdr");
    long sample_pos = 0;
    for (SF2Sample sample : samples) {
      shdr_chunk.writeString(sample.name, 20);
      long start = sample_pos;
      sample_pos += sample.data.capacity() / 2;
      long end = sample_pos;
      long startLoop = sample.startLoop + start;
      long endLoop = sample.endLoop + start;
      if (startLoop < start) startLoop = start;
      if (endLoop > end) endLoop = end;
      shdr_chunk.writeUnsignedInt(start);
      shdr_chunk.writeUnsignedInt(end);
      shdr_chunk.writeUnsignedInt(startLoop);
      shdr_chunk.writeUnsignedInt(endLoop);
      shdr_chunk.writeUnsignedInt(sample.sampleRate);
      shdr_chunk.writeUnsignedByte(sample.originalPitch);
      shdr_chunk.writeByte(sample.pitchCorrection);
      shdr_chunk.writeUnsignedShort(sample.sampleLink);
      shdr_chunk.writeUnsignedShort(sample.sampleType);
      sample_pos += 32;
    }
    shdr_chunk.writeString("EOS", 20);
    shdr_chunk.write(new byte[26]);
  }
Beispiel #2
0
  private void readPdtaChunk(RIFFReader riff) throws IOException {

    List<SF2Instrument> presets = new ArrayList<SF2Instrument>();
    List<Integer> presets_bagNdx = new ArrayList<Integer>();
    List<SF2InstrumentRegion> presets_splits_gen = new ArrayList<SF2InstrumentRegion>();
    List<SF2InstrumentRegion> presets_splits_mod = new ArrayList<SF2InstrumentRegion>();

    List<SF2Layer> instruments = new ArrayList<SF2Layer>();
    List<Integer> instruments_bagNdx = new ArrayList<Integer>();
    List<SF2LayerRegion> instruments_splits_gen = new ArrayList<SF2LayerRegion>();
    List<SF2LayerRegion> instruments_splits_mod = new ArrayList<SF2LayerRegion>();

    while (riff.hasNextChunk()) {
      RIFFReader chunk = riff.nextChunk();
      String format = chunk.getFormat();
      if (format.equals("phdr")) {
        // Preset Header / Instrument
        if (chunk.available() % 38 != 0) throw new RIFFInvalidDataException();
        int count = chunk.available() / 38;
        for (int i = 0; i < count; i++) {
          SF2Instrument preset = new SF2Instrument(this);
          preset.name = chunk.readString(20);
          preset.preset = chunk.readUnsignedShort();
          preset.bank = chunk.readUnsignedShort();
          presets_bagNdx.add(chunk.readUnsignedShort());
          preset.library = chunk.readUnsignedInt();
          preset.genre = chunk.readUnsignedInt();
          preset.morphology = chunk.readUnsignedInt();
          presets.add(preset);
          if (i != count - 1) this.instruments.add(preset);
        }
      } else if (format.equals("pbag")) {
        // Preset Zones / Instruments splits
        if (chunk.available() % 4 != 0) throw new RIFFInvalidDataException();
        int count = chunk.available() / 4;

        // Skip first record
        {
          int gencount = chunk.readUnsignedShort();
          int modcount = chunk.readUnsignedShort();
          while (presets_splits_gen.size() < gencount) presets_splits_gen.add(null);
          while (presets_splits_mod.size() < modcount) presets_splits_mod.add(null);
          count--;
        }

        int offset = presets_bagNdx.get(0);
        // Offset should be 0 (but just case)
        for (int i = 0; i < offset; i++) {
          if (count == 0) throw new RIFFInvalidDataException();
          int gencount = chunk.readUnsignedShort();
          int modcount = chunk.readUnsignedShort();
          while (presets_splits_gen.size() < gencount) presets_splits_gen.add(null);
          while (presets_splits_mod.size() < modcount) presets_splits_mod.add(null);
          count--;
        }

        for (int i = 0; i < presets_bagNdx.size() - 1; i++) {
          int zone_count = presets_bagNdx.get(i + 1) - presets_bagNdx.get(i);
          SF2Instrument preset = presets.get(i);
          for (int ii = 0; ii < zone_count; ii++) {
            if (count == 0) throw new RIFFInvalidDataException();
            int gencount = chunk.readUnsignedShort();
            int modcount = chunk.readUnsignedShort();
            SF2InstrumentRegion split = new SF2InstrumentRegion();
            preset.regions.add(split);
            while (presets_splits_gen.size() < gencount) presets_splits_gen.add(split);
            while (presets_splits_mod.size() < modcount) presets_splits_mod.add(split);
            count--;
          }
        }
      } else if (format.equals("pmod")) {
        // Preset Modulators / Split Modulators
        for (int i = 0; i < presets_splits_mod.size(); i++) {
          SF2Modulator modulator = new SF2Modulator();
          modulator.sourceOperator = chunk.readUnsignedShort();
          modulator.destinationOperator = chunk.readUnsignedShort();
          modulator.amount = chunk.readShort();
          modulator.amountSourceOperator = chunk.readUnsignedShort();
          modulator.transportOperator = chunk.readUnsignedShort();
          SF2InstrumentRegion split = presets_splits_mod.get(i);
          if (split != null) split.modulators.add(modulator);
        }
      } else if (format.equals("pgen")) {
        // Preset Generators / Split Generators
        for (int i = 0; i < presets_splits_gen.size(); i++) {
          int operator = chunk.readUnsignedShort();
          short amount = chunk.readShort();
          SF2InstrumentRegion split = presets_splits_gen.get(i);
          if (split != null) split.generators.put(operator, amount);
        }
      } else if (format.equals("inst")) {
        // Instrument Header / Layers
        if (chunk.available() % 22 != 0) throw new RIFFInvalidDataException();
        int count = chunk.available() / 22;
        for (int i = 0; i < count; i++) {
          SF2Layer layer = new SF2Layer(this);
          layer.name = chunk.readString(20);
          instruments_bagNdx.add(chunk.readUnsignedShort());
          instruments.add(layer);
          if (i != count - 1) this.layers.add(layer);
        }
      } else if (format.equals("ibag")) {
        // Instrument Zones / Layer splits
        if (chunk.available() % 4 != 0) throw new RIFFInvalidDataException();
        int count = chunk.available() / 4;

        // Skip first record
        {
          int gencount = chunk.readUnsignedShort();
          int modcount = chunk.readUnsignedShort();
          while (instruments_splits_gen.size() < gencount) instruments_splits_gen.add(null);
          while (instruments_splits_mod.size() < modcount) instruments_splits_mod.add(null);
          count--;
        }

        int offset = instruments_bagNdx.get(0);
        // Offset should be 0 (but just case)
        for (int i = 0; i < offset; i++) {
          if (count == 0) throw new RIFFInvalidDataException();
          int gencount = chunk.readUnsignedShort();
          int modcount = chunk.readUnsignedShort();
          while (instruments_splits_gen.size() < gencount) instruments_splits_gen.add(null);
          while (instruments_splits_mod.size() < modcount) instruments_splits_mod.add(null);
          count--;
        }

        for (int i = 0; i < instruments_bagNdx.size() - 1; i++) {
          int zone_count = instruments_bagNdx.get(i + 1) - instruments_bagNdx.get(i);
          SF2Layer layer = layers.get(i);
          for (int ii = 0; ii < zone_count; ii++) {
            if (count == 0) throw new RIFFInvalidDataException();
            int gencount = chunk.readUnsignedShort();
            int modcount = chunk.readUnsignedShort();
            SF2LayerRegion split = new SF2LayerRegion();
            layer.regions.add(split);
            while (instruments_splits_gen.size() < gencount) instruments_splits_gen.add(split);
            while (instruments_splits_mod.size() < modcount) instruments_splits_mod.add(split);
            count--;
          }
        }

      } else if (format.equals("imod")) {
        // Instrument Modulators / Split Modulators
        for (int i = 0; i < instruments_splits_mod.size(); i++) {
          SF2Modulator modulator = new SF2Modulator();
          modulator.sourceOperator = chunk.readUnsignedShort();
          modulator.destinationOperator = chunk.readUnsignedShort();
          modulator.amount = chunk.readShort();
          modulator.amountSourceOperator = chunk.readUnsignedShort();
          modulator.transportOperator = chunk.readUnsignedShort();
          SF2LayerRegion split = instruments_splits_gen.get(i);
          if (split != null) split.modulators.add(modulator);
        }
      } else if (format.equals("igen")) {
        // Instrument Generators / Split Generators
        for (int i = 0; i < instruments_splits_gen.size(); i++) {
          int operator = chunk.readUnsignedShort();
          short amount = chunk.readShort();
          SF2LayerRegion split = instruments_splits_gen.get(i);
          if (split != null) split.generators.put(operator, amount);
        }
      } else if (format.equals("shdr")) {
        // Sample Headers
        if (chunk.available() % 46 != 0) throw new RIFFInvalidDataException();
        int count = chunk.available() / 46;
        for (int i = 0; i < count; i++) {
          SF2Sample sample = new SF2Sample(this);
          sample.name = chunk.readString(20);
          long start = chunk.readUnsignedInt();
          long end = chunk.readUnsignedInt();
          sample.data = sampleData.subbuffer(start * 2, end * 2, true);
          if (sampleData24 != null) sample.data24 = sampleData24.subbuffer(start, end, true);
          /*
          sample.data = new ModelByteBuffer(sampleData, (int)(start*2),
                  (int)((end - start)*2));
          if (sampleData24 != null)
              sample.data24 = new ModelByteBuffer(sampleData24,
                      (int)start, (int)(end - start));
           */
          sample.startLoop = chunk.readUnsignedInt() - start;
          sample.endLoop = chunk.readUnsignedInt() - start;
          if (sample.startLoop < 0) sample.startLoop = -1;
          if (sample.endLoop < 0) sample.endLoop = -1;
          sample.sampleRate = chunk.readUnsignedInt();
          sample.originalPitch = chunk.readUnsignedByte();
          sample.pitchCorrection = chunk.readByte();
          sample.sampleLink = chunk.readUnsignedShort();
          sample.sampleType = chunk.readUnsignedShort();
          if (i != count - 1) this.samples.add(sample);
        }
      }
    }

    Iterator<SF2Layer> liter = this.layers.iterator();
    while (liter.hasNext()) {
      SF2Layer layer = liter.next();
      Iterator<SF2LayerRegion> siter = layer.regions.iterator();
      SF2Region globalsplit = null;
      while (siter.hasNext()) {
        SF2LayerRegion split = siter.next();
        if (split.generators.get(SF2LayerRegion.GENERATOR_SAMPLEID) != null) {
          int sampleid = split.generators.get(SF2LayerRegion.GENERATOR_SAMPLEID);
          split.generators.remove(SF2LayerRegion.GENERATOR_SAMPLEID);
          split.sample = samples.get(sampleid);
        } else {
          globalsplit = split;
        }
      }
      if (globalsplit != null) {
        layer.getRegions().remove(globalsplit);
        SF2GlobalRegion gsplit = new SF2GlobalRegion();
        gsplit.generators = globalsplit.generators;
        gsplit.modulators = globalsplit.modulators;
        layer.setGlobalZone(gsplit);
      }
    }

    Iterator<SF2Instrument> iiter = this.instruments.iterator();
    while (iiter.hasNext()) {
      SF2Instrument instrument = iiter.next();
      Iterator<SF2InstrumentRegion> siter = instrument.regions.iterator();
      SF2Region globalsplit = null;
      while (siter.hasNext()) {
        SF2InstrumentRegion split = siter.next();
        if (split.generators.get(SF2LayerRegion.GENERATOR_INSTRUMENT) != null) {
          int instrumentid = split.generators.get(SF2InstrumentRegion.GENERATOR_INSTRUMENT);
          split.generators.remove(SF2LayerRegion.GENERATOR_INSTRUMENT);
          split.layer = layers.get(instrumentid);
        } else {
          globalsplit = split;
        }
      }

      if (globalsplit != null) {
        instrument.getRegions().remove(globalsplit);
        SF2GlobalRegion gsplit = new SF2GlobalRegion();
        gsplit.generators = globalsplit.generators;
        gsplit.modulators = globalsplit.modulators;
        instrument.setGlobalZone(gsplit);
      }
    }
  }