Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: first setup of rfft~ components #110

Draft
wants to merge 10 commits into
base: develop
Choose a base branch
from
Draft
48 changes: 48 additions & 0 deletions hvcc/core/hv2ir/HIrRFFT.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright (C) 2014-2018 Enzien Audio, Ltd.
# Copyright (C) 2023 Wasted Audio
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from typing import Dict, Optional

from .HeavyIrObject import HeavyIrObject
from .HeavyGraph import HeavyGraph


class HIrRFFT(HeavyIrObject):
""" __rfft~f
"""

def __init__(
self,
obj_type: str,
args: Optional[Dict] = None,
graph: Optional[HeavyGraph] = None,
annotations: Optional[Dict] = None
) -> None:
assert obj_type in {"__rfft~f", "__rifft~f"}
super().__init__(obj_type, args=args, graph=graph, annotations=annotations)

def reduce(self) -> Optional[tuple]:
if self.graph is not None:
table_obj = self.graph.resolve_object_for_name(
self.args["table"],
["table", "__table"])
if table_obj is not None:
self.args["table_id"] = table_obj.id
return ({self}, [])
else:
self.add_error(f"Cannot find table named \"{self.args['table']}\" for object {self}.")

return None
1 change: 0 additions & 1 deletion hvcc/core/hv2ir/HeavyGraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ def resolve_object_for_name(
from this graph. Returns None if no objects are available.
This is a convenience method.
"""

objs = self.resolve_objects_for_name(name, obj_types, local_graph)
return objs[0] if len(objs) > 0 else None

Expand Down
3 changes: 3 additions & 0 deletions hvcc/core/hv2ir/HeavyParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from .HIrLorenz import HIrLorenz
from .HIrOutlet import HIrOutlet
from .HIrPack import HIrPack
from .HIrRFFT import HIrRFFT
from .HIrSwitchcase import HIrSwitchcase
from .HIrTabhead import HIrTabhead
from .HIrTabread import HIrTabread
Expand Down Expand Up @@ -306,6 +307,8 @@ def reduce(self) -> tuple:
"__tabwrite~f": HIrTabwrite,
"__tabwrite_stoppable~f": HIrTabwrite,
"__tabwrite": HIrTabwrite,
"__rfft~f": HIrRFFT,
"__rifft~f": HIrRFFT,
"receive": HLangReceive,
"send": HLangSend,
"__switchcase": HIrSwitchcase,
Expand Down
54 changes: 54 additions & 0 deletions hvcc/core/json/heavy.ir.json
Original file line number Diff line number Diff line change
Expand Up @@ -2223,6 +2223,60 @@
"-->"
]
},
"__rfft~f": {
"inlets": [
"~f>",
"-->",
"-->"
],
"ir": {
"control": false,
"signal": true,
"init": true
},
"outlets": [
"~f>",
"~f>"
],
"args": [{
"name": "table",
"value_type": "string",
"description": "",
"default": "",
"required": true
}],
"perf": {
"avx": 0,
"sse": 0
}
},
"__rifft~f": {
"inlets": [
"~f>",
"~f>",
"-->",
"-->"
],
"ir": {
"control": false,
"signal": true,
"init": true
},
"outlets": [
"~f>"
],
"args": [{
"name": "table",
"value_type": "string",
"description": "",
"default": "",
"required": true
}],
"perf": {
"avx": 0,
"sse": 0
}
},
"__rpole~f": {
"inlets": [
"~f>",
Expand Down
73 changes: 73 additions & 0 deletions hvcc/generators/ir2c/SignalRFFT.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Copyright (C) 2014-2018 Enzien Audio, Ltd.
# Copyright (C) 2023 Wasted Audio
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from typing import Dict, List

from .HeavyObject import HeavyObject


class SignalRFFT(HeavyObject):

c_struct = "SignalRFFT"
preamble = "sRFFT"

@classmethod
def get_C_header_set(cls) -> set:
return {"HvSignalRFFT.h"}

@classmethod
def get_C_file_set(cls) -> set:
return {"HvSignalRFFT.h", "HvSignalRFFT.c", "pffft.h", "pffft.c"}

@classmethod
def get_C_init(cls, obj_type: str, obj_id: int, args: Dict) -> List[str]:
return [
"sRFFT_init(&sRFFT_{0}, &hTable_{1}, {2});".format(
obj_id,
args["table_id"],
64)
]

@classmethod
def get_C_onMessage(cls, obj_type: str, obj_id: int, inlet_index: int, args: Dict) -> List[str]:
return [
"sRFFT_onMessage(_c, &Context(_c)->sRFFT_{0}, {1}, m, NULL);".format(
obj_id,
inlet_index)
]

@classmethod
def get_C_process(cls, process_dict: Dict, obj_type: str, obj_id: int, args: Dict) -> List[str]:
if obj_type == "__rfft~f":
return [
"__hv_rfft_f(&sRFFT_{0}, VIf({1}), VOf({2}), VOf({3}));".format(
process_dict["id"],
cls._c_buffer(process_dict["inputBuffers"][0]),
cls._c_buffer(process_dict["outputBuffers"][0]),
cls._c_buffer(process_dict["outputBuffers"][1])
)
]
elif obj_type == "__rifft~f":
return [
"__hv_rifft_f(&sRFFT_{0}, VIf({1}), VIf({2}), VOf({3}));".format(
process_dict["id"],
cls._c_buffer(process_dict["inputBuffers"][0]),
cls._c_buffer(process_dict["inputBuffers"][1]),
cls._c_buffer(process_dict["outputBuffers"][0])
)
]
else:
raise Exception
3 changes: 3 additions & 0 deletions hvcc/generators/ir2c/ir2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
from hvcc.generators.ir2c.SignalLorenz import SignalLorenz
from hvcc.generators.ir2c.SignalMath import SignalMath
from hvcc.generators.ir2c.SignalPhasor import SignalPhasor
from hvcc.generators.ir2c.SignalRFFT import SignalRFFT
from hvcc.generators.ir2c.SignalRPole import SignalRPole
from hvcc.generators.ir2c.SignalSample import SignalSample
from hvcc.generators.ir2c.SignalSamphold import SignalSamphold
Expand Down Expand Up @@ -98,6 +99,8 @@ class ir2c:
"__tabwrite_stoppable~f": SignalTabwrite,
"__phasor~f": SignalPhasor,
"__phasor_k~f": SignalPhasor,
"__rfft~f": SignalRFFT,
"__rifft~f": SignalRFFT,
"__sample~f": SignalSample,
"__samphold~f": SignalSamphold,
"__slice": ControlSlice,
Expand Down
127 changes: 127 additions & 0 deletions hvcc/generators/ir2c/static/HvSignalRFFT.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/**
* Copyright (c) 2023 Wasted Audio
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/

#include "HvSignalRFFT.h"
#include "pffft.h"

hv_size_t sRFFT_init(SignalRFFT *o, struct HvTable *table, const int size) {
o->table = table;
o->setup = pffft_new_setup(size, PFFFT_REAL);
hv_size_t numBytes = hTable_init(&o->inputs, size);
return numBytes;
}

void sRFFT_free(SignalRFFT *o) {
o->table = NULL;
hTable_free(&o->inputs);
pffft_destroy_setup(o->setup);
}

void sRFFT_onMessage(HeavyContextInterface *_c, SignalRFFT *o, int letIndex,
const HvMessage *m, void *sendMessage) {
switch (letIndex) {
case 1: {
if (msg_isHashLike(m,0)) {
HvTable *table = hv_table_get(_c, msg_getHash(m,0));
if (table != NULL) {
o->table = table;
if (hTable_getSize(&o->inputs) != hTable_getSize(table)) {
hTable_resize(&o->inputs,
(hv_uint32_t) hv_min_ui(hTable_getSize(&o->inputs), hTable_getSize(table)));
}
}
}
break;
}
case 2: {
if (msg_isFloat(m,0)) {
// rfft size should never exceed the coefficient table size
hTable_resize(&o->inputs, (hv_uint32_t) msg_getFloat(m,0));
}
break;
}
default: return;
}
}


static inline int wrap(const int i, const int n) {
if (i < 0) return (i+n);
if (i >= n) return (i-n);
return i;
}


void __hv_rfft_f(SignalRFFT *o, hv_bInf_t bIn, hv_bOutf_t bOut0, hv_bOutf_t bOut1) {
hv_assert(o->table != NULL);
float *const work = hTable_getBuffer(o->table);
hv_assert(work != NULL);
const int n = hTable_getSize(o->table); // length fir filter
hv_assert((n&HV_N_SIMD_MASK) == 0); // n is a multiple of HV_N_SIMD

float *const inputs = hTable_getBuffer(&o->inputs);
hv_assert(inputs != NULL);
const int m = hTable_getSize(&o->inputs); // length of input buffer.
hv_assert(m >= n);
const int h_orig = hTable_getHead(&o->inputs);

// float *const bOut = (float *)(hv_alloca(2*n*sizeof(float)));
float *const bOut = (float *)(hv_alloca(sizeof(bIn)));

pffft_transform_ordered(o->setup, &bIn, bOut, work, PFFFT_FORWARD);

// uninterleave result into the output buffers
for (int j = 0; j < n; ++j) {
bOut0[n+j] = bOut[0+2*j];
bOut1[n+j] = bOut[1+2*j];
}

__hv_store_f(inputs+h_orig, bIn); // store the new input to the inputs buffer
hTable_setHead(&o->inputs, wrap(h_orig+HV_N_SIMD, m));
}


void __hv_rifft_f(SignalRFFT *o, hv_bInf_t bIn0, hv_bInf_t bIn1, hv_bOutf_t bOut) {
hv_assert(o->table != NULL);
float *const work = hTable_getBuffer(o->table);
hv_assert(work != NULL);
const int n = hTable_getSize(o->table); // length fir filter
hv_assert((n&HV_N_SIMD_MASK) == 0); // n is a multiple of HV_N_SIMD

float *const inputs = hTable_getBuffer(&o->inputs);
hv_assert(inputs != NULL);
const int m = hTable_getSize(&o->inputs); // length of input buffer.
hv_assert(m >= n);
const int h_orig = hTable_getHead(&o->inputs);

float *bIn00 = &bIn0;
float *bIn10 = &bIn1;
// float *const bIn = (float *)(hv_alloca(2*n*sizeof(float)));
float *const bIn = (float *)(hv_alloca(sizeof(bOut)));

// interleave the input buffers into the transform buffer
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < n; ++j) {
bIn[0+2*j] = bIn00[n+j];
bIn[1+2*j] = bIn10[n+j];
}
}

pffft_transform_ordered(o->setup, bIn, bOut, work, PFFFT_BACKWARD);

// __hv_store_f(inputs+h_orig, bIn); // store the new input to the inputs buffer
hTable_setHead(&o->inputs, wrap(h_orig+HV_N_SIMD, m));
}
Loading
Loading