Merge pull request #2 from Stommm/master

Add windows soundsystem
This commit is contained in:
iProgramInCpp
2023-07-31 15:11:19 +03:00
committed by GitHub
5 changed files with 251 additions and 2 deletions

View File

@@ -0,0 +1,191 @@
/********************************************************************
Minecraft: Pocket Edition - Decompilation Project
Copyright (C) 2023 iProgramInCpp
The following code is licensed under the BSD 1 clause license.
SPDX-License-Identifier: BSD-1-Clause
********************************************************************/
#include "../../source/Base/Utils.hpp"
#include "SoundSystem_windows.hpp"
SoundSystemWindows::SoundSystemWindows()
{
printf("Init SoundSystemWindows\n");
HRESULT result;
DSBUFFERDESC bufferDesc;
result = DirectSoundCreate8(NULL, &m_directsound, NULL);
if (FAILED(result))
{
printf("SoundSystemWindows failed to create directsound8 handle\n");
return;
}
result = m_directsound->SetCooperativeLevel(GetHWND(), DSSCL_NORMAL);
if (FAILED(result))
{
printf("SoundSystemWindows failed set cooperation level\n");
return;
}
bufferDesc.dwSize = sizeof(DSBUFFERDESC);
bufferDesc.dwFlags = DSBCAPS_CTRL3D | DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRLVOLUME;
bufferDesc.dwBufferBytes = 0;
bufferDesc.dwReserved = 0;
bufferDesc.lpwfxFormat = NULL;
bufferDesc.guid3DAlgorithm = GUID_NULL;
// Get control of the primary sound buffer on the default sound device.
result = m_directsound->CreateSoundBuffer(&bufferDesc, &m_primarybuffer, NULL);
if (FAILED(result))
{
printf("SoundSystemWindows failed to create primary sound buffer\n");
return;
}
}
SoundSystemWindows::~SoundSystemWindows()
{
printf("Destroying SoundSystemWindows\n");
m_directsound->Release();
}
bool SoundSystemWindows::isAvailable()
{
return false;
}
void SoundSystemWindows::setListenerPos(float x, float y, float z)
{
}
void SoundSystemWindows::setListenerAngle(float f)
{
}
void SoundSystemWindows::load(const std::string& sound)
{
}
void SoundSystemWindows::play(const std::string& sound)
{
}
void SoundSystemWindows::pause(const std::string& sound)
{
}
void SoundSystemWindows::stop(const std::string& sound)
{
}
void SoundSystemWindows::playAt(const SoundDesc& sound, float x, float y, float z, float volume, float pitch)
{
//Release sounds that finished playing
for (size_t i = 0; i < m_buffers.size(); i++)
{
DWORD status;
(*m_buffers[i])->GetStatus(&status);
if (status != DSBSTATUS_PLAYING) {
(*m_buffers[i])->Release();
delete m_buffers[i];
m_buffers.erase(m_buffers.begin() + i);
}
}
if (sound.m_pHeader->m_id == 1) //for some reason if the id is 1 you need to pitch the sound down
{
pitch /= 2.f;
}
HRESULT result;
IDirectSoundBuffer* tempBuffer;
unsigned char* bufferPtr;
unsigned long bufferSize;
int length = sound.m_pHeader->m_length*sizeof(uint16_t);
LPDIRECTSOUNDBUFFER* soundbuffer = (LPDIRECTSOUNDBUFFER*)calloc(1, sizeof(LPDIRECTSOUNDBUFFER));
WAVEFORMATEX waveFormat;
waveFormat.wFormatTag = WAVE_FORMAT_PCM;
waveFormat.nSamplesPerSec = DWORD(float(sound.m_pHeader->m_sample_rate) * pitch);
waveFormat.wBitsPerSample = 16;
waveFormat.nChannels = sound.m_header.m_channels;
waveFormat.nBlockAlign = (waveFormat.wBitsPerSample / 8) * waveFormat.nChannels;
waveFormat.nAvgBytesPerSec = waveFormat.nSamplesPerSec * waveFormat.nBlockAlign;
waveFormat.cbSize = 0;
// Set the buffer description of the secondary sound buffer that the wave file will be loaded onto.
DSBUFFERDESC bufferDesc;
bufferDesc.dwSize = sizeof(DSBUFFERDESC);
if (sound.m_header.m_channels == 1) {
bufferDesc.dwFlags = DSBCAPS_CTRLVOLUME | DSBCAPS_GLOBALFOCUS | DSBCAPS_CTRL3D;
}
else {
bufferDesc.dwFlags = DSBCAPS_CTRLVOLUME | DSBCAPS_GLOBALFOCUS;
}
bufferDesc.dwBufferBytes = length;
bufferDesc.dwReserved = 0;
bufferDesc.lpwfxFormat = &waveFormat;
bufferDesc.guid3DAlgorithm = GUID_NULL;
// Create a temporary sound buffer with the specific buffer settings.
result = m_directsound->CreateSoundBuffer(&bufferDesc, &tempBuffer, NULL);
if (FAILED(result))
{
printf("SoundSystemWindows CreateSoundBuffer failed\n");
return;
}
// Test the buffer format against the direct sound 8 interface and create the secondary buffer.
result = tempBuffer->QueryInterface(IID_IDirectSoundBuffer8, (LPVOID*)soundbuffer);
if (FAILED(result))
{
printf("SoundSystemWindows QueryInterface failed\n");
return;
}
// Release the temporary buffer.
tempBuffer->Release();
tempBuffer = 0;
// Lock the secondary buffer to write wave data into it.
result = (*soundbuffer)->Lock(0, length, (void**)&bufferPtr, (DWORD*)&bufferSize, NULL, 0, 0);
if (FAILED(result))
{
printf("SoundSystemWindows lock failed\n");
return;
//return false;
}
// Copy the wave data into the buffer.
memcpy(bufferPtr, sound.m_pData, length);
// Unlock the secondary buffer after the data has been written to it.
result = (*soundbuffer)->Unlock((void*)bufferPtr, bufferSize, NULL, 0);
if (FAILED(result))
{
printf("SoundSystemWindows unlock failed\n");
return;
}
(*soundbuffer)->Play(0, 0, 0);
m_buffers.push_back(soundbuffer);
}

