Rearranged files

This commit is contained in:
2022-06-24 21:07:30 -07:00
parent ab9f15e137
commit 952d03ee10
7 changed files with 2 additions and 2 deletions

142
src/utility/bitmap.cpp Normal file
View File

@ -0,0 +1,142 @@
#include "bitmap.hpp"
Bitmap::Bitmap(size_t w, size_t h) {
this->width = w;
this->height = h;
this->data.reserve(h);
for (size_t r = 0; r < h; r++) {
data[r].reserve(w);
}
clear();
}
void Bitmap::dump_int_to_bytes(
uint64_t a,
uint8_t *array,
size_t index,
size_t num_bytes
) {
// Dump bytes of i into array starting at index.
// least-significant byte goes first.
if (num_bytes > 4) {
throw std::invalid_argument("Cannot dump more than 4 bytes.");
}
for (size_t i = 0; i < num_bytes; i++) {
array[index + i] = (a >> (8 * i)) & 0xFF;
}
}
void Bitmap::clear() {
// Fill with white pixels
for (size_t r = 0; r < height; r++) {
for (size_t i = 0; i < width; i++) {
data[r][i] = std::make_tuple(
0xFF, 0xFF, 0xFF
);
}
}
}
size_t Bitmap::get_width() const {
return width;
}
size_t Bitmap::get_height() const {
return height;
}
void Bitmap::setpixel(
size_t row,
size_t col,
uint8_t r,
uint8_t g,
uint8_t b
) {
data[row][col] = std::make_tuple(r, g, b);
}
void Bitmap::save(const char *filename) const {
uint8_t header[54] = {
// Universal 14-byte header
0x42, 0x4D, // Bitmap standard header
0x00, 0x00, 0x00, 0x00, // file size (bit 2, overwritten)
0x00, 0x00, 0x00, 0x00, // Reserved (leave as zero)
0x36, 0x00, 0x00, 0x00, // Index of first byte of pixmap
// 40-byte BITMAPINFOHEADER
0x28, 0x00, 0x00, 0x00, // Length of DIB
0x00, 0x00, 0x00, 0x00, // width (px, bit 18, overwritten)
0x00, 0x00, 0x00, 0x00, // height (px, bit 22, overwritten)
0x01, 0x00, // Number of color planes (always 1)
0x18, 0x00, // Number of bits per pixel (0x18 = 24)
0x00, 0x00, 0x00, 0x00, // BI_RGB, no pixel array compression
0x00, 0x00, 0x00, 0x00, // Size of raw bitmap data, incl. padding (bit 34, overwritten)
0xFF, 0x0A, 0x00, 0x00, // Size of horizontal print resolution (px/m)
0xFF, 0x0A, 0x00, 0x00, // Size of vertical print resolution (px/m)
0x00, 0x00, 0x00, 0x00, // Size of colors in palette
0x00, 0x00, 0x00, 0x00 // 0 important colors => all colors are important
};
// How many bytes we need to store a row, including padding
size_t row_size = ((24 * width + 31) / 32) * 4;
// Total size of pixmap
uint64_t pixmap_size = row_size * height;
// Total size of file
uint64_t file_size = 54 + pixmap_size;
// Set values in header
dump_int_to_bytes(file_size, header, 2, 4);
dump_int_to_bytes(width, header, 18, 4);
dump_int_to_bytes(height, header, 22, 4);
dump_int_to_bytes(pixmap_size, header, 34, 4);
FILE *fp = fopen(filename, "wb");
fwrite(header, sizeof(header), 1, fp);
uint8_t r, g, b;
for (size_t row = 0; row < height; row++) {
size_t i = 0;
// Pixel data
while (i < width) {
std::tie(r, g, b) = data[height - row - 1][i];
fwrite(&b, sizeof(uint8_t), 1, fp);
fwrite(&g, sizeof(uint8_t), 1, fp);
fwrite(&r, sizeof(uint8_t), 1, fp);
i += 1;
}
i *= 3;
// Row Padding
r = 0;
while (i < row_size) {
fwrite(&r, sizeof(uint8_t), 1, fp);
i += 1;
}
}
fclose(fp);
}
/*
int main() {
Bitmap b = Bitmap(200, 200);
b.save("file.bmp");
}
*/

51
src/utility/bitmap.hpp Normal file
View File

@ -0,0 +1,51 @@
#pragma once
#include <iostream>
#include <cstdint>
#include <vector>
#include <tuple>
class Bitmap {
public:
Bitmap(size_t w, size_t h);
void clear();
void setpixel(
size_t row,
size_t col,
uint8_t r,
uint8_t g,
uint8_t b
);
void save(const char *filename) const;
size_t get_width() const;
size_t get_height() const;
private:
size_t width;
size_t height;
// Pixel data.
// First is a vector of rows,
// Second is a vector of pixels.
// RGB format.
std::vector<
std::vector<
std::tuple<
uint8_t,
uint8_t,
uint8_t
>>
> data;
static void dump_int_to_bytes(
uint64_t a,
uint8_t* bytes,
size_t index,
size_t num_bytes
);
};

86
src/utility/buffer.cpp Normal file
View File

