Skip to content

Instantly share code, notes, and snippets.

@arifd
arifd / on_change_controller.rs
Last active May 19, 2021 12:17
A helper mod for the Rust Driuid GUI library, to bypass the AppDelgate in order to mutate data on a data change event
use druid::{widget::Controller, Data, Env, Event, EventCtx, Selector, UpdateCtx, Widget};
/// Bypass the `AppDelegate` entirely by appending the return from this function as a
/// `Controller` wrapping whatever you wish to perform a desired action when there is a change in the data
///
/// # Example
///
/// ```
///TextBox::new()
/// .lens(AppData::foo)
@arifd
arifd / place.rs
Created May 24, 2021 14:44
Druid GUI Widget that holds a child and places it at a given x,y coordinate relative to parent
//! Place a widget anywhere in x,y pixel space
use druid::widget::prelude::*;
use druid::{Data, Point, WidgetPod};
pub struct Place<T> {
children: Vec<ChildWidget<T>>,
}
@arifd
arifd / mmcapture
Last active October 5, 2021 21:32
FFMPEG wrapper for screen/microphone capture with well tuned defaults/processing
#!/bin/bash
###############################################################################
# This script is a wrapper over ffmpeg with well-tuned defaults and filters #
# to get the best out of your input devices #
# #
# Screen capture to file will prompt to trim, master and encode audio #
# #
# TODO: camera source / v4l2 sink still not implemented #
# #
@arifd
arifd / mmtrim
Last active September 26, 2021 15:10
Take an input movie file, blend a thumbnail to the beginning, normalize loudess and fade out at the end
#!/bin/bash
# Take an input movie file,
# blend a thumbnail to the beginning
# normalize loudness and fade in audio
# fade out at end
# encode for youtube/sharing
# Generate a test video:
# ffmpeg -f lavfi -i testsrc -f lavfi -i aevalsrc="sin(100*t*2*PI*t)" -vf "drawtext=box=1:fontsize=70:text='%{pts\:flt}':x=(w-text_w)/2:y=(h-text_h)/2" -t 10 out.avi
@arifd
arifd / yuyv422_to_rgb24.rs
Last active October 20, 2024 11:35
YUV to RGB in Rust
#![allow(dead_code)]
//! https://www.kernel.org/doc/html/v4.17/media/uapi/v4l/pixfmt-yuyv.html
//!
//! V4L2_PIX_FMT_YUYV — Packed format with ½ horizontal chroma resolution, also known as YUV 4:2:2
//! Description
//!
//! In this format each four bytes is two pixels. Each four bytes is two Y's, a Cb and a Cr. Each Y goes to one of the pixels, and the Cb and Cr belong to both pixels. As you can see, the Cr and Cb components have half the horizontal resolution of the Y component. V4L2_PIX_FMT_YUYV is known in the Windows environment as YUY2.
//!
//! Example 2.19. V4L2_PIX_FMT_YUYV 4 × 4 pixel image
@arifd
arifd / test.rs
Last active June 26, 2022 15:38
uppercase, lowercase, Unicode, chars and Rust
fn main() {
let y = "y̆";
let mut chars = y.chars();
assert_eq!(Some('y'), chars.next()); // not 'y̆'
assert_eq!(Some('\u{0306}'), chars.next());
// INTERESTING: chars is unicode compatible, it just gives you code points in between your graphemes
for c in y.chars() {
println!("{}", c.is_lowercase()); // true, false
}
@arifd
arifd / virtual_mic.sh
Last active July 10, 2022 10:49
Very high quality DSP for microphone input
#!/bin/bash
SCRIPT_NAME="$(basename "$0")"
AUDIO_INTERFACE_NAME="Scarlett Solo USB" # Discover name with command `arecord -l`
AUDIO_INTERFACE_DETAILS=$(arecord -l | grep "${AUDIO_INTERFACE_NAME}")
AUDIO_CARD=$(echo "${AUDIO_INTERFACE_DETAILS}" | grep -o -E 'card [0-9]+:' | tr -dc '0-9')
AUDIO_DEVICE=$(echo "${AUDIO_INTERFACE_DETAILS}" | grep -o -E 'device [0-9]+:' | tr -dc '0-9')
AUDIO_BUFFER_SIZE="2048" # The lower the number, the less latency introduced, but more likely buffer overruns
@arifd
arifd / non-crpytographically secure hash checklist
Created January 30, 2023 12:46
When to know if you can get away with a non-cryptographically secure hashing algorithm
Would it be bad if someone figured out how to manipulate the hash function to get a certain result?
Would it be bad if someone found out the input from the hash?
@arifd
arifd / CPoint.rs
Last active February 5, 2023 11:59
const QUANT: f32 = u16::MAX as f32;
struct CPoint(u16);
impl From<f32> for CPoint {
fn from(f: f32) -> Self {
assert!((0.0..=1.0).contains(&f), "given float {f} out of bounds");
CPoint((f * QUANT) as u16)
}
}
impl From<CPoint> for f32 {
@arifd
arifd / timing.rs
Created March 2, 2023 13:48
log slow functions, optimized out in release, example of cancelling a thread
use std::{
sync::mpsc,
time::{Duration, Instant},
};
/// Warns on any function that is behaving slow
///
/// Useful for when your code appears to be getting stuck somewhere
/// and you want a fast "debug print" way of knowing where.
#[inline]