From f5c31a605354cb3d4a1aa79e904887b097f5f306 Mon Sep 17 00:00:00 2001 From: Kermit Mei Date: Tue, 31 Mar 2009 23:02:56 +0800 Subject: [PATCH] Improve the windows respond by using thread --- src/CMakeLists.txt | 4 ++++ src/aplay/CMakeLists.txt | 24 ++++++++++++++++++++++++ src/aplay/aplay.c | 16 ++++++++++++++++ src/aplay/aplay.ico | Bin 0 -> 8566 bytes src/aplay/aplay.rc | 1 + src/gui/CMakeLists.txt | 5 +++++ src/gui/Speaker.cpp | 24 ++++++++++++++---------- src/gui/WinSpeaker.cpp | 26 ++++++++++++++++++++++++++ src/gui/WinSpeaker.h | 22 ++++++++++++++++++++++ src/gui/myapp.rc | 1 - 10 files changed, 112 insertions(+), 11 deletions(-) create mode 100644 src/aplay/CMakeLists.txt create mode 100644 src/aplay/aplay.c create mode 100644 src/aplay/aplay.ico create mode 100644 src/aplay/aplay.rc create mode 100644 src/gui/WinSpeaker.cpp create mode 100644 src/gui/WinSpeaker.h delete mode 100644 src/gui/myapp.rc diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a37f8c7..2f45a2e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,9 @@ # Recurse into the "core" and "ui" subdirectories. ADD_SUBDIRECTORY(core) ADD_SUBDIRECTORY(gui) +IF(WIN32) + ADD_SUBDIRECTORY(aplay) +ENDIF(WIN32) #ADD_SUBDIRECTORY(test) # Make sure the compiler can find include files from our ui library. @@ -32,6 +35,7 @@ SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin) SET(qfrt_EXE_SRCS qfrt.cpp) IF(WIN32) + IF(MINGW) # resource compilation for mingw ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/qfrt_rc.o diff --git a/src/aplay/CMakeLists.txt b/src/aplay/CMakeLists.txt new file mode 100644 index 0000000..6675582 --- /dev/null +++ b/src/aplay/CMakeLists.txt @@ -0,0 +1,24 @@ +SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin) +SET(aplay_EXE_SRCS + aplay.c +) + +ADD_DEFINITIONS(-mwindows -O4) + +if(WIN32) + if(MINGW) + # resource compilation for mingw + ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/aplay_rc.o + COMMAND windres.exe -I${CMAKE_CURRENT_SOURCE_DIR} + -i${CMAKE_CURRENT_SOURCE_DIR}/aplay.rc + -o ${CMAKE_CURRENT_BINARY_DIR}/aplay_rc.o) + SET(aplay_EXE_SRCS ${aplay_EXE_SRCS} ${CMAKE_CURRENT_BINARY_DIR}/aplay_rc.o) + else(MINGW) + SET(aplay_EXE_SRCS ${aplay_EXE_SRCS} aplay.rc) + endif(MINGW) +endif(WIN32) + +# sim executable + +ADD_EXECUTABLE(aplay WIN32 ${aplay_EXE_SRCS}) +TARGET_LINK_LIBRARIES(aplay winmm) diff --git a/src/aplay/aplay.c b/src/aplay/aplay.c new file mode 100644 index 0000000..725327a --- /dev/null +++ b/src/aplay/aplay.c @@ -0,0 +1,16 @@ +// compiling with mingw: gcc -mwindows playsound.c -lwinmm -o playsound.exe +// strip playsound.exe +#include +#include + +int main(int argc, char* argv[]) { + if (argc == 1) { + printf ("%s \n", argv[0]); + exit(0); + } + + if (!PlaySound(argv[1], 0, SND_FILENAME)) { + printf ("error playing file\n"); + } + return 0; +} diff --git a/src/aplay/aplay.ico b/src/aplay/aplay.ico new file mode 100644 index 0000000000000000000000000000000000000000..a78f19303cbe8f6ea163fca732402c7bec9cd693 GIT binary patch literal 8566 zcwX&Tc~Dj76~AawyUnznwwXyglbBAeafwlh8LdV%YTCHOB@t9mHbGGl0Z|c@eUU{l zAYqFvqJVJ$0a*n^F@{BCQ`zAGZ{Obo9%%gad{3D%4oEZ!{?l*f%)R%$d%xe=&-o6= z8E_N0$&)#{PUh17$Z`MTxF0;-1Ri47Ez$L}Z<3Qi)EaWW#2NTk0G|IY5m!WxOxDix=qYiCRA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2y}(UjK(Z5?Q-Z$MpDHR7Ybg5jJw zm^*tmW)p4LumLBj2Z$hhFf1nfN~#fNW@dUjFu57AdgVZ|!mtAZ0-)3B6urH@Q-*uU zXYs!u&{=mjwN$y-??qyCj6Po$=jEg5N-lO&1!moWVmP-DBdE zPpI<=l|CLLOd@)l$Y=!eVC4CjXj=nx(zfuN1e6-#$IdB!%6i_MVytg}j`d^b6l3l4 zGV4X>6l3l8qU&Ym6l3lAW7kiaQ;fCu^VZKbrx*+8+4W1zDaOJaUccm=;(55^F{c;} zd%WfpBVmvCoZ?yh@t#vWjr}U-6hFit|2f4l_N$sx4CBA5IR(T1|D590%qf`eU&Wl_ z>6jbuIR*F38t*y9v-smVr+5~dt?`^wjD$a4bBd9$*&44o#c25BHK!O2f4t@tqv4Oo qoMJTWr`Invrx*+O=`}p37=He#bBgC-y~ui*ImK9e4zE`|r}!T&F53+N literal 0 HcwPel00001 diff --git a/src/aplay/aplay.rc b/src/aplay/aplay.rc new file mode 100644 index 0000000..3b5f785 --- /dev/null +++ b/src/aplay/aplay.rc @@ -0,0 +1 @@ +IDI_ICON1 ICON DISCARDABLE "aplay.ico" diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 6ed515c..e08a3b3 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -66,6 +66,11 @@ SET( FRGUI_MOC_HDRS ResultWidget.h ) +IF(WIN32) + SET( FRGUI_MOC_HDRS ${FRGUI_MOC_HDRS} WinSpeaker.h ) + SET( FRGUI_SRCS ${FRGUI_SRCS} WinSpeaker.cpp ) +ENDIF(WIN32) + # some .ui files SET( FRGUI_UIS AddDialog.ui diff --git a/src/gui/Speaker.cpp b/src/gui/Speaker.cpp index e33458c..d53909a 100644 --- a/src/gui/Speaker.cpp +++ b/src/gui/Speaker.cpp @@ -1,20 +1,18 @@ -#ifdef WIN32 -extern "C" { -#include -#include -} -#endif - #include #include #include #include #include - #include #include #include "Speaker.h" +#ifdef WIN32 +#include +#include "WinSpeaker.h" +WinSpeaker winSpeaker; +#endif //WIN32 + using freeRecite::configHolder; namespace Speaker { @@ -28,7 +26,7 @@ void speak(const QString &word) { if(!finfo.exists()) return; #ifdef WIN32 - PlaySound(finfo.absoluteFilePath().toStdString().c_str(), 0, SND_FILENAME); + winSpeaker.speak(finfo.absoluteFilePath().toStdString()); #else QProcess::startDetached("aplay", QStringList(finfo.absoluteFilePath())); @@ -41,7 +39,13 @@ void play(const QString &sound) { std::string fileName = configHolder.musicDir() + sound.toStdString() + ".wav"; #ifdef WIN32 - PlaySound(fileName.c_str(), 0, SND_FILENAME); + //If thread can't play it, then paly in a new process! + if(!winSpeaker.speak(fileName) && + (sound == "type" || sound == "ok" || sound == "fail") ) { + QProcess::startDetached("aplay", + QStringList(fileName.c_str())); + qApp->processEvents(); + } #else QProcess::startDetached("aplay", QStringList(fileName.c_str())); diff --git a/src/gui/WinSpeaker.cpp b/src/gui/WinSpeaker.cpp new file mode 100644 index 0000000..0ad493b --- /dev/null +++ b/src/gui/WinSpeaker.cpp @@ -0,0 +1,26 @@ +#include +#include +#include "WinSpeaker.h" + +//By default, mutex should be nonrecursive. +QMutex WinSpeaker::mutex(QMutex::NonRecursive); + +WinSpeaker::WinSpeaker(QObject *parent) + : QThread(parent) { + /*Do Nothing Here!*/ +} + +bool WinSpeaker::speak(const std::string &wordFile) { + if(!mutex.tryLock()){ + return false; + }else { + word = wordFile; + start(); + return true; + } +} + +void WinSpeaker::run() { + PlaySound(word.c_str(), 0, SND_FILENAME); + mutex.unlock(); +} diff --git a/src/gui/WinSpeaker.h b/src/gui/WinSpeaker.h new file mode 100644 index 0000000..e117ead --- /dev/null +++ b/src/gui/WinSpeaker.h @@ -0,0 +1,22 @@ +#ifndef WINSPEAK_H_ +#define WINSPEAK_H_ + +#include +#include +#include + +class WinSpeaker : public QThread +{ + Q_OBJECT +public: + WinSpeaker(QObject *parent = 0); + virtual ~WinSpeaker() + { /* Do Nothing Here */ } + bool speak(const std::string &wordFile); +protected: + virtual void run(); + std::string word; + static QMutex mutex; +}; + +#endif //WINSPEAK_H_ diff --git a/src/gui/myapp.rc b/src/gui/myapp.rc deleted file mode 100644 index fc26ba2..0000000 --- a/src/gui/myapp.rc +++ /dev/null @@ -1 +0,0 @@ -IDI_ICON1 ICON DISCARDABLE "myapp.ico" \ No newline at end of file -- 2.11.4.GIT