Skip to content

Instantly share code, notes, and snippets.

View cemolcay's full-sized avatar
🎼
🎶

Cem Olcay cemolcay

🎼
🎶
View GitHub Profile
@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 / 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 / DrumkitMIDITemplate.swift
Last active February 28, 2026 07:39
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 / 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 / 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 / removeFilesWithNamesInAFolder
Created August 22, 2024 16:18
Remove files from a folder with a name reference file
#!/bin/bash
# Usage: ./remove_files.sh /path/to/folder /path/to/input_file.txt
# Get the directory and input file from the arguments
TARGET_DIR="$1"
INPUT_FILE="$2"
# Check if the directory exists
if [ ! -d "$TARGET_DIR" ]; then