static initializeService()

in src/core/awspack/AbstractTextToSpeechFeature.js [213:289]


  static initializeService(polly, presigner, version) {
    // Make sure all were defined
    if (
      polly === undefined ||
      presigner === undefined ||
      version === undefined
    ) {
      throw new Error(
        'Cannot initialize TextToSpeech feature. All arguments must be defined.'
      );
    }

    // Add sumerian hosts user-agent
    if (polly.config) {
      polly.config.customUserAgent = this._withCustomUserAgent(
        polly.config.customUserAgent
      );
    }
    if (presigner.service && presigner.service.config) {
      presigner.service.config.customUserAgent = this._withCustomUserAgent(
        presigner.service.config.customUserAgent
      );
    }

    this._isReady = false;

    // Store parameters
    this.SERVICES.polly = polly;
    this.SERVICES.presigner = presigner;
    awsVersion = version;

    // Clear the current polly objects
    const availableVoices = this.POLLY_VOICES;
    availableVoices.length = 0;

    const availableLanguages = this.POLLY_LANGUAGES;
    Object.keys(availableLanguages).forEach(name => {
      delete availableLanguages[name];
    });

    const availableLanguageCodes = this.POLLY_LANGUAGE_CODES;
    Object.keys(availableLanguageCodes).forEach(name => {
      delete availableLanguageCodes[name];
    });

    // Re-populate according to version
    const minNeuralSdk = this.POLLY_MIN_NEURAL_VERSION;

    return this.SERVICES.polly
      .describeVoices()
      .promise()
      .then(response => {
        const allCodes = {};

        response.Voices.forEach(voice => {
          if (
            voice.SupportedEngines.includes('standard') ||
            version >= minNeuralSdk
          ) {
            availableVoices.push(voice);
          }

          availableVoices.forEach(voice => {
            availableLanguages[voice.LanguageName] = voice.LanguageCode;
            allCodes[voice.LanguageCode] = voice.LanguageName;
          });
        });

        Object.entries(availableLanguages).forEach(([name, code]) => {
          availableLanguageCodes[code] = name;
        });

        // Notify that we're ready to generate speeches
        this._isReady = true;
        this.emit(this.EVENTS.ready);
      });
  }