Skip to content

Instantly share code, notes, and snippets.

@kazuki
Created November 25, 2017 01:39
Show Gist options
  • Save kazuki/7c7b50ba9df082bfe9f03819201648eb to your computer and use it in GitHub Desktop.
Save kazuki/7c7b50ba9df082bfe9f03819201648eb to your computer and use it in GitHub Desktop.
Python Bindings for RNNoise(https://github.com/xiph/rnnoise/)
from ctypes import byref, c_float, c_void_p, CDLL
class RNNoise(object):
def __init__(self):
self._native = CDLL('./librnnoise.so')
self._native.rnnoise_process_frame.restype = c_float
self._native.rnnoise_process_frame.argtypes = (
c_void_p, c_void_p, c_void_p)
self._native.rnnoise_create.restype = c_void_p
self._handle = self._native.rnnoise_create()
self._buf = (c_float * self.frame_size)()
@property
def frame_size(self):
return 480
def process_frame(self, samples):
if len(samples) > self.frame_size:
raise ValueError
for i in range(len(samples)):
self._buf[i] = samples[i]
for i in range(len(samples), self.frame_size):
self._buf[i] = 0
vad_prob = self._native.rnnoise_process_frame(
self._handle, byref(self._buf), byref(self._buf))
for i in range(len(samples)):
samples[i] = self._buf[i]
return vad_prob
def __del__(self):
if self._handle:
self._native.rnnoise_destroy(self._handle)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment