[TeamTalk 316]: [852] branches/mrmarge/PocketTeamTalk/Agents/MultiDecoder: Add (not branch, because this is a different repository...) custom audioserver and sphinxengine.

matt@edam.speech.cs.cmu.edu matt at edam.speech.cs.cmu.edu
Thu Oct 18 17:29:23 EDT 2007


An HTML attachment was scrubbed...
URL: http://mailman.srv.cs.cmu.edu/pipermail/teamtalk-developers/attachments/20071018/0cb3665d/attachment-0001.html
-------------- next part --------------
Added: branches/mrmarge/PocketTeamTalk/Agents/MultiDecoder/Audio_Server/Audio_Server.sln
===================================================================
--- branches/mrmarge/PocketTeamTalk/Agents/MultiDecoder/Audio_Server/Audio_Server.sln	                        (rev 0)
+++ branches/mrmarge/PocketTeamTalk/Agents/MultiDecoder/Audio_Server/Audio_Server.sln	2007-10-18 21:29:22 UTC (rev 852)
@@ -0,0 +1,19 @@
+Microsoft Visual Studio Solution File, Format Version 9.00
+# Visual Studio 2005
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Audio_Server", "Audio_Server.vcproj", "{C31484B0-179B-432D-AE1E-75FB90591F23}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Win32 = Debug|Win32
+		Release|Win32 = Release|Win32
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{C31484B0-179B-432D-AE1E-75FB90591F23}.Debug|Win32.ActiveCfg = Debug|Win32
+		{C31484B0-179B-432D-AE1E-75FB90591F23}.Debug|Win32.Build.0 = Debug|Win32
+		{C31484B0-179B-432D-AE1E-75FB90591F23}.Release|Win32.ActiveCfg = Release|Win32
+		{C31484B0-179B-432D-AE1E-75FB90591F23}.Release|Win32.Build.0 = Release|Win32
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+EndGlobal

Added: branches/mrmarge/PocketTeamTalk/Agents/MultiDecoder/Audio_Server/Audio_Server.vcproj
===================================================================
--- branches/mrmarge/PocketTeamTalk/Agents/MultiDecoder/Audio_Server/Audio_Server.vcproj	                        (rev 0)
+++ branches/mrmarge/PocketTeamTalk/Agents/MultiDecoder/Audio_Server/Audio_Server.vcproj	2007-10-18 21:29:22 UTC (rev 852)
@@ -0,0 +1,235 @@
+<?xml version="1.0" encoding="Windows-1252"?>
+<VisualStudioProject
+	ProjectType="Visual C++"
+	Version="8.00"
+	Name="Audio_Server"
+	ProjectGUID="{C31484B0-179B-432D-AE1E-75FB90591F23}"
+	RootNamespace="Audio_Server"
+	Keyword="Win32Proj"
+	>
+	<Platforms>
+		<Platform
+			Name="Win32"
+		/>
+	</Platforms>
+	<ToolFiles>
+	</ToolFiles>
+	<Configurations>
+		<Configuration
+			Name="Debug|Win32"
+			OutputDirectory="Debug"
+			IntermediateDirectory="Debug"
+			ConfigurationType="1"
+			InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops"
+			CharacterSet="2"
+			>
+			<Tool
+				Name="VCPreBuildEventTool"
+				Description="Setting up directories for executable..."
+				CommandLine="IF NOT EXIST  ..\..\..\bin\x86-nt\nul mkdir ..\..\..\bin\x86-nt"
+			/>
+			<Tool
+				Name="VCCustomBuildTool"
+			/>
+			<Tool
+				Name="VCXMLDataGeneratorTool"
+			/>
+			<Tool
+				Name="VCWebServiceProxyGeneratorTool"
+			/>
+			<Tool
+				Name="VCMIDLTool"
+			/>
+			<Tool
+				Name="VCCLCompilerTool"
+				Optimization="0"
+				AdditionalIncludeDirectories="..\..\..\Libraries\Galaxy\include;..\..\..\Libraries\Galaxy\contrib\MITRE\utilities\include;..\..\..\Libraries\sphinxbase\include"
+				PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;_MBCS;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS"
+				MinimalRebuild="true"
+				BasicRuntimeChecks="0"
+				RuntimeLibrary="3"
+				UsePrecompiledHeader="0"
+				WarningLevel="3"
+				Detect64BitPortabilityProblems="true"
+				DebugInformationFormat="4"
+			/>
+			<Tool
+				Name="VCManagedResourceCompilerTool"
+			/>
+			<Tool
+				Name="VCResourceCompilerTool"
+			/>
+			<Tool
+				Name="VCPreLinkEventTool"
+			/>
+			<Tool
+				Name="VCLinkerTool"
+				AdditionalDependencies="../../../Libraries/Galaxy/lib/x86-nt/libGalaxy_debug.lib ..\..\..\Libraries\Galaxy\contrib\MITRE\utilities\lib\x86-nt\libMITRE_galaxy_debug.lib wsock32.lib winmm.lib"
+				OutputFile="$(OutDir)\$(ProjectName)-DEBUG.exe"
+				LinkIncremental="2"
+				GenerateDebugInformation="true"
+				ProgramDatabaseFile="$(OutDir)/Audio_Server.pdb"
+				SubSystem="1"
+				TargetMachine="1"
+			/>
+			<Tool
+				Name="VCALinkTool"
+			/>
+			<Tool
+				Name="VCManifestTool"
+			/>
+			<Tool
+				Name="VCXDCMakeTool"
+			/>
+			<Tool
+				Name="VCBscMakeTool"
+			/>
+			<Tool
+				Name="VCFxCopTool"
+			/>
+			<Tool
+				Name="VCAppVerifierTool"
+			/>
+			<Tool
+				Name="VCWebDeploymentTool"
+			/>
+			<Tool
+				Name="VCPostBuildEventTool"
+				Description="Copying executable to Bin directory..."
+				CommandLine="copy &quot;$(TargetPath)&quot; ..\..\..\bin\x86-nt"
+			/>
+		</Configuration>
+		<Configuration
+			Name="Release|Win32"
+			OutputDirectory="Release"
+			IntermediateDirectory="Release"
+			ConfigurationType="1"
+			InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops"
+			CharacterSet="2"
+			>
+			<Tool
+				Name="VCPreBuildEventTool"
+				Description="Setting up directories for executable..."
+				CommandLine="IF NOT EXIST  ..\..\..\bin\x86-nt\nul mkdir ..\..\..\bin\x86-nt"
+			/>
+			<Tool
+				Name="VCCustomBuildTool"
+			/>
+			<Tool
+				Name="VCXMLDataGeneratorTool"
+			/>
+			<Tool
+				Name="VCWebServiceProxyGeneratorTool"
+			/>
+			<Tool
+				Name="VCMIDLTool"
+			/>
+			<Tool
+				Name="VCCLCompilerTool"
+				Optimization="2"
+				InlineFunctionExpansion="1"
+				OmitFramePointers="true"
+				AdditionalIncludeDirectories="..\..\..\Libraries\Galaxy\include;..\..\..\Libraries\Galaxy\contrib\MITRE\utilities\include;..\..\..\Libraries\sphinxbase\include"
+				PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;_MBCS;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS"
+				StringPooling="true"
+				RuntimeLibrary="2"
+				EnableFunctionLevelLinking="true"
+				UsePrecompiledHeader="0"
+				WarningLevel="3"
+				Detect64BitPortabilityProblems="true"
+				DebugInformationFormat="3"
+			/>
+			<Tool
+				Name="VCManagedResourceCompilerTool"
+			/>
+			<Tool
+				Name="VCResourceCompilerTool"
+			/>
+			<Tool
+				Name="VCPreLinkEventTool"
+			/>
+			<Tool
+				Name="VCLinkerTool"
+				AdditionalDependencies="../../../Libraries/Galaxy/lib/x86-nt/libGalaxy.lib ..\..\..\Libraries\Galaxy\contrib\MITRE\utilities\lib\x86-nt\libMITRE_galaxy_debug.lib wsock32.lib winmm.lib"
+				OutputFile="$(OutDir)\$(ProjectName).exe"
+				LinkIncremental="1"
+				GenerateDebugInformation="true"
+				SubSystem="1"
+				OptimizeReferences="2"
+				EnableCOMDATFolding="2"
+				TargetMachine="1"
+			/>
+			<Tool
+				Name="VCALinkTool"
+			/>
+			<Tool
+				Name="VCManifestTool"
+			/>
+			<Tool
+				Name="VCXDCMakeTool"
+			/>
+			<Tool
+				Name="VCBscMakeTool"
+			/>
+			<Tool
+				Name="VCFxCopTool"
+			/>
+			<Tool
+				Name="VCAppVerifierTool"
+			/>
+			<Tool
+				Name="VCWebDeploymentTool"
+			/>
+			<Tool
+				Name="VCPostBuildEventTool"
+				Description="Copying executable to Bin directory..."
+				CommandLine="copy &quot;$(TargetPath)&quot; ..\..\..\bin\x86-nt"
+			/>
+		</Configuration>
+	</Configurations>
+	<References>
+	</References>
+	<Files>
+		<Filter
+			Name="Source Files"
+			Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm"
+			>
+			<File
+				RelativePath=".\main.cpp"
+				>
+			</File>
+			<File
+				RelativePath="sock.c"
+				>
+			</File>
+			<File
+				RelativePath=".\Utils.cpp"
+				>
+			</File>
+		</Filter>
+		<Filter
+			Name="Header Files"
+			Filter="h;hpp;hxx;hm;inl;inc"
+			>
+			<File
+				RelativePath="sock.h"
+				>
+			</File>
+			<File
+				RelativePath="sphinx-server.h"
+				>
+			</File>
+			<File
+				RelativePath=".\Utils.h"
+				>
+			</File>
+		</Filter>
+		<Filter
+			Name="Resource Files"
+			Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
+			>
+		</Filter>
+	</Files>
+	<Globals>
+	</Globals>
+</VisualStudioProject>

Added: branches/mrmarge/PocketTeamTalk/Agents/MultiDecoder/Audio_Server/Utils.cpp
===================================================================
--- branches/mrmarge/PocketTeamTalk/Agents/MultiDecoder/Audio_Server/Utils.cpp	                        (rev 0)
+++ branches/mrmarge/PocketTeamTalk/Agents/MultiDecoder/Audio_Server/Utils.cpp	2007-10-18 21:29:22 UTC (rev 852)
@@ -0,0 +1,437 @@
+//=============================================================================
+//
+//   Copyright (c) 2000-2005, Carnegie Mellon University.  
+//   All rights reserved.
+//
+//   Redistribution and use in source and binary forms, with or without
+//   modification, are permitted provided that the following conditions
+//   are met:
+//
+//   1. Redistributions of source code must retain the above copyright
+//      notice, this list of conditions and the following disclaimer. 
+//
+//   2. Redistributions in binary form must reproduce the above copyright
+//      notice, this list of conditions and the following disclaimer in
+//      the documentation and/or other materials provided with the
+//      distribution.
+//
+//   This work was supported in part by funding from the Defense Advanced 
+//   Research Projects Agency and the National Science Foundation of the 
+//   United States of America, and the CMU Sphinx Speech Consortium.
+//
+//   THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 
+//   ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
+//   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+//   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
+//   NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+//   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
+//   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
+//   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
+//   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
+//   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
+//   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+//=============================================================================
+ 
+//-----------------------------------------------------------------------------
+// 
+// UTILS.CPP - implementation of various functions useful throughout the 
+//             components of the architecture
+// 
+// ----------------------------------------------------------------------------
+// 
+// BEFORE MAKING CHANGES TO THIS CODE, please read the appropriate 
+// documentation, available in the Documentation folder. 
+//
+// ANY SIGNIFICANT CHANGES made should be reflected back in the documentation
+// file(s)
+//
+// ANY CHANGES made (even small bug fixes, should be reflected in the history
+// below, in reverse chronological order
+// 
+// HISTORY --------------------------------------------------------------------
+//
+//   [2005-06-29] (antoine): started working on this by copying Utils.cpp from
+//							 RavenClaw
+//-----------------------------------------------------------------------------
+
+#include <windows.h>
+#include "Utils.h"
+
+// D: Static buffer commonly used by string routines
+static char szBuffer[STRING_MAX];
+
+//-----------------------------------------------------------------------------
+// Functions for extending the string STL class with some desired but
+// missing functionality
+//-----------------------------------------------------------------------------
+// D: implements the printf functionality in a string
+string FormatString(const char *lpszFormat, ...) {
+	va_list pArgs;
+	va_start(pArgs, lpszFormat);
+
+	// print into the buffer
+	_vsnprintf(szBuffer, STRING_MAX, lpszFormat, pArgs);
+	
+	// return a string object initialized from that buffer
+	return((string)szBuffer);
+}
+
+// D: implements the + operator for strings
+string operator + (const string sString1, const string sString2) {
+	string sResult = sString1;
+	sResult += sString2;
+	return sResult;
+}
+
+// D: conversion from bool to string
+string BoolToString(bool bBool) {
+    return bBool?"true":"false";
+}
+
+// A: conversion from int to string
+string IntToString(int iInt) {
+    return FormatString("%d", iInt);
+}
+
+// D: conversion from float to string
+string FloatToString(float fFloat) {
+    return FormatString("%f", fFloat);
+}
+
+// D: make a string uppercase
+string ToUpperCase(string sString) {
+	strcpy(szBuffer, sString.c_str());
+	_strupr(szBuffer);
+	return (string)szBuffer;
+}
+
+// D: makes a string lowercase
+string ToLowerCase(string sString) {
+	strcpy(szBuffer, sString.c_str());
+	_strlwr(szBuffer);
+	return (string)szBuffer;
+}
+
+// A: trim specified characters (default spaces) from the string on the left
+string TrimLeft(string sString, char * pToTrim) {
+	unsigned int i = 0;
+	while(i < sString.length()) 
+	{
+		char * c = pToTrim;
+		while ( *c != '\0' && *c != sString[i] )
+			c++;
+		if ( *c == '\0' )
+			break;
+		i++;
+	}
+	string sResult = sString.substr(i, sString.length() - i);
+	return sResult;
+}
+
+// A: trim specified characters (default spaces) from the string on the right
+string TrimRight(string sString, char * pToTrim) {
+	size_t i = sString.length()-1;
+	while((i >= 0) )
+	{
+		char * c = pToTrim;
+		while ( *c != '\0' && *c != sString[i] )
+			c++;
+		if ( *c == '\0' )
+			break;
+		i--;
+	}
+	string sResult = sString.substr(0, i+1);
+	return sResult;
+}
+
+// A: trim specified characters (default space) from the string at both ends
+string Trim(string sString, char * pToTrim) {
+	return TrimLeft(TrimRight(sString, pToTrim), pToTrim);
+}
+
+// D: extracts the first line of a string, and returns it (the string is 
+//    chopped)
+string ExtractFirstLine(string& rString) {
+	string sResult;
+	SplitOnFirst(rString, "\n", sResult, rString);
+	return sResult;
+}
+
+// A: splits the string in 2 parts, around and not including the first 
+//    occurence of any of a set of specified characters. Returns true on success
+bool SplitOnFirst(string sOriginal, char* pDividers, 
+				  string& rsFirstPart, string& rsSecondPart) {
+
+    size_t iCharPos = sOriginal.find_first_of(pDividers);
+    if(iCharPos == -1) {
+		// if the character was not found
+		rsFirstPart = sOriginal;
+		rsSecondPart = "";
+		return false;
+	} else {
+		// if the character was found
+		rsFirstPart = sOriginal.substr(0, iCharPos);
+		rsSecondPart = sOriginal.substr(iCharPos + 1, 
+										sOriginal.length() - iCharPos);
+		return true;			
+	}
+}
+
+// D: function similar to SplitOnFirst. It takes as an extra argument a char
+// that act as quote characters, and therefore any occurence of the dividers 
+// within that is not considered
+bool SplitOnFirst(string sOriginal, char* pDividers, 
+				  string& rsFirstPart, string& rsSecondPart, char cQuote) {
+
+    size_t i = 0;
+    bool bWithinQuotes = false;
+    size_t l = sOriginal.length();
+    while(i < l) {
+        // if we/re within quotes, just skip over everything until 
+        // a new quote character is met
+        if(bWithinQuotes) {
+            while((sOriginal[i] != cQuote) && (i < l)) 
+                i++;
+            // check that we didn't reach the end
+            if(i == l) {
+                rsFirstPart = sOriginal;
+                rsSecondPart = "";
+                return false;
+            }
+            // o/w increment i;
+            i++;
+            // and set ourselves out of quotes
+            bWithinQuotes = false;
+        } else if(sOriginal[i] == cQuote) {
+            // o/w if we just saw a quote, put ourselves in quote mode
+            bWithinQuotes = true;
+            // and move on
+            i++;
+        } else if(strchr(pDividers, sOriginal[i]) != NULL) {
+            // o/w if we hit on one of the dividers
+            rsFirstPart = sOriginal.substr(0, i);
+            rsSecondPart = sOriginal.substr(i + 1, sOriginal.length() - i);
+            return true;
+        } else {
+            i++;
+        }
+    }
+
+    // if we got out of the loop, it means we reached the end without returning, 
+    // so then there are no dividers
+    rsFirstPart = sOriginal;
+    rsSecondPart = "";
+    return false;
+}
+
+// A: splits the string in 2 parts, around and not including the last
+//    occurence of any of a set of specified characters. Returns true on success
+bool SplitOnLast(string sOriginal, char* pDividers, 
+				  string& rsFirstPart, string& rsSecondPart) {
+    size_t iCharPos = sOriginal.find_last_of(pDividers);
+	if(iCharPos == -1) {
+		// if the character was not found
+		rsFirstPart = "";
+		rsSecondPart = sOriginal;
+		return false;
+	} else {
+		// if the character was found
+		rsFirstPart = sOriginal.substr(0, iCharPos);
+		rsSecondPart = sOriginal.substr(iCharPos + 1, 
+										sOriginal.length() - iCharPos);
+		return true;			
+	}
+}
+
+// A: partitions a string into tokens divided by any of a set of specified
+//    characters.
+vector<string> PartitionString(string sString, char * pDividers) {
+	vector<string> saResult;
+	string sTemp;
+	while (sString != "" )
+	{
+		SplitOnFirst(sString, pDividers, sTemp, sString);
+		if ( sTemp != "" )
+			saResult.push_back(sTemp);
+	}
+	return saResult;
+} 
+
+// A: partitions a string into tokens divided by any of a set of specified
+//    characters.
+vector<string> PartitionString(string sString, char * pDividers, char cQuote) {
+	vector<string> saResult;
+	string sTemp;
+	while (sString != "" )
+	{
+		SplitOnFirst(sString, pDividers, sTemp, sString, cQuote);
+		if ( sTemp != "" )
+			saResult.push_back(sTemp);
+	}
+	return saResult;
+} 
+
+// D: replaces all the occurences of a substring in a string with another
+//    substring
+string ReplaceSubString(string sSource, string sToReplace, string sReplacement) {
+    // the resulting string
+    string sResult = sSource;
+    size_t pos;
+    while((pos = sResult.find(sToReplace)) >= 0) {
+        sResult.replace(pos, pos + sToReplace.length(), sReplacement);
+    }
+    return sResult;
+}
+
+// D: find the corresponding closing quote
+unsigned int FindClosingQuoteChar(string sString, unsigned int iStartPos, 
+								  char cOpenQuote, char cCloseQuote) {
+
+	unsigned int iOpenBraces = 1;	
+	unsigned int iPos = iStartPos;
+	while((iOpenBraces > 0) && (iPos < sString.length())) {
+		if(sString[iPos] == cOpenQuote) 
+			iOpenBraces++;
+		else if(sString[iPos] == cCloseQuote)
+			iOpenBraces--;
+		iPos++;
+	}
+
+	// finally return the position
+	return iPos;
+}
+
+//-----------------------------------------------------------------------------
+// Functions for constructing unique IDs
+//-----------------------------------------------------------------------------
+
+static int __ID = 0;
+
+// D: creates a uniques string ID
+string GetUniqueStringID() {
+	// make sure we're not starting from 0 again
+	__ID++;
+	assert( __ID );
+
+	return (FormatString("%d", __ID - 1));
+}
+
+// D: creates a unique integer ID
+int GetUniqueIntID() {
+	// make sure we're not starting from 0 again
+	__ID++;
+	assert( __ID );
+	return ( __ID - 1 );
+}
+
+// D: creates a random integer ID
+int GetRandomIntID() {
+    return rand();
+}
+
+
+//-----------------------------------------------------------------------------
+// Time related functions and variables
+//-----------------------------------------------------------------------------
+
+// D: variable that holds the counter frequency
+static LARGE_INTEGER iHighResCounterFrequency;
+
+// D: variable that holds the start-up counter value
+static LARGE_INTEGER iStartUpCounterValue;
+
+// D: variable that holds the start-up counter value
+static LONGLONG iStartUpCounterValueMs;
+
+// D: variable that holds the start-up clock
+static _timeb tStartUpTime;
+
+// D: initializes the high resolution timer
+void InitializeHighResolutionTimer() {
+    // obtain the counter frequency (ticks per second)
+    QueryPerformanceFrequency(&iHighResCounterFrequency);
+
+    // obtain the current time (starting time) (actually sit in a loop
+    // until the current time changes so that we can avoid as much as 
+    // possible granularity effects
+    _timeb tTimeNow, tLastTime;
+    _ftime(&tTimeNow);
+    do {
+        tLastTime = tTimeNow;
+        _ftime(&tTimeNow);
+    } while(tTimeNow.millitm == tLastTime.millitm);
+    tStartUpTime = tTimeNow;
+
+    // and obtain the current initial counter time
+    QueryPerformanceCounter(&iStartUpCounterValue);
+
+	// converts it into milliseconds
+	iStartUpCounterValueMs = 1000*iStartUpCounterValue.QuadPart/
+        iHighResCounterFrequency.QuadPart;
+}
+
+// D: returns the time of day as a _timeb structure
+_timeb GetTime() {
+
+    _timeb theTime;
+
+    // now compute obtain the current value for the high resolution timer
+    LARGE_INTEGER iCurrentCounterValue;
+    QueryPerformanceCounter(&iCurrentCounterValue);
+
+    // compute the number of milliseconds elapsed
+    LONGLONG liMillisecElapsed = 
+        (1000*(iCurrentCounterValue.QuadPart - iStartUpCounterValue.QuadPart))/
+        iHighResCounterFrequency.QuadPart;
+
+    // now add those milliseconds to the time structure
+    theTime = tStartUpTime;
+    unsigned short ms = theTime.millitm;
+    theTime.millitm = (unsigned short)((ms + liMillisecElapsed) % 1000);
+    // and the seconds
+    theTime.time = (time_t)(theTime.time + 
+        (long int)((ms + liMillisecElapsed)/1000));
+
+    // finally, return the updated time
+	return(theTime);
+}
+
+// A: transforms a string into a time (as a _timeb structure)
+LONGLONG GetCurrentAbsoluteTimeStamp() {
+
+    // now compute obtain the current value for the high resolution timer
+    LARGE_INTEGER iCurrentCounterValue;
+    QueryPerformanceCounter(&iCurrentCounterValue);
+
+    // compute the number of milliseconds elapsed
+    LONGLONG liTimeStamp = 1000*iCurrentCounterValue.QuadPart/
+        iHighResCounterFrequency.QuadPart;
+
+	return liTimeStamp;
+}
+
+// A: transforms a string into a time (as a _timeb structure)
+long GetSessionTimeStamp(LONGLONG liTimeStamp) {
+
+	long lTimeStamp = (long)(liTimeStamp-iStartUpCounterValueMs);
+
+	return lTimeStamp;
+}
+
+// D: transforms a time (as a _timeb structure) into a string
+string TimeToString(_timeb time) {
+	tm* tmTime;
+	string sResult;
+	// convert to local timezone
+	tmTime = localtime(&time.time);
+
+	// and format it as a string and return it
+    return FormatString("%02d:%02d:%02d.%03d", tmTime->tm_hour, 
+		 tmTime->tm_min, tmTime->tm_sec, time.millitm);
+}
+
+// D: returns the time as a long int. Used mainly for logging purposes
+string GetTimeAsString() {
+	return TimeToString(GetTime());
+}

Added: branches/mrmarge/PocketTeamTalk/Agents/MultiDecoder/Audio_Server/Utils.h
===================================================================
--- branches/mrmarge/PocketTeamTalk/Agents/MultiDecoder/Audio_Server/Utils.h	                        (rev 0)
+++ branches/mrmarge/PocketTeamTalk/Agents/MultiDecoder/Audio_Server/Utils.h	2007-10-18 21:29:22 UTC (rev 852)
@@ -0,0 +1,241 @@
+//=============================================================================
+//
+//   Copyright (c) 2000-2004, Carnegie Mellon University.  
+//   All rights reserved.
+//
+//   Redistribution and use in source and binary forms, with or without
+//   modification, are permitted provided that the following conditions
+//   are met:
+//
+//   1. Redistributions of source code must retain the above copyright
+//      notice, this list of conditions and the following disclaimer. 
+//
+//   2. Redistributions in binary form must reproduce the above copyright
+//      notice, this list of conditions and the following disclaimer in
+//      the documentation and/or other materials provided with the
+//      distribution.
+//
+//   This work was supported in part by funding from the Defense Advanced 
+//   Research Projects Agency and the National Science Foundation of the 
+//   United States of America, and the CMU Sphinx Speech Consortium.
+//
+//   THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 
+//   ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
+//   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+//   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
+//   NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+//   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
+//   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
+//   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
+//   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
+//   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
+//   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+//=============================================================================
+ 
+//-----------------------------------------------------------------------------
+// 
+// UTILS.H   - definition of various functions useful throughout the 
+//             components of the architecture
+// 
+// ----------------------------------------------------------------------------
+// 
+// BEFORE MAKING CHANGES TO THIS CODE, please read the appropriate 
+// documentation, available in the Documentation folder. 
+//
+// ANY SIGNIFICANT CHANGES made should be reflected back in the documentation
+// file(s)
+//
+// ANY CHANGES made (even small bug fixes, should be reflected in the history
+// below, in reverse chronological order
+// 
+// HISTORY --------------------------------------------------------------------
+//
+//   [2005-02-08] (antoine): added the Sleep function that waits for a number
+//                           milliseconds
+//   [2004-12-24] (antoine): Added a version of PartitionString with quotation.
+//   [2004-04-01] (dbohus): corrected buffer overrun problem in FormatString
+//   [2003-10-15] (dbohus): added ReplaceSubString for strings
+//   [2003-10-08] (dbohus): added SplitOnFirst variant which takes into account
+//                           quoted strings
+//   [2003-04-23] (dbohus): added GetRandomIntID
+//   [2002-06-26] (dbohus): Added a static buffer to be commonly used by string
+//							 routines
+//	 [2002-05-30] (agh):    Fixed PartitionString() so that it doesn't push
+//                           empty strings into the resultant vector
+//   [2002-05-25] (dbohus): deemed preliminary stable version 0.5
+//	 [2002-04-28] (agh):    added functionality to Trim() functions and added 
+//						     PartitionString()
+//   [2001-12-29] (dbohus): started working on this
+// 
+//-----------------------------------------------------------------------------
+
+#pragma once
+#ifndef __UTILS_H__
+#define __UTILS_H__
+
+// avoid warning messages which occur
+// when including the some of the headers (at least this is the case with 
+// VC++ 6.0
+#pragma warning (disable:4786)
+#pragma warning (disable:4710)
+#pragma warning (disable:4702)
+#pragma warning (disable:4514)
+#pragma warning (push, 1)
+
+// D: Other includes for generic headers
+#include <stdio.h>
+#include <malloc.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <time.h>
+#include <sys/timeb.h>
+#include <assert.h>
+#include <math.h>
+
+// D: now include the standard template library headers
+#include <map>
+#include <vector>
+#include <list>
+#include <set>
+#include <string>
+#include <queue>
+#include <stack>
+#include <iostream>
+
+#pragma warning (pop)
+
+using namespace std;
+
+//-----------------------------------------------------------------------------
+// Various defines used throughout the system
+//-----------------------------------------------------------------------------
+#ifndef PATH_MAX
+#define PATH_MAX 1024
+#endif
+
+#define STRING_MAX 65535
+
+// D: defines for common data types derived from STL
+typedef vector <int, allocator <int> > TIntVector;
+typedef vector <string, allocator <string> > TStringVector;
+typedef map <string, string, less<string>, allocator<string> > STRING2STRING;
+
+// D: defines for OAAS and GALAXYS (these defines are used to specify 
+//    strings that appear only under an OAA or a GALAXY configuration (i.e. 
+//    a common use is in the dialog task specification for specifying prompts)
+#ifdef OAA
+	#define OAAS(STRING) STRING
+	#define GALS(STRING) 
+#endif
+
+#ifdef GALAXY
+	#define OAAS(STRING) 
+	#define GALS(STRING) STRING
+#endif
+
+//-----------------------------------------------------------------------------
+// Functions for extending the string STL class with missing functionality
+//-----------------------------------------------------------------------------
+// D: implements the printf functionality in a string
+string FormatString(const char *lpszFormat, ...);
+
+// D: implements the + operator for strings
+string operator + (const string sString1, const string sString2);
+
+// D: conversion from bool to string
+string BoolToString(bool bBool);
+
+// A: conversion from int to string
+string IntToString(int iInt);
+
+// D: conversion from float to string
+string FloatToString(float fFloat);
+
+// D: make a string uppercase
+string ToUpperCase(string sString);
+
+// D: make a string lowercase
+string ToLowerCase(string sString);
+
+// D: trim specified characters (default spaces) from the string on the left
+string TrimLeft(string sString, char * pToTrim = " \n\t");
+
+// D: trim specified characters (default spaces) from the string on the right
+string TrimRight(string sString, char * pToTrim = " \n\t");
+
+// D: trim specified characters (default space) from the string at both ends
+string Trim(string sString, char * pToTrim = " \n\t");
+
+// D: extracts the first line of a string, and returns it (the string is 
+//    chopped)
+string ExtractFirstLine(string& rString);
+
+// D: splits the string in 2 parts, around and not including the first 
+// occurence of any of a set of specified characters. Returns true on success
+bool SplitOnFirst(string sOriginal, char* pDividers, 
+				  string& rsFirstPart, string& rsSecondPart);
+
+// D: splits the string in 2 parts, around and not including the first 
+// occurence of any of a set of specified characters, also taking into account
+// a quote character. Returns true on success
+bool SplitOnFirst(string sOriginal, char* pDividers, 
+				  string& rsFirstPart, string& rsSecondPart, char cQuote);
+
+// D: splits the string in 2 parts, around and not including the last
+// occurence of any of a set of specified characters. Returns true on success
+bool SplitOnLast(string sOriginal, char* pDividers, 
+				 string& rsFirstPart, string& rsSecondPart);
+
+// D: partitions a string into tokens divided by any of a set of specified
+//    characters.
+vector<string> PartitionString(string sString, char * pDividers);
+
+// D: partitions a string into tokens divided by any of a set of specified
+//    characters. This takes into account a quote character.
+vector<string> PartitionString(string sString, char * pDividers, char cQuote);
+
+// D: replaces a substring ocurring in a string with another string
+string ReplaceSubString(string sSource, string sToReplace, string sReplacement);
+
+// D: find the corresponding closing quote
+unsigned int FindClosingQuoteChar(string sString, unsigned int iStartPos, 
+								  char cOpenQuote, char cCloseQuote);
+
+//-----------------------------------------------------------------------------
+// Functions for constructing unique IDs
+//-----------------------------------------------------------------------------
+
+// D: creates a uniques string ID
+string GetUniqueStringID();
+
+// D: creates a unique integer ID
+int GetUniqueIntID();
+
+// D: creates a random integer ID
+int GetRandomIntID();
+
+//-----------------------------------------------------------------------------
+// Time related functions 
+//-----------------------------------------------------------------------------
+
+// D: initializes the high resolution timer used for logging
+void InitializeHighResolutionTimer();
+
+// D: returns the time of day as a _timeb structure
+_timeb GetTime();
+
+// A: returns the current value of the internal counter
+_int64 GetCurrentAbsoluteTimeStamp();
+
+// A: converts an absolute timestamp into a session timestamp
+//    (by substracting the session start timestamp)
+long GetSessionTimeStamp(_int64 liTimeStamp);
+
+// D: transforms a time (as a _timeb structure) into a string
+string TimeToString(_timeb time);
+
+// D: returns the time of day as a string. Used mainly for logging purposes
+string GetTimeAsString();
+
+#endif // __UTILS_H__
\ No newline at end of file

