summaryrefslogtreecommitdiffstats
path: root/dom/media/webspeech/synth/pico/nsPicoService.cpp
blob: c3cf812fcc45c2e5654e318e1a2e1d54f4711679 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsISupports.h"
#include "nsPicoService.h"
#include "nsPrintfCString.h"
#include "nsIWeakReferenceUtils.h"
#include "SharedBuffer.h"
#include "nsISimpleEnumerator.h"

#include "mozilla/dom/nsSynthVoiceRegistry.h"
#include "mozilla/dom/nsSpeechTask.h"

#include "nsIFile.h"
#include "nsThreadUtils.h"
#include "prenv.h"
#include "mozilla/Preferences.h"
#include "mozilla/DebugOnly.h"
#include <dlfcn.h>

// Pico API constants

// Size of memory allocated for pico engine and voice resources.
// We only have one voice and its resources loaded at once, so this
// should always be enough.
#define PICO_MEM_SIZE 2500000

// Max length of returned strings. Pico will never return longer strings,
// so this amount should be good enough for preallocating.
#define PICO_RETSTRINGSIZE 200

// Max amount we want from a single call of pico_getData
#define PICO_MAX_CHUNK_SIZE 128

// Arbitrary name for loaded voice, it doesn't mean anything outside of Pico
#define PICO_VOICE_NAME "pico"

// Return status from pico_getData meaning there is more data in the pipeline
// to get from more calls to pico_getData
#define PICO_STEP_BUSY 201

// For performing a "soft" reset between utterances. This is used when one
// utterance is interrupted by a new one.
#define PICO_RESET_SOFT 0x10

// Currently, Pico only provides mono output.
#define PICO_CHANNELS_NUM 1

// Pico's sample rate is always 16000
#define PICO_SAMPLE_RATE 16000

// The path to the language files in Gonk
#define GONK_PICO_LANG_PATH "/system/tts/lang_pico"

namespace mozilla {
namespace dom {

StaticRefPtr<nsPicoService> nsPicoService::sSingleton;

class PicoApi
{
public:

  PicoApi() : mInitialized(false) {}

  bool Init()
  {
    if (mInitialized) {
      return true;
    }

    void* handle = dlopen("libttspico.so", RTLD_LAZY);

    if (!handle) {
      NS_WARNING("Failed to open libttspico.so, pico cannot run");
      return false;
    }

    pico_initialize =
      (pico_Status (*)(void*, uint32_t, pico_System*))dlsym(
        handle, "pico_initialize");

    pico_terminate =
      (pico_Status (*)(pico_System*))dlsym(handle, "pico_terminate");

    pico_getSystemStatusMessage =
      (pico_Status (*)(pico_System, pico_Status, pico_Retstring))dlsym(
        handle, "pico_getSystemStatusMessage");;

    pico_loadResource =
      (pico_Status (*)(pico_System, const char*, pico_Resource*))dlsym(
        handle, "pico_loadResource");

    pico_unloadResource =
      (pico_Status (*)(pico_System, pico_Resource*))dlsym(
        handle, "pico_unloadResource");

    pico_getResourceName =
      (pico_Status (*)(pico_System, pico_Resource, pico_Retstring))dlsym(
        handle, "pico_getResourceName");

    pico_createVoiceDefinition =
      (pico_Status (*)(pico_System, const char*))dlsym(
        handle, "pico_createVoiceDefinition");

    pico_addResourceToVoiceDefinition =
      (pico_Status (*)(pico_System, const char*, const char*))dlsym(
        handle, "pico_addResourceToVoiceDefinition");

    pico_releaseVoiceDefinition =
      (pico_Status (*)(pico_System, const char*))dlsym(
        handle, "pico_releaseVoiceDefinition");

    pico_newEngine =
      (pico_Status (*)(pico_System, const char*, pico_Engine*))dlsym(
        handle, "pico_newEngine");

    pico_disposeEngine =
      (pico_Status (*)(pico_System, pico_Engine*))dlsym(
        handle, "pico_disposeEngine");

    pico_resetEngine =
      (pico_Status (*)(pico_Engine, int32_t))dlsym(handle, "pico_resetEngine");

    pico_putTextUtf8 =
      (pico_Status (*)(pico_Engine, const char*, const int16_t, int16_t*))dlsym(
        handle, "pico_putTextUtf8");

    pico_getData =
      (pico_Status (*)(pico_Engine, void*, int16_t, int16_t*, int16_t*))dlsym(
        handle, "pico_getData");

    mInitialized = true;
    return true;
  }

