RivenScriptPtr RivenScriptManager::createScriptFromData(uint16 commandCount, ...) { va_list args; va_start(args, commandCount); // Build a script from the variadic arguments Common::MemoryWriteStreamDynamic writeStream = Common::MemoryWriteStreamDynamic(DisposeAfterUse::YES); writeStream.writeUint16BE(commandCount); for (uint i = 0; i < commandCount; i++) { uint16 command = va_arg(args, int); writeStream.writeUint16BE(command); if (command == kRivenCommandSwitch) { // The switch command has a different format that is not implemented error("Cannot create a Switch command from data"); } uint16 argumentCount = va_arg(args, int); writeStream.writeUint16BE(argumentCount); for (uint j = 0; j < commandCount; j++) { uint16 argument = va_arg(args, int); writeStream.writeUint16BE(argument); } } va_end(args); Common::MemoryReadStream readStream = Common::MemoryReadStream(writeStream.getData(), writeStream.size()); return readScript(&readStream); }
static uint32 convertSND2MIDI(byte *snddata, byte **data) { int32 lp, ep; int n; double ll; Common::MemoryWriteStreamDynamic st; ll = log10(pow(2.0, 1.0 / 12.0)); /* Header */ st.write("MThd", 4); st.writeUint32BE(6); st.writeUint16BE(1); /* mode */ st.writeUint16BE(3); /* number of tracks */ st.writeUint16BE(192); /* ticks / quarter */ fprintf(_fd, "window.sound%d = [\n", _index); for (n = 0; n < 3; n++) { uint16 start, end, pos; st.write("MTrk", 4); lp = st.pos(); st.writeUint32BE(0); /* chunklength */ writeDelta(&st, 0); /* set instrument */ st.writeByte(0xc0 + n); st.writeByte(instr[n]); start = snddata[n * 2 + 0] | (snddata[n * 2 + 1] << 8); end = ((snddata[n * 2 + 2] | (snddata[n * 2 + 3] << 8))) - 5; fprintf(_fd, " ["); bool first = true; for (pos = start; pos < end; pos += 5) { uint16 freq, dur; dur = (snddata[pos + 0] | (snddata[pos + 1] << 8)) * SPEED_FACTOR; freq = ((snddata[pos + 2] & 0x3F) << 4) + (snddata[pos + 3] & 0x0F); if (snddata[pos + 2] > 0) { double fr; int note; /* I don't know, what frequency equals midi note 0 ... */ /* This moves the song 4 octaves down: */ fr = (log10(111860.0 / (double)freq) / ll) - 48; note = (int)floor(fr + 0.5); if (note < 0) note = 0; if (note > 127) note = 127; /* note on */ writeDelta(&st, 0); st.writeByte(144 + n); st.writeByte(note); st.writeByte(100); /* note off */ writeDelta(&st, dur); st.writeByte(128 + n); st.writeByte(note); st.writeByte(0); if ( first ) first = false; else fprintf(_fd, ","); fprintf(_fd, "%d,%d", note, dur/SPEED_FACTOR); } else { /* note on */ writeDelta(&st, 0); st.writeByte(144 + n); st.writeByte(0); st.writeByte(0); /* note off */ writeDelta(&st, dur); st.writeByte(128 + n); st.writeByte(0); st.writeByte(0); if ( first ) first = false; else fprintf(_fd, ","); fprintf(_fd, "%d,%d", -1, dur/SPEED_FACTOR); } } fprintf(_fd, n<2 ? "],\n" : "]\n"); writeDelta(&st, 0); st.writeByte(0xff); st.writeByte(0x2f); st.writeByte(0x0); ep = st.pos(); st.seek(lp, SEEK_SET); st.writeUint32BE((ep - lp) - 4); st.seek(ep, SEEK_SET); } fprintf(_fd, "];\n"); // *data = st.getData(); return st.pos(); }