/
stream.cpp
333 lines (268 loc) · 7.87 KB
/
stream.cpp
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
// Music Room Interface
// --------------------
// stream.cpp - Wave streaming
// --------------------
// "©" Nmlgc, 2010-2011
#include "musicroom.h"
#include <FXFile.h>
#include <bgmlib/libvorbis.h>
#include "stream.h"
#include <bgmlib/packmethod.h>
#include <bgmlib/ui.h>
#ifdef WIN32
// Initialization
// --------------
Streamer::Streamer()
{
Active = false;
DS = NULL;
SB = NULL;
Write = 0;
ZeroMemory(&Fmt, sizeof(WAVEFORMATEX));
Track = NULL;
}
bool Streamer::Init(void* xid)
{
MMRESULT Ret;
BGMLib::UI_Stat("Initializing DirectSound streaming...\n");
// Fill wave format structure
Fmt.wFormatTag = WAVE_FORMAT_PCM;
Fmt.nChannels = 2;
Fmt.nSamplesPerSec = 44100;
Fmt.wBitsPerSample = 16;
Fmt.nBlockAlign = (Fmt.wBitsPerSample >> 3) * Fmt.nChannels;
Fmt.nAvgBytesPerSec = Fmt.nBlockAlign * Fmt.nSamplesPerSec;
Fmt.cbSize = 0;
Ret = DirectSoundCreate8(NULL, &DS, NULL);
if(Ret != DS_OK)
{
BGMLib::UI_Stat("WARNING: DirectSound device could not be opened.\nTrack playback won't be available.\n");
}
else Active = true;
Ret = DS->SetCooperativeLevel((HWND)xid, DSSCL_PRIORITY);
DSBUFFERDESC BD;
BD.dwSize = sizeof(DSBUFFERDESC);
BD.dwBufferBytes = BlockSize * BlockCount;
BD.dwFlags = DSBCAPS_LOCDEFER | DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS | DSBCAPS_STICKYFOCUS | DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLFREQUENCY;
BD.lpwfxFormat = &Fmt;
BD.guid3DAlgorithm = DS3DALG_DEFAULT;
BD.dwReserved = 0;
Ret = DS->CreateSoundBuffer(&BD, &SB, NULL);
ClearBuffer();
return Active;
}
FXint Streamer::run()
{
ulong Play, CurBE;
StopReq = false;
while(!StopReq)
{
sleep(TIMEOUT);
if(New)
{
if(!ActiveGame->Scanned) New = NULL;
SwitchTrack();
}
else if(Track && CurFile.isOpen())
{
SetVolume();
// Fuck notifications, I'm writing my own, TRANSPARENT system.
// We're streaming on each call, unless the new data would extend until after the current play cursor.
// Since the write cursor is always placed after the play cursor, this will, after starting the process,
// result in a "chase" situation, where the (usually faster) write cursor tries to catch up to the play cursor.
// Once the buffer end reached it, we wait until it passed again and then slowly fill up the bytes it just passed
// without intersecting.
// So it's basically a reversed version of the internal DirectSound method. And mine is way more sane, if you ask me.
SB->GetCurrentPosition(&Play, NULL);
CurBE = Write + BlockSize;
if( (CurBE > BufferSize) && Play < (Write - BlockSize)) Play += BufferSize;
if(!BETWEEN_EQUAL(Play, Write, CurBE))
{
if(StreamFrame(Write, BlockSize)) Write = CurBE;
if(Write > BufferSize) Write -= BufferSize;
}
}
}
StopReq = false;
detach();
return 1;
}
void Streamer::StreamFrame_WAV(char* Buffer, const ulong& Size)
{
Pos = pcm_read_bgm(CurFile, Buffer, Size, Track);
}
void Streamer::StreamFrame_OGG(char* Buffer, const ulong& Size)
{
Pos = ov_read_bgm(&SF, Buffer, Size, Track);
}
bool Streamer::StreamFrame(const ulong& Offset, const ulong& Size)
{
if(!ActiveGame) return false;
HRESULT Ret;
ulong ReadBufferSize[2];
char* ReadBuffer[2];
Ret = SB->Lock(Offset, Size, (void**)&ReadBuffer[0], &ReadBufferSize[0], (void**)&ReadBuffer[1], &ReadBufferSize[1], 0);
for(ushort b = 0; b < 2; b++)
{
if(ActiveGame->Vorbis) StreamFrame_OGG(ReadBuffer[b], ReadBufferSize[b]);
else StreamFrame_WAV(ReadBuffer[b], ReadBufferSize[b]);
}
Ret = SB->Unlock(ReadBuffer[0], ReadBufferSize[0], ReadBuffer[1], ReadBufferSize[1]);
return true;
}
bool Streamer::SwitchTrack_WAV(TrackInfo* NewTrack, FXString& NewFN)
{
FXuint NewFNHash = NewFN.hash();
if(!Track || !CurFile.isOpen() || (CurFNHash != NewFNHash) )
{
CloseFile();
if(!ActiveGame->OpenBGMFile(CurFile, NewTrack)) return false;
}
Pos = NewTrack->GetStart(FMT_BYTE, SilRem);
CurFile.position(Pos);
SB->SetFrequency(NewTrack->Freq);
CurFNHash = NewFNHash;
return true;
}
bool Streamer::SwitchTrack_OGG(TrackInfo* NewTrack, FXString& NewFN)
{
FXuint NewFNHash = NewFN.hash();
if(ActiveGame->CryptKind)
{
// Decrypt temporary play file from archive and load it
CloseFile();
if(!DumpDecrypt(ActiveGame, NewTrack, OggPlayFile)) return false;
CurFile.open(OggPlayFile, FXIO::Reading);
ov_open_callbacks(&CurFile, &SF, NULL, 0, OV_CALLBACKS_FXFILE);
}
else if(Track && CurFNHash == NewFNHash)
{
ov_pcm_seek(&SF, NewTrack->GetStart(FMT_SAMPLE, SilRem));
}
else
{
CloseFile();
if(!OpenVorbisBGM(CurFile, SF, ActiveGame, NewTrack)) return false;
}
SB->SetFrequency(NewTrack->Freq);
CurFNHash = NewFNHash;
return true;
}
// Always called by thread
bool Streamer::SwitchTrack()
{
bool Ret;
TrackInfo* Load = New;
// SB->Stop();
ClearBuffer();
if(!New)
{
Track = NULL;
return true;
}
do
{
if(Load != New) Load = New;
FXString NewFN = ActiveGame->DiskFN(Load);
if(ActiveGame->Vorbis) Ret = SwitchTrack_OGG(Load, NewFN);
else Ret = SwitchTrack_WAV(Load, NewFN);
}
while(Load != New); // ... yeah, maybe someone scrolls through very fast and already changed [New] to a different track than the one we loaded
if(!Ret)
{
New = NULL;
return Ret;
}
Track = Load;
New = Load = NULL;
// Initial load
// ------------
ulong DXWrite = 0, Play = 0, c = 1;
// That was the only case when it was still bugged!
while(Play == 0 || DXWrite == 0)
{
SB->SetCurrentPosition(c += 32); // Skip a bit ahead to reduce pops and clicks
SB->GetCurrentPosition(&Play, &DXWrite);
}
// StreamFrame(DXWrite, BlockSize);
Write = DXWrite + BlockSize;
if(Write > BufferSize) Write -= BufferSize;
// ------------
if(!StopReq) SB->Play(0, 0, DSBPLAY_LOOPING);
return Ret;
}
void Streamer::SetVolume()
{
long VolLog = log10f(MAX(Volume, 1)) * abs(DSBVOLUME_MIN / 2) + DSBVOLUME_MIN;
VolLog = MIN(VolLog, 0);
SB->SetVolume(VolLog);
}
void Streamer::RequestTrackSwitch(TrackInfo* NewTrack)
{
if(NewTrack != Track) New = NewTrack;
}
void Streamer::Play()
{
if(!New) SB->Play(0, 0, DSBPLAY_LOOPING); // Fixes game/track switching artifacts from the last song
if(!running()) start(); // (SwitchTrack will turn the stream back on in those cases)
StopReq = false;
}
void Streamer::Stop()
{
if(running()) StopReq = true;
if(SB) SB->Stop();
while(StopReq && !New)
{
sleep(TIMEOUT);
}
}
void Streamer::ClearBuffer()
{
char* b;
ulong s;
if(!Active) return;
SB->Lock(0, BufferSize, (void**)&b, &s, NULL, NULL, DSBLOCK_ENTIREBUFFER);
ZeroMemory(b, s);
SB->Unlock(b, s, NULL, NULL);
}
void Streamer::CloseFile()
{
if(CurFile.isOpen())
{
ov_clear(&SF);
CurFile.close();
Track = NULL;
}
FXFile::remove(OggPlayFile);
ClearBuffer(); // Necessary for game switches!
}
void Streamer::Exit()
{
if(!Active) return;
Stop();
CloseFile();
SB->Release();
DS->Release();
BGMLib::UI_Stat("Streamer closed.\n");
Active = false;
}
#endif
// Front end
// ---------
bool StreamerFront::Init(void* xid) {return Streamer::Inst().Init(xid);}
TrackInfo* StreamerFront::CurTrack() {return Streamer::Inst().Track;}
void StreamerFront::RequestTrackSwitch(TrackInfo* NewTrack) {return Streamer::Inst().RequestTrackSwitch(NewTrack);}
void StreamerFront::Play() {return Streamer::Inst().Play();}
void StreamerFront::Stop() {return Streamer::Inst().Stop();}
void StreamerFront::CloseFile() {return Streamer::Inst().CloseFile();}
void StreamerFront::Exit() {return Streamer::Inst().Exit();}
ulong StreamerFront::Pos()
{
Streamer& Str = Streamer::Inst();
if(!Str.Track) return 0;
ulong Ret = Str.Pos;
if(ActiveGame->Vorbis) Ret <<= 2;
if(Str.Track->FS == 0) Ret -= Str.Track->GetStart(FMT_BYTE, SilRem);
return Ret;
}
// ---------