From 4aed2c8219774f5d797760606b8489a92ddc5163 Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdebase@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- khotkeys/kcontrol/voicerecorder.cpp | 203 ++++++++++++++++++++++++++++++++++++ 1 file changed, 203 insertions(+) create mode 100644 khotkeys/kcontrol/voicerecorder.cpp (limited to 'khotkeys/kcontrol/voicerecorder.cpp') diff --git a/khotkeys/kcontrol/voicerecorder.cpp b/khotkeys/kcontrol/voicerecorder.cpp new file mode 100644 index 000000000..92458c2ee --- /dev/null +++ b/khotkeys/kcontrol/voicerecorder.cpp @@ -0,0 +1,203 @@ +/**************************************************************************** + + KHotKeys + + Copyright (C) 2005 Olivier Goffgart + + Distributed under the terms of the GNU General Public License version 2. + +****************************************************************************/ + +#include +#include + +#include "voicerecorder.h" +#include "soundrecorder.h" +#include "voicesignature.h" +#include "voices.h" +#include "khotkeysglobal.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef HAVE_CONFIG_H +#include +#endif + +namespace KHotKeys +{ + +VoiceRecorder::arts_play_fun VoiceRecorder::arts_play = NULL; + +bool VoiceRecorder::init( KLibrary* lib ) +{ +#ifdef HAVE_ARTS + if( arts_play == NULL && lib != NULL ) + arts_play = (arts_play_fun) lib->symbol( "khotkeys_voicerecorder_arts_play" ); +#endif +// kdDebug( 1217 ) << "voicerecorder:" << arts_play << ":" << lib << endl; + return arts_play != NULL; +} + +VoiceRecorder::VoiceRecorder(const Sound& sound_P, const QString &voiceId, QWidget *parent, const char *name) + : Voice_input_widget_ui(parent, name) , _recorder( SoundRecorder::create(this)) , _state(sNotModified), _tempFile(0L) , _voiceId(voiceId) +{ + _sound=sound_P; + buttonPlay->setEnabled(sound_P.size() > 50); + buttonStop->setEnabled(false); + + connect (_recorder , SIGNAL(recorded(const Sound& )) , this , SLOT(slotSoundRecorded(const Sound& ) )); + + //if(voiceid_P.isEmpty()) + emit recorded(false); + + drawSound(); +} + +VoiceRecorder::~VoiceRecorder() +{ + delete _tempFile; +} + +void VoiceRecorder::slotRecordPressed() +{ + buttonRecord->setEnabled(false); + buttonPlay->setEnabled(false); + buttonStop->setEnabled(true); + _recorder->start(); + label->setText(i18n("Recording...")); +} + +void VoiceRecorder::slotStopPressed() +{ + buttonRecord->setEnabled(true); + buttonPlay->setEnabled(false); + buttonStop->setEnabled(false); + _recorder->stop(); +} + +void VoiceRecorder::slotPlayPressed() +{ +#ifdef HAVE_ARTS + if( !haveArts() || arts_play == NULL ) + return; + /*if(!_modified) + { + QString fileName = locateLocal( "appdata", _original_voiuceid + ".wav" ); + arts_play( fileName ); + } + else + {*/ + if(!_tempFile) + { + _tempFile=new KTempFile(QString::null,".wav"); + _tempFile->setAutoDelete(true); + } + _sound.save(_tempFile->name()); + arts_play( _tempFile->name()); +#endif +} + +Sound VoiceRecorder::sound() const +{ + return _sound; +} + + +void VoiceRecorder::slotSoundRecorded(const Sound &sound) +{ + buttonPlay->setEnabled(true); + _sound=sound; + + bool correct=drawSound() && sound.size()>50; + if(correct) + { + QString vm=voice_handler->isNewSoundFarEnough( VoiceSignature(sound), _voiceId); + if(!vm.isNull()) + { + KMessageBox::sorry (this, i18n("The word you recorded is too close to the existing reference '%1'. Please record another word.").arg(vm) ); + //TODO: messagebox saying there are too much noise + correct=false; + } + } + else + { + KMessageBox::sorry (this, i18n("Unable to extract voice information from noise.\nIf this error occurs repeatedly, it suggests that there is either too much background noise, or the quality of your microphone is too poor.") ); + } + + _state=correct ? sModified : sIncorrect; + emit recorded(correct); +} + + +/*VoiceSignature VoiceRecorder::voiceSig() const +{ + if(voiceId().isEmpty()) + return VoiceSignature(); + QString fileName = locateLocal( "appdata", voiceId() + ".wav" ); + _sound.save( fileName ); + return VoiceSignature(_sound); +}*/ + +bool VoiceRecorder::drawSound() +{ + label->setText(QString::null); + uint length=_sound.size(); + + if(length < 2) + return false; + + int width=label->width(); + int height=label->height(); + QPixmap pix(width,height); + pix.fill(QColor(255,255,255)); + QPainter p; + p.begin(&pix); + + p.setPen(QPen(QColor("green"),1)); + p.drawLine(0,height/2,width,height/2); + + p.setPen(QPen(QColor("red"),1)); + + uint lx=0; + uint ly=height/2; + + /*** DRAW THE SIGNAL ******/ + for(uint f=1; fsetPixmap(pix); + return res; +} + + +} // namespace KHotKeys + +#include "voicerecorder.moc" -- cgit v1.2.1