2022-06-24 21:33:12 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <vector>
|
2022-06-26 12:39:02 -07:00
|
|
|
#include <stdexcept>
|
2022-06-24 21:33:12 -07:00
|
|
|
// For reading FIFO
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2022-07-08 19:07:46 -07:00
|
|
|
// For sleep
|
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
2022-07-11 09:39:33 -07:00
|
|
|
// MPD client
|
|
|
|
#include "mpd/client.h"
|
2022-06-24 21:33:12 -07:00
|
|
|
|
|
|
|
// Local files
|
|
|
|
#include "utility/bitmap.hpp"
|
|
|
|
#include "utility/buffer.hpp"
|
|
|
|
#include "signal_processing/fft.hpp"
|
2022-06-26 12:39:02 -07:00
|
|
|
#include "ergodox.hpp"
|
2022-07-08 10:41:19 -07:00
|
|
|
#include "commands.h"
|
2022-07-08 16:56:32 -07:00
|
|
|
#include "config.h"
|
2022-06-25 19:12:01 -07:00
|
|
|
|
2022-07-08 19:36:05 -07:00
|
|
|
#include "spdlog/spdlog.h"
|
|
|
|
|
2022-06-25 19:12:01 -07:00
|
|
|
|
2022-06-24 21:33:12 -07:00
|
|
|
// TODO:
|
|
|
|
//
|
2022-07-11 09:40:48 -07:00
|
|
|
// MPD connection error handling
|
|
|
|
// Cleaner building
|
|
|
|
//
|
2022-07-08 16:56:32 -07:00
|
|
|
// FFT:
|
|
|
|
// stereo support (and maybe different bitrates?)
|
|
|
|
// Optimization: don't copy filename in buffer?
|
|
|
|
// understand consumption rate
|
|
|
|
// understand BIN2HZ
|
|
|
|
// understand values and sizes (DFT_TOTAL, DFT_NONZERO, etc)
|
|
|
|
// note that wave and spectrum have different sizes
|
|
|
|
// clear fft when not in use
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Keyboard interface:
|
2022-07-09 20:36:45 -07:00
|
|
|
// Fix segfault
|
2022-07-08 16:56:32 -07:00
|
|
|
// Clean up reconnect code
|
2022-07-08 19:36:05 -07:00
|
|
|
// Better log messages, compiled spdlog
|
2022-07-08 16:56:32 -07:00
|
|
|
//
|
|
|
|
// Get parameters from keyboard (width, height, etc)
|
|
|
|
//
|
|
|
|
// Later:
|
|
|
|
// beat detection
|
|
|
|
// waveform animation
|
2022-07-08 17:00:20 -07:00
|
|
|
// pcm from pulse
|
2022-07-08 10:41:19 -07:00
|
|
|
|
|
|
|
|
2022-07-08 10:45:08 -07:00
|
|
|
const size_t width = 10;
|
2022-07-08 16:56:32 -07:00
|
|
|
const size_t height = BOTTOM_SKIP + KB_RESOLUTION + TOP_SKIP;
|
2022-06-24 21:33:12 -07:00
|
|
|
|
|
|
|
|
2022-07-11 09:39:33 -07:00
|
|
|
|
|
|
|
|
2022-06-24 21:33:12 -07:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
2022-07-08 19:36:05 -07:00
|
|
|
spdlog::set_level(spdlog::level::info);
|
|
|
|
|
2022-06-24 21:33:12 -07:00
|
|
|
// buffer size for waveform:
|
|
|
|
// (44100 / fps * 10), make 10 bigger for slower scrolling
|
|
|
|
//
|
|
|
|
// Double both buffer sizes if stereo
|
|
|
|
|
2022-06-26 12:49:50 -07:00
|
|
|
// FFT generator
|
2022-06-24 21:33:12 -07:00
|
|
|
FFT_Visualizer fft = FFT_Visualizer(
|
2022-07-08 16:56:32 -07:00
|
|
|
width, height, MIN_HZ, MAX_HZ
|
2022-06-24 21:33:12 -07:00
|
|
|
);
|
|
|
|
|
2022-06-26 12:49:50 -07:00
|
|
|
// Audio buffer
|
2022-06-24 21:33:12 -07:00
|
|
|
Buffer buf = Buffer(
|
|
|
|
"/tmp/mpd.fifo",
|
|
|
|
44100 / 2, // Keep 500ms of data in buffer
|
|
|
|
fft.compute_buffer_output_size()
|
|
|
|
);
|
|
|
|
|
2022-06-26 12:49:50 -07:00
|
|
|
// HID interface wrapper
|
|
|
|
Ergodox Dox = Ergodox::init(
|
2022-07-08 16:56:32 -07:00
|
|
|
HID_VENDOR_ID,
|
|
|
|
HID_PRODUCT_ID,
|
|
|
|
HID_USAGE,
|
|
|
|
HID_USAGE_PAGE
|
2022-06-26 12:49:50 -07:00
|
|
|
);
|
|
|
|
|
2022-07-08 10:41:19 -07:00
|
|
|
// Write buffer
|
2022-07-08 16:33:42 -07:00
|
|
|
uint8_t hid_buf[Dox.packet_size];
|
2022-06-26 12:49:50 -07:00
|
|
|
|
2022-07-08 16:56:32 -07:00
|
|
|
Dox.connect_loop();
|
2022-07-08 10:41:19 -07:00
|
|
|
|
|
|
|
|
2022-07-08 19:07:46 -07:00
|
|
|
// Frame rate limiter
|
|
|
|
std::chrono::time_point<
|
|
|
|
std::chrono::steady_clock,
|
|
|
|
std::chrono::nanoseconds
|
|
|
|
> t = std::chrono::steady_clock::now();
|
|
|
|
|
2022-07-11 09:39:33 -07:00
|
|
|
std::chrono::time_point<
|
|
|
|
std::chrono::steady_clock,
|
|
|
|
std::chrono::nanoseconds
|
|
|
|
> last_fifo_sync = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
struct mpd_connection *conn = mpd_connection_new(NULL, 0, 0);
|
2022-07-08 19:07:46 -07:00
|
|
|
|
2022-07-08 16:33:42 -07:00
|
|
|
while (1) {
|
|
|
|
|
|
|
|
if (Dox.is_connected()) {
|
2022-07-08 19:07:46 -07:00
|
|
|
if (std::chrono::steady_clock::now() > t + std::chrono::milliseconds(30)) {
|
|
|
|
if (Dox.get_animation_mode() == 0x02) {
|
2022-07-11 09:39:33 -07:00
|
|
|
if (std::chrono::steady_clock::now() > last_fifo_sync + std::chrono::seconds(10)) {
|
|
|
|
mpd_run_disable_output(conn, 1);
|
|
|
|
mpd_run_enable_output(conn, 1);
|
|
|
|
last_fifo_sync = std::chrono::steady_clock::now();
|
|
|
|
spdlog::info("Synchronized fifo");
|
|
|
|
}
|
|
|
|
|
2022-07-08 19:07:46 -07:00
|
|
|
buf.update();
|
|
|
|
fft.update(buf);
|
|
|
|
|
|
|
|
hid_buf[0] = CMD_ANIM_DATA_fft;
|
|
|
|
for (size_t i = 0; i < 10; i++) {
|
|
|
|
// Get height from fft, apply bottom_skip
|
|
|
|
ssize_t h = fft.get_output()[i] - BOTTOM_SKIP;
|
|
|
|
|
|
|
|
// Enforce max and min
|
|
|
|
// max implicitly enforces top_skip
|
|
|
|
h = h>KB_RESOLUTION ? KB_RESOLUTION : h;
|
|
|
|
h = h<0 ? 0 : h;
|
|
|
|
|
|
|
|
hid_buf[i + 1] = h;
|
|
|
|
}
|
|
|
|
}
|
2022-07-08 10:41:19 -07:00
|
|
|
|
2022-07-08 19:07:46 -07:00
|
|
|
Dox.write(CMD_ANIM_DATA, hid_buf, Dox.packet_size);
|
2022-07-08 10:41:19 -07:00
|
|
|
|
2022-07-08 19:07:46 -07:00
|
|
|
t = std::chrono::steady_clock::now();
|
2022-07-08 10:41:19 -07:00
|
|
|
}
|
2022-06-25 19:12:01 -07:00
|
|
|
|
2022-07-08 16:33:42 -07:00
|
|
|
// Dox.write might detect that we've been disconnected,
|
|
|
|
// and Dox.read will fail if we are.
|
|
|
|
// This check prevents it from doing that, and instead jumps to reconnect.
|
|
|
|
if (!Dox.is_connected()) { continue; }
|
2022-07-08 10:41:19 -07:00
|
|
|
|
2022-07-08 16:33:42 -07:00
|
|
|
// Read a packet if there is a packet to read
|
|
|
|
if (Dox.read()) {
|
|
|
|
uint8_t cmd = Dox.read_buf[0];
|
|
|
|
|
|
|
|
switch(cmd) {
|
|
|
|
case CMD_SEND_STATE:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-07-08 16:56:32 -07:00
|
|
|
Dox.connect_loop();
|
2022-07-08 10:41:19 -07:00
|
|
|
}
|
2022-06-24 21:33:12 -07:00
|
|
|
}
|
2022-06-26 12:49:50 -07:00
|
|
|
|
2022-07-11 09:39:33 -07:00
|
|
|
mpd_connection_free(conn);
|
2022-06-25 19:12:01 -07:00
|
|
|
return 0;
|
2022-06-24 21:33:12 -07:00
|
|
|
}
|