1 {*************************************************************}
2 { TSsPlayTime Component - Estimates SakuraScript playing time }
4 { Copyright (c) 2001-2003 naruto/CANO-Lab }
5 {*************************************************************}
12 Windows, SysUtils, Classes, SsParser;
15 TSsPlayTimeException = class(Exception);
16 TSsPlayTimeInitException = class(TSsPlayTimeException);
18 TSsPlayTimeParams = class(TComponent)
24 FCostHiResWait: integer;
25 FCostSurface: integer;
26 FCostQuickChar: integer;
27 FCostWhiteSpace: integer;
28 FCostDBWhiteSpace: integer;
30 procedure SetCostChar(const Value: integer);
31 procedure SetCostConst(const Value: integer);
32 procedure SetCostDBChar(const Value: integer);
33 procedure SetCostWait(const Value: integer);
34 procedure SetCostHiResWait(const Value: integer);
35 procedure SetCostSurface(const Value: integer);
36 procedure SetCostQuickChar(const Value: integer);
37 procedure SetProfileName(const Value: String);
39 constructor Create(AOwner: TComponent); override;
40 procedure Assign(Source: TPersistent); override;
42 property ProfileName: String read FProfileName write SetProfileName;
43 property CostConst: integer read FCostConst write SetCostConst default 0;
44 property CostWait: integer read FCostWait write SetCostWait default 50;
45 property CostHiResWait: integer read FCostHiResWait write SetCostHiResWait default 1;
46 property CostSurface: integer read FCostSurface write SetCostSurface default 5;
47 property CostChar: integer read FCostChar write SetCostChar default 50;
48 property CostDBChar: integer read FCostDBChar write SetCostDBChar default 50;
49 property CostWhiteSpace: integer read FCostWhiteSpace write FCostWhiteSpace default 10;
50 property CostDBWhiteSpace: integer read FCostDBWhiteSpace write FCostDBWhiteSpace default 10;
51 property CostQuickChar: integer read FCostQuickChar write SetCostQuickChar default 0;
54 TSsPlayTimeCount = record
61 DBWhiteSpace : integer;
65 TSsPlayTime = class(TComponent)
67 FPlayTimeParams: TSsPlayTimeParams;
69 FCounts: TSsPlayTimeCount;
70 procedure SetPlayTimeParams(const Value: TSsPlayTimeParams);
71 procedure SetSsParser(const Value: TSsParser);
73 procedure CountElements;
74 procedure CountCharacterType(const Str: String; out SB, DB,
77 function PlayTime(const Script: String): integer;
78 property Counts: TSsPlayTimeCount read FCounts;
80 property PlayTimeParams: TSsPlayTimeParams read FPlayTimeParams write SetPlayTimeParams;
81 property SsParser: TSsParser read FSsParser write SetSsParser;
89 CDBWhiteSpace = #129 + #64; // Shift_JIS DB White Space;
93 RegisterComponents('Samples', [TSsPlayTime, TSsPlayTimeParams]);
98 procedure TSsPlayTimeParams.Assign(Source: TPersistent);
100 Src: TSsPlayTimeParams;
102 if not (Source is TSsPlayTimeParams) then
106 Src := Source as TSsPlayTimeParams;
107 FCostConst := Src.FCostConst;
108 FCostWait := Src.FCostWait;
109 FCostHiResWait := Src.FCostHiResWait;
110 FCostSurface := Src.FCostSurface;
111 FCostChar := Src.FCostChar;
112 FCostDBChar := Src.FCostDBChar;
113 FCostWhiteSpace := Src.FCostWhiteSpace;
114 FCostDBWhiteSpace := Src.FCostDBWhiteSpace;
115 FCostQuickChar := Src.FCostQuickChar;
116 FProfileName := Src.FProfileName;
120 constructor TSsPlayTimeParams.Create(AOwner: TComponent);
129 FCostWhiteSpace := 10;
130 FCostDBWhiteSpace := 10;
134 procedure TSsPlayTimeParams.SetCostChar(const Value: integer);
139 procedure TSsPlayTimeParams.SetCostConst(const Value: integer);
144 procedure TSsPlayTimeParams.SetCostDBChar(const Value: integer);
146 FCostDBChar := Value;
149 procedure TSsPlayTimeParams.SetCostHiResWait(const Value: integer);
151 FCostHiResWait := Value;
154 procedure TSsPlayTimeParams.SetCostQuickChar(const Value: integer);
156 FCostQuickChar := Value;
159 procedure TSsPlayTimeParams.SetCostSurface(const Value: integer);
161 FCostSurface := Value;
164 procedure TSsPlayTimeParams.SetCostWait(const Value: integer);
169 procedure TSsPlayTimeParams.SetProfileName(const Value: String);
171 FProfileName := Value;
176 procedure TSsPlayTime.CountCharacterType(const Str: String; out SB,
177 DB, SBS, DBS: integer);
187 for i := 1 to Length(Str) do
194 else if Str[i] in LeadBytes then
197 if Str[i] + Str[i+1] = CDBWhiteSpace then
203 else if Str[i] = ' ' then
210 procedure TSsPlayTime.CountElements;
212 i, SB, DB, SBS, DBS: integer;
216 ZeroMemory(@FCounts, sizeof(FCounts));
220 for i := 0 to SsParser.Count-1 do
223 case MarkUpType[i] of
227 InQuick := not InQuick
228 else if Match(Mark, '\w%d') = 3 then
229 FCounts.Wait := FCounts.Wait + Ord(Mark[3]) - Ord('0')
230 else if Match(Mark, '\_w[%D]') > 0 then
231 Inc(FCounts.HiResWait, StrToInt(GetParam(Mark, 0)))
232 else if Match(Mark, '\s%d') = 3 then
234 else if Match(Mark, '\s%b') > 0 then
241 CountCharacterType(Mark, SB, DB, SBS, DBS);
242 Inc(FCounts.QuickChar, SB + DB + SBS + DBS);
245 CountCharacterType(Mark, SB, DB, SBS, DBS);
246 Inc(FCounts.Char, SB);
247 Inc(FCounts.DBChar, DB);
248 Inc(FCounts.WhiteSpace, SBS);
249 Inc(FCounts.DBWhiteSpace, DBS);
252 // Ignore all tag errors
258 function TSsPlayTime.PlayTime(const Script: String): integer;
261 if FSsParser = nil then
262 raise TSsPlayTimeInitException.Create('SsParser is not set');
263 if FPlayTimeParams = nil then
264 raise TSsPlayTimeInitException.Create('PlayTimeParams is not set');
266 SsParser.InputString := Script;
268 with PlayTimeParams do
269 Result := CostConst + CostWait * FCounts.Wait + CostSurface * FCounts.Surface +
270 CostHiResWait * FCounts.HiResWait + CostChar * FCounts.Char +
271 CostDBChar * FCounts.DBChar +
272 CostWhiteSpace * FCounts.WhiteSpace + CostDBWhiteSpace * FCounts.DBWhiteSpace +
273 CostQuickChar * FCounts.QuickChar;
280 procedure TSsPlayTime.SetPlayTimeParams(const Value: TSsPlayTimeParams);
282 FPlayTimeParams := Value;
285 procedure TSsPlayTime.SetSsParser(const Value: TSsParser);