Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix stereo sound visualizations #1970

Merged
merged 4 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/libimhex/include/hex/helpers/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,29 @@ namespace hex {
class Provider;
}

template<typename T>
[[nodiscard]] std::vector<std::vector<T>> sampleChannels(const std::vector<T> &data, size_t count, size_t channels) {
if (channels == 0) return {};
size_t signalLength = std::max(1.0, double(data.size()) / channels);

size_t stride = std::max(1.0, double(signalLength) / count);

std::vector<std::vector<T>> result;
result.resize(channels);
for (size_t i = 0; i < channels; i++) {
result[i].reserve(count);
}
result.reserve(count);

for (size_t i = 0; i < data.size(); i += stride) {
for (size_t j = 0; j < channels; j++) {
result[j].push_back(data[i + j]);
}
}

return result;
}

template<typename T>
[[nodiscard]] std::vector<T> sampleData(const std::vector<T> &data, size_t count) {
size_t stride = std::max(1.0, double(data.size()) / count);
Expand Down
122 changes: 70 additions & 52 deletions plugins/visualizers/source/content/pl_visualizers/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ namespace hex::plugin::visualizers {
auto wavePattern = arguments[0].toPattern();
auto channels = arguments[1].toUnsigned();
auto sampleRate = arguments[2].toUnsigned();
u32 downSampling = wavePattern->getSize() / 300_scaled / 8 / channels;

static std::vector<i16> waveData, sampledData;
static std::vector<i16> waveData;
static std::vector<std::vector<i16>> sampledData;
sampledData.resize(channels);
static ma_device audioDevice;
static ma_device_config deviceConfig;
static bool shouldStop = false;
Expand All @@ -28,14 +31,16 @@ namespace hex::plugin::visualizers {
throw std::logic_error(hex::format("Invalid sample rate: {}", sampleRate));
else if (channels == 0)
throw std::logic_error(hex::format("Invalid channel count: {}", channels));

u64 sampledIndex;
if (shouldReset) {
waveData.clear();

resetTask = TaskManager::createTask("hex.visualizers.pl_visualizer.task.visualizing"_lang, TaskManager::NoProgress, [=](Task &) {
ma_device_stop(&audioDevice);
waveData = patternToArray<i16>(wavePattern.get());
sampledData = sampleData(waveData, 300_scaled * 4);
if (waveData.empty())
return;
sampledData = sampleChannels(waveData, 300_scaled * 4, channels);
index = 0;

deviceConfig = ma_device_config_init(ma_device_type_playback);
Expand All @@ -51,74 +56,87 @@ namespace hex::plugin::visualizers {
}

ma_copy_pcm_frames(pOutput, waveData.data() + index, frameCount, device->playback.format, device->playback.channels);
index += frameCount;
index += frameCount * device->playback.channels;
};

ma_device_init(nullptr, &deviceConfig, &audioDevice);
});
}

sampledIndex = index / downSampling;
ImGui::BeginDisabled(resetTask.isRunning());

u32 waveDataSize = waveData.size();
u32 sampledDataSize = sampledData[0].size();
auto subplotFlags = ImPlotSubplotFlags_LinkAllX | ImPlotSubplotFlags_LinkCols | ImPlotSubplotFlags_NoResize;
auto plotFlags = ImPlotFlags_CanvasOnly | ImPlotFlags_NoFrame | ImPlotFlags_NoInputs;
auto axisFlags = ImPlotAxisFlags_NoDecorations | ImPlotAxisFlags_NoMenus | ImPlotAxisFlags_AutoFit;
ImPlot::PushStyleVar(ImPlotStyleVar_PlotPadding, ImVec2(0, 0));
if (ImPlot::BeginPlot("##amplitude_plot", scaled(ImVec2(300, 80)), ImPlotFlags_CanvasOnly | ImPlotFlags_NoFrame | ImPlotFlags_NoInputs)) {
ImPlot::SetupAxes("##time", "##amplitude", ImPlotAxisFlags_NoDecorations | ImPlotAxisFlags_NoMenus, ImPlotAxisFlags_NoDecorations | ImPlotAxisFlags_NoMenus);
ImPlot::SetupAxesLimits(0, waveData.size(), std::numeric_limits<i16>::min(), std::numeric_limits<i16>::max(), ImGuiCond_Always);

double dragPos = index;
if (ImPlot::DragLineX(1, &dragPos, ImGui::GetStyleColorVec4(ImGuiCol_Text))) {
if (dragPos < 0) dragPos = 0;
if (dragPos >= waveData.size()) dragPos = waveData.size() - 1;
if (ImPlot::BeginSubplots("##AxisLinking", channels, 1, scaled(ImVec2(300, 80 * channels)), subplotFlags)) {
for (u32 i = 0; i < channels; i++) {
if (ImPlot::BeginPlot("##amplitude_plot", scaled(ImVec2(300, 80)), plotFlags)) {

index = dragPos;
}

ImPlot::PlotLine("##audio", sampledData.data(), sampledData.size());
ImPlot::SetupAxes("##time", "##amplitude", axisFlags, axisFlags);
double dragPos = sampledIndex;
if (ImPlot::DragLineX(1, &dragPos, ImGui::GetStyleColorVec4(ImGuiCol_Text))) {
if (dragPos < 0) dragPos = 0;
if (dragPos >= sampledDataSize) dragPos = sampledDataSize - 1;

ImPlot::EndPlot();
}
ImPlot::PopStyleVar();

{
const u64 min = 0, max = waveData.size();
ImGui::PushItemWidth(300_scaled);
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::SliderScalar("##index", ImGuiDataType_U64, &index, &min, &max, "");
ImGui::PopStyleVar();
ImGui::PopItemWidth();
}
sampledIndex = dragPos;
}
ImPlot::PlotLine("##audio", sampledData[i].data(), sampledDataSize);

if (shouldStop) {
shouldStop = false;
ma_device_stop(&audioDevice);
}
ImPlot::EndPlot();
}
}
ImPlot::PopStyleVar();

index = sampledIndex * downSampling;
{
const u64 min = 0, max = sampledDataSize-1;
ImGui::PushItemWidth(300_scaled);
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::SliderScalar("##index", ImGuiDataType_U64, &sampledIndex, &min, &max, "");
ImGui::PopStyleVar();
ImGui::PopItemWidth();
}
index = sampledIndex * downSampling;

bool playing = ma_device_is_started(&audioDevice);

if (ImGuiExt::IconButton(playing ? ICON_VS_DEBUG_PAUSE : ICON_VS_PLAY, ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_ToolbarGreen))) {
if (playing)
if (shouldStop) {
shouldStop = false;
ma_device_stop(&audioDevice);
else
ma_device_start(&audioDevice);
}
}

ImGui::SameLine();
bool playing = ma_device_is_started(&audioDevice);

if (ImGuiExt::IconButton(ICON_VS_DEBUG_STOP, ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_ToolbarRed))) {
index = 0;
ma_device_stop(&audioDevice);
}
if (ImGuiExt::IconButton(playing ? ICON_VS_DEBUG_PAUSE : ICON_VS_PLAY, ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_ToolbarGreen))) {
if (playing)
ma_device_stop(&audioDevice);
else
ma_device_start(&audioDevice);
}

