Skip to content

Commit

Permalink
Small compiler optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
ayuanx committed Aug 7, 2024
1 parent 82259b3 commit 5f93437
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ NOTE: It actually can also work on Win95/98 if you follow the extra procedure be

# Revisions:

v.2024.08.08
- Small compiler optimizations.

v.2024.03.21
- Fix the truncation at the end of each song ranging from 1ms to 500ms.

Expand Down
2 changes: 1 addition & 1 deletion ogg-winmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct play_info
struct track_info tracks[MAX_TRACKS+1]; // Track 0 is reserved.
struct play_info info = {0};

DWORD thread = 0; // Needed for Win85/98 compatibility
DWORD thread = 0; // Needed for Win95/98 compatibility
HANDLE player = NULL;
HANDLE event = NULL;
HWND window = NULL;
Expand Down
22 changes: 13 additions & 9 deletions player.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
bool plr_run = false;
bool plr_bsy = false;
unsigned int plr_len = 0;
float plr_vol[2] = {100.0, 100.0}; // Left, Right
float plr_vol[2] = {1.0, 1.0}; // Left, Right

HWAVEOUT plr_hw = NULL;
HANDLE plr_ev = NULL;
Expand All @@ -24,10 +24,10 @@ char plr_buf[WAV_BUF_CNT][WAV_BUF_LEN] __attribute__ ((aligned(4)));

void plr_volume(int vol_l, int vol_r)
{
if (vol_l < 0 || vol_l > 99) plr_vol[0] = 100.0;
if (vol_l < 0 || vol_l > 99) plr_vol[0] = 1.0;
else plr_vol[0] = vol_l / 100.0;

if (vol_r < 0 || vol_r > 99) plr_vol[1] = 100.0;
if (vol_r < 0 || vol_r > 99) plr_vol[1] = 1.0;
else plr_vol[1] = vol_r / 100.0;
}

Expand Down Expand Up @@ -102,8 +102,8 @@ int plr_play(const char *path, unsigned int from, unsigned int to)

if (from == -1) plr_len = 0;
else {
if (from) ov_time_seek(&plr_vf, (double)from / 1000);
plr_len = to != -1 ? (unsigned int)ceil((to - from) / 1000.0 * vi->rate) * 2 * vi->channels : -1; // heed alignment
if (from) ov_time_seek(&plr_vf, from * 0.001);
plr_len = to != -1 ? (unsigned int)ceil((to - from) * 0.001 * vi->rate) * 2 * vi->channels : -1; // heed alignment
}

plr_run = true;
Expand Down Expand Up @@ -180,11 +180,15 @@ int plr_pump()
plr_len -= pos;

/* volume control, kinda nasty */
if (plr_vol[0] != 100.0 || plr_vol[1] != 100.0) {
if (plr_vol[0] != 1.0 || plr_vol[1] != 1.0) {
short *sbuf = (short *)buf;
for (int j = 0, end = pos / 2; j < end; j+=2) {
if (plr_vol[0] != 100.0) sbuf[j] *= plr_vol[0];
if (plr_vol[1] != 100.0) sbuf[j+1] *= plr_vol[1];
// Surprisingly speedwise (fast > slow): float multiplication > float divison >> int multiplication > int division.
// Also remove branching for better compiler SIMD/loop unrolling optimization.
//if (plr_vol[0] != 1.0) sbuf[j] *= plr_vol[0];
//if (plr_vol[1] != 1.0) sbuf[j+1] *= plr_vol[1];
sbuf[j] *= plr_vol[0];
sbuf[j+1] *= plr_vol[1];
}
}

Expand Down Expand Up @@ -215,7 +219,7 @@ int plr_pump()
return 1;
}

/* TODO: */
/* Not needed: */
/*
int plr_seek(int sec)
{
Expand Down
16 changes: 8 additions & 8 deletions stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
#include <ctype.h>
#include "player.h"

static float midiVol = 100.0;
static float waveVol = 100.0;
static float midiVol = 1.0;
static float waveVol = 1.0;
static int waveBits = -1;

static HINSTANCE realWinmmDLL = NULL;

void stub_midivol(int vol) { midiVol = vol < 0 || vol > 99 ? 100.0 : vol / 100.0; }
void stub_wavevol(int vol) { waveVol = vol < 0 || vol > 99 ? 100.0 : vol / 100.0; }
void stub_midivol(int vol) { midiVol = vol < 0 || vol > 99 ? 1.0 : vol / 100.0; }
void stub_wavevol(int vol) { waveVol = vol < 0 || vol > 99 ? 1.0 : vol / 100.0; }

void unloadRealDLL()
{
Expand Down Expand Up @@ -43,7 +43,7 @@ MMRESULT WINAPI fake_midiStreamOut(HMIDISTRM a0, LPMIDIHDR a1, UINT a2)
funcp = (void*)GetProcAddress(loadRealDLL(), "midiStreamOut");

#ifdef MIDI_VELOCITY_SCALING
if (midiVol != 100.0 && a1 && a1->lpData) {
if (midiVol != 1.0 && a1 && a1->lpData) {
for (int i = 0, j = a1->dwBytesRecorded; i < j; i += sizeof(DWORD)*3) {
MIDIEVENT *pe = (MIDIEVENT *)(a1->lpData + i);
if (pe->dwEvent & MEVT_F_LONG) {
Expand Down Expand Up @@ -86,21 +86,21 @@ MMRESULT WINAPI fake_waveOutWrite(HWAVEOUT a0, LPWAVEHDR a1, UINT a2)
funcp = (void*)GetProcAddress(loadRealDLL(), "waveOutWrite");

/* let owr own OGG wave pass through */
if ((waveVol != 100.0 || midiVol != 100.0 ) && a1 && a1->lpData && a1->dwUser != 0xCDDA7777) {
if ((waveVol != 1.0 || midiVol != 1.0 ) && a1 && a1->lpData && a1->dwUser != 0xCDDA7777) {
/* Windows is f**ked up. MIDI synth driver converts MIDI to WAVE and then calls winmm.waveOutWrite!!! */
void *addr = __builtin_return_address(0);
char caller[MAX_PATH];
MEMORY_BASIC_INFORMATION mbi;
VirtualQuery(addr, &mbi, sizeof(MEMORY_BASIC_INFORMATION));
GetModuleFileName(mbi.AllocationBase, caller, MAX_PATH);

float vol = 100.0;
float vol = 1.0;
char *pos = strrchr(caller, '\\');
/* Mixer: msacm32.drv */
if (strstr(pos, "wdmaud.drv")) vol = midiVol;
else if (!strstr(pos, ".drv")) vol = waveVol;

if (vol != 100.0) {
if (vol != 1.0) {
short *wave16;
char *wave8;
switch (waveBits) {
Expand Down

0 comments on commit 5f93437

Please sign in to comment.