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>
|
|
|
|
|
|
|
|
// 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-06-25 19:12:01 -07:00
|
|
|
|
|
|
|
|
2022-06-24 21:33:12 -07:00
|
|
|
// TODO:
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
// MPD interface
|
|
|
|
// hid interface to keyboard
|
|
|
|
// beat detection
|
|
|
|
|
|
|
|
|
|
|
|
void draw_spectrum_bitmap(
|
|
|
|
const std::vector<size_t>& waveform,
|
|
|
|
Bitmap& bitmap
|
|
|
|
) {
|
|
|
|
for (size_t x = 0; x < waveform.size(); x++) {
|
|
|
|
for (size_t y = 0; y < waveform[x]; y++) {
|
|
|
|
bitmap.setpixel(bitmap.get_height() - y - 1, x, 0xFF, 0x00, 0x00);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-08 10:41:19 -07:00
|
|
|
|
|
|
|
|
|
|
|
// How many keys in a column * resolution per key.
|
|
|
|
// this MUST fit inside a uint8_t.
|
|
|
|
const uint8_t kb_resolution = 5 * 50;
|
|
|
|
|
|
|
|
// How many resolution steps to skip at the top and bottom.
|
|
|
|
const size_t bottom_skip = 25;
|
|
|
|
const size_t top_skip = 50;
|
|
|
|
|
|
|
|
|
2022-07-08 10:45:08 -07:00
|
|
|
const size_t width = 10;
|
2022-07-08 10:41:19 -07:00
|
|
|
const size_t height = bottom_skip + kb_resolution + top_skip;
|
2022-06-24 21:33:12 -07:00
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
|
|
|
// 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(
|
|
|
|
width, height,
|
2022-07-08 10:41:19 -07:00
|
|
|
100, 5000
|
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(
|
|
|
|
0x3297,
|
|
|
|
0x4976,
|
|
|
|
0x61,
|
|
|
|
0xFF60
|
|
|
|
);
|
|
|
|
|
2022-07-08 10:41:19 -07:00
|
|
|
// Write buffer
|
2022-06-26 12:49:50 -07:00
|
|
|
uint8_t hid_buf[12];
|
|
|
|
|
2022-06-24 21:33:12 -07:00
|
|
|
while (1) {
|
2022-07-08 10:41:19 -07:00
|
|
|
|
|
|
|
|
|
|
|
if (Dox.get_animation_mode() == 0x02) {
|
|
|
|
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
|
2022-07-08 10:45:08 -07:00
|
|
|
ssize_t h = fft.get_output()[i] - bottom_skip;
|
2022-07-08 10:41:19 -07:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
Dox.write(CMD_ANIM_DATA, hid_buf, 13);
|
2022-06-25 19:12:01 -07:00
|
|
|
}
|
|
|
|
|
2022-07-08 10:41:19 -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;
|
|
|
|
}
|
|
|
|
}
|
2022-06-24 21:33:12 -07:00
|
|
|
}
|
2022-06-26 12:49:50 -07:00
|
|
|
|
2022-06-25 19:12:01 -07:00
|
|
|
return 0;
|
2022-06-24 21:33:12 -07:00
|
|
|
}
|