-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeechworker.cpp
More file actions
92 lines (73 loc) · 1.53 KB
/
Copy pathspeechworker.cpp
File metadata and controls
92 lines (73 loc) · 1.53 KB
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
#include "speechworker.h"
#include <QDebug>
#ifndef NO_MULTIMEDIA
#include <QAudioOutput>
#include <QBuffer>
constexpr int bit_to_byte(int bit_length)
{
return bit_length / 8;
}
template <int SampleSize>
int PosToTime(int pos)
{
return pos / bit_to_byte(SampleSize);
}
template <int SampleSize>
int TimeToPos(int time)
{
return time * bit_to_byte(SampleSize);
}
SpeechWorker::SpeechWorker(const QByteArray &data, const QAudioFormat &format) :
QObject(0),
data(data),
output(new QAudioOutput(format)),
buffer(new QBuffer(&this->data)),
timer()
{
this->buffer->open(QIODevice::ReadOnly);
connect(&this->timer, SIGNAL(timeout()), this, SLOT(timer_timeout()));
}
SpeechWorker::~SpeechWorker()
{
}
void SpeechWorker::start()
{
if ( this->data.isEmpty() ) {
return;
}
emit this->ready();
}
QAudio::State SpeechWorker::state() const
{
return this->output->state();
}
int SpeechWorker::size() const
{
return PosToTime<SAMPLE_SIZE>(this->data.size());
}
void SpeechWorker::play()
{
this->output->start(this->buffer.data());
this->timer.start(20);
}
void SpeechWorker::pause()
{
this->timer.stop();
this->output->stop();
}
void SpeechWorker::stop()
{
this->timer.stop();
this->output->stop();
this->buffer->seek(0);
emit this->tick(0);
}
void SpeechWorker::seek(int time)
{
this->buffer->seek(TimeToPos<SAMPLE_SIZE>(time));
}
void SpeechWorker::timer_timeout()
{
emit this->tick(PosToTime<SAMPLE_SIZE>(this->buffer->pos()));
}
#endif // NO_MULTIMEDIA