Overview Package Class Source Class tree Glossary
previous class      next class frames      no frames

Jailbreak.JBSpeechManager


00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
// ============================================================================
// JBSpeechManager
// Copyright 2004 by Mychaeel <mychaeel@planetjailbreak.com>
// $Id: JBSpeechManager.uc,v 1.13 2008-05-01 12:36:37 wormbo Exp $
//
// Provides certain management functions for segmented speech output.
// ============================================================================


class JBSpeechManager extends JBSpeech
  config
  notplaceable;


// ============================================================================
// Types
// ============================================================================

struct TCacheSegment                          // caches sound segments
{
  var string Identifier;                      // sound segment identifier
  var array<TSegment> ListSegment;            // loaded sound segments
};


struct TCacheSetting                          // caches settings
{
  var string Section;                         // name of section to look in
  var string Setting;                         // name of setting to look for
  var string Value;                           // value of the setting
};


struct TInfoVoicePack                         // info on loaded voice pack
{
  var string Package;                         // sound package
  var string Group;                           // group within sound package

  var float Volume;                           // volume modifier for samples
  var float Pause;                            // default pause between segments

  var array<TCacheSegment> ListCacheSegment;  // cached sound segments
  var array<TCacheSetting> ListCacheSetting;  // cached settings
};


// ============================================================================
// Configuration
// ============================================================================

var protected config string VoicePack;        // package name of voice pack
var config bool bQueueAnnouncements;          // queue subsequent announcements


// ============================================================================
// Variables
// ============================================================================

var TInfoVoicePack InfoVoicePack;             // info on current voice pack
var array<TInfoVoicePack> ListInfoVoicePack;  // info on all loaded voice packs

var private array<JBSpeechClient> ListQueueSpeechClient;  // announcement queue

var bool            bUseFallbackVoicePack;
var JBDefaultVoice  FallbackVoicePack;

var transient name Announcement;              // used to cast string to name

var array<String> PrecacheCommonMacros;
var int iPrecacheCommonMacros;


// ============================================================================
// SpawnFor
//
// Finds or spawns and returns a JBSpeechManager object for the given level.
// ============================================================================

static function JBSpeechManager SpawnFor(LevelInfo Level)
{
  local JBSpeechManager thisSpeechManager;

  foreach Level.DynamicActors(Class'JBSpeechManager', thisSpeechManager)
    return thisSpeechManager;

  return Level.Spawn(Class'JBSpeechManager', Level.GetLocalPlayerController());
}


// ============================================================================
// PostBeginPlay
//
// Loads the default voice pack, if any.
// ============================================================================

simulated event PostBeginPlay()
{
  LoadVoicePack(VoicePack);
}


// ============================================================================
// GetVoicePack
//
// Returns the currently selected voice pack.
// ============================================================================

static function string GetVoicePack()
{
  return Default.VoicePack;
}


// ============================================================================
// SetVoicePack
//
// Sets the voice pack configuration option and makes all currently running
// speech managers switch to it. Input is not validated.
// ============================================================================

static function SetVoicePack(string VoicePackNew)
{
  local JBSpeechManager thisSpeechManager;

  foreach Default.Class.AllObjects(Class'JBSpeechManager', thisSpeechManager) {
    thisSpeechManager.VoicePack =   VoicePackNew;
    thisSpeechManager.LoadVoicePack(VoicePackNew);
  }

  Default.VoicePack = VoicePackNew;
  StaticSaveConfig();
}


// ============================================================================
// LoadVoicePack
//
// Loads the given voice pack. If the same voice pack was loaded once before
// already, takes it from the voice pack cache instead. Returns whether the
// voice pack was successfully loaded and activated.
// ============================================================================

