Skip to content

Instantly share code, notes, and snippets.

View SteveTrewick's full-sized avatar

Steve Trewick SteveTrewick

View GitHub Profile
@SteveTrewick
SteveTrewick / HexDump.swift
Last active February 1, 2025 18:52
Dump familiar hexies in swift 16 bytes then UTF8 chars
import Foundation
/*
Hex Dumper
*/
public struct HexDump {
/*
@SteveTrewick
SteveTrewick / sizeof.swift
Created January 6, 2025 21:02
stored a metatype and need to get the MemoryLayout, etc? Yeah, me too.
// if we have a type param, can we get the bits we need out of it,
// like length?
let xtype = UInt16.self
// MemoryLayout<xtype> etc, no no no, so ...
func sizeof<T>(_ value: T.Type) -> Int {
MemoryLayout<T>.size
}
let sz = sizeof(xtype)
@SteveTrewick
SteveTrewick / memhex.swift
Last active January 5, 2025 23:34
hex bytes of swift int in memory order (LSB->MSB)
// hex bytes of swift int in memory order (LSB->MSB)
func memhex<T: BinaryInteger>(_ value: T) -> String {
var value = value
var hex = ""
withUnsafeBytes(of: &value) { bytes in
for byte in bytes {
hex += String(format:"%02x", byte) + " "
}
@SteveTrewick
SteveTrewick / IQ_Oscillator.swift
Created June 28, 2023 14:27
Generate the I and Q components for a local oscillator
import Foundation
/*
Generate the I and Q components for a local oscillator
*/
public class IQ_Oscillator {
let tau = 2 * Double.pi
@SteveTrewick
SteveTrewick / Goertzel.swift
Created November 11, 2022 16:52
Demonstartion of a Goertzel Algorithm in Swift
import Foundation
/*
Demo implementation of the famous Goertzel algorithm in Swift
NB it is not currently possible to make this generic without
also pulling in the Swift Numerics package.
@SteveTrewick
SteveTrewick / AVFoundationBufferSize.swift
Last active November 9, 2022 13:55
Set the buffer size for AVFoundation captures including, e.g, AVCaptureSession via an AVCaptureAudioDataOutput
// couple of useful functions for dealing with an AVCaptureSession
// chucked out there for posterity and search
// What led to me to do this : Set size of AVCaptureAudioDataOutput buffer
// What might also lead you here : Swift AudioValueTranslation
// PLEASE NOTE : There are no error checks in this code, demo purposes only.
import AVFoundation
@SteveTrewick
SteveTrewick / AudioDeviceEnumerator.swift
Created November 5, 2022 13:11
Enumerate audio input and output devices using Swift on macOS
import Foundation
import CoreAudio
import OSLog
/*
So you want to get a list of audio input and output devices on macOS do you?
Should be easy right?
@SteveTrewick
SteveTrewick / bitreverse.swift
Last active March 9, 2021 17:32
Reverse the bits of an integer in Swift
func reverse <T: FixedWidthInteger> ( word: T ) -> T {
var word = word
var rev : T = 0
for _ in (0..<word.bitWidth) {
rev = (rev << 1) + (word & 0b1)
word >>= 1
}
return rev
@SteveTrewick
SteveTrewick / HTML-DSL3.swift
Last active July 22, 2020 18:14
Now with some embedded text tags. I'm not sold on the method or the code, but it beats writing a ton of weird combinations of buildBlock. This is one way. There is another!
import Foundation
// so the main limitation of the previous efforts was that we couldn't do e.g.
// p { "here is " em { "some" } "emphasis" }
// without starting to write crazy amounts of buildBlock functions, so...
extension XMLNode {
func named(_ name: String) -> XMLNode {
@SteveTrewick
SteveTrewick / HTML-DSL2.swift
Last active July 22, 2020 15:56
Second and much nicer cut on HTML DSL in swift. No attributes etc yet, but this is a cleaner base design (in some ways at least)
import Foundation
extension XMLNode {
func named(_ name: String) -> XMLNode {
self.name = name
return self
}