Author: remi
Date: 2008-12-17 23:42:16 +0100 (Wed, 17 Dec 2008)
New Revision: 3153
Modified:
software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPITTS.java
Log:
* added a way to asynchronously calling (through a threads) the tts.speak and
tux.tts.speakAsync in a thread-safe context. Theses functions need to be
mutexed in order to respecting the TTS asynchronous protocol. The problem is
how to start many asynchronous tts.speak requests without having a stack effect
(because of mutex) ? The solution is to skipping the intermediate requests and
to leaving the last one.
Modified:
software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPITTS.java
===================================================================
---
software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPITTS.java
2008-12-17 22:26:27 UTC (rev 3152)
+++
software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPITTS.java
2008-12-17 22:42:16 UTC (rev 3153)
@@ -24,6 +24,9 @@
private String encoding = "latin-1";
private String locutor = "Ryan";
private Integer pitch = 100;
+ private String currText, currLocutor;
+ private Integer currPitch;
+ private SLockedStack syncStackLock, asyncStackLock;
/**
* Constructor of the class.
@@ -33,6 +36,8 @@
{
pParent = parent;
eventHandlers = pParent.getEventHandlers();
+ syncStackLock = new SLockedStack();
+ asyncStackLock = new SLockedStack();
}
/**
@@ -120,64 +125,98 @@
{
Boolean ret;
String cmd;
+ Object soundState[];
- // Set the locutor
- if (locutor != "")
+ asyncStackLock.incStackCounter();
+
+ asyncStackLock.mutexPre.acquire();
+ stop();
+ soundState =
pParent.status.requestOne(TuxAPIConst.ST_NAME_TTS_SOUND_STATE);
+ if (soundState[0].equals("ON"))
{
- setLocutor(locutor);
+
eventHandlers.waitCondition(TuxAPIConst.ST_NAME_TTS_SOUND_STATE, 99999999.0,
+ "OFF", null);
}
- cmd = String.format("tts/locutor?name=%s", this.locutor);
- ret = cmdSimpleResult(cmd);
- if (!ret)
+ asyncStackLock.mutexPre.release();
+
+ asyncStackLock.mutexIn.acquire();
+ if (asyncStackLock.getStackCounter() <= 1)
{
- return false;
+ // Set the locutor
+ if (locutor != "")
+ {
+ setLocutor(locutor);
+ }
+ cmd = String.format("tts/locutor?name=%s",
this.locutor);
+ ret = cmdSimpleResult(cmd);
+ if (!ret)
+ {
+ asyncStackLock.decStackCounter();
+ asyncStackLock.mutexIn.release();
+ return false;
+ }
+ // Set the pitch
+ if (pitch != 0)
+ {
+ setPitch(pitch);
+ }
+ cmd = String.format("tts/pitch?value=%d", this.pitch);
+ ret = cmdSimpleResult(cmd);
+ if (!ret)
+ {
+ asyncStackLock.decStackCounter();
+ asyncStackLock.mutexIn.release();
+ return false;
+ }
+ // Remove ending lines
+ text = text.replace("\n", ".");
+ // Try to encode the string
+ try
+ {
+ text = URLEncoder.encode(text, "UTF-8");
+ } catch (Exception e) {}
+ // Perform the speech
+ cmd = String.format("tts/speak?text=%s", text);
+ ret = cmdSimpleResult(cmd);
+ if (!ret)
+ {
+ asyncStackLock.decStackCounter();
+ asyncStackLock.mutexIn.release();
+ return false;
+ }
+ if (pParent.server.getClientLevel() ==
TuxAPIConst.CLIENT_LEVEL_ANONYME)
+ {
+ asyncStackLock.decStackCounter();
+ asyncStackLock.mutexIn.release();
+ return true;
+ }
+ // Wait the speak status
+ ret =
eventHandlers.waitCondition(TuxAPIConst.ST_NAME_SPEAK_STATUS, 5.0,
+ null, null);
+ if (!ret)
+ {
+ asyncStackLock.decStackCounter();
+ asyncStackLock.mutexIn.release();
+ return false;
+ }
+ // Get the speak status
+ Object result[] =
pParent.status.requestOne(TuxAPIConst.ST_NAME_SPEAK_STATUS);
+ if (result[0].equals("NoError"))
+ {
+ asyncStackLock.decStackCounter();
+ asyncStackLock.mutexIn.release();
+ return true;
+ }
+ else
+ {
+ asyncStackLock.decStackCounter();
+ asyncStackLock.mutexIn.release();
+ return false;
+ }
}
- // Set the pitch
- if (pitch != 0)
- {
- setPitch(pitch);
- }
- cmd = String.format("tts/pitch?value=%d", this.pitch);
- ret = cmdSimpleResult(cmd);
- if (!ret)
- {
- return false;
- }
- // Remove ending lines
- text = text.replace("\n", ".");
- // Try to encode the string
- try
- {
- text = URLEncoder.encode(text, "UTF-8");
- } catch (Exception e) {}
- // Perform the speech
- cmd = String.format("tts/speak?text=%s", text);
- ret = cmdSimpleResult(cmd);
- if (!ret)
- {
- return false;
- }
- if (pParent.server.getClientLevel() ==
TuxAPIConst.CLIENT_LEVEL_ANONYME)
- {
- return true;
- }
- // Wait the speak status
- ret =
eventHandlers.waitCondition(TuxAPIConst.ST_NAME_SPEAK_STATUS, 5.0,
- null, null);
- if (!ret)
- {
- return false;
- }
- // Get the speak status
- Object result[] =
pParent.status.requestOne(TuxAPIConst.ST_NAME_SPEAK_STATUS);
- if (result[0].equals("NoError"))
- {
- return true;
- }
- else
- {
- return false;
- }
+ asyncStackLock.decStackCounter();
+ asyncStackLock.mutexIn.release();
+ return true;
}
/**
@@ -202,25 +241,53 @@
*/
public Boolean speak(String text, String locutor, Integer pitch)
{
- if (speakAsync(text, locutor, pitch))
+ Object soundState[];
+ Boolean result = true;
+
+ syncStackLock.incStackCounter();
+ syncStackLock.mutexPre.acquire();
+ currText = text;
+ currLocutor = locutor;
+ currPitch = pitch;
+ stop();
+ soundState =
pParent.status.requestOne(TuxAPIConst.ST_NAME_TTS_SOUND_STATE);
+ if (soundState[0].equals("ON"))
{
- if (pParent.server.getClientLevel() ==
TuxAPIConst.CLIENT_LEVEL_ANONYME)
+
eventHandlers.waitCondition(TuxAPIConst.ST_NAME_TTS_SOUND_STATE, 99999999.0,
+ "OFF", null);
+ }
+ syncStackLock.mutexPre.release();
+
+ syncStackLock.mutexIn.acquire();
+ if (syncStackLock.getStackCounter() <= 1)
+ {
+ if (speakAsync(currText, currLocutor, currPitch))
{
- return true;
+ if (pParent.server.getClientLevel() ==
TuxAPIConst.CLIENT_LEVEL_ANONYME)
+ {
+ result = true;
+ }
+ else
+ {
+
eventHandlers.waitCondition(TuxAPIConst.ST_NAME_TTS_SOUND_STATE, 1.0, "ON",
null);
+ soundState =
pParent.status.requestOne(TuxAPIConst.ST_NAME_TTS_SOUND_STATE);
+ if (soundState[0].equals("ON"))
+ {
+
eventHandlers.waitCondition(TuxAPIConst.ST_NAME_TTS_SOUND_STATE, 99999999.0,
+ "OFF", null);
+ }
+ }
+ result = true;
}
-
eventHandlers.waitCondition(TuxAPIConst.ST_NAME_TTS_SOUND_STATE, 1.0, "ON",
null);
- Object result[] =
pParent.status.requestOne(TuxAPIConst.ST_NAME_TTS_SOUND_STATE);
- if (result[0].equals("ON"))
+ else
{
-
eventHandlers.waitCondition(TuxAPIConst.ST_NAME_TTS_SOUND_STATE, 99999999.0,
- "OFF", null);
+ result = false;
}
- return true;
}
- else
- {
- return false;
- }
+ syncStackLock.decStackCounter();
+ syncStackLock.mutexIn.release();
+
+ return result;
}
/**
------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you. Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn