shithub: cstory

Download patch

ref: cc0464e774b972e059732d464fac501f8779fc9a
parent: ca1ed4f642052ef66f39c4dbc6f819efcfa0a3d6
author: Clownacy <Clownacy@users.noreply.github.com>
date: Tue Jul 7 20:39:34 EDT 2020

Restore InitSoundObject

Can be used to load SFX from .wav resource files (as long as they're
mono unsigned 8-bit samples).

--- a/src/Sound.cpp
+++ b/src/Sound.cpp
@@ -21,6 +21,7 @@
 
 #include "Backends/Audio.h"
 //#include "Main.h"	// Was for gModulePath, but this is unneeded in the portable branch since LoadSoundObject is commented-out
+#include "Resource.h"
 #include "Organya.h"
 #include "PixTone.h"
 
@@ -67,52 +68,42 @@
 
 	AudioBackend_Deinit();
 }
-/*
+
 // サウンドの設定 (Sound settings)
-BOOL InitSoundObject(LPCSTR resname, int no)
+BOOL InitSoundObject(const char *resname, int no)
 {
-	HRSRC hrscr;
-	DSBUFFERDESC dsbd;
-	DWORD *lpdword;	// リソースのアドレス (Resource address)
+	const unsigned char *resource_pointer;	// リソースのアドレス (Resource address)
 
-	if (lpDS == NULL)
+	if (!audio_backend_initialised)
 		return TRUE;
 
 	// リソースの検索 (Search for resources)
-	if ((hrscr = FindResourceA(NULL, resname, "WAVE")) == NULL)
+	resource_pointer = FindResource("WAVE100", "WAVE", NULL);
+
+	if (resource_pointer == NULL)
 		return FALSE;
 
-	// リソースのアドレスを取得 (Get resource address)
-	lpdword = (DWORD*)LockResource(LoadResource(NULL, hrscr));
+	// Get sound properties, and check if it's valid
+	unsigned long buffer_size = resource_pointer[0x36] | (resource_pointer[0x37] << 8) | (resource_pointer[0x38] << 16) | (resource_pointer[0x39] << 24);
+	unsigned short format = resource_pointer[5] | (resource_pointer[6] << 8);
+	unsigned short channels = resource_pointer[7] | (resource_pointer[8] << 8);
+	unsigned long sample_rate = resource_pointer[9] | (resource_pointer[0xA] << 8) | (resource_pointer[0xB] << 16) | (resource_pointer[0xC] << 24);
 
-	// 二次バッファの生成 (Create secondary buffer)
-	ZeroMemory(&dsbd, sizeof(dsbd));
-	dsbd.dwSize = sizeof(dsbd);
-	dsbd.dwFlags = DSBCAPS_STATIC | DSBCAPS_GLOBALFOCUS | DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLFREQUENCY;
-	dsbd.dwBufferBytes = *(DWORD*)((BYTE*)lpdword+0x36);	// WAVEデータのサイズ (WAVE data size)
-	dsbd.lpwfxFormat = (LPWAVEFORMATEX)(lpdword+5); 
+	if (format != 1)	// 1 is WAVE_FORMAT_PCM
+		return FALSE;
 
-	if (lpDS->CreateSoundBuffer(&dsbd, &lpSECONDARYBUFFER[no], NULL) != DS_OK)
+	if (channels != 1)	// The mixer only supports mono right now
 		return FALSE;
 
-	LPVOID lpbuf1, lpbuf2;
-	DWORD dwbuf1, dwbuf2;
+	// 二次バッファの生成 (Create secondary buffer)
+	lpSECONDARYBUFFER[no] = AudioBackend_CreateSound(sample_rate, resource_pointer + 0x3A, buffer_size);
 
-	// 二次バッファのロック (Secondary buffer lock)
-	lpSECONDARYBUFFER[no]->Lock(0, *(DWORD*)((BYTE*)lpdword+0x36), &lpbuf1, &dwbuf1, &lpbuf2, &dwbuf2, 0); 
+	if (lpSECONDARYBUFFER[no] == NULL)
+		return FALSE;
 
-	// 音源データの設定 (Sound source data settings)
-	CopyMemory(lpbuf1, (BYTE*)lpdword+0x3A, dwbuf1);
-
-	if (dwbuf2 != 0)
-		CopyMemory(lpbuf2, (BYTE*)lpdword+0x3A+dwbuf1, dwbuf2);
-
-	// 二次バッファのロック解除 (Unlock secondary buffer)
-	lpSECONDARYBUFFER[no]->Unlock(lpbuf1, dwbuf1, lpbuf2, dwbuf2); 
-
 	return TRUE;
 }
-
+/*
 // Completely unused function for loading a .wav file as a sound effect.
 // Some say that sounds heard in CS Beta footage don't sound like PixTone...
 BOOL LoadSoundObject(LPCSTR file_name, int no)