simulated function bool LoadVoicePack(string VoicePackNew, optional bool bNoFallbackToDefault)
{
  local int iCharSeparator;
  local int iInfoVoicePack;

  if (InfoVoicePack.Package ~= VoicePackNew && (bUseFallbackVoicePack || VoicePackNew != ""))
    return True;

  for (iInfoVoicePack = 0; iInfoVoicePack < ListInfoVoicePack.Length; iInfoVoicePack++)
    if (ListInfoVoicePack[iInfoVoicePack].Package ~= VoicePackNew)
      break;

  if (iInfoVoicePack < ListInfoVoicePack.Length) {
    InfoVoicePack = ListInfoVoicePack[iInfoVoicePack];
  }
  else {
    if (IsVoicePackInstalled(VoicePackNew)) {
      iCharSeparator = InStr(VoicePackNew $ ".", ".");

      InfoVoicePack.Package = Left(VoicePackNew, iCharSeparator);
      InfoVoicePack.Group   = Mid (VoicePackNew, iCharSeparator + 1);
      InfoVoicePack.Volume  = float(GetSetting("Settings", "Volume", 1.0));
      InfoVoicePack.Pause   = float(GetSetting("Settings", "Pause"));
      InfoVoicePack.ListCacheSegment.Length = 0;

      //PrecacheSegments(InfoVoicePack);

      ListInfoVoicePack[iInfoVoicePack] = InfoVoicePack;
      bUseFallbackVoicePack = False;
    }
    else {
      if (bNoFallbackToDefault) {
        if (FallbackVoicePack == None) {
          Log("No voice pack available.", 'JBSpeech');
          bUseFallbackVoicePack = False;
          return False;
        }
        if (!bUseFallbackVoicePack) {
          Log("Using fallback voice pack.", 'JBSpeech');
          iCharSeparator = InStr(FallbackVoicePack.VoicePackage $ ".", ".");

          InfoVoicePack.Package = Left(FallbackVoicePack.VoicePackage, iCharSeparator);
          InfoVoicePack.Group   = Mid (FallbackVoicePack.VoicePackage, iCharSeparator + 1);
          InfoVoicePack.Volume  = FallbackVoicePack.Volume;
          InfoVoicePack.Pause   = FallbackVoicePack.Pause;
          InfoVoicePack.ListCacheSegment.Length = 0;
          bUseFallbackVoicePack = True;
        }
        return True;
      }
      return LoadVoicePack("JBVoiceGrrrl.Classic", True);
    }
  }

  return True;
}


// ============================================================================
// IsVoicePackInstalled
//
// Returns whether a voice pack with the given name is installed.
// ============================================================================

simulated function bool IsVoicePackInstalled(string VoicePackTest)
{
  local int iEntry;
  local string Entry;

  if (DynamicLoadObject("Jailbreak.JBVoice", Class'Class', False) == None)
    return False;

  for (iEntry = 0; True; iEntry++) {
    Entry = GetNextInt("Jailbreak.JBVoice", iEntry);
    if (Entry ~= "")            return False;
    if (Entry ~= VoicePackTest) return True;
  }
}


// ============================================================================
// PlayFor
//
// Plays the given sequence definition for the local player in the given level.
// Returns whether playing the sequence was successfully started.
// ============================================================================

static function bool PlayFor(LevelInfo Level, string Definition, optional string Tags)
{
  local JBSpeechManager SpeechManager;

  SpeechManager = Static.SpawnFor(Level);
  return SpeechManager.Play(Definition, Tags);
}


// ============================================================================
// Play
//
// Spawns a JBSpeechClient actor, parses the sequence definition and either
// queues it if another client is currently running or starts playing it.
// Returns whether playing the sequence was started or queued successfully.
// ============================================================================

simulated function bool Play(string Definition, optional string Tags)
{
  local JBSpeechClient SpeechClient;

  SpeechClient = Spawn(Class'JBSpeechClient', Self);

  if (SpeechClient.Parse(Definition, Tags)) {
    if (bQueueAnnouncements) {
      ListQueueSpeechClient[ListQueueSpeechClient.Length] = SpeechClient;
      if (ListQueueSpeechClient.Length > 1 || SpeechClient.Play())
        return True;
    }
    else {
      if (SpeechClient.Play())
        return True;
    }
  }

  SpeechClient.Destroy();
  return False;
}


// ============================================================================
// NotifyFinishedPlaying
//
// Called by a client when it finished playing. Starts playing the next queued
// client if any is present.
// ============================================================================

simulated function NotifyFinishedPlaying(JBSpeechClient SpeechClient)
{
  if (ListQueueSpeechClient.Length == 0 ||
      ListQueueSpeechClient[0] != SpeechClient)
    return;

  ListQueueSpeechClient.Remove(0, 1);
  if (ListQueueSpeechClient.Length > 0)
    ListQueueSpeechClient[0].Play();
}


