diff --git a/AsuroTool/AsuroTool.cpp b/AsuroTool/AsuroTool.cpp index 222a9d5..1d346b5 100644 --- a/AsuroTool/AsuroTool.cpp +++ b/AsuroTool/AsuroTool.cpp @@ -254,18 +254,33 @@ ImVec2 audioDeviceWindow(ApplicationData& appData, std::vector& dev ImGui::TableNextColumn(); if (dev.state == DEVICE_STATE_ACTIVE) { - - - float volume = log10f(getMeterValue(dev.meterInterface) * 9. + 1.); + // Log scale because it looks better (no actual reason for these exact numbers) + float meterValue = log10f(getMeterValue(dev.meterInterface) * 9. + 1.); auto drawList = ImGui::GetWindowDrawList(); ImVec2 windowPos = ImGui::GetWindowPos(); ImVec2 cursorPos = ImGui::GetCursorScreenPos(); ImVec2 space = ImGui::GetContentRegionAvail(); - float lineY = cursorPos.y + ImGui::GetTextLineHeight() / 2.; + float lineY = cursorPos.y + ImGui::GetTextLineHeight() / 2. + 2.; + + const float linePaddingX = 3.; + cursorPos.x += linePaddingX; - drawList->AddLine(ImVec2(cursorPos.x, lineY), ImVec2(cursorPos.x + space.x, lineY), IM_COL32(120, 120, 120, 255), 2.); - drawList->AddLine(ImVec2(cursorPos.x, lineY), ImVec2(cursorPos.x + space.x * volume, lineY), IM_COL32(200, 200, 255, 255), 3.); + drawList->AddLine(ImVec2(cursorPos.x, lineY), ImVec2(cursorPos.x + (space.x - 2. * linePaddingX) , lineY), IM_COL32(120, 120, 120, 255), 2.); + drawList->AddLine(ImVec2(cursorPos.x, lineY), ImVec2(cursorPos.x + (space.x - 2. * linePaddingX) * meterValue, lineY), IM_COL32(200, 200, 255, 255), 3.); + + float volume = getVolume(dev.volumeInterface); + float prevVolume = volume; + ImGui::SetNextItemWidth(space.x); + ImGui::PushID(dev.device); + ImGui::PushStyleColor(ImGuiCol_FrameBg, ImVec4(0, 0, 0, 0)); + ImGui::SliderFloat("", &volume, 0., 1., "", ImGuiSliderFlags_NoRoundToFormat | ImGuiSliderFlags_AlwaysClamp); + ImGui::PopStyleColor(); + ImGui::PopID(); + if (prevVolume != volume) + { + setVolume(dev.volumeInterface, volume); + } } // Defaults diff --git a/AsuroTool/AudioApi.cpp b/AsuroTool/AudioApi.cpp index 124378d..479f4c5 100644 --- a/AsuroTool/AudioApi.cpp +++ b/AsuroTool/AudioApi.cpp @@ -149,7 +149,7 @@ void reloadDeviceLists(AudioData& audioData) float getVolume(IAudioEndpointVolume* volumeInterface) { float volume; - if (FAILED(volumeInterface->GetChannelVolumeLevel(0, &volume))) + if (FAILED(volumeInterface->GetMasterVolumeLevelScalar(&volume))) { volume = 0.; } @@ -157,6 +157,12 @@ float getVolume(IAudioEndpointVolume* volumeInterface) return volume; } +void setVolume(IAudioEndpointVolume* volumeInterface, float newVolume) +{ + HRESULT hr = volumeInterface->SetMasterVolumeLevelScalar(newVolume, NULL); + isError(hr, "Failed to set volume level: "); +} + float getMeterValue(IAudioMeterInformation* meterInterface) { float volume; diff --git a/AsuroTool/AudioApi.h b/AsuroTool/AudioApi.h index aff5d09..ae55c08 100644 --- a/AsuroTool/AudioApi.h +++ b/AsuroTool/AudioApi.h @@ -10,4 +10,5 @@ void setDefaultAudioDevice(AudioData& audioData, const wchar_t* deviceId, ERole void loadAudioDevices(AudioData& audioData, std::vector& deviceList, EDataFlow deviceType); void reloadDeviceLists(AudioData& audioData); float getVolume(IAudioEndpointVolume* volumeInterface); +void setVolume(IAudioEndpointVolume* volumeInterface, float newVolume); float getMeterValue(IAudioMeterInformation* meterInterface);