Skip to content

Instantly share code, notes, and snippets.

View cemolcay's full-sized avatar
🎼
🎶

Cem Olcay cemolcay

🎼
🎶
View GitHub Profile
@cemolcay
cemolcay / TitleColorLabel.swift
Created June 17, 2024 11:42
Change text color based on the background
class TitleColorLabel: UILabel {
var lightTitleColor: UIColor = .white { didSet { updateTitleColor() }}
var darkTitleColor: UIColor = .black { didSet { updateTitleColor() }}
override var backgroundColor: UIColor? {
didSet {
updateTitleColor()
}
}
@cemolcay
cemolcay / localnotif.swift
Created May 26, 2024 14:43
swift local notification
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if granted {
print("Permission granted")
let content = UNMutableNotificationContent()
content.title = "My title"
content.body = "Lots of text"
content.sound = UNNotificationSound.default
@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