Commit 14b44676e462d47d3dc8793a8022a96754b832b2
1 parent
c19a6df603
Exists in
master
and in
2 other branches
음원 재생 루틴 개선
- 재생 전용 스레드 생성 - 클릭 음원 반응성 개선
Showing
2 changed files
with
92 additions
and
16 deletions
Show diff stats
app/gui/oven_control/soundplayer.cpp
1 | 1 | #include "soundplayer.h" |
2 | 2 | |
3 | -QMap<QString, QSound *> SoundPlayer::map; | |
4 | -QSound *SoundPlayer::current = 0; | |
3 | +namespace { | |
4 | +QThread playThread; | |
5 | +} | |
5 | 6 | |
6 | -void SoundPlayer::play(const QString &filename) | |
7 | +SoundPlayWorker::SoundPlayWorker() | |
8 | +{ | |
9 | + current = 0; | |
10 | + click = 0; | |
11 | +} | |
12 | + | |
13 | +void SoundPlayWorker::play(const QString &filename) | |
7 | 14 | { |
8 | 15 | if (current && !current->isFinished()) |
9 | 16 | current->stop(); |
... | ... | @@ -19,27 +26,77 @@ void SoundPlayer::play(const QString &filename) |
19 | 26 | current->play(); |
20 | 27 | } |
21 | 28 | |
29 | +void SoundPlayWorker::playClick() | |
30 | +{ | |
31 | + if (click == 0) | |
32 | + { | |
33 | + click = new QSoundEffect; | |
34 | + click->setSource(QUrl::fromLocalFile(":/sounds/button.wav")); | |
35 | + click->setVolume(1.0); | |
36 | + } | |
37 | + | |
38 | + click->play(); | |
39 | +} | |
40 | + | |
41 | +SoundPlayer *SoundPlayer::instance = 0; | |
42 | +SoundPlayer::SoundPlayer() | |
43 | +{ | |
44 | + instance = this; | |
45 | + | |
46 | + SoundPlayWorker *w = new SoundPlayWorker; | |
47 | + w->moveToThread(&playThread); | |
48 | + connect(this, SIGNAL(operate(QString)), w, SLOT(play(QString))); | |
49 | + connect(this, SIGNAL(click()), w, SLOT(playClick())); | |
50 | + | |
51 | + playThread.start(); | |
52 | +}z | |
53 | + | |
54 | +void SoundPlayer::play(const QString &filename) | |
55 | +{ | |
56 | + emit operate(filename); | |
57 | +} | |
58 | + | |
59 | +void SoundPlayer::emitClick() | |
60 | +{ | |
61 | + emit click(); | |
62 | +} | |
63 | + | |
22 | 64 | void SoundPlayer::playClick() |
23 | 65 | { |
24 | - play(":/sounds/button.wav"); | |
66 | + if (instance == 0) | |
67 | + instance = new SoundPlayer; | |
68 | + | |
69 | + instance->emitClick(); | |
25 | 70 | } |
26 | 71 | |
27 | 72 | void SoundPlayer::playStart() |
28 | 73 | { |
29 | - play(":/sounds/start.wav"); | |
74 | + if (instance == 0) | |
75 | + instance = new SoundPlayer; | |
76 | + | |
77 | + instance->play(":/sounds/start.wav"); | |
30 | 78 | } |
31 | 79 | |
32 | 80 | void SoundPlayer::playStop() |
33 | 81 | { |
34 | - play(":/sounds/stop.wav"); | |
82 | + if (instance == 0) | |
83 | + instance = new SoundPlayer; | |
84 | + | |
85 | + instance->play(":/sounds/stop.wav"); | |
35 | 86 | } |
36 | 87 | |
37 | 88 | void SoundPlayer::playError1() |
38 | 89 | { |
39 | - play(":/sounds/error1.wav"); | |
90 | + if (instance == 0) | |
91 | + instance = new SoundPlayer; | |
92 | + | |
93 | + instance->play(":/sounds/error1.wav"); | |
40 | 94 | } |
41 | 95 | |
42 | 96 | void SoundPlayer::playError2() |
43 | 97 | { |
44 | - play(":/sounds/error2.wav"); | |
98 | + if (instance == 0) | |
99 | + instance = new SoundPlayer; | |
100 | + | |
101 | + instance->play(":/sounds/error2.wav"); | |
45 | 102 | } | ... | ... |
app/gui/oven_control/soundplayer.h
... | ... | @@ -3,13 +3,37 @@ |
3 | 3 | |
4 | 4 | #include <QObject> |
5 | 5 | #include <QSound> |
6 | +#include <QSoundEffect> | |
7 | +#include <QThread> | |
6 | 8 | #include <QMap> |
7 | 9 | |
10 | +class SoundPlayWorker : public QObject | |
11 | +{ | |
12 | + Q_OBJECT | |
13 | +public: | |
14 | + explicit SoundPlayWorker(); | |
15 | + | |
16 | +public slots: | |
17 | + void play(const QString &filename); | |
18 | + void playClick(); | |
19 | + | |
20 | +private: | |
21 | + QMap<QString, QSound *> map; | |
22 | + QSound *current; | |
23 | + QSoundEffect *click; | |
24 | +}; | |
25 | + | |
8 | 26 | class SoundPlayer : public QObject |
9 | 27 | { |
10 | 28 | Q_OBJECT |
29 | + | |
30 | + explicit SoundPlayer(); | |
31 | + void play(const QString &filename); | |
32 | + void emitClick(); | |
33 | + | |
34 | + static SoundPlayer *instance; | |
35 | + | |
11 | 36 | public: |
12 | - static void play(const QString &filename); | |
13 | 37 | static void playClick(); |
14 | 38 | static void playStart(); |
15 | 39 | static void playStop(); |
... | ... | @@ -17,13 +41,8 @@ public: |
17 | 41 | static void playError2(); |
18 | 42 | |
19 | 43 | signals: |
20 | - | |
21 | -public slots: | |
22 | - | |
23 | -private: | |
24 | - static QMap<QString, QSound *> map; | |
25 | - static QSound *current; | |
26 | - | |
44 | + void operate(const QString &); | |
45 | + void click(); | |
27 | 46 | }; |
28 | 47 | |
29 | 48 | #endif // SOUNDPLAYER_H | ... | ... |