kttsd Library API Documentation

testplayer.cpp

00001 /***************************************************** vim:set ts=4 sw=4 sts=4:
00002   Player Object for playing synthesized audio files.  Plays them
00003   synchronously.
00004   -------------------
00005   Copyright : (C) 2004 Gary Cramblitt
00006   -------------------
00007   Original author: Gary Cramblitt <garycramblitt@comcast.net>
00008   Current Maintainer: Gary Cramblitt <garycramblitt@comcast.net>
00009  ******************************************************************************/
00010 
00011 /***************************************************************************
00012  *                                                                         *
00013  *   This program is free software; you can redistribute it and/or modify  *
00014  *   it under the terms of the GNU General Public License as published by  *
00015  *   the Free Software Foundation; version 2 of the License.               *
00016  *                                                                         *
00017  ***************************************************************************/
00018 
00019 // Qt includes.
00020 #include <qfile.h>
00021 
00022 // KDE includes.
00023 #include <kapplication.h>
00024 #include <ktempfile.h>
00025 #include <kstandarddirs.h>
00026 #include <kparts/componentfactory.h>
00027 #include <ktrader.h>
00028 #include <kdebug.h>
00029 
00030 // KTTS includes.
00031 #include "player.h"
00032 #include "stretcher.h"
00033 #include "pluginconf.h"
00034 
00035 // TestPlayer includes.
00036 #include "testplayer.h"
00037 
00041 TestPlayer::TestPlayer(QObject *parent, const char *name,
00042     const int playerOption, const float audioStretchFactor, const QString &sinkName) :
00043     QObject(parent, name)
00044 {
00045     m_playerOption = playerOption;
00046     m_audioStretchFactor = audioStretchFactor;
00047     m_stretcher = 0;
00048     m_player = 0;
00049     m_sinkName = sinkName;
00050 }
00051 
00055 TestPlayer::~TestPlayer()
00056 {
00057     delete m_stretcher;
00058     delete m_player;
00059 }
00060 
00066 void TestPlayer::setPlayerOption(const int playerOption) { m_playerOption = playerOption; }
00067 
00074 void TestPlayer::setAudioStretchFactor(const float audioStretchFactor)
00075     { m_audioStretchFactor = audioStretchFactor; }
00076 
00077 void TestPlayer::setSinkName(const QString &sinkName) { m_sinkName = sinkName; }
00078 
00084 void TestPlayer::play(const QString &waveFile)
00085 {
00086     // kdDebug() << "TestPlayer::play: running" << endl;
00087     // Create a Stretcher object to adjust the audio Speed.
00088     QString playFile = waveFile;
00089     QString tmpFile;
00090     if (m_audioStretchFactor != 1.0)
00091     {
00092         tmpFile = makeSuggestedFilename();
00093         // kdDebug() << "TestPlayer::play: stretching file " << playFile << " by " << m_audioStretchFactor
00094         //     << " to file " << tmpFile << endl;
00095         m_stretcher = new Stretcher();
00096         if (m_stretcher->stretch(playFile, tmpFile, m_audioStretchFactor))
00097         {
00098             while (m_stretcher->getState() != Stretcher::ssFinished) qApp->processEvents();
00099             playFile = m_stretcher->getOutFilename();
00100         }
00101         delete m_stretcher;
00102         m_stretcher = 0;
00103     }
00104 
00105     // Create player object based on player option.
00106     // kdDebug() << "TestPlayer::play: creating Player object with playerOption " << m_playerOption << endl;
00107     m_player = createPlayerObject(m_playerOption);
00108     // If player object could not be created, avoid crash is the best we can do!
00109     if (!m_player) return;
00110     // kdDebug() << "TestPlayer::play: starting playback." << endl;
00111     m_player->startPlay(playFile);
00112 
00113     // TODO: The following hunk of code would ideally be unnecessary.  We would just
00114     // return at this point and let take care of
00115     // cleaning up the play object.  However, because we've been called from DCOP,
00116     // this seems to be necessary.  The call to processEvents is problematic because
00117     // it can cause re-entrancy.
00118     while (m_player->playing()) qApp->processEvents();
00119     // kdDebug() << "TestPlayer::play: stopping playback." << endl;
00120     m_player->stop();
00121     delete m_player;
00122     m_player = 0;
00123     if (!tmpFile.isEmpty()) QFile::remove(tmpFile);
00124 }
00125 
00129 Player* TestPlayer::createPlayerObject(int playerOption)
00130 {
00131     Player* player = 0;
00132     QString plugInName;
00133     switch(playerOption)
00134     {
00135         case 1 :
00136         {
00137             plugInName = "KTTSD GStreamer Plugin";
00138             break;
00139         }
00140         default:
00141         {
00142             plugInName = "KTTSD Arts Plugin";
00143             break;
00144         }
00145     }
00146     KTrader::OfferList offers = KTrader::self()->query(
00147             "KTTSD/AudioPlugin", QString("Name == '%1'").arg(plugInName));
00148 
00149     if(offers.count() == 1)
00150     {
00151         kdDebug() << "TestPlayer::createPlayerObject: Loading " << offers[0]->library() << endl;
00152         KLibFactory *factory = KLibLoader::self()->factory(offers[0]->library());
00153         if (factory)
00154             player = 
00155                 KParts::ComponentFactory::createInstanceFromLibrary<Player>(
00156                     offers[0]->library(), this, offers[0]->library());
00157         else
00158             kdDebug() << "TestPlayer::createPlayerObject: Could not create factory." << endl;
00159     }
00160     if (player == 0)
00161         kdDebug() << "TestPlayer::createPlayerObject: Could not load " + plugInName +
00162             ".  Is KDEDIRS set correctly?" << endl;
00163     else
00164         // Must have GStreamer >= 0.8.7.
00165         if (playerOption == 1)
00166         {
00167             if (!player->requireVersion(0, 8, 7))
00168             {
00169                 delete player;
00170                 player = 0;
00171             }
00172             else
00173                 player->setSinkName(m_sinkName);
00174         }
00175     return player;
00176 }
00177 
00183 QString TestPlayer::makeSuggestedFilename()
00184 {
00185     KTempFile tempFile (locateLocal("tmp", "kttsmgr-"), ".wav");
00186     QString waveFile = tempFile.file()->name();
00187     tempFile.close();
00188     QFile::remove(waveFile);
00189     kdDebug() << "TestPlayer::makeSuggestedFilename: Suggesting filename: " << waveFile << endl;
00190     return PlugInConf::realFilePath(waveFile);
00191 }
00192 
KDE Logo
This file is part of the documentation for kttsd Library Version 3.3.90.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Dec 12 14:37:18 2004 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003