228 lines
4.6 KiB
C++
228 lines
4.6 KiB
C++
#include <windows.h>
|
|
|
|
#include "TRACK.h"
|
|
#include "MEDIA.h"
|
|
#include "process.h"
|
|
|
|
|
|
void SaveDBGInfo(char *text);
|
|
extern HWND hWnd;
|
|
|
|
typedef struct SndFadeType_
|
|
{
|
|
int started; // -1: No Fade, 0: Fade not begin, 1: Fade begins
|
|
u32 type; // 1: Fade In, 2: Fade Out
|
|
DWORD BeginTime;
|
|
int Duration;
|
|
} SndFadeType;
|
|
|
|
unsigned __stdcall DSVolumeSoundThread(void *params);
|
|
|
|
|
|
|
|
BOOL DSSoundInit(HWND hWnd);
|
|
|
|
void *DSSoundCreate(char *name);
|
|
|
|
BOOL DSSoundPlay(void *sndObj, DWORD dwPriority = 0, DWORD dwFlags = 0, LONG lVolume = 0, LONG lFrequency = -1, LONG lPan = 0 );
|
|
BOOL DSSoundPause(void *sndObj);
|
|
BOOL DSSoundStop(void *sndObj);
|
|
|
|
float DSSoundGetDuration(void *sndObj);
|
|
float DSSoundGetTime(void *sndObj);
|
|
BOOL DSSoundSetTime(void *sndObj, float time);
|
|
|
|
BOOL DSSoundSetVolume(void *sndObj, long volume);
|
|
BOOL DSSoundSetFrequency(void *sndObj, float value);
|
|
BOOL DSSoundIsPlaying(void *sndObj);
|
|
void DSSoundDelete(void *sndObj);
|
|
|
|
|
|
class DSWAV_Grabber:public INPUT_IO
|
|
{
|
|
public:
|
|
void *g_pSound;
|
|
BOOL g_bBufferPaused;
|
|
|
|
double lastTime;
|
|
float volume;
|
|
|
|
SndFadeType sndFadeType;
|
|
|
|
int INIT (class WORLD*);
|
|
int FRAME (void);
|
|
int END (void);
|
|
IOTYPE GetType() {return IOtype_DSWAV;};
|
|
void SetData(int dataIdx, void *data);
|
|
};
|
|
|
|
|
|
BOOL DSWAV_Grabber::INIT(WORLD *pScene)
|
|
{
|
|
// init sound manager if necessary
|
|
|
|
lastTime =
|
|
dCurrentTime = 0.0f;
|
|
lTimeState = 0;
|
|
|
|
g_pSound = NULL;
|
|
g_bBufferPaused = 1;
|
|
sndFadeType.started = -1;
|
|
|
|
ulMediaParams = 0;
|
|
volume = 1.0f;
|
|
|
|
if (!DSSoundInit(hWnd))
|
|
return false;
|
|
|
|
g_pSound = DSSoundCreate((char *)MediaName);
|
|
if (!g_pSound)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
int DSWAV_Grabber::FRAME()
|
|
{
|
|
if (dCurrentTime != lastTime)
|
|
{
|
|
if (dCurrentTime == 0.0)
|
|
DSSoundStop(g_pSound);
|
|
else
|
|
DSSoundSetTime(g_pSound, dCurrentTime);
|
|
g_bBufferPaused = 1;
|
|
}
|
|
if (sndFadeType.started == -1)
|
|
{
|
|
switch (lTimeState)
|
|
{
|
|
case 0:
|
|
if (!g_bBufferPaused && DSSoundIsPlaying(g_pSound))
|
|
DSSoundPause(g_pSound);
|
|
break;
|
|
case 255:
|
|
if (!g_bBufferPaused && DSSoundIsPlaying(g_pSound))
|
|
DSSoundStop(g_pSound);
|
|
break;
|
|
case 1:
|
|
if (g_bBufferPaused && !DSSoundIsPlaying(g_pSound))
|
|
DSSoundPlay(g_pSound, 0, (ulMediaParams & 1) ? 1 : 0L);
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (sndFadeType.started == 0)
|
|
{
|
|
u32 threadID;
|
|
if (lTimeState == 1)
|
|
DSSoundPlay(g_pSound, 0, (ulMediaParams & 1) ? 1 : 0L, -10000);
|
|
_beginthreadex(NULL, 256, &DSVolumeSoundThread, (void *)this, 0, &threadID);
|
|
}
|
|
|
|
if (ulMediaParams & 2)
|
|
DSSoundSetVolume(g_pSound, -10000);
|
|
else
|
|
DSSoundSetVolume(g_pSound, -10000*(1-volume));
|
|
|
|
if (lTimeState != 1)
|
|
{
|
|
lTimeState = 0;
|
|
g_bBufferPaused = 1;
|
|
} else
|
|
{
|
|
if (!DSSoundIsPlaying(g_pSound))
|
|
dCurrentTime = dTotalTime;
|
|
else
|
|
dCurrentTime = DSSoundGetTime(g_pSound);
|
|
lastTime = dCurrentTime;
|
|
g_bBufferPaused = 0;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
void DSWAV_Grabber::SetData(int dataIdx, void *data)
|
|
{
|
|
switch (dataIdx)
|
|
{
|
|
case 0:
|
|
sndFadeType.type = *(int *)data;
|
|
sndFadeType.BeginTime = timeGetTime();
|
|
sndFadeType.started = 0;
|
|
break;
|
|
case 1:
|
|
sndFadeType.Duration = *(int *)data;
|
|
sndFadeType.BeginTime = timeGetTime();
|
|
sndFadeType.started = 0;
|
|
break;
|
|
case 2:
|
|
DSSoundSetFrequency(g_pSound, *(float *)data);
|
|
break;
|
|
case 3:
|
|
volume = *(float *)data;
|
|
volume = log(volume+1.0f)/log(2.0f);
|
|
DSSoundSetVolume(g_pSound, -10000*(1.0f-volume));
|
|
sndFadeType.started = -1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
int DSWAV_Grabber::END()
|
|
{
|
|
if (g_pSound)
|
|
DSSoundDelete(g_pSound);
|
|
g_pSound = NULL;
|
|
return 0;
|
|
}
|
|
|
|
|
|
INPUT_IO *DSWAV_CREATE(WORLD *scene, char *Filename)
|
|
{
|
|
u32 SX,SY,Col;
|
|
INPUT_IO *Ret;
|
|
Ret = (INPUT_IO *)new(DSWAV_Grabber);
|
|
SX = SY = Col = 0;
|
|
|
|
|
|
Ret->Image.SX = 0;
|
|
Ret->Image.SY = 0;
|
|
Ret->dAvgFramePerSecond = 30.0;
|
|
strcpy((char*)Ret->MediaName,Filename);
|
|
|
|
if (Ret->INIT(scene))
|
|
Ret->dTotalTime = DSSoundGetDuration(((DSWAV_Grabber *)Ret)->g_pSound);
|
|
|
|
|
|
return (INPUT_IO *)Ret;
|
|
};
|
|
|
|
|
|
unsigned __stdcall DSVolumeSoundThread(void *params)
|
|
{
|
|
DSWAV_Grabber *sndPtr = (DSWAV_Grabber *)params;
|
|
int newVolume;
|
|
double ratio;
|
|
DWORD curTime;
|
|
|
|
sndPtr->sndFadeType.started = 1;
|
|
while ((curTime = timeGetTime()) < sndPtr->sndFadeType.BeginTime + sndPtr->sndFadeType.Duration)
|
|
{
|
|
switch (sndPtr->sndFadeType.type)
|
|
{
|
|
case 1:
|
|
ratio = ((double)curTime - sndPtr->sndFadeType.BeginTime)/sndPtr->sndFadeType.Duration;
|
|
break;
|
|
case 2:
|
|
ratio = ((double)sndPtr->sndFadeType.Duration - curTime + sndPtr->sndFadeType.BeginTime)/sndPtr->sndFadeType.Duration;
|
|
break;
|
|
default:
|
|
return 0;
|
|
}
|
|
ratio = sqrt(ratio);
|
|
newVolume = -10000*(1-ratio);
|
|
DSSoundSetVolume(sndPtr->g_pSound, newVolume);
|
|
Sleep(5);
|
|
}
|
|
sndPtr->sndFadeType.started = -1;
|
|
return 0;
|
|
}
|