// ============================================================================
// GetSetting
//
// Reads a setting from the voice pack's localization file from the given
// section; if a group within the voice pack is used, tries to load from the
// more specific section first before falling back to the generic.
// ============================================================================

simulated function string GetSetting(string Section, string Setting, optional coerce string ValueDefault)
{
  local int iCacheSetting;
  local string Value;

  for (iCacheSetting = 0; iCacheSetting < InfoVoicePack.ListCacheSetting.Length; iCacheSetting++)
    if (InfoVoicePack.ListCacheSetting[iCacheSetting].Section ~= Section &&
        InfoVoicePack.ListCacheSetting[iCacheSetting].Setting ~= Setting)
      return InfoVoicePack.ListCacheSetting[iCacheSetting].Value;

  if (bUseFallbackVoicePack) {
    Value = FallbackVoicePack.GetPropertyText(Setting);
  }
  else {
    if (InfoVoicePack.Group != "")
                     Value = Localize(Section $ "." $ InfoVoicePack.Group, Setting, InfoVoicePack.Package);
    if (Value == "") Value = Localize(Section,                             Setting, InfoVoicePack.Package);
  }
  if (Value == "") Value = ValueDefault;

  InfoVoicePack.ListCacheSetting.Insert(iCacheSetting, 1);
  InfoVoicePack.ListCacheSetting[iCacheSetting].Section = Section;
  InfoVoicePack.ListCacheSetting[iCacheSetting].Setting = Setting;
  InfoVoicePack.ListCacheSetting[iCacheSetting].Value   = Value;

  return Value;
}


// ============================================================================
// GetSegment
//
// Returns the segment corresponding to the given identifier. Uses the cache
// if possible, and caches the retrieved segments for future use.
// ============================================================================

simulated function TSegment GetSegment(string Identifier)
{
  local int iCacheSegment;
  local int iCharSeparatorAnnouncer;
  local int iSegment;
  local int iSegmentLoaded;
  local string Announcer;
  local string Package;
  local string Group;
  local string Suffix;
  local Sound SoundLoaded;
  local TCacheSegment CacheSegment;
  local TSegment SegmentAnnouncement;
  local TSegment SegmentNone;

  iCharSeparatorAnnouncer = InStr(Identifier, "/");

  if (iCharSeparatorAnnouncer >= 0) {
    Announcer = Left(Identifier, iCharSeparatorAnnouncer);
    SetPropertyText("Announcement", Mid(Identifier, iCharSeparatorAnnouncer + 1));

         if (Announcer ~= "Reward") SoundLoaded = Level.GetLocalPlayerController().RewardAnnouncer.GetSound(Announcement);
    else if (Announcer ~= "Status") SoundLoaded = Level.GetLocalPlayerController().StatusAnnouncer.GetSound(Announcement);

    if (SoundLoaded != None) {
      SegmentAnnouncement.Sound    =                  SoundLoaded;
      SegmentAnnouncement.Duration = GetSoundDuration(SoundLoaded);
      return SegmentAnnouncement;
    }
  }
  else {
    for (iCacheSegment = 0; iCacheSegment < InfoVoicePack.ListCacheSegment.Length; iCacheSegment++)
      if (InfoVoicePack.ListCacheSegment[iCacheSegment].Identifier ~= Identifier)
        break;

    if (iCacheSegment < InfoVoicePack.ListCacheSegment.Length) {
      CacheSegment = InfoVoicePack.ListCacheSegment[iCacheSegment];
    }
    else {
      CacheSegment.Identifier = Identifier;

      Package = InfoVoicePack.Package;
      Group   = InfoVoicePack.Group;

      for (iSegmentLoaded = 0; True; iSegmentLoaded++) {
        if (iSegmentLoaded == 0)
               Suffix = "";
          else Suffix = "_" $ iSegmentLoaded;

        SoundLoaded = None;
        if (InStr(Identifier, ".") >= 0)
                                   SoundLoaded = DynamicLoadSound(                              Identifier $ Suffix);
        else {
          if (Group != "")         SoundLoaded = DynamicLoadSound(Package $ "." $ Group $ "_" $ Identifier $ Suffix);
          if (SoundLoaded == None) SoundLoaded = DynamicLoadSound(Package $ "."               $ Identifier $ Suffix);
        }

        if (SoundLoaded == None)
          if (iSegmentLoaded == 0) continue;
                              else break;

        iSegment = CacheSegment.ListSegment.Length;
        CacheSegment.ListSegment.Insert(iSegment, 1);
        CacheSegment.ListSegment[iSegment].Sound    =                  SoundLoaded;
        CacheSegment.ListSegment[iSegment].Duration = GetSoundDuration(SoundLoaded);
      }

      InfoVoicePack.ListCacheSegment[iCacheSegment] = CacheSegment;
    }

    if (CacheSegment.ListSegment.Length > 0) {
      iSegment = Rand(CacheSegment.ListSegment.Length);
      return CacheSegment.ListSegment[iSegment];
    }
  }

  return SegmentNone;
}


