summaryrefslogtreecommitdiffstats
path: root/khotkeys/kcontrol/voicerecorder.cpp
blob: 92458c2ee25b6d2749df612d6b2a51b23ad4225d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/****************************************************************************

 KHotKeys
 
 Copyright (C) 2005 Olivier Goffgart <ogoffart @ kde.org>

 Distributed under the terms of the GNU General Public License version 2.
 
****************************************************************************/

#include <qcolor.h>
#include <qevent.h>

#include "voicerecorder.h"
#include "soundrecorder.h"
#include "voicesignature.h"
#include "voices.h"
#include "khotkeysglobal.h"
#include <kpushbutton.h>
#include <klineedit.h>
#include <klocale.h>
#include <ktempfile.h>
#include <qlabel.h>
#include <qpainter.h>
#include <kmessagebox.h>
#include <klibloader.h>
#include <kstandarddirs.h>

#ifdef HAVE_CONFIG_H
#include <config.h>
#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; f<length; f++)
	{
		uint nx=f*width/length;
		uint ny=(uint)(height/2* (1.0 - _sound.at(f)));
		p.drawLine(lx,ly,nx,ny);
		lx=nx; ly=ny;
	}

	unsigned int start=0 , stop=0;
	bool res=KHotKeys::VoiceSignature::window(_sound,&start,&stop);
	p.setPen(QPen(QColor("blue"),1));
	if(res)
	{
		p.drawLine(start*width/length ,0,start*width/length  ,height);
		p.drawLine(stop*width/length ,0,stop*width/length  ,height);
	}
	else
	{
		p.drawLine(0 ,0,  width  ,height);
		p.drawLine(width ,0,  0  ,height);
	}

	p.end();

	label->setPixmap(pix);
	return res;
}


} // namespace KHotKeys

#include "voicerecorder.moc"