Skip to content

Commit

Permalink
#9 tab to space
Browse files Browse the repository at this point in the history
  • Loading branch information
XProger committed Oct 4, 2016
1 parent be5dae6 commit 6591726
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 80 deletions.
78 changes: 39 additions & 39 deletions src/sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
#define STB_VORBIS_HEADER_ONLY
#include "libs/stb_vorbis/stb_vorbis.c"

#define SND_CHANNELS_MAX 32
#define BUFFER_SIZE_MP3 8192
#define SND_CHANNELS_MAX 32
#define BUFFER_SIZE_MP3 8192

namespace Sound {

struct Frame {
short L, R;
};
short L, R;
};

struct Decoder {
Stream *stream;
Expand Down Expand Up @@ -77,12 +77,12 @@ namespace Sound {
sample = clamp(sample, -32768, 32767);
sample2 = sample1;
sample1 = sample;
delta = max(table[nibble] * delta / 256, 16);
delta = max(table[nibble] * delta / 256, 16);
return sample;
}
} channel[2];

virtual int decode(Frame *frames, int count) {
virtual int decode(Frame *frames, int count) {
static const int coeff1[] = { 256, 512, 0, 192, 240, 460, 392 };
static const int coeff2[] = { 0, -256, 0, 64, 0, -208, -232 };

Expand Down Expand Up @@ -110,7 +110,7 @@ namespace Sound {
frames[1].R = channel[1].sample1;
}
return 2;
} else {
} else {
uint8 value;
stream->read(value);
uint8 n1 = value >> 4, n2 = value & 0xF;
Expand All @@ -125,7 +125,7 @@ namespace Sound {
return 1;
}
}
}
}
};

struct MP3 : Decoder {
Expand All @@ -149,9 +149,9 @@ namespace Sound {
int i = 0;
char *ptr = (char*)frames;
while (ptr < (char*)&frames[count]) {
int res = mp3_decode(mp3, buffer + pos, size - pos, (short*)ptr, &info);
if (res) {
pos += res;
int res = mp3_decode(mp3, buffer + pos, size - pos, (short*)ptr, &info);
if (res) {
pos += res;
ptr += info.audio_bytes;
i += info.audio_bytes;
} else
Expand Down Expand Up @@ -191,7 +191,7 @@ namespace Sound {
int res = stb_vorbis_get_samples_short_interleaved(ogg, channels, (short*)frames + i, (count - i) * 2);
if (!res) break;
i += res;
}
}
return i;
}
};
Expand All @@ -201,19 +201,19 @@ namespace Sound {
vec3 velocity;
} listener;

enum Flags {
LOOP = 1,
PAN = 2,
REVERB_NEAR = 4,
REVERB_MIDDLE = 8,
REVERB_FAR = 16,
};
enum Flags {
LOOP = 1,
PAN = 2,
REVERB_NEAR = 4,
REVERB_MIDDLE = 8,
REVERB_FAR = 16,
};

struct Sample {
struct Sample {
Decoder *decoder;
float volume;
float pitch;
int flags;
float volume;
float pitch;
int flags;
bool isPlaying;

Sample(Stream *stream, float volume, float pitch, int flags) : decoder(NULL), volume(volume), pitch(pitch), flags(flags), isPlaying(true) {
Expand Down Expand Up @@ -250,10 +250,10 @@ namespace Sound {
decoder = new OGG(stream, 2);
} else if (fourcc == FOURCC("ID3\3")) { // mp3
decoder = new MP3(stream, 2);
}
}

ASSERT(decoder != NULL);
}
}

~Sample() {
delete decoder;
Expand All @@ -272,7 +272,7 @@ namespace Sound {
return true;
}

} *channels[SND_CHANNELS_MAX];
} *channels[SND_CHANNELS_MAX];
int channelsCount;

void init() {
Expand All @@ -286,7 +286,7 @@ namespace Sound {
mp3_decode_free();
}

void fill(Frame *frames, int count) {
void fill(Frame *frames, int count) {
struct FrameHI {
int L, R;
};
Expand All @@ -296,32 +296,32 @@ namespace Sound {

Frame *buffer = new Frame[count];

for (int i = 0; i < channelsCount; i++) {
for (int i = 0; i < channelsCount; i++) {

memset(buffer, 0, sizeof(Frame) * count);
channels[i]->render(buffer, count);
channels[i]->render(buffer, count);
for (int j = 0; j < count; j++) {
result[j].L += buffer[j].L;
result[j].R += buffer[j].R;
}
}
result[j].L += buffer[j].L;
result[j].R += buffer[j].R;
}
}

for (int i = 0; i < count; i++) {
frames[i].L = clamp(result[i].L, -32768, 32767);
frames[i].R = clamp(result[i].R, -32768, 32767);
}

delete[] buffer;
delete[] result;
delete[] buffer;
delete[] result;

for (int i = 0; i < channelsCount; i++)
if (!channels[i]->isPlaying) {
delete channels[i];
channels[i] = channels[--channelsCount];
i--;
}
}
}

Stream *openWAD(const char *name) {
Stream *stream = new Stream("cdaudio.wad");
Expand All @@ -344,13 +344,13 @@ namespace Sound {
return NULL;
}

void play(Stream *stream, float volume, float pitch, int flags) {
void play(Stream *stream, float volume, float pitch, int flags) {
if (!stream) return;
if (channelsCount < SND_CHANNELS_MAX)
channels[channelsCount++] = new Sample(stream, volume, pitch, flags);
else
LOG("! no free channels\n");
}
}
}

#endif
82 changes: 41 additions & 41 deletions src/win/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <stdint.h>
void __cdecl operator delete(void *ptr, unsigned int size) {
//
//
}
*/
#include "game.h"
Expand Down Expand Up @@ -120,14 +120,14 @@ WAVEHDR waveBuf[2];
void soundFree() {
if (!sndReady) return;
sndReady = false;
EnterCriticalSection(&sndCS);
waveOutUnprepareHeader(waveOut, &waveBuf[0], sizeof(WAVEHDR));
waveOutUnprepareHeader(waveOut, &waveBuf[1], sizeof(WAVEHDR));
waveOutReset(waveOut);
waveOutClose(waveOut);
delete[] sndData;
LeaveCriticalSection(&sndCS);
DeleteCriticalSection(&sndCS);
EnterCriticalSection(&sndCS);
waveOutUnprepareHeader(waveOut, &waveBuf[0], sizeof(WAVEHDR));
waveOutUnprepareHeader(waveOut, &waveBuf[1], sizeof(WAVEHDR));
waveOutReset(waveOut);
waveOutClose(waveOut);
delete[] sndData;
LeaveCriticalSection(&sndCS);
DeleteCriticalSection(&sndCS);
}

void CALLBACK sndFill(HWAVEOUT waveOut, UINT uMsg, DWORD_PTR dwInstance, LPWAVEHDR waveBuf, DWORD dwParam2) {
Expand All @@ -137,45 +137,45 @@ void CALLBACK sndFill(HWAVEOUT waveOut, UINT uMsg, DWORD_PTR dwInstance, LPWAVEH
return;
}

EnterCriticalSection(&sndCS);
waveOutUnprepareHeader(waveOut, waveBuf, sizeof(WAVEHDR));
Sound::fill((Sound::Frame*)waveBuf->lpData, SND_SIZE / 4);
waveOutPrepareHeader(waveOut, waveBuf, sizeof(WAVEHDR));
waveOutWrite(waveOut, waveBuf, sizeof(WAVEHDR));
LeaveCriticalSection(&sndCS);
EnterCriticalSection(&sndCS);
waveOutUnprepareHeader(waveOut, waveBuf, sizeof(WAVEHDR));
Sound::fill((Sound::Frame*)waveBuf->lpData, SND_SIZE / 4);
waveOutPrepareHeader(waveOut, waveBuf, sizeof(WAVEHDR));
waveOutWrite(waveOut, waveBuf, sizeof(WAVEHDR));
LeaveCriticalSection(&sndCS);
}

void soundInit(HWND hwnd) {
InitializeCriticalSection(&sndCS);
if (waveOutOpen(&waveOut, WAVE_MAPPER, &waveFmt, (INT_PTR)sndFill, 0, CALLBACK_FUNCTION) == MMSYSERR_NOERROR) {
InitializeCriticalSection(&sndCS);
if (waveOutOpen(&waveOut, WAVE_MAPPER, &waveFmt, (INT_PTR)sndFill, 0, CALLBACK_FUNCTION) == MMSYSERR_NOERROR) {
sndReady = true;
sndData = new char[SND_SIZE * 2];
memset(&waveBuf, 0, sizeof(waveBuf));
for (int i = 0; i < 2; i++) {
waveBuf[i].dwBufferLength = SND_SIZE;
waveBuf[i].lpData = sndData + SND_SIZE * i;
sndFill(waveOut, 0, 0, &waveBuf[i], 0);
}
} else {
sndData = new char[SND_SIZE * 2];
memset(&waveBuf, 0, sizeof(waveBuf));
for (int i = 0; i < 2; i++) {
waveBuf[i].dwBufferLength = SND_SIZE;
waveBuf[i].lpData = sndData + SND_SIZE * i;
sndFill(waveOut, 0, 0, &waveBuf[i], 0);
}
} else {
sndReady = false;
sndData = NULL;
sndData = NULL;
}
}


static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
// window
// window
case WM_ACTIVATE :
Input::reset();
break;
case WM_SIZE:
Core::width = LOWORD(lParam);
Core::height = HIWORD(lParam);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_SIZE:
Core::width = LOWORD(lParam);
Core::height = HIWORD(lParam);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
// keyboard
case WM_KEYDOWN :
case WM_KEYUP :
Expand Down Expand Up @@ -212,7 +212,7 @@ static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lPara
return 1;
// touch
// TODO
// sound
// sound
default :
return DefWindowProc(hWnd, msg, wParam, lParam);
}
Expand Down Expand Up @@ -253,12 +253,12 @@ int main() {

HWND hWnd = CreateWindow("static", "OpenLara", WS_OVERLAPPEDWINDOW, 0, 0, r.right - r.left, r.bottom - r.top, 0, 0, 0, 0);

HDC hDC = GetDC(hWnd);
HDC hDC = GetDC(hWnd);
HGLRC hRC = initGL(hDC);
joyInit();
soundInit(hWnd);
Game::init();
joyInit();
soundInit(hWnd);
Game::init();

SetWindowLong(hWnd, GWL_WNDPROC, (LONG)&WndProc);
ShowWindow(hWnd, SW_SHOWDEFAULT);
Expand Down Expand Up @@ -301,7 +301,7 @@ int main() {
}
} while (msg.message != WM_QUIT);

soundFree();
soundFree();
Game::free();

freeGL(hRC);
Expand Down

0 comments on commit 6591726

Please sign in to comment.