// ============================================================================
// DynamicLoadSound
//
// Dynamically loads a sound object with the given fully qualified name and
// returns a reference to it. Returns None if the sound object was not found.
// ============================================================================

final static function Sound DynamicLoadSound(string Name)
{
  return Sound(DynamicLoadObject(Name, Class'Sound', True));
}


// ============================================================================
// Tick
//
// Precaches all segments, to prevent hitches during play.
// ============================================================================

simulated function Tick(float dt)
{
  local JBSpeechClient SpeechClient;

  if (Level.NetMode != NM_DedicatedServer && iPrecacheCommonMacros < PrecacheCommonMacros.Length) {
    if (iPrecacheCommonMacros == 0)
      log("Precaching the announcer - the localization log entries are normal", 'JBSpeech');
    
    SpeechClient = Spawn(Class'JBSpeechClient', Self);
    
    SpeechClient.Parse(PrecacheCommonMacros[iPrecacheCommonMacros++]);
  }
  else {
    Disable('Tick');
  }
}


// ============================================================================
// Destroyed
//
// Destroys all queued JBSpeechClient actors.
// ============================================================================

event Destroyed()
{
  while (ListQueueSpeechClient.Length > 0) {
    ListQueueSpeechClient[0].Destroy();
    ListQueueSpeechClient.Remove(0, 1);
  }
}


// ============================================================================
// Defaults
// ============================================================================

defaultproperties
{
  VoicePack = "JBVoiceGrrrl.Classic";
  bQueueAnnouncements = True;

  Begin Object Class=JBDefaultVoice Name=FallbackVoice
  End Object
  FallbackVoicePack = JBDefaultVoice'FallbackVoice';

  PrecacheCommonMacros(0)  = "$LastMan"
  PrecacheCommonMacros(1)  = "$LastSecondSave"
  PrecacheCommonMacros(2)  = "$TeamRed"
  PrecacheCommonMacros(3)  = "$TeamBlue"
  PrecacheCommonMacros(4)  = "$TeamCapturedRed"
  PrecacheCommonMacros(5)  = "$TeamCapturedBlue"
  PrecacheCommonMacros(6)  = "$TeamCapturedBoth"
  PrecacheCommonMacros(7)  = "$TeamReleasedRed"
  PrecacheCommonMacros(8)  = "$TeamReleasedBlue"
  PrecacheCommonMacros(9)  = "$ArenaWarning"
  PrecacheCommonMacros(10) = "$ArenaStart"
  PrecacheCommonMacros(11) = "$ArenaCancelled"
  PrecacheCommonMacros(12) = "$ArenaEndTimeout"
  PrecacheCommonMacros(13) = "$ArenaEndWinner"
  PrecacheCommonMacros(14) = "$ArenaEndLoser"
  PrecacheCommonMacros(15) = "$GameStart"
  PrecacheCommonMacros(16) = "$GameOvertime"
  PrecacheCommonMacros(17) = "$GameOverWinnerRed"
  PrecacheCommonMacros(18) = "$GameOverWinnerBlue"
  PrecacheCommonMacros(19) = "$AddonLlamaStart"
  PrecacheCommonMacros(20) = "$AddonLlamaDisconnect"
  PrecacheCommonMacros(21) = "$AddonLlamaFragged"
  PrecacheCommonMacros(22) = "$AddonVengeanceStart"
}

Overview Package Class Source Class tree Glossary
previous class      next class frames      no frames
Class file time: Do 1.5.2008 14:36:38.000 - Creation time: Do 14.8.2014 09:58:43.085 - Created with UnCodeX