  typedef signed int pico_Status;
  typedef char pico_Retstring[PICO_RETSTRINGSIZE];

  pico_Status (* pico_initialize)(void*, uint32_t, pico_System*);
  pico_Status (* pico_terminate)(pico_System*);
  pico_Status (* pico_getSystemStatusMessage)(
    pico_System, pico_Status, pico_Retstring);

  pico_Status (* pico_loadResource)(pico_System, const char*, pico_Resource*);
  pico_Status (* pico_unloadResource)(pico_System, pico_Resource*);
  pico_Status (* pico_getResourceName)(
    pico_System, pico_Resource, pico_Retstring);
  pico_Status (* pico_createVoiceDefinition)(pico_System, const char*);
  pico_Status (* pico_addResourceToVoiceDefinition)(
    pico_System, const char*, const char*);
  pico_Status (* pico_releaseVoiceDefinition)(pico_System, const char*);
  pico_Status (* pico_newEngine)(pico_System, const char*, pico_Engine*);
  pico_Status (* pico_disposeEngine)(pico_System, pico_Engine*);

  pico_Status (* pico_resetEngine)(pico_Engine, int32_t);
  pico_Status (* pico_putTextUtf8)(
    pico_Engine, const char*, const int16_t, int16_t*);
  pico_Status (* pico_getData)(
    pico_Engine, void*, const int16_t, int16_t*, int16_t*);

private:

  bool mInitialized;

} sPicoApi;

#define PICO_ENSURE_SUCCESS_VOID(_funcName, _status)                      \
  if (_status < 0) {                                                      \
    PicoApi::pico_Retstring message;                                      \
    sPicoApi.pico_getSystemStatusMessage(                                 \
      nsPicoService::sSingleton->mPicoSystem, _status, message);          \
    NS_WARNING(                                                           \
      nsPrintfCString("Error running %s: %s", _funcName, message).get()); \
    return;                                                               \
  }

#define PICO_ENSURE_SUCCESS(_funcName, _status, _rv)                      \
  if (_status < 0) {                                                      \
    PicoApi::pico_Retstring message;                                      \
    sPicoApi.pico_getSystemStatusMessage(                                 \
      nsPicoService::sSingleton->mPicoSystem, _status, message);          \
    NS_WARNING(                                                           \
      nsPrintfCString("Error running %s: %s", _funcName, message).get()); \
    return _rv;                                                           \
  }

class PicoVoice
{
public:

  PicoVoice(const nsAString& aLanguage)
    : mLanguage(aLanguage) {}

  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(PicoVoice)

  // Voice language, in BCB-47 syntax
  nsString mLanguage;

  // Language resource file
  nsCString mTaFile;

