~hecanjog/pippi

e7270f1c4f2e33aac452dab8fa16a1ccf9706dfc — Erik Schoster 6 days ago 02784bf
Add SoundBuffer.wt() wavetable factory
5 files changed, 54 insertions(+), 13 deletions(-)

M REFACTOR
M libpippi/src/pippicore.c
M pippi/buffers.pxd
M pippi/buffers.pyx
M tests/test_newbuffer.py
M REFACTOR => REFACTOR +2 -2
@@ 56,6 56,8 @@ Just some refactoring notes...
- [x] speed
- [x] vspeed
- [x] taper
- [ ] toenv
- [ ] towavetable
- [x] trim
- [x] write



@@ 71,6 73,4 @@ Just some refactoring notes...
- [ ] cloud -- graincloud
- [ ] stretch
- [ ] transpose
- [ ] toenv
- [ ] towavetable


M libpippi/src/pippicore.c => libpippi/src/pippicore.c +1 -1
@@ 1620,7 1620,7 @@ void wavetable_rsaw(lpfloat_t* out, int length) {

/* create a wavetable (-1 to 1) */
lpbuffer_t* create_wavetable(int name, size_t length) {
    lpbuffer_t* buf = LPBuffer.create(length, 1, -1);
    lpbuffer_t* buf = LPBuffer.create(length, 1, DEFAULT_SAMPLERATE);
    if(name == WT_SINE) {
        wavetable_sine(buf->data, length);            
    } else if (name == WT_COS) {

M pippi/buffers.pxd => pippi/buffers.pxd +1 -1
@@ 45,7 45,7 @@ cdef extern from "pippicore.h":
        size_t pos

    ctypedef struct lpwavetable_factory_t:
        lpbuffer_t * (*create)(const char * name, size_t length)
        lpbuffer_t * (*create)(int name, size_t length)
        void (*destroy)(lpbuffer_t *)

    ctypedef struct lpwindow_factory_t:

M pippi/buffers.pyx => pippi/buffers.pyx +43 -9
@@ 17,7 17,17 @@ from pippi import graph, rand
from pippi.wavetables cimport _window, Wavetable
from pippi cimport interpolation

cdef dict FLAGS = {
cdef dict WT_FLAGS = {
    'sine': WT_SINE,
    'cos': WT_COS,
    'square': WT_SQUARE,
    'tri': WT_TRI,
    'saw': WT_RSAW,
    'rsaw': WT_RSAW,
    'rnd': WT_RND,
}

cdef dict WIN_FLAGS = {
    'sine': WIN_SINE,
    'sineine': WIN_SINEIN,
    'sineout': WIN_SINEOUT,


@@ 45,19 55,37 @@ cdef int to_pan_method(str name):
    except KeyError:
        return PANMETHOD_CONSTANT

cdef int to_flag(str name):
cdef int to_win_flag(str name):
    try:
        return FLAGS[name]
        return WIN_FLAGS[name]
    except KeyError:
        return WIN_SINE

cdef int to_wt_flag(str name):
    try:
        return WT_FLAGS[name]
    except KeyError:
        return WT_SINE

cdef lpbuffer_t * to_window(object o, size_t length=0):
    cdef lpbuffer_t * out
    if length <= 0:
        length = DEFAULT_WTSIZE

    if isinstance(o, str):
        out = LPWindow.create(to_flag(o), length)
        out = LPWindow.create(to_win_flag(o), length)
    else:
        out = to_lpbuffer(o, length, 1)

    return out

cdef lpbuffer_t * to_wavetable(object o, size_t length=0):
    cdef lpbuffer_t * out
    if length <= 0:
        length = DEFAULT_WTSIZE

    if isinstance(o, str):
        out = LPWavetable.create(to_wt_flag(o), length)
    else:
        out = to_lpbuffer(o, length, 1)



@@ 239,12 267,18 @@ cdef class SoundBuffer:

        return SoundBuffer.fromlpbuffer(out)

    """"
    @staticmethod
    cdef SoundBuffer wt(object w, double minvalue=-1, double maxvalue=-1, double length=-1):
        cdef lpbuffer_t * _win = to_wavetable(w, length)
        return SoundBuffer.fromlpbuffer(_win)
    """
    def wt(object w, double minvalue=-1, double maxvalue=1, double length=0, double samplerate=DEFAULT_SAMPLERATE):
        cdef lpbuffer_t * out
        if length > 0:
            length *= samplerate

        out = to_wavetable(w, <size_t>int(length))

        if minvalue != -1 and maxvalue != 1:
            LPBuffer.scale(out, -1, 1, minvalue, maxvalue)

        return SoundBuffer.fromlpbuffer(out)

    def __bool__(self):
        return bool(len(self))

M tests/test_newbuffer.py => tests/test_newbuffer.py +7 -0
@@ 512,6 512,13 @@ class TestNewBuffer(TestCase):
        w = SoundBuffer.win('hann', 0.5, 2)
        w.write('tests/renders/newbuffer_window_hann.wav')

    def test_newbuffer_as_wavetable(self):
        w = SoundBuffer.wt('sine')
        w.write('tests/renders/newbuffer_wavetable_sine.wav')

        w = SoundBuffer.wt('sine', -0.2, 0.6)
        w.write('tests/renders/newbuffer_wavetable_sine_scaled.wav')

    def test_fixed_speed(self):
        sound = SoundBuffer(filename='tests/sounds/guitar1s.wav')
        speed = random.random()