File indexing completed on 2024-03-24 15:43:47

0001 /***************************************************************************
0002                           csoundplayer.cpp  -  sound player
0003                              -------------------
0004     begin                : So apr 19 2003
0005     copyright            : (C) 2003-2008 by Tomas Mecir
0006     email                : kmuddy@kmuddy.com
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either version 2 of the License, or     *
0014  *   (at your option) any later version.                                   *
0015  *                                                                         *
0016  ***************************************************************************/
0017 
0018 #include "csoundplayer.h"
0019 
0020 struct cSoundPlayer::Private {
0021   QMediaPlayer player;
0022 
0023   bool isWave;
0024   bool nosound;
0025   
0026   QString fName;
0027   int repeatCount, priority, volume;
0028   QString newFName;
0029   int newRepeatCount, newPriority, newVolume;
0030 };
0031 
0032 cSoundPlayer::cSoundPlayer (bool isWAVE)
0033     : cActionBase (isWAVE ? "soundplayer" : "musicplayer", 0)
0034 {
0035   d = new Private;
0036 
0037   d->isWave = isWAVE;
0038   d->nosound = false;
0039 }
0040 
0041 cSoundPlayer::~cSoundPlayer()
0042 {
0043   stop ();
0044   delete d;
0045 }
0046 
0047 void cSoundPlayer::init ()
0048 {
0049   connect (&d->player, &QMediaPlayer::stateChanged, this, &cSoundPlayer::stateChanged);
0050 }
0051 
0052 bool cSoundPlayer::isPlaying ()
0053 {
0054   return (d->player.state() == QMediaPlayer::PlayingState);
0055 }
0056 
0057 int cSoundPlayer::curPriority ()
0058 {
0059   return d->priority;
0060 }
0061 
0062 int cSoundPlayer::remainingRepeats ()
0063 {
0064   return d->repeatCount;
0065 }
0066 
0067 QString cSoundPlayer::fileName ()
0068 {
0069   return d->fName;
0070 }
0071 
0072 int cSoundPlayer::curVolume ()
0073 {
0074   return d->volume;
0075 }
0076 
0077 void cSoundPlayer::setPriority (int val)
0078 {
0079   d->newPriority = val;
0080 }
0081 
0082 void cSoundPlayer::setRepeatsCount (int val)
0083 {
0084   d->newRepeatCount = val;
0085 }
0086 
0087 void cSoundPlayer::setFileName (const QString &name)
0088 {
0089   d->newFName = name;
0090 }
0091 
0092 void cSoundPlayer::setVolume (int val)
0093 {
0094   d->newVolume = val;
0095 }
0096 
0097 void cSoundPlayer::play ()
0098 {
0099   if (d->nosound) return;
0100 
0101   // stop existing sound, if any
0102   stop ();
0103 
0104   //apply new parameters
0105   d->fName = d->newFName;
0106   d->repeatCount = d->newRepeatCount;
0107   d->priority = d->newPriority;
0108   d->volume = d->newVolume;
0109 
0110   // Intialise the play object, if needed
0111   init ();
0112 
0113   d->player.setMedia (QUrl::fromLocalFile (d->fName));
0114   d->player.setVolume (d->volume);
0115   d->player.play ();
0116 }
0117 
0118 void cSoundPlayer::stop ()
0119 {
0120   if (d->nosound) return;
0121   d->player.stop();
0122 }
0123 
0124 void cSoundPlayer::forceUpdateParams ()
0125 {
0126   d->repeatCount = d->newRepeatCount;
0127   d->priority = d->newPriority;
0128   d->volume = d->newVolume;
0129 }
0130 
0131 void cSoundPlayer::disableSound ()
0132 {
0133   d->nosound = true;
0134 }
0135 
0136 void cSoundPlayer::stateChanged (QMediaPlayer::State newState)
0137 {
0138   if ((newState == QMediaPlayer::StoppedState) && (d->player.mediaStatus() == QMediaPlayer::EndOfMedia))
0139     finished();
0140 
0141   // TODO error reporting
0142 }
0143 
0144 void cSoundPlayer::finished ()
0145 {
0146   if (d->repeatCount != -1)  //-1 means infinite playing
0147     d->repeatCount--;  //decrease repeat count
0148   if (d->repeatCount != 0) {
0149     // we need to play again - so play again
0150     d->player.setMedia (QUrl::fromLocalFile (d->fName));
0151     d->player.setVolume (d->volume);
0152     d->player.play ();
0153   }
0154 }
0155 
0156 #include "moc_csoundplayer.cpp"