1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
23 #define WIN32_LEAN_AND_MEAN 1
28 #include <MUtils/Sound.h>
29 #include <MUtils/OSSupport.h>
32 #include <QReadWriteLock>
38 ///////////////////////////////////////////////////////////////////////////////
40 ///////////////////////////////////////////////////////////////////////////////
42 bool MUtils::Sound::beep(const MUtils::Sound::beep_t
&beepType
)
46 case BEEP_NFO
: return MessageBeep(MB_ICONASTERISK
) == TRUE
; break;
47 case BEEP_WRN
: return MessageBeep(MB_ICONEXCLAMATION
) == TRUE
; break;
48 case BEEP_ERR
: return MessageBeep(MB_ICONHAND
) == TRUE
; break;
49 default: return false;
53 ///////////////////////////////////////////////////////////////////////////////
55 ///////////////////////////////////////////////////////////////////////////////
57 typedef QHash
<const QString
, const unsigned char*> SoundDB
;
59 static QReadWriteLock g_sound_lock
;
60 static QScopedPointer
<SoundDB
> g_sound_db
;
62 static const unsigned char *get_sound_from_cache(const QString
&name
)
64 //Try to look-up the sound in the cache first
65 QReadLocker
readLock(&g_sound_lock
);
66 if((!g_sound_db
.isNull()) && g_sound_db
->contains(name
))
68 return g_sound_db
->value(name
);
71 //Get the write lock now
73 QWriteLocker
writeLock(&g_sound_lock
);
75 //Is sound still not in cache?
76 if((!g_sound_db
.isNull()) && g_sound_db
->contains(name
))
78 return g_sound_db
->value(name
);
81 //If data not found in cache, try to load from resource!
82 QResource
resource(QString(":/sounds/%1.wav").arg(name
));
83 if(resource
.isValid())
85 if(const unsigned char *data
= resource
.data())
87 if(g_sound_db
.isNull())
89 g_sound_db
.reset(new SoundDB());
91 g_sound_db
->insert(name
, data
);
96 qWarning("Sound effect \"%s\" not found!", MUTILS_UTF8(name
));
100 bool MUtils::Sound::play_sound(const QString
&name
, const bool &bAsync
)
104 if(const unsigned char *data
= get_sound_from_cache(name
))
106 return PlaySound(LPCWSTR(data
), NULL
, (SND_MEMORY
| (bAsync
? SND_ASYNC
: SND_SYNC
))) != FALSE
;
113 bool MUtils::Sound::play_system_sound(const QString
&alias
, const bool &bAsync
)
115 return PlaySound(MUTILS_WCHR(alias
), GetModuleHandle(NULL
), (SND_ALIAS
| (bAsync
? SND_ASYNC
: SND_SYNC
))) != FALSE
;
118 bool MUtils::Sound::play_sound_file(const QString
&library
, const unsigned short uiSoundIdx
, const bool &bAsync
)
122 QFileInfo
libraryFile(library
);
123 if(!libraryFile
.isAbsolute())
125 const QString
&systemDir
= MUtils::OS::known_folder(MUtils::OS::FOLDER_SYSTEMFOLDER
);
126 if(!systemDir
.isEmpty())
128 libraryFile
.setFile(QDir(systemDir
), libraryFile
.fileName());
132 if(libraryFile
.exists() && libraryFile
.isFile())
134 if(const HMODULE module
= GetModuleHandleW(MUTILS_WCHR(QDir::toNativeSeparators(libraryFile
.canonicalFilePath()))))
136 result
= (PlaySound(MAKEINTRESOURCE(uiSoundIdx
), module
, (SND_RESOURCE
| (bAsync
? SND_ASYNC
: SND_SYNC
))) != FALSE
);
138 else if(const HMODULE module
= LoadLibraryW(MUTILS_WCHR(QDir::toNativeSeparators(libraryFile
.canonicalFilePath()))))
140 result
= (PlaySound(MAKEINTRESOURCE(uiSoundIdx
), module
, (SND_RESOURCE
| (bAsync
? SND_ASYNC
: SND_SYNC
))) != FALSE
);
146 qWarning("PlaySound: File \"%s\" could not be found!", MUTILS_UTF8(libraryFile
.absoluteFilePath()));
152 ///////////////////////////////////////////////////////////////////////////////