  // Speaker resource file
  nsCString mSgFile;

private:
    ~PicoVoice() {}
};

class PicoCallbackRunnable : public Runnable,
                             public nsISpeechTaskCallback
{
  friend class PicoSynthDataRunnable;

public:
  PicoCallbackRunnable(const nsAString& aText, PicoVoice* aVoice,
                       float aRate, float aPitch, nsISpeechTask* aTask,
                       nsPicoService* aService)
    : mText(NS_ConvertUTF16toUTF8(aText))
    , mRate(aRate)
    , mPitch(aPitch)
    , mFirstData(true)
    , mTask(aTask)
    , mVoice(aVoice)
    , mService(aService) { }

  NS_DECL_ISUPPORTS_INHERITED
  NS_DECL_NSISPEECHTASKCALLBACK

  NS_IMETHOD Run() override;

  bool IsCurrentTask() { return mService->mCurrentTask == mTask; }

private:
  ~PicoCallbackRunnable() { }

  void DispatchSynthDataRunnable(already_AddRefed<SharedBuffer>&& aBuffer,
                                 size_t aBufferSize);

  nsCString mText;

  float mRate;

  float mPitch;

  bool mFirstData;

  // We use this pointer to compare it with the current service task.
  // If they differ, this runnable should stop.
  nsISpeechTask* mTask;

  // We hold a strong reference to the service, which in turn holds
  // a strong reference to this voice.
  PicoVoice* mVoice;

  // By holding a strong reference to the service we guarantee that it won't be
  // destroyed before this runnable.
  RefPtr<nsPicoService> mService;
};

NS_IMPL_ISUPPORTS_INHERITED(PicoCallbackRunnable, Runnable, nsISpeechTaskCallback)

// Runnable

NS_IMETHODIMP
PicoCallbackRunnable::Run()
{
  MOZ_ASSERT(!NS_IsMainThread());
  PicoApi::pico_Status status = 0;

  if (mService->CurrentVoice() != mVoice) {
    mService->LoadEngine(mVoice);
  } else {
    status = sPicoApi.pico_resetEngine(mService->mPicoEngine, PICO_RESET_SOFT);
    PICO_ENSURE_SUCCESS("pico_unloadResource", status, NS_ERROR_FAILURE);
  }

  // Add SSML markup for pitch and rate. Pico uses a minimal parser,
  // so no namespace is needed.
  nsPrintfCString markedUpText(
    "<pitch level=\"%0.0f\"><speed level=\"%0.0f\">%s</speed></pitch>",
    std::min(std::max(50.0f, mPitch * 100), 200.0f),
    std::min(std::max(20.0f, mRate * 100), 500.0f),
    mText.get());

  const char* text = markedUpText.get();
  size_t buffer_size = 512, buffer_offset = 0;
  RefPtr<SharedBuffer> buffer = SharedBuffer::Create(buffer_size);
  int16_t text_offset = 0, bytes_recv = 0, bytes_sent = 0, out_data_type = 0;
  int16_t text_remaining = markedUpText.Length() + 1;

  // Run this loop while this is the current task
  while (IsCurrentTask()) {
    if (text_remaining) {
      status = sPicoApi.pico_putTextUtf8(mService->mPicoEngine,
                                         text + text_offset, text_remaining,
                                         &bytes_sent);
      PICO_ENSURE_SUCCESS("pico_putTextUtf8", status, NS_ERROR_FAILURE);
      // XXX: End speech task on error
      text_remaining -= bytes_sent;
      text_offset += bytes_sent;
    } else {
      // If we already fed all the text to the engine, send a zero length buffer
      // and quit.
      DispatchSynthDataRunnable(already_AddRefed<SharedBuffer>(), 0);
      break;
    }

    do {
      // Run this loop while the result of getData is STEP_BUSY, when it finishes
      // synthesizing audio for the given text, it returns STEP_IDLE. We then
      // break to the outer loop and feed more text, if there is any left.
      if (!IsCurrentTask()) {
        // If the task has changed, quit.
        break;
      }

      if (buffer_size - buffer_offset < PICO_MAX_CHUNK_SIZE) {
        // The next audio chunk retrieved may be bigger than our buffer,
        // so send the data and flush the buffer.
        DispatchSynthDataRunnable(buffer.forget(), buffer_offset);
        buffer_offset = 0;
        buffer = SharedBuffer::Create(buffer_size);
      }

      status = sPicoApi.pico_getData(mService->mPicoEngine,
                                     (uint8_t*)buffer->Data() + buffer_offset,
                                     PICO_MAX_CHUNK_SIZE,
                                     &bytes_recv, &out_data_type);
      PICO_ENSURE_SUCCESS("pico_getData", status, NS_ERROR_FAILURE);
      buffer_offset += bytes_recv;
    } while (status == PICO_STEP_BUSY);
  }

  return NS_OK;
}

void
PicoCallbackRunnable::DispatchSynthDataRunnable(
  already_AddRefed<SharedBuffer>&& aBuffer, size_t aBufferSize)
{
  class PicoSynthDataRunnable final : public Runnable
  {
  public:
    PicoSynthDataRunnable(already_AddRefed<SharedBuffer>& aBuffer,
                          size_t aBufferSize, bool aFirstData,
                          PicoCallbackRunnable* aCallback)
      : mBuffer(aBuffer)
      , mBufferSize(aBufferSize)
      , mFirstData(aFirstData)
      , mCallback(aCallback) {
    }

    NS_IMETHOD Run() override
    {
      MOZ_ASSERT(NS_IsMainThread());

      if (!mCallback->IsCurrentTask()) {
        return NS_ERROR_NOT_AVAILABLE;
      }

      nsISpeechTask* task = mCallback->mTask;

      if (mFirstData) {
        task->Setup(mCallback, PICO_CHANNELS_NUM, PICO_SAMPLE_RATE, 2);
      }

      return task->SendAudioNative(
        mBufferSize ? static_cast<short*>(mBuffer->Data()) : nullptr, mBufferSize / 2);
    }

  private:
    RefPtr<SharedBuffer> mBuffer;

    size_t mBufferSize;

    bool mFirstData;

    RefPtr<PicoCallbackRunnable> mCallback;
  };

  nsCOMPtr<nsIRunnable> sendEvent =
    new PicoSynthDataRunnable(aBuffer, aBufferSize, mFirstData, this);
  NS_DispatchToMainThread(sendEvent);
  mFirstData = false;
}

// nsISpeechTaskCallback

NS_IMETHODIMP
PicoCallbackRunnable::OnPause()
{
  return NS_OK;
}

NS_IMETHODIMP
PicoCallbackRunnable::OnResume()
{
  return NS_OK;
}

NS_IMETHODIMP
PicoCallbackRunnable::OnCancel()
{
  mService->mCurrentTask = nullptr;
  return NS_OK;
}

NS_IMETHODIMP
PicoCallbackRunnable::OnVolumeChanged(float aVolume)
{
  return NS_OK;
}

NS_INTERFACE_MAP_BEGIN(nsPicoService)
  NS_INTERFACE_MAP_ENTRY(nsISpeechService)
  NS_INTERFACE_MAP_ENTRY(nsIObserver)
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIObserver)
NS_INTERFACE_MAP_END

NS_IMPL_ADDREF(nsPicoService)
NS_IMPL_RELEASE(nsPicoService)

nsPicoService::nsPicoService()
  : mInitialized(false)
  , mVoicesMonitor("nsPicoService::mVoices")
  , mCurrentTask(nullptr)
  , mPicoSystem(nullptr)
  , mPicoEngine(nullptr)
  , mSgResource(nullptr)
  , mTaResource(nullptr)
  , mPicoMemArea(nullptr)
{
}

nsPicoService::~nsPicoService()
{
  // We don't worry about removing the voices because this gets
  // destructed at shutdown along with the voice registry.
  MonitorAutoLock autoLock(mVoicesMonitor);
  mVoices.Clear();

  if (mThread) {
    mThread->Shutdown();
  }

  UnloadEngine();
}

// nsIObserver

NS_IMETHODIMP
nsPicoService::Observe(nsISupports* aSubject, const char* aTopic,
                       const char16_t* aData)
{
  MOZ_ASSERT(NS_IsMainThread());
  if(NS_WARN_IF(!(!strcmp(aTopic, "profile-after-change")))) {
    return NS_ERROR_UNEXPECTED;
  }

  if (!Preferences::GetBool("media.webspeech.synth.enabled") ||
      Preferences::GetBool("media.webspeech.synth.test")) {
    return NS_OK;
  }

  DebugOnly<nsresult> rv = NS_NewNamedThread("Pico Worker", getter_AddRefs(mThread));
  MOZ_ASSERT(NS_SUCCEEDED(rv));
  return mThread->Dispatch(
    NewRunnableMethod(this, &nsPicoService::Init), NS_DISPATCH_NORMAL);
}
// nsISpeechService

NS_IMETHODIMP
nsPicoService::Speak(const nsAString& aText, const nsAString& aUri,
                     float aVolume, float aRate, float aPitch,
                     nsISpeechTask* aTask)
{
  if(NS_WARN_IF(!(mInitialized))) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  MonitorAutoLock autoLock(mVoicesMonitor);
  bool found = false;
  PicoVoice* voice = mVoices.GetWeak(aUri, &found);
  if(NS_WARN_IF(!(found))) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  mCurrentTask = aTask;
  RefPtr<PicoCallbackRunnable> cb = new PicoCallbackRunnable(aText, voice, aRate, aPitch, aTask, this);
  return mThread->Dispatch(cb, NS_DISPATCH_NORMAL);
}

NS_IMETHODIMP
nsPicoService::GetServiceType(SpeechServiceType* aServiceType)
{
  *aServiceType = nsISpeechService::SERVICETYPE_DIRECT_AUDIO;
  return NS_OK;
}

// private methods

void
nsPicoService::Init()
{
  MOZ_ASSERT(!NS_IsMainThread());
  MOZ_ASSERT(!mInitialized);

  if (!sPicoApi.Init()) {
    NS_WARNING("Failed to initialize pico library");
    return;
  }

  // Use environment variable, or default android/b2g path
  nsAutoCString langPath(PR_GetEnv("PICO_LANG_PATH"));

  if (langPath.IsEmpty()) {
    langPath.AssignLiteral(GONK_PICO_LANG_PATH);
  }

  nsCOMPtr<nsIFile> voicesDir;
  NS_NewNativeLocalFile(langPath, true, getter_AddRefs(voicesDir));

  nsCOMPtr<nsISimpleEnumerator> dirIterator;
  nsresult rv = voicesDir->GetDirectoryEntries(getter_AddRefs(dirIterator));

  if (NS_FAILED(rv)) {
    NS_WARNING(nsPrintfCString("Failed to get contents of directory: %s", langPath.get()).get());
    return;
  }

  bool hasMoreElements = false;
  rv = dirIterator->HasMoreElements(&hasMoreElements);
  MOZ_ASSERT(NS_SUCCEEDED(rv));

  MonitorAutoLock autoLock(mVoicesMonitor);

  while (hasMoreElements && NS_SUCCEEDED(rv)) {
    nsCOMPtr<nsISupports> supports;
    rv = dirIterator->GetNext(getter_AddRefs(supports));
    MOZ_ASSERT(NS_SUCCEEDED(rv));

    nsCOMPtr<nsIFile> voiceFile = do_QueryInterface(supports);
    MOZ_ASSERT(voiceFile);

    nsAutoCString leafName;
    voiceFile->GetNativeLeafName(leafName);

    nsAutoString lang;

    if (GetVoiceFileLanguage(leafName, lang)) {
      nsAutoString uri;
      uri.AssignLiteral("urn:moz-tts:pico:");
      uri.Append(lang);

      bool found = false;
      PicoVoice* voice = mVoices.GetWeak(uri, &found);

      if (!found) {
        voice = new PicoVoice(lang);
        mVoices.Put(uri, voice);
      }

      // Each voice consists of two lingware files: A language resource file,
      // suffixed by _ta.bin, and a speaker resource file, suffixed by _sb.bin.
      // We currently assume that there is a pair of files for each language.
      if (StringEndsWith(leafName, NS_LITERAL_CSTRING("_ta.bin"))) {
        rv = voiceFile->GetPersistentDescriptor(voice->mTaFile);
        MOZ_ASSERT(NS_SUCCEEDED(rv));
      } else if (StringEndsWith(leafName, NS_LITERAL_CSTRING("_sg.bin"))) {
        rv = voiceFile->GetPersistentDescriptor(voice->mSgFile);
        MOZ_ASSERT(NS_SUCCEEDED(rv));
      }
    }

    rv = dirIterator->HasMoreElements(&hasMoreElements);
  }

  NS_DispatchToMainThread(NewRunnableMethod(this, &nsPicoService::RegisterVoices));
}

void
nsPicoService::RegisterVoices()
{
  nsSynthVoiceRegistry* registry = nsSynthVoiceRegistry::GetInstance();

  for (auto iter = mVoices.Iter(); !iter.Done(); iter.Next()) {
    const nsAString& uri = iter.Key();
    RefPtr<PicoVoice>& voice = iter.Data();

    // If we are missing either a language or a voice resource, it is invalid.
    if (voice->mTaFile.IsEmpty() || voice->mSgFile.IsEmpty()) {
      iter.Remove();
      continue;
    }

    nsAutoString name;
    name.AssignLiteral("Pico ");
    name.Append(voice->mLanguage);

    // This service is multi-threaded and can handle more than one utterance at a
    // time before previous utterances end. So, aQueuesUtterances == false
    DebugOnly<nsresult> rv =
      registry->AddVoice(this, uri, name, voice->mLanguage, true, false);
    NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to add voice");
  }

  mInitialized = true;
}

bool
nsPicoService::GetVoiceFileLanguage(const nsACString& aFileName, nsAString& aLang)
{
  nsACString::const_iterator start, end;
  aFileName.BeginReading(start);
  aFileName.EndReading(end);

  // The lingware filename syntax is language_(ta/sg).bin,
  // we extract the language prefix here.
  if (FindInReadable(NS_LITERAL_CSTRING("_"), start, end)) {
    end = start;
    aFileName.BeginReading(start);
    aLang.Assign(NS_ConvertUTF8toUTF16(Substring(start, end)));
    return true;
  }

  return false;
}

void
nsPicoService::LoadEngine(PicoVoice* aVoice)
{
  PicoApi::pico_Status status = 0;

  if (mPicoSystem) {
    UnloadEngine();
  }

  if (!mPicoMemArea) {
    mPicoMemArea = MakeUnique<uint8_t[]>(PICO_MEM_SIZE);
  }

  status = sPicoApi.pico_initialize(mPicoMemArea.get(),
                                    PICO_MEM_SIZE, &mPicoSystem);
  PICO_ENSURE_SUCCESS_VOID("pico_initialize", status);

  status = sPicoApi.pico_loadResource(mPicoSystem, aVoice->mTaFile.get(), &mTaResource);
  PICO_ENSURE_SUCCESS_VOID("pico_loadResource", status);

  status = sPicoApi.pico_loadResource(mPicoSystem, aVoice->mSgFile.get(), &mSgResource);
  PICO_ENSURE_SUCCESS_VOID("pico_loadResource", status);

  status = sPicoApi.pico_createVoiceDefinition(mPicoSystem, PICO_VOICE_NAME);
  PICO_ENSURE_SUCCESS_VOID("pico_createVoiceDefinition", status);

  char taName[PICO_RETSTRINGSIZE];
  status = sPicoApi.pico_getResourceName(mPicoSystem, mTaResource, taName);
  PICO_ENSURE_SUCCESS_VOID("pico_getResourceName", status);

  status = sPicoApi.pico_addResourceToVoiceDefinition(
    mPicoSystem, PICO_VOICE_NAME, taName);
  PICO_ENSURE_SUCCESS_VOID("pico_addResourceToVoiceDefinition", status);

  char sgName[PICO_RETSTRINGSIZE];
  status = sPicoApi.pico_getResourceName(mPicoSystem, mSgResource, sgName);
  PICO_ENSURE_SUCCESS_VOID("pico_getResourceName", status);

  status = sPicoApi.pico_addResourceToVoiceDefinition(
    mPicoSystem, PICO_VOICE_NAME, sgName);
  PICO_ENSURE_SUCCESS_VOID("pico_addResourceToVoiceDefinition", status);

  status = sPicoApi.pico_newEngine(mPicoSystem, PICO_VOICE_NAME, &mPicoEngine);
  PICO_ENSURE_SUCCESS_VOID("pico_newEngine", status);

  if (sSingleton) {
    sSingleton->mCurrentVoice = aVoice;
  }
}

void
nsPicoService::UnloadEngine()
{
  PicoApi::pico_Status status = 0;

  if (mPicoEngine) {
    status = sPicoApi.pico_disposeEngine(mPicoSystem, &mPicoEngine);
    PICO_ENSURE_SUCCESS_VOID("pico_disposeEngine", status);
    status = sPicoApi.pico_releaseVoiceDefinition(mPicoSystem, PICO_VOICE_NAME);
    PICO_ENSURE_SUCCESS_VOID("pico_releaseVoiceDefinition", status);
    mPicoEngine = nullptr;
  }

  if (mSgResource) {
    status = sPicoApi.pico_unloadResource(mPicoSystem, &mSgResource);
    PICO_ENSURE_SUCCESS_VOID("pico_unloadResource", status);
    mSgResource = nullptr;
  }

  if (mTaResource) {
    status = sPicoApi.pico_unloadResource(mPicoSystem, &mTaResource);
    PICO_ENSURE_SUCCESS_VOID("pico_unloadResource", status);
    mTaResource = nullptr;
  }

  if (mPicoSystem) {
    status = sPicoApi.pico_terminate(&mPicoSystem);
    PICO_ENSURE_SUCCESS_VOID("pico_terminate", status);
    mPicoSystem = nullptr;
  }
}

PicoVoice*
nsPicoService::CurrentVoice()
{
  MOZ_ASSERT(!NS_IsMainThread());

  return mCurrentVoice;
}

// static methods

nsPicoService*
nsPicoService::GetInstance()
{
  MOZ_ASSERT(NS_IsMainThread());
  if (!XRE_IsParentProcess()) {
    MOZ_ASSERT(false, "nsPicoService can only be started on main gecko process");
    return nullptr;
  }

  if (!sSingleton) {
    sSingleton = new nsPicoService();
  }

  return sSingleton;
}

already_AddRefed<nsPicoService>
nsPicoService::GetInstanceForService()
{
  RefPtr<nsPicoService> picoService = GetInstance();
  return picoService.forget();
}

void
nsPicoService::Shutdown()
{
  if (!sSingleton) {
    return;
  }

  sSingleton->mCurrentTask = nullptr;

  sSingleton = nullptr;
}

} // namespace dom
} // namespace mozilla