private void writeSdtaChunk(RIFFWriter writer) throws IOException { byte[] pad = new byte[32]; RIFFWriter smpl_chunk = writer.writeChunk("smpl"); for (SF2Sample sample : samples) { ModelByteBuffer data = sample.getDataBuffer(); data.writeTo(smpl_chunk); /* smpl_chunk.write(data.array(), data.arrayOffset(), data.capacity()); */ smpl_chunk.write(pad); smpl_chunk.write(pad); } if (major < 2) return; if (major == 2 && minor < 4) return; for (SF2Sample sample : samples) { ModelByteBuffer data24 = sample.getData24Buffer(); if (data24 == null) return; } RIFFWriter sm24_chunk = writer.writeChunk("sm24"); for (SF2Sample sample : samples) { ModelByteBuffer data = sample.getData24Buffer(); data.writeTo(sm24_chunk); /* sm24_chunk.write(data.array(), data.arrayOffset(), data.capacity());*/ smpl_chunk.write(pad); } }
private void writeInfoStringChunk(RIFFWriter writer, String name, String value) throws IOException { if (value == null) return; RIFFWriter chunk = writer.writeChunk(name); chunk.writeString(value); int len = value.getBytes("ascii").length; chunk.write(0); len++; if (len % 2 != 0) chunk.write(0); }
public void write(AudioInputStream stream, RIFFWriter writer) throws IOException { RIFFWriter fmt_chunk = writer.writeChunk("fmt "); AudioFormat format = stream.getFormat(); fmt_chunk.writeUnsignedShort(3); // WAVE_FORMAT_IEEE_FLOAT fmt_chunk.writeUnsignedShort(format.getChannels()); fmt_chunk.writeUnsignedInt((int) format.getSampleRate()); fmt_chunk.writeUnsignedInt(((int) format.getFrameRate()) * format.getFrameSize()); fmt_chunk.writeUnsignedShort(format.getFrameSize()); fmt_chunk.writeUnsignedShort(format.getSampleSizeInBits()); fmt_chunk.close(); RIFFWriter data_chunk = writer.writeChunk("data"); byte[] buff = new byte[1024]; int len; while ((len = stream.read(buff, 0, buff.length)) != -1) data_chunk.write(buff, 0, len); data_chunk.close(); }
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]); }