ImGui::EndDisabled();
ImGui::SameLine();

ImGui::SameLine();
if (ImGuiExt::IconButton(ICON_VS_DEBUG_STOP, ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_ToolbarRed))) {
index = 0;
sampledIndex = 0;
ma_device_stop(&audioDevice);
}

ImGui::EndDisabled();

ImGui::SameLine();
index = sampledIndex * downSampling;

if (resetTask.isRunning())
ImGuiExt::TextSpinner("");
else
ImGuiExt::TextFormatted("{:02d}:{:02d} / {:02d}:{:02d}",
(index / sampleRate) / 60, (index / sampleRate) % 60,
(waveData.size() / sampleRate) / 60, (waveData.size() / sampleRate) % 60);
if (resetTask.isRunning())
ImGuiExt::TextSpinner("");
else
ImGuiExt::TextFormatted("{:02d}:{:02d}:{:03d} / {:02d}:{:02d}:{:03d}",
(index / sampleRate / channels) / 60, (index / sampleRate / channels) % 60, (index * 1000 / sampleRate / channels ) % 1000,
((waveDataSize-1) / sampleRate / channels) / 60, ((waveDataSize-1) / sampleRate / channels) % 60, ((waveDataSize-1) * 1000 / sampleRate / channels) % 1000);
ImPlot::EndSubplots();
}
}

}
Loading