Skip to content

Commit

Permalink
Killed off the basic visualizer and added projectM and it's MilkDrop …
Browse files Browse the repository at this point in the history
…Stuff!

Old fft code still there but i will probably kill it
  • Loading branch information
las3rlars committed Sep 14, 2015
1 parent 8ea1779 commit 75aa979
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 65 deletions.
38 changes: 7 additions & 31 deletions wxSpot/AudioBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <wx/log.h>
#include <algorithm> // std::min
#include <cmath>
#include "fix_fft.h"
#include "Visualizer.h"

Expand All @@ -12,7 +13,6 @@

AudioBuffer::AudioBuffer()
{
m_visualizer = nullptr;
currentBuffer = getBufferFromPool();
queuedBuffers.push_back(currentBuffer);
reset();
Expand All @@ -23,10 +23,6 @@ AudioBuffer::~AudioBuffer()
{
}

void AudioBuffer::setVisualizer(Visualizer *visualizer)
{
m_visualizer = visualizer;
}
std::shared_ptr<int16_t> AudioBuffer::getBufferFromPool()
{
if (bufferPool.empty()) {
Expand Down Expand Up @@ -100,7 +96,6 @@ int AudioBuffer::readData(int16_t *dest, const unsigned int samples)
}
readOffset += samples;
currentFrame += samples;
updateFFT(dest, samples);
return samples;
}

Expand Down Expand Up @@ -134,6 +129,12 @@ void AudioBuffer::setPlayTime(unsigned int time)
writeOffset = 0;
}

while (!queuedBuffers.empty() && queuedBuffers.front() != currentBuffer) {
bufferPool.push_front(queuedBuffers.front());
queuedBuffers.pop_front();
}


currentFrame = newPlayedFrame;
}

Expand All @@ -157,28 +158,3 @@ void AudioBuffer::reset()
queuedBuffers.push_back(currentBuffer);

}

void AudioBuffer::updateFFT(int16_t *buffer, unsigned int size)
{
//memcpy(fftBuffer, buffer, size);
short avg[512];
int half = size / 2;

// make mono
for (int i = 0, j = 0; i < half; i++, j += 2) {
avg[i] = (buffer[j] + buffer[j + 1]) / 2;
}

for (int i = 0; i < half; i++) {
if (i & 0x01)
fftBuffer[(half + i) >> 1] = avg[i];
else
fftBuffer[i >> 1] = avg[i];
}

// log2(512) = 9
int scale = fix_fftr(fftBuffer, 9, 1);

m_visualizer->update(fftBuffer, half, scale);
//wxLogDebug("%+.3f %+.3f %+.3f %+.3f", fftBuffer[255] / 32767.0, fftBuffer[511] / 32767.0, fftBuffer[767] / 32767.0, fftBuffer[1023] / 32767.0);
}
7 changes: 0 additions & 7 deletions wxSpot/AudioBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class AudioBuffer
AudioBuffer();
~AudioBuffer();

void setVisualizer(Visualizer *visualizer);
void addData(const int16_t *data, const unsigned int samples);
int readData(int16_t *dest, const unsigned int samples);
int getSampleDiff();
Expand All @@ -22,8 +21,6 @@ class AudioBuffer
void setPlayTime(unsigned int time);
void reset();

void updateFFT(int16_t *buffer, unsigned int size);

private:
std::shared_ptr<int16_t> getBufferFromPool();
unsigned short int channels;
Expand All @@ -35,10 +32,6 @@ class AudioBuffer
unsigned int readBufferOffset;
unsigned int writeBufferOffset;

int16_t fftBuffer[512];

Visualizer *m_visualizer;

std::shared_ptr<int16_t> currentBuffer;
std::forward_list<std::shared_ptr<int16_t>> bufferPool;
std::list<std::shared_ptr<int16_t>> queuedBuffers;
Expand Down
45 changes: 38 additions & 7 deletions wxSpot/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "SpotifyManager.h"
#include "LoginDialog.h"
#include "SettingsDialog.h"
#include "MilkDropVisualizer.h"

