The issue with the dB value staying at -9000 dB likely arises from the way the trackbar position is being set and how you're converting that position to a dB value.
In the original code, when you set the trackbar range to SendMessage(hGainTrackbar, TBM_SETRANGE, TRUE, MAKELONG(-9600, 50000));, it seems that you intended to allow for a range of dB levels from -960.0 dB to +5000.0 dB (scaled by 10 for the trackbar position).
Let's clarify how we should scale the trackbar's values and ensure that the dB level displays correctly in the GUI and reacts to changes:
- Update Trackbar Range: We should keep the scaling from dB to values that the trackbar can handle (typically 0-10000 for float precision).
- Correct the Calculation: Calculate the dB level from the trackbar position correctly by ensuring the conversion takes the scaling into account.
Here’s the corrected version of the relevant portions of the original code:
Full Code with Fixed dB Conversion
#include "pch.h" // Precompiled header
#include <windows.h>
#include <commctrl.h>
#include <iostream>
#include <tchar.h>
#include <shellapi.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <comdef.h>
#include <cmath> // For pow()
#include <sstream>
// Function prototypes
DWORD WINAPI OpenGUI(LPVOID lpParam);
void ToggleWindow();
void SetMicrophoneGain(float gainLevel);
void SetBitrate(int bitrate);
// Global variables
HWND hWnd;
HINSTANCE hInstance;
HWND hGainTrackbar;
HWND hBitrateTrackbar;
BOOL isWindowVisible = TRUE;
float gainLevel = 0.0f; // Default gain in dB
int bitrate = 128; // Default bitrate in kbps
// Function to set microphone gain
void SetMicrophoneGain(float gainDb) {
CoInitialize(NULL);
IMMDeviceEnumerator* pEnumerator = NULL;
CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&pEnumerator);
IMMDevice* pDevice = NULL;
pEnumerator->GetDefaultAudioEndpoint(eCapture, eConsole, &pDevice);
IAudioEndpointVolume* pVolume = NULL;
pDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, NULL, (void**)&pVolume);
// Convert dB gain to a linear scale (SetMasterVolumeLevelScalar expects values from 0.0 to 1.0)
float volumeScalar = pow(10.0f, gainDb / 20.0f);
volumeScalar = min(max(volumeScalar, 0.0f), 1.0f); // Clamping between 0 and 1
HRESULT hr = pVolume->SetMasterVolumeLevelScalar(volumeScalar, NULL);
if (FAILED(hr)) {
std::cerr << "Error setting volume level: " << std::hex << hr << std::endl;
}
else {
std::cout << "Microphone gain set to: " << gainDb << " dB" << std::endl;
}
// Clean up
pVolume->Release();
pDevice->Release();
pEnumerator->Release();
CoUninitialize();
}
// Function to set bitrate
void SetBitrate(int bitrate) {
// Implement your logic to change the bitrate here
// For demonstration, we just print the bitrate
std::cout << "Bitrate set to: " << bitrate << " kbps" << std::endl;
}
// Function to toggle GUI visibility
void ToggleWindow() {
isWindowVisible = !isWindowVisible;
ShowWindow(hWnd, isWindowVisible ? SW_SHOW : SW_HIDE);
}
// Window Procedure
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
RECT rect;
GetClientRect(hwnd, &rect);
HBRUSH hBrush = CreateSolidBrush(RGB(50, 50, 50));
FillRect(hdc, &rect, hBrush);
DeleteObject(hBrush);
SetBkMode(hdc, TRANSPARENT);
SetTextColor(hdc, RGB(240, 240, 240));
HFONT hFont = CreateFont(20, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_OUTLINE_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
VARIABLE_PITCH, _T("Segoe UI"));
SelectObject(hdc, hFont);
TextOut(hdc, 10, 10, _T("Jakacord"), 8);
DeleteObject(hFont);
TCHAR gainText[50];
_stprintf_s(gainText, _T("Gain Level: %.2f dB"), gainLevel);
TextOut(hdc, 10, 80, gainText, _tcslen(gainText));
TCHAR bitrateText[50];
_stprintf_s(bitrateText, _T("Bitrate: %d kbps"), bitrate);
TextOut(hdc, 10, 130, bitrateText, _tcslen(bitrateText));
EndPaint(hwnd, &ps);
break;
}
case WM_HSCROLL: {
if ((HWND)lParam == hGainTrackbar) {
// Get the position and convert it back to dB
int trackbarPos = SendMessage(hGainTrackbar, TBM_GETPOS, 0, 0);
gainLevel = trackbarPos / 10.0f; // Convert to dB; for example, if -9600 -> -960.0 dB
InvalidateRect(hwnd, NULL, TRUE);
SetMicrophoneGain(gainLevel);
}
else if ((HWND)lParam == hBitrateTrackbar) {
bitrate = SendMessage(hBitrateTrackbar, TBM_GETPOS, 0, 0);
InvalidateRect(hwnd, NULL, TRUE);
SetBitrate(bitrate);
}
break;
}
case WM_HOTKEY:
if (wParam == 1) { // F5 hotkey
ToggleWindow();
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
// Function to create GUI
DWORD WINAPI OpenGUI(LPVOID lpParam) {
hInstance = GetModuleHandle(NULL);
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = _T("Jaka");
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
RegisterClass(&wc);
hWnd = CreateWindowEx(
WS_EX_TOOLWINDOW, _T("Jaka"), _T("Jakacord"),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 400,
NULL, NULL, hInstance, NULL);
if (!hWnd) return 1;
RegisterHotKey(hWnd, 1, 0, VK_F5);
// Gain trackbar
hGainTrackbar = CreateWindowEx(
0, TRACKBAR_CLASS, NULL, WS_CHILD | WS_VISIBLE | TBS_BOTH,
30, 150, 300, 30, hWnd, NULL, hInstance, NULL);
// Set trackbar range from -9600 to 50000 in increments of 10
SendMessage(hGainTrackbar, TBM_SETRANGE, TRUE, MAKELONG(-9600, 50000));
SendMessage(hGainTrackbar, TBM_SETPOS, TRUE, static_cast<int>(gainLevel * 10));
// Bitrate trackbar
hBitrateTrackbar = CreateWindowEx(
0, TRACKBAR_CLASS, NULL, WS_CHILD | WS_VISIBLE | TBS_BOTH,
30, 200, 300, 30, hWnd, NULL, hInstance, NULL);
SendMessage(hBitrateTrackbar, TBM_SETRANGE, TRUE, MAKELONG(64, 320)); // Bitrates from 64 to 320 kbps
SendMessage(hBitrateTrackbar, TBM_SETPOS, TRUE, bitrate);
// Additional UI Elements
HWND hGainLabel = CreateWindowEx(0, _T("STATIC"), _T("Adjust Gain (dB):"),
WS_VISIBLE | WS_CHILD | SS_LEFT, 30, 120, 200, 25, hWnd, NULL, hInstance, NULL);
HWND hBitrateLabel = CreateWindowEx(0, _T("STATIC"), _T("Select Bitrate (kbps):"),
WS_VISIBLE | WS_CHILD | SS_LEFT, 30, 170, 200, 25, hWnd, NULL, hInstance, NULL);
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnregisterHotKey(hWnd, 1);
return 0;
}
// Entry point of the DLL
extern "C" __declspec(dllexport) BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
if (ul_reason_for_call == DLL_PROCESS_ATTACH) {
AllocConsole();
freopen_s((FILE**)stdout, "CONOUT$", "w", stdout);
std::cout << "Jakacord (uncapped audio)" << std::endl;
CreateThread(NULL, 0, OpenGUI, NULL, 0, NULL);
}
else if (ul_reason_for_call == DLL_PROCESS_DETACH) {
FreeConsole();
}
return TRUE;
}
Key Changes:
-
Corrected dB Scaling: The position of the trackbar directly corresponds to the dB level. The conversion now takes the raw position and scales it properly to floating-point dB values, effectively converting the range of the trackbar into meaningful dB.
- Specifically, the line
gainLevel = trackbarPos / 10.0f; converts the position value (which can range between -9600 to 50000) back down into a realistic dB value, facilitating accurate readings and proper function.
-
Bitrate Trackbar: Remains unchanged but ensures proper bitrate settings where you can set values between 64 kbps and 320 kbps.
-
No Clamping to Unrealistic Values: The application allows adjusting gain to a human-defined and valid range, while still letting you implement safety checks if necessary.
Additional Considerations:
- This solution will work with the trackbar correctly converting values, ensuring the gain reflects what's shown by the trackbar during user interaction.
- Test your application to ensure you are not hitting performance limits or gain levels that could cause distortion or feedback.
This should fix the issue with the gain level staying at unrealistic values. Let me know if you need further adjustments or features!