NoiceSynth/main.cpp
2026-02-27 19:55:28 +01:00

150 lines
4.8 KiB
C++

#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"
#include <SDL2/SDL.h>
#include <vector>
#include <atomic>
#include "synth_engine.h" // Include our portable engine
#include <stdio.h>
// --- Configuration ---
const uint32_t SAMPLE_RATE = 44100;
const uint32_t CHANNELS = 1; // Mono
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;
// --- Visualization Buffer ---
const size_t VIS_BUFFER_SIZE = 8192;
std::vector<int16_t> vis_buffer(VIS_BUFFER_SIZE, 0);
std::atomic<size_t> vis_write_index{0};
// --- Global Synth Engine Instance ---
// The audio callback needs access to our synth, so we make it global.
SynthEngine engine(SAMPLE_RATE);
/**
* @brief The audio callback function that miniaudio will call.
*
* This function acts as the bridge between the audio driver and our synth engine.
* It asks the engine to fill the audio buffer provided by the driver.
*/
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount) {
(void)pDevice; // Unused
(void)pInput; // Unused
// Cast the output buffer to the format our engine expects (int16_t).
int16_t* pOutputS16 = (int16_t*)pOutput;
// Tell our engine to process `frameCount` samples and fill the buffer.
engine.process(pOutputS16, frameCount);
// Copy to visualization buffer
size_t idx = vis_write_index.load(std::memory_order_relaxed);
for (ma_uint32 i = 0; i < frameCount; ++i) {
vis_buffer[idx] = pOutputS16[i];
idx = (idx + 1) % VIS_BUFFER_SIZE;
}
vis_write_index.store(idx, std::memory_order_relaxed);
}
int main(int argc, char* argv[]) {
(void)argc; (void)argv;
// --- Init SDL ---
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
return -1;
}
SDL_Window* window = SDL_CreateWindow("NoiceSynth Scope", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
if (!window) return -1;
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!renderer) return -1;
ma_device_config config = ma_device_config_init(ma_device_type_playback);
config.playback.format = ma_format_s16; // Must match our engine's output format
config.playback.channels = CHANNELS;
config.sampleRate = SAMPLE_RATE;
config.dataCallback = data_callback;
ma_device device;
if (ma_device_init(NULL, &config, &device) != MA_SUCCESS) {
printf("Failed to initialize playback device.\n");
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return -1;
}
printf("Device Name: %s\n", device.playback.name);
ma_device_start(&device);
// --- Main Loop ---
bool quit = false;
SDL_Event e;
while (!quit) {
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
quit = true;
}
}
// Clear screen
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
// Draw Waveform
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); // Green
// Determine read position (snapshot atomic write index)
size_t write_idx = vis_write_index.load(std::memory_order_relaxed);
// Find trigger (zero crossing) to stabilize the display
// Look back from write_idx to find a stable point
size_t search_start_offset = 2000;
size_t read_idx = (write_idx + VIS_BUFFER_SIZE - search_start_offset) % VIS_BUFFER_SIZE;
// Simple trigger search: find crossing from negative to positive
for (size_t i = 0; i < 1000; ++i) {
int16_t s1 = vis_buffer[read_idx];
size_t next_idx = (read_idx + 1) % VIS_BUFFER_SIZE;
int16_t s2 = vis_buffer[next_idx];
if (s1 <= 0 && s2 > 0) {
read_idx = next_idx; // Found trigger
break;
}
read_idx = next_idx;
}
// Draw points
int prev_x = 0;
int prev_y = WINDOW_HEIGHT / 2;
for (int x = 0; x < WINDOW_WIDTH; ++x) {
int16_t sample = vis_buffer[read_idx];
read_idx = (read_idx + 1) % VIS_BUFFER_SIZE;
// Map 16-bit sample (-32768 to 32767) to screen height
// Invert Y because screen Y grows downwards
int y = WINDOW_HEIGHT / 2 - (sample * (WINDOW_HEIGHT / 2) / 32768);
if (x > 0) {
SDL_RenderDrawLine(renderer, prev_x, prev_y, x, y);
}
prev_x = x;
prev_y = y;
}
SDL_RenderPresent(renderer);
}
ma_device_uninit(&device);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}