Skip to content

Instantly share code, notes, and snippets.

View hollance's full-sized avatar

Matthijs Hollemans hollance

View GitHub Profile
@hollance
hollance / eliza.swift
Last active February 17, 2023 15:50
The classic ELIZA chat bot in Swift.
/*
Joseph Weizenbaum's classic ELIZA chat bot in Swift.
Based on the IBM PC BASIC program from CREATIVE COMPUTING by Patricia
Danielson and Paul Hashfield, and the Java adaptation by Jesper Juul.
Run this script from Terminal:
$ swift eliza.swift
Press Ctrl-C or Ctrl-D to quit. (Or type "shut up".)
@hollance
hollance / XOR.swift
Created January 11, 2017 18:05
Playing with BNNS (Swift version). The "hello world" of neural networks.
/*
The "hello world" of neural networks: a simple 3-layer feed-forward
network that implements an XOR logic gate.
The first layer is the input layer. It has two neurons a and b, which
are the two inputs to the XOR gate.
The middle layer is the hidden layer. This has two neurons h1, h2 that
will learn what it means to be an XOR gate.
@hollance
hollance / ProtectYourEars.h
Created June 26, 2022 18:45
For testing audio code with headphones on...
#pragma once
#include <JuceHeader.h>
/**
Silences the buffer if bad or loud values are detected in the output buffer.
Use this during debugging to avoid blowing out your eardrums on headphones.
If the output value is out of the range [-1, +1] it will be hard clipped.
*/
inline void protectYourEars(float *buffer, int sampleCount)
@hollance
hollance / synth.c
Created May 21, 2022 22:19
Render basic waveform using Core Audio on macOS
// Compile this with:
// $ gcc synth.c -o synth -framework AudioToolbox
// Based on the CH07_AUGraphSineWave example from the book
// "Learning Core Audio: A Hands-On Guide to Audio Programming
// for Mac and iOS" by Chris Adamson and Kevin Avila
#include <AudioToolbox/AudioToolbox.h>
typedef struct
@hollance
hollance / BiQuadFilter.h
Created December 8, 2021 19:56
bi-quad filter in C++
inline float denormalsToZero(float x) {
return (std::abs(x) < 1e-15f) ? 0.0f : x;
}
/**
mystran's Bi-quadratic filter
https://www.kvraudio.com/forum/viewtopic.php?p=4836443#p4836443
This is not a direct form topology! Essentially it is modified coupled form
@hollance
hollance / Compressing-MobileNet.ipynb
Created September 26, 2017 12:47
Jupyter notebook for compressing MobileNet (work in progress)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@hollance
hollance / ExampleViewController.m
Last active June 3, 2021 07:39
You cannot easily cancel Objective-C blocks that are running on a background queue. One possible solution is to let the block capture an object -- the so-called "cancel ticket" -- that determines whether it should stop or not. Whoever holds the ticket can call [ticket cancel] to stop the block. Of course, this does require the block to periodica…
// Shows how to use the cancel ticket to abort the async block.
#import "ExampleViewController.h"
#import "MHCancelTicket.h"
@implementation ExampleViewController
{
MHCancelTicket *_cancelTicket;
}
@hollance
hollance / deconv.py
Created May 13, 2017 20:47
Playing with "deconvolution"
import numpy as np
i = np.array(list(range(1, 50)), dtype=np.float).reshape((7, 7))
k = np.array(list(range(1, 10)), dtype=np.float).reshape((3, 3))
print("Input:"); print(i)
print("Kernel:"); print(k)
# Forward convolution. We need to pad the input so that we can read from
# its borders. (Not doing stride etc here.)
@hollance
hollance / YourMainDataModel.m
Created August 24, 2013 13:39
Saving an NSMutableArray to a plist file using NSKeyedArchiver and NSCoding.
// Gets the path to the app's Documents folder
- (NSString *)documentsDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return paths[0];
}
// Gets the path to the data file
- (NSString *)dataFilePath
{
@hollance
hollance / workaround.markdown
Created October 9, 2020 09:59
Workaround for Xcode 12 problems with Core ML models

I ran into a problem with Xcode 12's Core ML compiler, and several people have emailed me about what appears to be the same issue.

The workaround is to use Xcode 11 to compile the Core ML model. This means you still need to have Xcode 11 installed (which is a good idea anyway).

What worked for me is the following...

From the Terminal, switch to the Xcode 11.7 installation (your path may be different):

sudo xcode-select -s /Applications/Xcode11.7.app/Contents/Developer/