@ -0,0 +1,86 @@
#include "buffer.hpp"
/*
A Buffer consists of three parts:
an incoming vector, a rollingbuffer, and an output vector.
Note that the RollingBuffer class does most of the work:
its `get` and `put` methods move the values in our
input and output vectors.
*/
Buffer::Buffer(
const char *pipe_file,
size_t buffer_size,
size_t output_size
):
buffer_size(buffer_size),
output_size(output_size),
pipe_file(pipe_file)
{
sample_con_rate = 5;
sample_con_rate_up_ctr = 0;
sample_con_rate_dn_ctr = 0;
incoming.resize(buffer_size);
rolling_buffer.resize(buffer_size);
output.resize(output_size);
}
void Buffer::update() {
// TODO:
// Disable and enable FIFO here to get rid of
// the difference between audio and visualization.
int fd = open(pipe_file, O_RDONLY);
ssize_t bytes_read = read(
fd,
incoming.data(),
sizeof(int16_t) * incoming.size()
);
const auto begin = incoming.begin();
const auto end = incoming.begin() + bytes_read/sizeof(int16_t);
// Autoscale here
rolling_buffer.put(begin, end);
// 44100 samples per second / fps = samples per frame
// 60 fps
// *2 if stereo
size_t requested_samples = (44100 / 60) * pow(1.1, sample_con_rate);
size_t new_samples = rolling_buffer.get(
requested_samples,
output
);
if (new_samples == 0) {
printf("no new samples\n");
return;
}
// A crude way to adjust the amount of samples consumed from the buffer
// depending on how fast the rendering is.
if (rolling_buffer.size() > 0) {
if (++sample_con_rate_up_ctr > 8) {
sample_con_rate_up_ctr = 0;
++sample_con_rate;
}
} else if (sample_con_rate > 0) {
if (++sample_con_rate_dn_ctr > 4) {
sample_con_rate_dn_ctr = 0;
--sample_con_rate;
}
sample_con_rate_up_ctr = 0;
}
if (fd >= 0) {
close(fd);
}
}

39
src/utility/buffer.hpp Normal file
View File

@ -0,0 +1,39 @@
#pragma once
#include <cstdint>
#include <vector>
#include <math.h>
// For reading FIFO
#include <fcntl.h>
#include <unistd.h>
#include "rollingbuffer.hpp"
class Buffer {
public:
Buffer(
const char *pipe_file,
size_t buffer_size,
size_t output_size
);
void update();
const std::vector<int16_t>& get_output() const {
return output;
};
private:
std::vector<int16_t> incoming;
RollingBuffer<int16_t> rolling_buffer;
std::vector<int16_t> output;
size_t buffer_size;
size_t output_size;
const char *pipe_file;
size_t sample_con_rate;
size_t sample_con_rate_up_ctr;
size_t sample_con_rate_dn_ctr;
};

View File

@ -0,0 +1,137 @@
#pragma once
#include <stdexcept>
#include <stdlib.h>
#include <cstdint>
#include <vector>
/*
This file defines a simple "RollingBuffer" class, based on the
implementation of SampleBuffer in ncmpcpp.
*/
template<class T>
struct RollingBuffer {
public:
typedef typename std::vector<T>::iterator Iterator;
RollingBuffer() : m_offset(0) { }
void put(Iterator begin, Iterator end);
size_t get(size_t elems, std::vector<T> &dest);
void resize(size_t n);
void clear();
size_t size() const;
const std::vector<T> &buffer() const;
private:
size_t m_offset;
std::vector<T> m_buffer;
};
template <class T>
void RollingBuffer<T>::put(
RollingBuffer::Iterator begin,
RollingBuffer::Iterator end
) {
// How many elements to put
size_t elems = end - begin;
if (elems > m_buffer.size()) {
throw std::out_of_range("Size of the buffer is smaller than the amount of elements");
}
// How much space we have for new elements
size_t free_elems = m_buffer.size() - m_offset;
// If we don't have enough free space,
// make more by moving the buffer forwards.
if (elems > free_elems) {
size_t to_remove = elems - free_elems;
std::copy(
m_buffer.begin() + to_remove,
m_buffer.end() - free_elems,
m_buffer.begin()
);
m_offset -= to_remove;
}
// Add new elements to buffer
std::copy(begin, end, m_buffer.begin() + m_offset);
m_offset += elems;
}
template <class T>
size_t RollingBuffer<T>::get(
size_t elems,
std::vector<T> &dest
) {
// elems: how many samples to get
// dest: where to put them
// (m_offset == 0) => we have no data
if (m_offset == 0) {
return 0;
}
// If too many elements are requested,
// give as much as we can.
if (elems > m_offset) {
elems = m_offset;
}
if (elems >= dest.size()) {
// If dest is smaller than request size,
// discard earlier elements to fit.
size_t elems_lost = elems - dest.size();
std::copy(m_buffer.begin() + elems_lost, m_buffer.begin() + elems, dest.begin());
} else {
// otherwise, copy samples to the destination buffer.
std::copy(dest.begin() + elems, dest.end(), dest.begin());
std::copy(m_buffer.begin(), m_buffer.begin() + elems, dest.end() - elems);
}
// Remove elements from the internal buffer.
std::copy(m_buffer.begin() + elems, m_buffer.begin() + m_offset, m_buffer.begin());
m_offset -= elems;
return elems;
}
template <class T>
void RollingBuffer<T>::resize(size_t n) {
m_buffer.resize(n);
clear();
}
template <class T>
void RollingBuffer<T>::clear() {
m_offset = 0;
}
template <class T>
size_t RollingBuffer<T>::size() const {
return m_offset;
}
template <class T>
const std::vector<T> &RollingBuffer<T>::buffer() const {
return m_buffer;
}