Cleanup Code
- FIXED: Same Naming camelCase - FIXED: member variables marked with m_
This commit is contained in:
parent
1a17f1db63
commit
9b791f0765
21 changed files with 201 additions and 196 deletions
|
@ -10,9 +10,9 @@ public:
|
|||
HIDHelper();
|
||||
~HIDHelper();
|
||||
int openDevices();
|
||||
int get_feature_report(unsigned char *buf, int size) const;
|
||||
int send_feature_report(unsigned char *buf, int size) const;
|
||||
void close_ctrl_device();
|
||||
int getFeatureReport(unsigned char *buf, int size) const;
|
||||
int sendFeatureReport(unsigned char *buf, int size) const;
|
||||
void closeCtrlDevice();
|
||||
|
||||
hid_device *m_ledDevice{};
|
||||
int ctrl_device{};
|
||||
|
|
|
@ -12,13 +12,13 @@
|
|||
#include <VulcanoLE/Audio/FFT.h>
|
||||
#include <VulcanoLE/Audio/Types.h>
|
||||
|
||||
static const char k_record_stream_name[] = "vulcanoLE";
|
||||
static const char k_record_stream_description[] = "Keyboard Input Stream";
|
||||
static const char recStreamName[] = "vulcanoLE";
|
||||
static const char recStreamDescription[] = "Keyboard Input Stream";
|
||||
|
||||
static const int32_t k_sample_rate = 44100;
|
||||
static const int32_t k_channels = 2;
|
||||
static const int32_t sampleRate = 44100;
|
||||
static const int32_t channels = 2;
|
||||
|
||||
static const std::string k_default_monitor_postfix = ".monitor";
|
||||
static const std::string defaultMonitorPostfix = ".monitor";
|
||||
|
||||
class AudioGrabber {
|
||||
public:
|
||||
|
@ -30,29 +30,29 @@ public:
|
|||
};
|
||||
AudioGrabber();
|
||||
~AudioGrabber();
|
||||
bool read(pcm_stereo_sample *buffer, uint32_t buffer_size);
|
||||
bool read(stereoSample *buffer, uint32_t bufferSize);
|
||||
static AudioGrabber* createAudioGrabber();
|
||||
void init();
|
||||
FFT fft;
|
||||
ReqMode requestMode = ReqMode::FFT;
|
||||
double loudness = 0.0;
|
||||
float getLoudness();
|
||||
bool doWork();
|
||||
bool work();
|
||||
VUtils::Environment *env = nullptr;
|
||||
private:
|
||||
std::mutex m_mtx;
|
||||
pcm_stereo_sample *m_pcm_buffer{};
|
||||
pa_simple *m_pulseaudio_simple{};
|
||||
pa_mainloop *m_pulseaudio_mainloop{};
|
||||
std::string m_pulseaudio_default_source_name;
|
||||
void populate_default_source_name();
|
||||
bool open_pulseaudio_source(uint32_t max_buffer_size);
|
||||
static void pulseaudio_context_state_callback(pa_context *c,
|
||||
void *userdata);
|
||||
static void pulseaudio_server_info_callback(pa_context *context,
|
||||
const pa_server_info *i,
|
||||
void *userdata);
|
||||
void calculateRMS(pcm_stereo_sample *pFrame);
|
||||
void calculatePEAK(pcm_stereo_sample *pFrame);
|
||||
stereoSample *m_buffer{};
|
||||
pa_simple *m_pulseaudioSimple{};
|
||||
pa_mainloop *m_pulseaudioMainloop{};
|
||||
std::string m_PulseaudioDefaultSourceName;
|
||||
void populateDefaultSourceName();
|
||||
bool openPulseaudioSource(uint32_t maxBufferSize);
|
||||
static void pulseaudioContextStateCallback(pa_context *c,
|
||||
void *userdata);
|
||||
static void pulseaudioServerInfoCallback(pa_context *context,
|
||||
const pa_server_info *i,
|
||||
void *userdata);
|
||||
void calculateRMS(stereoSample *pFrame);
|
||||
void calculatePEAK(stereoSample *pFrame);
|
||||
double m_scale = 1.0;
|
||||
};
|
||||
|
|
|
@ -2,23 +2,23 @@
|
|||
|
||||
#include <mutex>
|
||||
#include <fftw3.h>
|
||||
#include "Types.h"
|
||||
#include <VulcanoLE/Audio/Types.h>
|
||||
|
||||
class FFT {
|
||||
public:
|
||||
FFT();
|
||||
~FFT();
|
||||
void process(pcm_stereo_sample *pFrame);
|
||||
void process(stereoSample *frame);
|
||||
outputSample *getData();
|
||||
bool prepareInput(pcm_stereo_sample *buffer, uint32_t sample_size);
|
||||
bool prepareInput(stereoSample *buffer, uint32_t sampleSize);
|
||||
protected:
|
||||
double *m_fftw_input_left{};
|
||||
double *m_fftw_input_right{};
|
||||
fftw_complex *m_fftw_output_left{};
|
||||
fftw_complex *m_fftw_output_right{};
|
||||
double *m_fftwInputLeft{};
|
||||
double *m_fftwInputRight{};
|
||||
fftw_complex *m_fftwOutputLeft{};
|
||||
fftw_complex *m_fftwOutputRight{};
|
||||
outputSample *m_sample = new outputSample(FFT_SIZE);
|
||||
fftw_plan m_fftw_plan_left{};
|
||||
fftw_plan m_fftw_plan_right{};
|
||||
fftw_plan m_fftwPlanLeft{};
|
||||
fftw_plan m_fftwPlanRight{};
|
||||
std::mutex m_mtx;
|
||||
size_t m_fftw_results;
|
||||
size_t m_fftwResults;
|
||||
};
|
||||
|
|
|
@ -2,12 +2,12 @@
|
|||
#define FFT_SIZE 1024
|
||||
#define BUFFER_SIZE 2048
|
||||
|
||||
struct stereo_sample_frame {
|
||||
struct stereoSampleFrame {
|
||||
float l;
|
||||
float r;
|
||||
};
|
||||
|
||||
using pcm_stereo_sample = struct stereo_sample_frame;
|
||||
using stereoSample = struct stereoSampleFrame;
|
||||
|
||||
struct outputSample {
|
||||
explicit outputSample(int fftSize) {
|
||||
|
|
|
@ -20,12 +20,12 @@ class Vulcan121 {
|
|||
public:
|
||||
explicit Vulcan121(HIDHelper *helper);
|
||||
~Vulcan121();
|
||||
int send_led_map(led_map *src, bool deleteMap = false);
|
||||
int send_led_to(rgba rgb);
|
||||
bool send_init_sequence();
|
||||
bool query_ctrl_report(unsigned char);
|
||||
bool send_ctrl_report(unsigned char id);
|
||||
bool wait_for_ctrl_dev();
|
||||
int sendLedMap(led_map *src, bool deleteMap = false);
|
||||
int sendToLEDs(rgba rgb);
|
||||
bool sendInitSequence();
|
||||
bool queryCtrlReport(unsigned char id);
|
||||
bool sendCtrlReport(unsigned char id);
|
||||
bool waitForCtrlDev();
|
||||
static led_map *createEmptyLEDMap();
|
||||
struct DATA {
|
||||
int num_rows = NUM_ROWS;
|
||||
|
@ -43,8 +43,8 @@ protected:
|
|||
void setupMap();
|
||||
// we need some mapping feature! rows and cols dont have the same amount of keys. so the struct needs
|
||||
HIDHelper *m_helper;
|
||||
rgba *rv_fixed[NUM_KEYS]{};
|
||||
rgba rv_color_off = { 0x0000, 0x0000, 0x0000 };
|
||||
rgba *m_fixed[NUM_KEYS]{};
|
||||
rgba m_colorOff = { 0x0000, 0x0000, 0x0000 };
|
||||
keys *keyMapRow[NUM_ROWS];
|
||||
keys *keyMapCol[NUM_COLS]; // need to find out the count!
|
||||
};
|
||||
|
|
|
@ -8,8 +8,8 @@ namespace VIZ {
|
|||
public:
|
||||
Loudness(AudioGrabber *pGrabber, Vulcan121 *pVulcan121);
|
||||
~Loudness() override = default;
|
||||
void on_setup() override;
|
||||
void on_tick(float delta) override;
|
||||
void onSetup() override;
|
||||
void onTick(float delta) override;
|
||||
float lastVal = 0;
|
||||
const char *name() override;
|
||||
std::string m_name = "Loudness Meter";
|
||||
|
|
|
@ -11,8 +11,8 @@ namespace VIZ {
|
|||
public:
|
||||
PoliceLike(AudioGrabber *pGrabber, Vulcan121 *pVulcan121);
|
||||
~PoliceLike() override = default;
|
||||
void on_setup() override;
|
||||
void on_tick(float delta) override;
|
||||
void onSetup() override;
|
||||
void onTick(float delta) override;
|
||||
void switchOnPeak(double);
|
||||
int tick = 0;
|
||||
bool left = true;
|
||||
|
|
|
@ -15,13 +15,14 @@ namespace VIZ {
|
|||
public:
|
||||
RainbowLine(AudioGrabber *pGrabber, Vulcan121 *vulcan);
|
||||
~RainbowLine() override;
|
||||
void on_setup() override;
|
||||
void on_tick(float delta) override;
|
||||
void onSetup() override;
|
||||
void onTick(float delta) override;
|
||||
void calcNextDelta(double ratio);
|
||||
void updateMap(double factor);
|
||||
const char *name() override;
|
||||
std::string m_name = "Rainbow Line";
|
||||
led_map *data = Vulcan121::createEmptyLEDMap();
|
||||
bool firstUnder = true;
|
||||
double deltaMove(double val);
|
||||
};
|
||||
}
|
|
@ -7,8 +7,8 @@ namespace VIZ {
|
|||
struct Spectrum : public VIZ {
|
||||
Spectrum(AudioGrabber *pGrabber, Vulcan121 *pVulcan121);
|
||||
~Spectrum() override = default;
|
||||
void on_setup() override;
|
||||
void on_tick(float delta) override;
|
||||
void onSetup() override;
|
||||
void onTick(float delta) override;
|
||||
double lastVal = 0;
|
||||
const char *name() override;
|
||||
std::string m_name = "Spectrum One";
|
||||
|
|
|
@ -6,8 +6,8 @@ namespace VIZ {
|
|||
struct VIZ {
|
||||
VIZ(AudioGrabber *pGrabber, Vulcan121 *pVulcan121) : grabber(pGrabber), keyboard(pVulcan121) {}
|
||||
virtual ~VIZ() = default;
|
||||
virtual void on_setup() = 0;
|
||||
virtual void on_tick(float delta) = 0;
|
||||
virtual void onSetup() = 0;
|
||||
virtual void onTick(float delta) = 0;
|
||||
Vulcan121 *keyboard{};
|
||||
Vulcan121::DATA keyboardData = Vulcan121::DATA{};
|
||||
AudioGrabber *grabber{};
|
||||
|
|
|
@ -11,9 +11,9 @@ namespace VIZ {
|
|||
struct VisPlugins {
|
||||
int mode = 0;
|
||||
void init(HIDHelper *, AudioGrabber *);
|
||||
void on_startup();
|
||||
void on_tick();
|
||||
void on_shutdown();
|
||||
void onStartup();
|
||||
void onTick();
|
||||
void onShutdown();
|
||||
void setCurrentMode(int);
|
||||
~VisPlugins();
|
||||
VUtils::Environment *env{};
|
||||
|
|
|
@ -142,7 +142,7 @@ bool HIDHelper::findLED(hid_device_info *devs, unsigned int product_id) {
|
|||
return true;
|
||||
}
|
||||
|
||||
int HIDHelper::get_feature_report(unsigned char *buf, int size) const {
|
||||
int HIDHelper::getFeatureReport(unsigned char *buf, int size) const {
|
||||
int res = ioctl(ctrl_device, HIDIOCGFEATURE(size), buf);
|
||||
if (res < 0) {
|
||||
return 0;
|
||||
|
@ -150,7 +150,7 @@ int HIDHelper::get_feature_report(unsigned char *buf, int size) const {
|
|||
return size;
|
||||
}
|
||||
|
||||
int HIDHelper::send_feature_report(unsigned char *buf, int size) const {
|
||||
int HIDHelper::sendFeatureReport(unsigned char *buf, int size) const {
|
||||
int res = ioctl(ctrl_device, HIDIOCSFEATURE(size), buf);
|
||||
if (res < 0) {
|
||||
return 0;
|
||||
|
@ -158,7 +158,7 @@ int HIDHelper::send_feature_report(unsigned char *buf, int size) const {
|
|||
return size;
|
||||
}
|
||||
|
||||
void HIDHelper::close_ctrl_device() {
|
||||
void HIDHelper::closeCtrlDevice() {
|
||||
if (ctrl_device) close(ctrl_device);
|
||||
ctrl_device = 0;
|
||||
}
|
||||
|
|
|
@ -7,24 +7,24 @@
|
|||
AudioGrabber::AudioGrabber() = default;
|
||||
AudioGrabber::~AudioGrabber() = default;
|
||||
|
||||
bool AudioGrabber::read(pcm_stereo_sample *buffer, uint32_t buffer_size) {
|
||||
auto buffer_size_bytes = static_cast<size_t>(sizeof(pcm_stereo_sample) * buffer_size);
|
||||
if (m_pulseaudio_simple == nullptr) {
|
||||
open_pulseaudio_source(static_cast<uint32_t>(buffer_size_bytes));
|
||||
bool AudioGrabber::read(stereoSample *buffer, uint32_t bufferSize) {
|
||||
auto buffer_size_bytes = static_cast<size_t>(sizeof(stereoSample) * bufferSize);
|
||||
if (m_pulseaudioSimple == nullptr) {
|
||||
openPulseaudioSource(static_cast<uint32_t>(buffer_size_bytes));
|
||||
}
|
||||
|
||||
if (m_pulseaudio_simple != nullptr) {
|
||||
if (m_pulseaudioSimple != nullptr) {
|
||||
memset(buffer, 0, buffer_size_bytes);
|
||||
int32_t error_code;
|
||||
auto return_code = pa_simple_read(m_pulseaudio_simple, buffer,
|
||||
auto return_code = pa_simple_read(m_pulseaudioSimple, buffer,
|
||||
buffer_size_bytes, &error_code);
|
||||
if (return_code < 0) {
|
||||
WARN("Could not finish reading pulse Audio stream buffer\n bytes read: %d buffer\n size: %d", return_code,
|
||||
buffer_size_bytes)
|
||||
// zero out buffer
|
||||
memset(buffer, 0, buffer_size_bytes);
|
||||
pa_simple_free(m_pulseaudio_simple);
|
||||
m_pulseaudio_simple = nullptr;
|
||||
pa_simple_free(m_pulseaudioSimple);
|
||||
m_pulseaudioSimple = nullptr;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
@ -36,51 +36,51 @@ bool AudioGrabber::read(pcm_stereo_sample *buffer, uint32_t buffer_size) {
|
|||
}
|
||||
|
||||
|
||||
void AudioGrabber::populate_default_source_name() {
|
||||
void AudioGrabber::populateDefaultSourceName() {
|
||||
pa_mainloop_api *mainloop_api;
|
||||
pa_context *pulseaudio_context;
|
||||
m_pulseaudio_mainloop = pa_mainloop_new();
|
||||
mainloop_api = pa_mainloop_get_api(m_pulseaudio_mainloop);
|
||||
m_pulseaudioMainloop = pa_mainloop_new();
|
||||
mainloop_api = pa_mainloop_get_api(m_pulseaudioMainloop);
|
||||
pulseaudio_context = pa_context_new(mainloop_api, "VulcanoLE device list");
|
||||
pa_context_connect(pulseaudio_context, nullptr, PA_CONTEXT_NOFLAGS,
|
||||
nullptr);
|
||||
pa_context_set_state_callback(pulseaudio_context,
|
||||
pulseaudio_context_state_callback,
|
||||
pulseaudioContextStateCallback,
|
||||
reinterpret_cast<void *>(this));
|
||||
|
||||
int ret;
|
||||
if (pa_mainloop_run(m_pulseaudio_mainloop, &ret) < 0) {
|
||||
if (pa_mainloop_run(m_pulseaudioMainloop, &ret) < 0) {
|
||||
ERR("Could not open pulseaudio mainloop to find default device name: %d", ret)
|
||||
}
|
||||
}
|
||||
|
||||
bool AudioGrabber::open_pulseaudio_source(uint32_t max_buffer_size) {
|
||||
bool AudioGrabber::openPulseaudioSource(uint32_t maxBufferSize) {
|
||||
int32_t error_code = 0;
|
||||
static const pa_sample_spec sample_spec = { PA_SAMPLE_FLOAT32NE, k_sample_rate,
|
||||
k_channels };
|
||||
static const pa_buffer_attr buffer_attr = { max_buffer_size, 0, 0, 0,
|
||||
(max_buffer_size / 2) };
|
||||
populate_default_source_name();
|
||||
if (!m_pulseaudio_default_source_name.empty()) {
|
||||
m_pulseaudio_simple =
|
||||
pa_simple_new(nullptr, k_record_stream_name, PA_STREAM_RECORD,
|
||||
m_pulseaudio_default_source_name.c_str(),
|
||||
k_record_stream_description, &sample_spec,
|
||||
static const pa_sample_spec sample_spec = { PA_SAMPLE_FLOAT32NE, sampleRate,
|
||||
channels };
|
||||
static const pa_buffer_attr buffer_attr = { maxBufferSize, 0, 0, 0,
|
||||
(maxBufferSize / 2) };
|
||||
populateDefaultSourceName();
|
||||
if (!m_PulseaudioDefaultSourceName.empty()) {
|
||||
m_pulseaudioSimple =
|
||||
pa_simple_new(nullptr, recStreamName, PA_STREAM_RECORD,
|
||||
m_PulseaudioDefaultSourceName.c_str(),
|
||||
recStreamDescription, &sample_spec,
|
||||
nullptr, &buffer_attr, &error_code);
|
||||
}
|
||||
if (m_pulseaudio_simple == nullptr) {
|
||||
m_pulseaudio_simple =
|
||||
pa_simple_new(nullptr, k_record_stream_name, PA_STREAM_RECORD,
|
||||
nullptr, k_record_stream_description,
|
||||
if (m_pulseaudioSimple == nullptr) {
|
||||
m_pulseaudioSimple =
|
||||
pa_simple_new(nullptr, recStreamName, PA_STREAM_RECORD,
|
||||
nullptr, recStreamDescription,
|
||||
&sample_spec, nullptr, &buffer_attr, &error_code);
|
||||
}
|
||||
if (m_pulseaudio_simple == nullptr) {
|
||||
m_pulseaudio_simple =
|
||||
pa_simple_new(nullptr, k_record_stream_name, PA_STREAM_RECORD,
|
||||
"0", k_record_stream_description, &sample_spec,
|
||||
if (m_pulseaudioSimple == nullptr) {
|
||||
m_pulseaudioSimple =
|
||||
pa_simple_new(nullptr, recStreamName, PA_STREAM_RECORD,
|
||||
"0", recStreamDescription, &sample_spec,
|
||||
nullptr, &buffer_attr, &error_code);
|
||||
}
|
||||
if (m_pulseaudio_simple != nullptr) {
|
||||
if (m_pulseaudioSimple != nullptr) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -88,7 +88,7 @@ bool AudioGrabber::open_pulseaudio_source(uint32_t max_buffer_size) {
|
|||
return false;
|
||||
}
|
||||
|
||||
void AudioGrabber::pulseaudio_context_state_callback(pa_context *c, void *userdata) {
|
||||
void AudioGrabber::pulseaudioContextStateCallback(pa_context *c, void *userdata) {
|
||||
switch (pa_context_get_state(c)) {
|
||||
case PA_CONTEXT_UNCONNECTED:
|
||||
case PA_CONTEXT_CONNECTING:
|
||||
|
@ -98,7 +98,7 @@ void AudioGrabber::pulseaudio_context_state_callback(pa_context *c, void *userda
|
|||
|
||||
case PA_CONTEXT_READY: {
|
||||
pa_operation_unref(pa_context_get_server_info(
|
||||
c, pulseaudio_server_info_callback, userdata));
|
||||
c, pulseaudioServerInfoCallback, userdata));
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -106,21 +106,21 @@ void AudioGrabber::pulseaudio_context_state_callback(pa_context *c, void *userda
|
|||
case PA_CONTEXT_TERMINATED:
|
||||
auto *src =
|
||||
reinterpret_cast<AudioGrabber *>(userdata);
|
||||
pa_mainloop_quit(src->m_pulseaudio_mainloop, 0);
|
||||
pa_mainloop_quit(src->m_pulseaudioMainloop, 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void AudioGrabber::pulseaudio_server_info_callback(pa_context *context, const pa_server_info *i, void *userdata) {
|
||||
void AudioGrabber::pulseaudioServerInfoCallback(pa_context *context, const pa_server_info *i, void *userdata) {
|
||||
if (i != nullptr) {
|
||||
auto *src = reinterpret_cast<AudioGrabber *>(userdata);
|
||||
std::string name = i->default_sink_name;
|
||||
name.append(k_default_monitor_postfix);
|
||||
name.append(defaultMonitorPostfix);
|
||||
|
||||
src->m_pulseaudio_default_source_name = name;
|
||||
src->m_PulseaudioDefaultSourceName = name;
|
||||
|
||||
// stop mainloop after finding default name
|
||||
pa_mainloop_quit(src->m_pulseaudio_mainloop, 0);
|
||||
pa_mainloop_quit(src->m_pulseaudioMainloop, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -130,14 +130,14 @@ AudioGrabber *AudioGrabber::createAudioGrabber() {
|
|||
}
|
||||
|
||||
void AudioGrabber::init() {
|
||||
m_pcm_buffer = static_cast<pcm_stereo_sample *>(calloc(BUFFER_SIZE, sizeof(pcm_stereo_sample)));
|
||||
m_buffer = static_cast<stereoSample *>(calloc(BUFFER_SIZE, sizeof(stereoSample)));
|
||||
if (env != nullptr)
|
||||
m_scale = env->getAsDouble("audio_scale", 1.0);
|
||||
DBG("SET Audio Scale: %.3f", m_scale)
|
||||
loudness = 0.0;
|
||||
}
|
||||
|
||||
void AudioGrabber::calculateRMS(pcm_stereo_sample *pFrame) {
|
||||
void AudioGrabber::calculateRMS(stereoSample *pFrame) {
|
||||
float square = 0, mean;
|
||||
for (int i = 0; i < BUFFER_SIZE; i++) {
|
||||
float left = pFrame[0].l;
|
||||
|
@ -147,7 +147,7 @@ void AudioGrabber::calculateRMS(pcm_stereo_sample *pFrame) {
|
|||
loudness = std::sqrt(mean);
|
||||
}
|
||||
|
||||
void AudioGrabber::calculatePEAK(pcm_stereo_sample *pFrame) {
|
||||
void AudioGrabber::calculatePEAK(stereoSample *pFrame) {
|
||||
float max = 0;
|
||||
for (int i = 0; i < BUFFER_SIZE; i++) {
|
||||
float left = std::abs(pFrame[0].l);
|
||||
|
@ -163,28 +163,28 @@ float AudioGrabber::getLoudness() {
|
|||
return (float) loudness;
|
||||
}
|
||||
|
||||
bool AudioGrabber::doWork() {
|
||||
bool AudioGrabber::work() {
|
||||
std::unique_lock<std::mutex> lck(m_mtx);
|
||||
if (this->read(m_pcm_buffer, BUFFER_SIZE)) {
|
||||
if (this->read(m_buffer, BUFFER_SIZE)) {
|
||||
for (int i = 0; i < BUFFER_SIZE; ++i) {
|
||||
// my system is fucking quite
|
||||
m_pcm_buffer[i].l *= m_scale;
|
||||
m_pcm_buffer[i].r *= m_scale;
|
||||
m_buffer[i].l *= m_scale;
|
||||
m_buffer[i].r *= m_scale;
|
||||
}
|
||||
switch (requestMode) {
|
||||
case ReqMode::FFT:
|
||||
fft.process(m_pcm_buffer);
|
||||
fft.process(m_buffer);
|
||||
break;
|
||||
case ReqMode::RMS:
|
||||
calculateRMS(m_pcm_buffer);
|
||||
calculateRMS(m_buffer);
|
||||
break;
|
||||
case ReqMode::PEAK:
|
||||
calculatePEAK(m_pcm_buffer);
|
||||
calculatePEAK(m_buffer);
|
||||
break;
|
||||
default:
|
||||
fft.process(m_pcm_buffer);
|
||||
calculateRMS(m_pcm_buffer);
|
||||
calculatePEAK(m_pcm_buffer);
|
||||
fft.process(m_buffer);
|
||||
calculateRMS(m_buffer);
|
||||
calculatePEAK(m_buffer);
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
#include <VulcanoLE/Audio/FFT.h>
|
||||
#include <VUtils/Logging.h>
|
||||
|
||||
void FFT::process(pcm_stereo_sample *pFrame) {
|
||||
void FFT::process(stereoSample *frame) {
|
||||
std::unique_lock<std::mutex> lck(m_mtx);
|
||||
prepareInput(pFrame, FFT_SIZE);
|
||||
m_fftw_plan_left = fftw_plan_dft_r2c_1d(static_cast<int>(BUFFER_SIZE), m_fftw_input_left,
|
||||
m_fftw_output_left, FFTW_ESTIMATE);
|
||||
m_fftw_plan_right = fftw_plan_dft_r2c_1d(static_cast<int>(BUFFER_SIZE), m_fftw_input_right,
|
||||
m_fftw_output_right, FFTW_ESTIMATE);
|
||||
fftw_execute(m_fftw_plan_left);
|
||||
fftw_execute(m_fftw_plan_right);
|
||||
fftw_destroy_plan(m_fftw_plan_left);
|
||||
fftw_destroy_plan(m_fftw_plan_right);
|
||||
prepareInput(frame, FFT_SIZE);
|
||||
m_fftwPlanLeft = fftw_plan_dft_r2c_1d(static_cast<int>(BUFFER_SIZE), m_fftwInputLeft,
|
||||
m_fftwOutputLeft, FFTW_ESTIMATE);
|
||||
m_fftwPlanRight = fftw_plan_dft_r2c_1d(static_cast<int>(BUFFER_SIZE), m_fftwInputRight,
|
||||
m_fftwOutputRight, FFTW_ESTIMATE);
|
||||
fftw_execute(m_fftwPlanLeft);
|
||||
fftw_execute(m_fftwPlanRight);
|
||||
fftw_destroy_plan(m_fftwPlanLeft);
|
||||
fftw_destroy_plan(m_fftwPlanRight);
|
||||
for (int i = 0; i < FFT_SIZE; ++i) {
|
||||
double left = (double(*m_fftw_output_left[i]));
|
||||
double right = (double(*m_fftw_output_right[i]));
|
||||
double left = (double(*m_fftwOutputLeft[i]));
|
||||
double right = (double(*m_fftwOutputRight[i]));
|
||||
m_sample->leftChannel[i] = left;
|
||||
m_sample->rightChannel[i] = right;
|
||||
}
|
||||
|
@ -26,28 +26,28 @@ outputSample *FFT::getData() {
|
|||
return m_sample;
|
||||
}
|
||||
|
||||
bool FFT::prepareInput(pcm_stereo_sample *buffer, uint32_t sample_size) {
|
||||
bool FFT::prepareInput(stereoSample *buffer, uint32_t sampleSize) {
|
||||
bool is_silent = true;
|
||||
for (auto i = 0u; i < sample_size; ++i) {
|
||||
m_fftw_input_left[i] = buffer[i].l;
|
||||
m_fftw_input_right[i] = buffer[i].r;
|
||||
if (is_silent && (m_fftw_input_left[i] > 0 || m_fftw_input_right[i] > 0)) is_silent = false;
|
||||
for (auto i = 0u; i < sampleSize; ++i) {
|
||||
m_fftwInputLeft[i] = buffer[i].l;
|
||||
m_fftwInputRight[i] = buffer[i].r;
|
||||
if (is_silent && (m_fftwInputLeft[i] > 0 || m_fftwInputRight[i] > 0)) is_silent = false;
|
||||
}
|
||||
return is_silent;
|
||||
}
|
||||
|
||||
FFT::FFT() {
|
||||
m_fftw_results = (static_cast<size_t>(BUFFER_SIZE) / 2) + 1;
|
||||
m_fftwResults = (static_cast<size_t>(BUFFER_SIZE) / 2) + 1;
|
||||
|
||||
m_fftw_input_left = static_cast<double *>(
|
||||
m_fftwInputLeft = static_cast<double *>(
|
||||
fftw_malloc(sizeof(double) * BUFFER_SIZE));
|
||||
m_fftw_input_right = static_cast<double *>(
|
||||
m_fftwInputRight = static_cast<double *>(
|
||||
fftw_malloc(sizeof(double) * BUFFER_SIZE));
|
||||
|
||||
m_fftw_output_left = static_cast<fftw_complex *>(
|
||||
fftw_malloc(sizeof(fftw_complex) * m_fftw_results));
|
||||
m_fftw_output_right = static_cast<fftw_complex *>(
|
||||
fftw_malloc(sizeof(fftw_complex) * m_fftw_results));
|
||||
m_fftwOutputLeft = static_cast<fftw_complex *>(
|
||||
fftw_malloc(sizeof(fftw_complex) * m_fftwResults));
|
||||
m_fftwOutputRight = static_cast<fftw_complex *>(
|
||||
fftw_malloc(sizeof(fftw_complex) * m_fftwResults));
|
||||
}
|
||||
FFT::~FFT() {
|
||||
delete m_sample;
|
||||
|
|
|
@ -17,7 +17,7 @@ void VisAudioRunner::init() {
|
|||
grabber->env = env;
|
||||
plugins->env = env;
|
||||
grabber->init();
|
||||
plugins->on_startup();
|
||||
plugins->onStartup();
|
||||
LOG("Create Visual Audio Runner Thread")
|
||||
thread = std::thread(&VisAudioRunner::run, this);
|
||||
}
|
||||
|
@ -25,13 +25,13 @@ void VisAudioRunner::init() {
|
|||
void VisAudioRunner::run() const {
|
||||
usleep(5000);
|
||||
while (isActive) {
|
||||
if (grabber->doWork()) {
|
||||
plugins->on_tick();
|
||||
if (grabber->work()) {
|
||||
plugins->onTick();
|
||||
} else {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100ul));
|
||||
}
|
||||
}
|
||||
usleep(50000);
|
||||
DBG("SHUTDOWN HOOk")
|
||||
plugins->on_shutdown();
|
||||
plugins->onShutdown();
|
||||
}
|
||||
|
|
|
@ -26,14 +26,14 @@ Vulcan121::~Vulcan121() {
|
|||
}
|
||||
}
|
||||
|
||||
int Vulcan121::send_led_map(led_map *src, bool deleteMap) {
|
||||
int Vulcan121::sendLedMap(led_map *src, bool deleteMap) {
|
||||
int i, k;
|
||||
rgba rgb;
|
||||
unsigned char hwmap[444]{};
|
||||
unsigned char workbuf[65];
|
||||
memset(hwmap, 0, sizeof(hwmap));
|
||||
for (k = 0; k < NUM_KEYS; k++) {
|
||||
rgb = rv_fixed[k] ? *(rv_fixed[k]) : (src ? src->key[k] : rv_color_off);
|
||||
rgb = m_fixed[k] ? *(m_fixed[k]) : (src ? src->key[k] : m_colorOff);
|
||||
|
||||
rgb.r = (rgb.r > 255) ? 255 : (rgb.r < 0) ? 0 : rgb.r;
|
||||
rgb.g = (rgb.g > 255) ? 255 : (rgb.g < 0) ? 0 : rgb.g;
|
||||
|
@ -80,12 +80,12 @@ int Vulcan121::send_led_map(led_map *src, bool deleteMap) {
|
|||
}
|
||||
return 1;
|
||||
}
|
||||
int Vulcan121::send_led_to(rgba rgb) {
|
||||
int Vulcan121::sendToLEDs(rgba rgb) {
|
||||
auto *map = new led_map();
|
||||
for (auto &i : map->key) {
|
||||
i = rgb;
|
||||
}
|
||||
int st = send_led_map(map, true);
|
||||
int st = sendLedMap(map, true);
|
||||
return st;
|
||||
}
|
||||
|
||||
|
@ -93,34 +93,34 @@ led_map *Vulcan121::createEmptyLEDMap() {
|
|||
return new led_map();
|
||||
}
|
||||
|
||||
bool Vulcan121::send_init_sequence() {
|
||||
bool Vulcan121::sendInitSequence() {
|
||||
LOG("Sending device init sequence...")
|
||||
unsigned char a[9] = { 0x15, 0x05, 0x07, 0x0a, 0x0b, 0x06, 0x09, 0x0d, 0x13 };
|
||||
if (!query_ctrl_report(0x0f))
|
||||
if (!queryCtrlReport(0x0f))
|
||||
return false;
|
||||
for (auto i : a) {
|
||||
if (!send_ctrl_report(i) || !wait_for_ctrl_dev()) {
|
||||
if (!sendCtrlReport(i) || !waitForCtrlDev()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
m_helper->close_ctrl_device();
|
||||
m_helper->closeCtrlDevice();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Vulcan121::query_ctrl_report(unsigned char id) {
|
||||
bool Vulcan121::queryCtrlReport(unsigned char id) {
|
||||
if (id != 0x0f) return false;
|
||||
unsigned char buffer[8] = {};
|
||||
int length = 8;
|
||||
buffer[0] = id;
|
||||
int res = m_helper->get_feature_report(buffer, length);
|
||||
int res = m_helper->getFeatureReport(buffer, length);
|
||||
if (res) {
|
||||
return true;
|
||||
}
|
||||
ERR("query_ctrl_report(%02hhx) failed", id);
|
||||
ERR("queryCtrlReport(%02hhx) failed", id);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Vulcan121::send_ctrl_report(unsigned char id) {
|
||||
bool Vulcan121::sendCtrlReport(unsigned char id) {
|
||||
unsigned char *buffer;
|
||||
int length;
|
||||
|
||||
|
@ -209,19 +209,19 @@ bool Vulcan121::send_ctrl_report(unsigned char id) {
|
|||
default: ERR("UNKNOWN BUFFER OPTION")
|
||||
return false;
|
||||
}
|
||||
int res = m_helper->send_feature_report(buffer, length);
|
||||
int res = m_helper->sendFeatureReport(buffer, length);
|
||||
if (res == length) return true;
|
||||
ERR("send_ctrl_report(%02hhx) failed", id)
|
||||
ERR("sendCtrlReport(%02hhx) failed", id)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Vulcan121::wait_for_ctrl_dev() {
|
||||
bool Vulcan121::waitForCtrlDev() {
|
||||
unsigned char buffer[] = { 0x04, 0x00, 0x00, 0x00 };
|
||||
int res;
|
||||
while (true) {
|
||||
// 150ms is the magic number here, should suffice on first try.
|
||||
usleep(10000);
|
||||
res = m_helper->get_feature_report(buffer, sizeof(buffer));
|
||||
res = m_helper->getFeatureReport(buffer, sizeof(buffer));
|
||||
if (res) {
|
||||
if (buffer[1] == 0x01) break;
|
||||
} else {
|
||||
|
|
|
@ -3,13 +3,13 @@
|
|||
namespace VIZ {
|
||||
Loudness::Loudness(AudioGrabber *pGrabber, Vulcan121 *pVulcan121) : VIZ(pGrabber, pVulcan121) {}
|
||||
|
||||
void Loudness::on_setup() {
|
||||
keyboard->send_led_to({ 0, 0, 0, 0 });
|
||||
void Loudness::onSetup() {
|
||||
keyboard->sendToLEDs({ 0, 0, 0, 0 });
|
||||
grabber->requestMode = AudioGrabber::ReqMode::PEAK;
|
||||
usleep(100000);
|
||||
}
|
||||
|
||||
void Loudness::on_tick(float delta) {
|
||||
void Loudness::onTick(float delta) {
|
||||
auto data = Vulcan121::createEmptyLEDMap();
|
||||
float val = grabber->getLoudness();
|
||||
val = val > 1.0f ? 1.0f : val;
|
||||
|
@ -25,7 +25,7 @@ namespace VIZ {
|
|||
}
|
||||
// delete map! ;)
|
||||
lastVal = val;
|
||||
keyboard->send_led_map(data, true);
|
||||
keyboard->sendLedMap(data, true);
|
||||
}
|
||||
|
||||
const char *Loudness::name() {
|
||||
|
|
|
@ -5,13 +5,13 @@ namespace VIZ {
|
|||
|
||||
}
|
||||
|
||||
void PoliceLike::on_setup() {
|
||||
keyboard->send_led_to({ 0, 0, 0, 0 });
|
||||
void PoliceLike::onSetup() {
|
||||
keyboard->sendToLEDs({ 0, 0, 0, 0 });
|
||||
grabber->requestMode = AudioGrabber::ReqMode::FFT;
|
||||
usleep(100000);
|
||||
}
|
||||
|
||||
void PoliceLike::on_tick(float delta) {
|
||||
void PoliceLike::onTick(float delta) {
|
||||
auto map = Vulcan121::createEmptyLEDMap();
|
||||
auto data = grabber->fft.getData()->leftChannel;
|
||||
auto val = 0.0;
|
||||
|
@ -32,7 +32,7 @@ namespace VIZ {
|
|||
map[0].key[col->index[j]] = colors[colorInd];
|
||||
}
|
||||
}
|
||||
keyboard->send_led_map(map, true);
|
||||
keyboard->sendLedMap(map, true);
|
||||
}
|
||||
|
||||
void PoliceLike::switchOnPeak(double peak) {
|
||||
|
|
|
@ -18,16 +18,16 @@ namespace VIZ {
|
|||
delete[] colours;
|
||||
delete data;
|
||||
}
|
||||
void RainbowLine::on_setup() {
|
||||
void RainbowLine::onSetup() {
|
||||
currentColumn = 0;
|
||||
updateMap(0);
|
||||
tailFactor = VUtils::Math::clamp(grabber->env->getAsDouble("rainbow_tail_factor", 0.3), 0.0, 0.9);
|
||||
keyboard->send_led_to({ 0, 0, 0, 0 });
|
||||
keyboard->sendToLEDs({ 0, 0, 0, 0 });
|
||||
grabber->requestMode = AudioGrabber::ReqMode::FFT;
|
||||
usleep(100000);
|
||||
}
|
||||
|
||||
void RainbowLine::on_tick(float delta) {
|
||||
void RainbowLine::onTick(float delta) {
|
||||
updateMap(tailFactor);
|
||||
deltaElapsed += delta;
|
||||
auto fftData = grabber->fft.getData()->leftChannel;
|
||||
|
@ -38,6 +38,26 @@ namespace VIZ {
|
|||
val = avg;
|
||||
}
|
||||
val = VUtils::Math::clamp(val, 0, 255);
|
||||
double avg = deltaMove(val);
|
||||
if (deltaElapsed >= deltaNeeded) {
|
||||
deltaElapsed = 0;
|
||||
currentColumn++;
|
||||
if (currentColumn >= keyboardData.num_cols) {
|
||||
currentColumn = 0;
|
||||
}
|
||||
}
|
||||
colours[currentColumn].a = val;
|
||||
auto column = keyboard->getColumn(currentColumn);
|
||||
for (int i = 0; i < column->count; ++i) {
|
||||
auto index = column->index[i];
|
||||
data[0].key[index] = colours[currentColumn];
|
||||
}
|
||||
lastValue = avg;
|
||||
// we clean up the map self later! :)
|
||||
keyboard->sendLedMap(data, false);
|
||||
}
|
||||
|
||||
double RainbowLine::deltaMove(double val) {
|
||||
auto avg = (lastValue + val) * 0.5;
|
||||
if (avg > MAX_PEAK || avg > decayValue) {
|
||||
calcNextDelta(val < MAX_PEAK && firstUnder ? 160 : val);
|
||||
|
@ -50,25 +70,9 @@ namespace VIZ {
|
|||
if (avg < 10) {
|
||||
deltaNeeded = 1000000000;
|
||||
}
|
||||
if (deltaElapsed >= deltaNeeded) {
|
||||
deltaElapsed = 0;
|
||||
currentColumn++;
|
||||
if (currentColumn >= keyboardData.num_cols) {
|
||||
currentColumn = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (decayValue >= 0)
|
||||
decayValue -= 10;
|
||||
colours[currentColumn].a = val;
|
||||
auto column = keyboard->getColumn(currentColumn);
|
||||
for (int i = 0; i < column->count; ++i) {
|
||||
auto index = column->index[i];
|
||||
data[0].key[index] = colours[currentColumn];
|
||||
}
|
||||
lastValue = avg;
|
||||
// we clean up the map self later! :)
|
||||
keyboard->send_led_map(data, false);
|
||||
return avg;
|
||||
}
|
||||
|
||||
const char *RainbowLine::name() {
|
||||
|
|
|
@ -3,12 +3,12 @@
|
|||
namespace VIZ {
|
||||
Spectrum::Spectrum(AudioGrabber *pGrabber, Vulcan121 *pVulcan121) : VIZ(pGrabber, pVulcan121) {}
|
||||
|
||||
void Spectrum::on_setup() {
|
||||
keyboard->send_led_to({ 0, 0, 0, 0 });
|
||||
void Spectrum::onSetup() {
|
||||
keyboard->sendToLEDs({ 0, 0, 0, 0 });
|
||||
grabber->requestMode = AudioGrabber::ReqMode::FFT;
|
||||
usleep(100000);
|
||||
}
|
||||
void Spectrum::on_tick(float delta) {
|
||||
void Spectrum::onTick(float delta) {
|
||||
auto map = Vulcan121::createEmptyLEDMap();
|
||||
auto data = grabber->fft.getData()->leftChannel;
|
||||
// find largest bass ~43hz (44100 / FFT_SIZE) range
|
||||
|
@ -34,7 +34,7 @@ namespace VIZ {
|
|||
x = 0;
|
||||
}
|
||||
}
|
||||
keyboard->send_led_map(map, true);
|
||||
keyboard->sendLedMap(map, true);
|
||||
}
|
||||
|
||||
const char *Spectrum::name() {
|
||||
|
|
|
@ -16,29 +16,29 @@ namespace VIZ {
|
|||
currentVis = viz[mode];
|
||||
}
|
||||
|
||||
void VisPlugins::on_startup() {
|
||||
if (!keyboard->send_init_sequence()) {
|
||||
void VisPlugins::onStartup() {
|
||||
if (!keyboard->sendInitSequence()) {
|
||||
ERR("FAILED TO INIT KEYBOARD")
|
||||
exit(1);
|
||||
}
|
||||
currentVis->on_setup();
|
||||
currentVis->onSetup();
|
||||
start = std::chrono::high_resolution_clock::now();
|
||||
}
|
||||
|
||||
void VisPlugins::on_tick() {
|
||||
void VisPlugins::onTick() {
|
||||
auto stop = std::chrono::high_resolution_clock::now();
|
||||
auto delta = std::chrono::duration_cast<micro>(stop - start).count();
|
||||
currentVis->on_tick(delta);
|
||||
currentVis->onTick(delta);
|
||||
usleep(1000);
|
||||
start = stop;
|
||||
}
|
||||
|
||||
void VisPlugins::on_shutdown() {
|
||||
void VisPlugins::onShutdown() {
|
||||
int16_t r = env->getAsInt("shutdown_color_red", 0);
|
||||
int16_t g = env->getAsInt("shutdown_color_green", 0);
|
||||
int16_t b = env->getAsInt("shutdown_color_blue", 150);
|
||||
int16_t a = env->getAsInt("shutdown_brightness", 100);
|
||||
keyboard->send_led_to({ r, g, b, a });
|
||||
keyboard->sendToLEDs({ r, g, b, a });
|
||||
}
|
||||
|
||||
VisPlugins::~VisPlugins() {
|
||||
|
@ -58,7 +58,7 @@ namespace VIZ {
|
|||
m -= 1;
|
||||
currentVis = viz[m];
|
||||
LOG("Now Using: %s", currentVis->name());
|
||||
currentVis->on_setup();
|
||||
currentVis->onSetup();
|
||||
mode = m;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue