| 1 | # Copyright (C) 2011-2024 A S Lewis | ||
| 2 | # | ||
| 3 | # This program is free software: you can redistribute it and/or modify it under the terms of the GNU | ||
| 4 | # General Public License as published by the Free Software Foundation, either version 3 of the | ||
| 5 | # License, or (at your option) any later version. | ||
| 6 | # | ||
| 7 | # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without | ||
| 8 | # even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 9 | # General Public License for more details. | ||
| 10 | # | ||
| 11 | # You should have received a copy of the GNU General Public License along with this program. If not, | ||
| 12 | # see <http://www.gnu.org/licenses/>. | ||
| 13 | # | ||
| 14 | # | ||
| 15 | # Games::Axmud::Obj::Tts | ||
| 16 | # A TTS configuration object storing settings for text-to-speech (TTS) | ||
| 17 | # Games::Axmud::Obj::TtsJob | ||
| 18 | # An object storing parameters for a single piece of text, to be converted to speech | ||
| 19 | |||
| 20 | { package Games::Axmud::Obj::Tts; | ||
| 21 | |||
| 22 | use strict; | ||
| 23 | use warnings; | ||
| 24 | # use diagnostics; | ||
| 25 | |||
| 26 | use Glib qw(TRUE FALSE); | ||
| 27 | |||
| 28 | our @ISA = qw(Games::Axmud); | ||
| 29 | |||
| 30 | ################## | ||
| 31 | # Constructors | ||
| 32 | |||
| 33 | sub new { | ||
| 34 | |||
| 35 | # Creates a new instance of the TTS (text-to-speech) object | ||
| 36 | # | ||
| 37 | # Expected arguments | ||
| 38 | # $name - A unique string name for this TTS object (max 16 chars, containing | ||
| 39 | # A-Za-z0-9_ - 1st char can't be number, non-Latin alphabets acceptable. | ||
| 40 | # Reserved names in $axmud::CLIENT->constReservedHash allowed) | ||
| 41 | # $engine - TTS engine to use (must be one of the values in | ||
| 42 | # GA::CLIENT->constTTSList) | ||
| 43 | # | ||
| 44 | # Optional arguments | ||
| 45 | # $voice - Voice used with the TTS engine (eSpeak, espeak-ng, Flite, Festival server, | ||
| 46 | # Swift only; ignored when the engine is Festival command line). If an | ||
| 47 | # empty string or 'undef', the engine's default voice is used | ||
| 48 | # $speed - Word speed used with the TTS engine (eSpeak and espeak-ng only; ignored | ||
| 49 | # for other engines). A value in the range 0-100. If an invalid value or | ||
| 50 | # 'undef', the engine's default speed is used | ||
| 51 | # $rate - Word rate used with the TTS engine (Festival server and Swift only; | ||
| 52 | # ignored for other engines, including Festival command line). A value in | ||
| 53 | # the range 0-100. If an invalid value or 'undef', the engine's default | ||
| 54 | # rate is used | ||
| 55 | # $pitch - Word pitch used with the TTS engine (eSpeak and Swift only; ignored for | ||
| 56 | # other engines). A value in the range 0-100. If an invalid value or | ||
| 57 | # 'undef', the engine's default pitch is used | ||
| 58 | # $volume - Volume used with the TTS engine (Festival server and Swift only; ignored | ||
| 59 | # for other engines, including Festival command line). A value in the | ||
| 60 | # range 0-100. If an invalid value or 'undef', the engine's default pitch | ||
| 61 | # is used | ||
| 62 | # | ||
| 63 | # Return values | ||
| 64 | # 'undef' on improper arguments or if $name is invalid | ||
| 65 | # Blessed reference to the newly-created object on success | ||
| 66 | |||
| 67 | my ($class, $name, $engine, $voice, $speed, $rate, $pitch, $volume, $check) = @_; | ||
| 68 | |||
| 69 | # Local variables | ||
| 70 | my $ttsObj; | ||
| 71 | |||
| 72 | # Check for improper arguments | ||
| 73 | if (! defined $class || ! defined $name || ! defined $engine || defined $check) { | ||
| 74 | |||
| 75 | return $axmud::CLIENT->writeImproper($class . '->new', @_); | ||
| 76 | } | ||
| 77 | |||
| 78 | # Check that $name is valid and not already in use by another TTS object | ||
| 79 | if (! ($name =~ m/^[[:alpha:]\_]{1}[[:word:]]{0,15}$/)) { | ||
| 80 | |||
| 81 | return $axmud::CLIENT->writeError( | ||
| 82 | 'Registry naming error: invalid name \'' . $name . '\'', | ||
| 83 | $class . '->new', | ||
| 84 | ); | ||
| 85 | |||
| 86 | } elsif ($axmud::CLIENT->ivExists('ttsObjHash', $name)) { | ||
| 87 | |||
| 88 | return $axmud::CLIENT->writeError( | ||
| 89 | 'Registry naming error: there is already a TTS object called \'' . $name . '\'', | ||
| 90 | $class . '->new', | ||
| 91 | ); | ||
| 92 | } | ||
| 93 | |||
| 94 | # Check the engine is valid. The remaining arguments are checked by GA::Client->tts, which | ||
| 95 | # uses default voice/word speed/word pitch as required | ||
| 96 | if (! defined $axmud::CLIENT->ivFind('constTTSList', $engine)) { | ||
| 97 | |||
| 98 | return $axmud::CLIENT->writeError( | ||
| 99 | 'Unrecognised TTS engine \'' . $engine . '\'', | ||
| 100 | $class . '->new', | ||
| 101 | ); | ||
| 102 | } | ||
| 103 | |||
| 104 | # Find the TTS configuration object with the same name as the engine (which has default | ||
| 105 | # settings for voice, speed and pitch) | ||
| 106 | if ($name ne $engine) { | ||
| 107 | |||
| 108 | $ttsObj = $axmud::CLIENT->ivShow('ttsObjHash', $engine); | ||
| 109 | } | ||
| 110 | |||
| 111 | # (The first time Axmud runs, that object might not exist yet...) | ||
| 112 | if ($ttsObj) { | ||
| 113 | |||
| 114 | # Use the engine's default settings for the voice, speed, pitch, rate and volume, unless | ||
| 115 | # they were specified by the calling function's argument(s) | ||
| 116 | if (! defined $voice) { | ||
| 117 | |||
| 118 | $voice = $ttsObj->voice; | ||
| 119 | } | ||
| 120 | |||
| 121 | if (! defined $speed) { | ||
| 122 | |||
| 123 | $speed = $ttsObj->speed; | ||
| 124 | } | ||
| 125 | |||
| 126 | if (! defined $rate) { | ||
| 127 | |||
| 128 | $rate = $ttsObj->rate; | ||
| 129 | } | ||
| 130 | |||
| 131 | if (! defined $pitch) { | ||
| 132 | |||
| 133 | $pitch = $ttsObj->pitch; | ||
| 134 | } | ||
| 135 | |||
| 136 | if (! defined $volume) { | ||
| 137 | |||
| 138 | $volume = $ttsObj->volume; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | # Setup | ||
| 143 | my $self = { | ||
| 144 | _objName => $name, | ||
| 145 | _objClass => $class, | ||
| 146 | _parentFile => 'tts', | ||
| 147 | _parentWorld => undef, | ||
| 148 | _privFlag => FALSE, # All IVs are public | ||
| 149 | |||
| 150 | # IVs | ||
| 151 | # --- | ||
| 152 | |||
| 153 | # A unique name for the TTS object (max 16 chars) | ||
| 154 | name => $name, | ||
| 155 | # TTS engine to use (must be one of the values in GA::CLIENT->constTTSList) | ||
| 156 | engine => $engine, | ||
| 157 | # Voice used with the TTS engine (eSpeak, Flite, Festival server, Swift only; ignored | ||
| 158 | # when the engine is Festival command line). If an empty string or 'undef', the | ||
| 159 | # engine's default voice is used | ||
| 160 | voice => $voice, | ||
| 161 | # Word speed used with the TTS engine(eSpeak only; ignored for other engines). A value | ||
| 162 | # in the range 0-100. If an invalid value or 'undef', the engine's default speed is | ||
| 163 | # used | ||
| 164 | speed => $speed, | ||
| 165 | # Word rate used with the TTS engine (Festival server and Swift only; ignored for other | ||
| 166 | # engines, including Festival command line). A value in the range 0-100. If an invalid | ||
| 167 | # value or 'undef', the engine's default rate is used | ||
| 168 | rate => $rate, | ||
| 169 | # Word pitch used with the TTS engine (eSpeak and Swift only; ignored for other | ||
| 170 | # engines). A value in the range 0-100. If an invalid value or 'undef', the engine's | ||
| 171 | # default pitch is used | ||
| 172 | pitch => $pitch, | ||
| 173 | # Volume used with the TTS engine (Festival server and Swift only; ignored for other | ||
| 174 | # engines, including Festival command line). A value in the range 0-100. If an invalid | ||
| 175 | # value or 'undef', the engine's default pitch is used | ||
| 176 | volume => $volume, | ||
| 177 | |||
| 178 | # List of exclusive patterns. If set, only received lines matching these patterns will | ||
| 179 | # be read aloud by the TTS engine. Patterns are tested case-insensitively | ||
| 180 | # NB GA::Client->tts automatically splits its supplied string into lines, separated by | ||
| 181 | # a newline character, and tests each line against these patterns). | ||
| 182 | exclusiveList => [], | ||
| 183 | # List of excluded patterns. If set, received lines matching these patterns will NOT be | ||
| 184 | # sent to the TTS engine, but all other lines WILL be sent to the engine. Ignored if | ||
| 185 | # $self->exclusiveList is set. Patterns are tested case-insensitively | ||
| 186 | excludedList => [], | ||
| 187 | }; | ||
| 188 | |||
| 189 | # Bless the object into existence | ||
| 190 | bless $self, $class; | ||
| 191 | return $self; | ||
| 192 | } | ||
| 193 | |||
| 194 | sub clone { | ||
| 195 | |||
| 196 | # Called by GA::Cmd::CloneConfig->do | ||
| 197 | # Creates a clone of an existing TTS configuration object | ||
| 198 | # | ||
| 199 | # Expected arguments | ||
| 200 | # $name - A unique string name for this TTS object (max 16 chars, containing | ||
| 201 | # A-Za-z0-9_ - 1st char can't be number, non-Latin alphabets acceptable. | ||
| 202 | # Must not exist as a key in the global hash of reserved names, | ||
| 203 | # $axmud::CLIENT->constReservedHash) | ||
| 204 | # | ||
| 205 | # Return values | ||
| 206 | # 'undef' on improper arguments or if $name is invalid | ||
| 207 | # Blessed reference to the newly-created object on success | ||
| 208 | |||
| 209 | my ($self, $name, $check) = @_; | ||
| 210 | |||
| 211 | # Check for improper arguments | ||
| 212 | if (! defined $name || defined $check) { | ||
| 213 | |||
| 214 | return $axmud::CLIENT->writeImproper($self->_objClass . '->clone', @_); | ||
| 215 | } | ||
| 216 | |||
| 217 | # Check that $name is valid and not already in use by another TTS object | ||
| 218 | if (! $axmud::CLIENT->nameCheck($name, 16)) { | ||
| 219 | |||
| 220 | return $axmud::CLIENT->writeError( | ||
| 221 | 'Registry naming error: invalid name \'' . $name . '\'', | ||
| 222 | $self->_objClass . '->clone', | ||
| 223 | ); | ||
| 224 | |||
| 225 | } elsif ($axmud::CLIENT->ivExists('ttsObjHash', $name)) { | ||
| 226 | |||
| 227 | return $axmud::CLIENT->writeError( | ||
| 228 | 'Registry naming error: there is already a TTS object called \'' . $name . '\'', | ||
| 229 | $self->_objClass . '->clone', | ||
| 230 | ); | ||
| 231 | } | ||
| 232 | |||
| 233 | # Setup | ||
| 234 | my $clone = { | ||
| 235 | _objName => $name, | ||
| 236 | _objClass => $self->_objClass, | ||
| 237 | _parentFile => 'tts', | ||
| 238 | _parentWorld => undef, | ||
| 239 | _privFlag => FALSE, # All IVs are public | ||
| 240 | |||
| 241 | name => $name, | ||
| 242 | engine => $self->engine, | ||
| 243 | voice => $self->voice, | ||
| 244 | speed => $self->speed, | ||
| 245 | rate => $self->rate, | ||
| 246 | pitch => $self->pitch, | ||
| 247 | volume => $self->volume, | ||
| 248 | |||
| 249 | exclusiveList => [$self->exclusiveList], | ||
| 250 | excludedList => [$self->excludedList], | ||
| 251 | }; | ||
| 252 | |||
| 253 | # Bless the cloned object into existence | ||
| 254 | bless $clone, $self->_objClass; | ||
| 255 | return $clone; | ||
| 256 | } | ||
| 257 | |||
| 258 | ################## | ||
| 259 | # Methods | ||
| 260 | |||
| 261 | ################## | ||
| 262 | # Accessors - set | ||
| 263 | |||
| 264 | ################## | ||
| 265 | # Accessors - get | ||
| 266 | |||
| 267 | sub name | ||
| 268 | { $_[0]->{name} } | ||
| 269 | sub engine | ||
| 270 | { $_[0]->{engine} } | ||
| 271 | sub voice | ||
| 272 | { $_[0]->{voice} } | ||
| 273 | sub speed | ||
| 274 | { $_[0]->{speed} } | ||
| 275 | sub rate | ||
| 276 | { $_[0]->{rate} } | ||
| 277 | sub pitch | ||
| 278 | { $_[0]->{pitch} } | ||
| 279 | sub volume | ||
| 280 | { $_[0]->{volume} } | ||
| 281 | |||
| 282 | sub exclusiveList | ||
| 283 | { my $self = shift; return @{$self->{exclusiveList}}; } | ||
| 284 | sub excludedList | ||
| 285 | { my $self = shift; return @{$self->{excludedList}}; } | ||
| 286 | } | ||
| 287 | |||
| 288 | { package Games::Axmud::Obj::TtsJob; | ||
| 289 | |||
| 290 | use strict; | ||
| 291 | use warnings; | ||
| 292 | # use diagnostics; | ||
| 293 | |||
| 294 | use Glib qw(TRUE FALSE); | ||
| 295 | |||
| 296 | our @ISA = qw(Games::Axmud); | ||
| 297 | |||
| 298 | ################## | ||
| 299 | # Constructors | ||
| 300 | |||
| 301 | sub new { | ||
| 302 | |||
| 303 | # Called by GA::Client->tts (only) | ||
| 304 | # Creates a new instance of the TTS (text-to-speech) job object | ||
| 305 | # | ||
| 306 | # Expected arguments | ||
| 307 | # $text - The text to convert to speech (should not be an empty string) | ||
| 308 | # $engine - TTS engine to use (must be one of the values in | ||
| 309 | # GA::CLIENT->constTTSList) | ||
| 310 | # | ||
| 311 | # Optional arguments | ||
| 312 | # $voice - Voice used with the TTS engine (eSpeak, espeak-ng, Flite, Festival server, | ||
| 313 | # Swift only; ignored when the engine is Festival command line). If an | ||
| 314 | # empty string or 'undef', the engine's default voice is used | ||
| 315 | # $speed - Word speed used with the TTS engine (eSpeak and espeak-ng only; ignored | ||
| 316 | # for other engines). A value in the range 0-100. If an invalid value or | ||
| 317 | # 'undef', the engine's default speed is used | ||
| 318 | # $rate - Word rate used with the TTS engine (Festival server and Swift only; | ||
| 319 | # ignored for other engines, including Festival command line). A value in | ||
| 320 | # the range 0-100. If an invalid value or 'undef', the engine's default | ||
| 321 | # rate is used | ||
| 322 | # $pitch - Word pitch used with the TTS engine (eSpeak and Swift only; ignored for | ||
| 323 | # other engines). A value in the range 0-100. If an invalid value or | ||
| 324 | # 'undef', the engine's default pitch is used | ||
| 325 | # $volume - Volume used with the TTS engine (Festival server and Swift only; ignored | ||
| 326 | # for other engines, including Festival command line). A value in the | ||
| 327 | # range 0-100. If an invalid value or 'undef', the engine's default pitch | ||
| 328 | # is used | ||
| 329 | # $urgentFlag - Flag set to TRUE if this is a so-called 'urgent' job (performed before | ||
| 330 | # any other job), FALSE or 'undef' if not | ||
| 331 | # | ||
| 332 | # Return values | ||
| 333 | # 'undef' on improper arguments | ||
| 334 | # Blessed reference to the newly-created object on success | ||
| 335 | |||
| 336 | my ( | ||
| 337 | $class, $text, $engine, $voice, $speed, $rate, $pitch, $volume, $urgentFlag, $check | ||
| 338 | ) = @_; | ||
| 339 | |||
| 340 | # Local variables | ||
| 341 | my $ttsObj; | ||
| 342 | |||
| 343 | # Check for improper arguments | ||
| 344 | if (! defined $class || ! defined $text || ! defined $engine || defined $check) { | ||
| 345 | |||
| 346 | return $axmud::CLIENT->writeImproper($class . '->new', @_); | ||
| 347 | } | ||
| 348 | |||
| 349 | # Remove leading/trailing whitespace, and reduce any remaining whitespace to one character | ||
| 350 | # (this will save a lot of trouble later on) | ||
| 351 | $text = $axmud::CLIENT->trimWhitespace($text, TRUE); | ||
| 352 | # Use a TRUE or FALSE flag value, but not undef | ||
| 353 | if (! defined $urgentFlag) { | ||
| 354 | |||
| 355 | $urgentFlag = FALSE; | ||
| 356 | } | ||
| 357 | |||
| 358 | # Setup | ||
| 359 | my $self = { | ||
| 360 | _objName => 'tts_job', | ||
| 361 | _objClass => $class, | ||
| 362 | _parentFile => undef, # No parent file object | ||
| 363 | _parentWorld => undef, # No parent file object | ||
| 364 | _privFlag => FALSE, # All IVs are public | ||
| 365 | |||
| 366 | # IVs | ||
| 367 | # --- | ||
| 368 | |||
| 369 | # The whole text due to be converted to speech (should not be an empty string) | ||
| 370 | text => $text, | ||
| 371 | # The actual text to convert to speech. Depending on the value of | ||
| 372 | # GA::Client->ttsJobMode, this may be the whole of $self->text, or just a sentence | ||
| 373 | # in it, or just a word in it | ||
| 374 | # (An empty string is also possible; see the comments in $self->prepareText) | ||
| 375 | currentText => $text, | ||
| 376 | # Flag set to TRUE if this is a so-called 'urgent' job (performed before any other job), | ||
| 377 | # FALSE if not | ||
| 378 | urgentFlag => $urgentFlag, | ||
| 379 | # Flag set to FALSE as soon as any part of $self->text is converted to speech | ||
| 380 | newFlag => TRUE, | ||
| 381 | |||
| 382 | # TTS engine to use (must be one of the values in GA::CLIENT->constTTSList) | ||
| 383 | engine => $engine, | ||
| 384 | # Voice used with the TTS engine (eSpeak, Flite, Festival server, Swift only; ignored | ||
| 385 | # when the engine is Festival command line). If an empty string or 'undef', the | ||
| 386 | # engine's default voice is used | ||
| 387 | voice => $voice, | ||
| 388 | # Word speed used with the TTS engine(eSpeak only; ignored for other engines). A value | ||
| 389 | # in the range 0-100. If an invalid value or 'undef', the engine's default speed is | ||
| 390 | # used | ||
| 391 | speed => $speed, | ||
| 392 | # Word rate used with the TTS engine (Festival server and Swift only; ignored for other | ||
| 393 | # engines, including Festival command line). A value in the range 0-100. If an invalid | ||
| 394 | # value or 'undef', the engine's default rate is used | ||
| 395 | rate => $rate, | ||
| 396 | # Word pitch used with the TTS engine (eSpeak and Swift only; ignored for other | ||
| 397 | # engines). A value in the range 0-100. If an invalid value or 'undef', the engine's | ||
| 398 | # default pitch is used | ||
| 399 | pitch => $pitch, | ||
| 400 | # Volume used with the TTS engine (Festival server and Swift only; ignored for other | ||
| 401 | # engines, including Festival command line). A value in the range 0-100. If an invalid | ||
| 402 | # value or 'undef', the engine's default pitch is used | ||
| 403 | volume => $volume, | ||
| 404 | }; | ||
| 405 | |||
| 406 | # Bless the object into existence | ||
| 407 | bless $self, $class; | ||
| 408 | return $self; | ||
| 409 | } | ||
| 410 | |||
| 411 | ################## | ||
| 412 | # Methods | ||
| 413 | |||
| 414 | sub findNextSentence { | ||
| 415 | |||
| 416 | # Called by functions in GA::Client | ||
| 417 | # Given a pointer to a character in $self->text (the first character is zero), find the | ||
| 418 | # beginning of the next sentence in $self->text, if any | ||
| 419 | # | ||
| 420 | # Expected arguments | ||
| 421 | # $pointer - The position of a character in $self->text | ||
| 422 | # | ||
| 423 | # Return values | ||
| 424 | # 'undef' on improper arguments, if there is an error or if $pointer is already pointing | ||
| 425 | # at the last sentence | ||
| 426 | # Otherwise, returns the position of the beginning of the next sentence | ||
| 427 | |||
| 428 | my ($self, $pointer, $check) = @_; | ||
| 429 | |||
| 430 | # Local variables | ||
| 431 | my ( | ||
| 432 | $text, | ||
| 433 | @list, | ||
| 434 | ); | ||
| 435 | |||
| 436 | # Check for improper arguments | ||
| 437 | if (! defined $pointer || defined $check) { | ||
| 438 | |||
| 439 | return $axmud::CLIENT->writeImproper($self->_objClass . '->findNextSentence', @_); | ||
| 440 | } | ||
| 441 | |||
| 442 | # First check that $pointer actually points to a character in $self->text | ||
| 443 | if ($pointer < 0 || length($self->text) == 0 || $pointer >= length($self->text)) { | ||
| 444 | |||
| 445 | return undef; | ||
| 446 | } | ||
| 447 | |||
| 448 | # Remove everything before the pointer | ||
| 449 | $text = substr($self->text, $pointer); | ||
| 450 | |||
| 451 | # Split the remaining text into sentences. Set the pointer after the first one (if any) | ||
| 452 | @list = $text =~ m/[^\.\!\?]+[\.\!\?]*/g; | ||
| 453 | |||
| 454 | $pointer += length($list[0]); | ||
| 455 | shift @list; | ||
| 456 | |||
| 457 | # Discard sentences containing no alphanumeric characters | ||
| 458 | while (@list && ! ($list[0] =~ m/[[[[:alnum:]]/)) { | ||
| 459 | |||
| 460 | $pointer += length $list[0]; | ||
| 461 | shift @list; | ||
| 462 | } | ||
| 463 | |||
| 464 | if (@list) { | ||
| 465 | |||
| 466 | return $pointer; | ||
| 467 | |||
| 468 | } else { | ||
| 469 | |||
| 470 | # Only one sentence | ||
| 471 | return undef; | ||
| 472 | } | ||
| 473 | } | ||
| 474 | |||
| 475 | sub findPreviousSentence { | ||
| 476 | |||
| 477 | # Called by functions in GA::Client | ||
| 478 | # Given a pointer to a character in $self->text (the first character is zero), find the | ||
| 479 | # beginning of the previous sentence in $self->text, if any | ||
| 480 | # | ||
| 481 | # Expected arguments | ||
| 482 | # $pointer - The position of a character in $self->text | ||
| 483 | # | ||
| 484 | # Return values | ||
| 485 | # 'undef' on improper arguments, if there is an error or if $pointer is already pointing | ||
| 486 | # at the first sentence | ||
| 487 | # Otherwise, returns the position of the beginning of the previous sentence | ||
| 488 | |||
| 489 | my ($self, $pointer, $check) = @_; | ||
| 490 | |||
| 491 | # Local variables | ||
| 492 | my ( | ||
| 493 | $text, | ||
| 494 | @list, | ||
| 495 | ); | ||
| 496 | |||
| 497 | # Check for improper arguments | ||
| 498 | if (! defined $pointer || defined $check) { | ||
| 499 | |||
| 500 | return $axmud::CLIENT->writeImproper($self->_objClass . '->findPreviousSentence', @_); | ||
| 501 | } | ||
| 502 | |||
| 503 | # If the pointer is already at the first character (position 0), then there cannot be a | ||
| 504 | # previouis sentence | ||
| 505 | # Also check that $pointer actually points to a character in $self->text, or the | ||
| 506 | # hypothetical character immediately after it | ||
| 507 | if ($pointer <= 0 || length($self->text) == 0 || $pointer >= (length($self->text) + 1)) { | ||
| 508 | |||
| 509 | return undef; | ||
| 510 | } | ||
| 511 | |||
| 512 | # Remove everything from the pointer, onwards | ||
| 513 | $text = substr($self->text, 0, $pointer); | ||
| 514 | |||
| 515 | # Split the remaining text into sentences, and use the last (or only) one | ||
| 516 | @list = $text =~ m/[^\.\!\?]+[\.\!\?]*/g; | ||
| 517 | |||
| 518 | # Discard sentences containing no alphanumeric characters | ||
| 519 | while (@list && ! ($list[-1] =~ m/[[[[:alnum:]]/)) { | ||
| 520 | |||
| 521 | pop @list; | ||
| 522 | } | ||
| 523 | |||
| 524 | if (! @list) { | ||
| 525 | |||
| 526 | # Remaining $text contained no sentences | ||
| 527 | return undef; | ||
| 528 | |||
| 529 | } elsif (@list == 1) { | ||
| 530 | |||
| 531 | # Remaining $text contained only one sentence | ||
| 532 | return 0; | ||
| 533 | |||
| 534 | } else { | ||
| 535 | |||
| 536 | # Pointer should be just after the last unpopped sentence in @list | ||
| 537 | pop @list; | ||
| 538 | return length(join('', @list)); | ||
| 539 | } | ||
| 540 | } | ||
| 541 | |||
| 542 | sub findNextWord { | ||
| 543 | |||
| 544 | # Called by functions in GA::Client | ||
| 545 | # Given a pointer to a character in $self->text (the first character is zero), find the | ||
| 546 | # beginning of the next word in $self->text, if any | ||
| 547 | # | ||
| 548 | # Expected arguments | ||
| 549 | # $pointer - The position of a character in $self->text | ||
| 550 | # | ||
| 551 | # Return values | ||
| 552 | # 'undef' on improper arguments, if there is an error or if $pointer is already pointing | ||
| 553 | # at the last word | ||
| 554 | # Otherwise, returns the position of the beginning of the next word | ||
| 555 | |||
| 556 | my ($self, $pointer, $check) = @_; | ||
| 557 | |||
| 558 | # Local variables | ||
| 559 | my ( | ||
| 560 | $text, | ||
| 561 | @list, | ||
| 562 | ); | ||
| 563 | |||
| 564 | # Check for improper arguments | ||
| 565 | if (! defined $pointer || defined $check) { | ||
| 566 | |||
| 567 | return $axmud::CLIENT->writeImproper($self->_objClass . '->findNextWord', @_); | ||
| 568 | } | ||
| 569 | |||
| 570 | # First check that $pointer actually points to a character in $self->text | ||
| 571 | if ($pointer < 0 || length($self->text) == 0 || $pointer >= length($self->text)) { | ||
| 572 | |||
| 573 | return undef; | ||
| 574 | } | ||
| 575 | |||
| 576 | # Remove everything before the pointer | ||
| 577 | $text = substr($self->text, $pointer); | ||
| 578 | |||
| 579 | # Split the remaining text into words. Set the pointer after the first word (if any) | ||
| 580 | @list = $text =~ m/[^\s]+[\s]*/g; | ||
| 581 | |||
| 582 | $pointer += length($list[0]); | ||
| 583 | shift @list; | ||
| 584 | |||
| 585 | # Discard words containing no alphanumeric characters | ||
| 586 | while (@list && ! ($list[0] =~ m/[[[[:alnum:]]/)) { | ||
| 587 | |||
| 588 | $pointer += length $list[0]; | ||
| 589 | shift @list; | ||
| 590 | } | ||
| 591 | |||
| 592 | if (@list) { | ||
| 593 | |||
| 594 | return $pointer; | ||
| 595 | |||
| 596 | } else { | ||
| 597 | |||
| 598 | # No next word to return | ||
| 599 | return undef; | ||
| 600 | } | ||
| 601 | } | ||
| 602 | |||
| 603 | sub findPreviousWord { | ||
| 604 | |||
| 605 | # Called by functions in GA::Client | ||
| 606 | # Given a pointer to a character in $self->text (the first character is zero), find the | ||
| 607 | # beginning of the previous word in $self->text, if any | ||
| 608 | # | ||
| 609 | # Expected arguments | ||
| 610 | # $pointer - The position of a character in $self->text | ||
| 611 | # | ||
| 612 | # Return values | ||
| 613 | # 'undef' on improper arguments, if there is an error or if $pointer is already pointing | ||
| 614 | # at the first word | ||
| 615 | # Otherwise, returns the position of the beginning of the previous word | ||
| 616 | |||
| 617 | my ($self, $pointer, $check) = @_; | ||
| 618 | |||
| 619 | # Local variables | ||
| 620 | my ( | ||
| 621 | $text, | ||
| 622 | @list, | ||
| 623 | ); | ||
| 624 | |||
| 625 | # Check for improper arguments | ||
| 626 | if (! defined $pointer || defined $check) { | ||
| 627 | |||
| 628 | return $axmud::CLIENT->writeImproper($self->_objClass . '->findPreviousWord', @_); | ||
| 629 | } | ||
| 630 | |||
| 631 | # If the pointer is already at the first character (position 0), then there cannot be a | ||
| 632 | # previouis word | ||
| 633 | # Also check that $pointer actually points to a character in $self->text, or the | ||
| 634 | # hypothetical character immediately after it | ||
| 635 | if ($pointer <= 0 || length($self->text) == 0 || $pointer >= (length($self->text) + 1)) { | ||
| 636 | |||
| 637 | return undef; | ||
| 638 | } | ||
| 639 | |||
| 640 | # Remove everything at the pointer onwards | ||
| 641 | $text = substr($self->text, 0, $pointer); | ||
| 642 | |||
| 643 | # Split the remaining text into words, and use the last (or only) one | ||
| 644 | @list = $text =~ m/[^\s]+[\s]*/g; | ||
| 645 | |||
| 646 | # Discard words containing no alphanumeric characters | ||
| 647 | while (@list && ! ($list[-1] =~ m/[[[[:alnum:]]/)) { | ||
| 648 | |||
| 649 | pop @list; | ||
| 650 | } | ||
| 651 | |||
| 652 | if (! @list) { | ||
| 653 | |||
| 654 | # Remaining $text contained no words | ||
| 655 | return undef; | ||
| 656 | |||
| 657 | } elsif (@list == 1) { | ||
| 658 | |||
| 659 | # Remaining $text contained only one word | ||
| 660 | return 0; | ||
| 661 | |||
| 662 | } else { | ||
| 663 | |||
| 664 | # Pointer should be just after the last unpopped word in @list | ||
| 665 | pop @list; | ||
| 666 | return length(join('', @list)); | ||
| 667 | } | ||
| 668 | } | ||
| 669 | |||
| 670 | sub prepareText { | ||
| 671 | |||
| 672 | # Called by GA::Client->ttsPerformJobs | ||
| 673 | # Sets the value of $self->currentText, which might be the whole of $self->text, or just | ||
| 674 | # one sentence, or just one word | ||
| 675 | # | ||
| 676 | # Expected arguments | ||
| 677 | # (none besides $self) | ||
| 678 | # | ||
| 679 | # Return values | ||
| 680 | # 'undef' on improper arguments | ||
| 681 | # 1 otherwise | ||
| 682 | |||
| 683 | my ($self, $check) = @_; | ||
| 684 | |||
| 685 | # Local variables | ||
| 686 | my ( | ||
| 687 | $text, | ||
| 688 | @list, | ||
| 689 | ); | ||
| 690 | |||
| 691 | # Check for improper arguments | ||
| 692 | if (defined $check) { | ||
| 693 | |||
| 694 | return $axmud::CLIENT->writeImproper($self->_objClass . '->prepareText', @_); | ||
| 695 | } | ||
| 696 | |||
| 697 | if ($axmud::CLIENT->ttsJobMode eq 'default' || $axmud::CLIENT->ttsJobAutoFlag) { | ||
| 698 | |||
| 699 | $self->ivPoke('currentText', $self->text); | ||
| 700 | |||
| 701 | } else { | ||
| 702 | |||
| 703 | # GA::Client->ttsJobMiniPointer points at a position in $self->text. Use the whole | ||
| 704 | # sentence/word after (and including) that character | ||
| 705 | |||
| 706 | # Remove everything before the pointer | ||
| 707 | $text = $self->text; | ||
| 708 | if ($axmud::CLIENT->ttsJobMiniPointer > 0) { | ||
| 709 | |||
| 710 | $text = substr($text, $axmud::CLIENT->ttsJobMiniPointer); | ||
| 711 | } | ||
| 712 | |||
| 713 | if ($axmud::CLIENT->ttsJobMode eq 'sentence') { | ||
| 714 | |||
| 715 | # Split the remaining text into sentences, and use the first sentence | ||
| 716 | @list = $text =~ m/[^\.\!\?]+[\.\!\?]*/g; | ||
| 717 | |||
| 718 | } else { | ||
| 719 | |||
| 720 | # Split the remaining text into words, and use the first word | ||
| 721 | @list = $text =~ m/[^\s]+[\s]*/g; | ||
| 722 | } | ||
| 723 | |||
| 724 | # Discard words/sentences containing no alphanumeric characters | ||
| 725 | while (@list && ! ($list[0] =~ m/[[[[:alnum:]]/)) { | ||
| 726 | |||
| 727 | shift @list; | ||
| 728 | } | ||
| 729 | |||
| 730 | if (! @list) { | ||
| 731 | |||
| 732 | # No alphanumeric text found. Set the IV to an empty string; the calling code | ||
| 733 | # checks for that | ||
| 734 | $self->ivPoke('currentText', ''); | ||
| 735 | |||
| 736 | } else { | ||
| 737 | |||
| 738 | $self->ivPoke( | ||
| 739 | 'currentText', | ||
| 740 | $axmud::CLIENT->trimWhitespace($list[0]), | ||
| 741 | ); | ||
| 742 | } | ||
| 743 | } | ||
| 744 | |||
| 745 | return 1; | ||
| 746 | } | ||
| 747 | |||
| 748 | ################## | ||
| 749 | # Accessors - set | ||
| 750 | |||
| 751 | ################## | ||
| 752 | # Accessors - get | ||
| 753 | |||
| 754 | sub text | ||
| 755 | { $_[0]->{text} } | ||
| 756 | sub currentText | ||
| 757 | { $_[0]->{currentText} } | ||
| 758 | sub urgentFlag | ||
| 759 | { $_[0]->{urgentFlag} } | ||
| 760 | sub newFlag | ||
| 761 | { $_[0]->{newFlag} } | ||
| 762 | |||
| 763 | sub engine | ||
| 764 | { $_[0]->{engine} } | ||
| 765 | sub voice | ||
| 766 | { $_[0]->{voice} } | ||
| 767 | sub speed | ||
| 768 | { $_[0]->{speed} } | ||
| 769 | sub rate | ||
| 770 | { $_[0]->{rate} } | ||
| 771 | sub pitch | ||
| 772 | { $_[0]->{pitch} } | ||
| 773 | sub volume | ||
| 774 | { $_[0]->{volume} } | ||
| 775 | } | ||
| 776 | |||
| 777 | # Package must return a true value | ||
| 778 | 1 |