Function pointers: go->C; C structs to go


(Ethin Probst) #1

So, I’m trying to call functions in a speech synthesis library in Golang that’s written in C using its DLL. I can’t build the DLL to use with cgo (or maybe I can, if someone knows how, please let me know using something like dlltool). Anyway, the DLL has functions like:

ESPEAK_API void espeak_SetSynthCallback(t_espeak_callback* SynthCallback);

The t_espeak_callback type is defined as:

typedef int (t_espeak_callback)(short*, int, espeak_EVENT*);

And the espeak_EVENT structure is defined as:

typedef struct {
	espeak_EVENT_TYPE type;
	unsigned int unique_identifier; // message identifier (or 0 for key or character)
	int text_position;    // the number of characters from the start of the text
	int length;           // word length, in characters (for espeakEVENT_WORD)
	int audio_position;   // the time in mS within the generated speech output data
	int sample;           // sample id (internal use)
	void* user_data;      // pointer supplied by the calling program
	union {
		int number;        // used for WORD and SENTENCE events.
		const char *name;  // used for MARK and PLAY events.  UTF8 string
		char string[8];    // used for phoneme names (UTF8). Terminated by a zero byte unless the name needs the full 8 bytes.
	} id;
} espeak_EVENT;

So, how would I write a callback in go for this, then pass it to the C library? I don’t know how I would make a union in Go (or something like it). Even if I were to use cgo, how would that help me any?
Edit: this seems to apply to all speech synthesis libraries I look at – they all have a callback of some kind, and that’s what I’m primarily confused about. The structure thing seems to be ESpeak-related – though I’m guessing that won’t be the end of that.