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); } }
public int write(AudioInputStream stream, Type fileType, File out) throws IOException { checkFormat(fileType, stream); if (stream.getFormat().isBigEndian()) stream = toLittleEndian(stream); RIFFWriter writer = new RIFFWriter(out, "WAVE"); write(stream, writer); int fpointer = (int) writer.getFilePointer(); writer.close(); return fpointer; }
private void writeModulators(RIFFWriter writer, List<SF2Modulator> modulators) throws IOException { for (SF2Modulator modulator : modulators) { writer.writeUnsignedShort(modulator.sourceOperator); writer.writeUnsignedShort(modulator.destinationOperator); writer.writeShort(modulator.amount); writer.writeUnsignedShort(modulator.amountSourceOperator); writer.writeUnsignedShort(modulator.transportOperator); } }
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 static void main(String[] args) throws Exception { RIFFWriter writer = null; RIFFReader reader = null; File tempfile = File.createTempFile("test", ".riff"); try { writer = new RIFFWriter(tempfile, "TEST"); RIFFWriter chunk = writer.writeChunk("TSCH"); chunk.writeByte((byte) 33); writer.close(); writer = null; FileInputStream fis = new FileInputStream(tempfile); reader = new RIFFReader(fis); assertEquals(reader.getFormat(), "RIFF"); assertEquals(reader.getType(), "TEST"); RIFFReader readchunk = reader.nextChunk(); assertEquals(readchunk.getFormat(), "TSCH"); assertEquals((int) reader.readByte(), 33); fis.close(); reader = null; } finally { if (writer != null) writer.close(); if (reader != null) reader.close(); if (tempfile.exists()) if (!tempfile.delete()) tempfile.deleteOnExit(); } }
private void writeGenerators(RIFFWriter writer, Map<Integer, Short> generators) throws IOException { Short keyrange = (Short) generators.get(SF2Region.GENERATOR_KEYRANGE); Short velrange = (Short) generators.get(SF2Region.GENERATOR_VELRANGE); if (keyrange != null) { writer.writeUnsignedShort(SF2Region.GENERATOR_KEYRANGE); writer.writeShort(keyrange); } if (velrange != null) { writer.writeUnsignedShort(SF2Region.GENERATOR_VELRANGE); writer.writeShort(velrange); } for (Map.Entry<Integer, Short> generator : generators.entrySet()) { if (generator.getKey() == SF2Region.GENERATOR_KEYRANGE) continue; if (generator.getKey() == SF2Region.GENERATOR_VELRANGE) continue; writer.writeUnsignedShort(generator.getKey()); writer.writeShort(generator.getValue()); } }
private void writeInfo(RIFFWriter writer) throws IOException { if (this.targetEngine == null) this.targetEngine = "EMU8000"; if (this.name == null) this.name = ""; RIFFWriter ifil_chunk = writer.writeChunk("ifil"); ifil_chunk.writeUnsignedShort(this.major); ifil_chunk.writeUnsignedShort(this.minor); writeInfoStringChunk(writer, "isng", this.targetEngine); writeInfoStringChunk(writer, "INAM", this.name); writeInfoStringChunk(writer, "irom", this.romName); if (romVersionMajor != -1) { RIFFWriter iver_chunk = writer.writeChunk("iver"); iver_chunk.writeUnsignedShort(this.romVersionMajor); iver_chunk.writeUnsignedShort(this.romVersionMinor); } writeInfoStringChunk(writer, "ICRD", this.creationDate); writeInfoStringChunk(writer, "IENG", this.engineers); writeInfoStringChunk(writer, "IPRD", this.product); writeInfoStringChunk(writer, "ICOP", this.copyright); writeInfoStringChunk(writer, "ICMT", this.comments); writeInfoStringChunk(writer, "ISFT", this.tools); writer.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]); }
private void writeSoundbank(RIFFWriter writer) throws IOException { writeInfo(writer.writeList("INFO")); writeSdtaChunk(writer.writeList("sdta")); writePdtaChunk(writer.writeList("pdta")); writer.close(); }
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(); }