Skip to content

Instantly share code, notes, and snippets.

View jonashaag's full-sized avatar

Jonas Haag jonashaag

View GitHub Profile
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
def bin_freqs(x, nbins):
return (
x.T.reshape((-1, int(x.shape[0] / nbins))).mean(axis=-1).reshape((-1, nbins)).T
)
def compact_freqs(x):
return np.vstack(
(
x[:16],
@jonashaag
jonashaag / cirm.py
Last active November 18, 2021 02:09
Python cIRM (Complex Ideal Ratio Mask), ORM (Optimal Ratio Mask), IRM (Ideal Ratio Mask), PSM (Phase Sensitive Mask)
def cirm(y, s, K=10, C=0.1, flat=True):
y = librosa.core.stft(y.astype('float64'), 256, 64).astype('complex128')
s = librosa.core.stft(s.astype('float64'), 256, 64).astype('complex128')
mr = (np.real(y) * np.real(s) + np.imag(y) * np.imag(s))/(np.real(y)**2 + np.imag(y)**2)
mi = (np.real(y) * np.imag(s) - np.imag(y) * np.real(s))/(np.real(y)**2 + np.imag(y)**2)
m = mr + 1j * mi
if flat:
return m
else:
return K * ((1 - np.exp(-C * m))/(1 + np.exp(-C * m)))
@jonashaag
jonashaag / gist:7ca5b19abf537a447d0de1b923043f45
Last active March 28, 2024 12:20
T60/RT60, C50/C80, D in Python
import librosa
from scipy.signal import butter, lfilter
SR = 8000
NFFT = 256 # change ~proportionally to SR
def butter_bandpass(lowcut, highcut, fs, order=5):
nyq = 0.5 * fs
low = lowcut / nyq
import requests
import os
url = 'http://' + os.environ['COLAB_TPU_ADDR'].split(':')[0] + ':8475/requestversion/2.2.0-dev20200311'
resp = requests.post(url)
print(resp)
%pip install tf-nightly==2.2.0-dev20200311
if __name__ == '__main__':
import dask.distributed
client = dask.distributed.Client()
queue = dask.distributed.Queue('queue1')
def f():
return 42
class PickleableCtypesFuncPtr:
"""A version of a ctypes._FuncPtr that can be pickled. The shared library
must be available in the depickling environment.
"""
def __init__(self, dll_type, lib, name):
self.__setstate__((dll_type, lib, name))
def __setstate__(self, state):
self.state = state
dll_type, lib, func = self.state
@jonashaag
jonashaag / lockgil.py
Last active May 5, 2020 15:44
Pure Python solution to locking the GIL
# Use libc in ctypes "PyDLL" mode, which prevents CPython from
# releasing the GIL during procedure calls.
_libc_name = ctypes.util.find_library("c")
if _libc_name is None:
raise RuntimeError("Cannot find libc")
libc_py = ctypes.PyDLL(_libc_name)
...
libc_py.usleep(...)
<!doctype html>
<script type="application/javascript">
const s = `
application/mp4
application/mp4; codecs=bogus
application/octet-stream
application/octet-stream; codecs='avc1.42E01E, mp4a.40.2'
application/octet-stream; codecs='mp4a.40.2'
application/octet-stream; codecs='theora, vorbis'
application/octet-stream; codecs='vorbis'