Cockos : REAPER : NINJAM : Forums
Forum Home : Register : FAQ : Members List : Search :
Old 02-08-2017, 04:41 PM   #1
Human being with feelings
Youlean's Avatar
Join Date: May 2015
Location: Serbia
Posts: 579
Default VST3 using vectors in chunks

VST3 requires to have fixed buffers in chunks, but this code change will enable you to use vectors that you may dynamically resize. NOTE: This will break old save states as it is inserting chink size at chunk beginning.

tresult PLUGIN_API IPlugVST3::setEditorState(IBStream* state)
  WDL_MutexLock lock(&mMutex);

  ByteChunk chunk;

  // Get the chunk size
  int chunkSize;
  state->read(&chunkSize, sizeof(int));

  if (chunkSize > 0)

    // Resize IPlug ByteChunk to fit all new information

    state->read(chunk.GetBytes(), chunk.Size());
    UnserializeState(&chunk, 0);
    int32 savedBypass = 0;
    if (state->read (&savedBypass, sizeof (int32)) != kResultOk)
      return kResultFalse;
    mIsBypassed = (bool) savedBypass;
    return kResultOk;

  return kResultFalse;

tresult PLUGIN_API IPlugVST3::getEditorState(IBStream* state)
  WDL_MutexLock lock(&mMutex);

  ByteChunk chunk;

  int chunkSize = 0;
  int64 stateStartPosition, stateEndPosition;
  // Get the position at the start

  // Leave room to write chunk size at the beginning. 
  // Write dummy chunk size that will be overwritten at the end.
  state->write(&chunkSize, sizeof(int));

  if (SerializeState(&chunk))
    state->write(chunk.GetBytes(), chunk.Size());
    return kResultFalse;

  // Get the position at the end

  // Move to beginning to write the chunk size
  state->seek(stateStartPosition, IBStream::IStreamSeekMode::kIBSeekSet);

  // Write chunk size at the beginning
  chunkSize = chunk.Size();
  state->write(&chunkSize, sizeof(int));

  // Return position to the end
  state->seek(stateEndPosition, IBStream::IStreamSeekMode::kIBSeekSet);
  int32 toSaveBypass = mIsBypassed ? 1 : 0;
  state->write(&toSaveBypass, sizeof (int32));  

  return kResultOk;

Last edited by Youlean; 02-08-2017 at 04:49 PM.
Youlean is offline   Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -7. The time now is 08:34 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2018, vBulletin Solutions Inc.