Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Mar 31st, 2010  |  syntax: Diff  |  size: 5.64 KB  |  views: 120  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. diff --git a/rbutil/rbutilqt/base/talkgenerator.cpp b/rbutil/rbutilqt/base/talkgenerator.cpp
  2. index 5c0f8e9..6d566e2 100644
  3. --- a/rbutil/rbutilqt/base/talkgenerator.cpp
  4. +++ b/rbutil/rbutilqt/base/talkgenerator.cpp
  5. @@ -22,7 +22,7 @@
  6.  #include "systeminfo.h"
  7.  #include "wavtrim.h"
  8.  
  9. -TalkGenerator::TalkGenerator(QObject* parent): QObject(parent)
  10. +TalkGenerator::TalkGenerator(QObject* parent): QObject(parent), encFutureWatcher(this)
  11.  {
  12.  
  13.  }
  14. @@ -184,50 +184,69 @@ TalkGenerator::Status TalkGenerator::voiceList(QList<TalkEntry>* list,int wavtri
  15.  //!
  16.  TalkGenerator::Status TalkGenerator::encodeList(QList<TalkEntry>* list)
  17.  {
  18. -    QStringList dublicates;
  19. +    QStringList duplicates;
  20.  
  21. -    int progressMax = list->size();
  22. -    int m_progress = 0;
  23. -    emit logProgress(m_progress,progressMax);
  24. +       int itemsCount = list->size();
  25. +       emit logProgress(0, itemsCount);
  26.  
  27. -    for(int i=0; i < list->size(); i++)
  28. +       /* Do some preprocessing and remove the invalid entries. As far as I can see,
  29. +        * this list is not going to be used anywhere else, so we might as well butcher it*/
  30. +       for (int idx=0; idx < itemsCount; idx++)
  31.      {
  32. -        if(m_abort)
  33. +               if(list->at(idx).voiced == false)
  34.          {
  35. -            emit logItem(tr("Encoding aborted"), LOGERROR);
  36. -            return eERROR;
  37. +            qDebug() << "non voiced entry" << list->at(idx).toSpeak <<"detected";
  38. +                       list->removeAt(idx);
  39. +                       itemsCount--;
  40. +                       idx--;
  41. +            continue;
  42.          }
  43. -
  44. -         //skip non-voiced entrys
  45. -        if(list->at(i).voiced == false)
  46. +               if(duplicates.contains(list->at(idx).talkfilename))
  47.          {
  48. -            qDebug() << "non voiced entry" << list->at(i).toSpeak <<"detected";
  49. -            emit logProgress(++m_progress,progressMax);
  50. +                       list->removeAt(idx);
  51. +                       itemsCount--;
  52. +                       idx--;
  53.              continue;
  54.          }
  55. -        //skip dublicates
  56. -         if(!dublicates.contains(list->at(i).talkfilename))
  57. -            dublicates.append(list->at(i).talkfilename);
  58. +               duplicates.append(list->at(idx).talkfilename);
  59. +               (*list)[idx].encoder = m_enc;
  60. +               (*list)[idx].generator = this;
  61. +       }
  62. +      
  63. +       connect(&encFutureWatcher, SIGNAL(progressValueChanged(int)), this, SLOT(encProgress(int)));
  64. +       encFutureWatcher.setFuture(QtConcurrent::map(*list, &TalkGenerator::encEntryPoint));
  65. +
  66. +       /* We use this loop as an equivalent to encFutureWatcher.waitForFinished()
  67. +        * since the latter blocks all events */
  68. +       while (encFutureWatcher.isRunning())
  69. +               QCoreApplication::processEvents(QEventLoop::AllEvents);
  70. +
  71. +       if (encFutureWatcher.isCanceled())
  72. +               return eERROR;
  73.          else
  74. -        {
  75. -            qDebug() << "dublicate skipped";
  76. -            (*list)[i].encoded = true;
  77. -            emit logProgress(++m_progress,progressMax);
  78. -            continue;
  79. +           return eOK;
  80.          }
  81.  
  82. -        //encode entry
  83. -        qDebug() << "encoding " << list->at(i).wavfilename << "to" << list->at(i).talkfilename;
  84. -        if(!m_enc->encode(list->at(i).wavfilename,list->at(i).talkfilename))
  85. +void TalkGenerator::encEntryPoint(TalkEntry& entry)
  86.          {
  87. -            emit logItem(tr("Encoding of %1 failed").arg(list->at(i).wavfilename), LOGERROR);
  88. -            return eERROR;
  89. +       bool res = entry.encoder->encode(entry.wavfilename, entry.talkfilename);
  90. +       entry.encoded = res;
  91. +       if (!entry.encoded)
  92. +               entry.generator->encFailEntry(entry);
  93. +       return;
  94.          }
  95. -        (*list)[i].encoded = true;
  96. -        emit logProgress(++m_progress,progressMax);
  97. -        QCoreApplication::processEvents();
  98. +
  99. +void TalkGenerator::encProgress(int value)
  100. +{
  101. +       qDebug() << "[TalkGen] Progress at " << value;
  102. +       emit logProgress(value, encFutureWatcher.progressMaximum());
  103.      }
  104. -    return eOK;
  105. +
  106. +void TalkGenerator::encFailEntry(const TalkEntry& entry)
  107. +{
  108. +       encFutureWatcher.cancel();
  109. +       encFutureWatcher.waitForFinished();
  110. +       emit logItem(tr("Encoding of %1 failed").arg(entry.wavfilename), LOGERROR);
  111.  }
  112.  
  113.  //! \brief slot, which is connected to the abort of the Logger. Sets a flag, so Creating Talkfiles ends at the next possible position
  114. @@ -235,5 +254,12 @@ TalkGenerator::Status TalkGenerator::encodeList(QList<TalkEntry>* list)
  115.  void TalkGenerator::abort()
  116.  {
  117.      m_abort = true;
  118. +
  119. +       if (encFutureWatcher.isRunning())
  120. +       {
  121. +               encFutureWatcher.cancel();
  122. +               encFutureWatcher.waitForFinished();
  123. +               emit logItem(tr("Encoding aborted"), LOGERROR);
  124. +       }
  125.  }
  126.  
  127. diff --git a/rbutil/rbutilqt/base/talkgenerator.h b/rbutil/rbutilqt/base/talkgenerator.h
  128. index b139c18..7b54b7b 100644
  129. --- a/rbutil/rbutilqt/base/talkgenerator.h
  130. +++ b/rbutil/rbutilqt/base/talkgenerator.h
  131. @@ -49,14 +49,22 @@ public:
  132.          QString target;
  133.          bool voiced;
  134.          bool encoded;
  135. +
  136. +                               /* We need the following members because
  137. +                                * 1) the QtConcurrent entry points are all static methods (and we need to communicate
  138. +                                * with the TalkGenerator
  139. +                                * 2) we are not guaranteed to go through the list in any particular order,
  140. +                                * so we can't use the progress slot for error checking */
  141. +                               EncBase* encoder;
  142. +                               TalkGenerator* generator;
  143.      };
  144.  
  145.      TalkGenerator(QObject* parent);
  146. -
  147.      Status process(QList<TalkEntry>* list,int wavtrimth = -1);
  148.  
  149.  public slots:
  150.      void abort();
  151. +               void encProgress(int value);
  152.  
  153.  signals:
  154.      void done(bool);
  155. @@ -64,9 +72,14 @@ signals:
  156.      void logProgress(int, int); //! set progress bar.
  157.  
  158.  private:
  159. +               QFutureWatcher<void> encFutureWatcher;
  160. +               void encFailEntry(const TalkEntry& entry);
  161. +              
  162.      Status voiceList(QList<TalkEntry>* list,int wavetrimth);
  163.      Status encodeList(QList<TalkEntry>* list);
  164.  
  165. +               static void encEntryPoint(TalkEntry& entry);
  166. +
  167.      TTSBase* m_tts;
  168.      EncBase* m_enc;
clone this paste RAW Paste Data