Skip to content

Instantly share code, notes, and snippets.

View msfrms's full-sized avatar
🏠
Working from home

Mikhail Radaev msfrms

🏠
Working from home
View GitHub Profile
import notify
import Combine
enum Notify {}
extension Notify {
struct Status: Error {
let rawValue: UInt32
init(_ rawValue: UInt32) {
self.rawValue = rawValue
@tsuzukihashi
tsuzukihashi / DataExtension.swift
Last active July 7, 2023 09:42
Data → CMBlockBuffer → CMSampleBuffer
import AVKit
import AVFoundation
extension Data {
func toCMBlockBuffer() throws -> CMBlockBuffer {
var blockBuffer: CMBlockBuffer?
let data: NSMutableData = .init(data: self)
var source: CMBlockBufferCustomBlockSource = .init()
source.refCon = Unmanaged.passRetained(data).toOpaque()
@msfrms
msfrms / scalac-compiler-flags-2.13.sbt
Created December 14, 2020 02:09 — forked from tabdulradi/scalac-compiler-flags-2.13.sbt
Scala Compiler flags for 2.13. Based on Tpolecat's 2.12 version
scalacOptions ++= Seq(
"-deprecation", // Emit warning and location for usages of deprecated APIs.
"-encoding", "utf-8", // Specify character encoding used by source files.
"-explaintypes", // Explain type errors in more detail.
"-feature", // Emit warning and location for usages of features that should be imported explicitly.
"-language:existentials", // Existential types (besides wildcard types) can be written and inferred
// "-language:experimental.macros", // Allow macro definition (besides implementation and application). Disabled, as this will significantly change in Scala 3
"-language:higherKinds", // Allow higher-kinded types
// "-language:implicitConversions", // Allow definition of implicit functions called views. Disabled, as it might be dropped in Scala 3. Instead use extension methods (implemented as implicit class Wrapper(va
@T1T4N
T1T4N / CVPixelBuffer+Data.swift
Last active October 2, 2025 10:06
A format-agnostic way of converting CVPixelBuffer to Data and back
extension CVPixelBuffer {
public static func from(_ data: Data, width: Int, height: Int, pixelFormat: OSType) -> CVPixelBuffer {
data.withUnsafeBytes { buffer in
var pixelBuffer: CVPixelBuffer!
let result = CVPixelBufferCreate(kCFAllocatorDefault, width, height, pixelFormat, nil, &pixelBuffer)
guard result == kCVReturnSuccess else { fatalError() }
CVPixelBufferLockBaseAddress(pixelBuffer, [])
defer { CVPixelBufferUnlockBaseAddress(pixelBuffer, []) }
func asynchronouslyLoadURLAssets(_ newAsset: AVURLAsset) {
DispatchQueue.main.async {
newAsset.loadValuesAsynchronously(forKeys: self.assetKeysRequiredToPlay) {
for key in self.assetKeysRequiredToPlay {
var error: NSError?
if newAsset.statusOfValue(forKey: key, error: &error) == .failed {
self.delegate?.playerDidFailToPlay(message: "Can't use this AVAsset because one of it's keys failed to load")
return
}
}
@shalyf
shalyf / audio_format.swift
Last active May 10, 2024 20:28
format CMSampleBuffer from/to AudioBufferList/Data
func data(from sampleBuffer: CMSampleBuffer) -> Data {
var abl = AudioBufferList()
var blockBuffer: CMBlockBuffer?
CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer,
bufferListSizeNeededOut: nil,
bufferListOut: &abl,
bufferListSize: MemoryLayout<AudioBufferList>.size,
blockBufferAllocator: nil,
blockBufferMemoryAllocator: nil,
@chriszielinski
chriszielinski / Image+Trim.swift
Last active May 15, 2025 13:40
[Swift 5] NSImage/UIImage Crop/Trim Transparency
// Image+Trim.swift
//
// Copyright © 2020 Christopher Zielinski.
// https://gist.github.com/chriszielinski/aec9a2f2ba54745dc715dd55f5718177
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
@guilgaly
guilgaly / 1 - scalac options 2.13.scala
Last active June 2, 2024 19:50
Scala compiler options I typically use
val scalacOptions = Seq(
"-encoding",
"utf-8", // Specify character encoding used by source files.
"-Ybackend-parallelism", //
"8",
"-explaintypes", // Explain type errors in more detail.
"-feature", // Emit warning and location for usages of features that should be imported explicitly.
"-unchecked", // Enable additional warnings where generated code depends on assumptions.
"-Xcheckinit", // Wrap field accessors to throw an exception on uninitialized access.
// "-Ymacro-annotations", // Enable support for macro annotations, formerly in macro paradise.
//
// Example of Using AVAudioPlayer
// to play a buffer of (synthesized) audio samples from memory
// by converting a [Float] buffer into an in-memory WAV file
//
// Copyright © 2019 Ronald H Nicholson Jr. All rights reserved.
// (re)Distribution permitted under the 3-clause New BSD license.
//
import Foundation
@shaps80
shaps80 / AVPlayer+Scrubbing.swift
Last active August 20, 2025 19:08
Enables smooth frame-by-frame scrubbing (in both directions) – similar to Apple's applications.
public enum Direction {
case forward
case backward
}
internal var player: AVPlayer?
private var isSeekInProgress = false
private var chaseTime = kCMTimeZero
private var preferredFrameRate: Float = 23.98