Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
larme committed Aug 31, 2020
0 parents commit 5ed4e2a
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 0 deletions.
138 changes: 138 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/
Empty file added fakebox/__init__.py
Empty file.
2 changes: 2 additions & 0 deletions fakebox/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DEFAULT_SAMPLE_RATE = 44100
DEFAULT_BIT_N = 16
61 changes: 61 additions & 0 deletions fakebox/dsp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import abc

import numpy as np

from fakebox.conf import DEFAULT_SAMPLE_RATE, DEFAULT_BIT_N

DSPFloat = np.float64
DSPZero = DSPFloat(0.0)
DSPOne_L = DSPFloat(1.0 - 1e-8)

class DSPContext(object):

def __init__(self, sample_rate=DEFAULT_SAMPLE_RATE, bit_n=DEFAULT_BIT_N):
self.sample_rate = sample_rate
self.bit_n = bit_n
self.sample_step = 0

def tick(self):
self.sample_step += 1


ctx = DSPContext()


class DSPObj(abc.ABC):

def __init__(self):

try:
self.in_n + self.out_n
except AttributeError:
raise NotImplementedError

self._ctx = ctx
self.counter = 0
self.in_buffer = np.zeros(self.in_n, dtype=DSPFloat)
self.out_buffer = np.zeros(self.out_n, dtype=DSPFloat)

def reset(self):
self.counter = 0
self.in_buffer = np.zeros(self.in_n, dtype=DSPFloat)
self.out_buffer = np.zeros(self.out_n, dtype=DSPFloat)

@abc.abstractmethod
def _tick(self, ins):
raise NotImplementedError

def tick(self):
outs = self._tick(self.in_buffer)
self.out_buffer[:] = outs
self.in_buffer[:] = DSPZero
self.counter += 1

@property
def sample_rate(self):
return self._ctx.sample_rate

@property
def bit_n(self):
return self._ctx.bit_n

27 changes: 27 additions & 0 deletions fakebox/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import numpy as np
from scipy.io.wavfile import write

import fakebox.dsp as dsp

def test(dsp_ctx=dsp.ctx):
return dsp.ctx.sample_rate

def init_waveform(duration=None, sample_n=None, ctx=dsp.ctx):

assert(duration or sample_n)

sample_rate = ctx.sample_rate

if duration:
sample_n = int(round(duration * sample_rate))

return np.zeros(sample_n, dtype=dsp.DSPFloat)


def save_waveform(waveform, path, quiet_factor=0.5, ctx=dsp.ctx):

bit_n = ctx.bit_n
sample_rate = ctx.sample_rate
waveform_quiet = waveform * quiet_factor
waveform_integers = np.int16(waveform_quiet * (2 ** (bit_n - 1) - 1))
write(path, sample_rate, waveform_integers)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scipy>=1.0
15 changes: 15 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from setuptools import setup

setup(name='fakebox',
version='0.1',
description='toy dsp library in python',
url='http://github.com/larme/fakebox',
author='Zhao Shenyang',
author_email='dev@zsy.im',
license='MIT',
packages=['fakebox'],
install_requires=[
'numpy>=1.0',
'scipy>=1.0',
],
zip_safe=False)

0 comments on commit 5ed4e2a

Please sign in to comment.