Skip to content

Instantly share code, notes, and snippets.

@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
}
import Foundation
@_functionBuilder struct XMLBuilder {
static func buildBlock(_ content: String) -> String {
return content
}
@SteveTrewick
SteveTrewick / PeekAheadIterator.swift
Last active March 2, 2019 18:49
When you just really need both the current and next elements in a swift sequence
import Foundation
struct PeekAheadIterator<T> {
let elements:[T]
public init(elements:[T]) {
self.elements = elements
}
@SteveTrewick
SteveTrewick / duckspeak.swift
Last active February 20, 2017 11:06
Set AVAudioSession category and options to mix with other audio and turn volume down/up as well as interrupt any spoken text.
import UIKit
import AVFoundation
class ViewController: UIViewController, AVSpeechSynthesizerDelegate {
let synth = AVSpeechSynthesizer()
let avsesh = AVAudioSession.sharedInstance()
let voice = AVSpeechSynthesisVoice(language: "en-GB")
let avopts:AVAudioSessionCategoryOptions = [
.MixWithOthers,
.DuckOthers,