Include File in Object


#1

Another stupid noob question, I'm afraid.

I have a load of data tables stored as constant arrays, and I'd like dump them into an external file and include them in several custom objects I'm working on.

I've tried adding a reference to the file in the [relative path] section of the objects XML, but this doesn't seem to work, sadly.

I can tell the file is being brought in when the object is compiled, as if I also define any of the same constants in Local Data section of the XML of the object itself, I get an error about the redefinition.

However, I can't seem to access the data in the included file from my code in the K-Rate Code section of my object code. I get no error in the console, but every request for data from the included file returns zero.

I suspect this is a scope issue, but there must be a way to get it to work.

Incidentally, I don't seem to be able to enter any text in the Includes (or Dependencies) field in the Overview tab of the Object Editor. Is this a known issue?

a|x


#2

Doing a bit of reading around this, it looks like adding 'extern' to the variable declaration in the external file might help, but there are warnings about doing this because it makes the variable global.

Global variables seem to be used all the time in Factory objects, but I'm not sure they are actually global. Maybe their scope is limited in some way.

There's also advice about using a .h header file, but I don't know if this is possible in the Axoloti object environment.

I'd love to know what is the right way to do this.

a|x


#3

Take a look at @thetechnobear's Push object? It has a bunch of includes, I was nosing around at it yesterday to find out some info on sysex.


#4

Good call. I will have a look.

Cheers,

a|x


#5

So, this seems to work. But, is it the right way to do it?

This is my header file, tables.h:

#ifndef CANTO_TABLES_H
#define CANTO_TABLES_H

// Sinewave table
extern const int8_t sinCalc[];

// Squarewave table
extern const int8_t sqrCalc[];

// Formant table
extern const int8_t formantTable[][7];

// Vowel indices
extern const int8_t vowels[];

// Sample-rate reduction divisor table
extern const int8_t srateTable[];

#include "tables.c"

#endif

and an excerpt from tables.c:

const int8_t sinCalc[256] = {
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,2,2,3,3,4,5,6,7,8,10,12,14,17,20,24,
    0,4,4,5,6,7,9,11,13,15,18,22,26,31,37,45,
    0,5,6,7,8,10,12,14,17,20,24,28,34,41,49,58,
    0,5,6,7,9,10,12,15,18,21,26,31,37,44,53,63,
    0,5,6,7,8,10,12,14,17,20,24,28,34,41,49,58,
    0,4,4,5,6,7,9,11,13,15,18,22,26,31,37,45,
    0,2,2,3,3,4,5,6,7,8,10,12,14,17,20,24,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,-2,-2,-3,-3,-4,-5,-6,-7,-8,-10,-12,-14,-17,-20,-24,
    0,-4,-4,-5,-6,-7,-9,-11,-13,-15,-18,-22,-26,-31,-37,-45,
    0,-5,-6,-7,-8,-10,-12,-14,-17,-20,-24,-28,-34,-41,-49,-58,
    0,-5,-6,-7,-9,-10,-12,-15,-18,-21,-26,-31,-37,-44,-53,-63,
    0,-5,-6,-7,-8,-10,-12,-14,-17,-20,-24,-28,-34,-41,-49,-58,
    0,-4,-4,-5,-6,-7,-9,-11,-13,-15,-18,-22,-26,-31,-37,-45,
    0,-2,-2,-3,-3,-4,-5,-6,-7,-8,-10,-12,-14,-17,-20,-24
};

However, having read a little about the evils of global variables in the past (and I understand declaring a variable as 'extern' makes it global), I have a feeling this is the wrong way to do thing.

I have two goals in putting the the tables in an external file:

  1. So that I can use them in multiple objects
  2. So that if I do the above, the same variables will not be declared multiple times, if I use more than one instance of the objects that use the external file.

I don't know if this method fulfils either of these criteria.

Incidentally, if I can do the same thing with a single, rather than 2 files, that might be cool.

Any advice gratefully accepted.

a|x


#6

I've discovered that I can just dump the whole of my .c file into the header file, and declare and assign values to my constant arrays there, without the need for the .c file at all.

I don't think this is how header files are supposed to be used, though. My understanding is that you're supposed to declare variables in the header, but not assign them, and you're actually supposed to assign values to them in a .c file that's included by the header.

Here's a cut-down version of what's in my header file, included by my object:

#ifndef CANTO_TABLES_H
	
	#define CANTO_TABLES_H

	// All audible phonemes (73)
	const int8_t phonemes[] = {
		5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,
		21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,
		37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,
		53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,
		69,70,71,72,73,74,75,76,77,	
	};

#endif

It seems to work fine like this.

a|x


#7

A gentle bump.
Would be great if someone could tell me if I'm doing this right.
I'm pretty sure I'm not, but it does seem to work, at least...

a|x


#8

Hey @toneburst

Was wondering if you would show me how you include the headerfile in the Axo object?

I tried this:
- I put the headerfile, called "functions.h", that I created in Xcode, into the same folder as the axo object, in this case called "sum.axo". I included the headerfile using include, as shown on the picture:

But when I try to go live I get this error:

Can you tell me what I am doing wrong here?

The header file has to be in the same folder as the object, right?


#9

you have to edit the objects file directly with another editor. you will see an "include" section at the beginning of the file (if not, create one) and put your filename there.


#10

Ahh found it, will try that today.

Thanks :wink:


#11

I had trouble with this at first, then I noticed that you also need to populate the helpPatch field, otherwise the editor clears the include tag from the xml when loading:

   <helpPatch>myheader.axh</helpPatch>
   ...
   <includes>
      <include>./myheader.h</include>
   </includes>

(same name as your header file but with .axh extension)
The header file is located in the same directory as the .axo file.