diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index a1e4e93..4e76774 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -91,4 +91,5 @@ if (GGWAVE_SUPPORT_SDL2) endif() add_subdirectory(waver) + add_subdirectory(goertzel) endif() diff --git a/examples/goertzel/CMakeLists.txt b/examples/goertzel/CMakeLists.txt new file mode 100644 index 0000000..5a5b379 --- /dev/null +++ b/examples/goertzel/CMakeLists.txt @@ -0,0 +1,16 @@ +set(TARGET goertzel) + +add_executable(${TARGET} main.cpp) + +target_include_directories(${TARGET} PRIVATE + .. + ${SDL2_INCLUDE_DIRS} + ) + +target_link_libraries(${TARGET} PRIVATE + ggwave + ggwave-common + ggwave-common-sdl2 + imgui-sdl2 + ${CMAKE_THREAD_LIBS_INIT} + ) diff --git a/examples/goertzel/main.cpp b/examples/goertzel/main.cpp new file mode 100644 index 0000000..bba105b --- /dev/null +++ b/examples/goertzel/main.cpp @@ -0,0 +1,508 @@ +#include "ggwave/ggwave.h" +#include "ggwave-common.h" + +#include + +#include +#include + +#include +#include +#include + +namespace { + +std::string g_defaultCaptureDeviceName = ""; + +SDL_AudioDeviceID g_devIdInp = 0; +SDL_AudioDeviceID g_devIdOut = 0; + +SDL_AudioSpec g_obtainedSpecInp; +SDL_AudioSpec g_obtainedSpecOut; + +struct FreqData { + float freq; + + int head = 0; + std::vector mag; +}; + +bool g_isCapturing = true; +int g_nSamplesPerFrame = 1024; +std::vector g_freqData; + +// ref : https://github.com/Harvie/Programs/blob/master/c/goertzel/goertzel.c +float goertzel_mag(int numSamples, float TARGET_FREQUENCY, int SAMPLING_RATE, float* data) { + int k,i; + float floatnumSamples; + float omega,sine,cosine,coeff,q0,q1,q2,magnitude,real,imag; + + float scalingFactor = numSamples / 2.0; + + floatnumSamples = (float) numSamples; + k = (int) (0.5 + ((floatnumSamples * TARGET_FREQUENCY) / (float)SAMPLING_RATE)); + omega = (2.0 * M_PI * k) / floatnumSamples; + sine = sin(omega); + cosine = cos(omega); + coeff = 2.0 * cosine; + q0=0; + q1=0; + q2=0; + + for (i = 0; i < numSamples; i++) { + q0 = coeff * q1 - q2 + data[i]; + q2 = q1; + q1 = q0; + } + + // calculate the real and imaginary results + // scaling appropriately + real = (q1 * cosine - q2) / scalingFactor; + imag = (q1 * sine) / scalingFactor; + + magnitude = sqrtf(real*real + imag*imag); + //phase = atan(imag/real) + return magnitude; +} + +} + +void GGWave_setDefaultCaptureDeviceName(std::string name) { + g_defaultCaptureDeviceName = std::move(name); +} + +bool GGWave_init( + const int playbackId, + const int captureId) { + + if (g_devIdInp && g_devIdOut) { + return false; + } + + if (g_devIdInp == 0 && g_devIdOut == 0) { + SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); + + if (SDL_Init(SDL_INIT_AUDIO) < 0) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError()); + return (1); + } + + SDL_SetHintWithPriority(SDL_HINT_AUDIO_RESAMPLING_MODE, "medium", SDL_HINT_OVERRIDE); + + { + int nDevices = SDL_GetNumAudioDevices(SDL_FALSE); + printf("Found %d playback devices:\n", nDevices); + for (int i = 0; i < nDevices; i++) { + printf(" - Playback device #%d: '%s'\n", i, SDL_GetAudioDeviceName(i, SDL_FALSE)); + } + } + { + int nDevices = SDL_GetNumAudioDevices(SDL_TRUE); + printf("Found %d capture devices:\n", nDevices); + for (int i = 0; i < nDevices; i++) { + printf(" - Capture device #%d: '%s'\n", i, SDL_GetAudioDeviceName(i, SDL_TRUE)); + } + } + } + + bool reinit = false; + + if (g_devIdOut == 0) { + printf("Initializing playback ...\n"); + + SDL_AudioSpec playbackSpec; + SDL_zero(playbackSpec); + + playbackSpec.freq = GGWave::kBaseSampleRate; + playbackSpec.format = AUDIO_S16SYS; + playbackSpec.channels = 1; + playbackSpec.samples = 16*1024; + playbackSpec.callback = NULL; + + SDL_zero(g_obtainedSpecOut); + + if (playbackId >= 0) { + printf("Attempt to open playback device %d : '%s' ...\n", playbackId, SDL_GetAudioDeviceName(playbackId, SDL_FALSE)); + g_devIdOut = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(playbackId, SDL_FALSE), SDL_FALSE, &playbackSpec, &g_obtainedSpecOut, 0); + } else { + printf("Attempt to open default playback device ...\n"); + g_devIdOut = SDL_OpenAudioDevice(NULL, SDL_FALSE, &playbackSpec, &g_obtainedSpecOut, 0); + } + + if (!g_devIdOut) { + printf("Couldn't open an audio device for playback: %s!\n", SDL_GetError()); + g_devIdOut = 0; + } else { + printf("Obtained spec for output device (SDL Id = %d):\n", g_devIdOut); + printf(" - Sample rate: %d (required: %d)\n", g_obtainedSpecOut.freq, playbackSpec.freq); + printf(" - Format: %d (required: %d)\n", g_obtainedSpecOut.format, playbackSpec.format); + printf(" - Channels: %d (required: %d)\n", g_obtainedSpecOut.channels, playbackSpec.channels); + printf(" - Samples per frame: %d (required: %d)\n", g_obtainedSpecOut.samples, playbackSpec.samples); + + if (g_obtainedSpecOut.format != playbackSpec.format || + g_obtainedSpecOut.channels != playbackSpec.channels || + g_obtainedSpecOut.samples != playbackSpec.samples) { + g_devIdOut = 0; + SDL_CloseAudio(); + fprintf(stderr, "Failed to initialize playback SDL_OpenAudio!"); + + return false; + } + + reinit = true; + } + } + + if (g_devIdInp == 0) { + SDL_AudioSpec captureSpec; + captureSpec = g_obtainedSpecOut; + captureSpec.freq = GGWave::kBaseSampleRate; + captureSpec.format = AUDIO_F32SYS; + captureSpec.samples = g_nSamplesPerFrame; + + SDL_zero(g_obtainedSpecInp); + + if (captureId >= 0) { + printf("Attempt to open capture device %d : '%s' ...\n", captureId, SDL_GetAudioDeviceName(captureId, SDL_FALSE)); + g_devIdInp = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(captureId, SDL_TRUE), SDL_TRUE, &captureSpec, &g_obtainedSpecInp, 0); + } else { + printf("Attempt to open default capture device ...\n"); + g_devIdInp = SDL_OpenAudioDevice(g_defaultCaptureDeviceName.empty() ? nullptr : g_defaultCaptureDeviceName.c_str(), + SDL_TRUE, &captureSpec, &g_obtainedSpecInp, 0); + } + if (!g_devIdInp) { + printf("Couldn't open an audio device for capture: %s!\n", SDL_GetError()); + g_devIdInp = 0; + } else { + printf("Obtained spec for input device (SDL Id = %d):\n", g_devIdInp); + printf(" - Sample rate: %d\n", g_obtainedSpecInp.freq); + printf(" - Format: %d (required: %d)\n", g_obtainedSpecInp.format, captureSpec.format); + printf(" - Channels: %d (required: %d)\n", g_obtainedSpecInp.channels, captureSpec.channels); + printf(" - Samples per frame: %d\n", g_obtainedSpecInp.samples); + + reinit = true; + } + } + + GGWave::SampleFormat sampleFormatInp = GGWAVE_SAMPLE_FORMAT_UNDEFINED; + GGWave::SampleFormat sampleFormatOut = GGWAVE_SAMPLE_FORMAT_UNDEFINED; + + switch (g_obtainedSpecInp.format) { + case AUDIO_U8: sampleFormatInp = GGWAVE_SAMPLE_FORMAT_U8; break; + case AUDIO_S8: sampleFormatInp = GGWAVE_SAMPLE_FORMAT_I8; break; + case AUDIO_U16SYS: sampleFormatInp = GGWAVE_SAMPLE_FORMAT_U16; break; + case AUDIO_S16SYS: sampleFormatInp = GGWAVE_SAMPLE_FORMAT_I16; break; + case AUDIO_S32SYS: sampleFormatInp = GGWAVE_SAMPLE_FORMAT_F32; break; + case AUDIO_F32SYS: sampleFormatInp = GGWAVE_SAMPLE_FORMAT_F32; break; + } + + switch (g_obtainedSpecOut.format) { + case AUDIO_U8: sampleFormatOut = GGWAVE_SAMPLE_FORMAT_U8; break; + case AUDIO_S8: sampleFormatOut = GGWAVE_SAMPLE_FORMAT_I8; break; + case AUDIO_U16SYS: sampleFormatOut = GGWAVE_SAMPLE_FORMAT_U16; break; + case AUDIO_S16SYS: sampleFormatOut = GGWAVE_SAMPLE_FORMAT_I16; break; + case AUDIO_S32SYS: sampleFormatOut = GGWAVE_SAMPLE_FORMAT_F32; break; + case AUDIO_F32SYS: sampleFormatOut = GGWAVE_SAMPLE_FORMAT_F32; break; + break; + } + + return true; +} + +bool GGWave_mainLoop() { + if (g_devIdInp == 0 && g_devIdOut == 0) { + return false; + } + + static GGWave::CBWaveformOut cbQueueAudio = [&](const void * data, uint32_t nBytes) { + SDL_QueueAudio(g_devIdOut, data, nBytes); + }; + + static GGWave::CBWaveformInp cbWaveformInp = [&](void * data, uint32_t nMaxBytes) { + return SDL_DequeueAudio(g_devIdInp, data, nMaxBytes); + }; + + SDL_PauseAudioDevice(g_devIdInp, SDL_FALSE); + if (!g_isCapturing) { + SDL_ClearQueuedAudio(g_devIdInp); + } + + int n = 0; + float data[g_nSamplesPerFrame]; + do { + n = cbWaveformInp(data, sizeof(float)*g_nSamplesPerFrame); + if (n <= 0) break; + + for (int i = 0; i < (int) g_freqData.size(); ++i) { + g_freqData[i].mag[g_freqData[i].head] = goertzel_mag(g_nSamplesPerFrame, g_freqData[i].freq, GGWave::kBaseSampleRate, data); + if (++g_freqData[i].head == (int) g_freqData[i].mag.size()) { + g_freqData[i].head = 0; + } + } + } while (n > 0); + + return true; +} + +bool GGWave_deinit() { + if (g_devIdInp == 0 && g_devIdOut == 0) { + return false; + } + + SDL_PauseAudioDevice(g_devIdInp, 1); + SDL_CloseAudioDevice(g_devIdInp); + SDL_PauseAudioDevice(g_devIdOut, 1); + SDL_CloseAudioDevice(g_devIdOut); + SDL_CloseAudio(); + SDL_Quit(); + + return true; +} + +bool ImGui_BeginFrame(SDL_Window * window) { + SDL_Event event; + while (SDL_PollEvent(&event)) + { + ImGui_ProcessEvent(&event); + if (event.type == SDL_QUIT) return false; + if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window)) return false; + } + + ImGui_NewFrame(window); + + return true; +} + +bool ImGui_EndFrame(SDL_Window * window) { + // Rendering + int display_w, display_h; + SDL_GetWindowSize(window, &display_w, &display_h); + glViewport(0, 0, display_w, display_h); + glClearColor(0.0f, 0.0f, 0.0f, 0.4f); + glClear(GL_COLOR_BUFFER_BIT); + + ImGui::Render(); + ImGui_RenderDrawData(ImGui::GetDrawData()); + + SDL_GL_SwapWindow(window); + + return true; +} + +bool ImGui_SetStyle() { + ImGuiStyle & style = ImGui::GetStyle(); + + style.AntiAliasedFill = true; + style.AntiAliasedLines = true; + style.WindowRounding = 0.0f; + + style.WindowPadding = ImVec2(8, 8); + style.WindowRounding = 0.0f; + style.FramePadding = ImVec2(4, 3); + style.FrameRounding = 0.0f; + style.ItemSpacing = ImVec2(8, 4); + style.ItemInnerSpacing = ImVec2(4, 4); + style.IndentSpacing = 21.0f; + style.ScrollbarSize = 16.0f; + style.ScrollbarRounding = 9.0f; + style.GrabMinSize = 10.0f; + style.GrabRounding = 3.0f; + + style.Colors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); + style.Colors[ImGuiCol_TextDisabled] = ImVec4(0.24f, 0.41f, 0.41f, 1.00f); + style.Colors[ImGuiCol_WindowBg] = ImVec4(0.08f, 0.08f, 0.08f, 0.94f); + //style.Colors[ImGuiCol_ChildWindowBg] = ImVec4(0.07f, 0.07f, 0.09f, 1.00f); + style.Colors[ImGuiCol_PopupBg] = ImVec4(0.07f, 0.07f, 0.09f, 1.00f); + style.Colors[ImGuiCol_Border] = ImVec4(0.31f, 0.31f, 0.31f, 0.71f); + style.Colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); + style.Colors[ImGuiCol_FrameBg] = ImVec4(0.00f, 0.39f, 0.39f, 0.39f); + style.Colors[ImGuiCol_FrameBgHovered] = ImVec4(0.26f, 1.00f, 1.00f, 0.39f); + style.Colors[ImGuiCol_FrameBgActive] = ImVec4(0.00f, 0.78f, 0.00f, 1.00f); + style.Colors[ImGuiCol_TitleBg] = ImVec4(0.00f, 0.50f, 0.50f, 0.70f); + style.Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.00f, 0.50f, 0.50f, 1.00f); + style.Colors[ImGuiCol_TitleBgActive] = ImVec4(0.00f, 0.70f, 0.70f, 1.00f); + style.Colors[ImGuiCol_MenuBarBg] = ImVec4(0.00f, 0.70f, 0.70f, 1.00f); + style.Colors[ImGuiCol_ScrollbarBg] = ImVec4(0.10f, 0.27f, 0.27f, 1.00f); + style.Colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.80f, 0.80f, 0.83f, 0.31f); + style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.26f, 1.00f, 1.00f, 0.39f); + style.Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.00f, 0.78f, 0.00f, 1.00f); + //style.Colors[ImGuiCol_ComboBg] = ImVec4(0.00f, 0.39f, 0.39f, 1.00f); + style.Colors[ImGuiCol_CheckMark] = ImVec4(0.80f, 0.80f, 0.83f, 0.39f); + style.Colors[ImGuiCol_SliderGrab] = ImVec4(0.80f, 0.80f, 0.83f, 0.39f); + style.Colors[ImGuiCol_SliderGrabActive] = ImVec4(0.00f, 0.78f, 0.00f, 1.00f); + style.Colors[ImGuiCol_Button] = ImVec4(0.13f, 0.55f, 0.55f, 1.00f); + style.Colors[ImGuiCol_ButtonHovered] = ImVec4(0.61f, 1.00f, 0.00f, 0.51f); + style.Colors[ImGuiCol_ButtonActive] = ImVec4(0.00f, 0.78f, 0.00f, 1.00f); + style.Colors[ImGuiCol_Header] = ImVec4(0.79f, 0.51f, 0.00f, 0.51f); + style.Colors[ImGuiCol_HeaderHovered] = ImVec4(0.79f, 0.51f, 0.00f, 0.67f); + style.Colors[ImGuiCol_HeaderActive] = ImVec4(0.79f, 0.51f, 0.00f, 0.67f); + //style.Colors[ImGuiCol_Column] = ImVec4(0.79f, 0.51f, 0.00f, 0.67f); + //style.Colors[ImGuiCol_ColumnHovered] = ImVec4(0.25f, 1.00f, 0.00f, 1.00f); + //style.Colors[ImGuiCol_ColumnActive] = ImVec4(0.79f, 0.51f, 0.00f, 0.67f); + style.Colors[ImGuiCol_ResizeGrip] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); + style.Colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.26f, 1.00f, 1.00f, 0.39f); + style.Colors[ImGuiCol_ResizeGripActive] = ImVec4(0.00f, 0.78f, 0.00f, 1.00f); + //style.Colors[ImGuiCol_CloseButton] = ImVec4(0.40f, 0.39f, 0.38f, 0.16f); + //style.Colors[ImGuiCol_CloseButtonHovered] = ImVec4(0.26f, 1.00f, 1.00f, 0.39f); + //style.Colors[ImGuiCol_CloseButtonActive] = ImVec4(0.79f, 0.51f, 0.00f, 0.67f); + style.Colors[ImGuiCol_PlotLines] = ImVec4(1.00f, 0.65f, 0.38f, 0.67f); + style.Colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.25f, 1.00f, 0.00f, 1.00f); + style.Colors[ImGuiCol_PlotHistogram] = ImVec4(1.00f, 0.65f, 0.38f, 0.67f); + style.Colors[ImGuiCol_PlotHistogramHovered] = ImVec4(0.25f, 1.00f, 0.00f, 1.00f); + style.Colors[ImGuiCol_TextSelectedBg] = ImVec4(0.25f, 1.00f, 0.00f, 0.43f); + style.Colors[ImGuiCol_ModalWindowDarkening] = ImVec4(1.00f, 0.98f, 0.95f, 0.78f); + + return true; +} + +static std::function g_doInit; +static std::function g_setWindowSize; +static std::function g_mainUpdate; + +void mainUpdate(void *) { + g_mainUpdate(); +} + +int main(int argc, char** argv) { + auto argm = parseCmdArguments(argc, argv); + int captureId = argm["c"].empty() ? 0 : std::stoi(argm["c"]); + int playbackId = argm["p"].empty() ? 0 : std::stoi(argm["p"]); + + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + fprintf(stderr, "Error: %s\n", SDL_GetError()); + return -1; + } + + ImGui_PreInit(); + + int windowX = 1920; + int windowY = 1200; + + const char * windowTitle = "goertzel experiment"; + + SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI); + SDL_Window * window = SDL_CreateWindow(windowTitle, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowX, windowY, window_flags); + + void * gl_context = SDL_GL_CreateContext(window); + + SDL_GL_MakeCurrent(window, gl_context); + SDL_GL_SetSwapInterval(1); // Enable vsync + + ImGui_Init(window, gl_context); + ImGui::GetIO().IniFilename = nullptr; + + ImGui_SetStyle(); + + ImGui_NewFrame(window); + ImGui::Render(); + + bool isInitialized = false; + + g_doInit = [&]() { + if (GGWave_init(playbackId, captureId) == false) { + fprintf(stderr, "Failed to initialize GGWave\n"); + return false; + } + + int nHistory = (3*GGWave::kBaseSampleRate)/g_nSamplesPerFrame; + + g_freqData.resize(96); + for (int i = 0; i < 96; ++i) { + g_freqData[i].freq = 1875.000 + 46.875*i; + g_freqData[i].mag.resize(nHistory); + } + + isInitialized = true; + + return true; + }; + + g_setWindowSize = [&](int sizeX, int sizeY) { + SDL_SetWindowSize(window, sizeX, sizeY); + }; + + g_mainUpdate = [&]() { + if (isInitialized == false) { + return true; + } + + if (ImGui_BeginFrame(window) == false) { + return false; + } + + const auto& displaySize = ImGui::GetIO().DisplaySize; + + ImGui::SetNextWindowPos({ 0, 0, }); + ImGui::SetNextWindowSize(displaySize); + ImGui::Begin("Main", nullptr, + ImGuiWindowFlags_NoMove | + ImGuiWindowFlags_NoTitleBar | + ImGuiWindowFlags_NoScrollbar | + ImGuiWindowFlags_NoResize | + ImGuiWindowFlags_NoSavedSettings); + + auto drawList = ImGui::GetWindowDrawList(); + const float dx = displaySize.x/(g_freqData[0].mag.size() + 1); + const float dy = displaySize.y/(g_freqData.size() + 1); + + float sum = 0.0; + int nf = (int) g_freqData.size(); + int ns = (int) g_freqData[0].mag.size(); + for (int i = 0; i < nf; ++i) { + for (int j = 0; j < ns; ++j) { + sum += g_freqData[i].mag[j]; + } + } + sum /= (nf*ns); + + auto p0 = ImGui::GetCursorScreenPos(); + for (int i = 0; i < nf; ++i) { + for (int j = 0; j < ns; ++j) { + int k = g_freqData[i].head + j; + if (k >= ns) k -= ns; + auto v = g_freqData[i].mag[k]; + ImVec4 c = { 0.0f, 1.0f, 0.0, 0.0f }; + c.w = v/(5.0*sum); + drawList->AddRectFilled({ p0.x + j*dx, p0.y + i*dy }, { p0.x + j*dx + dx - 1, p0.y + i*dy + dy - 1 }, ImGui::ColorConvertFloat4ToU32(c)); + } + } + + //for (int i = 0; i < (int) g_freqData.size(); ++i) { + // ImGui::PushID(i); + // ImGui::PlotLines("##signal", g_freqData[i].mag.data(), g_freqData[i].mag.size(), g_freqData[i].head, std::to_string(g_freqData[i].freq).c_str(), 0.0f, FLT_MAX, { ImGui::GetContentRegionAvailWidth(), 20 }); + // ImGui::PopID(); + //} + ImGui::End(); + + if (ImGui::IsKeyPressed(40)) { + g_isCapturing = !g_isCapturing; + } + + GGWave_mainLoop(); + + ImGui_EndFrame(window); + + return true; + }; + + if (g_doInit() == false) { + printf("Error: failed to initialize audio\n"); + return -2; + } + + while (true) { + if (g_mainUpdate() == false) break; + } + + GGWave_deinit(); + + // Cleanup + ImGui_Shutdown(); + ImGui::DestroyContext(); + + SDL_GL_DeleteContext(gl_context); + SDL_DestroyWindow(window); + SDL_Quit(); + + return 0; +}