Create an objet inside another thread

#1

I want to use windows speech (tts) in a app, currently it works but the call to “speak” is blocking so each time my app talks it gets freeze.

So i moved my talks to another thread, but this do not work as the tts object needs to be created inside the same thread that gets called.

If i understand correctly the ttspeak object needs to be created inside the threadedFunction function to be on the same thread? How can i create the ttspeak object inside the same thread that calls the comSpeak method?

Thanks!

#pragma once
#include "ofMain.h"
#include "ttspeak.h"

class parlante : public ofThread
{
public:

	parlante() {
		startThread();
		created = false;
	}
	//--------------------------------------------------------------
	~parlante() {
		waitForThread(true);
	}
	//--------------------------------------------------------------
	void habla(string & text) {
		paraHablar.send(text);
	}
	//--------------------------------------------------------------
	void init() {
		speechOut.loadComSpeak(100, FEMALE);
	}

private:
	void threadedFunction() {
		string text;
		while (paraHablar.receive(text)) {			
			speechOut.comSpeak(text);
		}
	}
	bool created;
	ttspeak speechOut;
	ofThreadChannel<string> paraHablar;
};
#2
void threadedFunction() {
		string text;
		ttspeak speechOut;
		while (paraHablar.receive(text)) {			
			speechOut.comSpeak(text);
		}
	}
#3

Thank you arturo

I do not understand because… this way each time the threadFunction is called an ttspeak object is created, it will not make memory grow up?

#4

but the threaded function should only be called once in the constructor when you call startThread

#5

Ahhhh i see, it will only be called once and will be stucked on the while waiting for receive data from the threadedchannel.

Thanks a lot, now i understand