Skip to content

Instantly share code, notes, and snippets.

View cemolcay's full-sized avatar
🎼
🎶

Cem Olcay cemolcay

🎼
🎶
View GitHub Profile
@cemolcay
cemolcay / DrumkitMIDITemplate.swift
Last active January 27, 2024 08:58
Drum Kit MIDI Template
//
// KitTemplate.swift
// AutoFills
//
// Created by Cem Olcay on 1/20/24.
//
import Foundation
enum KitTemplate: Int, Codable, CustomStringConvertible, CaseIterable {
@cemolcay
cemolcay / WeightedRandom.swift
Last active November 7, 2023 11:39
Weighted Random Picker Swift
class WeightedRandom<T> {
var elements: [Element]
init(elements: [Element] = []) {
self.elements = elements
}
struct Element {
let item: T
let weight: Double
@cemolcay
cemolcay / Variety.swift
Last active September 10, 2023 11:03
Generate a random value with variety.
func convert<T: FloatingPoint>(value: T, inRange: ClosedRange<T>, toRange: ClosedRange<T>) -> T {
let oldRange = inRange.upperBound - inRange.lowerBound
let newRange = toRange.upperBound - toRange.lowerBound
return (((value - inRange.lowerBound) * newRange) / oldRange) + toRange.lowerBound
}
/// Variety: [0 - 1]. 0 means no randomisation, 1 means a random value between full range.
func variety(value: Double, inRange: ClosedRange<Double>, variety: Double) -> Double {
let normalised = inRange == 0...1 ? value : convert(value: value, inRange: inRange, toRange: 0 ... 1)
let upper = min(normalised + variety, 1.0)
@cemolcay
cemolcay / TuringMachine.swift
Created April 12, 2022 10:32
ShiftBud bit shift algorithm.
//
// TuringMachine.swift
// TuringBud
//
// Created by Cem Olcay on 3/8/22.
//
import Foundation
class TuringMachine {
@cemolcay
cemolcay / 1dArrayWithColRow.swift
Created December 31, 2021 11:50
Get item with row and col from 1d array
extension Collection where Self.Index == Int {
subscript(row: Int, col: Int, colCount: Int) -> Element? {
let index = row * colCount + col
guard index >= 0, index < count else { return nil }
return self[index]
}
}
@cemolcay
cemolcay / LiveKnob-SwiftUI.swift
Created December 16, 2021 21:24
LiveKnob SwiftUI Bridge
//
// LiveKnobSwiftUI.swift
// ControlBud-SwiftUI
//
// Created by Cem Olcay on 11/3/21.
//
import UIKit
import SwiftUI
import LiveKnob
@cemolcay
cemolcay / LiveFader-SwiftUI.swift
Created December 16, 2021 21:24
LiveFader SwiftUI Bridge
//
// LiveFaderSwiftUI.swift
// ControlBud-SwiftUI
//
// Created by Cem Olcay on 11/4/21.
//
import UIKit
import SwiftUI
import LiveFader
@cemolcay
cemolcay / ones.xcplayground
Created October 18, 2021 14:19
creates fractal sequence
import UIKit
import XCPlayground
// 500-1000, 1-100
func seq(length: Int, sampleRate: Int) -> [Int] {
var s = [Int]()
for i in 0..<length {
s.append((i * sampleRate).nonzeroBitCount)
}
return s
@cemolcay
cemolcay / EuclidianSequencer.swift
Created April 17, 2021 17:21
Creates euclidian sequences
func euclid(steps: Int, pulses: Int) -> [Int] {
let pulses = min(pulses, steps)
let ones = pulses
let zeros = steps - pulses
if zeros == 0 {
return [Int](repeating: 1, count: steps)
}
var s: [[Int]] = [Int].init(repeating: 1, count: ones).map({ [$0] }) + [Int](repeating: 0, count: zeros).map({ [$0] })
@cemolcay
cemolcay / DictValidation.swift
Created June 25, 2020 15:57
Validates swift dictionary with rules
import UIKit
var data: [String: Any] = [
"connection": [
"wifi": [
"sx": 1.1,
"tx": 11
],
"cell": [
"rx": 10,