#include "glyphicons-174-play.h"
#include "glyphicons-175-pause.h"
Expand All @@ -44,6 +45,7 @@ EVT_COMMAND(wxID_ANY, SPOTIFY_LOGGED_IN_EVENT, MainFrame::OnSpotifyLoggedInEvent

EVT_MENU(wxID_EXIT, MainFrame::OnExit)
EVT_MENU(ID_Settings, MainFrame::OnSettings)
EVT_MENU(ID_MilkDrop, MainFrame::OnMilkDrop)
EVT_MENU(wxID_ABOUT, MainFrame::OnAbout)
EVT_TIMER(wxID_ANY, MainFrame::OnTimerEvent)

Expand All @@ -56,13 +58,17 @@ MainFrame::MainFrame(const wxString &title, const wxPoint &pos, const wxSize &si
: wxFrame((wxFrame *) nullptr, -1, title, pos, size),
timerStatusUpdate(this, wxID_ANY),
activeSongIndex(0),
activePlaylist(nullptr)
activePlaylist(nullptr),
milkDropFrame(nullptr)

{

wxMenu *menuFile = new wxMenu();
menuFile->Append(wxID_EXIT);

wxMenu *visualizationsFile = new wxMenu();
visualizationsFile->Append(ID_MilkDrop, "&MilkDrop");

wxMenu *optionsFile = new wxMenu();
optionsFile->Append(ID_Settings, "&Settings...\tCtrl-S");

Expand All @@ -71,16 +77,17 @@ MainFrame::MainFrame(const wxString &title, const wxPoint &pos, const wxSize &si

wxMenuBar *menuBar = new wxMenuBar();
menuBar->Append(menuFile, "&File");
menuBar->Append(visualizationsFile, "&Visualizations");
menuBar->Append(optionsFile, "&Settings");
menuBar->Append(menuHelp, "&Help");

SetMenuBar(menuBar);

panel = new wxPanel(this, wxID_ANY);

horzBox = new wxBoxSizer(wxHORIZONTAL);
vertBox = new wxBoxSizer(wxVERTICAL);
bottomHorzBox = new wxBoxSizer(wxHORIZONTAL);
auto horzBox = new wxBoxSizer(wxHORIZONTAL);
auto vertBox = new wxBoxSizer(wxVERTICAL);
auto bottomHorzBox = new wxBoxSizer(wxHORIZONTAL);

spotifyManager = new SpotifyManager(this);

Expand Down Expand Up @@ -193,7 +200,7 @@ MainFrame::MainFrame(const wxString &title, const wxPoint &pos, const wxSize &si
spotifyManager->search(searchTextCtrl->GetValue());
});

searchPlaylistBox = new wxBoxSizer(wxVERTICAL);
auto searchPlaylistBox = new wxBoxSizer(wxVERTICAL);

searchPlaylistBox->Add(searchTextCtrl, wxSizerFlags(0).Expand().Border(wxALL, 2));
searchPlaylistBox->Add(playlistTree, 1, wxGROW);
Expand Down Expand Up @@ -297,7 +304,6 @@ MainFrame::MainFrame(const wxString &title, const wxPoint &pos, const wxSize &si

}

audioBuffer.setVisualizer(visualizer);
showLoginDialog();
}

Expand Down Expand Up @@ -327,9 +333,34 @@ void MainFrame::OnSettings(wxCommandEvent &event)
}
}

void MainFrame::OnMilkDrop(wxCommandEvent &event)
{
if (milkDropFrame != nullptr) return;

milkDropFrame = new wxFrame(this, wxID_ANY, wxString("wxSpot - MilkDrop"), wxDefaultPosition, wxSize(512, 512));

int args[] = { WX_GL_RGBA, WX_GL_DOUBLEBUFFER, WX_GL_DEPTH_SIZE, 24, 0 };

milkDropFrame->Bind(wxEVT_CLOSE_WINDOW, [=](wxCloseEvent &event) {
soundManager->setMilkDropVisualizer(nullptr);
event.Skip();
milkDropFrame = nullptr;
});

auto milkDropVisualizer = new MilkDropVisualizer(milkDropFrame, args);

milkDropVisualizer->Bind(wxEVT_KEY_DOWN, [=](wxKeyEvent &event) {
if (event.GetModifiers() == wxMOD_ALT && event.GetKeyCode() == WXK_RETURN) {
milkDropFrame->ShowFullScreen(!milkDropFrame->IsFullScreen());
}
});
soundManager->setMilkDropVisualizer(milkDropVisualizer);
milkDropFrame->Show();
}

void MainFrame::OnAbout(wxCommandEvent &event)
{
wxMessageBox("wxSpot 0.8 by Viktor Müntzing", "About", wxOK | wxICON_INFORMATION);
wxMessageBox("wxSpot 0.9 by Viktor Müntzing\nVisualizations from projectM", "About", wxOK | wxICON_INFORMATION);
}

void MainFrame::OnSpotifyWakeUpEvent(wxCommandEvent &event)
Expand Down
9 changes: 3 additions & 6 deletions wxSpot/Main.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class MainFrame : public wxFrame

