Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / khotkeys / kcontrol / voicerecorder.cpp
blob4f71d814b200336a3f8cbb98cdfd856f56113cb9
1 /****************************************************************************
3 KHotKeys
5 Copyright (C) 2005 Olivier Goffgart <ogoffart @ kde.org>
7 Distributed under the terms of the GNU General Public License version 2.
9 ****************************************************************************/
11 #include <QtGui/QColor>
12 #include <QtGui/QActionEvent>
14 #include "voicerecorder.h"
15 #include "soundrecorder.h"
16 #include "voicesignature.h"
17 #include "voices.h"
18 #include "khotkeysglobal.h"
19 #include <kpushbutton.h>
20 #include <klineedit.h>
21 #include <klocale.h>
22 #include <ktemporaryfile.h>
23 #include <QtGui/QLabel>
24 #include <QtGui/QPainter>
25 #include <kmessagebox.h>
26 #include <klibloader.h>
27 #include <kstandarddirs.h>
29 #include <config-khotkeys.h>
31 namespace KHotKeys
34 VoiceRecorder::arts_play_fun VoiceRecorder::arts_play = NULL;
36 bool VoiceRecorder::init( KLibrary* lib )
38 #ifdef HAVE_ARTS
39 if( arts_play == NULL && lib != NULL )
40 arts_play = (arts_play_fun) lib->symbol( "khotkeys_voicerecorder_arts_play" );
41 #endif
42 // kDebug( 1217 ) << "voicerecorder:" << arts_play << ":" << lib;
43 return arts_play != NULL;
46 VoiceRecorder::VoiceRecorder(const Sound& sound_P, const QString &voiceId, QWidget *parent, const char *name)
47 : Voice_input_widget_ui(parent) , _recorder( SoundRecorder::create(this)) , _state(sNotModified), _tempFile(0L) , _voiceId(voiceId)
49 setObjectName(name);
50 _sound=sound_P;
51 buttonPlay->setEnabled(sound_P.size() > 50);
52 buttonStop->setEnabled(false);
54 connect (_recorder , SIGNAL(recorded(const Sound& )) , this , SLOT(slotSoundRecorded(const Sound& ) ));
55 connect(buttonRecord,SIGNAL(pressed()),this,SLOT(slotRecordPressed()));
56 connect(buttonStop,SIGNAL(pressed()),this,SLOT(slotStopPressed()));
57 connect(buttonPlay,SIGNAL(pressed()),this,SLOT(slotPlayPressed()));
59 //if(voiceid_P.isEmpty())
60 emit recorded(false);
62 drawSound();
65 VoiceRecorder::~VoiceRecorder()
67 delete _tempFile;
70 void VoiceRecorder::slotRecordPressed()
72 buttonRecord->setEnabled(false);
73 buttonPlay->setEnabled(false);
74 buttonStop->setEnabled(true);
75 _recorder->start();
76 label->setText(i18n("Recording..."));
79 void VoiceRecorder::slotStopPressed()
81 buttonRecord->setEnabled(true);
82 buttonPlay->setEnabled(false);
83 buttonStop->setEnabled(false);
84 _recorder->stop();
87 void VoiceRecorder::slotPlayPressed()
89 #ifdef HAVE_ARTS
90 if( !haveArts() || arts_play == NULL )
91 return;
92 /*if(!_modified)
94 QString fileName = locateLocal( "appdata", _original_voiuceid + ".wav" );
95 arts_play( fileName );
97 else
98 {*/
99 if(!_tempFile)
101 _tempFile=new KTemporaryFile();
102 _tempFile->setSuffix(".wav");
103 _tempFile->open();
105 _sound.save(_tempFile->fileName());
106 arts_play( _tempFile->fileName());
107 #endif
110 Sound VoiceRecorder::sound() const
112 return _sound;
116 void VoiceRecorder::slotSoundRecorded(const Sound &sound)
118 buttonPlay->setEnabled(true);
119 _sound=sound;
121 bool correct=drawSound() && sound.size()>50;
122 if(correct)
124 QString vm=voice_handler->isNewSoundFarEnough( VoiceSignature(sound), _voiceId);
125 if(!vm.isNull())
127 KMessageBox::sorry (this, i18n("The word you recorded is too close to the existing reference '%1'. Please record another word.", vm) );
128 //TODO: messagebox saying there are too much noise
129 correct=false;
132 else
134 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.") );
137 _state=correct ? sModified : sIncorrect;
138 emit recorded(correct);
142 /*VoiceSignature VoiceRecorder::voiceSig() const
144 if(voiceId().isEmpty())
145 return VoiceSignature();
146 QString fileName = locateLocal( "appdata", voiceId() + ".wav" );
147 _sound.save( fileName );
148 return VoiceSignature(_sound);
151 bool VoiceRecorder::drawSound()
153 label->setText(QString());
154 uint length=_sound.size();
156 if(length < 2)
157 return false;
159 int width=label->width();
160 int height=label->height();
161 QPixmap pix(width,height);
162 pix.fill(QColor(255,255,255));
163 QPainter p;
164 p.begin(&pix);
166 p.setPen(QPen(QColor("green"),1));
167 p.drawLine(0,height/2,width,height/2);
169 p.setPen(QPen(QColor("red"),1));
171 uint lx=0;
172 uint ly=height/2;
174 /*** DRAW THE SIGNAL ******/
175 for(uint f=1; f<length; f++)
177 uint nx=f*width/length;
178 uint ny=(uint)(height/2* (1.0 - _sound.at(f)));
179 p.drawLine(lx,ly,nx,ny);
180 lx=nx; ly=ny;
183 unsigned int start=0 , stop=0;
184 bool res=KHotKeys::VoiceSignature::window(_sound,&start,&stop);
185 p.setPen(QPen(QColor("blue"),1));
186 if(res)
188 p.drawLine(start*width/length ,0,start*width/length ,height);
189 p.drawLine(stop*width/length ,0,stop*width/length ,height);
191 else
193 p.drawLine(0 ,0, width ,height);
194 p.drawLine(width ,0, 0 ,height);
197 p.end();
199 label->setPixmap(pix);
200 return res;
204 } // namespace KHotKeys
206 #include "voicerecorder.moc"