Skip to content

Commit

Permalink
Organize imports; Move types and structures to separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewryanscott committed Nov 9, 2016
1 parent fc17dee commit be2bffa
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 102 deletions.
12 changes: 8 additions & 4 deletions sunvox/api.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import sunvox.dll
import sunvox.process
import sunvox.slot
import sunvox.types

from sunvox.dll import * # NOQA
from sunvox.process import * # NOQA
from sunvox.slot import * # NOQA
from sunvox.types import * # NOQA

__all__ = sunvox.dll.__all__ + sunvox.slot.__all__ + sunvox.process.__all__

for __name in __all__:
setattr(sunvox, __name, locals()[__name])
__all__ = (
sunvox.dll.__all__ +
sunvox.slot.__all__ +
sunvox.process.__all__ +
sunvox.types.__all__
)
4 changes: 2 additions & 2 deletions sunvox/buffered/process.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy
from numpy import float32, int16, zeros
from sunvox import SV_INIT_FLAG, Process
from sunvox.api import SV_INIT_FLAG, Process

from .processor import BufferedProcessor

Expand Down Expand Up @@ -41,7 +41,7 @@ def _recv(self):
try:
return self._conn.recv()
finally:
self._lock.release()
self._lock.release()

@property
def samples(self):
Expand Down
11 changes: 6 additions & 5 deletions sunvox/buffered/processor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ctypes

import sunvox
import sunvox.dll
import sunvox.types
from sunvox.processor import Processor


Expand All @@ -12,9 +13,9 @@ def __init__(self, conn):
def init(self, device, freq, channels, flags):
super().init(device, freq, channels, flags)
self.channels = channels
if flags & sunvox.SV_INIT_FLAG.AUDIO_INT16:
if flags & sunvox.types.SV_INIT_FLAG.AUDIO_INT16:
self.type_code = 'h'
elif flags & sunvox.SV_INIT_FLAG.AUDIO_FLOAT32:
elif flags & sunvox.types.SV_INIT_FLAG.AUDIO_FLOAT32:
self.type_code = 'f'
self.type_size = {'h': 2, 'f': 4}[self.type_code]

Expand All @@ -24,7 +25,7 @@ def init_buffer(self, size):
self._buffer = ctypes.create_string_buffer(self._buffer_bytes)

def fill_buffer(self):
sunvox.audio_callback(
sunvox.dll.audio_callback(
ctypes.byref(self._buffer), self._buffer_size, 0,
sunvox.get_ticks())
sunvox.dll.get_ticks())
return self._buffer.raw
85 changes: 3 additions & 82 deletions sunvox/dll.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

import os
import sys
from ctypes import (POINTER, Structure, c_char_p, c_int, c_short, c_ubyte,
c_uint, c_ushort, c_void_p)
from ctypes import POINTER, c_char_p, c_int, c_short, c_uint, c_void_p
from ctypes.util import find_library
from enum import IntEnum
from textwrap import dedent

from sunvox.types import sunvox_note


