Skip to content

Commit

Permalink
TONY: Make FPSOUND work in ScummVM
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Hoops committed Jun 9, 2012
1 parent 05340fa commit 1668a23
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 133 deletions.
121 changes: 11 additions & 110 deletions engines/tony/sound.cpp
Expand Up @@ -26,6 +26,7 @@
* Copyright (c) 1997-2003 Nayma Software
*/

#include "audio/mixer.h"
#include "common/textconsole.h"
#include "tony/game.h"
#include "tony/tony.h"
Expand All @@ -52,109 +53,24 @@ namespace Tony {
\****************************************************************************/

FPSOUND::FPSOUND() {
lpDS = NULL;
lpDSBPrimary = NULL;
hwnd = 0;
bSoundSupported = false;
}


/****************************************************************************\
*
* Function: bool FPSOUND::Init(HWND hWnd);
* Function: bool FPSOUND::Init();
*
* Description: Inizializza l'oggetto, e prepara tutto il necessario per
* creare stream e effetti sonori.
*
* Input: HWND hWnd Handle della finestra principale
*
* Return: True se tutto OK, false in caso di errore.
*
\****************************************************************************/

bool FPSOUND::Init(/*HWND hWnd*/) {
#ifdef REFACTOR_ME
HRESULT err;
static DSBUFFERDESC dsbdesc;
static PCMWAVEFORMAT pcmwf;
static char errbuf[128];

/* Salva l'handle della finestra nella variabile globale. DirectSound ha
bisogno dell'handle per effetuare il multitasking sonoro. */
hwnd = hWnd;

/* Di default, disabilita il sonoro. Se non troveremo problemi, lo
riabiliteremo alla fine della routine */
bSoundSupported = false;

/* Crea un oggetto DirectSound. Usiamo il driver sonoro settato di default.
In realta' sarebbe possibile richiedere una lista delle schede sonore
presenti, e lasciare scegliere all'utente quale utilizzare, ma mi sembra
una perdita di tempo. */
if ((err = DirectSoundCreate(NULL, &lpDS, NULL)) != DS_OK) {
return false;
}

/* Richiede le caratteristiche del driver sonoro */
dscaps.dwSize = sizeof(dscaps);
lpDS->GetCaps(&dscaps);

/* Controlla se siamo in emulazione, e in caso affermativo avverte l'utente */
if ((dscaps.dwFlags & DSCAPS_EMULDRIVER))
error("The current sound driver is not directly supported by DirectSound. This will slow down sound performance of the game.");

/* Setta il livello di cooperazione a esclusivo. In questo modo il gioco
sara' il solo ad accedere alla scheda sonora mentre e' in escuzione, ed
eventuali player in background saranno automaticamente stoppati.
Inoltre in questo modo e' possibile settare il formato di output sonoro
del primary buffer */
if ((err = lpDS->SetCooperativeLevel(hWnd, DSSCL_PRIORITY)) != DS_OK) {
MessageBox(hwnd, "Cannot set exclusive mode!", "soundInit()", MB_OK);
return false;
}


/* Crea il primary buffer. In realta' DirectSound la farebbe automaticamente,
ma noi vogliamo il pointer al primary perche' dobbiamo settare il
formato di output */
ZeroMemory(&dsbdesc, sizeof(dsbdesc));
dsbdesc.dwSize = sizeof(dsbdesc);
dsbdesc.dwFlags = DSBCAPS_CTRLVOLUME | DSBCAPS_PRIMARYBUFFER;
if (lpDS->CreateSoundBuffer(&dsbdesc, &lpDSBPrimary, NULL) != DS_OK) {
MessageBox(hwnd, "Cannot create primary buffer!", "soundInit()", MB_OK);
return false;
}

/* Settiamo il formato del buffer primario. L'ideale sarebbe 16bit 44khz
stereo, ma dobbiamo anche controllare che cio' sia permesso dalla scheda
sonora, guardando nelle caratteristiche che abbiamo richiesto sopra.
Inoltre in seguito sara' possibile lasciare scegliere all'utente il
formato da utilizzare */
pcmwf.wBitsPerSample = ((dscaps.dwFlags & DSCAPS_PRIMARY16BIT) != 0 ? 16 : 8);
pcmwf.wf.wFormatTag = WAVE_FORMAT_PCM;
pcmwf.wf.nChannels = ((dscaps.dwFlags & DSCAPS_PRIMARYSTEREO) != 0 ? 2 : 1);
pcmwf.wf.nSamplesPerSec = 44100;
pcmwf.wf.nBlockAlign = (pcmwf.wBitsPerSample / 8) * pcmwf.wf.nChannels;
pcmwf.wf.nAvgBytesPerSec = (uint32)pcmwf.wf.nBlockAlign * (uint32)pcmwf.wf.nSamplesPerSec;

if ((err = lpDSBPrimary->SetFormat((LPWAVEFORMATEX) & pcmwf)) != DS_OK) {
wsprintf(errbuf, "Error setting the output format (%lx)", err);
MessageBox(hwnd, errbuf, "soundInit()", MB_OK);
return false;
}

/* Controlla che il driver DirectSound supporti buffer secondari con
play di stream 16bit, 44khz stereo */
if (dscaps.dwMaxSecondarySampleRate != 0 && dscaps.dwMaxSecondarySampleRate < 44100) {
wsprintf(errbuf, "Driver does not support 16bit 44khz stereo mixing! (%lu)", dscaps.dwMaxSecondarySampleRate);
MessageBox(hwnd, errbuf, "soundInit()", MB_OK);
return false;
}

/* Tutto OK. */
bSoundSupported = true;
#endif
return true;
bool FPSOUND::Init() {
bSoundSupported = g_system->getMixer()->isReady();
return bSoundSupported;
}


Expand All @@ -167,10 +83,6 @@ bool FPSOUND::Init(/*HWND hWnd*/) {
\****************************************************************************/

FPSOUND::~FPSOUND() {
#ifdef REFACTOR_ME
RELEASE(lpDSBPrimary);
RELEASE(lpDS);
#endif
}


Expand All @@ -197,7 +109,7 @@ FPSOUND::~FPSOUND() {
\****************************************************************************/

bool FPSOUND::CreateStream(FPSTREAM **lplpStream) {
(*lplpStream) = new FPSTREAM(lpDS, hwnd, bSoundSupported);
(*lplpStream) = new FPSTREAM(bSoundSupported);

return (*lplpStream != NULL);
}
Expand All @@ -221,7 +133,7 @@ bool FPSOUND::CreateStream(FPSTREAM **lplpStream) {
\****************************************************************************/

bool FPSOUND::CreateSfx(FPSFX **lplpSfx) {
(*lplpSfx) = new FPSFX(lpDS, hwnd, bSoundSupported);
(*lplpSfx) = new FPSFX(bSoundSupported);

return (*lplpSfx != NULL);
}
Expand All @@ -239,16 +151,10 @@ bool FPSOUND::CreateSfx(FPSFX **lplpSfx) {
\****************************************************************************/

void FPSOUND::SetMasterVolume(int dwVolume) {
#ifdef REFACTOR_ME

if (!bSoundSupported)
return;

if (dwVolume > 63) dwVolume = 63;
if (dwVolume < 0) dwVolume = 0;

lpDSBPrimary->SetVolume(dwVolume * (DSBVOLUME_MAX - DSBVOLUME_MIN) / 64 + DSBVOLUME_MIN);
#endif
g_system->getMixer()->setVolumeForSoundType(Audio::Mixer::kPlainSoundType, CLIP<int>(dwVolume, 0, 63) * Audio::Mixer::kMaxChannelVolume / 63);
}


Expand All @@ -263,15 +169,10 @@ void FPSOUND::SetMasterVolume(int dwVolume) {
\****************************************************************************/

void FPSOUND::GetMasterVolume(int *lpdwVolume) {
#ifdef REFACTOR_ME
if (!bSoundSupported)
return;

lpDSBPrimary->GetVolume((uint32 *)lpdwVolume);
*lpdwVolume -= (DSBVOLUME_MIN);
*lpdwVolume *= 64;
*lpdwVolume /= (DSBVOLUME_MAX - DSBVOLUME_MIN);
#endif
*lpdwVolume = g_system->getMixer()->getVolumeForSoundType(Audio::Mixer::kPlainSoundType) * 63 / Audio::Mixer::kMaxChannelVolume;
}


Expand All @@ -288,7 +189,7 @@ void FPSOUND::GetMasterVolume(int *lpdwVolume) {
*
\****************************************************************************/

FPSFX::FPSFX(LPDIRECTSOUND lpds, HWND hWnd, bool bSoundOn) {
FPSFX::FPSFX(bool bSoundOn) {
#ifdef REFACTOR_ME

static char errbuf[128];
Expand Down Expand Up @@ -861,7 +762,7 @@ void FPSFX::GetVolume(int *lpdwVolume) {
*
\****************************************************************************/

FPSTREAM::FPSTREAM(LPDIRECTSOUND LPDS, HWND hWnd, bool bSoundOn) {
FPSTREAM::FPSTREAM(bool bSoundOn) {
#ifdef REFACTOR_ME
//hwnd=hWnd;
lpDS = LPDS;
Expand Down
29 changes: 6 additions & 23 deletions engines/tony/sound.h
Expand Up @@ -36,17 +36,8 @@

namespace Tony {

// Dummy type declarations
typedef void *LPDIRECTSOUND;
typedef void *LPDIRECTSOUNDBUFFER;
typedef uint32 HWND;
struct DSCAPS {
};


class FPSTREAM;
class FPSFX;
class CODEC;

enum CODECS {
FPCODEC_RAW,
Expand All @@ -67,10 +58,6 @@ class FPSOUND {
private:

bool bSoundSupported;
LPDIRECTSOUND lpDS;
LPDIRECTSOUNDBUFFER lpDSBPrimary;
DSCAPS dscaps;
HWND hwnd;

/****************************************************************************\
* Metodi
Expand Down Expand Up @@ -102,19 +89,17 @@ class FPSOUND {

/****************************************************************************\
*
* Function: bool FPSOUND::Init(HWND hWnd);
* Function: bool FPSOUND::Init();
*
* Description: Inizializza l'oggetto, e prepara tutto il necessario per
* creare stream e effetti sonori.
*
* Input: HWND hWnd Handle della finestra principale
*
* Return: True se tutto OK, FALSE in caso di errore.
*
\****************************************************************************/


bool Init(/*HWND hWnd*/);
bool Init();


/****************************************************************************\
Expand Down Expand Up @@ -211,7 +196,6 @@ class FPSFX {
bool b16bit; // TRUE se è 16 bit
uint32 dwFreq; // Frequenza originale di campionamento

// CODEC* lpCodec; // CODEC da utilizzare.
bool bIsPlaying; // TRUE se si sta playando l'effetto sonoro

bool bIsVoice;
Expand All @@ -233,14 +217,14 @@ class FPSFX {

/****************************************************************************\
*
* Function: FPSFX(LPDIRECTSOUND lpDS, bool bSoundOn);
* Function: FPSFX(bool bSoundOn);
*
* Description: Costruttore di default. *NON* bisogna dichiarare direttamente
* un oggetto, ma crearlo piuttosto tramite FPSOUND::CreateSfx()
*
\****************************************************************************/

FPSFX(void * /*LPDIRECTSOUND */lpDS, uint32 /*HWND*/ hwnd, bool bSoundOn);
FPSFX(bool bSoundOn);


/****************************************************************************\
Expand Down Expand Up @@ -413,7 +397,6 @@ class FPSTREAM {
FPSTREAM *SyncToPlay;
// DSBPOSITIONNOTIFY dspnHot[3];

CODEC *lpCodec; // CODEC da utilizzare.
bool CreateBuffer(int nBufSize);

public:
Expand All @@ -431,14 +414,14 @@ class FPSTREAM {

/****************************************************************************\
*
* Function: FPSTREAM(LPDIRECTSOUND lpDS, bool bSoundOn);
* Function: FPSTREAM(bool bSoundOn);
*
* Description: Costruttore di default. *NON* bisogna dichiarare direttamente
* un oggetto, ma crearlo piuttosto tramite FPSOUND::CreateStream()
*
\****************************************************************************/

FPSTREAM(void * /*LPDIRECTSOUND*/ lpDS, uint32 /*HWND hWnd */, bool bSoundOn);
FPSTREAM(bool bSoundOn);


/****************************************************************************\
Expand Down

0 comments on commit 1668a23

Please sign in to comment.