Loading a Binary file as a FXP into a VST setChunk

Ok, this is a bit tricky. I have a wusikprst file, which is actually what I use to read and use as a setChunk for a VST. So, all I need to do is read it to memory and send to a setChunk. But I don’t want to over-do any code. From what I see the setStateInformation() would do the job, as long as I convert the read information into a FXP/FXB format, right? Here’s the code I’m trying to use on Windows, it crashes every time. :oops:

[code] MemoryBlock dest;

			MemoryBlock chunk;
			fileLoad.loadFileAsData(chunk);

			const int totalLen = sizeof (fxChunkSet) + chunk.getSize() - 8;
			dest.setSize (totalLen, true);

			fxChunkSet* const set = (fxChunkSet*) dest.getData();
			set->chunkMagic = vst_swap ('CcnK');
			set->byteSize = 0;
			set->fxMagic = vst_swap ('FBCh');
			set->version = vst_swap (fxbVersionNum);
			set->fxID = vst_swap ('Wskk');
			set->fxVersion = vst_swap (1);
			set->numPrograms = vst_swap (512);
			set->chunkSize = vst_swap (chunk.getSize());

			chunk.copyTo (set->chunk, 0, chunk.getSize());

			getFilter()->VstInstance->setStateInformation((void*)set,totalLen);[/code]

What I’m missing?

In any event, tomorrow I will try to debug this further and see what I’m doing wrong. :wink:

Wk

chunk.copyTo (set->chunk, 0, chunk.getSize()); 

what’s set->chunk (what does it point to) and why i are you trying to set the copied data size to the size of the holding buffer.

set a breakpoint at the beginning and you’ll see where it’s crashing.

If it’s all your code, it should surely be easy to debug into the code and see where it’s going bang…

:oops: :oops: :oops: Yup, I found the problem. I should be doing a Preset Chunk, and NOT a Bank of Chunks. :oops: :oops: :oops: :oops:

Ok, let me post my finished code, could be useful to others. :wink:

Wk

Here’s the working code. It will read a WusikPRST file, which is actually a Chunk Data saved to a Binary file, and output a MemoryBlock ready to be sent to the setInformationSate. 8)

[code]void getFileToFXP (MemoryBlock& dest, File& fileLoad)
{
dest.setSize (sizeof (fxProgramSet_convert), true);

fxProgramSet_convert* const set = (fxProgramSet_convert*) dest.getData();
set->chunkMagic = vst_swap ('CcnK');
set->byteSize = 0;
set->fxMagic = vst_swap ('FPCh');
set->version = vst_swap (fxbVersionNum);
set->fxID = vst_swap ('82n8');
set->fxVersion = vst_swap (1);
set->numPrograms = vst_swap (512);

MemoryBlock chunk;
fileLoad.loadFileAsData(chunk);
set->chunkSize = vst_swap (chunk.getSize());
dest.append(chunk.getData(),chunk.getSize());

}[/code]

Here’s the headers:

[code] struct fxProgramSet_convert
{
long chunkMagic; // 'CcnK’
long byteSize; // of this chunk, excl. magic + byteSize
long fxMagic; // ‘FxCh’, ‘FPCh’, or 'FBCh’
long version;
long fxID; // fx unique id
long fxVersion;
long numPrograms;
char name[28];
long chunkSize;
};

#ifdef JUCE_LITTLE_ENDIAN
 static long vst_swap (const long x) throw()    { return (long) swapByteOrder ((uint32) x); }

 static float vst_swapFloat (const float x) throw()
 {
	 union { uint32 asInt; float asFloat; } n;
	 n.asFloat = x;
	 n.asInt = swapByteOrder (n.asInt);
	 return n.asFloat;
 }
#else
 #define vst_swap(x) (x)
 #define vst_swapFloat(x) (x)
#endif

const int fxbVersionNum = 1;[/code]

Here’s the usage:

MemoryBlock dest; getFileToFXP(dest,fileLoad); getFilter()->VstInstance->setStateInformation(dest.getData(),dest.getSize());

Thanks again guys.

Wk

Here’s another useful code, for those who need to convert something to a FXB/FXP format. I had to use this for when I load an old Wusik Station project. So it would detect it wasn’t in the Juce FXB/FXP format and convert if required.

[code]void convertDataToFXB (MemoryBlock& dest, const void* data, int sizeInBytes)
{
// First Check if its not a FXP already //
if (sizeInBytes > 28)
{
fxProgramSet_convert* set = (fxProgramSet_convert*) data;
if (set->chunkMagic == vst_swap (‘CcnK’))
{
dest.append((const void*)data,sizeInBytes);
return; // Its already a FXP //
}
}

// Otherwise, convert to FXP //
dest.setSize (sizeof (fxChunkSet_convert), true);

fxChunkSet_convert* const set = (fxChunkSet_convert*) dest.getData();
set->chunkMagic = vst_swap ('CcnK');
set->byteSize = 0;
set->fxMagic = vst_swap ('FBCh');
set->version = vst_swap (fxbVersionNum);
set->fxID = vst_swap ('82n8');
set->fxVersion = vst_swap (1);
set->numPrograms = vst_swap (512);

set->chunkSize = vst_swap (sizeInBytes);
dest.append((const void*)data,sizeInBytes);

}[/code]

Some headers:

[code]struct fxChunkSet_convert
{
long chunkMagic; // 'CcnK’
long byteSize; // of this chunk, excl. magic + byteSize
long fxMagic; // ‘FxCh’, ‘FPCh’, or 'FBCh’
long version;
long fxID; // fx unique id
long fxVersion;
long numPrograms;
char future[128];
long chunkSize;
};

void convertDataToFXB (MemoryBlock& dest, const void* data, int sizeInBytes);[/code]

Plus the headers from the previous code I posted above.

Now, here’s how I use this:

void FilterComponent::setStateInformation (const void* data, int sizeInBytes) { if (VstInstance != 0) { MemoryBlock dest; convertDataToFXB(dest,data,sizeInBytes); VstInstance->setStateInformation (dest.getData(),dest.getSize()); } }