Initial import to Gerrit.
[profile/ivi/festival.git] / src / modules / MultiSyn / EST_JoinCostCache.cc
1 /*************************************************************************/
2 /*                                                                       */
3 /*                Centre for Speech Technology Research                  */
4 /*                 (University of Edinburgh, UK) and                     */
5 /*                           Korin Richmond                              */
6 /*                         Copyright (c) 2004                            */
7 /*                         All Rights Reserved.                          */
8 /*                                                                       */
9 /*  Permission is hereby granted, free of charge, to use and distribute  */
10 /*  this software and its documentation without restriction, including   */
11 /*  without limitation the rights to use, copy, modify, merge, publish,  */
12 /*  distribute, sublicense, and/or sell copies of this work, and to      */
13 /*  permit persons to whom this work is furnished to do so, subject to   */
14 /*  the following conditions:                                            */
15 /*                                                                       */
16 /*   1. The code must retain the above copyright notice, this list of    */
17 /*      conditions and the following disclaimer.                         */
18 /*   2. Any modifications must be clearly marked as such.                */
19 /*   3. Original authors' names are not deleted.                         */
20 /*   4. The authors' names are not used to endorse or promote products   */
21 /*      derived from this software without specific prior written        */
22 /*      permission.                                                      */
23 /*                                                                       */
24 /*  THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK        */
25 /*  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING      */
26 /*  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT   */
27 /*  SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE     */
28 /*  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES    */
29 /*  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   */
30 /*  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,          */
31 /*  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF       */
32 /*  THIS SOFTWARE.                                                       */
33 /*                                                                       */
34 /*************************************************************************/
35 /*                                                                       */
36 /*                          Author: Korin Richmond                       */
37 /*                            Date: January 2004                         */
38 /*                                                                       */
39 /*************************************************************************/
40
41 #include "EST_JoinCostCache.h"
42 #include "EST_JoinCost.h"
43 #include "EST_error.h"
44 #include "safety.h"
45 #include <iostream>
46 #include <cmath>
47
48 EST_JoinCostCache::EST_JoinCostCache( unsigned int id )
49   : numInstances(0),
50     _id(id),
51     cache(0),
52     cachelen(0)
53
54 {
55
56 }
57
58 EST_JoinCostCache::EST_JoinCostCache( unsigned int id, unsigned int n )
59   : numInstances(n),
60     _id(id),
61     cachelen((n*n)/2-n),
62     deleteMemoryOnDeath(true)
63 {
64   cache = new unsigned char [cachelen];
65   CHECK_PTR( cache );
66 }
67   
68 EST_JoinCostCache::EST_JoinCostCache( unsigned int id, unsigned char *mem, unsigned int n, bool del )
69   : numInstances(n),
70     _id(id),
71     cache(mem),
72     cachelen((n*n)/2-n),
73     deleteMemoryOnDeath(del)
74 {
75
76 }
77
78 EST_JoinCostCache::~EST_JoinCostCache()
79 {
80   if( cache != 0 )
81     if( deleteMemoryOnDeath )
82       delete [] cache;
83 }
84
85 unsigned char EST_JoinCostCache::val( unsigned int a, unsigned int b ) const
86 {
87   if( a>numInstances || b>numInstances )
88     EST_error( "Requested index greater than cache size" );
89
90   if( a == b )
91     return minVal;
92   else if( b > a )
93     return cache[(b*(b-1)>>1)+a];
94   else
95     return cache[(a*(a-1)>>1)+b];
96   
97   return defVal;
98 }
99
100
101 bool EST_JoinCostCache::setval( unsigned int a, unsigned int b, unsigned char v )
102 {
103   if( a>numInstances || b>numInstances )
104     EST_error( "Requested index greater than cache size" );
105
106   if( a == b ){
107     return true;
108   }
109   else if( b > a ){
110     cache[(b*(b-1)>>1)+a] = v;
111     return true;
112   }
113   else{
114     cache[(a*(a-1)>>1)+b] = v;
115     return true;
116   }
117   
118   return false;
119 }
120
121
122 ostream& EST_JoinCostCache::write( ostream &os ) const
123 {
124   os << cachelen;
125   //  os.write( cache, cachelen );
126   return os;
127 }
128
129 bool EST_JoinCostCache::computeAndCache( const EST_TList<EST_Item*> &list,
130                                          const EST_JoinCost &jc, 
131                                          bool verbose )
132 {
133   unsigned char qcost; // quantized cost
134   
135   unsigned int qleveln = maxVal-minVal;
136
137   float ulimit = 1.0-1/(float)(qleveln);
138   float llimit = 0.0+1/(float)(qleveln);
139   
140   unsigned int i=0;
141   EST_warning("EST_JoinCostCache::computeAndCache");
142   for( EST_Litem *it=list.head(); it; it=it->next(), ++i ){
143     
144     unsigned int j=i+1;    
145     for( EST_Litem *jt=it->next(); jt; jt=jt->next(), ++j ){
146       float cost = jc( list(it), list(jt) );
147       
148       if( cost >= ulimit )
149         qcost = maxVal;
150       else if( cost <= llimit )
151         qcost = minVal;
152       else
153         qcost = static_cast<unsigned char>(rint(cost*(float)qleveln));
154
155       setval( i, j, qcost );
156     }
157
158     // yet to be convinced this is the best place for this...
159     list(it)->set( "jccid", (int)this->id() );
160     list(it)->set( "jccindex", (int) i );
161   }
162
163   return true;
164 }
165
166
167
168