Skip to content

Instantly share code, notes, and snippets.

View jhurliman's full-sized avatar
🐨

John Hurliman jhurliman

🐨
View GitHub Profile
@jhurliman
jhurliman / Colors.ts
Created October 24, 2020 09:38
TypeScript methods for color conversion
export type HSL = [number, number, number]
export function HexToHSL(hex: string): HSL | undefined {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
if (!result) return undefined
const r = parseInt(result[1], 16) / 255
const g = parseInt(result[2], 16) / 255
const b = parseInt(result[3], 16) / 255
const max = Math.max(r, g, b)
@jhurliman
jhurliman / akai-apc40mkii-colors.json
Created October 24, 2020 08:02
Akai APC40 MkII LED colors as hex RGB list
[
"#FF4C4C",
"#FF0000",
"#590000",
"#190000",
"#FFBD6C",
"#FF5400",
"#591D00",
"#271B00",
"#FFFF4C",
@jhurliman
jhurliman / split2.sh
Created May 21, 2020 08:41
Use spleeter to split an input MP3 into separate vocal and accompaniment WAV files
#!/usr/bin/env bash
DIR=$(dirname `readlink -e $1`)
FILE=$(basename `readlink -e $1`)
OUT_DIR=`basename ${FILE} .mp3`
MODEL_DIRECTORY="${HOME}/.cache/spleeter"
mkdir -p ${MODEL_DIRECTORY}
docker run \
-v ${DIR}:/work \
@jhurliman
jhurliman / circular_array.h
Created April 5, 2020 08:42
CircularArray - Wraps std::array and provides a circular iterator in C++
#pragma once
#include <algorithm>
#include <array>
template <class T, size_t N>
class CircularArray {
public:
CircularArray() {}
CircularArray(const T initValue) { std::fill_n(data_.begin(), data_.size(), initValue); }
@jhurliman
jhurliman / HexArrayToStr.cpp
Created February 29, 2020 09:01
C++ Print a char* out as a hex dump
static void HexArrayToStr(const char* data, size_t length, std::string& output) {
const char* NIBBLE_TO_HEX = {"0123456789ABCDEF"};
output.assign(length * 2 + 1, 0);
char* buffer = output.data();
for (size_t i = 0; i < length; i++) {
int nibble = uint8_t(data[i]) >> 4;
buffer[2 * i] = NIBBLE_TO_HEX[nibble];
nibble = uint8_t(data[i]) & 0x0F;
buffer[2 * i + 1] = NIBBLE_TO_HEX[nibble];
}
@jhurliman
jhurliman / color.h
Last active February 8, 2020 10:21
COLOR macro for C/C++ ANSI color code printing
// Assumes the surrounding context has a bool named `color`
#define COLOR( code, s ) ( color ? "\033[0;" #code "m" : "" ) << s << ( color ? "\033[0;m" : "" )
@jhurliman
jhurliman / Dockerfile-cppgraphicsdev
Created January 27, 2020 21:10
Dockerfile for C++ graphics development
FROM ubuntu:18.04
# Install common C++ and imaging libraries, Clang, and add Clang to the PATH
RUN apt-get update && apt-get install -y \
xz-utils \
git \
wget \
nano \
build-essential \
libboost-all-dev \
@jhurliman
jhurliman / record-video0.sh
Created May 24, 2019 05:54
Record from a HDMI to USB adapter
ffmpeg -f v4l2 -framerate 10 -thread_queue_size 512 -i /dev/video0 -t 5 -c:v h264 -pix_fmt yuv420p test001.mp4
@jhurliman
jhurliman / UntrackedAllocator.h
Created January 15, 2019 07:47
Simple std::allocator example wrapping malloc/free
#pragma once
#include <memory>
#define NOMINMAX
#undef max
template <typename T>
class UntrackedAllocator {
public:
@jhurliman
jhurliman / result.hpp
Created December 29, 2018 02:03
C++ Result type wrapping std::expected and std::exception
template<typename T>
class [[nodiscard]] Result {
public:
Result(const T& value) : _result(value) {}
Result(T && value) : _result(std::move(value)) {}
Result(std::exception error) : _result(std::unexpected<std::exception>(std::move(error))) {}
bool isOk() const {
markChecked();
return _result.has_value();