Skip to content

Commit

Permalink
first setup of rfft~ components
Browse files Browse the repository at this point in the history
  • Loading branch information
dromer committed Jul 16, 2023
1 parent e7c9639 commit f33ddcb
Show file tree
Hide file tree
Showing 11 changed files with 2,461 additions and 0 deletions.
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
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_id",
"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_id",
"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 @@ -97,6 +98,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
106 changes: 106 additions & 0 deletions hvcc/generators/ir2c/static/HvSignalRFFT.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* 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);
}

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);


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

__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);


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

__hv_store_f(inputs+h_orig, bIn0); // store the new input to the inputs buffer
hTable_setHead(&o->inputs, wrap(h_orig+HV_N_SIMD, m));
}
52 changes: 52 additions & 0 deletions hvcc/generators/ir2c/static/HvSignalRFFT.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* 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.
*/

#ifndef _SIGNAL_RFFT_H_
#define _SIGNAL_RFFT_H_

#include "HvHeavyInternal.h"
#include "pffft.h"

#ifdef __cplusplus
extern "C" {
#endif

#ifdef HV_SIMD_NONE
#define PFFFT_SIMD_DISABLE
#endif

typedef struct SignalRFFT {
struct HvTable *table;
struct HvTable inputs;
struct PFFFT_Setup *setup;
} SignalRFFT;

hv_size_t sRFFT_init(SignalRFFT *o, struct HvTable *coeffs, const int size);

void sRFFT_free(SignalRFFT *o);

void sRFFT_onMessage(HeavyContextInterface *_c, SignalRFFT *o, int letIndex,
const HvMessage *m, void *sendMessage);

void __hv_rfft_f(SignalRFFT *o, hv_bInf_t bIn, hv_bOutf_t bOut0, hv_bOutf_t bOut1);

void __hv_rifft_f(SignalRFFT *o, hv_bInf_t bIn0, hv_bInf_t bIn1, hv_bOutf_t bOut);

#ifdef __cplusplus
} // extern "C"
#endif

#endif // _SIGNAL_RFFT_H_
Loading

0 comments on commit f33ddcb

Please sign in to comment.