Skip to content

Instantly share code, notes, and snippets.

@ab5tract
Created September 23, 2014 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ab5tract/94f7b4b88a186cec5651 to your computer and use it in GitHub Desktop.
Save ab5tract/94f7b4b88a186cec5651 to your computer and use it in GitHub Desktop.
//-----------------------------------------------------------------------------
// includes
//-----------------------------------------------------------------------------
#include "IndexOfChangedArray.h"
#include <vector>
//----------------------------------------------------------------------------
// create, general info and destroy methodes
//----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Create
void CreateModule (void* &pModule, AinsiCharPtr optionalString, LongBool Flag, MasterInfo* pMasterInfo, AinsiCharPtr optionalContent)
{
pModule = new IndexOfChangedArray();
}
// destroy
void DestroyModule(void* pModule)
{
// cast is important to call the good destructor
delete ((IndexOfChangedArray*)pModule);
}
//-------------------------------------------------------------------------
// module constructors/destructors
//-------------------------------------------------------------------------
// constructor
IndexOfChangedArray::IndexOfChangedArray()
{
hasRun = 0;
memoryBuffer.resize(MAX_ARRAY);
changedBuffer.resize(MAX_ARRAY);
}
// destructor
IndexOfChangedArray::~IndexOfChangedArray()
{
//
}
//void IndexOfChangedArray::onCreate(AinsiCharPtr optionalString);
//void IndexOfChangedArray::onDestroy();
void IndexOfChangedArray::onGetModuleInfo (MasterInfo* pMasterInfo, ModuleInfo* pModuleInfo)
{
pModuleInfo->Name = "index of changed array";
pModuleInfo->Description = "Index of Changed Array";
pModuleInfo->ModuleType = mtSimple;
pModuleInfo->BackColor = sdkGetUsineColor(clDataModuleColor);
pModuleInfo->NumberOfParams = 3;
pModuleInfo->Version = "1.0";
pModuleInfo->DontProcess = false;
}
//-----------------------------------------------------------------------------
// parameters and process
void IndexOfChangedArray::onGetParamInfo (int ParamIndex, TParamInfo* pParamInfo)
{
switch (ParamIndex)
{
// Add all parameters declared in the module class, example :
case 0:
pParamInfo->ParamType = ptArray;
pParamInfo->Caption = "array in";
pParamInfo->IsInput = true;
pParamInfo->IsOutput = false;
pParamInfo->IsSeparator = true;
pParamInfo->ReadOnly = false;
pParamInfo->MinValue = - FLT_MAX;
pParamInfo->MaxValue = FLT_MAX;
pParamInfo->CallBackType = ctImmediate;
break;
case 1:
pParamInfo->ParamType = ptDataField;
pParamInfo->Caption = "index";
pParamInfo->IsInput = false;
pParamInfo->IsOutput = true;
pParamInfo->ReadOnly = false;
pParamInfo->MinValue = 0;
pParamInfo->MaxValue = MAX_ARRAY;
break;
case 2:
pParamInfo->ParamType = ptDataField;
pParamInfo->Caption = "value";
pParamInfo->IsInput = false;
pParamInfo->IsOutput = true;
pParamInfo->ReadOnly = false;
pParamInfo->MinValue = - FLT_MAX;
pParamInfo->MaxValue = FLT_MAX;
break;
// default case
default:
break;
}
}
void IndexOfChangedArray::onSetEventAddress (int ParamIndex, UsineEventPtr pEvent)
{
switch( ParamIndex ) {
case 0:
arrInArray = pEvent;
break;
case 1:
dataIndexOut = pEvent;
break;
case 2:
dataValueOut = pEvent;
break;
default:
break;
}
}
void IndexOfChangedArray::onCallBack (UsineMessage *Message) {
if( (Message->wParam == 0) && (Message->lParam == MSG_CHANGE) ) {
int changes = 0;
int length = sdkGetEvtSize( arrInArray );
do_debug("length in callback ", length);
for( int i=0; i < length; i++ ) {
TPrecision value = sdkGetEvtArrayData( arrInArray, i );
if( value != memoryBuffer[i] ) {
changedBuffer[i] = bufPair(i,value);
changes++;
}
}
// push out the changed values
for( int c=0; c < changes; c++ ) {
bufPair changed = changedBuffer[c];
sdkSetEvtData( dataIndexOut, changed.first );
sdkSetEvtData( dataValueOut, changed.second );
}
}
}
void IndexOfChangedArray::onProcess () {
if( hasRun == 0 ) {
// sdkSetEvtData( dataIndexOut, 0 );
// sdkSetEvtData( dataValueOut, 0 );
sdkTraceLogChar("either this works or i quit");
int len = sdkGetEvtSize( arrInArray );
do_debug("initial length is ", len);
// memoryBuffer.reserve(len);
for( int i=0; i < len; i++ ) {
memoryBuffer[i] = sdkGetEvtArrayData( arrInArray, i );
}
hasRun = 1;
}
}
void IndexOfChangedArray::do_debug( std::string text, TPrecision value) {
std::ostringstream debugs;
debugs << text << value;
std::string what = debugs.str();
char *help = new char[what.length() + 1];
strcpy(help, what.c_str());
sdkTraceLogChar( help );
delete [] help;
}
//-----------------------------------------------------------------------------
// midi out callbacks
void IndexOfChangedArray::onMidiSendOut (int DeviceID, UsineMidiCode Code) {}
void IndexOfChangedArray::onMidiSysexSendOut (int DeviceID, char* Sysex) {}
//-----------------------------------------------------------------------------
// chunk system
int IndexOfChangedArray::onGetChunkLen (LongBool Preset) {return 0;}
void IndexOfChangedArray::onGetChunk (void* chunk, LongBool Preset) {}
void IndexOfChangedArray::onSetChunk (const void* chunk, int sizeInBytes, LongBool Preset) {}
//-----------------------------------------------------------------------------
// layout
void IndexOfChangedArray::onCreateSettings() {}
void IndexOfChangedArray::onSettingsHasChanged() {}
void IndexOfChangedArray::onPaint () {}
//-----------------------------------------------------------------------------
// mouse and multi touch interaction
void IndexOfChangedArray::onMouseMove(TShiftState Shift, float X, float Y) {}
void IndexOfChangedArray::onMouseDown(TMouseButton MouseButton, TShiftState Shift, float X,float Y) {}
void IndexOfChangedArray::onMouseUp (TMouseButton MouseButton, TShiftState Shift, float X,float Y) {}
void IndexOfChangedArray::onMouseMoveMultiProc(TShiftState Shift, UsineEventPtr X, UsineEventPtr Y) {}
void IndexOfChangedArray::onMouseDownMultiProc(TMouseButton MouseButton, TShiftState Shift, UsineEventPtr X, UsineEventPtr Y) {}
void IndexOfChangedArray::onMouseUpMultiProc (TMouseButton MouseButton, TShiftState Shift,UsineEventPtr X, UsineEventPtr Y) {}
void IndexOfChangedArray::onOpenEditor() {}
void IndexOfChangedArray::onBringToFront() {}
//-----------------------------------------------------------------------------
// audio setup update
void IndexOfChangedArray::onBlocSizeChange (int BlocSize) {}
void IndexOfChangedArray::onSampleRateChange (double SampleRate) {}
//-----------------------------------------------------------------------------
// recording
void IndexOfChangedArray::onSetRecordedValue (TPrecision X, TPrecision Y, TPrecision Z) {}
// include once, no more
#ifndef __INDEX_OF_CHANGED_ARRAY_H__
#define __INDEX_OF_CHANGED_ARRAY_H__
//-----------------------------------------------------------------------------
// includes
//-----------------------------------------------------------------------------
#include "../../sdk/UserDefinitions.h"
#include <iostream>
#include <sstream>
#include <cmath>
#include <limits>
// no unordered_map??
//#include <unordered_map>
#include <string>
#include <vector>
//-----------------------------------------------------------------------------
// defines and constantes
//-----------------------------------------------------------------------------
#define MAX_ARRAY 512
// defines and constantes goes here
//-----------------------------------------------------------------------------
// structures and typedef
//-----------------------------------------------------------------------------
// structures and typedef goes here
typedef std::pair<int,TPrecision> bufPair;
typedef std::vector<bufPair> ArrayBuffer;
// convenience functions
//-----------------------------------------------------------------------------
// class definition
//-----------------------------------------------------------------------------
class IndexOfChangedArray : public UserModuleBase
{
//-------------------------------------------------------------------------
// module constructors/destructors
//-------------------------------------------------------------------------
public:
// constructor
IndexOfChangedArray();
// destructor
virtual ~IndexOfChangedArray();
//-------------------------------------------------------------------------
// public methodes inherited from UserModule
//-------------------------------------------------------------------------
public:
//-----------------------------------------------------------------------------
// module info
void onGetModuleInfo (MasterInfo* pMasterInfo, ModuleInfo* pModuleInfo);
//-----------------------------------------------------------------------------
// parameters and process
void onGetParamInfo (int ParamIndex, TParamInfo* pParamInfo);
void onSetEventAddress (int ParamIndex, UsineEventPtr pEvent);
void onCallBack (UsineMessage *Message);
void onProcess ();
//-----------------------------------------------------------------------------
// midi out callbacks
void onMidiSendOut (int DeviceID, UsineMidiCode Code);
void onMidiSysexSendOut (int DeviceID, char* Sysex);
//-----------------------------------------------------------------------------
// chunk system
int onGetChunkLen (LongBool Preset);
void onGetChunk (void* chunk, LongBool Preset);
void onSetChunk (const void* chunk, int sizeInBytes, LongBool Preset);
//-----------------------------------------------------------------------------
// layout
void onCreateSettings();
void onSettingsHasChanged();
//void onResize (float contentWidth, float contentHeight);
//void onCreateCommands();
void onPaint ();
//-----------------------------------------------------------------------------
// mouse and multi touch interaction
void onMouseMove(TShiftState Shift, float X, float Y);
void onMouseDown(TMouseButton MouseButton, TShiftState Shift, float X,float Y);
void onMouseUp (TMouseButton MouseButton, TShiftState Shift, float X,float Y);
void onMouseMoveMultiProc(TShiftState Shift, UsineEventPtr X, UsineEventPtr Y);
void onMouseDownMultiProc(TMouseButton MouseButton, TShiftState Shift, UsineEventPtr X, UsineEventPtr Y);
void onMouseUpMultiProc (TMouseButton MouseButton, TShiftState Shift,UsineEventPtr X, UsineEventPtr Y);
void onOpenEditor();
void onBringToFront();
//-----------------------------------------------------------------------------
// audio setup update
void onBlocSizeChange (int BlocSize);
void onSampleRateChange (double SampleRate);
//-----------------------------------------------------------------------------
// recording
void onSetRecordedValue (TPrecision X, TPrecision Y, TPrecision Z);
//-------------------------------------------------------------------------
// public methodes
//-------------------------------------------------------------------------
public:
// public methodes goes here
//-------------------------------------------------------------------------
// protected members
//-------------------------------------------------------------------------
protected:
//-------------------------------------------------------------------------
// parameters events
UsineEventPtr arrInArray;
UsineEventPtr dataIndexOut;
UsineEventPtr dataValueOut;
// std::unordered_map<std::string, int> paramsMap;
bool hasRun;
// std::vector<TPrecision> memoryBuffer;
// TPrecision memoryBuffer[MAX_ARRAY];
std::vector<TPrecision> memoryBuffer;
ArrayBuffer changedBuffer;
void do_debug( std::string text, TPrecision value);
// protected members goes here
//-------------------------------------------------------------------------
// private methodes
//-------------------------------------------------------------------------
private:
// private methodes goes here
}; // class IndexOfChangedArray
#endif //__INDEX_OF_CHANGED_ARRAY_H__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment