Skip to content

Commit ed4c94b

Browse files
author
mars
committed
changed to signed 16bit parameters, updates to checksum
1 parent 73fd4d0 commit ed4c94b

11 files changed

Lines changed: 87 additions & 97 deletions

File tree

HeavySource/HvMessage.c

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
HvMessage *msg_init(HvMessage *m, hv_size_t numElements, hv_uint32_t timestamp) {
2121
m->timestamp = timestamp;
2222
m->numElements = (hv_uint16_t) numElements;
23-
m->numBytes = (hv_uint16_t) msg_getByteSize(numElements);
23+
m->numBytes = (hv_uint16_t) msg_getCoreSize(numElements);
2424
return m;
2525
}
2626

@@ -43,7 +43,7 @@ HvMessage *msg_initWithBang(HvMessage *m, hv_uint32_t timestamp) {
4343
HvMessage *msg_initWithSymbol(HvMessage *m, hv_uint32_t timestamp, char *s) {
4444
m->timestamp = timestamp;
4545
m->numElements = 1;
46-
m->numBytes = sizeof(HvMessage);
46+
m->numBytes = sizeof(HvMessage) + (hv_uint16_t) hv_strlen(s);
4747
msg_setSymbol(m, 0, s);
4848
return m;
4949
}
@@ -56,51 +56,18 @@ HvMessage *msg_initWithHash(HvMessage *m, hv_uint32_t timestamp, hv_uint32_t h)
5656
return m;
5757
}
5858

59-
HvMessage *msg_initV(HvMessage *const m, const hv_uint32_t timestamp, const char *format, ...) {
60-
va_list ap;
61-
va_start(ap, format);
62-
63-
const int numElem = (int) hv_strlen(format);
64-
msg_init(m, numElem, timestamp);
65-
for (int i = 0; i < numElem; i++) {
66-
switch (format[i]) {
67-
case 'b': msg_setBang(m,i); break;
68-
case 'f': msg_setFloat(m, i, (float) va_arg(ap, double)); break;
69-
case 's': msg_setSymbol(m, i, (char *) va_arg(ap, char *)); break;
70-
case 'h': // hash not supported
71-
default: break;
72-
}
73-
}
74-
va_end(ap);
75-
76-
return m;
77-
}
78-
79-
hv_size_t msg_getNumHeapBytes(const HvMessage *m) {
80-
// get the size of all symbol elements
81-
hv_size_t rsizeofsym = 0;
82-
for (int i = 0; i < msg_getNumElements(m); ++i) {
83-
if (msg_isSymbol(m,i)) {
84-
rsizeofsym += (hv_size_t) hv_strlen(msg_getSymbol(m,i)) + 1; // +1 to allow for trailing '\0'
85-
}
86-
}
87-
88-
// the total byte size on the heap
89-
return (msg_getByteSize(msg_getNumElements(m)) + rsizeofsym);
90-
}
91-
9259
void msg_copyToBuffer(const HvMessage *m, char *buffer, hv_size_t len) {
9360
HvMessage *r = (HvMessage *) buffer;
9461

62+
hv_size_t len_r = msg_getCoreSize(msg_getNumElements(m));
63+
9564
// assert that the message is not already larger than the length of the buffer
96-
hv_assert(msg_getNumBytes(m) <= len);
65+
hv_assert(len_r <= len);
9766

9867
// copy the basic message to the buffer
99-
hv_memcpy(r, m, msg_getNumBytes(m));
68+
hv_memcpy(r, m, len_r);
10069

101-
hv_size_t len_r = msg_getNumBytes(m);
102-
103-
char *p = buffer + msg_getByteSize(msg_getNumElements(m)); // points to the end of the base message
70+
char *p = buffer + len_r; // points to the end of the base message
10471
for (int i = 0; i < msg_getNumElements(m); ++i) {
10572
if (msg_isSymbol(m,i)) {
10673
const hv_size_t symLen = (hv_size_t) hv_strlen(msg_getSymbol(m,i)) + 1; // include the trailing null char
@@ -117,7 +84,7 @@ void msg_copyToBuffer(const HvMessage *m, char *buffer, hv_size_t len) {
11784

11885
// the message is serialised such that all symbol elements are placed in order at the end of the buffer
11986
HvMessage *msg_copy(const HvMessage *m) {
120-
const hv_size_t heapSize = msg_getNumHeapBytes(m);
87+
const hv_uint32_t heapSize = msg_getSize(m);
12188
char *r = (char *) hv_malloc(heapSize);
12289
hv_assert(r != NULL);
12390
msg_copyToBuffer(m, r, heapSize);
@@ -184,6 +151,7 @@ hv_uint32_t msg_symbolToHash(const char *s) {
184151
static const hv_int32_t r = 24;
185152

186153
if (s == NULL) return 0;
154+
187155
hv_uint32_t len = (hv_uint32_t) hv_strlen(s);
188156
hv_uint32_t x = len; // seed (0) ^ len
189157

LibSource/StompBox.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,8 @@ bool Patch::isButtonPressed(PatchButtonId bid){
6464
}
6565

6666
int Patch::getSamplesSinceButtonPressed(PatchButtonId bid){
67-
int index = bid+PARAMETER_F;
68-
return index <= getProgramVector()->parameters_size ?
69-
getProgramVector()->parameters[index] : 0;
67+
// deprecated
68+
return 0;
7069
}
7170

7271
AudioBuffer* Patch::createMemoryBuffer(int channels, int samples){

LibSource/StompBox.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class Patch {
9090
float getParameterValue(PatchParameterId pid);
9191
void setParameterValue(PatchParameterId pid, float value);
9292
bool isButtonPressed(PatchButtonId bid);
93+
/** @deprecated */
9394
int getSamplesSinceButtonPressed(PatchButtonId bid);
9495
void setButton(PatchButtonId bid, uint16_t value, uint16_t samples=0);
9596
int getBlockSize();

Source/HeavyPatch.hpp

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
#define HV_OWL_PARAM_C "Channel-C"
1111
#define HV_OWL_PARAM_D "Channel-D"
1212
#define HV_OWL_PARAM_E "Channel-E"
13+
#define HV_OWL_PARAM_F "Channel-F"
14+
#define HV_OWL_PARAM_G "Channel-G"
15+
#define HV_OWL_PARAM_H "Channel-H"
1316
#define HV_OWL_PARAM_PUSH "Channel-Push"
17+
#define HEAVY_MESSAGE_POOL_SIZE 4 // in kB (default 10kB)
18+
#define HEAVY_MESSAGE_QUEUE_SIZE 1 // in kB (default 2kB)
1419

1520
extern "C" {
1621
static bool isButtonPressed(PatchButtonId bid){
@@ -48,9 +53,8 @@ extern "C" {
4853

4954
class HeavyPatch : public Patch {
5055
private:
51-
bool pushbutton;
52-
unsigned int receiverHash[6];
53-
56+
unsigned int receiverHash[9];
57+
HvMessage* notein;
5458
public:
5559
HeavyPatch() {
5660
registerParameter(PARAMETER_E, HV_OWL_PARAM_A);
@@ -63,31 +67,67 @@ class HeavyPatch : public Patch {
6367
receiverHash[2] = hv_stringToHash(HV_OWL_PARAM_C);
6468
receiverHash[3] = hv_stringToHash(HV_OWL_PARAM_D);
6569
receiverHash[4] = hv_stringToHash(HV_OWL_PARAM_E);
66-
receiverHash[5] = hv_stringToHash(HV_OWL_PARAM_PUSH);
67-
context = hv_owl_new(getSampleRate());
70+
receiverHash[5] = hv_stringToHash(HV_OWL_PARAM_F);
71+
receiverHash[6] = hv_stringToHash(HV_OWL_PARAM_G);
72+
receiverHash[7] = hv_stringToHash(HV_OWL_PARAM_H);
73+
receiverHash[8] = hv_stringToHash(HV_OWL_PARAM_PUSH);
74+
context = hv_owl_new_with_options(getSampleRate(),
75+
HEAVY_MESSAGE_POOL_SIZE,
76+
HEAVY_MESSAGE_QUEUE_SIZE);
6877
hv_setPrintHook(context, &printHook);
6978
hv_setSendHook(context, sendHook);
79+
80+
// create note in message
81+
notein = (HvMessage*)malloc(hv_msg_getByteSize(5));
82+
hv_msg_init(notein, 5, 0.0);
83+
hv_msg_setFloat(notein, 0, 60.0); // note
84+
hv_msg_setFloat(notein, 1, 80.0); // velocity
85+
hv_msg_setFloat(notein, 2, 1.0f); // channel
86+
hv_msg_setFloat(notein, 3, 0x90); // command
87+
hv_msg_setFloat(notein, 4, 0.0f); // port
7088
}
7189

7290
~HeavyPatch() {
7391
hv_owl_free(context);
92+
free(notein);
7493
}
7594

95+
void buttonChanged(PatchButtonId bid, uint16_t value, uint16_t samples){
96+
if(bid == PUSHBUTTON){
97+
hv_sendFloatToReceiver(context, receiverHash[8], value ? 1.0 : 0.0);
98+
}else if(bid >= MIDI_NOTE_BUTTON){
99+
// send message to notein object
100+
unsigned int hash = 0x67E37CA3; // __hv_notein
101+
float note = (float)(bid - MIDI_NOTE_BUTTON);
102+
float velocity = (float)(value>>5);
103+
// unsigned int hash = 0x41BE0F9C; // __hv_ctlin
104+
float ms = 1000.0f*(samples+getBlockSize())/getSampleRate(); // delay in milliseconds
105+
// float cmd = value ? 0x90 : 0x80;
106+
hv_msg_setFloat(notein, 0, note);
107+
hv_msg_setFloat(notein, 1, velocity);
108+
// notein expects: note, velocity, channel, command, port
109+
// not thread safe
110+
hv_scheduleMessageForReceiver(context, hash, ms, notein);
111+
}
112+
}
113+
76114
void processAudio(AudioBuffer &buffer) {
77115
float paramA = getParameterValue(PARAMETER_A);
78116
float paramB = getParameterValue(PARAMETER_B);
79117
float paramC = getParameterValue(PARAMETER_C);
80118
float paramD = getParameterValue(PARAMETER_D);
81119
float paramE = getParameterValue(PARAMETER_E);
82-
if(isButtonPressed(PUSHBUTTON) != pushbutton){
83-
pushbutton = isButtonPressed(PUSHBUTTON);
84-
hv_sendFloatToReceiver(context, receiverHash[5], pushbutton ? 1.0 : 0.0);
85-
}
120+
float paramF = getParameterValue(PARAMETER_F);
121+
float paramG = getParameterValue(PARAMETER_G);
122+
float paramH = getParameterValue(PARAMETER_H);
86123
hv_sendFloatToReceiver(context, receiverHash[0], paramA);
87124
hv_sendFloatToReceiver(context, receiverHash[1], paramB);
88125
hv_sendFloatToReceiver(context, receiverHash[2], paramC);
89126
hv_sendFloatToReceiver(context, receiverHash[3], paramD);
90127
hv_sendFloatToReceiver(context, receiverHash[4], paramE);
128+
hv_sendFloatToReceiver(context, receiverHash[5], paramF);
129+
hv_sendFloatToReceiver(context, receiverHash[6], paramG);
130+
hv_sendFloatToReceiver(context, receiverHash[7], paramH);
91131
float* outputs[] = {buffer.getSamples(LEFT_CHANNEL), buffer.getSamples(RIGHT_CHANNEL) };
92132
hv_owl_process(context, outputs, outputs, getBlockSize());
93133
}

Source/PatchProcessor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ AudioBuffer* PatchProcessor::createMemoryBuffer(int channels, int size){
4646
return buf;
4747
}
4848

49-
void PatchProcessor::setParameterValues(uint16_t *params){
49+
void PatchProcessor::setParameterValues(int16_t *params){
5050
if(getProgramVector()->hardware_version == OWL_MODULAR_HARDWARE){
5151
for(int i=0; i<4 && i<parameterCount; ++i)
5252
parameters[i]->update(4095 - params[i]);
@@ -68,7 +68,7 @@ class LinearParameterUpdater : public ParameterUpdater {
6868
public:
6969
LinearParameterUpdater(T min, T max, V initialValue)
7070
: parameter(NULL), minimum(min), maximum(max), value(initialValue) {}
71-
void update(uint16_t newValue){
71+
void update(int16_t newValue){
7272
value = (newValue*(maximum-minimum))/4096+minimum;
7373
if(parameter != NULL)
7474
parameter->update((T)value);
@@ -97,7 +97,7 @@ class ExponentialParameterUpdater : public ParameterUpdater {
9797
// ASSERT(skew > 0.0 && skew <= 2.0, "Invalid exponential skew value");
9898
ASSERT(skew > 0.0, "Invalid exponential skew value");
9999
}
100-
void update(uint16_t newValue){
100+
void update(int16_t newValue){
101101
float v = newValue/4096.0f;
102102
v = expf(logf(v)/skew);
103103
value = v*(maximum-minimum)+minimum;

Source/PatchProcessor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class ParameterUpdater {
99
public:
1010
virtual ~ParameterUpdater(){}
11-
virtual void update(uint16_t value) = 0;
11+
virtual void update(int16_t value) = 0;
1212
virtual void setParameter(IntParameter* p){}
1313
virtual void setParameter(FloatParameter* p){}
1414
};
@@ -22,7 +22,7 @@ class PatchProcessor {
2222
int getBlockSize();
2323
double getSampleRate();
2424
AudioBuffer* createMemoryBuffer(int channels, int samples);
25-
void setParameterValues(uint16_t* parameters);
25+
void setParameterValues(int16_t* parameters);
2626
Patch* patch;
2727
uint8_t index;
2828
void setPatchParameter(int pid, FloatParameter* param);

Source/PatchProgram.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ PatchProcessor* getInitialisingPatchProcessor(){
1414
return &processor;
1515
}
1616

17-
void doSetPatchParameter(uint8_t id, uint16_t value){
18-
if(getProgramVector()->checksum == sizeof(ProgramVector) &&
17+
void doSetPatchParameter(uint8_t id, int16_t value){
18+
if(getProgramVector()->checksum >= PROGRAM_VECTOR_CHECKSUM_V12 &&
1919
getProgramVector()->setPatchParameter != NULL)
2020
getProgramVector()->setPatchParameter(id, value);
2121
}
2222

2323
void doSetButton(uint8_t id, uint16_t value, uint16_t samples){
24-
if(getProgramVector()->checksum == sizeof(ProgramVector) &&
24+
if(getProgramVector()->checksum >= PROGRAM_VECTOR_CHECKSUM_V12 &&
2525
getProgramVector()->setButton != NULL)
2626
getProgramVector()->setButton((PatchButtonId)id, value, samples);
2727
}
@@ -32,7 +32,6 @@ void onButtonChanged(uint8_t id, uint16_t value, uint16_t samples){
3232
}
3333

3434
void onEncoderChanged(uint8_t id, int16_t delta, uint16_t samples){
35-
debugMessage("encoder changed", id, delta, samples);
3635
if(processor.patch != NULL)
3736
processor.patch->encoderChanged((PatchParameterId)id, delta, samples);
3837
}

Source/ProgramVector.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ extern "C" {
1111
#define OWL_MODULAR_HARDWARE 0x12
1212
#define OWL_RACK_HARDWARE 0x13
1313

14-
#define PROGRAM_VECTOR_CHECKSUM_V11 0xff
15-
#define PROGRAM_VECTOR_CHECKSUM_V12 0x80
14+
#define PROGRAM_VECTOR_CHECKSUM_V11 0x40
15+
#define PROGRAM_VECTOR_CHECKSUM_V12 0x50
1616

1717
typedef enum {
1818
AUDIO_IDLE_STATUS = 0,
@@ -30,7 +30,7 @@ extern "C" {
3030
uint8_t audio_bitdepth;
3131
uint16_t audio_blocksize;
3232
uint32_t audio_samplingrate;
33-
uint16_t* parameters;
33+
int16_t* parameters;
3434
uint8_t parameters_size;
3535
uint16_t buttons;
3636
int8_t error;
@@ -44,7 +44,7 @@ extern "C" {
4444
uint32_t heap_bytes_used;
4545
char* message;
4646
void (*setButton)(uint8_t id, uint16_t state, uint16_t samples);
47-
void (*setPatchParameter)(uint8_t id, uint16_t value);
47+
void (*setPatchParameter)(uint8_t id, int16_t value);
4848
void (*buttonChangedCallback)(uint8_t bid, uint16_t state, uint16_t samples);
4949
void (*encoderChangedCallback)(uint8_t bid, int16_t delta, uint16_t samples);
5050
} ProgramVector;

Source/device.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include <inttypes.h>
22

33
#define DEBUG_MEM
4-
/* #define DEBUG_DWT */
54
/* #define STARTUP_CODE */
65

76
#define AUDIO_BIGEND

Source/main.cpp

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,46 +44,26 @@ int main(void){
4444
};
4545
vPortDefineHeapRegions( xHeapRegions );
4646

47-
#ifdef DEBUG_DWT
48-
volatile unsigned int *DWT_CYCCNT = (volatile unsigned int *)0xE0001004; //address of the register
49-
volatile unsigned int *DWT_CONTROL = (volatile unsigned int *)0xE0001000; //address of the register
50-
volatile unsigned int *SCB_DEMCR = (volatile unsigned int *)0xE000EDFC; //address of the register
51-
*SCB_DEMCR = *SCB_DEMCR | 0x01000000;
52-
*DWT_CONTROL = *DWT_CONTROL | 1 ; // enable the counter
53-
#endif /* DEBUG_DWT */
5447
ProgramVector* pv = getProgramVector();
55-
if(pv->checksum == sizeof(ProgramVector)){
56-
debugMessage("checksum v12", pv->checksum);
48+
if(pv->checksum >= PROGRAM_VECTOR_CHECKSUM_V12){
5749
// set event callbacks
58-
// pv->setButton = doSetButton;
59-
// pv->setPatchParameter = doSetPatchParameter;
6050
pv->buttonChangedCallback = onButtonChanged;
6151
pv->encoderChangedCallback = onEncoderChanged;
62-
63-
}else if(pv->checksum != PROGRAM_VECTOR_CHECKSUM_V11){
64-
pv->error = CHECKSUM_ERROR_STATUS;
65-
pv->message = (char*)"ProgramVector checksum error";
66-
pv->programStatus(AUDIO_ERROR_STATUS);
52+
}else if(pv->checksum >= PROGRAM_VECTOR_CHECKSUM_V11){
53+
// no event callbacks
54+
}else{
55+
error(CHECKSUM_ERROR_STATUS, "ProgramVector checksum error");
6756
return -1;
6857
}
69-
if(pv->audio_blocksize <= 0 ||
70-
pv->audio_blocksize > AUDIO_MAX_BLOCK_SIZE){
71-
pv->error = CONFIGURATION_ERROR_STATUS;
72-
pv->message = (char*)"Invalid blocksize";
73-
pv->programStatus(AUDIO_ERROR_STATUS);
58+
if(pv->audio_blocksize <= 0 || pv->audio_blocksize > AUDIO_MAX_BLOCK_SIZE){
59+
error(CONFIGURATION_ERROR_STATUS, "Invalid blocksize");
7460
return -1;
7561
}
7662

7763
setup(pv);
7864

7965
for(;;){
8066
pv->programReady();
81-
#ifdef DEBUG_DWT
82-
*DWT_CYCCNT = 0; // reset the counter
83-
#endif /* DEBUG_DWT */
8467
processBlock(pv);
85-
#ifdef DEBUG_DWT
86-
pv->cycles_per_block = *DWT_CYCCNT;
87-
#endif /* DEBUG_DWT */
8868
}
8969
}

0 commit comments

Comments
 (0)