Added: branches/mrmarge/PocketTeamTalk/Agents/MultiDecoder/Audio_Server/WRSTBEEP.wav
===================================================================
(Binary files differ)


Property changes on: branches/mrmarge/PocketTeamTalk/Agents/MultiDecoder/Audio_Server/WRSTBEEP.wav
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: branches/mrmarge/PocketTeamTalk/Agents/MultiDecoder/Audio_Server/main.cpp
===================================================================
--- branches/mrmarge/PocketTeamTalk/Agents/MultiDecoder/Audio_Server/main.cpp	                        (rev 0)
+++ branches/mrmarge/PocketTeamTalk/Agents/MultiDecoder/Audio_Server/main.cpp	2007-10-18 21:29:22 UTC (rev 852)
@@ -0,0 +1,1887 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdarg.h>
+
+#include "Utils.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <galaxy/galaxy_all.h>
+#define SERVER_FUNCTIONS_INCLUDE "sphinx-server.h" 
+#define USE_SERVER_DATA 
+#include <galaxy/server_functions.h>
+
+#include <prim_type.h>	/* CMU sphinx data type definition	*/
+#include <ad.h>			/* CMU A2D routines/data structures */
+#include <cont_ad.h>	/* CMU continuous listener */
+#include <fe.h>			/* CMU front-end processing	*/
+#include <ckd_alloc.h>
+#include "sock.h"	
+
+#ifdef __cplusplus
+}
+#endif
+
+#include <vector>
+
+using namespace std;
+
+#define DEFAULT_SPS     "8000"	/* only support 8000, 11025, 16000 now */
+
+
+#define DEFAULT_MFC_WRITE	"0"	/* Whether to write MFCs to the logdir */
+#define DEFAULT_LOG_SESSION_AUDIO	"0"	/* Whether to Log whole sessions audio */
+#define DEFAULT_LOG	"1"	/* Whether to Log events/warnings/errors */
+
+static float **mfcbuf;
+static int32 n_rawfr = 0;
+static double silence_secs = 0.1f; /* let n seconds of silence pass after utterance (user may still speak)*/
+static int only_listen_for_dtmf = 0;
+#define MAX_UTT_LEN	6000	/* #frames */
+#define MAX_CEP_LEN	(MAX_UTT_LEN*CEP_SIZE)
+#define MAX_POW_LEN	(MAX_UTT_LEN*POW_SIZE)
+
+#define MAX_FILENAME_SIZE 2048
+#define MAX_LINE_SIZE 2048
+#define CONT_AD_BUFFER_SIZE 4096
+
+#define MAX_NUM_ENGINES 10
+static int MAX_ENGINE_WAIT_ITERATIONS=100;
+#define NUM_FEATURES_FROM_ENGINE 9
+
+#define FRAME_EQUALS_SIGN "= \t"
+
+#define MAX_STRING_SIZE 2048
+
+/*	Default engine_list file name	*/
+#define DEFAULT_CLIENT_LIST	"sphinx_engines.txt"
+#define MAX_NAME_SIZE 256
+#define MAX_HOST_SIZE 256
+#define MAX_PORT_SIZE 256
+
+/* Engine list specifications */
+int n_engines;
+char **engine_name;
+char **engine_host;
+int *engine_port;
+
+/* Variables for handling input file list in batch mode */
+int n_input_files, i_next_input_file;
+char **input_files;
+
+
+SOCKET *engine_socket;
+
+
+/*	Data structure to receive message from engine	*/
+#define RETURN_BUF_SIZE 131072
+#define MAX_HYP_SIZE 2048
+#define MAX_FRAMELIST_SIZE 4096
+#define MAX_UTTID_SIZE 256
+char **engine_buffer;
+int *engine_P;
+
+int mfc_write; /* boolean */
+int log_session_audio; /* boolean */
+int log_messages; /* boolean */
+int batch_mode; /* boolean */
+fe_t *fe;
+
+Gal_Frame *gfResult;
+int *ready;
+
+char Message[RETURN_BUF_SIZE + 1];
+
+/* Definition of the server control block */
+typedef struct {
+	int state;
+	int mode;
+	ad_rec_t *ad;
+	cont_ad_t *cont;
+	int n_frm;
+	int ts;
+	short buf[CONT_AD_BUFFER_SIZE];
+	int n_rd;
+	double utt_maxpow;
+	double conf_thresh;
+} srvcb_t;
+static srvcb_t *srvcb;
+int SPS;
+
+#define ST_IDLE			0
+#define ST_LISTENING	1
+#define ST_DECODING     2
+#define ST_EXITING		3
+
+#define MD_DECODING		0
+#define MD_LISTENING	1
+
+#define STD_STREAM	0
+#define DBG_STREAM	1
+#define ERR_STREAM	2
+
+#define MAX_CIRCULAR_RAW_BUF 512000
+
+/*	if want to change to decoding mode from listening mode
+	and sphinx is currently recording an utterance
+	then switch to decoding mode after the current utterance is finished
+	(xw 26nov00)	*/
+static int about_to_change_to_decoding_mode = 0;
+
+static char *hub_log_dir = NULL;
+static char *hub_log_prefix = NULL;
+static FILE *session_rawfp    = NULL;
+static FILE *rawfp = NULL;
+static FILE *mfcfp = NULL;
+static FILE *logfp = NULL;
+static int  utt_cnt = 0;
+static int interrupt_cnt = 0;	//(xw 26nov00)
+static int usr_speaking = 0;	//xw 26nov00
+
+// (dbohus): store the incoming audio data in a circular raw buffer
+//           this is used for performing the clipping detection annotation
+static short sCircRawBuf[MAX_CIRCULAR_RAW_BUF];
+static int iCircRawBufPtr = 0;
+// (dbohus): store the max signal level value
+static short sMaxSignalValue = 0;
+// (dbohus): define the number of bins for the clipping histogram
+#define CLIPPING_NUM_BINS 10
+
+
+/* (antoine 2004-04-03): Utterance ID management to prevent desync */
+int utt_id;
+char utt_id_str[MAX_UTTID_SIZE];
+
+// Timestamp (in samples since the continuous listener was started)
+// at the beginning of the current utterance
+int utt_start_timestamp;
+
+/* This array of structures defines the command line arguments for
+ * the OA command line processing routines in the GalaxyCommunicator
+ * distribution
+ */
+static char *oas[] = {
+  "-sps <int>", "Samples per second", DEFAULT_SPS,
+  "-engine_list <list_file>", "File containing list of sphinx engines", DEFAULT_CLIENT_LIST,
+  "-mfc_write <0/1>", "Whether .mfc files should be written", DEFAULT_MFC_WRITE,
+  "-log <0/1>", "Turns logging to a file on and off.", DEFAULT_LOG,
+  "-log_full_session_input <0/1>", 
+	"Whether a single raw file for whole sessions should be stored (in additiotn to turn raw files)", 
+	DEFAULT_LOG_SESSION_AUDIO,
+  "-ctl_file <file>", "Control file containing list of raw files to recognize (batch mode)", NULL,
+NULL
+};
+
+//tk
+vector<GalIO_CommStruct*> sv_comms;
+
+#ifdef _DEBUG
+int bDebug = 1;
+#endif
+
+#ifndef _DEBUG
+int bDebug = 0;
+#endif
+
+void srvtt_idle(void *arg);
+void srvtt_listen(void *arg);
+void srvtt_utt(void *arg);
+void srvtt_end(void *arg);
+static void audio_stop_flush ();
+int get_word(char **string, char *word);
+int get_frame(char **string, char *word);
+void wait_for_return();
+int send_message_to_engine(int engine_id, char *message);
+int is_dtmf_engine(int);
+
+char *get_timestamp() {
+	SYSTEMTIME time;
+	char *timestamp;
+
+	timestamp = (char*)malloc(MAX_LINE_SIZE);
+	GetLocalTime(&time);
+	sprintf( timestamp, "%.2d:%.2d:%.2d.%.3d", time.wHour, time.wMinute, 
+		time.wSecond, time.wMilliseconds);
+
+	return timestamp;
+}
+
+void Log(int stream, const char *message, ...) {
+    va_list ap;
+	char *str_id;
+	char *timestamp = get_timestamp();
+
+	// Define the stream id
+	switch (stream) {
+		case DBG_STREAM:
+			str_id = "DBG";
+			break;
+		case STD_STREAM:
+			str_id = "STD";
+			break;
+		case ERR_STREAM:
+			str_id = "ERR";
+			break;
+		default:
+			str_id = "UNK";
+	}
+
+	// Debug stream is displayed on console only in debug mode
+	if (stream == DBG_STREAM) {
+		if (bDebug) {
+			fprintf( stderr, "[%s@%s] ", str_id, timestamp);
+			va_start (ap, message);
+			vfprintf( stderr, message, ap);
+			fprintf( stderr, "\n");
+		}
+	}
+	else {
+		fprintf( stderr, "[%s@%s] ", str_id, timestamp);
+		va_start (ap, message);
+		vfprintf( stderr, message, ap);
+		fprintf( stderr, "\n");
+	}
+
+	// Output message to Log file
+	if (logfp) {
+		fprintf( logfp, "[%s@%s] ", str_id, timestamp);
+		va_start (ap, message);
+		vfprintf (logfp, message, ap);
+		fprintf( logfp, "\n");
+		fflush( logfp);
+	}
+
+    va_end (ap);
+
+	free(timestamp);
+}
+
+void read_engine_list( const char *engine_list) {
+	int i;
+	char line[MAX_LINE_SIZE];
+	FILE *stream;
+
+	/* Reads engine list */
+	if( (stream  = fopen( engine_list, "r" )) == NULL ) {
+	   Log( ERR_STREAM, "failed to open engine list file %s", engine_list );
+	   exit(-1);
+	}
+	
+	/* Traverses the file once to get the number of lines */
+	n_engines = 0;
+	while (fgets( line, MAX_FILENAME_SIZE, stream))
+		if ((strlen(line) > 1)&&(line[0] != '#')) n_engines++;
+	fseek( stream, 0, SEEK_SET);
+
+	/* initializes engine description arrays */
+	engine_name = (char**)malloc( n_engines * sizeof(char*));
+	engine_host = (char**)malloc( n_engines * sizeof(char*));
+	engine_port = (int*)malloc( n_engines * sizeof(int));
+	engine_socket = (SOCKET*)malloc( n_engines * sizeof( SOCKET));
+	engine_buffer = (char**)malloc( n_engines * sizeof(char*));
+	engine_P = (int*)malloc( n_engines * sizeof(int));
+	ready = (int*)malloc( n_engines * sizeof(int));
+	gfResult = (Gal_Frame*)malloc( n_engines * sizeof(Gal_Frame));
+
+	/* initializes each engine's parameters */
+	for (i = 0; i < n_engines; i++) {
+		engine_name[i] = (char*)malloc( MAX_NAME_SIZE + 1);
+		engine_host[i] = (char*)malloc( MAX_HOST_SIZE + 1);
+		engine_buffer[i] = (char*)malloc( RETURN_BUF_SIZE + 1);
+		engine_P[i] = 0;
+
+		/* reads from the file the description of the engines (name host port) */
+		fgets( line, MAX_FILENAME_SIZE, stream);
+		while ((strlen(line) <= 1)||(line[0] == '#')) {
+			if (!fgets( line, MAX_FILENAME_SIZE, stream)) {
+				Log( ERR_STREAM, "invalid format in list file %s", engine_list );
+				exit(-1);
+			}			
+		}
+
+		sscanf( line, "%s %s %d", engine_name[i], engine_host[i], &engine_port[i]);
+		Log( STD_STREAM, "%s: host %s   port %d", engine_name[i], engine_host[i], engine_port[i]);
+
+		engine_socket[i] = INVALID_SOCKET;
+
+	}
+
+	return;
+}
+
+/* Reads a list of audio files from a control file */
+/* for batch mode processing */
+void read_ctl_file( const char *ctl_file) {
+	int i;
+	char line[MAX_FILENAME_SIZE];
+	FILE *stream;
+
+	/* Reads ctl file */
+	if( (stream  = fopen( ctl_file, "r" )) == NULL )
+		Log( ERR_STREAM, "failed to open ctl file %s", ctl_file );
+
+	/* Traverses the file once to get the number of lines */
+	i_next_input_file = 0;
+	n_input_files = 0;
+	while (fgets( line, MAX_FILENAME_SIZE, stream))
+		// Counts line that are neither empty nor messages
+		// (messages start with ">")
+		if (strlen(line) > 1) n_input_files++;
+	fseek( stream, 0, SEEK_SET);
+
+	/* Reads the raw audio input file names */
+	input_files = (char**)malloc( n_input_files * sizeof(char*));
+	for (i = 0; i < n_input_files; i++) {
+		input_files[i] = (char*)malloc( MAX_FILENAME_SIZE * sizeof(char));
+		fgets( input_files[i], MAX_FILENAME_SIZE, stream);
+
+		// Don't count empty lines as file names
+		if (strlen(input_files[i]) <= 1) 
+			i--;
+		else {
+			// Gets rid of the newline
+			if (input_files[i][strlen(input_files[i])-1] == '\n')
+				input_files[i][strlen(input_files[i])-1] = '\0';
+
+			if (input_files[i][0] != '>') {
+				// Adds the file extension
+//				input_files[i] = strcat(input_files[i], ".raw");
+			}
+		}
+	}
+
+	fclose(stream);
+
+	return;
+}
+
+/************************************** interface for hub *************************************************/
+
+/*
+ * This routine is called by GalaxyCommunicator once at
+ * program startup time.
+ */
+void *_GalSS_init_server(Gal_Server *s, int argc, char **argv)
+{
+	int i, k;
+	char ctl_fn[MAX_FILENAME_SIZE];
+	char engine_list_fn[MAX_FILENAME_SIZE];
+	char *p;
+
+	GalUtil_Print (-1, "Compiled on %s at %s\n", __DATE__, __TIME__);
+
+	if (!GalUtil_OACheckUsage(argc, argv, oas, &i))
+		exit(1);
+
+	if (!GalUtil_OAExtract(argc, argv, oas, "-sps", GAL_OA_STRING, &p)) {
+		SPS = atoi(DEFAULT_SPS);
+	}
+	else
+		SPS = atoi(p);
+
+	if (!GalUtil_OAExtract(argc, argv, oas, "-engine_list", GAL_OA_STRING, &p)) {
+		strncpy(engine_list_fn, DEFAULT_CLIENT_LIST, MAX_FILENAME_SIZE-1);
+	}
+	else
+		strncpy(engine_list_fn, p, MAX_FILENAME_SIZE-1);
+
+	if (!GalUtil_OAExtract(argc, argv, oas, "-mfc_write", GAL_OA_STRING, &p)) {
+		mfc_write = atoi(DEFAULT_MFC_WRITE);
+	}
+	else
+		mfc_write = atoi(p);
+
+	if (!GalUtil_OAExtract(argc, argv, oas, "-log_full_session_input", GAL_OA_STRING, &p)) {
+		log_session_audio = atoi(DEFAULT_LOG_SESSION_AUDIO);
+	}
+	else
+		log_session_audio = atoi(p);
+
+	if (!GalUtil_OAExtract(argc, argv, oas, "-log", GAL_OA_STRING, &p)) {
+		log_messages = atoi(DEFAULT_LOG);
+	}
+	else
+		log_messages = atoi(p);
+
+	if (!GalUtil_OAExtract(argc, argv, oas, "-ctl_file", GAL_OA_STRING, &p)) {
+		batch_mode = 0;
+	}
+	else {
+		batch_mode = 1;
+		strncpy(ctl_fn, p, MAX_FILENAME_SIZE-1);
+
+		// In batch mode, no real time constraint, so we
+		// can wait for responses from engines longer
+		MAX_ENGINE_WAIT_ITERATIONS=1000;
+	}
+
+	if (mfc_write)
+	{
+		// copied from uttproc.c
+		param_t *fe_param;
+		fe_param = (param_t*)ckd_calloc(1, sizeof(param_t));
+		fe_param->SAMPLING_RATE = (float32)SPS;
+		fe_param->FRAME_RATE    = 100;
+		fe_param->PRE_EMPHASIS_ALPHA = (float32)0.97;
+		fe = fe_init(fe_param);
+		free(fe_param);
+
+		mfcbuf = (float **) ckd_calloc (MAX_UTT_LEN+10, sizeof(float *));
+		mfcbuf[0] = (float *) ckd_calloc ((MAX_UTT_LEN+10)*fe->NUM_CEPSTRA, sizeof(float));
+		for (k = 1; k < MAX_UTT_LEN+10; k++)
+			mfcbuf[k] = mfcbuf[k-1] + fe->NUM_CEPSTRA;
+	}
+
+	// Reads engine specifications
+	read_engine_list( engine_list_fn);
+
+	// Reads input audio file list
+	if (batch_mode) {
+		read_ctl_file( ctl_fn);
+	}
+
+	Log( STD_STREAM, "Sampling Rate: %d", SPS);
+
+	/* Initialize and test audio device, to make sure we can use it */
+	if (!batch_mode) {
+		srvcb = (srvcb_t *)calloc(1, sizeof(srvcb_t));
+		if (srvcb == NULL) {
+			GalUtil_Fatal("Unable to alloc memory for server control block\n");
+		}
+		if ((srvcb->ad = ad_open_sps (SPS)) == NULL) {
+			GalUtil_Fatal("ad_open failed\n");
+		}
+		ad_close (srvcb->ad);
+
+		srvcb->state = ST_IDLE;
+		srvcb->mode = MD_DECODING;		//default is decoding mode (xw 26nov00)
+	}
+
+	return NULL;
+}
+
+
+/* This is invoked by the hub when it connects to the server (when else???) */
+Gal_Frame reinitialize(Gal_Frame f, void *server_data)
+{
+	int i;
+
+	GalIO_CommStruct *comm = GalSS_EnvComm((GalSS_Environment *)server_data);
+
+	if (Gal_GetObject(f, ":greeting")) {
+		GalUtil_Print(-1, "H: %s\n", Gal_GetString(f, ":greeting"));
+	}
+
+	if (!batch_mode) {
+		srvcb->state = ST_IDLE;
+		srvcb->mode = MD_DECODING;		//default is decoding mode (xw 26nov00)
+		if (!sv_comms.empty()) {
+			Gal_RemoveTimedTask(srvtt_idle, sv_comms.back());
+		}
+	}
+
+	sv_comms.push_back(comm);
+
+	for (i = 0; i < n_engines; i++) {
+		if (engine_socket[i] != INVALID_SOCKET) {
+			sock_close(engine_socket[i]);	
+			engine_socket[i] = INVALID_SOCKET;
+		}
+	}
+
+	if (!batch_mode) {
+		GalUtil_Print(-1, "*** -> IDLE ***\n");
+		Gal_AddTimedTask(srvtt_idle, (void *)comm, 1);
+	}
+
+	return f;
+}
+
+/* This is invoked by the hub when it connects to the server (when else???) */
+Gal_Frame begin_session( Gal_Frame f, void *server_data)
+{
+	//char to_engine[256];
+	fprintf(stderr, "beginning session\n");
+
+	if (batch_mode) {
+		utt_id = 0;
+	} 
+	else {
+		// (antoine 12/22/2004) Calibrates the continuous listener for the new session
+		cont_ad_calib(srvcb->cont);
+	}
+
+	// Announces the beginning of a new session to the engines
+	//sprintf(to_engine, "%s\n", "engine_new_session");
+	//for (i = 0; i < n_engines; i++) {
+	//	send_message_to_engine(i, to_engine);
+	//}
+
+	fprintf(stderr, "session begun\n");
+	return f;
+}
+
+/* This is invoked at the end of a session */
+Gal_Frame end_session( Gal_Frame f, void *server_data)
+{
+	Log( STD_STREAM, "end_session received");
+
+	if (logfp) {
+		fflush(logfp);
+		fclose(logfp);
+		logfp = NULL;
+	}
+
+	return f;
+}
+
+/*	set configuration (was previously set_lm) */
+Gal_Frame set_config(Gal_Frame f, void *server_data)
+{
+	int i;
+	char to_engine[256];
+	char *slotvalue;
+
+	GalIO_CommStruct *comm = GalSS_EnvComm((GalSS_Environment *)server_data);
+
+	if (Gal_GetObject(f, ":set_lm")) {
+		Log( DBG_STREAM, ":set_lm found");
+		slotvalue = Gal_GetString(f, ":set_lm");
+
+		for (i = 0; i < n_engines; i++) {
+			if (!is_dtmf_engine(i)) {
+				// engine is *not* DTMF, so send it the LM
+				Log( STD_STREAM, "Setting LM for engine %s to %s", engine_name[i], slotvalue);
+				sprintf(to_engine, "%s %s \n", "engine_set_lm", slotvalue);
+				send_message_to_engine(i, to_engine);
+			}
+		}
+	}
+	if (Gal_GetObject(f, ":set_dtmf_len")) {
+		Log( DBG_STREAM, ":set_dtmf_len found");
+		slotvalue = Gal_GetString(f, ":set_dtmf_len");
+
+		for (i = 0; i < n_engines; i++) {
+			if (is_dtmf_engine(i)) {
+				// engine *is* DTMF, so send it the LM
+				Log( STD_STREAM, "Setting LM for engine %s to %s", engine_name[i], slotvalue);
+				sprintf(to_engine, "%s %s \n", "engine_set_lm", slotvalue);
+				send_message_to_engine(i, to_engine);
+			}
+		}
+	}
+	if (Gal_GetObject(f, ":set_silence_secs")) {
+		Log( DBG_STREAM, ":set_silence_secs found");
+		slotvalue = Gal_GetString(f, ":set_silence_secs");
+		silence_secs = atof(slotvalue);
+	}
+	if (Gal_GetObject(f, ":only_listen_for_dtmf")) {
+		Log( DBG_STREAM, ":only_listen_for_dtmf found");
+		slotvalue = Gal_GetString(f, ":only_listen_for_dtmf");
+		only_listen_for_dtmf = atoi(slotvalue);
+	}
+	return f;
+}
+
+
+/* Synchronous hub scripts will call this routine when token processing
+ * for an utterance has finished and the system is ready for any new
+ * utterances. */
+Gal_Frame start_listening(Gal_Frame f, void *server_data)
+{
+	fprintf(stderr, "starting listening\n");
+	char session_rawfn[1024];
+	char logfn[1024];
+	char to_engine[256];
+	int i;
+	int32 delta_sil, delta_speech, min_noise, max_noise,
+		  winsize, speech_onset, sil_onset, leader, 
+		  trailer;
+	float32 adapt_rate;
+
+	if (!batch_mode) {
+		srvcb->state = ST_LISTENING;
+		srvcb->mode = MD_DECODING;	//default is decoding mode (xw 26nov00)
+		utt_cnt = 0;
+		interrupt_cnt = 0;			//xw 26nov00
+	}
+
+	if (hub_log_dir) {
+		free(hub_log_dir);
+		hub_log_dir = NULL;
+	}
+	if (hub_log_prefix) {
+		free(hub_log_prefix);
+		hub_log_prefix = NULL;
+	}
+	if (Gal_GetObject(f, ":hub_logdir")) {
+		hub_log_dir = _strdup(Gal_GetString(f, ":hub_logdir"));
+	}
+	else
+		fprintf( stderr, "Can't find :hub_logdir\n");
+
+	if (Gal_GetObject(f, ":hub_log_prefix")) {
+		hub_log_prefix = _strdup(Gal_GetString(f, ":hub_log_prefix"));
+	}
+	else
+		fprintf( stderr, "Can't find :hub_log_prefix\n");
+
+	if (log_messages) {
+		if (logfp)
+			fclose(logfp);
+		sprintf(logfn, "%s%s-audio_server.log", hub_log_dir, hub_log_prefix);
+		logfp = fopen( logfn, "wb");
+	}
+
+	Log( STD_STREAM, "hub_logdir= |%s|",
+		(hub_log_dir ? hub_log_dir : "null"));
+	Log( STD_STREAM, "hub_log_prefix= |%s|",
+		(hub_log_prefix ? hub_log_prefix : "null"));
+
+	if ((srvcb->ad = ad_open_sps (SPS)) == NULL) {
+		GalUtil_Fatal("ad_open failed\n");
+	}
+	/* Open the continuous listening/silence filtering module and calibrate it */
+	if ((srvcb->cont = cont_ad_init (srvcb->ad, ad_read)) == NULL) {
+		ad_close (srvcb->ad);
+		GalUtil_Fatal("cont_ad_init failed\n");
+	}
+
+	// (antoine 07/24/2006) Log detailed information about the endpointer
+	// (for debug purposes)
+	//cont_ad_set_logfp(srvcb->cont, logfp);
+
+	// (antoine 03/07/2005) Eventually modify some parameters 
+	// of the continuous listener
+	cont_ad_get_params (srvcb->cont, &delta_sil, &delta_speech,
+			  &min_noise, &max_noise,
+			  &winsize, &speech_onset, &sil_onset,
+			  &leader, &trailer, &adapt_rate);
+	// Set a high min_noise threshold to ignore absolute zeros in the
+	// estimation of noise level (these might be sent by cell phones
+	// during non-speech phases)
+	min_noise = 10;
+	cont_ad_set_params (srvcb->cont, delta_sil, delta_speech,
+			  min_noise, max_noise, winsize, speech_onset, sil_onset,
+			  leader, trailer, adapt_rate);
+
+
+
+	// (antoine 01/03/2005) Raw audio file to store the whole session
+	// (regardless of endpointing)
+	if (log_session_audio) {
+		sprintf(session_rawfn, "%s%s-input.raw", hub_log_dir, hub_log_prefix);
+		session_rawfp = fopen( session_rawfn, "wb");
+		cont_ad_set_rawfp( srvcb->cont, session_rawfp);
+	}
+
+	ad_start_rec (srvcb->ad);	// start the flow of data from the A2D
+
+	// Sends the path to the new Log folder to the engines
+	sprintf(to_engine, "%s %s%s\n", "engine_new_session", hub_log_dir, hub_log_prefix);
+	for (i = 0; i < n_engines; i++) {
+		send_message_to_engine(i, to_engine);
+	}
+	fprintf(stderr, "ending listening\n");
+
+	return (Gal_Frame) f;
+}
+
+
+Gal_Frame stop_listening(Gal_Frame f, void *server_data)
+{
+	if (!batch_mode)
+		srvcb->state = ST_IDLE;
+
+	return (Gal_Frame) f;
+}
+
+
+
+// D: This function serves as a file feeder for cont_ad in batch mode
+//    (it's essentially copied from sphinx2's file_ad_read); it assumes
+//    the file is opened and pointed to by fpBatchRaw
+static FILE* fpBatchRaw;
+static int32 my_file_ad_read (ad_rec_t *r, int16 *buf, int32 max)
+{
+    int32 k;
+    
+    k = (int32)fread (buf, sizeof(int16), max, fpBatchRaw);
+    
+    return ((k > 0) ? k : -1);
+}
+
+// A: This function reads audio through the standard ad_read function
+//    (from Sphinx's ad library) but keeps a buffer in memory.
+//    It allows to set the point from which to read (from the buffer),
+//    thus allowing to "rewind" and restart streaming from some point
+//    in the past. This is used when an external module does endpointing
+//    and specifies where an utterance started (originally for Antoine's 
+//    Interaction Manager architecture).
+#define AUDIO_BUFFER_SIZE 65536
+static int32 iABShiftSize = 8192;
+static int16 pAudioBuffer[AUDIO_BUFFER_SIZE];
+static int32 iABReadPointer = 0;
+static int32 iABFillPointer = 0;
+static int32 iABStartTimeStamp = 0;
+static int32 buffered_ad_read (ad_rec_t *r, int16 *buf, int32 max)
+{
+	// Get the latest audio from the AD device
+	int iNumRead = ad_read(r, buf, max);
+
+	// Test if we filled up the buffer
+	if (iABFillPointer + iNumRead > AUDIO_BUFFER_SIZE) {
+		// Shift the content of the buffer (losing the first slice of
+		// size iABShiftSize samples)
+		memcpy(pAudioBuffer, pAudioBuffer + iABShiftSize * sizeof(int16),
+			iABFillPointer * sizeof(int16));
+
+		// Adjust the pointers
+		iABFillPointer -= iABShiftSize;
+		iABReadPointer -= iABShiftSize;
+		if (iABReadPointer < 0) iABReadPointer = 0;
+		iABStartTimeStamp += iABShiftSize;
+	}
+
+	// Add the new audio at the end of the buffer
+	memcpy(pAudioBuffer + iABFillPointer, buf, max * sizeof(int16));
+	iABFillPointer += iNumRead;
+
+	int iNumOut = iNumRead;
+
+	// If the reading pointer has been set at some point in the past
+	if (iABReadPointer != iABFillPointer) {
+		// Number of samples up to the last one
+		iNumOut = iABFillPointer - iABReadPointer;
+		// Should return at most max samples
+		if (iNumRead > max) iNumRead = max;
+
+		// Fill in the output buffer
+		memcpy(buf, pAudioBuffer + iABReadPointer, iNumRead * sizeof(int16));
+	}
+
+    return iNumOut;
+}
+
+// A: Sets the reading pointerfor buffered_ad_read
+static void set_buffered_ad_pointer(int32 iTimeStamp) {
+	iABReadPointer = iTimeStamp - iABStartTimeStamp;
+}
+
+/* (antoine 2005-01-04)
+ *  In batch mode: decodes all files and sends the results to galaxy
+ *	In live mode: switch to listening mode (waits for data from
+ *                continuous listener 
+ */
+Gal_Frame start_decoding(Gal_Frame f, void *server_data)
+{
+	int16 cpRawBuffer[CONT_AD_BUFFER_SIZE+1];
+	char sToEngine[256], sGalaxyCommand[256], sGalaxyArg[2048];
+	int i, j, iNumRead, iNumSamplesRead;
+	int bGotResult = 0;
+	cont_ad_t *cont; 
+	ad_rec_t ad;
+	float batchPow=0, batchNPow=0;
+
+
+	Gal_Object *pgoUtterances;
+	Gal_Frame repl;
+	GalIO_CommStruct *comm = GalSS_EnvComm((GalSS_Environment *)server_data);
+
+	// In batch mode, send all the files from the ctl file
+	// to the engines now
+	if (batch_mode) {
+
+		char logfn[1024];
+		if (logfp)
+			fclose(logfp);
+		sprintf(logfn, "audio_server.log");
+		logfp = fopen( logfn, "wb");
+
+		// Look for the next input file path in the ctl file
+		while ((i_next_input_file < n_input_files)
+			&&(input_files[i_next_input_file][0] == '>')) {
+
+			// Message line (e.g. begin/end session)
+			// Send the message to Galaxy
+		
+			// Get the command to send
+			sGalaxyCommand[0] = ':';
+			for (i = 1; 
+				(i < (signed)strlen(input_files[i_next_input_file]))&&
+					(input_files[i_next_input_file][i] != ' '); i++) {
+				sGalaxyCommand[i] = input_files[i_next_input_file][i];
+			}
+			sGalaxyCommand[i] = '\0';
+			i++;
+
+			// Get the arguments to send
+			j=0;
+			while (i < (signed)strlen(input_files[i_next_input_file])) {
+				sGalaxyArg[j] = input_files[i_next_input_file][i];
+				i++;
+				j++;
+			}
+			sGalaxyArg[j] = '\0';
+
+			// If it's a begin_session command build a frame with the
+			// directory of the Log file for helios
+			if (!strcmp( sGalaxyCommand, ":begin_session")) {
+				Log( STD_STREAM, "New Session (%s)", sGalaxyArg);
+
+				// Reinitializes the utterance ID
+				utt_id = 0;
+
+				repl = Gal_MakeFrame("main", GAL_CLAUSE);
+				Gal_SetProp(repl, ":begin_session", Gal_StringObject(""));
+				Gal_SetProp(repl, ":batch_logdir", Gal_StringObject(sGalaxyArg));
+
+				// Announces the beginning of a new session to the engines
+				sprintf(sToEngine, "%s\n", "engine_new_session");
+				for (i = 0; i < n_engines; i++) {
+					send_message_to_engine(i, sToEngine);
+				}
+			}
+			else {
+				Log( STD_STREAM, "Sending to hub: %s=%s", sGalaxyCommand, sGalaxyArg);
+
+				// Send the command to the Galaxy hub
+				repl = Gal_MakeFrame("main", GAL_CLAUSE);
+				Gal_SetProp(repl, sGalaxyCommand, Gal_StringObject(sGalaxyArg));
+			}
+
+			GalIO_CommWriteFrame(comm, repl, GAL_TRUE /* blocking calls */);
+			i_next_input_file++;
+		}
+
+		// Check if we've done all the input files
+		if (i_next_input_file == n_input_files) {
+			repl = Gal_MakeFrame("main", GAL_CLAUSE);
+			Gal_SetProp(repl, ":endofctlfile", Gal_StringObject( ""));
+			GalIO_CommWriteFrame(comm, repl, GAL_FALSE /* don't block */);
+			return repl;
+		}
+
+		// Extract state name (separated from path by a tab char)
+		char *sState = strchr(input_files[i_next_input_file],'\t');
+		if (sState) {
+			// There was a state name specified
+
+			// Split the string between path and state
+			*sState = '\0';
+			sState++;
+
+			// Sends the state name to all (non-dtmf) engines
+			for (i = 0; i < n_engines; i++) {
+				if (!is_dtmf_engine(i)) {
+					// engine is *not* DTMF, so send it the LM
+					Log( STD_STREAM, "Setting LM for engine %s to %s", engine_name[i], sState);
+					sprintf(sToEngine, "%s %s \n", "engine_set_lm", sState);
+					send_message_to_engine(i, sToEngine);
+				}
+			}
+
+		}
+
+		// Start processing the next input file
+		fpBatchRaw = fopen( input_files[i_next_input_file], "rb");
+		if (fpBatchRaw == NULL) {
+			Log( ERR_STREAM, "Couldn't open raw input file %s", input_files[i_next_input_file]);
+			exit(1);
+		}
+		Log( DBG_STREAM, "Opening RAW file %s...", input_files[i_next_input_file]);
+
+		// For each file, open the continuous listening/silence filtering 
+		// module and calibrate it; open it so that it reads from the 
+		// specified file, also use the cont_ad_init_raw_mode opening
+		// function so that the files are not chopped prematurely
+		ad.sps = SPS;
+		ad.bps = sizeof(int16);
+		if ((cont = cont_ad_init_rawmode(&ad, my_file_ad_read)) == NULL) {
+			ad_close (&ad);
+			GalUtil_Fatal("cont_ad_init failed\n");
+		}
+
+		// Announce coming data to engines
+		sprintf(utt_id_str, "%.3d", utt_id);
+		sprintf(sToEngine, "%s %s 0\n", "engine_begin_utt", utt_id_str);
+		for (i = 0; i < n_engines; i++) {
+			send_message_to_engine(i, sToEngine);
+		}
+
+		// also compute batchPow
+		batchPow = -1;
+
+		// Reads raw audio data block by block and sends it to the engines
+		iNumSamplesRead = cont_ad_read (cont, cpRawBuffer, CONT_AD_BUFFER_SIZE);
+		iNumRead = iNumSamplesRead * ad.bps;
+		if(batchPow < cont->siglvl) {
+			batchPow = (float)cont->siglvl;
+		}
+		
+		Log( DBG_STREAM, "Read %d bytes from file...", iNumRead);
+
+		while (iNumRead >= 0) {
+			sprintf(sToEngine, "%s %d \n", "engine_proc_raw", iNumRead);
+			for (i = 0; i < n_engines; i++) {
+				if (!only_listen_for_dtmf || is_dtmf_engine(i)) {
+					if (send_message_to_engine(i, sToEngine)) {
+						if (sock_send_block(engine_socket[i], (char*)cpRawBuffer, iNumRead) != iNumRead)
+							Log( ERR_STREAM, "problems on sending data to %s decoding engine: raw data", engine_name[i]);
+						else 
+							Log( DBG_STREAM, "%d bytes raw data sent to %s decoding engine", iNumRead, engine_name[i]);
+					}
+				}
+			}
+			// set the raw mode for the sphinx decoder so that it does not
+			// chop the utterance before it ends
+			iNumSamplesRead = cont_ad_read (cont, cpRawBuffer, CONT_AD_BUFFER_SIZE);
+			iNumRead = iNumSamplesRead * ad.bps;
+			if(batchPow < cont->siglvl) {
+				batchPow = (float)cont->siglvl;
+			}
+			
+			Log( DBG_STREAM, "Read %d bytes from file...", iNumRead);
+		}
+
+		batchNPow = (float)cont->noise_level;
+
+		// asks results to engines
+		Log( STD_STREAM, "Sent utterance %s to engines ... ", utt_id_str);
+		for (i = 0; i < n_engines; i++) {
+			send_message_to_engine(i, "engine_end_utt \n");
+			send_message_to_engine(i, "engine_proc_result \n");
+		}
+
+		wait_for_return();
+
+		repl = Gal_MakeFrame("main", GAL_CLAUSE);
+
+		// Gets the hypothesis from each engine
+
+		// (dbohus): first count how many hyps we will have total
+		int iHypsCount = 0;
+		for(i = 0; i < n_engines; i++) {
+			int iNumHyps;
+			Gal_GetList(gfResult[i], ":results", &iNumHyps);
+			if(iNumHyps)
+				iHypsCount += iNumHyps;
+			else
+				iHypsCount++;
+		}
+
+		// (dbohus): now allocate the utterances
+		pgoUtterances = (Gal_Object *)malloc(sizeof(Gal_Object)*iHypsCount);
+		int iIndex = 0;
+		// (dbohus): and fill them in
+		for (i = 0; i < n_engines; i++) {
+			if(gfResult[i]) {
+				int iNumHyps = 0;

@@ Diff output truncated at 60000 characters. @@


More information about the TeamTalk-developers mailing list