Help! Store/restore whole machine state via CMachine pointer?

Post Reply
User avatar
IXix
Posts: 1113
Joined: Wed Nov 23, 2011 3:24 pm

Help! Store/restore whole machine state via CMachine pointer?

Post by IXix »

The peer code can capture machine states by taking a snapshot of the params and attributes but that doesn't work for VSTs. It can work to an extent if you've mapped all the VST controls to machine parameters but even then, many VSTs have extra state information that can't be stored by just looking at the params. I need access to CMachineInterface::Save/Load() to dump the whole state into a buffer but I don't think I can get to them from a CMachine*. Is it possible to get a CMachineInterface* for a machine from its CMachine* or is there another way that I could store the full state of a VST?

Really need this!
User avatar
IXix
Posts: 1113
Joined: Wed Nov 23, 2011 3:24 pm

Re: Help! Store/restore whole machine state via CMachine pointer?

Post by IXix »

If I was in .NET I could use the preset system but I'm not. Could I get to that from my C++ machine somehow? I never managed to get my head around getting C++ and .NET to cooperate. :?:
User avatar
UNZ
Posts: 808
Joined: Mon Nov 21, 2011 9:42 pm
Contact:

Re: Help! Store/restore whole machine state via CMachine pointer?

Post by UNZ »

from the buzé source code (i think..)

class CMachine {
public:
// Jeskola Buzz compatible CMachine header.
char _placeholder[16] = { 0 };
const char* _internal_name = ""; // 0x14: polac's VST reads this string, set to 0
char _placeholder2[52] = { 0 };
CMachineInterface* interface_machine = nullptr; //scanned for by some plugins
CMachineInterfaceEx* interface_machine_ex = nullptr; // 0x50: not scanned for?
char _placeholder3[20] = { 0 };
char* _internal_global_state = nullptr; // 0x68: copy of machines global state
char* _internal_track_state = nullptr; // 0x6C: copy of machines track state
char _placeholder4[120] = { 0 };
int _internal_seqCommand = 0; // 0xE8: used by mooter, 0 = --, 1 = mute, 2 = thru
char _placeholder6[17] = { 0 };
bool hardMuted = false; // 0xFD: true when muted by user, used by mooter
// End of Buzz compatible header

CMachine()
{}

virtual ~CMachine()
{}
};