DEFAULT_DLL_BASE = os.path.abspath(
os.path.join(os.path.dirname(__file__), 'lib')
Expand Down Expand Up @@ -61,80 +61,6 @@
_s = loader.LoadLibrary(_sunvox_lib_path)


class NOTECMD(IntEnum):
(C0, d0, D0, e0, E0, F0, g0, G0, a0, A0, b0, B0,
C1, d1, D1, e1, E1, F1, g1, G1, a1, A1, b1, B1,
C2, d2, D2, e2, E2, F2, g2, G2, a2, A2, b2, B2,
C3, d3, D3, e3, E3, F3, g3, G3, a3, A3, b3, B3,
C4, d4, D4, e4, E4, F4, g4, G4, a4, A4, b4, B4,
C5, d5, D5, e5, E5, F5, g5, G5, a5, A5, b5, B5,
C6, d6, D6, e6, E6, F6, g6, G6, a6, A6, b6, B6,
C7, d7, D7, e7, E7, F7, g7, G7, a7, A7, b7, B7,
C8, d8, D8, e8, E8, F8, g8, G8, a8, A8, b8, B8,
C9, d9, D9, e9, E9, F9, g9, G9, a9, A9, b9, B9) = range(1, 121)
EMPTY = 0
NOTE_OFF = 128
ALL_NOTES_OFF = 129 # notes of all synths off
CLEAN_SYNTHS = 130 # stop and clean all synths
STOP = 131
PLAY = 132
SET_PITCH = 133
PREV_TRACK = 134


class SV_INIT_FLAG(IntEnum):

NO_DEBUG_OUTPUT = 1 << 0

# Interaction with sound card is on the user side
USER_AUDIO_CALLBACK = 1 << 1

AUDIO_INT16 = 1 << 2

AUDIO_FLOAT32 = 1 << 3

# Audio callback and song modification functions are in single thread
ONE_THREAD = 1 << 4


class SV_MODULE(IntEnum):
FLAG_EXISTS = 1
FLAG_EFFECT = 2
INPUTS_OFF = 16
INPUTS_MASK = 255 << INPUTS_OFF
OUTPUTS_OFF = 16 + 8
OUTPUTS_MASK = 255 << OUTPUTS_OFF


class SV_STYPE(IntEnum):
INT16 = 0
INT32 = 1
FLOAT32 = 2
FLOAT64 = 3


class sunvox_note(Structure):
_fields_ = [
# 0 - nothing; 1..127 - note num; 128 - note off; 129, 130...
# - see NOTECMD_xxx defines
('note', c_ubyte),

# Velocity 1..129; 0 - default
('vel', c_ubyte),

# 0 - nothing; 1..255 - module number (real module number + 1)
('module', c_ubyte),

('nothing', c_ubyte),

# CCEE. CC - number of a controller (1..255). EE - std effect
('ctl', c_ushort),

# XXYY. Value of controller/effect
('ctl_val', c_ushort),
]


def decorated_fn(fn, argtypes, restype, needs_lock, doc):
fn.argtypes = argtypes
fn.restype = restype
Expand Down Expand Up @@ -578,11 +504,6 @@ def decorated_fn(fn, argtypes, restype, needs_lock, doc):
'DEFAULT_DLL_BASE',
'DLL_BASE',
'DLL_PATH',
'NOTECMD',
'SV_INIT_FLAG',
'SV_MODULE',
'SV_STYPE',
'sunvox_note',
'audio_callback',
'open_slot',
'close_slot',
Expand Down
5 changes: 3 additions & 2 deletions sunvox/process.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import multiprocessing as mp
from threading import Lock

import sunvox
import sunvox.dll
from .processor import Processor


Expand Down Expand Up @@ -32,7 +32,8 @@ def __init__(self, *args, **kwargs):
self._process.start()

_k, _v = None, None
for _k, _v in sunvox.__dict__.items():
for _k in sunvox.dll.__all__:
_v = getattr(sunvox.dll, _k)
if hasattr(_v, 'sunvox_dll_fn'):
locals()[_k] = passthrough(_k)
del _k, _v
Expand Down
7 changes: 4 additions & 3 deletions sunvox/processor.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import sunvox
import sunvox.dll


def passthrough(name):
def fn(self, *args, **kw):
return getattr(sunvox, name)(*args, **kw)
return getattr(sunvox.dll, name)(*args, **kw)
fn.__name__ = name
return fn

Expand All @@ -22,7 +22,8 @@ def _process_commands(self):
self.conn.send(fn(*args, **kwargs))

_k, _v = None, None
for _k, _v in sunvox.__dict__.items():
for _k in sunvox.dll.__all__:
_v = getattr(sunvox.dll, _k)
if hasattr(_v, 'sunvox_dll_fn'):
locals()[_k] = passthrough(_k)
del _k, _v
Expand Down
85 changes: 85 additions & 0 deletions sunvox/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from ctypes import Structure, c_ubyte, c_ushort
from enum import IntEnum


class NOTECMD(IntEnum):
(C0, d0, D0, e0, E0, F0, g0, G0, a0, A0, b0, B0,
C1, d1, D1, e1, E1, F1, g1, G1, a1, A1, b1, B1,
C2, d2, D2, e2, E2, F2, g2, G2, a2, A2, b2, B2,
C3, d3, D3, e3, E3, F3, g3, G3, a3, A3, b3, B3,
C4, d4, D4, e4, E4, F4, g4, G4, a4, A4, b4, B4,
C5, d5, D5, e5, E5, F5, g5, G5, a5, A5, b5, B5,
C6, d6, D6, e6, E6, F6, g6, G6, a6, A6, b6, B6,
C7, d7, D7, e7, E7, F7, g7, G7, a7, A7, b7, B7,
C8, d8, D8, e8, E8, F8, g8, G8, a8, A8, b8, B8,
C9, d9, D9, e9, E9, F9, g9, G9, a9, A9, b9, B9) = range(1, 121)
EMPTY = 0
NOTE_OFF = 128
ALL_NOTES_OFF = 129 # notes of all synths off
CLEAN_SYNTHS = 130 # stop and clean all synths
STOP = 131
PLAY = 132
SET_PITCH = 133
PREV_TRACK = 134


class SV_INIT_FLAG(IntEnum):

NO_DEBUG_OUTPUT = 1 << 0

# Interaction with sound card is on the user side
USER_AUDIO_CALLBACK = 1 << 1

AUDIO_INT16 = 1 << 2

AUDIO_FLOAT32 = 1 << 3

# Audio callback and song modification functions are in single thread
ONE_THREAD = 1 << 4


class SV_MODULE(IntEnum):
FLAG_EXISTS = 1
FLAG_EFFECT = 2
INPUTS_OFF = 16
INPUTS_MASK = 255 << INPUTS_OFF
OUTPUTS_OFF = 16 + 8
OUTPUTS_MASK = 255 << OUTPUTS_OFF


class SV_STYPE(IntEnum):
INT16 = 0
INT32 = 1
FLOAT32 = 2
FLOAT64 = 3


class sunvox_note(Structure):
_fields_ = [
# 0 - nothing; 1..127 - note num; 128 - note off; 129, 130...
# - see NOTECMD_xxx defines
('note', c_ubyte),

# Velocity 1..129; 0 - default
('vel', c_ubyte),

# 0 - nothing; 1..255 - module number (real module number + 1)
('module', c_ubyte),

('nothing', c_ubyte),

# CCEE. CC - number of a controller (1..255). EE - std effect
('ctl', c_ushort),

# XXYY. Value of controller/effect
('ctl_val', c_ushort),
]


__all__ = [
'NOTECMD',
'SV_INIT_FLAG',
'SV_MODULE',
'SV_STYPE',
'sunvox_note',
]
8 changes: 4 additions & 4 deletions tests/test_init_deinit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

def test_init_deinit():
flags = (
sunvox.SV_INIT_FLAG.ONE_THREAD |
sunvox.SV_INIT_FLAG.USER_AUDIO_CALLBACK
sunvox.api.SV_INIT_FLAG.ONE_THREAD |
sunvox.api.SV_INIT_FLAG.USER_AUDIO_CALLBACK
)
sunvox.init(None, 44100, 2, flags)
sunvox.deinit()
sunvox.api.init(None, 44100, 2, flags)
sunvox.api.deinit()

0 comments on commit be2bffa

Please sign in to comment.