Skip to content

Commit

Permalink
Added Internal Mode, and Audio Latency Mode
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyFilter committed Nov 12, 2023
1 parent fbfedb5 commit 09429b1
Show file tree
Hide file tree
Showing 8 changed files with 431 additions and 199 deletions.
54 changes: 51 additions & 3 deletions Audio/Sound_Helper.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "Sound_Helper.h"

//#include <xaudio2.h>

int Waveform_SoundHelper::StartRecording()
{
// Start recording audio
Expand Down Expand Up @@ -291,15 +293,15 @@ UINT GetAudioDevices(_Out_ AudioDeviceInfo** devices)
UINT WaveformID = 0;

int devs = waveInGetNumDevs();
for (int i = 0; i < devs; i++)
for (int j = 0; j < devs; j++)
{
MMRESULT res;

WAVEINCAPSW* caps = new WAVEINCAPSW();
res = waveInGetDevCapsW(i, caps, sizeof(WAVEINCAPSW));
res = waveInGetDevCapsW(j, caps, sizeof(WAVEINCAPSW));

if (wcsncmp(varName.pwszVal, caps->szPname, wcslen(caps->szPname)) == 0)
WaveformID = i;
WaveformID = j;
}

(*devices)[i] = AudioDeviceInfo(WaveformID, devCount - i - 1, !var.uiVal, varName.pwszVal, varEnumName.pwszVal);
Expand All @@ -312,4 +314,50 @@ UINT GetAudioDevices(_Out_ AudioDeviceInfo** devices)
pDevices->Release();

return devCount;
}

// -------------------- Output Audio --------------------

#define WAV_PLAY_TIME_FRAC 64
#define WAV_HEADER_DATA_SIZE 44100/WAV_PLAY_TIME_FRAC

typedef struct WAV_HEADER {
/* RIFF Chunk Descriptor */
uint8_t RIFF[4] = { 'R', 'I', 'F', 'F' }; // RIFF Header Magic header
uint32_t ChunkSize = 44 + (sizeof(short) * WAV_HEADER_DATA_SIZE); // File size
uint8_t WAVE[4] = { 'W', 'A', 'V', 'E' }; // WAVE Header
uint8_t fmt[4] = { 'f', 'm', 't', ' ' }; // FMT header
uint32_t Subchunk1Size = 16; // Size of the fmt chunk
uint16_t AudioFormat = 1; // Audio format 1 = PCM
uint16_t NumOfChan = 1; // Number of channels 1=Mono 2=Sterio
uint32_t SamplesPerSec = WAV_HEADER_DATA_SIZE * WAV_PLAY_TIME_FRAC; // Sampling Frequency in Hz
uint32_t bytesPerSec = SamplesPerSec * 2; // bytes per second
uint16_t blockAlign = 2; // 2=16-bit mono, 4=16-bit stereo
uint16_t bitsPerSample = 16; // Number of bits per sample
/* "data" sub-chunk */
uint8_t Subchunk2ID[4] = { 'd', 'a', 't', 'a' }; // "data" string
uint32_t Subchunk2Size = sizeof(short) * WAV_HEADER_DATA_SIZE; // data chunk size
short data[WAV_HEADER_DATA_SIZE];
};

bool WinAudio_Player::Setup()
{

header = new WAV_HEADER();

return true;
}

void WinAudio_Player::SetBuffer(int (*func)(int))
{

for (int i = 0; i < WAV_HEADER_DATA_SIZE; i++)
{
header->data[i] = func(i);
}
}

void WinAudio_Player::Play()
{
PlaySound((LPCSTR)header, GetModuleHandle(NULL), SND_MEMORY | SND_ASYNC);
}
11 changes: 11 additions & 0 deletions Audio/Sound_Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,15 @@ class Waveform_SoundHelper
public:

~Waveform_SoundHelper();
};

struct WAV_HEADER;

class WinAudio_Player {
WAV_HEADER* header;

public:
bool Setup();
void SetBuffer(int (*func)(int));
void Play();
};
50 changes: 49 additions & 1 deletion gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,9 @@ int GUI::DrawGui() noexcept
g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, NULL);
//g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, clear_color_with_alpha);
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
g_pSwapChain->Present(*VSyncFrame, 0);
auto presentRes = g_pSwapChain->Present(*VSyncFrame, 0);
if (presentRes == DXGI_STATUS_OCCLUDED)
Sleep(1);
return 0;
}

Expand Down Expand Up @@ -392,6 +394,10 @@ LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)

switch (msg)
{
case WM_SETFOCUS:
if (KeyDownFunc)
KeyDownFunc(-1, -1, 0);
break;
case WM_SIZE:
if (g_pd3dDevice != NULL && wParam != SIZE_MINIMIZED)
{
Expand Down Expand Up @@ -427,6 +433,15 @@ LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
::PostQuitMessage(0);
return 0;
}
break;
case WM_SYSKEYDOWN:
if (KeyDownFunc)
KeyDownFunc(wParam, lParam, true);
break;
case WM_SYSKEYUP:
if (KeyDownFunc)
KeyDownFunc(wParam, lParam, false);
break;
case WM_KEYDOWN:
if (KeyDownFunc)
KeyDownFunc(wParam, lParam, true);
Expand All @@ -435,6 +450,39 @@ LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
if (KeyDownFunc)
KeyDownFunc(wParam, lParam, false);
break;
//case WM_INPUT:
//{
// if (!RawInputEventFunc)
// break;
// UINT dwSize;
//
// GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &dwSize, sizeof(RAWINPUTHEADER));
// LPBYTE lpb = new BYTE[dwSize];
// if (lpb == NULL)
// {
// OutputDebugStringA("0");
// return 0;
// }
// OutputDebugStringA("1\n");
//
// if (GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER)) != dwSize)
// OutputDebugString(TEXT("GetRawInputData does not return correct size !\n"));
//
// // request size of the raw input buffer to dwSize
// GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &dwSize,
// sizeof(RAWINPUTHEADER));
//
// // allocate buffer for input data
// auto buffer = (RAWINPUT*)HeapAlloc(GetProcessHeap(), 0, dwSize);
//
// if (GetRawInputData((HRAWINPUT)lParam, RID_INPUT, buffer, &dwSize,
// sizeof(RAWINPUTHEADER)))
// {
// RawInputEventFunc(buffer);
// }
// HeapFree(GetProcessHeap(), 0, buffer);
// break;
//}
}
return ::DefWindowProc(hWnd, msg, wParam, lParam);
}
1 change: 1 addition & 0 deletions gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace GUI
inline std::vector<IDXGIAdapter*> vAdapters;
inline bool (*onExitFunc)();
inline void (*KeyDownFunc)(WPARAM key, LPARAM info, bool isPressed);
inline void (*RawInputEventFunc)(RAWINPUT *rawInput);

inline UINT MAX_SUPPORTED_FRAMERATE;
inline UINT (*VSyncFrame);
Expand Down
Loading

0 comments on commit 09429b1

Please sign in to comment.