so you should be able to cast your CMachine* to a class that you define (lets call it CMachineHeader) with the above layout (maybe needs some special #pragma pack attributes, not sure, it just worked here...) and then access the CMachineInterface* etc
User avatar
UNZ
Posts: 808
Joined: Mon Nov 21, 2011 9:42 pm
Contact:

Re: Help! Store/restore whole machine state via CMachine pointer?

Post by UNZ »

if you do this, it might be a good idea to use the Lock() and Unlock() while loading / saving (or even when calling anything on your illicitly casted CMachine that you're not supposed to have access to) to not interfere with buzz calling stuff at the same time. haven't looked that deep into it yet, but you might want to look at what buzé does when it calls load / save. All in all i think at this point its OK to resort to hacks like this.
User avatar
IXix
Posts: 1113
Joined: Wed Nov 23, 2011 3:24 pm

Re: Help! Store/restore whole machine state via CMachine pointer?

Post by IXix »

UNZ wrote: Wed Sep 08, 2021 9:16 pm from the buzé source code (i think..)

class CMachine {
...
};
Thanks UNZ! I wondered about some kind of hack. I think the old peerlib did something similar in the old days before oskari came back to Buzz. I was hoping there might be a non-hack way but it's very quiet around here these days. :(
oskari
Site Admin
Posts: 296
Joined: Mon Nov 21, 2011 2:04 pm

Re: Help! Store/restore whole machine state via CMachine pointer?

Post by oskari »

i have moved on to rust
User avatar
IXix
Posts: 1113
Joined: Wed Nov 23, 2011 3:24 pm

Re: Help! Store/restore whole machine state via CMachine pointer?

Post by IXix »

oskari wrote: Thu Sep 09, 2021 4:12 pm i have moved on to rust
Ha, are you referring to the language or is that some kind of metaphor? :D

I hope you're fit and well in these weird times. Buzz has been keeping me sane. :D
User avatar
IXix
Posts: 1113
Joined: Wed Nov 23, 2011 3:24 pm

Re: Help! Store/restore whole machine state via CMachine pointer?

Post by IXix »

oskari wrote: Thu Sep 09, 2021 4:12 pm i have moved on to rust
I don't know much about rust. Are you using it for audio stuff?
User avatar
UNZ
Posts: 808
Joined: Mon Nov 21, 2011 9:42 pm
Contact:

Re: Help! Store/restore whole machine state via CMachine pointer?

Post by UNZ »

IXix wrote: Thu Sep 09, 2021 11:11 am
UNZ wrote: Wed Sep 08, 2021 9:16 pm from the buzé source code (i think..)

class CMachine {
...
};
Thanks UNZ! I wondered about some kind of hack. I think the old peerlib did something similar in the old days before oskari came back to Buzz. I was hoping there might be a non-hack way but it's very quiet around here these days. :(
yeah it's the same approach i guess. i think it's ok to peek into the cmachine and get that pointer. if there will ever be an official api for that (or just for load/save) it would be easy to switch to that anyway (like it happened for peer machines)..
User avatar
IXix
Posts: 1113
Joined: Wed Nov 23, 2011 3:24 pm

Re: Help! Store/restore whole machine state via CMachine pointer?

Post by IXix »

Spent a while playing with this and basically just managed to crash Buzz a lot. :lol: I got access to the Save/Load methods in 32bit but my attempts to load data back into a machine have been rather unsuccessful so far and the interface pointers aren't working at all in 64bit.

It would be awesome to get this working in Magic but I'm starting to think I might be better off making a .NET rip-off of PeerState.
User avatar
UNZ
Posts: 808
Joined: Mon Nov 21, 2011 9:42 pm
Contact:

Re: Help! Store/restore whole machine state via CMachine pointer?

Post by UNZ »

IXix wrote: Fri Sep 10, 2021 11:30 am the interface pointers aren't working at all in 64bit.
this is probably due to packing or alignment (pointers are twice the size etc) or a completely different layout of the x64 class to that of the 32bit class. Since CMachine layout is not part of the official API, it would have been fair game to change it for x64. I guess the CMachineInterface pointer is still in there tough. Just take your cmachine pointer and try to figure out at what byte offset exactly the cmachineinterface pointer is (in the debugger or so, by trying to cast and see if sensible values come out) then make a separate header class based on your findings. i would try get this rock solid in 32bit before even thinking about x64.

as for crashing: probably needs locks and/or being on the right thread to call these methods, i doubt they are called from audio thread so you might need to dispatch them to another thread (probably main or gui thread?). Also, i'm not sure if what you're supposed to do is just feed the CMachineOuput* data back simply as casted CMachineInput*, or if maybe there is also some offset or so into the CMachineOutput data to consider?

is this code up somewhere, i might be able to take a look. Pretty sure we can figure this out looking at buzé.
User avatar
IXix
Posts: 1113
Joined: Wed Nov 23, 2011 3:24 pm

Re: Help! Store/restore whole machine state via CMachine pointer?

Post by IXix »

UNZ wrote: Fri Sep 10, 2021 12:55 pmthis is probably due to packing or alignment...
Thanks, good advice. I was locking but hadn't considered putting it on a different thread, that could well be the problem. Or it might just be that I suck at programming. Either way, I think I'll branch Magic for trying to hack this out and see about a making managed PeerState equivalent as well. :)
User avatar
IXix
Posts: 1113
Joined: Wed Nov 23, 2011 3:24 pm

Re: Help! Store/restore whole machine state via CMachine pointer?

Post by IXix »

UNZ wrote: Fri Sep 10, 2021 12:55 pmis this code up somewhere, i might be able to take a look. Pretty sure we can figure this out looking at buzé.
Sorry, missed that sentence! :lol: I'll make a simple test machine for this and put it on GitHub.
User avatar
UNZ
Posts: 808
Joined: Mon Nov 21, 2011 9:42 pm
Contact:

Re: Help! Store/restore whole machine state via CMachine pointer?

Post by UNZ »

btw i assume you inherited from class CMachineDataOutput and implemented Write(void *pbuf, int const numbytes) yeah? and making sure there's enough backing storage obviously.
User avatar
IXix
Posts: 1113
Joined: Wed Nov 23, 2011 3:24 pm

Re: Help! Store/restore whole machine state via CMachine pointer?

Post by IXix »

UNZ wrote: Fri Sep 10, 2021 2:36 pm btw i assume you inherited from class CMachineDataOutput and implemented Write(void *pbuf, int const numbytes) yeah? and making sure there's enough backing storage obviously.
Yup, I'm almost competent. :lol:
Post Reply