Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- #include <fstream>
- #include <memory>
- #include <math.h>
- #include "bass.h"
- #include "bassflac.h"
- #include "bass_fx.h"
- #include "GL/glew.h"
- #include "GL/glfw.h"
- #include <string>
- #include <Windows.h>
- #include "BPM_Custom.h"
- int inputStream;
- int inputStream_ply;
- float fft[512];
- float oldFFT[512];
- float fftMult = 5.0f;
- int WIDTH = 1280;
- int HEIGHT = 720;
- int FULL_WIDTH = NULL;
- int FULL_HEIGHT = NULL;
- int fboresx = NULL;
- int fboresy = NULL;
- int startId = 0;
- int nextId = 0;
- //framebuffer test stuff
- unsigned int fbo; // The frame buffer object
- unsigned int fbo_depth; // The depth buffer for the frame buffer object
- unsigned int fbo_texture; // The texture object to write our frame buffer object to
- GLuint image1;
- GLuint image2;
- GLuint image3;
- GLuint program; // for shader
- GLuint program2; // for shader
- float multGain = 0.12f;
- float autoGain = 1.0f;
- int bass_init = false;
- bool bPunchEnableDotLoop = true;
- bool bPunchOnlySimpleFFTDisplay = false;
- bool bPunchEnableSimpleFFTDisplay = true;
- int iPunchSwitchBoard = 0;
- int g_argc;
- char **g_argv;
- //Window mode (windowed, fullscreen)
- int windowMode = GLFW_WINDOW;
- void printError();
- inline float clamp(float x, float min, float max)
- {
- return x < min ? min : (x > max ? max : x);
- }
- std::string ExePath() {
- char buffer[MAX_PATH];
- GetModuleFileName( NULL, buffer, MAX_PATH );
- std::string::size_type pos = std::string( buffer ).find_last_of( "\\/" );
- return std::string( buffer ).substr( 0, pos);
- }
- char *textFileRead(char *fn) {
- FILE *fp;
- char *content = "";
- int count=0;
- char fn2[MAX_PATH];
- sprintf_s(fn2,MAX_PATH,"%s\\%s",ExePath().c_str(),fn);
- printf("loading file: %s",fn2);
- if (fn2 != NULL) {
- fp = fopen(fn2,"rt");
- if (fp != NULL) {
- fseek(fp, 0, SEEK_END);
- count = ftell(fp);
- rewind(fp);
- if (count > 0) {
- content = (char *)malloc(sizeof(char) * (count+1));
- count = fread(content,sizeof(char),count,fp);
- content[count] = '\0';
- }
- fclose(fp);
- }
- }
- return content;
- }
- void CheckGLErrors(const char* who){
- GLenum error = glGetError();
- if(error!=GL_NO_ERROR){
- printf("GL:%s:",who);
- }
- switch(error){
- case GL_NO_ERROR:
- return;
- break;
- case GL_INVALID_ENUM:
- printf("Invalid enumerator.\n");
- break;
- case GL_INVALID_VALUE:
- printf("Invalid value.\n");
- break;
- case GL_INVALID_OPERATION:
- printf("Invalid operation.\n");
- break;
- case GL_STACK_OVERFLOW:
- printf("Invalid stack overflow.\n");
- break;
- case GL_STACK_UNDERFLOW:
- printf("Invalid stack underflow.\n");
- break;
- case GL_OUT_OF_MEMORY:
- printf("Out of memory.\n");
- break;
- case GL_TABLE_TOO_LARGE:
- printf("Table too large.\n");
- break;
- default:
- printf("Unidentified error.\n");
- break;
- }
- }
- void checkShaderCompile(GLuint v){
- GLint blen = 0;
- GLsizei slen = 0;
- glGetShaderiv(v, GL_INFO_LOG_LENGTH , &blen);
- if (blen > 1)
- {
- GLchar* compiler_log = (GLchar*)malloc(blen);
- glGetInfoLogARB(v, blen, &slen, compiler_log);
- std::cout << "compiler_log:\n"<< compiler_log;
- free (compiler_log);
- system("pause");
- //exit(0);
- }
- }
- void setShaders() {
- printf("SETSHADERS1\n");
- GLuint v, f;
- char *vs, *fs;
- printf("Creating shaders.\n");
- // Create shader handlers
- v = glCreateShader(GL_VERTEX_SHADER);
- f = glCreateShader(GL_FRAGMENT_SHADER);
- printf("Reading source files.\n");
- // Read source code from files
- //vs = textFileRead("F:\\Projects\\Games\\OpenGL\\Punch\\Release\\example.vert");
- //fs = textFileRead("F:\\Projects\\Games\\OpenGL\\Punch\\Release\\example.frag");
- vs = textFileRead("example.vert");
- fs = textFileRead("example.frag");
- CheckGLErrors("Error creating shaders");
- const char * vv = vs;
- const char * ff = fs;
- // Set shader source
- printf("Setting vertex shader source\n");
- glShaderSource(v, 1, &vv,NULL);
- printf("Setting fragment shader source\n");
- glShaderSource(f, 1, &ff,NULL);
- free(vs);
- free(fs);
- CheckGLErrors("Error setting up shader sources");
- printf("Compiling shaders.\n");
- // Compile all shaders
- glCompileShader(v);
- checkShaderCompile(v);
- CheckGLErrors("Error compiling 'v'");
- glCompileShader(f);
- checkShaderCompile(f);
- CheckGLErrors("Error compiling 'f'");
- printf("Creating program.\n");
- // Create the program
- program = glCreateProgram();
- printf("Attaching shaders.\n");
- // Attach shaders to program
- glAttachShader(program,v);
- glAttachShader(program,f);
- printf("Linking program.\n");
- // Link and set program to use
- glLinkProgram(program);
- GLint linked;
- glGetProgramiv(program, GL_LINK_STATUS, &linked);
- if (!linked)
- {
- printf("Couldn't link program.\n");
- }
- printf("Using program.\n");
- CheckGLErrors("Error while setting shaders");
- }
- void setShaders2() {
- printf("SETSHADERS2\n");
- GLuint v, f;
- char *vs, *fs;
- printf("Creating shaders.\n");
- // Create shader handlers
- v = glCreateShader(GL_VERTEX_SHADER);
- f = glCreateShader(GL_FRAGMENT_SHADER);
- printf("Reading source files.\n");
- // Read source code from files
- //vs = textFileRead("F:\\Projects\\Games\\OpenGL\\Punch\\Release\\example.vert");
- //fs = textFileRead("F:\\Projects\\Games\\OpenGL\\Punch\\Release\\example2.frag");
- vs = textFileRead("\\example.vert");
- fs = textFileRead("\\example2.frag");
- CheckGLErrors("Error creating shaders");
- const char * vv = vs;
- const char * ff = fs;
- // Set shader source
- printf("Setting vertex shader source\n");
- glShaderSource(v, 1, &vv,NULL);
- printf("Setting fragment shader source\n");
- glShaderSource(f, 1, &ff,NULL);
- free(vs);
- free(fs);
- CheckGLErrors("Error setting up shader sources");
- printf("Compiling shaders.\n");
- // Compile all shaders
- glCompileShader(v);
- checkShaderCompile(v);
- CheckGLErrors("Error compiling 'v'");
- glCompileShader(f);
- checkShaderCompile(f);
- CheckGLErrors("Error compiling 'f'");
- printf("Creating program.\n");
- // Create the program
- program2 = glCreateProgram();
- printf("Attaching shaders.\n");
- // Attach shaders to program
- glAttachShader(program2,v);
- glAttachShader(program2,f);
- printf("Linking program.\n");
- // Link and set program to use
- glLinkProgram(program2);
- GLint linked;
- glGetProgramiv(program2, GL_LINK_STATUS, &linked);
- if (!linked)
- {
- printf("Couldn't link program.\n");
- }
- printf("Using program.\n");
- CheckGLErrors("Error while setting shaders");
- }
- void fboSetup()
- {
- glGenTextures(1, &fbo_texture); // Generate one texture
- glBindTexture(GL_TEXTURE_2D, fbo_texture); // Bind the texture fbo_texture
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, fboresx , fboresy, 0, GL_RGBA, GL_FLOAT, NULL); // Create a standard texture with the width and height of our window
- // Setup the basic texture parameters
- //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- // Unbind the texture
- glBindTexture(GL_TEXTURE_2D, 0);
- glGenFramebuffers( 1, &fbo );
- glBindFramebuffer( GL_FRAMEBUFFER, fbo );
- glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, fbo_texture, 0); // Attach the texture fbo_texture to the color buffer in our frame buffer
- GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); // Check that status of our generated frame buffer
- if (status != GL_FRAMEBUFFER_COMPLETE_EXT) // If the frame buffer does not report back as complete
- {
- std::cout << "Couldn't create frame buffer" << std::endl; // Output an error to the console
- system("pause");
- exit(0); // Exit the application
- }
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); // Unbind our frame buffer
- CheckGLErrors("There was a problem setting up the FBO.");
- printf("fboSetup() end\n");
- }
- void glSettings()
- {
- glEnable( GL_POINT_SMOOTH );
- glPointSize(5.0f*(FULL_WIDTH/WIDTH));
- fboSetup();
- }
- //Calculates the average of input range
- //TODO: unstable but it works still have to add some checks
- float FFTAverage(float fft[],int index,int range){
- int low = index-(range/2);
- int high = index+(range/2);
- float sum = 0;
- int count = 0;
- for(int i = low;i<high;i++){
- sum+=fft[i];
- count++;
- }
- return sum/count;
- }
- void bassFunc()
- {
- if(BASS_ChannelIsActive(inputStream)==BASS_ACTIVE_STOPPED)
- {
- if(g_argv[nextId+1]!=0){
- nextId+=1;
- }
- else
- {
- nextId=startId;
- }
- printf("Full file: %s\n",g_argv[nextId]);
- std::string fileFull = g_argv[nextId];
- std::string fileExt = fileFull.substr(fileFull.rfind("."),std::string::npos);
- printf("File extension: %s\n",fileExt.c_str());
- //Check if .txt file was provided
- if(fileExt.compare(".txt") == 0)
- {
- //This is a .txt file
- //Read file
- std::string fileContent;
- std::ifstream textFile (fileFull.c_str());
- if (textFile.is_open())
- {
- std::getline(textFile,fileContent);
- printf("Text file content: %s\n",fileContent.c_str());
- }
- else
- {
- printf("Unable to open file.\n");
- system("pause");
- return;
- }
- inputStream = BASS_StreamCreateURL(fileContent.c_str(),0, 0, NULL, 0);
- }
- else
- {
- if(fileExt.compare(".flac") == 0)
- {
- //FLAC File
- inputStream = BASS_FLAC_StreamCreateFile(false,g_argv[nextId],0,0,0);
- }
- else
- {
- //Not a .txt nor FLAC file
- inputStream = BASS_StreamCreateFile(false,g_argv[nextId],0,0,0);
- }
- }
- printError();
- BASS_BFX_DAMP amprams;
- amprams.fTarget = 1.2f;
- amprams.fQuiet = 0.0f;
- amprams.fGain = 4.8f;
- amprams.fRate = 0.6f;
- amprams.fDelay = 0.1f;
- amprams.lChannel = BASS_BFX_CHANALL;
- //int fx = BASS_ChannelSetFX(inputStream,BASS_FX_BFX_DAMP,0);
- //BASS_FXSetParameters(fx,&rams);
- bool play = BASS_ChannelPlay(inputStream,true);
- printError();
- //BASS_ChannelSetAttribute(inputStream,BASS_ATTRIB_VOL,0.2);
- printError();
- printf("init:%i\nplay:%i\n",bass_init,play);
- }
- BASS_ChannelGetData(inputStream,fft,BASS_DATA_FFT1024|BASS_DATA_FLOAT);
- }
- float punchSwitchboardLast = 0.0f;
- float punchSwitchMaxDelta = 10.0f;
- void punchUpdateSwitchboard()
- {
- float punchSwitchCurrentTime = (float)glfwGetTime();
- float punchSwitchDelta = punchSwitchCurrentTime - punchSwitchboardLast;
- if(punchSwitchDelta > punchSwitchMaxDelta)
- {
- if(iPunchSwitchBoard < 2)
- iPunchSwitchBoard += 1;
- else
- iPunchSwitchBoard = 0;
- punchSwitchboardLast = punchSwitchCurrentTime;
- }
- }
- void punchForceSwitchboard()
- {
- if(iPunchSwitchBoard < 2)
- iPunchSwitchBoard += 1;
- else
- iPunchSwitchBoard = 0;
- }
- double timecap = 0;
- float vexRes(float calc)
- {
- calc *= 100;
- calc *= (float)glfwGetTime()*0.1f;
- calc += (float)timecap;
- return calc;
- }
- float sinVex(float calc)
- {
- calc = vexRes(calc);
- return sin(calc)*0.4f;
- }
- float cosVex(float calc)
- {
- calc = vexRes(calc);
- return cos(calc)*0.4f;
- }
- float* multiplyArray3(float array[3], float scale)
- {
- array[0] *= scale;
- array[1] *= scale;
- array[2] *= scale;
- return array;
- }
- void drawSquare(float flConvertedAmp, float scale, float color[3], int gridSize)
- {
- glBegin(GL_TRIANGLES);
- float fanLoc = 0.05f + (0.05f * flConvertedAmp);
- fanLoc /= gridSize/10;
- float offsetX = sin((-1.0f + flConvertedAmp * 10.0f) + ((float)glfwGetTime()*0.1f)) * 0.5f * scale;
- float offsetY = cos((-1.0f + flConvertedAmp * 10.0f) + ((float)glfwGetTime()*0.1f)) * 0.5f * scale;
- int iOffsetX = (int)(offsetX * gridSize);
- int iOffsetY = (int)(offsetY * gridSize);
- offsetX = (float)iOffsetX / (float)gridSize;
- offsetY = (float)iOffsetY / (float)gridSize;
- glColor3f(color[0],color[1],color[2]);
- glVertex3f(offsetX+0.0f,offsetY+0.0f,0.0f);
- glVertex3f(offsetX+fanLoc,offsetY+0.0f,0.0f);
- glVertex3f(offsetX+fanLoc,offsetY+fanLoc,0.0f);
- glColor3f(color[0],color[1],color[2]);
- glVertex3f(offsetX+0.0f,offsetY+0.0f,0.0f);
- glVertex3f(offsetX+0.0f,offsetY+fanLoc,0.0f);
- glVertex3f(offsetX+fanLoc,offsetY+fanLoc,0.0f);
- glEnd( );
- }
- void drawSquareSimpleDisplay(float flConvertedAmp, float offsetX)
- {
- glBegin(GL_TRIANGLES);
- float fanLoc = flConvertedAmp*2.5f;
- float offsetY = -1.0f;
- float barWidth = 0.003f;
- glColor3f(1.0,1.0,1.0);
- glVertex3f(offsetX+0.0f,offsetY+0.0f,0.0f);
- glVertex3f(offsetX+barWidth,offsetY+0.0f,0.0f);
- glVertex3f(offsetX+barWidth,offsetY+fanLoc,0.0f);
- glColor3f(1.0,1.0,1.0);
- glVertex3f(offsetX+0.0f,offsetY+0.0f,0.0f);
- glVertex3f(offsetX+0.0f,offsetY+fanLoc,0.0f);
- glVertex3f(offsetX+barWidth,offsetY+fanLoc,0.0f);
- glEnd( );
- }
- int maxdraw = 100;
- float direction = 1.0f;
- float timeDir = 0.0f;
- float lastForceSwitch = 0.0f;
- float lastBeatHit = 0.0f;
- double lastFrameTime = 0.0f;
- //FFT vars
- float totalcalc = 0.0f;
- float calc;
- float FFTDrawData[100]; //maxdraw
- void calculateFFTVars()
- {
- totalcalc = 0.0f;
- calc = 0.0f;
- float newcalc = 0.0f;
- for(int i=0;i<maxdraw;i++)
- {
- int idx = (i*(512/maxdraw));
- calc = FFTAverage(fft,idx,10)*fftMult;
- newcalc = (float)((oldFFT[idx]*0.93)+(calc*0.07));
- FFTDrawData[i] = newcalc;
- //float newcalc2 = ((oldFFT[idx]*0.9)+(calc*0.1));
- totalcalc+=newcalc;
- timecap+=(0.05f*newcalc);
- if(!bPunchEnableDotLoop || iPunchSwitchBoard==1)
- {
- if(!bPunchOnlySimpleFFTDisplay)
- oldFFT[(i*(512/maxdraw))] = newcalc;
- }
- }
- }
- GLenum drawMode = GL_POINTS;
- void drawGL()
- {
- CheckGLErrors("unknown location - error");
- int viewWidth = fboresx;
- int viewHeight = fboresy;
- float scaleFactorX = (float)(FULL_WIDTH/fboresx);
- float scaleFactorY = (float)(FULL_HEIGHT/fboresy);
- if(windowMode==GLFW_FULLSCREEN)
- {
- viewWidth *= (int)scaleFactorX;
- viewHeight *= (int)scaleFactorY;
- glViewport(0, 0, viewWidth, viewHeight);
- }
- glViewport(0, 0, viewWidth, viewHeight);
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); // Bind our frame buffer for rendering
- CheckGLErrors("While binding framebuffer");
- glPushAttrib(GL_VIEWPORT_BIT | GL_ENABLE_BIT); // Push our glEnable and glViewport states
- //glClearColor(0.0f, 0.0f, 0.0f, 0.4f);
- //glClear(GL_COLOR_BUFFER_BIT);
- CheckGLErrors("smthing");
- if(bPunchOnlySimpleFFTDisplay)
- {
- bPunchEnableDotLoop=false;
- iPunchSwitchBoard = -1;
- }
- for(int i=0;i<maxdraw;i++)
- {
- int idx = (i*(512/maxdraw));
- float newcalc = FFTDrawData[i];
- float dark = 1.0f-(i*(1.0f/maxdraw));
- if(!bPunchEnableDotLoop || iPunchSwitchBoard==1)
- {
- if(!bPunchOnlySimpleFFTDisplay)
- oldFFT[(i*(512/maxdraw))] = newcalc;
- continue;
- }
- glPointSize(5.0f*(FULL_WIDTH/WIDTH)+newcalc*50.0f);
- glBegin(drawMode);
- if(drawMode!=GL_LINES)
- {
- glColor3f(1.0f-(i/maxdraw),newcalc,0.0f);
- }
- else
- {
- glColor3f(0.0f,0.0f,0.0f);
- }
- glVertex3f(-0.5f,0.0f,0.0f);
- if(drawMode==GL_LINES)
- {
- glVertex3f(0.0f,0.0f,0.0f);
- }
- glEnd( );
- glPointSize(5.0f*(FULL_WIDTH/WIDTH)+newcalc*50.0f);
- glBegin(drawMode);
- if(drawMode!=GL_LINES)
- {
- glColor3f(0,newcalc,1.0f-(i/maxdraw));
- }
- else
- {
- glColor3f(0.0f,0.0f,0.0f);
- }
- glVertex3f(0.0f,0.0f,0.0f);
- if(drawMode==GL_LINES)
- {
- glVertex3f(0.5f,0.0f,0.0f);
- }
- glEnd( );
- glPointSize(5.0f*(FULL_WIDTH/WIDTH)+newcalc*50.0f);
- glBegin(drawMode);
- if(drawMode!=GL_LINES)
- {
- glColor3f(1.0f-(i/maxdraw),0.0f,newcalc);
- }
- else
- {
- glColor3f(0.0f,0.0f,0.0f);
- }
- glVertex3f(0.5f,0.0f,0.0f);
- if(drawMode==GL_LINES)
- {
- glVertex3f(0.0f+(i*0.5*direction*cos(glfwGetTime()*0.05)*sinVex(newcalc)*(2.0f/maxdraw))*1.5, 0.1f+cosVex(newcalc), 0.0f);
- }
- glEnd( );
- glPointSize(5.0f*(FULL_WIDTH/WIDTH)+newcalc);
- glBegin(drawMode);
- if(drawMode!=GL_LINES)
- {
- glColor3f(1.0f*dark,0.2f*dark,0.8f*dark);
- }
- else
- {
- glColor3f(0.0f,0.0f,0.0f);
- }
- glVertex3f(0.0f+(i*0.5*direction*cos(glfwGetTime()*0.05)*sinVex(newcalc)*(2.0f/maxdraw))*1.5, 0.1f+cosVex(newcalc), 0.0f);
- if(drawMode==GL_LINES)
- {
- glVertex3f(0.0f-(i*0.5*direction*sin(glfwGetTime()*0.05)*cosVex(newcalc)*(2.0f/maxdraw))*1.5, -0.1f+sinVex(newcalc), 0.0f);
- }
- glEnd( );
- glBegin(drawMode);
- if(drawMode!=GL_LINES)
- {
- glColor3f(0.4f*dark,0.8f*dark,0.0f*dark);
- }
- else
- {
- glColor3f(0.0f,0.0f,0.0f);
- }
- glVertex3f(0.0f-(i*0.5*direction*sin(glfwGetTime()*0.05)*cosVex(newcalc)*(2.0f/maxdraw))*1.5, -0.1f+sinVex(newcalc), 0.0f);
- glEnd( );
- glBegin(drawMode);
- glColor3f(1.0f,0.8f,0.0f);
- glVertex3f(-1.0f+(i*(2.0f/maxdraw)), sinVex(newcalc)*0.2, 0.0f);
- if(drawMode==GL_LINES)
- {
- glVertex3f(1.0f-(i*(2.0f/maxdraw)), cosVex(newcalc), 0.0f);
- }
- glEnd( );
- glBegin(drawMode);
- glColor3f(0.0f,0.4f,0.5f);
- glVertex3f(1.0f-(i*(2.0f/maxdraw)), cosVex(newcalc), 0.0f);
- if(drawMode==GL_LINES)
- {
- glVertex3f(-1.0f+(i*(2.0f/maxdraw)), 0.1f+cosVex(newcalc), 0.0f);
- }
- glEnd( );
- glBegin(drawMode);
- glColor3f(0.0f,0.8f,1.0f);
- glVertex3f(-1.0f+(i*(2.0f/maxdraw)), 0.1f+cosVex(newcalc), 0.0f);
- if(drawMode==GL_LINES)
- {
- glVertex3f(1.0f-(i*(2.0f/maxdraw)), -0.3f+sinVex(newcalc)*0.2, 0.0f);
- }
- glEnd( );
- glBegin(drawMode);
- glColor3f(1.0f,0.1f,0.0f);
- glVertex3f(1.0f-(i*(2.0f/maxdraw)), -0.3f+sinVex(newcalc)*0.2, 0.0f);
- if(drawMode==GL_LINES)
- {
- glVertex3f(-1.0f+(i*(2.0f/maxdraw)), -0.8f+calc, 0.0f);
- }
- glEnd( );
- glPointSize(5.0f*(FULL_WIDTH/WIDTH)+calc*20.0f);
- glBegin(drawMode);
- glColor3f(1.0f-(i/maxdraw),0.2f+calc,0.2f+calc);
- glVertex3f(-1.0f+(i*(2.0f/maxdraw)), -0.8f+calc, 0.0f);
- if(drawMode==GL_LINES)
- {
- glVertex3f(-1.0f+(i*(2.0f/maxdraw)), -0.8f+newcalc, 0.0f);
- }
- glEnd( );
- glPointSize(5.0f*(FULL_WIDTH/WIDTH));
- glBegin(drawMode);
- glColor3f(0.2f+calc,0.0f,1.0f-(i*(i/maxdraw)));
- glVertex3f(-1.0f+(i*(2.0f/maxdraw)), -0.8f+newcalc, 0.0f);
- glEnd( );
- oldFFT[(i*(512/maxdraw))] = newcalc;
- }
- if(bPunchOnlySimpleFFTDisplay || bPunchEnableSimpleFFTDisplay)
- {
- static float fftSimpleAmp[512];
- for(int i=0;i<512;i++)
- {
- float equalAmp = (float)i / 51.2;
- if(!bPunchOnlySimpleFFTDisplay)
- equalAmp *= 0.6f;
- float fftAmp = FFTAverage(fft,i,2)*fftMult*equalAmp;
- fftAmp = (fftAmp*0.1) + (fftSimpleAmp[i]*0.9);
- fftSimpleAmp[i] = fftAmp;
- float offset = -1.0f;
- offset += ((float)i/256) * 2.0f;
- drawSquareSimpleDisplay(fftSimpleAmp[i], offset);
- }
- }
- if(iPunchSwitchBoard==1)
- {
- float color1[3] = {1.0f,0.2f,0.1f};
- float color2[3] = {0.2f,1.0f,0.1f};
- float color3[3] = {0.1f,0.2f,1.0f};
- float fftSquareAmp1 = FFTAverage(fft,32,10)*fftMult;
- float fftSquareAmp2 = FFTAverage(fft,64,10)*fftMult;
- float fftSquareAmp3 = FFTAverage(fft,128,10)*fftMult;
- int squareManyMaker = fftSquareAmp1 * 50;
- for(int i=0;i<squareManyMaker;i++)
- {
- float loopScale = 1.0f * i/5;
- //Smaller squares
- drawSquare(fftSquareAmp2, 0.95f + loopScale, color3, 20);
- drawSquare(fftSquareAmp2, 0.75f + loopScale, color2, 20);
- drawSquare(fftSquareAmp2, 0.55f + loopScale, color1, 20);
- drawSquare(fftSquareAmp3, 1.05f + loopScale, color3, 20);
- drawSquare(fftSquareAmp3, 0.85f + loopScale, color2, 20);
- drawSquare(fftSquareAmp3, 0.65f + loopScale, color1, 20);
- //Bigger squares
- drawSquare(fftSquareAmp1, 1.0f + loopScale, color1, 10);
- drawSquare(fftSquareAmp2, 0.8f + loopScale, color2, 10);
- drawSquare(fftSquareAmp3, 0.6f + loopScale, color3, 10);
- }
- }
- glPointSize(20.0f*(FULL_WIDTH/WIDTH)*totalcalc);
- glBegin(GL_POINTS);
- glColor3f(1.0f-(totalcalc*multGain*4.5),1.0f-(totalcalc*multGain*4.5),1.0f-(totalcalc*multGain*4.5));
- glVertex3f(sinVex((totalcalc+glfwGetTime())*0.01)*0.5,cosVex((totalcalc+glfwGetTime())*0.01)*0.5,0.0f);
- glEnd( );
- glBegin(GL_POINTS);
- glColor3f(1.0f-(totalcalc*multGain*4.5),1.0f-(totalcalc*multGain*4.5),1.0f-(totalcalc*multGain*4.5));
- glVertex3f(cosVex((totalcalc+glfwGetTime()*0.08)*0.005)*1.2,sinVex((totalcalc+glfwGetTime()*0.008)*0.005)*1.2,0.0f);
- glEnd( );
- glBegin(GL_POINTS);
- glColor3f(1.0f-(totalcalc*multGain*4.5),1.0f-(totalcalc*multGain*4.5),1.0f-(totalcalc*multGain*4.5));
- glVertex3f(cosVex((totalcalc+glfwGetTime()*0.001)*0.001)*1.8,sinVex((totalcalc+glfwGetTime()*0.001)*0.001)*1.8,0.0f);
- glEnd( );
- if(drawMode==GL_LINES)
- {
- drawMode=GL_POINTS;
- }
- if(totalcalc>2.0 && (glfwGetTime()-timeDir)>0.5f)
- {
- direction*=-1;
- timeDir=glfwGetTime();
- }
- float switchFloat = FFTAverage(fft,32,4)*fftMult;
- //punchUpdateSwitchboard();
- if(totalcalc > 2.0f && totalcalc < 2.5f && (glfwGetTime()-lastForceSwitch)>0.3f)
- {
- //punchForceSwitchboard();
- lastForceSwitch = glfwGetTime();
- }
- static float peakFloat = 0.1f;
- float beatFloat = FFTAverage(fft,40,20)*fftMult;
- peakFloat = (peakFloat*0.99) + (beatFloat*0.01);
- if(beatFloat > peakFloat && (glfwGetTime()-lastBeatHit)>(80/60) && (glfwGetTime()-lastBeatHit)<(200/60))
- {
- BeatRegisterHit();
- lastBeatHit = (float)glfwGetTime();
- }
- if(totalcalc>5.5 && totalcalc<5.8)
- {
- if(drawMode==GL_POINTS)
- {
- drawMode=GL_LINES;
- }
- else
- {
- drawMode=GL_POINTS;
- }
- }
- autoGain = clamp(2.5f-(totalcalc/1.72),0.0f,2.0f);
- float norGain = multGain*autoGain;
- CheckGLErrors("smthing1");
- glPopAttrib(); // Restore our glEnable and glViewport states
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
- if(windowMode==GLFW_WINDOW)
- {
- glViewport(0, 0, WIDTH, HEIGHT);
- }
- else
- {
- glViewport(0, 0, FULL_WIDTH, FULL_HEIGHT);
- }
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT);
- CheckGLErrors("fb bind & clear");
- glBindTexture(GL_TEXTURE_2D, fbo_texture);
- glUseProgram(program);
- glActiveTexture(GL_TEXTURE0);
- int texture_location = glGetUniformLocation(program, "RTScene");
- glUniform1i(texture_location, 0);
- CheckGLErrors("uniform texture_location ");
- float glslDeltaTime = (float)(glfwGetTime() - lastFrameTime);
- int uniform_time_delta = glGetUniformLocation(program, "deltaTime");
- glUniform1f(uniform_time_delta, glslDeltaTime);
- CheckGLErrors("uniform uniform_time_delta ");
- int ampdata = glGetUniformLocation(program, "inputGain");
- if(bPunchOnlySimpleFFTDisplay)
- {
- glUniform1f(ampdata, 0.5f);
- CheckGLErrors("uniform ampdata 0.5f ");
- }
- else
- {
- glUniform1f(ampdata, totalcalc);
- CheckGLErrors("uniform ampdata totalcalc ");
- }
- CheckGLErrors("uniform location3 unknown aftershit ");
- //glMatrixMode(GL_PROJECTION); // Switch to the projection matrix so that we can manipulate how our scene is viewed
- //glLoadIdentity();
- glBegin(GL_QUADS);
- glTexCoord2f(0.0f, 0.0f);
- glVertex3f(-1.0f, -1.0f, 0.0f); // The bottom left corner
- glTexCoord2f(0.0f, 1.0f);
- glVertex3f(-1.0f, 1.0, 0.0f); // The top left corner
- glTexCoord2f(1.0f, 1.0f);
- glVertex3f(1.0f, 1.0f, 0.0f); // The top right corner
- glTexCoord2f(1.0f, 0.0f);
- glVertex3f(1.0f, -1.0f, 0.0f); // The bottom right corner
- glEnd();
- glBindTexture(GL_TEXTURE_2D, 0);
- glUseProgram(0);
- glViewport(0, 0, fboresx, fboresy);
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); // Bind our frame buffer for rendering
- CheckGLErrors("While binding framebuffer");
- glPushAttrib(GL_VIEWPORT_BIT | GL_ENABLE_BIT); // Push our glEnable and glViewport states
- glBindTexture(GL_TEXTURE_2D, fbo_texture);
- if(bPunchOnlySimpleFFTDisplay)
- {
- totalcalc = 0.5f;
- }
- glUseProgram(program2);
- glActiveTexture(GL_TEXTURE0);
- texture_location = glGetUniformLocation(program2, "RTScene");
- glUniform1i(texture_location, 0);
- CheckGLErrors("uniform program2 texture_location ");
- uniform_time_delta = glGetUniformLocation(program2, "deltaTime");
- glUniform1f(uniform_time_delta, glslDeltaTime);
- CheckGLErrors("uniform program2 uniform_time_delta ");
- ampdata = glGetUniformLocation(program2, "inputGain");
- glUniform1f(ampdata, totalcalc);
- CheckGLErrors("uniform program2 ampdata");
- int multdata = glGetUniformLocation(program2, "multGain");
- glUniform1f(multdata, norGain);
- CheckGLErrors("uniform program2 multdata");
- glBegin(GL_QUADS);
- glTexCoord2f(0.0f, 0.0f);
- glVertex3f(-1.0f, -1.0f, 0.0f); // The bottom left corner
- glTexCoord2f(0.0f, 1.0f);
- glVertex3f(-1.0f, 1.0, 0.0f); // The top left corner
- glTexCoord2f(1.0f, 1.0f);
- glVertex3f(1.0f, 1.0f, 0.0f); // The top right corner
- glTexCoord2f(1.0f, 0.0f);
- glVertex3f(1.0f, -1.0f, 0.0f); // The bottom right corner
- glEnd();
- glBindTexture(GL_TEXTURE_2D, 0);
- glUseProgram(0);
- glPopAttrib(); // Restore our glEnable and glViewport states
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
- // Swap front and back rendering buffers
- glfwSwapBuffers();
- lastFrameTime = glfwGetTime();
- }
- void printError()
- {
- int err = BASS_ErrorGetCode();
- if(err==BASS_OK)
- {
- printf("BASS is ok\n");
- }
- else
- {
- printf("BASS troubles %i\n",err);
- }
- }
- std::string openfilename(char *filter = "All Files (*.*)\0*.*\0", HWND owner = NULL) {
- OPENFILENAME ofn;
- char fileName[MAX_PATH] = "";
- ZeroMemory(&ofn, sizeof(ofn));
- ofn.lStructSize = sizeof(OPENFILENAME);
- ofn.hwndOwner = owner;
- ofn.lpstrFilter = filter;
- ofn.lpstrFile = fileName;
- ofn.nMaxFile = MAX_PATH;
- ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
- ofn.lpstrDefExt = "";
- std::string fileNameStr;
- if ( GetOpenFileName(&ofn) )
- fileNameStr = fileName;
- return fileNameStr;
- }
- void GLFWCALL punchResize(int width, int height )
- {
- WIDTH = width;
- HEIGHT = height;
- }
- void IncrementStartId(int inputId)
- {
- startId = inputId + 1;
- nextId = startId;
- }
- int running = GL_TRUE;
- double lastDeltaTime = 0.0;
- int main(int argc, char *argv[])
- {
- printf("Run Punch with /help for startup command list.\n");
- printf("Playlist and args:\n");
- for(int i=0;i<argc;i++)
- {
- printf("%s\n",argv[i]);
- }
- //Run commands if any
- for(int i=0;i<argc;i++)
- {
- std::string argString = argv[i];
- if(argString.compare("/help") == 0)
- {
- IncrementStartId(i);
- printf("Commands:\n");
- printf("/help : this\n");
- printf("-debug_simple : simple debug visualisation, just a simple spectrum\n");
- }
- if(argString.compare("-debug_simple") == 0)
- {
- IncrementStartId(i);
- bPunchOnlySimpleFFTDisplay = true;
- }
- }
- g_argc = argc;
- g_argv = argv;
- if(g_argv[startId]==0)
- {
- MessageBox(NULL,"Please drag and drop one or multiple audio files onto the Punch.exe file.\n(*.mp3, *ogg, *wav and others)\nYou can also drag a .txt file containing a Shoutcast URL (not playlist) onto the .exe.","No files to Punch.",0);
- return 0;
- }
- printf("initializing bass.\n");
- bass_init = BASS_Init(-1, 44100, 0, 0, NULL);
- printError();
- printf("Loading bass_fx.\n");
- if (HIWORD(BASS_FX_GetVersion())!=BASSVERSION) {
- // incorrect version loaded!
- printf("could not load bass_fx\n");
- }
- printError();
- printf("Creating stream.\n");
- glfwOpenWindowHint( GLFW_OPENGL_VERSION_MAJOR, 3 );
- glfwOpenWindowHint( GLFW_OPENGL_VERSION_MINOR, 2 );
- glfwOpenWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
- //glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
- glfwOpenWindowHint( GLFW_WINDOW_NO_RESIZE, GL_TRUE );
- //glfwEnable( GLFW_STICKY_KEYS );
- //glfwEnable( GLFW_KEY_REPEAT );
- double lastPress = 0.0;
- bool windowReset = false;
- bool globalRunning = true;
- if(glfwInit())
- {
- GLFWvidmode dMode;
- glfwGetDesktopMode(&dMode);
- FULL_WIDTH = dMode.Width;
- FULL_HEIGHT = dMode.Height;
- while(globalRunning)
- {
- if(running==false && windowReset==false)
- {
- globalRunning=false;
- continue;
- }
- if(windowReset)
- {
- running=true;
- windowReset=false;
- }
- int glfwWidth = WIDTH;
- int glfwHeight = HEIGHT;
- if(windowMode==GLFW_FULLSCREEN)
- {
- glfwWidth = FULL_WIDTH;
- glfwHeight = FULL_HEIGHT;
- }
- if(glfwOpenWindow(glfwWidth,glfwHeight,0,0,0,0,0,0,windowMode))
- {
- GLenum err = glewInit();
- if (GLEW_OK != err)
- {
- // GLEW failed!
- printf("GLEW could not be initialized.\n");
- fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
- system("pause");
- exit( EXIT_FAILURE );
- }
- printf("GLEW has been initialized.\n");
- fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
- fboresx = FULL_WIDTH;
- fboresy = FULL_HEIGHT;
- glSettings();
- setShaders();
- setShaders2();
- glfwSetWindowSizeCallback( punchResize );
- glfwSetWindowTitle("Punch - Audio Visualisation created by Christiaan Bakker www.anicator.com");
- glfwSwapInterval( 0 );
- // Main loop
- while( running )
- {
- // OpenGL rendering goes here...
- if(glfwGetKey( GLFW_KEY_SPACE ) &&
- glfwGetWindowParam( GLFW_OPENED )){
- setShaders();
- setShaders2();
- }
- if(glfwGetKey( GLFW_KEY_RIGHT ) == GLFW_PRESS &&
- glfwGetWindowParam( GLFW_OPENED ) && (glfwGetTime()-lastPress)>0.3f){
- BASS_ChannelStop(inputStream);
- lastPress=glfwGetTime();
- }
- if(glfwGetKey( GLFW_KEY_LEFT ) == GLFW_PRESS &&
- glfwGetWindowParam( GLFW_OPENED ) && (glfwGetTime()-lastPress)>0.3f){
- nextId-=2;
- if(nextId<1) nextId=0;
- BASS_ChannelStop(inputStream);
- lastPress=glfwGetTime();
- }
- if(glfwGetKey( GLFW_KEY_KP_SUBTRACT ) == GLFW_PRESS &&
- glfwGetWindowParam( GLFW_OPENED ) && (glfwGetTime()-lastPress)>0.3f){
- float vol;
- BASS_ChannelGetAttribute(inputStream,BASS_ATTRIB_VOL,&vol);
- if(vol>0.0)
- {
- vol-=0.1;
- printf("Lowering volume. ( %f )\n",vol);
- }
- BASS_ChannelSetAttribute(inputStream,BASS_ATTRIB_VOL,vol);
- lastPress=glfwGetTime();
- }
- if(glfwGetKey( GLFW_KEY_KP_ADD ) == GLFW_PRESS &&
- glfwGetWindowParam( GLFW_OPENED ) && (glfwGetTime()-lastPress)>0.3f){
- float vol;
- BASS_ChannelGetAttribute(inputStream,BASS_ATTRIB_VOL,&vol);
- if(vol<1.0)
- {
- vol+=0.1;
- printf("Increasing volume. ( %f )\n",vol);
- }
- BASS_ChannelSetAttribute(inputStream,BASS_ATTRIB_VOL,vol);
- lastPress=glfwGetTime();
- }
- if(glfwGetKey( '-' ) &&
- glfwGetWindowParam( GLFW_OPENED )){
- if(multGain>0)
- multGain-=0.01f;
- }
- if(glfwGetKey( '=' ) &&
- glfwGetWindowParam( GLFW_OPENED )){
- if(multGain<1)
- multGain+=0.01f;
- }
- if(glfwGetWindowParam( GLFW_OPENED )){
- if((glfwGetTime() - lastDeltaTime) > 0.01)
- {
- bassFunc();
- calculateFFTVars();
- lastDeltaTime = glfwGetTime();
- }
- drawGL();
- }
- // Check if ESC key was pressed or window was closed
- running = !glfwGetKey( GLFW_KEY_ESC ) &&
- glfwGetWindowParam( GLFW_OPENED );
- if(glfwGetKey( 'F' ) && running &&
- glfwGetWindowParam( GLFW_OPENED )){
- printf("F tap\n");
- if(windowMode==GLFW_WINDOW)
- {
- windowMode=GLFW_FULLSCREEN;
- running=false;
- windowReset=true;
- }
- else
- {
- windowMode=GLFW_WINDOW;
- running=false;
- windowReset=true;
- }
- }
- }
- }
- // Close window
- glfwCloseWindow();
- }
- //Terminate GLFW
- glfwTerminate();
- }
- BASS_Free();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement