Skip to content

Instantly share code, notes, and snippets.

@jsbain
jsbain / audiounittest.py
Created October 22, 2018 15:45
audiounittest.py
from objc_util import *
from ctypes import *
from coreaudioconstants import *
''' Adapted from https://www.cocoawithlove.com/2010/10/ios-tone-generator-introduction-to.html
'''
AudioUnitRenderActionFlags=c_uint32
OSStatus=c_int32
OSType=c_uint32
@jsbain
jsbain / propertyListener.py
Created November 14, 2014 05:20
propertyListener.py
import threading
class propertyListener(threading.Thread):
'''create a listener on one or more of a view's properties, and dispatches a callback whenever those properties change.
NOTE: it is highly recommended that the propertyListener instance is part of s custom view class, and in that class's will_close method, call stop() on the instance. otherwise, it will continue to poll even after the view is closed, and could cause problems later.
cosntructor args:
view - the view to watch
propertylist - list of properties to watch. must be a list even if only one prop, e.g ['frame']
action the callback to call. the function should be of the following form
@jsbain
jsbain / customMenuItem.py
Created April 4, 2020 07:09
customMenuItem.py
from objc_util import *
from objc_hacks import swizzle # github.com/jsbain/objc_hacks.git
NSNotificationCenter=ObjCClass('NSNotificationCenter')
class PYUIMenuItem(object):
'''Base class representing a uimenuitem, which can be added to the manager.
Each item must have a title string, a selector name, and an action callback.
Must define subclasses with __selector__ field, __title__ string
Action callback must be of form
@jsbain
jsbain / gistcheck.py
Last active November 24, 2022 00:18 — forked from davenicholls/gistcheck.py
updated comment: prevent opening of pyui in editor
# Source: https://gist.github.com/5212628
#
# All-purpose gist tool for Pythonista.
#
# When run directly, this script sets up four other scripts that call various
# functions within this file. Each of these sub-scripts are meant for use as
# action menu items. They are:
#
# Set Gist ID.py - Set the gist id that the current file should be
# associated with.
@jsbain
jsbain / detector.py
Created June 11, 2020 08:36
detector.py
# coding: utf-8
# based on Cethric's image capture gist....
FRAME_PROC_INTERVAL=15 #num frames to skip. 1=go as fast as possible, 5=every fifth frame
import ui
from objc_util import *
import ctypes
from objc_util import autoreleasepool
AVCaptureDevice = ObjCClass('AVCaptureDevice')
AVCaptureDeviceInput = ObjCClass('AVCaptureDeviceInput')
AVCaptureVideoDataOutput = ObjCClass('AVCaptureVideoDataOutput')
@jsbain
jsbain / audiounit_lib.py
Created October 28, 2018 05:21
audiounittest3.py
from ctypes import *
from objc_util import c
AudioUnitRenderActionFlags=c_uint32
OSStatus=c_int32
OSType=c_uint32
class SMPTETimeType(c_uint32):
kSMPTETimeType24 = 0
kSMPTETimeType25 = 1
kSMPTETimeType30Drop = 2
kSMPTETimeType30 = 3
@jsbain
jsbain / wavexample.py
Last active June 7, 2021 01:22
wavpiano
import numpy as N
import wave, sound, os, ui
def get_signal_data(frequency=440, duration=1, volume=32767, samplerate=44100):
"""Outputs a numpy array of intensities"""
samples = duration * samplerate
period = samplerate / float(frequency)
omega = N.pi * 2 / period
t = N.arange(samples, dtype=N.float)
@jsbain
jsbain / Map View Demo2.py
Created May 10, 2018 07:41
Map View Demo2.py
# coding: utf-8
'''
NOTE: This requires Pythonista 3
Demo of a custom ui.View subclass that embeds a native map view using MapKit (via objc_util). Tap and hold the map to drop a pin.
The MapView class is designed to be reusable, but it doesn't implement *everything* you might need. I hope that the existing methods give you a basic idea of how to add new capabilities though. For reference, here's Apple's documentation about the underlying MKMapView class: http://developer.apple.com/library/ios/documentation/MapKit/reference/MKMapView_Class/index.html
adapted from original code by omz
@jsbain
jsbain / turtle2.py
Created August 6, 2018 06:19
turtle2.py
#\input texinfo
# coding: utf-8
# ui-based iOS port of the turtle module (not 100% compatible with standard library
# turtle module, but most things beginners would use should work)
import ui
from math import *
import math
import time
@jsbain
jsbain / mapscene.py
Last active August 18, 2020 18:56
mapscene.py
from scene import *
import sound
import random
import math
A = Action
mapsprites=['plc:Brown_Block','plc:Dirt_Block','plc:Grass_Block','plc:Plain_Block',]
def choose_random_node():
tx=random.choice(mapsprites)
sn=SpriteNode(tx)