c++ audio visualizer memory bounds bug
hey i m building a audio visualizer and in the live microphone version , the program keeps exiting (
Job 1, 'output/main' terminated by signal SIGSEGV (Address boundary error)
, i tried avoiding the buffer garbage value and checking for possible memory flaws but still nothing , even tried using multiple buffers but still the result is still a program exit ,can someone address the bug?
here is the headers.h:
#ifndef HEADERS_H
#define HEADERS_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
class SDLinit{
private:
SDL_Window* window;
SDL_Renderer* renderer;
TTF_Font* font;
public:
SDL_Renderer* getrenderer(){return renderer;}
SDLinit(const std::string title,int w,int h);
~SDLinit();
void clear();
void present();
void drawbut(int x,int y,int w,int h,int r,int g,int b,const std::string &text);
};
SDLinit::SDLinit(const std::string title,int w,int h){
SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO);
window=SDL_CreateWindow(title.c_str(),SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,w,h,SDL_WINDOW_SHOWN);
renderer=SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);
TTF_Init();
font=TTF_OpenFont("font.ttf",50);
}
SDLinit::~SDLinit(){
if(renderer){SDL_DestroyRenderer(renderer);}
if(window){SDL_DestroyWindow(window);}
if(font){TTF_CloseFont(font);}
TTF_Quit();
SDL_Quit();
}
void SDLinit::clear(){
if (renderer){
SDL_SetRenderDrawColor(renderer,156,90,60,255);
SDL_RenderClear(renderer);
}
}
void SDLinit::present(){
if(renderer){
SDL_RenderPresent(renderer);
}
}
void SDLinit::drawbut(int x,int y,int w,int h,int r,int g,int b,const std::string &text){
SDL_SetRenderDrawColor(renderer,r,g,b,255);
SDL_Rect rect6={x,y,w,h};
SDL_RenderFillRect(renderer,&rect6);
SDL_SetRenderDrawColor(renderer,0,0,0,255);
SDL_RenderDrawRect(renderer,&rect6);
SDL_Color white = {120,120,120,255};
SDL_Surface* surf=TTF_RenderText_Solid(font,text.c_str(),white);
SDL_Texture* tex=SDL_CreateTextureFromSurface(renderer,surf);
SDL_Rect rect5={x+27,y+10,w-50,h-30};
SDL_RenderCopy(renderer,tex,NULL,&rect5);
SDL_FreeSurface(surf);
SDL_DestroyTexture(tex);
}
class audiocap{
private:
SDL_AudioDeviceID device;
fftw_plan plan;
public:
float buffer[1024];
float bars[50];
double* fftw_input=fftw_alloc_real(1024);
fftw_complex* fftw_output=fftw_alloc_complex(513);
audiocap();
void startmic();
void processfft();
static void callback(void* userdata,Uint8* stream,int len);
void mode1();
};
class uinter{
private:
SDLinit& sdl;
audiocap& cap;
// using 1 for live audio and 2 for file system
public:
uinter(SDLinit& s,audiocap& c):sdl(s),cap(c){}
void handel(SDL_Event event,int* mode);
void layout(int* mode);
bool checkmouse(int x,int y,int lx,int rx,int upy,int dwy);
};
void uinter::layout(int* mode){
if (*mode==-1){
sdl.drawbut(150,150,400,150,107, 62, 46,"live audio");
sdl.drawbut(600,150,400,150,245, 230, 211,"file?");
}else if (*mode==1){
int xb=50;
for (int i=0;i<50;i++){
SDL_Rect rect={xb,(int)(720-cap.bars[i]*0.05f),5,(int)(cap.bars[i]*0.05f)};
SDL_Renderer* renderer=sdl.getrenderer();
SDL_SetRenderDrawColor(renderer,0,0,0,255);
SDL_RenderFillRect(renderer,&rect);
xb+=24;
}
}
}
bool uinter::checkmouse(int x,int y,int lx,int rx,int upy,int dwy){
if(x>lx && xupy && ybuffer[i]=samples[i];
}
}
void audiocap::processfft(){
for(int i=0;i<1024;i++){
double hann =0.5*(1.0-cos(2.0*M_PI*i/1023.0));
fftw_input[i]=buffer[i]*hann;
}fftw_execute(plan);
for(int b=0;b<50;b++){
float low_freq=20.0*pow(1000.0f,float(b)/50.0f);
float high_freq=20.0*pow(1000.0f,float(b+1)/50.0f);
int bin_low= (int)(low_freq/43.0f);
int bin_high= (int)(high_freq/43.0f);
bin_low = std::max(1, std::min(bin_low, 512));
bin_high = std::max(1, std::min(bin_high, 512));
std::cout << "b=" << b << " bin_low=" << bin_low << " bin_high=" << bin_high << std::endl;
float sum=0;
for(int h=bin_low;h<=bin_high;h++){
double real=fftw_output[h][0];
double img=fftw_output[h][1];
sum+=sqrt(real*real+img*img);
}
float raw=sum/(bin_high- bin_low +1);
bars[b]=bars[b]*0.8+raw*0.2f;
}
}
#endif
and this the main.cpp :
#include "headers.h"
int main(){
bool mon=false;
SDLinit sdl("fuckass visualizer" , 1280,720);
audiocap cap;
uinter uic(sdl,cap);
bool running =true;
SDL_Event event;
int mode=-1;
while(running){
if (mode==1 && mon==false){cap.startmic();mon=true;}
while(SDL_PollEvent(&event)){
if(event.type==SDL_QUIT){
running=false;
sdl.~SDLinit();
}
uic.handel(event,&mode);
}
sdl.clear();
if(mode==1){
cap.processfft();
}
uic.layout(&mode);
sdl.present();
}
return 0;
}