enum {
ID_Settings = 1,
ID_MilkDrop,
ID_Menu_Add_To_Playlist,
ID_Menu_Delete_Track,
ID_Menu_Copy_TrackName,
Expand All @@ -47,6 +48,7 @@ class MainFrame : public wxFrame

void OnExit(wxCommandEvent &event);
void OnSettings(wxCommandEvent &event);
void OnMilkDrop(wxCommandEvent &event);
void OnAbout(wxCommandEvent &event);
void OnSpotifyWakeUpEvent(wxCommandEvent &event);
void OnSpotifyStartedPlayingEvent(wxCommandEvent &event);
Expand All @@ -72,16 +74,10 @@ class MainFrame : public wxFrame

wxPanel *panel;

wxBoxSizer *horzBox;
wxBoxSizer *vertBox;
wxBoxSizer *bottomHorzBox;

SpotifyManager *spotifyManager;
SoundManager *soundManager;
AudioBuffer audioBuffer;

wxBoxSizer *searchPlaylistBox;

wxTextCtrl *searchTextCtrl;
wxTreeCtrl *playlistTree;
SongListCtrl *songList;
Expand All @@ -97,6 +93,7 @@ class MainFrame : public wxFrame
wxCheckBox *checkBoxShuffle;

Visualizer *visualizer;
wxFrame *milkDropFrame;

wxTimer timerStatusUpdate;

Expand Down
54 changes: 54 additions & 0 deletions wxSpot/MilkDropVisualizer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "MilkDropVisualizer.h"

#include <projectM.hpp>


BEGIN_EVENT_TABLE(MilkDropVisualizer, wxGLCanvas)
EVT_PAINT(MilkDropVisualizer::OnPaint)
EVT_SIZE(MilkDropVisualizer::OnSize)
END_EVENT_TABLE()

MilkDropVisualizer::MilkDropVisualizer(wxFrame *parent, int *args) : wxGLCanvas(parent, wxID_ANY, args, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
{
m_context = new wxGLContext(this);
SetMinSize(wxSize(64, 64));
SetBackgroundStyle(wxBG_STYLE_CUSTOM);
SetCurrent(*m_context);

//m_projectM = new projectM("projectM/config.inp");
m_projectM = std::make_unique<projectM>("projectM/config.inp");

}


MilkDropVisualizer::~MilkDropVisualizer()
{
//delete m_projectM;
}

void MilkDropVisualizer::OnPaint(wxPaintEvent &event)
{
SetCurrent(*m_context);
wxPaintDC(this);

if (!IsShownOnScreen()) return;

//glClearColor(1.0f, 0.0f, 1.0f, 0.0f);
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

m_projectM->renderFrame();

SwapBuffers();
}

void MilkDropVisualizer::OnSize(wxSizeEvent &event)
{
Refresh(false);
m_projectM->projectM_resetGL(event.GetSize().x, event.GetSize().y);
}

void MilkDropVisualizer::updatePCM(short *data)
{
m_projectM->pcm()->addPCM16Data(data, 512);
Refresh(false);
}
31 changes: 31 additions & 0 deletions wxSpot/MilkDropVisualizer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <wx/wxprec.h>
#ifndef WX_PRECOMP
# include <wx/wx.h>
#endif

#include <wx/glcanvas.h>
#include <memory>

class projectM;

class MilkDropVisualizer : public wxGLCanvas
{

public:
MilkDropVisualizer(wxFrame *parent, int *args);
~MilkDropVisualizer();
void updatePCM(short *data);

protected:
void OnPaint(wxPaintEvent &event);
void OnSize(wxSizeEvent &event);

private:
wxGLContext *m_context;
std::unique_ptr<projectM> m_projectM;
//projectM *m_projectM;
DECLARE_EVENT_TABLE()
};

17 changes: 15 additions & 2 deletions wxSpot/SoundManager.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "SoundManager.h"

#include "AudioBuffer.h"
#include "MilkDropVisualizer.h"

#define SAMPLE_RATE (44100)

Expand All @@ -25,7 +26,7 @@ static int paCallback(const void *input, void *output, unsigned long frameCount,

}

SoundManager::SoundManager(MainFrame *mainFrame)
SoundManager::SoundManager(MainFrame *mainFrame) : m_milkDropVisualizer(nullptr)
{
m_pMainFrame = mainFrame;
PaError error = Pa_Initialize();
Expand Down Expand Up @@ -105,7 +106,14 @@ unsigned int SoundManager::getSoundData(void *frames, int num_frames)
{
AudioBuffer *buffer = m_pMainFrame->getAudioBuffer();

return buffer->readData((int16_t *)frames, num_frames);
int len = buffer->readData((int16_t *)frames, num_frames);


//return buffer->readData((int16_t *)frames, num_frames);
if (m_milkDropVisualizer != nullptr)
m_milkDropVisualizer->updatePCM((int16_t *)frames);

return len;

}

Expand All @@ -114,6 +122,11 @@ void SoundManager::bufferDone()
m_pMainFrame->bufferDone();
}

void SoundManager::setMilkDropVisualizer(MilkDropVisualizer *visualizer)
{
m_milkDropVisualizer = visualizer;
}

std::vector<std::shared_ptr<Device>> *SoundManager::getDevices()
{
return &devices;
Expand Down
Loading

0 comments on commit 75aa979

Please sign in to comment.