1 /****************************************************************************
5 Copyright (C) 2005 Olivier Goffart <ogoffart @ kde.org>
7 Distributed under the terms of the GNU General Public License version 2.
9 ****************************************************************************/
15 #include "voicesignature.h"
17 #include "soundrecorder.h"
24 #include <kapplication.h>
26 #include <kxerrorhandler.h>
27 #include <QtCore/QTimer>
40 Voice::Voice( bool enabled_P
, QObject
* parent_P
)
41 : QObject( parent_P
) ,_enabled( enabled_P
), _recording( false ), _recorder(0)
43 assert( voice_handler
== NULL
);
61 void Voice::enable( bool enabled_P
)
64 enabled_P
= false; // never enabled when there's no support
66 if( _enabled
== enabled_P
)
69 _kga
->setEnabled(enabled_P
);
72 void Voice::register_handler( Voice_trigger
*trigger_P
)
74 if( !_references
.contains( trigger_P
))
75 _references
.append(trigger_P
);
78 void Voice::unregister_handler( Voice_trigger
*trigger_P
)
80 _references
.remove(trigger_P
);
84 void Voice::record_start()
89 _recorder
= SoundRecorder::create(this);
90 connect(_recorder
, SIGNAL(recorded(const Sound
& )), this, SLOT(slot_sound_recorded(const Sound
& )));
97 void Voice::record_stop()
111 void Voice::slot_sound_recorded(const Sound
&sound_P
)
113 VoiceSignature
signature(sound_P
);
115 Voice_trigger
*trig
=0L;
116 Voice_trigger
*sec_trig
=0L;
117 double minimum
=800000;
118 double second_minimum
=80000;
120 foreach(Voice_trigger
*t
, _references
)
122 for(int ech
=1; ech
<=2 ; ech
++)
124 double diff
=VoiceSignature::diff(signature
, t
->voicesignature(ech
));
127 second_minimum
=minimum
;
132 else if(second_minimum
>=diff
)
137 if( diff
< REJECT_FACTOR_DIFF
)
139 kDebug() << ( (diff
< REJECT_FACTOR_DIFF
) ? "+++" : "---" ) <<t
->voicecode() << ech
<< " : " << diff
;
142 // double ecart_relatif=(second_minimum-minimum)/minimum;
144 // kDebug() << ecart_relatif;
147 kDebug() << "**** " << trig
->voicecode() << " : " << minimum
;
150 // if(trig && ecart_relatif > REJECT_FACTOR_ECART_REL)
151 // if(trig && got_count==1)
152 bool selected
=trig
&& (got_count
==1 || ( minimum
< 1.5*REJECT_FACTOR_DIFF
&& trig
==sec_trig
) );
156 trig
->handle_Voice();
162 /*bool Voice::x11Event( XEvent* pEvent )
164 if( pEvent->type != XKeyPress && pEvent->type != XKeyRelease )
167 KKeyNative keyNative( pEvent );
169 //kDebug() << keyNative.key().toString();
171 if(_shortcut.contains(keyNative))
173 if(pEvent->type == XKeyPress && !_recording )
178 if(pEvent->type == XKeyRelease && _recording )
191 void Voice::set_shortcut( const KShortcut
&shortcut
)
193 _shortcut
= shortcut
;
198 _kga
= new KAction(this);
199 _kga
->setObjectName("khotkeys_voice");
200 connect(_kga
,SIGNAL(triggered ( bool )) , this , SLOT(slot_key_pressed()));
202 _kga
->setGlobalShortcut(shortcut
);
205 void Voice::slot_key_pressed()
216 _timer
=new QTimer(this);
217 connect(_timer
, SIGNAL(timeout()) , this, SLOT(slot_timeout()));
219 _timer
->setSingleShot(true);
220 _timer
->start(1000*20);
225 void Voice::slot_timeout()
227 if(_recording
&& _recorder
)
232 _timer
->deleteLater();
237 QString
Voice::isNewSoundFarEnough(const VoiceSignature
& signature
, const QString
¤tTrigger
)
239 Voice_trigger
*trig
=0L;
240 Voice_trigger
*sec_trig
=0L;
241 double minimum
=800000;
242 double second_minimum
=80000;
244 foreach (Voice_trigger
*t
, _references
)
246 if(t
->voicecode()==currentTrigger
)
249 for(int ech
=1; ech
<=2 ; ech
++)
251 double diff
=VoiceSignature::diff(signature
, t
->voicesignature(ech
));
254 second_minimum
=minimum
;
259 else if(second_minimum
>=diff
)
264 if( diff
< REJECT_FACTOR_DIFF
)
266 kDebug() << ( (diff
< REJECT_FACTOR_DIFF
) ? "+++" : "---" ) <<t
->voicecode() << ech
<< " : " << diff
;
271 kDebug() << "**** " << trig
->voicecode() << " : " << minimum
;
273 bool selected
=trig
&& ((got_count
==1 && minimum
< REJECT_FACTOR_DIFF
*0.7 ) || ( minimum
< REJECT_FACTOR_DIFF
&& trig
==sec_trig
) );
274 return selected
? trig
->voicecode() : QString();
277 bool Voice::doesVoiceCodeExists(const QString
&vc
)
279 foreach (Voice_trigger
*t
, _references
)
281 if(t
->voicecode()==vc
)
287 } // namespace KHotKeys
289 #include "voices.moc"