View File

@@ -0,0 +1,45 @@
/********************************************************************
Minecraft: Pocket Edition - Decompilation Project
Copyright (C) 2023 iProgramInCpp
The following code is licensed under the BSD 1 clause license.
SPDX-License-Identifier: BSD-1-Clause
********************************************************************/
#pragma once
#include <string>
#include <Windows.h>
#include <uuids.h>
#include <strmif.h>
#include <stdexcept>
#include <vector>
#pragma comment(lib, "dsound.lib")
#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "winmm.lib")
#include <mmsystem.h>
#include <dsound.h>
#include "SoundData.hpp"
class SoundSystemWindows
{
public:
SoundSystemWindows();
~SoundSystemWindows();
virtual bool isAvailable();
virtual void setListenerPos(float x, float y, float z);
virtual void setListenerAngle(float f);
virtual void load(const std::string& sound);
virtual void play(const std::string& sound);
virtual void pause(const std::string& sound);
virtual void stop(const std::string& sound);
virtual void playAt(const SoundDesc& sound, float x, float y, float z, float a, float b);
private:
IDirectSound8* m_directsound;
IDirectSoundBuffer* m_primarybuffer;
std::vector<LPDIRECTSOUNDBUFFER*> m_buffers;
};

View File

@@ -14,12 +14,17 @@
#include "Random.hpp"
// Platform specific type for the sound system.
#ifdef _WIN32
#include "../../platforms/windows/SoundSystem_windows.hpp"
#define SOUND_SYSTEM_TYPE SoundSystemWindows
#else
#define SOUND_SYSTEM_TYPE SoundSystem
#endif
//#define SOUND_SYSTEM_TYPE SoundSystemSL
//#define SOUND_SYSTEM_TYPE SoundSystemWindows
// @TODO: SoundSystemSL - Use this for the Android port
// @TODO: SoundSystemWindows - Sound system for Windows.
class SoundEngine
{

View File

@@ -29,6 +29,7 @@
<ItemGroup>
<ClInclude Include="..\compat\AKeyCodes.hpp" />
<ClInclude Include="..\platforms\windows\AppPlatform_windows.hpp" />
<ClInclude Include="..\platforms\windows\SoundSystem_windows.hpp" />
<ClInclude Include="..\source\App\App.hpp" />
<ClInclude Include="..\source\App\AppPlatform.hpp" />
<ClInclude Include="..\source\App\Minecraft.hpp" />
@@ -318,6 +319,7 @@
<ClCompile Include="..\compat\GLExt.cpp" />
<ClCompile Include="..\platforms\windows\AppPlatform_windows.cpp" />
<ClCompile Include="..\platforms\windows\main.cpp" />
<ClCompile Include="..\platforms\windows\SoundSystem_windows.cpp" />
<ClCompile Include="..\source\App\App.cpp" />
<ClCompile Include="..\source\App\AppPlatform.cpp" />
<ClCompile Include="..\source\App\Minecraft.cpp" />

View File

@@ -1056,6 +1056,9 @@
<ClCompile Include="..\platforms\windows\main.cpp">
<Filter>Source Files\App</Filter>
</ClCompile>
<ClCompile Include="..\platforms\windows\SoundSystem_windows.cpp">
<Filter>Source Files\App</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\source\Options.hpp">
@@ -1916,6 +1919,9 @@
<ClInclude Include="..\platforms\windows\AppPlatform_windows.hpp">
<Filter>Header Files\App</Filter>
</ClInclude>
<ClInclude Include="..\platforms\windows\SoundSystem_windows.hpp">
<Filter>Header Files\App</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Text Include="..\thirdparty\raknet\CMakeLists.txt">