Skip to content

Instantly share code, notes, and snippets.

@devilholk
devilholk / sound_test.py
Created May 23, 2017 12:17
Testing pulse audio using ctypes
#Run this with python3 interactively like
#python3 -i sound_test.py
#In the python interactive console you can play with
#env, wave and rb to adjust envelope, waveform and reverb settings
import ctypes, sys, math, threading
PA = ctypes.CDLL('libpulse-simple.so')
@devilholk
devilholk / test1.py
Created May 27, 2017 15:39
Outline for graphic quick replacement tool
from PyQt5.QtGui import QTextOption, QFont
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QTableView, QTableWidget, QTableWidgetItem, QMainWindow, QGridLayout, QWidget, QPlainTextEdit
import re
monospace = QFont('DejaVu Sans Mono')
monospace.setStyleHint(QFont.Monospace)
print(monospace)
@devilholk
devilholk / freecam.py
Created May 27, 2017 16:25
Simple free fly camera (libglfw, libGL, python3)
from lowlevel_gl import *
# Test
#Kom ihåg error check för push/pop matrix
import threading
assert glfwInit() == 1
w = glfwCreateWindow(640, 480, b"Hello World!", None, None)
assert w, 'Failed to create window'
@devilholk
devilholk / play_with_ft.py
Created September 2, 2017 14:30
Using python and numpy to filter a pulse audio stream
import numpy, subprocess, struct, sys
#pacmd load-module module-null-sink sink_name=Python_input
#pacmd update-sink-proplist Python_input device.description="Python sound processing input"
#Note - seems we can't have spaces in description so replace with _ or something for now
# - it might be a bash thing so it might work from subprocess.call, have not tested
#figure out what monitor-stream this is and put it down there where we pamon
#also remember to redirect the source application to this sink (for instance pavucontrol)
Processed events 34.68 K @ 69.26 K/s.
Processed events 52.52 K @ 31.35 K/s.
Processed events 64.64 K @ 23.5 K/s.
Processed events 83.74 K @ 38.16 K/s.
Deflated processed 7.0 MiB @ 3.01 MiB/s.
Inflated processed 30.04 MiB @ 12.9 MiB/s.
Processed events 107.22 K @ 43.06 K/s.
Processed events 151.13 K @ 87.78 K/s.
Processed events 176.55 K @ 50.83 K/s.
Processed events 202.01 K @ 50.93 K/s.
@devilholk
devilholk / bz2xml_processing.py
Last active June 9, 2018 13:48
Incremental processing of bz2-compressed XML files
import bz2, time, functools, xml.etree.ElementTree
#serial reading of XML inspired by http://boscoh.com/programming/reading-xml-serially.html
#note that you can do elem.clear for every element
def format_amount(amount, precision=2, unit='B', base=1024, unit_prefix='i'):
if amount < base ** 1:
return f'{amount}{unit}'
elif amount < base ** 2:
return f'{round(amount/(base**1), precision)} K{unit_prefix}{unit}'
@devilholk
devilholk / slotted_pole_slider.scad
Created November 26, 2018 02:08
For connecting a flat piece to a slider that slides along a pole
/*
___ _ _ _ _ ___ _ ___ _ _ _
/ __| |___| |_| |_ ___ __| | | _ \___| |___ / __| (_)__| |___ _ _
\__ \ / _ \ _| _/ -_) _` | | _/ _ \ / -_) \__ \ | / _` / -_) '_|
|___/_\___/\__|\__\___\__,_| |_| \___/_\___| |___/_|_\__,_\___|_|
by: Mikael Lövqvist @ LED Alert Hackerspace
.-----. outer diameter: 2 × pole_shroud_thickness + pole_diameter
@devilholk
devilholk / test_our_png_writer.py
Created June 1, 2019 08:29
Testing custom PNG writer for PIL that allows non 256 size palettes
import PIL.Image, our_png_save
im = PIL.Image.new('RGB', (256, 256))
p = im.load()
for y in range(256):
for x in range(256):
p[x,y] = (x, y, (x * y) >> 8)
colors = set()
for y in range(256):
@devilholk
devilholk / our_png_save.py
Created June 1, 2019 08:33
Our modified PNG saver that allows custom size palette
import PIL.PngImagePlugin
#Import all the things unless they start with two underscores. "from stuff import *" will not import things beginning with one underscore
#And we need things like _MAGIC etc
locals().update({k: v for k, v in PIL.PngImagePlugin.__dict__.items() if not k.startswith('__')})
#This function is taken from PIL.PngImagePlugin with a small change that is noted below
#In the bottom we register this modified function to be the PNG save function -devilholk
def overidden_save(im, fp, filename, chunk=putchunk):
# save an image to disk (called by the save method)
@devilholk
devilholk / recursion_limiter.py
Last active July 3, 2019 22:33
Limit recursion with a context handler
import sys, collections
class max_stack_level:
active = collections.Counter()
def __init__(self, max):
self.max = max
def __enter__(self):
frame = sys._getframe(1)