diff --git a/Audio.hpp b/Audio.hpp index e2e3710..30b248d 100644 --- a/Audio.hpp +++ b/Audio.hpp @@ -13,6 +13,8 @@ class Audio { const sf::Int16* samples; sf::Uint64 sampleSize; + + sf::Time duration; // load raw audio data into buffer void loadBuffer( std::string audioFilePath) { @@ -27,7 +29,8 @@ class Audio { sampleRate = buffer.getSampleRate(); sampleCount = buffer.getSampleCount(); - + duration = buffer.getDuration(); + } }; \ No newline at end of file diff --git a/Audio/eden-xo.wav b/Audio/eden-xo.wav new file mode 100644 index 0000000..dd74979 Binary files /dev/null and b/Audio/eden-xo.wav differ diff --git a/Graphic/Bar.hpp b/Graphic/Bar.hpp index 583ce60..f9deb2e 100644 --- a/Graphic/Bar.hpp +++ b/Graphic/Bar.hpp @@ -1,14 +1,16 @@ +#pragma once + #include class Bar { public: - const int numBar = 20; // number of bars + const int count = 30; // number of bars float scaleBar; // value to scale the bars // float barScale[]; - GLfloat barVertices[8] = { + GLfloat vertices[8] = { -0.90, 0.05, -0.95, 0.05, -0.95, -0.05, @@ -16,19 +18,23 @@ class Bar { }; // color format: CMY - GLfloat barColors[12] = { + GLfloat colors[12] = { // 0.6275, 0.7059, 0.2863, // purple // 0.6275, 0.7059, 0.2863, // 0.1451, 0.0000, 0.5098, // green // 0.1451, 0.0000, 0.5098, 0.2314, 0.2824, 0.2039, // light purple 0.2314, 0.2824, 0.2039, - 0.4078, 0.0824, 0.1765, // mint bllue - 0.4078, 0.0824, 0.1765, + 0.2314, 0.2824, 0.2039, + 0.2314, 0.2824, 0.2039, + // 1.0, 1.0, 1.0, //white + // 1.0, 1.0, 1.0, + // 0.4078, 0.0824, 0.1765, // mint bllue + // 0.4078, 0.0824, 0.1765, }; // bar indices - GLubyte barIndices[4] = { + GLubyte indices[4] = { 0, 1, 2, 3, }; diff --git a/Graphic/Stars.hpp b/Graphic/Stars.hpp new file mode 100644 index 0000000..580b1c1 --- /dev/null +++ b/Graphic/Stars.hpp @@ -0,0 +1,40 @@ +#pragma once + +#include + + +class Star { + + public: + + float size = 3; + float displace = 0; + // float displaceY = 0; + + GLfloat vertices[2] = { + 0.0, 0.0, + }; + + // color format: CMY + GLfloat colorsOff[4] = { + 0.0, 0.0, 0.0, 1.0 + }; + + GLfloat colorsOn[4] = { + 1.0, 0.0, 0.0, 1.0 + }; + + // bar indices + GLubyte indices[2] = { + 0, 1, + }; + + // initialize barScale + // void initBarScale() { + // for ( int i = 0; i < numBar; i++) { + // barScale[i] = 0; + // } + // } + + +}; diff --git a/Graphic/sky.hpp b/Graphic/sky.hpp new file mode 100644 index 0000000..e42ec9b --- /dev/null +++ b/Graphic/sky.hpp @@ -0,0 +1,51 @@ +#pragma once + +#include + + +class Sky { + + public: + // const int count = 30; // number of bars + // float scaleBar; // value to scale the bars + // float barScale[]; + + GLfloat vertices[8] = { + -1, 1, + -1, 1, + -1, -1, + -1, -1, + }; + + // color format: CMY + GLfloat colors[12] = { + // 0.6275, 0.7059, 0.2863, // purple + // 0.6275, 0.7059, 0.2863, + // 0.1451, 0.0000, 0.5098, // green + // 0.1451, 0.0000, 0.5098, + // 0.2314, 0.2824, 0.2039, // light purple + // 0.2314, 0.2824, 0.2039, + 1.0, 0.0, 0.0, //white + 1.0, 0.0, 0.0, //white + 1.0, 0.0, 0.0, //white + 1.0, 0.0, 0.0, //white + // 1.0, 1.0, 1.0, + // 0.4078, 0.0824, 0.1765, // mint bllue + // 0.4078, 0.0824, 0.1765, + }; + + // bar indices + GLubyte indices[4] = { + 0, 1, + 2, 3, + }; + + // initialize barScale + // void initBarScale() { + // for ( int i = 0; i < numBar; i++) { + // barScale[i] = 0; + // } + // } + + +}; diff --git a/Textures/back.png b/Textures/back.png new file mode 100644 index 0000000..3c6b4f0 Binary files /dev/null and b/Textures/back.png differ diff --git a/Textures/bottom.png b/Textures/bottom.png new file mode 100644 index 0000000..e708ce0 Binary files /dev/null and b/Textures/bottom.png differ diff --git a/Textures/front.png b/Textures/front.png new file mode 100644 index 0000000..d295fa9 Binary files /dev/null and b/Textures/front.png differ diff --git a/Textures/left.png b/Textures/left.png new file mode 100644 index 0000000..dbd014d Binary files /dev/null and b/Textures/left.png differ diff --git a/Textures/right.png b/Textures/right.png new file mode 100644 index 0000000..d091891 Binary files /dev/null and b/Textures/right.png differ diff --git a/Textures/skybox.png b/Textures/skybox.png new file mode 100644 index 0000000..a083bed Binary files /dev/null and b/Textures/skybox.png differ diff --git a/Textures/top.png b/Textures/top.png new file mode 100644 index 0000000..5be35d2 Binary files /dev/null and b/Textures/top.png differ diff --git a/a.out b/a.out index a12bc50..425bb92 100644 Binary files a/a.out and b/a.out differ diff --git a/main.cpp b/main.cpp index d0dc16e..23404b2 100644 --- a/main.cpp +++ b/main.cpp @@ -8,6 +8,8 @@ #include "Audio.hpp" #include "Graphic/Bar.hpp" +#include "Graphic/Stars.hpp" +#include "Graphic/Sky.hpp" // const char *audioFilePath = "Audio/freq-test.wav"; @@ -16,35 +18,45 @@ // clock sf::Clock clock_; sf::Time timeElapsed = sf::milliseconds(0); -sf::Time fps_delay = sf::milliseconds(33); -sf::Time song_delay = sf::milliseconds(500); +sf::Time fps_delay = sf::milliseconds(33.33); +sf::Time song_delay = sf::milliseconds(950); // sf::Time delay; // initializing custom Audio class and loading song into buffer -std::string audioFilePath = "Audio/aero.wav"; +// std::string audioFilePath = "Audio/aero-trim.wav"; +// std::string audioFilePath = "Audio/aero.wav"; +std::string audioFilePath = "Audio/eden-xo.wav"; // std::string audioFilePath = "Audio/freq-test.wav"; Audio audio; // inintializing some OpenGL primitives Bar bar; +const int barCount = 30; +Star star; + +Sky sky; // initializing some fft variables int real = 0; int imag = 1; -const int freqBand = 30; -const int chunkSize = 2947; + +// const int chunkSize = 2947; // aero +const int chunkSize = 3200; // eden-xo int chunksCovered = 0; + int tempAmpFreq = 0; int powerSpectrum[chunkSize]; -int powerSpectrumSmoothed[freqBand]; +// int powerSpectrumSmoothed = new int[count]; +int powerSpectrumSmoothed[barCount]; -int maxFreq = 0; -int overallMaxFreq = 0; -int minFreq = 0; +float maxChunkFreq = 0; +float minChunkFreq = 0; +float maxFreq = 0; +float minFreq = 0; int avgFreq = 0; @@ -59,11 +71,10 @@ float normalize(float num, float min, float max, float maximus){ - // gets new fft values void updateFFTInput() { - maxFreq = 0; + maxChunkFreq = 0; for (int i = 0; i < chunkSize && i+chunksCovered < audio.sampleCount; i++) { input[i][real] = audio.samples[chunksCovered+i]; @@ -81,15 +92,15 @@ void updateFFTInput() { powerSpectrum[i] = sqrt(pow(output[i+1][real], 2.0) + pow(output[i+1][imag], 2.0)); - for (int i = 0; i maxFreq){ - maxFreq = powerSpectrumSmoothed[i]; + if (powerSpectrumSmoothed[i] > maxChunkFreq){ + maxChunkFreq = powerSpectrumSmoothed[i]; } } @@ -104,8 +115,8 @@ void findMaxFreq() { for (int c = 0; c < (audio.sampleCount/chunkSize); c++) { updateFFTInput(); - if ( overallMaxFreq < maxFreq) { - overallMaxFreq = maxFreq; + if ( maxFreq < maxChunkFreq) { + maxFreq = maxChunkFreq; } } chunksCovered = 0; @@ -114,26 +125,71 @@ void findMaxFreq() { void displayBars() { - for (int i = 1; i 0.35*maxFreq ) { + glColorPointer(4, GL_FLOAT, 0, star.colorsOn); + // std::cout << maxChunkFreq << " " << 0.35*maxFreq << std::endl; + } + else + glColorPointer(4, GL_FLOAT, 0, star.colorsOff); + + glPointSize(star.size); + + // glTranslatef( star.displaceX, star.displaceY, 0.0 ); + glPushMatrix(); + for ( float y = -0.9; y <= 1; y += 0.1) { + for ( float x = -0.9; x <= 1; x += 0.1) { + + glRotatef( 1000, 0.0, 0.0, 1.0); + glRotatef( star.displace, 0.0, 0.0, 1.0); + glPushMatrix(); + glTranslatef( x, y, 0.0 ); + glDrawElements(GL_POINTS, 1, GL_UNSIGNED_BYTE, star.indices); + glPopMatrix(); + + } + } + glPopMatrix(); + +} + +void rotateStars() { + star.displace += normalize( maxChunkFreq, 0, maxFreq, 0.1); + // star.displace += 0.01; + // star.displaceY += 0.01; +} + + +void drawSky() { + glVertexPointer(2, GL_FLOAT, 0, sky.vertices); + glColorPointer(3, GL_FLOAT, 0, sky.colors); + glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, sky.indices); +} + -// } void display() { glClearColor( 1.0, 1.0, 1.0, 1.0 ); // Background Color @@ -145,6 +201,12 @@ void display() { // glScalef( normFloat, normFloat, normFloat ); // glScalef( j, j, j ); + + + // if ( maxChunkFreq > (9/10)*maxFreq ) + drawSky(); + displayStars(); + displayBars(); // std::cout << "HELLO" << std::endl; @@ -158,6 +220,7 @@ void display() { void idlefunc() { updateFFTInput(); // gets new fft values + rotateStars(); // fps_delay.asMilliseconds() - clock_.getElapsedTime().asMilliseconds(); sf::sleep(fps_delay - clock_.getElapsedTime()); clock_.restart(); @@ -177,9 +240,10 @@ int main(int argc, char **argv) { std::cout << "Sample Rate: " << audio.sampleRate << std::endl; std::cout << "Sample Count: " << audio.sampleCount << std::endl; + std::cout << "Audio Duration: " << audio.duration.asSeconds() << std::endl; findMaxFreq(); - std::cout << "Max Freq: " << overallMaxFreq << std::endl; + std::cout << "Max Freq: " << maxFreq << std::endl; // return 0; @@ -191,7 +255,7 @@ int main(int argc, char **argv) { // initializing OpenGL glutInit(&argc, argv); // glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB ); - // glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); + glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB ); glutInitWindowSize(500, 500); glutCreateWindow("MUSIC VISUALIZER"); // glutReshapeFunc(myReshape); @@ -201,16 +265,17 @@ int main(int argc, char **argv) { // glEnable(GL_DEPTH_TEST); glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_VERTEX_ARRAY); - glVertexPointer(2, GL_FLOAT, 0, bar.barVertices); + // glColorPointer(4, GL_FLOAT, 0, colors); - glColorPointer(3, GL_FLOAT, 0, bar.barColors); + // glColor3f(1.0, 1.0, 1.0); audio.song.setVolume(30); audio.song.play(); sf::sleep(song_delay); + glutMainLoop(); // sf::SoundStream::stop() return 0; -} +} \ No newline at end of file