speaker.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*****************************************************************************
  2. * speaker.cpp - QStarDict, a StarDict clone written using Qt *
  3. * Copyright (C) 2008 Alexander Rodin *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License along *
  16. * with this program; if not, write to the Free Software Foundation, Inc., *
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  18. *****************************************************************************/
  19. #include "speaker.h"
  20. #include <QProcess>
  21. #include <QSettings>
  22. namespace QStarDict
  23. {
  24. Speaker::Speaker()
  25. {
  26. m_speechProcess = new QProcess;
  27. QSettings settings;
  28. m_speechCmd = settings.value("Speaker/speechCmd", "festival --tts").toString();
  29. }
  30. Speaker::~Speaker()
  31. {
  32. QSettings settings;
  33. settings.setValue("Speaker/speechCmd", m_speechCmd);
  34. delete m_speechProcess;
  35. }
  36. void Speaker::speak(const QString &word)
  37. {
  38. if (m_speechCmd.isEmpty())
  39. return;
  40. if (m_speechProcess->state() != QProcess::NotRunning)
  41. m_speechProcess->kill();
  42. QString s = m_speechCmd;
  43. s.replace("%s", word);
  44. m_speechProcess->start(s, QIODevice::WriteOnly);
  45. if (! m_speechProcess->waitForStarted())
  46. return;
  47. if (! m_speechCmd.contains("%s"))
  48. {
  49. m_speechProcess->write(word.toUtf8());
  50. m_speechProcess->closeWriteChannel();
  51. }
  52. }
  53. }
  54. // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc