1 /****************************************************************************
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"
18 #include "khotkeysglobal.h"
19 #include <kpushbutton.h>
20 #include <klineedit.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>
34 VoiceRecorder::arts_play_fun
VoiceRecorder::arts_play
= NULL
;
36 bool VoiceRecorder::init( KLibrary
* lib
)
39 if( arts_play
== NULL
&& lib
!= NULL
)
40 arts_play
= (arts_play_fun
) lib
->symbol( "khotkeys_voicerecorder_arts_play" );
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
)
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())
65 VoiceRecorder::~VoiceRecorder()
70 void VoiceRecorder::slotRecordPressed()
72 buttonRecord
->setEnabled(false);
73 buttonPlay
->setEnabled(false);
74 buttonStop
->setEnabled(true);
76 label
->setText(i18n("Recording..."));
79 void VoiceRecorder::slotStopPressed()
81 buttonRecord
->setEnabled(true);
82 buttonPlay
->setEnabled(false);
83 buttonStop
->setEnabled(false);
87 void VoiceRecorder::slotPlayPressed()
90 if( !haveArts() || arts_play
== NULL
)
94 QString fileName = locateLocal( "appdata", _original_voiuceid + ".wav" );
95 arts_play( fileName );
101 _tempFile
=new KTemporaryFile();
102 _tempFile
->setSuffix(".wav");
105 _sound
.save(_tempFile
->fileName());
106 arts_play( _tempFile
->fileName());
110 Sound
VoiceRecorder::sound() const
116 void VoiceRecorder::slotSoundRecorded(const Sound
&sound
)
118 buttonPlay
->setEnabled(true);
121 bool correct
=drawSound() && sound
.size()>50;
124 QString vm
=voice_handler
->isNewSoundFarEnough( VoiceSignature(sound
), _voiceId
);
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
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();
159 int width
=label
->width();
160 int height
=label
->height();
161 QPixmap
pix(width
,height
);
162 pix
.fill(QColor(255,255,255));
166 p
.setPen(QPen(QColor("green"),1));
167 p
.drawLine(0,height
/2,width
,height
/2);
169 p
.setPen(QPen(QColor("red"),1));
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
);
183 unsigned int start
=0 , stop
=0;
184 bool res
=KHotKeys::VoiceSignature::window(_sound
,&start
,&stop
);
185 p
.setPen(QPen(QColor("blue"),1));
188 p
.drawLine(start
*width
/length
,0,start
*width
/length
,height
);
189 p
.drawLine(stop
*width
/length
,0,stop
*width
/length
,height
);
193 p
.drawLine(0 ,0, width
,height
);
194 p
.drawLine(width
,0, 0 ,height
);
199 label
->setPixmap(pix
);
204 } // namespace KHotKeys
206 #include "voicerecorder.moc"