summaryrefslogtreecommitdiff
path: root/src/T2DLL/T2SoundPlayer.cpp
blob: 8c06ec9c01695a59010b5c7ea0020b9ed34a9c25 (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#include "GlobalFunc.h"
#include "T2SoundPlayer.h"
#include "T2TowerDoc.h"
#include "T2TowerMainView.h"
#include "Wave.h"

BEGIN_MESSAGE_MAP(T2SoundPlayer, CWnd)
    ON_MESSAGE(MM_MCINOTIFY, OnMCINotify)
END_MESSAGE_MAP()

T2SoundPlayer::T2SoundPlayer(CWnd* inParentWnd, IDirectSound* inDirectSound) {
    mDirectSound = inDirectSound;
    mItemList = new T2SoundObjItemList;
    mCurrentCDTrack = 0;

    MCI_OPEN_PARMS mciParms;
    memset(&mciParms, 0, sizeof(mciParms));

    mciParms.lpstrDeviceType = (LPSTR) MCI_DEVTYPE_CD_AUDIO;

    CString theName;
    theName.Format("%s:", GetInstallSourceDrive());
    CString theAlias;
    theAlias = "T2CD";

    mciParms.lpstrElementName = theName;
    mciParms.lpstrAlias = theAlias;

    mciSendCommand(0, MCI_OPEN, MCI_WAIT | MCI_OPEN_SHAREABLE | MCI_OPEN_ELEMENT | MCI_OPEN_ALIAS | MCI_OPEN_TYPE_ID | MCI_OPEN_TYPE, (DWORD) &mciParms);

    CRect rect(0, 0, 1, 1);
    CString theClass = AfxRegisterWndClass(CS_NOCLOSE);

    Create(theClass, "", WS_CHILD, rect, inParentWnd, 9999);

    mSEMask = 0xFFFFFFFF;
    mIsSoundOn = true;
    mIsFadeOut = false;
}

/*virtual*/ T2SoundPlayer::~T2SoundPlayer() {
    StopCD();

    CString cmd;
    cmd.Format("close %s", "T2CD");
    mciSendString(cmd, NULL, 0, NULL);

    POSITION pos = mItemList->GetHeadPosition();
    while (pos) {
        T2SoundObjItem *item = mItemList->GetNext(pos);
        DeleteSound(item->mName);
    }

    delete mItemList;
}

void T2SoundPlayer::AddSound(const CString& inName, SOUNDPRIORITY inPriority, const CString& inPath) {
    if (!mDirectSound)
        return;

    T2SoundObjItem *item = new T2SoundObjItem;
    CWave wave(inPath);

    if (LoadSound(item, inName, wave, false)) {
        item->mSourceKind = T2SoundObjItem::FileSource;
        item->mPath = inPath;
        item->mPriority = inPriority;
        mItemList->AddTail(item);
    } else {
        delete item;
    }
}

void T2SoundPlayer::AddSound(const CString& inName, SOUNDPRIORITY inPriority, unsigned int inResID, HINSTANCE inModule) {
    if (!mDirectSound)
        return;

    if (FindResource(inModule, MAKEINTRESOURCE(inResID), "WAVE")) {
        T2SoundObjItem *item = new T2SoundObjItem;
        CWave wave(inResID, inModule);

        if (LoadSound(item, inName, wave, false)) {
            item->mSourceKind = T2SoundObjItem::ResSource;
            item->mResID = inResID;
            item->mModuleHandle = inModule;
            item->mPriority = inPriority;
            mItemList->AddTail(item);
        } else {
            delete item;
        }
    }
}

BOOL T2SoundPlayer::LoadSound(T2SoundObjItem* inItem, const CString& inName, CWave& inWave, BOOL inReloadFlag) {
    if (!inReloadFlag) {
        POSITION pos = mItemList->GetHeadPosition();
        while (pos) {
            T2SoundObjItem *item = mItemList->GetNext(pos);
            if (item->mName == inName)
                return false;
        }
    }

    inItem->mName = inName;

    BYTE *waveDataPtr;
    DWORD waveDataLength = inWave.GetDataLen();

    WAVEFORMATEX waveFormat;
    inWave.GetFormat(waveFormat);

    DSBUFFERDESC desc;
    memset(&desc, 0, sizeof(DSBUFFERDESC));
    desc.dwSize = sizeof(DSBUFFERDESC);
    desc.dwFlags = DSBCAPS_STATIC | DSBCAPS_LOCSOFTWARE | DSBCAPS_CTRLDEFAULT;
    desc.dwBufferBytes = waveDataLength;
    desc.lpwfxFormat = &waveFormat;

    mDirectSound->CreateSoundBuffer(&desc, &inItem->mDSBuffers[0], NULL);

    inItem->mDSBuffers[0]->Lock(0, waveDataLength, &waveDataPtr, &waveDataLength, NULL, NULL, 0);
    waveDataLength = inWave.GetData(waveDataPtr, waveDataLength);
    inItem->mDSBuffers[0]->Unlock(waveDataPtr, waveDataLength, NULL, 0);

    inItem->mPlayedAt[0] = GetTickCount();

    for (int i = 1; i < 4; i++) {
        mDirectSound->DuplicateSoundBuffer(inItem->mDSBuffers[0], &inItem->mDSBuffers[i]);
        inItem->mPlayedAt[i] = GetTickCount();
    }

    return true;
}

void T2SoundPlayer::DeleteSound(const CString& inName) {
    if (!mDirectSound)
        return;

    POSITION pos = mItemList->GetHeadPosition();
    while (pos) {
        POSITION prev = pos;
        T2SoundObjItem *item = mItemList->GetNext(pos);

        if (item->mName == inName) {
            mItemList->RemoveAt(prev);
            for (int i = 3; i >= 0; i--) {
                if (item->mDSBuffers[i])
                    item->mDSBuffers[i]->Release();
            }

            delete item;
            break;
        }
    }
}

void T2SoundPlayer::DeleteSoundAll() {
    if (!mDirectSound)
        return;

    POSITION pos = mItemList->GetHeadPosition();
    while (pos) {
        T2SoundObjItem *item = mItemList->GetNext(pos);
        DeleteSound(item->mName);
    }

    mItemList->RemoveAll();
}

void T2SoundPlayer::Play(const CString& inName, unsigned int inMask, unsigned int inFlags, POINT* inPt, PLAYMODE inPlayMode, int inVolume) {
    if (!mDirectSound)
        return;

    if (!mIsSoundOn || (inMask & mSEMask) == 0)
        return;

    POSITION pos = mItemList->GetHeadPosition();
    while (pos) {
        T2SoundObjItem *item = mItemList->GetNext(pos);
        if (item->mName == inName) {
            DWORD currentStatus;
            int index;

            if (inPlayMode == PlayMode_0 || inPlayMode == PlayMode_1 || inPlayMode == PlayMode_3) {
                index = 0;
            } else if (inPlayMode == PlayMode_2) {
                for (index = 0; index < 4; index++) {
                    item->mDSBuffers[index]->GetStatus(&currentStatus);
                    if (!(currentStatus & DSBSTATUS_PLAYING))
                        break;
                }

                if (index == 4) {
                    // all slots full, pick the oldest sound to replace
                    DWORD oldest = GetTickCount();
                    index = 0;
                    for (int i = 0; i < 4; i++) {
                        if (item->mPlayedAt[i] < oldest) {
                            index = i;
                            oldest = item->mPlayedAt[i];
                        }
                    }
                }
            }

            item->mDSBuffers[index]->GetStatus(&currentStatus);

            if (currentStatus & DSBSTATUS_BUFFERLOST) {
                for (int i = 3; i >= 0; i--)
                    item->mDSBuffers[i]->Restore();

                CWave *wave;
                if (item->mSourceKind == T2SoundObjItem::FileSource)
                    wave = new CWave(item->mPath);
                else
                    wave = new CWave(item->mResID, item->mModuleHandle);

                LoadSound(item, item->mName, *wave, true);
            }

            int pan = 0;
            if (inPt) {
                CRect rect;
                GetCurrentT2TowerDoc()->GetTowerMainView()->tmv_vf140(rect);

                POINT centerPt = rect.CenterPoint();
                float x = ((float) (inPt->x - centerPt.x) / (float) ((rect.right - rect.left) / 2));
                x *= 0.5;
                if (x < 0.0)
                    pan = -10000.0f * -x;
                else
                    pan = 10000.0f * x;
            }

            BOOL play = false;
            if (inPlayMode == PlayMode_0 || inPlayMode == PlayMode_3) {
                if (!(currentStatus & DSBSTATUS_PLAYING))
                    play = true;
            } else if (inPlayMode == PlayMode_1 || inPlayMode == PlayMode_2) {
                if (currentStatus & DSBSTATUS_PLAYING) {
                    item->mDSBuffers[index]->Stop();
                    item->mDSBuffers[index]->SetCurrentPosition(0);
                }
                play = true;
            }

            if (play) {
                int volume = ((inVolume * 10000) / 100) - 10000;
                if (!mIsFadeOut || (inFlags & SoundFlags_10000))
                    item->mDSBuffers[index]->SetVolume(volume);
                else
                    item->mDSBuffers[index]->SetVolume(-10000);

                item->mDSBuffers[index]->SetPan(pan);
                item->mDSBuffers[index]->Play(0, 0, inPlayMode == PlayMode_3);
            }

            item->mPlayedAt[index] = GetTickCount();
            break;
        }
    }
}

void T2SoundPlayer::Stop(const CString& inName) {
    if (!mDirectSound)
        return;

    POSITION pos = mItemList->GetHeadPosition();
    while (pos) {
        T2SoundObjItem *item = mItemList->GetNext(pos);

        if (item->mName == inName) {
            for (int index = 0; index < 4; index++) {
                DWORD currentStatus;
                item->mDSBuffers[index]->GetStatus(&currentStatus);

                if (currentStatus & DSBSTATUS_PLAYING) {
                    item->mDSBuffers[index]->Stop();
                    item->mDSBuffers[index]->SetCurrentPosition(0);
                }
            }
        }
    }
}

void T2SoundPlayer::StopAll() {
    if (!mDirectSound)
        return;

    POSITION pos = mItemList->GetHeadPosition();
    while (pos) {
        T2SoundObjItem *item = mItemList->GetNext(pos);

        for (int index = 0; index < 4; index++) {
            DWORD currentStatus;
            item->mDSBuffers[index]->GetStatus(&currentStatus);

            if (currentStatus & DSBSTATUS_PLAYING) {
                item->mDSBuffers[index]->Stop();
                item->mDSBuffers[index]->SetCurrentPosition(0);
            }
        }
    }
}

void T2SoundPlayer::SetVolume(const CString& inName, int inVolume) {
    if (!mDirectSound)
        return;

    int volume = ((inVolume * 10000) / 100) - 10000;
    POSITION pos = mItemList->GetHeadPosition();
    while (pos) {
        T2SoundObjItem *item = mItemList->GetNext(pos);

        if (item->mName == inName) {
            for (int index = 0; index < 4; index++) {
                DWORD currentStatus;
                item->mDSBuffers[index]->GetStatus(&currentStatus);

                if (currentStatus & DSBSTATUS_PLAYING)
                    item->mDSBuffers[index]->SetVolume(volume);
            }
        }
    }
}

void T2SoundPlayer::SetSoundOn(BOOL inSoundOn) {
    mIsSoundOn = inSoundOn;

    if (!inSoundOn) {
        POSITION pos = mItemList->GetHeadPosition();
        while (pos) {
            T2SoundObjItem *item = mItemList->GetNext(pos);
            DWORD currentStatus;

            for (int index = 0; index < 4; index++) {
                item->mDSBuffers[index]->GetStatus(&currentStatus);

                if (currentStatus & DSBSTATUS_PLAYING) {
                    item->mDSBuffers[index]->Stop();
                    item->mDSBuffers[index]->SetCurrentPosition(0);
                }
            }
        }
    }
}

void T2SoundPlayer::FadeOut() {
    mIsFadeOut = true;

    DWORD time = GetTickCount();
    while (GetTickCount() == time) {}

    int i = 0;
    while (i <= 10) {
        int volume = 0 - ((i * 10000) / 10);
        POSITION pos = mItemList->GetHeadPosition();
        while (pos) {
            T2SoundObjItem *item = mItemList->GetNext(pos);
            DWORD currentStatus;

            for (int index = 0; index < 4; index++) {
                item->mDSBuffers[index]->GetStatus(&currentStatus);

                if (currentStatus & DSBSTATUS_PLAYING)
                    item->mDSBuffers[index]->SetVolume(volume);
            }
        }

        time = GetTickCount();
        while ((GetTickCount() - time) < 50) {
            MSG msg;
            PeekMessage(&msg, NULL, 0, 0, 0);
        }

        i++;
    }
}

void T2SoundPlayer::FadeIn() {
    mIsFadeOut = false;

    DWORD time = GetTickCount();
    while (GetTickCount() == time) {}

    int i = 0;
    while (i <= 10) {
        int volume = ((i * 10000) / 10) - 10000;
        POSITION pos = mItemList->GetHeadPosition();
        while (pos) {
            T2SoundObjItem *item = mItemList->GetNext(pos);
            DWORD currentStatus;

            for (int index = 0; index < 4; index++) {
                item->mDSBuffers[index]->GetStatus(&currentStatus);

                if (currentStatus & DSBSTATUS_PLAYING)
                    item->mDSBuffers[index]->SetVolume(volume);
            }
        }

        time = GetTickCount();
        while ((GetTickCount() - time) < 50) {
            MSG msg;
            PeekMessage(&msg, NULL, 0, 0, 0);
        }

        i++;
    }
}

void T2SoundPlayer::PlayCDTrack(int inTrack, BOOL inFlag) {
    if (inFlag)
        mCurrentCDTrack = inTrack;
    else
        mCurrentCDTrack = 0;

    CString cmd;
    cmd.Format("status %s length track %d", "T2CD", inTrack);

    char returnString[20];
    MCIERROR err = mciSendString(cmd, returnString, 20, NULL);

    cmd.Format("play %s from %02d:00:00:00 to %02d:%s notify", "T2CD", inTrack, inTrack, returnString);
    err = mciSendString(cmd, NULL, 0, *this);
}

void T2SoundPlayer::StopCD() {
    mCurrentCDTrack = 0;

    CString cmd;
    cmd.Format("stop %s", "T2CD");
    mciSendString(cmd, NULL, 0, NULL);
}

LRESULT T2SoundPlayer::OnMCINotify(WPARAM wParam, LPARAM lParam) {
    if (wParam == MCI_NOTIFY_SUCCESSFUL || wParam == MCI_NOTIFY_ABORTED) {
        if (mCurrentCDTrack)
            PlayCDTrack(mCurrentCDTrack, true);
    }

    return 0;
}



T2SoundObjItem::T2SoundObjItem() {
    mName = "";

    for (int i = 0; i < 4; i++) {
        mDSBuffers[i] = NULL;
        mPlayedAt[i] = GetTickCount();
    }
}

T2SoundObjItem::~T2SoundObjItem() {
}



T2SoundObjItemList::T2SoundObjItemList() {
}

/*virtual*/ T2SoundObjItemList::~T2SoundObjItemList() {
}