-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathPlaySoundThread.cpp
More file actions
141 lines (118 loc) · 3.4 KB
/
PlaySoundThread.cpp
File metadata and controls
141 lines (118 loc) · 3.4 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
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
/*
* Copyright (c) 2005-2018, BearWare.dk
*
* Contact Information:
*
* Bjoern D. Rasmussen
* Kirketoften 5
* DK-8260 Viby J
* Denmark
* Email: contact@bearware.dk
* Phone: +45 20 20 54 59
* Web: http://www.bearware.dk
*
* This source code is part of the TeamTalk SDK owned by
* BearWare.dk. Use of this file, or its compiled unit, requires a
* TeamTalk SDK License Key issued by BearWare.dk.
*
* The TeamTalk SDK License Agreement along with its Terms and
* Conditions are outlined in the file License.txt included with the
* TeamTalk SDK distribution.
*
*/
// PlaySoundThread.cpp : implementation file
//
#include "stdafx.h"
#include "PlaySoundThread.h"
#include <Mmsystem.h>
extern TTInstance* ttInst;
// CPlaySoundThread
IMPLEMENT_DYNCREATE(CPlaySoundThread, CWinThread)
CPlaySoundThread::CPlaySoundThread()
: m_handles()
{
}
CPlaySoundThread::~CPlaySoundThread()
{
}
void CPlaySoundThread::KillThread()
{
m_mutex.Lock();
while (m_SoundQueue.size())m_SoundQueue.pop();
m_mutex.Unlock();
::SetEvent(m_handles[KILL_EVENT]);
}
BOOL CPlaySoundThread::InitInstance()
{
//semaphore
m_handles[DATA_SEMAPHORE] = ::CreateSemaphore(NULL, 0, 25, NULL);
m_handles[KILL_EVENT] = ::CreateEvent(NULL, TRUE, FALSE, NULL);
return TRUE;
}
int CPlaySoundThread::ExitInstance()
{
::CloseHandle(m_handles[KILL_EVENT]);
::CloseHandle(m_handles[DATA_SEMAPHORE]);
return CWinThread::ExitInstance();
}
void CPlaySoundThread::AddSoundEvent(LPCTSTR szFilename, PlaybackMode mode, int sndVol)
{
m_mutex.Lock();
PlaybackFile pf = {};
pf.mode = mode;
pf.szFilename = szFilename;
pf.sndVol = sndVol;
m_SoundQueue.push(pf);
if (m_SoundQueue.size()>5)
{
m_SoundQueue.pop();
}
m_mutex.Unlock();
::ReleaseSemaphore(m_handles[DATA_SEMAPHORE], 1, NULL);
}
BEGIN_MESSAGE_MAP(CPlaySoundThread, CWinThread)
END_MESSAGE_MAP()
// CPlaySoundThread message handlers
int CPlaySoundThread::Run()
{
while(TRUE)
{
switch(::WaitForMultipleObjects(sizeof(m_handles)/sizeof(m_handles[0]), m_handles, FALSE, INFINITE))
{
case DATA_SEMAPHORE:
{
PlaybackFile pf = {};
m_mutex.Lock();
if (m_SoundQueue.size())
{
pf = m_SoundQueue.front();
m_SoundQueue.pop();
}
m_mutex.Unlock();
switch (pf.mode)
{
case PLAYBACKMODE_SYNC :
PlayWaveFile(pf.szFilename, FALSE);
break;
case PLAYBACKMODE_ASYNC :
PlayWaveFile(pf.szFilename, TRUE);
break;
case PLAYBACKMODE_TEAMTALK :
{
MediaFilePlayback mfp = {};
mfp.uOffsetMSec = TT_MEDIAPLAYBACK_OFFSET_IGNORE;
mfp.bPaused = FALSE;
mfp.audioPreprocessor = InitDefaultAudioPreprocessor(TEAMTALK_AUDIOPREPROCESSOR);
mfp.audioPreprocessor.ttpreprocessor.nGainLevel = RefGain(pf.sndVol);
auto inst = TT_InitLocalPlayback(ttInst, pf.szFilename, &mfp);
break;
}
}
break;
}
case KILL_EVENT:
default:
return ExitInstance();
}
}
}