• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdeedu
  • Sitemap
  • Contact Us
 

klettres

soundfactory.cpp

Go to the documentation of this file.
00001 /* -------------------------------------------------------------
00002    From KDE Tuberling
00003    mailto:e.bischoff@noos.fr
00004  ------------------------------------------------------------- */
00005 /*
00006  * Copyright (C) 2001 Eric Bischoff
00007    2004-2007 Anne-Marie Mahfouf <annma@kde.org>
00008 
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public
00011     License as published by the Free Software Foundation; either
00012     version 2 of the License, or (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017     GNU General Public License for more details.
00018 
00019     You should have received a copy of the GNU General Public License
00020     along with this program; if not, write to the Free Software
00021     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00022  */
00023 
00024 #include "soundfactory.h"
00025 
00026 #include <KMessageBox>
00027 #include <KLocale>
00028 #include <KDebug>
00029 #include <KRandomSequence>
00030 #include <KStandardDirs>
00031 #include <Phonon/MediaObject>
00032 
00033 #include "klettres.h"
00034 #include "prefs.h"
00035 
00036 SoundFactory::SoundFactory(KLettres *parent, const char *)
00037         : QObject(parent), m_player(0)
00038 {
00039     klettres = parent;
00040 
00041     namesList.clear();
00042     filesList.clear();
00043     sounds = 0;
00044 
00045     bool ok = klettres->loadLayout(m_layoutsDocument);
00046     if (ok)  {
00047     change(Prefs::language());
00048     }
00049     if (!ok)  {
00050     loadFailure();
00051     }  else  {
00052     setSoundSequence();
00053     }
00054 }
00055 
00056 SoundFactory::~SoundFactory()
00057 {
00058 }
00059 
00060 void SoundFactory::change(const QString &currentLanguage)
00061 {
00062     //go load the sounds for the current language
00063     bool ok = loadLanguage(m_layoutsDocument, currentLanguage);
00064     //tell the user if there are no sounds or get the random sounds
00065     if (!ok)  {
00066     loadFailure();
00067     }  else  {
00068     setSoundSequence();
00069     }
00070 }
00071 
00072 void SoundFactory::playSound(int mySound)
00073 {
00074     QString soundFile;
00075 
00076     if ((uint) mySound >= sounds) {
00077     return;
00078     }
00079 
00080     soundFile = KStandardDirs::locate("data", "klettres/" + filesList[mySound]);
00081     kDebug() << "soundFile " << soundFile;
00082 
00083     if (soundFile.isEmpty()) {
00084     return;
00085     }
00086 
00087     if (!m_player)  {
00088         m_player = Phonon::createPlayer(Phonon::GameCategory, soundFile);
00089         m_player->setParent(this);
00090     }  else  {
00091         m_player->setCurrentSource(soundFile);
00092     }
00093     m_player->play();
00094 }
00095 
00096 void SoundFactory::loadFailure()
00097 {
00098     KMessageBox::error(klettres, i18n("Error while loading the sound names."));
00099 }
00100 
00101 bool SoundFactory::loadLanguage(QDomDocument &layoutDocument, const QString &currentLanguage)
00102 {
00103     QDomNodeList languagesList,
00104     alphabetList,
00105     syllablesList,
00106     soundNamesList;
00107     QDomElement languageElement,
00108     alphabetElement,
00109     syllableElement,
00110     soundNameElement;
00111     QDomAttr nameAttribute, fileAttribute;
00112 
00113     languagesList = layoutDocument.elementsByTagName("language");
00114     QDomAttr codeAttribute;
00115     //check if the sound files match current language
00116     languageElement = (const QDomElement &) languagesList.item(0).toElement();
00117     codeAttribute = languageElement.attributeNode("code");
00118 
00119     if (currentLanguage != codeAttribute.value()) {
00120         kDebug() << "Fail reading language !!! ";
00121         return false;
00122     } else {
00123     kDebug() << "current language " << currentLanguage;
00124     }
00125     //load the sounds for level 1 and 2 (alphabet)
00126     if ((Prefs::level() == 1) || (Prefs::level() == 2))  {
00127         alphabetList = languageElement.elementsByTagName("alphabet");
00128         if (alphabetList.count() != 1) {
00129             return false;
00130     }
00131         alphabetElement = (const QDomElement &) alphabetList.item(0).toElement();
00132         soundNamesList = alphabetElement.elementsByTagName("sound");
00133     }
00134 
00135     //load the sounds for level 3 and 4 (syllables)
00136     if ((Prefs::level() == 3) || (Prefs::level() == 4))  {
00137         syllablesList = languageElement.elementsByTagName("syllables");
00138         if (syllablesList.count() != 1)
00139             return false;
00140 
00141         syllableElement = (const QDomElement &) syllablesList.item(0).toElement();
00142 
00143         soundNamesList = syllableElement.elementsByTagName("sound");
00144     }
00145     //Counts the number of sounds
00146     sounds = soundNamesList.count();
00147     kDebug() << "number of sounds" << sounds << endl;
00148     if (sounds < 1)  {
00149         return false;
00150     }
00151     namesList.clear();
00152     filesList.clear();
00153 
00154     for (uint sound = 0; sound < sounds; sound++)  {
00155         soundNameElement = (const QDomElement &) soundNamesList.item(sound).toElement();
00156         nameAttribute = soundNameElement.attributeNode("name");
00157         //namesList helds the names of the letter or syllable to display
00158         namesList.append(nameAttribute.value());
00159         fileAttribute = soundNameElement.attributeNode("file");
00160         //filesList helds the names of the sound files (i.e the location of the sounds like fr/alpha/a-0.mp3)
00161         filesList.append(fileAttribute.value());
00162     }
00163     if (namesList.isEmpty()) {
00164     return false;
00165     }
00166     if (filesList.isEmpty())  {
00167     return false;
00168     }
00169     return true;
00170 }
00171 
00172 void SoundFactory::setSoundSequence()
00173 {
00174     // Seed the random number generator
00175     KRandomSequence randomSequence;
00176     randomList.clear();
00177     //get the number of sounds then shuffle it: each number will be taken once then the sequence will come back
00178     for (uint j = 0; j < sounds; j++) 
00179         randomList.append(j);
00180 
00181     randomSequence.randomize(randomList);
00182 }
00183 
00184 #include "soundfactory.moc"

klettres

Skip menu "klettres"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

kdeedu

Skip menu "kdeedu"
  • kalzium
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  •   docs
  •   src
  • parley
  •   stepcore
Generated for kdeedu by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal