Chunks

see chunks

ValueInput.WriteToChunk('demo.chunk', 'fader');
ValueDisplay.ReadFromChunk('demo.chunk', 'fader');
Procedure Description
procedure TParameter.ReadFromChunk(ChunkName, ItemName: string); Read a parameter value from a specified chunk and item
procedure TParameter.WriteToChunk(ChunkName, ItemName: string); Write a parameter value to a specified chunk and item
procedure SubscribeChunk(ChunkName, ItemName: string; CallBackID: uint32; Immediate: boolean); Subscribe to changes in a chunk item (the Immediate parameter has no effect)
procedure unSubscribeAll(); Unsubscribe from all previously subscribed chunks
procedure ClearChunkItem(ChunkName, ItemName: string); Empty the content of a chunk item (length 0); the item entry itself remains and ChunkItemExist still returns True — use DeleteChunkItem to remove the entry
procedure DeleteChunkItem(ChunkName, ItemName: string); Remove an item from a chunk; ChunkItemExist then returns False
procedure ClearChunk(ChunkName: string); Clear all data within a specified chunk
function ChunkExist(ChunkName: string): boolean; Return True if the specified chunk exists
function ChunkItemExist(ChunkName, ItemName: string): boolean; Return True if the specified item exists within the chunk
////////////////////////////////////////////////////////////////////////////////
// Chunk Management Demo - demonstrates chunk subscriptions and read/write
// helpers. Use the controls to push a value to a chunk, read it back, and clear
// chunk items while observing callback notifications.
////////////////////////////////////////////////////////////////////////////////

const CHUNK_NAME      = 'demo.chunk';
const CHUNK_ITEM      = 'fader';
const CALLBACK_ID     = $A01;  // not a parameter number: N would also match a parameter

var
  WriteBtn      : TParameter;
  ReadBtn       : TParameter;
  ClearItemBtn  : TParameter;
  ClearChunkBtn : TParameter;
  ValueInput    : TParameter;
  ValueDisplay  : TParameter;
  InfoText      : TParameter;

procedure UpdateInfo(const msg: string);
begin
  InfoText.asString(msg);
end;

procedure PushToChunk;
begin
  ValueInput.WriteToChunk(CHUNK_NAME, CHUNK_ITEM);
  UpdateInfo('Value written to chunk "' + CHUNK_NAME + '" item "' + CHUNK_ITEM + '".');
end;

procedure PullFromChunk;
begin
  ValueDisplay.ReadFromChunk(CHUNK_NAME, CHUNK_ITEM);
  UpdateInfo('Value read from chunk "' + CHUNK_NAME + '" item "' + CHUNK_ITEM + '".');
end;

procedure Init;
begin
  ModuleColor($FF7E57C2);

  WriteBtn      := CreateParam('write chunk',  ptButton,    pioInput);
  ReadBtn       := CreateParam('read chunk',   ptButton,    pioInput);
  ClearItemBtn  := CreateParam('clear item',   ptButton,    pioInput);
  ClearChunkBtn := CreateParam('clear chunk',  ptButton,    pioInput);
  ValueInput    := CreateParam('value in',     ptDataFader, pioInput);
  ValueDisplay  := CreateParam('value out',    ptDataField, pioOutput);
  InfoText      := CreateParam('info',         ptTextField, pioOutput);

  ValueInput.Min(0);
  ValueInput.Max(1);
  ValueInput.AsFloat(0.5);

  ValueDisplay.Min(0);
  ValueDisplay.Max(1);

  UpdateInfo('Press "write chunk" to store the current value.');

  // Subscribe to chunk changes so the Callback gets triggered on updates.
  SubscribeChunk(CHUNK_NAME, CHUNK_ITEM, CALLBACK_ID, true);
end;

procedure Destroy;
begin
  // Unsubscribe and clear chunk data when this script is released.
  unSubscribeAll;
  ClearChunk(CHUNK_NAME);
end;

procedure Callback(N: integer);
begin
  if N = WriteBtn then
    PushToChunk;

  if N = ReadBtn then
    PullFromChunk;

  if N = ClearItemBtn then
  begin
    ClearChunkItem(CHUNK_NAME, CHUNK_ITEM);
    UpdateInfo('Chunk item "' + CHUNK_ITEM + '" cleared.');
    ValueDisplay.asFloat(0);
  end;

  if N = ClearChunkBtn then
  begin
    ClearChunk(CHUNK_NAME);
    UpdateInfo('Entire chunk "' + CHUNK_NAME + '" cleared.');
    ValueDisplay.asFloat(0);
  end;

  // Handle the chunk subscription callback (CALLBACK_ID).
  if N = CALLBACK_ID then
  begin
    ValueDisplay.ReadFromChunk(CHUNK_NAME, CHUNK_ITEM);
    UpdateInfo('Callback received for chunk update; value refreshed.');
  end;
end;

TParameter.WriteToChunk

procedure TParameter.WriteToChunk (ChunkName,ItemName : string);

Copy parameter into a named chunk.

//write to a chunk
PROCEDURE PROCESS();                                          
BEGIN   
  // the item is rewritten in place; a write that changes nothing notifies nobody
  input.WriteToChunk('MYCHUNK','item1');
END;      

TParameter.ReadFromChunk

procedure TParameter.ReadFromChunk (ChunkName,ItemName : string);

Restores a parameter from a chunk entry.

//read a chunk
PROCEDURE PROCESS();                                          
BEGIN   
   output.ReadFromChunk('MYCHUNK','item2');
END;

ClearChunkItem

procedure ClearChunkItem(ChunkName, ItemName: string);

Clears a specific item within a chunk

ClearChunkItem('ArpState', 'notes');

DeleteChunkItem

procedure DeleteChunkItem(ChunkName, ItemName: string);

Deletes a specific item within a chunk

DeleteChunkItem('ArpState', 'notes');

ClearChunk

procedure ClearChunk(ChunkName: string);

Clear all data within a specified chunk.

ClearChunk('ArpState');

ChunkExist

function ChunkExist(ChunkName: string): boolean;

Returns TRUE if the specified chunk exists. Unlike the read/write helpers, it never creates the chunk.

if ChunkExist('ArpState') then
  ValueDisplay.ReadFromChunk('ArpState', 'notes');

ChunkItemExist

function ChunkItemExist(ChunkName, ItemName: string): boolean;

Returns TRUE if the specified item exists within the chunk. Returns FALSE when the chunk itself does not exist.

if ChunkItemExist('ArpState', 'notes') then
  ValueDisplay.ReadFromChunk('ArpState', 'notes');

SubscribeChunk

procedure SubscribeChunk(ChunkName, ItemName: string; CallBackID: uint32; Immediate: boolean);

Registers a callback to receive notifications when a module alters a chunk. The script is informed of the change by sending a message in the callback procedure. If ItemName is empty, the script subscribes to any change of the chunk (any item). It is then also called when items are added to or removed from the chunk, and when the chunk is destroyed. A subscription to one item is not called when ClearChunk empties the whole chunk, nor when the chunk is destroyed. Subscriptions are kept by name: they work again once a chunk of that name is created again.

A write notifies only when it changes the content of the item: writing the value the item already holds sends no notification.

The Immediate parameter has no effect. Every notification goes through the real-time message queue: the CALLBACK runs on the real-time queue thread, usually within about 10 ms of the change (more under load), never on the audio thread or the main thread. Several changes notified within the same cycle with the same CallBackID can give a single call.

var output : TParameter; 

//INIT PROCEDURE
PROCEDURE INIT();
BEGIN    
  output:=CreateParam('out',ptDataFader,pioOutput);
  SubscribeChunk('QSDF','i1',$A01,true);
END;

//CALLBACK         
PROCEDURE CALLBACK(N:INTEGER);
BEGIN
  CASE N OF
   $A01: Begin     
          output.ReadFromChunk('QSDF','i1');
          End;
  End;//END MAIN CASE            
END;

UnSubscribeAll

procedure UnSubscribeAll();

Cancels every chunk subscription.

Procedure Destroy;
Begin
   UnSubscribeAll;
End;

more about scripts

version 7.0.250921

Edit All Pages