Debian Codegraph @

src/objects/granulatormodule.c

raw
1 /**************************************************************************
2 * Copyright 2009-2015 Olivier Belanger *
3 * *
4 * This file is part of pyo, a python module to help digital signal *
5 * processing script creation. *
6 * *
7 * pyo is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU Lesser General Public License as *
9 * published by the Free Software Foundation, either version 3 of the *
10 * License, or (at your option) any later version. *
11 * *
12 * pyo is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Lesser General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with pyo. If not, see <http://www.gnu.org/licenses/>. *
19 *************************************************************************/
20
21 #include <Python.h>
22 #include "structmember.h"
23 #include <math.h>
24 #include "pyomodule.h"
25 #include "streammodule.h"
26 #include "servermodule.h"
27 #include "dummymodule.h"
28 #include "tablemodule.h"
29 #include "interpolation.h"
30
31 static MYFLT LOOPER_LINEAR_FADE[513] = {0.0, 0.001953125, 0.00390625, 0.005859375, 0.0078125, 0.009765625, 0.01171875, 0.013671875, 0.015625, 0.017578125, 0.01953125, 0.021484375, 0.0234375, 0.025390625, 0.02734375, 0.029296875, 0.03125, 0.033203125, 0.03515625, 0.037109375, 0.0390625, 0.041015625, 0.04296875, 0.044921875, 0.046875, 0.048828125, 0.05078125, 0.052734375, 0.0546875, 0.056640625, 0.05859375, 0.060546875, 0.0625, 0.064453125, 0.06640625, 0.068359375, 0.0703125, 0.072265625, 0.07421875, 0.076171875, 0.078125, 0.080078125, 0.08203125, 0.083984375, 0.0859375, 0.087890625, 0.08984375, 0.091796875, 0.09375, 0.095703125, 0.09765625, 0.099609375, 0.1015625, 0.103515625, 0.10546875, 0.107421875, 0.109375, 0.111328125, 0.11328125, 0.115234375, 0.1171875, 0.119140625, 0.12109375, 0.123046875, 0.125, 0.126953125, 0.12890625, 0.130859375, 0.1328125, 0.134765625, 0.13671875, 0.138671875, 0.140625, 0.142578125, 0.14453125, 0.146484375, 0.1484375, 0.150390625, 0.15234375, 0.154296875, 0.15625, 0.158203125, 0.16015625, 0.162109375, 0.1640625, 0.166015625, 0.16796875, 0.169921875, 0.171875, 0.173828125, 0.17578125, 0.177734375, 0.1796875, 0.181640625, 0.18359375, 0.185546875, 0.1875, 0.189453125, 0.19140625, 0.193359375, 0.1953125, 0.197265625, 0.19921875, 0.201171875, 0.203125, 0.205078125, 0.20703125, 0.208984375, 0.2109375, 0.212890625, 0.21484375, 0.216796875, 0.21875, 0.220703125, 0.22265625, 0.224609375, 0.2265625, 0.228515625, 0.23046875, 0.232421875, 0.234375, 0.236328125, 0.23828125, 0.240234375, 0.2421875, 0.244140625, 0.24609375, 0.248046875, 0.25, 0.251953125, 0.25390625, 0.255859375, 0.2578125, 0.259765625, 0.26171875, 0.263671875, 0.265625, 0.267578125, 0.26953125, 0.271484375, 0.2734375, 0.275390625, 0.27734375, 0.279296875, 0.28125, 0.283203125, 0.28515625, 0.287109375, 0.2890625, 0.291015625, 0.29296875, 0.294921875, 0.296875, 0.298828125, 0.30078125, 0.302734375, 0.3046875, 0.306640625, 0.30859375, 0.310546875, 0.3125, 0.314453125, 0.31640625, 0.318359375, 0.3203125, 0.322265625, 0.32421875, 0.326171875, 0.328125, 0.330078125, 0.33203125, 0.333984375, 0.3359375, 0.337890625, 0.33984375, 0.341796875, 0.34375, 0.345703125, 0.34765625, 0.349609375, 0.3515625, 0.353515625, 0.35546875, 0.357421875, 0.359375, 0.361328125, 0.36328125, 0.365234375, 0.3671875, 0.369140625, 0.37109375, 0.373046875, 0.375, 0.376953125, 0.37890625, 0.380859375, 0.3828125, 0.384765625, 0.38671875, 0.388671875, 0.390625, 0.392578125, 0.39453125, 0.396484375, 0.3984375, 0.400390625, 0.40234375, 0.404296875, 0.40625, 0.408203125, 0.41015625, 0.412109375, 0.4140625, 0.416015625, 0.41796875, 0.419921875, 0.421875, 0.423828125, 0.42578125, 0.427734375, 0.4296875, 0.431640625, 0.43359375, 0.435546875, 0.4375, 0.439453125, 0.44140625, 0.443359375, 0.4453125, 0.447265625, 0.44921875, 0.451171875, 0.453125, 0.455078125, 0.45703125, 0.458984375, 0.4609375, 0.462890625, 0.46484375, 0.466796875, 0.46875, 0.470703125, 0.47265625, 0.474609375, 0.4765625, 0.478515625, 0.48046875, 0.482421875, 0.484375, 0.486328125, 0.48828125, 0.490234375, 0.4921875, 0.494140625, 0.49609375, 0.498046875, 0.5, 0.501953125, 0.50390625, 0.505859375, 0.5078125, 0.509765625, 0.51171875, 0.513671875, 0.515625, 0.517578125, 0.51953125, 0.521484375, 0.5234375, 0.525390625, 0.52734375, 0.529296875, 0.53125, 0.533203125, 0.53515625, 0.537109375, 0.5390625, 0.541015625, 0.54296875, 0.544921875, 0.546875, 0.548828125, 0.55078125, 0.552734375, 0.5546875, 0.556640625, 0.55859375, 0.560546875, 0.5625, 0.564453125, 0.56640625, 0.568359375, 0.5703125, 0.572265625, 0.57421875, 0.576171875, 0.578125, 0.580078125, 0.58203125, 0.583984375, 0.5859375, 0.587890625, 0.58984375, 0.591796875, 0.59375, 0.595703125, 0.59765625, 0.599609375, 0.6015625, 0.603515625, 0.60546875, 0.607421875, 0.609375, 0.611328125, 0.61328125, 0.615234375, 0.6171875, 0.619140625, 0.62109375, 0.623046875, 0.625, 0.626953125, 0.62890625, 0.630859375, 0.6328125, 0.634765625, 0.63671875, 0.638671875, 0.640625, 0.642578125, 0.64453125, 0.646484375, 0.6484375, 0.650390625, 0.65234375, 0.654296875, 0.65625, 0.658203125, 0.66015625, 0.662109375, 0.6640625, 0.666015625, 0.66796875, 0.669921875, 0.671875, 0.673828125, 0.67578125, 0.677734375, 0.6796875, 0.681640625, 0.68359375, 0.685546875, 0.6875, 0.689453125, 0.69140625, 0.693359375, 0.6953125, 0.697265625, 0.69921875, 0.701171875, 0.703125, 0.705078125, 0.70703125, 0.708984375, 0.7109375, 0.712890625, 0.71484375, 0.716796875, 0.71875, 0.720703125, 0.72265625, 0.724609375, 0.7265625, 0.728515625, 0.73046875, 0.732421875, 0.734375, 0.736328125, 0.73828125, 0.740234375, 0.7421875, 0.744140625, 0.74609375, 0.748046875, 0.75, 0.751953125, 0.75390625, 0.755859375, 0.7578125, 0.759765625, 0.76171875, 0.763671875, 0.765625, 0.767578125, 0.76953125, 0.771484375, 0.7734375, 0.775390625, 0.77734375, 0.779296875, 0.78125, 0.783203125, 0.78515625, 0.787109375, 0.7890625, 0.791015625, 0.79296875, 0.794921875, 0.796875, 0.798828125, 0.80078125, 0.802734375, 0.8046875, 0.806640625, 0.80859375, 0.810546875, 0.8125, 0.814453125, 0.81640625, 0.818359375, 0.8203125, 0.822265625, 0.82421875, 0.826171875, 0.828125, 0.830078125, 0.83203125, 0.833984375, 0.8359375, 0.837890625, 0.83984375, 0.841796875, 0.84375, 0.845703125, 0.84765625, 0.849609375, 0.8515625, 0.853515625, 0.85546875, 0.857421875, 0.859375, 0.861328125, 0.86328125, 0.865234375, 0.8671875, 0.869140625, 0.87109375, 0.873046875, 0.875, 0.876953125, 0.87890625, 0.880859375, 0.8828125, 0.884765625, 0.88671875, 0.888671875, 0.890625, 0.892578125, 0.89453125, 0.896484375, 0.8984375, 0.900390625, 0.90234375, 0.904296875, 0.90625, 0.908203125, 0.91015625, 0.912109375, 0.9140625, 0.916015625, 0.91796875, 0.919921875, 0.921875, 0.923828125, 0.92578125, 0.927734375, 0.9296875, 0.931640625, 0.93359375, 0.935546875, 0.9375, 0.939453125, 0.94140625, 0.943359375, 0.9453125, 0.947265625, 0.94921875, 0.951171875, 0.953125, 0.955078125, 0.95703125, 0.958984375, 0.9609375, 0.962890625, 0.96484375, 0.966796875, 0.96875, 0.970703125, 0.97265625, 0.974609375, 0.9765625, 0.978515625, 0.98046875, 0.982421875, 0.984375, 0.986328125, 0.98828125, 0.990234375, 0.9921875, 0.994140625, 0.99609375, 0.998046875, 1.0};
32 static MYFLT LOOPER_POWER_FADE[513] = {0.0, 0.0030679567629659761, 0.0061358846491544753, 0.0092037547820598194, 0.012271538285719925, 0.0153392062849881, 0.01840672990580482, 0.021474080275469508, 0.024541228522912288, 0.02760814577896574, 0.030674803176636626, 0.03374117185137758, 0.036807222941358832, 0.039872927587739811, 0.04293825693494082, 0.046003182130914623, 0.049067674327418015, 0.052131704680283324, 0.055195244349689941, 0.058258264500435752, 0.061320736302208578, 0.064382630929857465, 0.067443919563664051, 0.070504573389613856, 0.073564563599667426, 0.076623861392031492, 0.079682437971430126, 0.082740264549375692, 0.085797312344439894, 0.0888535525825246, 0.091908956497132724, 0.094963495329638992, 0.098017140329560604, 0.10106986275482782, 0.10412163387205459, 0.10717242495680884, 0.11022220729388306, 0.11327095217756435, 0.11631863091190475, 0.11936521481099135, 0.1224106751992162, 0.12545498341154623, 0.12849811079379317, 0.13154002870288312, 0.13458070850712617, 0.13762012158648604, 0.14065823933284921, 0.14369503315029447, 0.14673047445536175, 0.14976453467732151, 0.15279718525844344, 0.15582839765426523, 0.15885814333386145, 0.16188639378011183, 0.16491312048996992, 0.16793829497473117, 0.17096188876030122, 0.17398387338746382, 0.17700422041214875, 0.18002290140569951, 0.18303988795514095, 0.18605515166344663, 0.18906866414980619, 0.19208039704989244, 0.19509032201612825, 0.19809841071795356, 0.2011046348420919, 0.20410896609281687, 0.20711137619221856, 0.21011183688046961, 0.21311031991609136, 0.21610679707621952, 0.2191012401568698, 0.22209362097320351, 0.22508391135979283, 0.22807208317088573, 0.23105810828067111, 0.23404195858354343, 0.2370236059943672, 0.2400030224487415, 0.24298017990326387, 0.24595505033579459, 0.24892760574572015, 0.25189781815421697, 0.25486565960451457, 0.25783110216215899, 0.26079411791527551, 0.26375467897483135, 0.26671275747489837, 0.26966832557291509, 0.27262135544994898, 0.27557181931095814, 0.27851968938505306, 0.28146493792575794, 0.28440753721127188, 0.28734745954472951, 0.29028467725446233, 0.29321916269425863, 0.29615088824362379, 0.29907982630804048, 0.30200594931922808, 0.30492922973540237, 0.30784964004153487, 0.31076715274961147, 0.31368174039889152, 0.31659337555616585, 0.31950203081601569, 0.32240767880106985, 0.32531029216226293, 0.3282098435790925, 0.33110630575987643, 0.33399965144200938, 0.33688985339222005, 0.33977688440682685, 0.34266071731199438, 0.34554132496398909, 0.34841868024943456, 0.35129275608556709, 0.35416352542049034, 0.35703096123342998, 0.35989503653498811, 0.36275572436739723, 0.36561299780477385, 0.36846682995337232, 0.37131719395183754, 0.37416406297145793, 0.37700741021641826, 0.37984720892405116, 0.38268343236508978, 0.38551605384391885, 0.38834504669882625, 0.39117038430225387, 0.3939920400610481, 0.39680998741671031, 0.39962419984564679, 0.40243465085941843, 0.40524131400498986, 0.40804416286497869, 0.41084317105790391, 0.41363831223843456, 0.41642956009763715, 0.41921688836322391, 0.42200027079979968, 0.42477968120910881, 0.42755509343028208, 0.43032648134008261, 0.43309381885315196, 0.43585707992225547, 0.43861623853852766, 0.44137126873171667, 0.4441221445704292, 0.44686884016237416, 0.44961132965460654, 0.45234958723377089, 0.45508358712634384, 0.45781330359887723, 0.46053871095824001, 0.46325978355186015, 0.46597649576796618, 0.46868882203582796, 0.47139673682599764, 0.47410021465054997, 0.47679923006332209, 0.47949375766015301, 0.48218377207912272, 0.48486924800079106, 0.487550160148436, 0.49022648328829116, 0.49289819222978404, 0.49556526182577254, 0.49822766697278187, 0.50088538261124071, 0.50353838372571758, 0.50618664534515523, 0.50883014254310699, 0.5114688504379703, 0.51410274419322166, 0.51673179901764987, 0.51935599016558964, 0.52197529293715439, 0.52458968267846895, 0.52719913478190139, 0.52980362468629461, 0.5324031278771979, 0.53499761988709715, 0.53758707629564539, 0.54017147272989285, 0.54275078486451589, 0.54532498842204646, 0.54789405917310019, 0.55045797293660481, 0.55301670558002747, 0.55557023301960218, 0.5581185312205561, 0.56066157619733603, 0.56319934401383409, 0.56573181078361312, 0.56825895267013149, 0.57078074588696726, 0.5732971666980422, 0.57580819141784534, 0.57831379641165559, 0.58081395809576453, 0.58330865293769829, 0.58579785745643886, 0.58828154822264522, 0.59075970185887416, 0.5932322950397998, 0.59569930449243336, 0.59816070699634238, 0.60061647938386897, 0.60306659854034816, 0.60551104140432555, 0.60794978496777363, 0.61038280627630948, 0.61281008242940971, 0.61523159058062682, 0.61764730793780387, 0.6200572117632891, 0.62246127937414997, 0.62485948814238634, 0.62725181549514408, 0.62963823891492698, 0.63201873593980906, 0.63439328416364549, 0.6367618612362842, 0.63912444486377573, 0.64148101280858316, 0.64383154288979139, 0.64617601298331628, 0.64851440102211244, 0.65084668499638099, 0.65317284295377676, 0.65549285299961535, 0.65780669329707864, 0.66011434206742048, 0.66241577759017178, 0.66471097820334479, 0.66699992230363747, 0.66928258834663601, 0.67155895484701833, 0.67382900037875604, 0.67609270357531592, 0.67835004312986147, 0.68060099779545302, 0.68284554638524808, 0.68508366777270036, 0.68731534089175905, 0.68954054473706683, 0.69175925836415775, 0.69397146088965389, 0.69617713149146299, 0.69837624940897292, 0.70056879394324834, 0.7027547444572253, 0.70493408037590488, 0.70710678118654746, 0.70927282643886558, 0.71143219574521632, 0.71358486878079352, 0.71573082528381859, 0.7178700450557316, 0.72000250796138165, 0.72212819392921523, 0.72424708295146689, 0.7263591550843459, 0.7284643904482252, 0.73056276922782759, 0.7326542716724127, 0.73473887809596339, 0.73681656887736979, 0.73888732446061511, 0.74095112535495899, 0.74300795213512161, 0.74505778544146595, 0.74710060598018013, 0.74913639452345926, 0.75116513190968637, 0.75318679904361241, 0.75520137689653644, 0.75720884650648446, 0.75920918897838796, 0.76120238548426178, 0.76318841726338127, 0.76516726562245885, 0.76713891193582029, 0.76910333764557959, 0.77106052426181371, 0.77301045336273688, 0.77495310659487382, 0.77688846567323244, 0.77881651238147587, 0.78073722857209438, 0.78265059616657562, 0.78455659715557524, 0.78645521359908577, 0.78834642762660623, 0.79023022143731003, 0.79210657730021228, 0.79397547755433706, 0.79583690460888346, 0.79769084094339104, 0.79953726910790501, 0.80137617172314013, 0.80320753148064483, 0.80503133114296355, 0.80684755354379922, 0.80865618158817498, 0.81045719825259477, 0.81225058658520388, 0.8140363297059483, 0.81581441080673378, 0.81758481315158371, 0.8193475200767969, 0.82110251499110465, 0.82284978137582632, 0.82458930278502529, 0.82632106284566342, 0.82804504525775569, 0.82976123379452305, 0.83146961230254512, 0.83317016470191319, 0.83486287498638001, 0.83654772722351189, 0.83822470555483797, 0.83989379419599941, 0.84155497743689833, 0.84320823964184544, 0.84485356524970701, 0.84649093877405202, 0.84812034480329712, 0.84974176800085244, 0.8513551931052652, 0.85296060493036363, 0.85455798836540053, 0.85614732837519436, 0.85772861000027212, 0.85930181835700825, 0.8608669386377672, 0.8624239561110405, 0.8639728561215867, 0.86551362409056898, 0.86704624551569265, 0.8685707059713409, 0.87008699110871135, 0.87159508665595098, 0.87309497841828998, 0.87458665227817611, 0.87607009419540649, 0.87754529020726124, 0.87901222642863341, 0.88047088905216075, 0.88192126434835494, 0.88336333866573158, 0.88479709843093779, 0.88622253014888064, 0.88763962040285393, 0.88904835585466446, 0.89044872324475788, 0.89184070939234272, 0.89322430119551532, 0.89459948563138258, 0.89596624975618511, 0.89732458070541832, 0.89867446569395382, 0.90001589201616028, 0.90134884704602203, 0.90267331823725883, 0.90398929312344334, 0.90529675931811882, 0.90659570451491533, 0.90788611648766615, 0.90916798309052238, 0.91044129225806714, 0.91170603200542988, 0.9129621904283981, 0.91420975570353069, 0.91544871608826783, 0.9166790599210427, 0.91790077562139039, 0.91911385169005777, 0.92031827670911048, 0.9215140393420419, 0.92270112833387852, 0.92387953251128674, 0.92504924078267758, 0.92621024213831138, 0.92736252565040111, 0.92850608047321548, 0.92964089584318121, 0.93076696107898371, 0.93188426558166815, 0.93299279883473885, 0.93409255040425887, 0.9351835099389475, 0.93626566717027826, 0.93733901191257496, 0.93840353406310806, 0.93945922360218992, 0.9405060705932683, 0.94154406518302081, 0.94257319760144687, 0.94359345816196039, 0.94460483726148026, 0.94560732538052128, 0.94660091308328353, 0.94758559101774109, 0.94856134991573027, 0.94952818059303667, 0.9504860739494817, 0.95143502096900834, 0.95237501271976588, 0.95330604035419375, 0.95422809510910567, 0.95514116830577067, 0.95604525134999641, 0.95694033573220894, 0.95782641302753291, 0.9587034748958716, 0.95957151308198452, 0.96043051941556579, 0.96128048581132064, 0.96212140426904158, 0.96295326687368388, 0.96377606579543984, 0.96458979328981265, 0.9653944416976894, 0.96619000344541262, 0.96697647104485207, 0.96775383709347551, 0.96852209427441727, 0.96928123535654853, 0.97003125319454397, 0.97077214072895035, 0.97150389098625178, 0.97222649707893627, 0.97293995220556007, 0.97364424965081187, 0.97433938278557586, 0.97502534506699412, 0.97570213003852857, 0.97636973133002114, 0.97702814265775439, 0.97767735782450993, 0.97831737071962765, 0.9789481753190622, 0.97956976568544052, 0.98018213596811732, 0.98078528040323043, 0.98137919331375456, 0.98196386910955524, 0.98253930228744124, 0.98310548743121629, 0.98366241921173025, 0.98421009238692903, 0.98474850180190421, 0.98527764238894122, 0.98579750916756737, 0.98630809724459867, 0.98680940181418542, 0.98730141815785843, 0.98778414164457218, 0.98825756773074946, 0.98872169196032378, 0.98917650996478101, 0.98962201746320078, 0.99005821026229712, 0.99048508425645698, 0.99090263542778001, 0.99131085984611544, 0.99170975366909953, 0.9920993131421918, 0.99247953459870997, 0.9928504144598651, 0.9932119492347945, 0.9935641355205953, 0.99390697000235606, 0.9942404494531879, 0.99456457073425542, 0.99487933079480562, 0.99518472667219682, 0.99548075549192694, 0.99576741446765982, 0.99604470090125197, 0.996312612182778, 0.99657114579055484, 0.99682029929116567, 0.99706007033948296, 0.99729045667869021, 0.99751145614030345, 0.99772306664419164, 0.997925286198596, 0.99811811290014918, 0.99830154493389289, 0.99847558057329477, 0.99864021818026527, 0.99879545620517241, 0.99894129318685687, 0.99907772775264536, 0.99920475861836389, 0.99932238458834954, 0.99943060455546173, 0.99952941750109314, 0.99961882249517864, 0.99969881869620425, 0.99976940535121528, 0.9998305817958234, 0.99988234745421256, 0.9999247018391445, 0.9999576445519639, 0.99998117528260111, 0.99999529380957619, 1.0};
33 static MYFLT LOOPER_SIGMOID_FADE[513] = {0.0, 9.4123586994454556e-06, 3.7649080427748505e-05, 8.4709102088298405e-05, 0.00015059065189787502, 0.00023529124945342872, 0.00033880770582522812, 0.00046113612367731927, 0.0006022718974137975, 0.00076220971335261289, 0.00094094354992541041, 0.0011384666779041819, 0.0013547716606548965, 0.0015898503544171105, 0.0018436939086109994, 0.0021162927661700914, 0.0024076366639015356, 0.0027177146328722368, 0.0030465149988219697, 0.0033940253826026945, 0.0037602327006450165, 0.0041451231654502374, 0.0045486822861099951, 0.0049708948688514387, 0.0054117450176094928, 0.0058712161346252678, 0.0063492909210707826, 0.0068459513777006653, 0.0073611788055293337, 0.0078949538065354319, 0.008447256284391802, 0.0090180654452223785, 0.0096073597983847847, 0.010215117157279741, 0.010841314640186173, 0.011485928671122803, 0.012148934980735715, 0.012830308607212071, 0.013530023897219912, 0.014248054506874053, 0.014984373402728013, 0.015738952862791311, 0.016511764477573909, 0.017302779151155301, 0.018111967102280024, 0.01893929786547921, 0.019784740292217051, 0.0206482625520642, 0.021529832133895532, 0.022429415847114664, 0.023346979822903013, 0.024282489515495831, 0.025235909703481607, 0.026207204491129399, 0.02719633730973936, 0.028203270919019807, 0.029227967408489597, 0.030270388198904985, 0.03133049404371252, 0.032408245030526195, 0.033503600582630522, 0.034616519460508088, 0.035746959763392205, 0.036894878930844255, 0.038060233744356631, 0.039242980328979049, 0.040443074154971115, 0.041660470039478648, 0.042895122148234655, 0.044146983997285061, 0.045416008454738754, 0.046702147742542333, 0.048005353438278275, 0.049325576476988986, 0.050662767153022981, 0.052016875121907391, 0.053387849402242338, 0.054775638377621005, 0.056180189798573033, 0.05760145078453105, 0.059039367825822475, 0.060493886785683182, 0.061964952902296699, 0.06345251079085501, 0.06495650444564427, 0.066476877242153565, 0.068013571939206596, 0.069566530681116401, 0.071135694999863941, 0.072721005817299733, 0.074322403447367402, 0.075939827598351384, 0.077573217375146386, 0.079222511281550778, 0.080887647222581016, 0.082568562506809939, 0.084265193848727271, 0.085977477371122046, 0.087705348607487355, 0.08944874250444762, 0.091207593424208144, 0.092981835147025738, 0.094771400873702616, 0.096576223228100277, 0.098396234259677529, 0.10023136544604749, 0.10208154769555822, 0.10394671134989369, 0.10582678618669683, 0.10772170142221238, 0.1096313857139527, 0.11155576716338378, 0.11349477331863145, 0.11544833117721015, 0.11741636718877047, 0.11939880725786906, 0.12139557674675777, 0.12340660047819374, 0.1254318027382702, 0.12747110727926697, 0.1295244373225205, 0.13159171556131499, 0.13367286416379359, 0.13576780477588735, 0.1378764585242665, 0.13999874601930906, 0.14213458735809065, 0.14428390212739184, 0.14644660940672616, 0.14862262777138718, 0.15081187529551349, 0.153014269555173, 0.15522972763146642, 0.15745816611364977, 0.15969950110227338, 0.16195364821234198, 0.16422052257649067, 0.16650003884818121, 0.16879211120491411, 0.17109665335146057, 0.17341357852311146, 0.17574279948894383, 0.17808422855510425, 0.18043777756811202, 0.18280335791817703, 0.18518088054253651, 0.18757025592880677, 0.18997139411835529, 0.19238420470968631, 0.19480859686184526, 0.19724447929783717, 0.19969176030806535, 0.20215034775378338, 0.20462014907056286, 0.20710107127178046, 0.20959302095211751, 0.21209590429107739, 0.21460962705651632, 0.21713409460819322, 0.21966921190133171, 0.22221488349019891, 0.22477101353169748, 0.2273375057889766, 0.22991426363505363, 0.23250119005645137, 0.23509818765685253, 0.23770515866076569, 0.24032200491720523, 0.24294862790338906, 0.24558492872844634, 0.24823080813714093, 0.25088616651360907, 0.25355090388510787, 0.25622491992578178, 0.25890811396043867, 0.26160038496833893, 0.26430163158700104, 0.26701175211601708, 0.26973064452088003, 0.27245820643682805, 0.27519433517269654, 0.27793892771478512, 0.2806918807307362, 0.28345309057342394, 0.28622245328485874, 0.28899986460010024, 0.2917852199511814, 0.29457841447104793, 0.29737934299750524, 0.30018790007717666, 0.30300397996947592, 0.30582747665058668, 0.30865828381745486, 0.31149629489179087, 0.31434140302408115, 0.31719350109761285, 0.320052481732506, 0.32291823728975477, 0.32579065987528255, 0.32866964134400301, 0.33155507330389, 0.33444684712006173, 0.33734485391886837, 0.34024898459199188, 0.3431591298005543, 0.34607517997923243, 0.34899702534038579, 0.35192455587818816, 0.35485766137276881, 0.35779623139436395, 0.36074015530747361, 0.36368932227502559, 0.36664362126255073, 0.36960294104236202, 0.37256717019774244, 0.37553619712713993, 0.37850991004836793, 0.38148819700281617, 0.38447094585966451, 0.38745804432010356, 0.39044937992156492, 0.39344484004195446, 0.39644431190389073, 0.39944768257895397, 0.40245483899193568, 0.40546566792509658, 0.40848005602242954, 0.41149788979392549, 0.41451905561984914, 0.41754343975501512, 0.42057092833306925, 0.42360140737077812, 0.42663476277231926, 0.42967088033357542, 0.43270964574643683, 0.43575094460310321, 0.43879466240039156, 0.44184068454404762, 0.44488889635305834, 0.44793918306397246, 0.45099142983521978, 0.45404552175143359, 0.45710134382777989, 0.46015878101428509, 0.46321771820016633, 0.46627804021816788, 0.46933963184889549, 0.47240237782515471, 0.47546616283629101, 0.47853087153252949, 0.48159638852932035, 0.48466259841168174, 0.4877293857385438, 0.49079663504709742, 0.49386423085714021, 0.49693205767542281, 0.49999999999999989, 0.50306794232457708, 0.50613576914285963, 0.50920336495290242, 0.51227061426145604, 0.51533740158831809, 0.51840361147067948, 0.5214691284674704, 0.52453383716370883, 0.52759762217484507, 0.53066036815110429, 0.5337219597818319, 0.53678228179983345, 0.53984121898571469, 0.54289865617221988, 0.54595447824856624, 0.54900857016478, 0.55206081693602738, 0.55511110364694149, 0.55815931545595221, 0.56120533759960822, 0.56424905539689663, 0.567290354253563, 0.57032911966642441, 0.57336523722768051, 0.57639859262922166, 0.57942907166693058, 0.58245656024498471, 0.58548094438015064, 0.58850211020607435, 0.59151994397757024, 0.59453433207490325, 0.5975451610080641, 0.60055231742104587, 0.60355568809610904, 0.60655515995804532, 0.60955062007843486, 0.61254195567989622, 0.61552905414033532, 0.61851180299718367, 0.62149008995163191, 0.62446380287285996, 0.62743282980225745, 0.63039705895763776, 0.6333563787374491, 0.63631067772497429, 0.63925984469252617, 0.64220376860563588, 0.64514233862723103, 0.64807544412181162, 0.6510029746596141, 0.65392482002076735, 0.65684087019944559, 0.65975101540800796, 0.66265514608113152, 0.6655531528799381, 0.66844492669610978, 0.67133035865599688, 0.67420934012471723, 0.67708176271024501, 0.67994751826749389, 0.68280649890238698, 0.68565859697591869, 0.68850370510820902, 0.69134171618254503, 0.69417252334941315, 0.69699602003052397, 0.69981209992282323, 0.70262065700249465, 0.70542158552895196, 0.70821478004881844, 0.71100013539989959, 0.71377754671514104, 0.71654690942657595, 0.71930811926926363, 0.72206107228521477, 0.72480566482730335, 0.72754179356317183, 0.7302693554791202, 0.73298824788398276, 0.73569836841299874, 0.73839961503166096, 0.74109188603956111, 0.743775080074218, 0.74644909611489196, 0.74911383348639071, 0.75176919186285884, 0.75441507127155349, 0.75705137209661078, 0.75967799508279499, 0.76229484133923409, 0.76490181234314725, 0.76749880994354847, 0.77008573636494626, 0.77266249421102318, 0.77522898646830229, 0.77778511650980087, 0.78033078809866807, 0.78286590539180656, 0.78539037294348346, 0.78790409570892272, 0.79040697904788193, 0.79289892872821932, 0.79537985092943697, 0.7978496522462164, 0.80030823969193454, 0.80275552070216261, 0.80519140313815452, 0.80761579529031347, 0.8100286058816446, 0.81242974407119306, 0.81481911945746366, 0.81719664208182241, 0.81956222243188781, 0.82191577144489558, 0.824257200511056, 0.82658642147688832, 0.82890334664853926, 0.83120788879508578, 0.83349996115181879, 0.83577947742350922, 0.83804635178765785, 0.84030049889772662, 0.84254183388634996, 0.84477027236853341, 0.84698573044482683, 0.84918812470448612, 0.85137737222861265, 0.85355339059327373, 0.85571609787260805, 0.85786541264190941, 0.86000125398069083, 0.86212354147573333, 0.86423219522411276, 0.86632713583620613, 0.8684082844386849, 0.87047556267747939, 0.87252889272073275, 0.87456819726172963, 0.87659339952180615, 0.87860442325324217, 0.880601192742131, 0.88258363281122942, 0.88455166882278968, 0.88650522668136855, 0.888444232836616, 0.89036861428604719, 0.89227829857778751, 0.89417321381330295, 0.89605328865010625, 0.89791845230444167, 0.8997686345539524, 0.90160376574032253, 0.90342377677189956, 0.90522859912629738, 0.90701816485297426, 0.90879240657579163, 0.91055125749555232, 0.91229465139251253, 0.91402252262887773, 0.91573480615127267, 0.91743143749319001, 0.91911235277741887, 0.92077748871844922, 0.92242678262485356, 0.92406017240164862, 0.92567759655263271, 0.92727899418269999, 0.92886430500013595, 0.9304334693188836, 0.93198642806079324, 0.93352312275784632, 0.93504349555435562, 0.93654748920914499, 0.93803504709770336, 0.93950611321431676, 0.94096063217417747, 0.94239854921546895, 0.9438198102014268, 0.94522436162237888, 0.94661215059775761, 0.94798312487809244, 0.94933723284697691, 0.9506744235230109, 0.9519946465617215, 0.95329785225745778, 0.95458399154526119, 0.95585301600271488, 0.95710487785176546, 0.95833952996052119, 0.95955692584502883, 0.9607570196710209, 0.96193976625564326, 0.96310512106915569, 0.96425304023660774, 0.96538348053949186, 0.96649639941736942, 0.96759175496947369, 0.96866950595628742, 0.96972961180109496, 0.97077203259151024, 0.97179672908098014, 0.97280366269026053, 0.97379279550887043, 0.97476409029651834, 0.97571751048450417, 0.97665302017709688, 0.97757058415288545, 0.97847016786610441, 0.97935173744793569, 0.98021525970778289, 0.98106070213452068, 0.98188803289771998, 0.9826972208488447, 0.98348823552242592, 0.98426104713720863, 0.98501562659727204, 0.98575194549312584, 0.98646997610278009, 0.98716969139278787, 0.98785106501926423, 0.98851407132887714, 0.98915868535981377, 0.98978488284272026, 0.99039264020161522, 0.99098193455477757, 0.9915527437156082, 0.99210504619346451, 0.99263882119447056, 0.99315404862229939, 0.99365070907892916, 0.99412878386537473, 0.99458825498239056, 0.99502910513114851, 0.99545131771388995, 0.99585487683454976, 0.99623976729935493, 0.99660597461739719, 0.99695348500117809, 0.99728228536712771, 0.99759236333609846, 0.99788370723382991, 0.99815630609138895, 0.99841014964558283, 0.99864522833934499, 0.99886153332209582, 0.99905905645007453, 0.99923779028664739, 0.9993977281025862, 0.99953886387632274, 0.99966119229417472, 0.99976470875054657, 0.99984940934810207, 0.99991529089791165, 0.99996235091957231, 0.99999058764130055, 1.0};
34
35 typedef struct
36 {
37 pyo_audio_HEAD
38 PyObject *table;
39 PyObject *env;
40 PyObject *pitch;
41 Stream *pitch_stream;
42 PyObject *pos;
43 Stream *pos_stream;
44 PyObject *dur;
45 Stream *dur_stream;
46 int ngrains;
47 MYFLT basedur;
48 MYFLT pointerPos;
49 MYFLT *startPos;
50 MYFLT *gsize;
51 MYFLT *gphase;
52 MYFLT *lastppos;
53 MYFLT srScale;
54 int modebuffer[5];
55 } Granulator;
56
57 static void
58 Granulator_transform_iii(Granulator *self)
59 {
60 MYFLT val, x, x1, inc, index, fpart, amp, ppos;
61 int i, j;
62 T_SIZE_T ipart;
63
64 MYFLT *tablelist = TableStream_getData((TableStream *)self->table);
65 T_SIZE_T size = TableStream_getSize((TableStream *)self->table);
66
67 MYFLT *envlist = TableStream_getData((TableStream *)self->env);
68 T_SIZE_T envsize = TableStream_getSize((TableStream *)self->env);
69
70 MYFLT pit = PyFloat_AS_DOUBLE(self->pitch);
71 MYFLT pos = PyFloat_AS_DOUBLE(self->pos);
72 MYFLT dur = PyFloat_AS_DOUBLE(self->dur);
73
74 inc = pit * (1.0 / self->basedur) / self->sr;
75
76 for (i = 0; i < self->bufsize; i++)
77 {
78 self->data[i] = 0.0;
79 self->pointerPos += inc;
80
81 for (j = 0; j < self->ngrains; j++)
82 {
83 ppos = self->pointerPos + self->gphase[j];
84
85 if (ppos >= 1.0)
86 {
87 ppos -= 1.0;
88 }
89
90 // compute envelope
91 index = ppos * envsize;
92 ipart = (T_SIZE_T)index;
93 fpart = index - ipart;
94 x = envlist[ipart];
95 x1 = envlist[ipart + 1];
96 amp = x + (x1 - x) * fpart;
97
98 if (ppos < self->lastppos[j])
99 {
100 self->startPos[j] = pos;
101 self->gsize[j] = dur * self->sr * self->srScale;
102 }
103
104 self->lastppos[j] = ppos;
105
106 // compute sampling
107 index = ppos * self->gsize[j] + self->startPos[j];
108
109 if (index >= 0 && index < size)
110 {
111 ipart = (T_SIZE_T)index;
112 fpart = index - ipart;
113 x = tablelist[ipart];
114 x1 = tablelist[ipart + 1];
115 val = x + (x1 - x) * fpart;
116 }
117 else
118 val = 0.0;
119
120 self->data[i] += (val * amp);
121 }
122
123 if (self->pointerPos < 0)
124 self->pointerPos += 1.0;
125 else if (self->pointerPos >= 1)
126 self->pointerPos -= 1.0;
127 }
128 }
129
130 static void
131 Granulator_transform_aii(Granulator *self)
132 {
133 MYFLT val, x, x1, inc, index, fpart, amp, ppos, frtosamps;
134 int i, j;
135 T_SIZE_T ipart;
136
137 MYFLT *tablelist = TableStream_getData((TableStream *)self->table);
138 T_SIZE_T size = TableStream_getSize((TableStream *)self->table);
139
140 MYFLT *envlist = TableStream_getData((TableStream *)self->env);
141 T_SIZE_T envsize = TableStream_getSize((TableStream *)self->env);
142
143 MYFLT *pit = Stream_getData((Stream *)self->pitch_stream);
144 MYFLT pos = PyFloat_AS_DOUBLE(self->pos);
145 MYFLT dur = PyFloat_AS_DOUBLE(self->dur);
146
147 frtosamps = (1.0 / self->basedur) / self->sr;
148
149 for (i = 0; i < self->bufsize; i++)
150 {
151 self->data[i] = 0.0;
152 inc = pit[i] * frtosamps;
153 self->pointerPos += inc;
154
155 for (j = 0; j < self->ngrains; j++)
156 {
157 ppos = self->pointerPos + self->gphase[j];
158
159 if (ppos >= 1.0)
160 {
161 ppos -= 1.0;
162 }
163
164 // compute envelope
165 index = ppos * envsize;
166 ipart = (T_SIZE_T)index;
167 fpart = index - ipart;
168 x = envlist[ipart];
169 x1 = envlist[ipart + 1];
170 amp = x + (x1 - x) * fpart;
171
172 if (ppos < self->lastppos[j])
173 {
174 self->startPos[j] = pos;
175 self->gsize[j] = dur * self->sr * self->srScale;
176 }
177
178 self->lastppos[j] = ppos;
179
180 // compute sampling
181 index = ppos * self->gsize[j] + self->startPos[j];
182
183 if (index >= 0 && index < size)
184 {
185 ipart = (T_SIZE_T)index;
186 fpart = index - ipart;
187 x = tablelist[ipart];
188 x1 = tablelist[ipart + 1];
189 val = x + (x1 - x) * fpart;
190 }
191 else
192 val = 0.0;
193
194 self->data[i] += (val * amp);
195 }
196
197 if (self->pointerPos < 0)
198 self->pointerPos += 1.0;
199 else if (self->pointerPos >= 1)
200 self->pointerPos -= 1.0;
201 }
202 }
203
204 static void
205 Granulator_transform_iai(Granulator *self)
206 {
207 MYFLT val, x, x1, inc, index, fpart, amp, ppos;
208 int i, j;
209 T_SIZE_T ipart;
210
211 MYFLT *tablelist = TableStream_getData((TableStream *)self->table);
212 T_SIZE_T size = TableStream_getSize((TableStream *)self->table);
213
214 MYFLT *envlist = TableStream_getData((TableStream *)self->env);
215 T_SIZE_T envsize = TableStream_getSize((TableStream *)self->env);
216
217 MYFLT pit = PyFloat_AS_DOUBLE(self->pitch);
218 MYFLT *pos = Stream_getData((Stream *)self->pos_stream);
219 MYFLT dur = PyFloat_AS_DOUBLE(self->dur);
220
221 inc = pit * (1.0 / self->basedur) / self->sr;
222
223 MYFLT gsize = dur * self->sr * self->srScale;
224
225 for (j = 0; j < self->ngrains; j++)
226 {
227 self->gsize[j] = gsize;
228 }
229
230 for (i = 0; i < self->bufsize; i++)
231 {
232 self->data[i] = 0.0;
233 self->pointerPos += inc;
234
235 for (j = 0; j < self->ngrains; j++)
236 {
237 ppos = self->pointerPos + self->gphase[j];
238
239 if (ppos >= 1.0)
240 {
241 ppos -= 1.0;
242 }
243
244 // compute envelope
245 index = ppos * envsize;
246 ipart = (T_SIZE_T)index;
247 fpart = index - ipart;
248 x = envlist[ipart];
249 x1 = envlist[ipart + 1];
250 amp = x + (x1 - x) * fpart;
251
252 if (ppos < self->lastppos[j])
253 {
254 self->startPos[j] = pos[i];
255 }
256
257 self->lastppos[j] = ppos;
258
259 // compute sampling
260 index = ppos * self->gsize[j] + self->startPos[j];
261
262 if (index >= 0 && index < size)
263 {
264 ipart = (T_SIZE_T)index;
265 fpart = index - ipart;
266 x = tablelist[ipart];
267 x1 = tablelist[ipart + 1];
268 val = x + (x1 - x) * fpart;
269 }
270 else
271 val = 0.0;
272
273 self->data[i] += (val * amp);
274 }
275
276 if (self->pointerPos < 0)
277 self->pointerPos += 1.0;
278 else if (self->pointerPos >= 1)
279 self->pointerPos -= 1.0;
280 }
281 }
282
283 static void
284 Granulator_transform_aai(Granulator *self)
285 {
286 MYFLT val, x, x1, inc, index, fpart, amp, ppos, frtosamps;
287 int i, j;
288 T_SIZE_T ipart;
289
290 MYFLT *tablelist = TableStream_getData((TableStream *)self->table);
291 T_SIZE_T size = TableStream_getSize((TableStream *)self->table);
292
293 MYFLT *envlist = TableStream_getData((TableStream *)self->env);
294 T_SIZE_T envsize = TableStream_getSize((TableStream *)self->env);
295
296 MYFLT *pit = Stream_getData((Stream *)self->pitch_stream);
297 MYFLT *pos = Stream_getData((Stream *)self->pos_stream);
298 MYFLT dur = PyFloat_AS_DOUBLE(self->dur);
299
300 frtosamps = (1.0 / self->basedur) / self->sr;
301
302 for (i = 0; i < self->bufsize; i++)
303 {
304 self->data[i] = 0.0;
305 inc = pit[i] * frtosamps;
306 self->pointerPos += inc;
307
308 for (j = 0; j < self->ngrains; j++)
309 {
310 ppos = self->pointerPos + self->gphase[j];
311
312 if (ppos >= 1.0)
313 {
314 ppos -= 1.0;
315 }
316
317 // compute envelope
318 index = ppos * envsize;
319 ipart = (T_SIZE_T)index;
320 fpart = index - ipart;
321 x = envlist[ipart];
322 x1 = envlist[ipart + 1];
323 amp = x + (x1 - x) * fpart;
324
325 if (ppos < self->lastppos[j])
326 {
327 self->startPos[j] = pos[i];
328 self->gsize[j] = dur * self->sr * self->srScale;
329 }
330
331 self->lastppos[j] = ppos;
332
333 // compute sampling
334 index = ppos * self->gsize[j] + self->startPos[j];
335
336 if (index >= 0 && index < size)
337 {
338 ipart = (T_SIZE_T)index;
339 fpart = index - ipart;
340 x = tablelist[ipart];
341 x1 = tablelist[ipart + 1];
342 val = x + (x1 - x) * fpart;
343 }
344 else
345 val = 0.0;
346
347 self->data[i] += (val * amp);
348 }
349
350 if (self->pointerPos < 0)
351 self->pointerPos += 1.0;
352 else if (self->pointerPos >= 1)
353 self->pointerPos -= 1.0;
354 }
355 }
356
357 static void
358 Granulator_transform_iia(Granulator *self)
359 {
360 MYFLT val, x, x1, inc, index, fpart, amp, ppos;
361 int i, j;
362 T_SIZE_T ipart;
363
364 MYFLT *tablelist = TableStream_getData((TableStream *)self->table);
365 T_SIZE_T size = TableStream_getSize((TableStream *)self->table);
366
367 MYFLT *envlist = TableStream_getData((TableStream *)self->env);
368 T_SIZE_T envsize = TableStream_getSize((TableStream *)self->env);
369
370 MYFLT pit = PyFloat_AS_DOUBLE(self->pitch);
371 MYFLT pos = PyFloat_AS_DOUBLE(self->pos);
372 MYFLT *dur = Stream_getData((Stream *)self->dur_stream);
373
374 inc = pit * (1.0 / self->basedur) / self->sr;
375
376 for (i = 0; i < self->bufsize; i++)
377 {
378 self->data[i] = 0.0;
379 self->pointerPos += inc;
380
381 for (j = 0; j < self->ngrains; j++)
382 {
383 ppos = self->pointerPos + self->gphase[j];
384
385 if (ppos >= 1.0)
386 {
387 ppos -= 1.0;
388 }
389
390 // compute envelope
391 index = ppos * envsize;
392 ipart = (T_SIZE_T)index;
393 fpart = index - ipart;
394 x = envlist[ipart];
395 x1 = envlist[ipart + 1];
396 amp = x + (x1 - x) * fpart;
397
398 if (ppos < self->lastppos[j])
399 {
400 self->startPos[j] = pos;
401 self->gsize[j] = dur[i] * self->sr * self->srScale;
402 }
403
404 self->lastppos[j] = ppos;
405
406 // compute sampling
407 index = ppos * self->gsize[j] + self->startPos[j];
408
409 if (index >= 0 && index < size)
410 {
411 ipart = (T_SIZE_T)index;
412 fpart = index - ipart;
413 x = tablelist[ipart];
414 x1 = tablelist[ipart + 1];
415 val = x + (x1 - x) * fpart;
416 }
417 else
418 val = 0.0;
419
420 self->data[i] += (val * amp);
421 }
422
423 if (self->pointerPos < 0)
424 self->pointerPos += 1.0;
425 else if (self->pointerPos >= 1)
426 self->pointerPos -= 1.0;
427 }
428 }
429
430 static void
431 Granulator_transform_aia(Granulator *self)
432 {
433 MYFLT val, x, x1, inc, index, fpart, amp, ppos, frtosamps;
434 int i, j;
435 T_SIZE_T ipart;
436
437 MYFLT *tablelist = TableStream_getData((TableStream *)self->table);
438 T_SIZE_T size = TableStream_getSize((TableStream *)self->table);
439
440 MYFLT *envlist = TableStream_getData((TableStream *)self->env);
441 T_SIZE_T envsize = TableStream_getSize((TableStream *)self->env);
442
443 MYFLT *pit = Stream_getData((Stream *)self->pitch_stream);
444 MYFLT pos = PyFloat_AS_DOUBLE(self->pos);
445 MYFLT *dur = Stream_getData((Stream *)self->dur_stream);
446
447 frtosamps = (1.0 / self->basedur) / self->sr;
448
449 for (i = 0; i < self->bufsize; i++)
450 {
451 self->data[i] = 0.0;
452 inc = pit[i] * frtosamps;
453 self->pointerPos += inc;
454
455 for (j = 0; j < self->ngrains; j++)
456 {
457 ppos = self->pointerPos + self->gphase[j];
458
459 if (ppos >= 1.0)
460 {
461 ppos -= 1.0;
462 }
463
464 // compute envelope
465 index = ppos * envsize;
466 ipart = (T_SIZE_T)index;
467 fpart = index - ipart;
468 x = envlist[ipart];
469 x1 = envlist[ipart + 1];
470 amp = x + (x1 - x) * fpart;
471
472 if (ppos < self->lastppos[j])
473 {
474 self->startPos[j] = pos;
475 self->gsize[j] = dur[i] * self->sr * self->srScale;
476 }
477
478 self->lastppos[j] = ppos;
479
480 // compute sampling
481 index = ppos * self->gsize[j] + self->startPos[j];
482
483 if (index >= 0 && index < size)
484 {
485 ipart = (T_SIZE_T)index;
486 fpart = index - ipart;
487 x = tablelist[ipart];
488 x1 = tablelist[ipart + 1];
489 val = x + (x1 - x) * fpart;
490 }
491 else
492 val = 0.0;
493
494 self->data[i] += (val * amp);
495 }
496
497 if (self->pointerPos < 0)
498 self->pointerPos += 1.0;
499 else if (self->pointerPos >= 1)
500 self->pointerPos -= 1.0;
501 }
502 }
503
504 static void
505 Granulator_transform_iaa(Granulator *self)
506 {
507 MYFLT val, x, x1, inc, index, fpart, amp, ppos;
508 int i, j;
509 T_SIZE_T ipart;
510
511 MYFLT *tablelist = TableStream_getData((TableStream *)self->table);
512 T_SIZE_T size = TableStream_getSize((TableStream *)self->table);
513
514 MYFLT *envlist = TableStream_getData((TableStream *)self->env);
515 T_SIZE_T envsize = TableStream_getSize((TableStream *)self->env);
516
517 MYFLT pit = PyFloat_AS_DOUBLE(self->pitch);
518 MYFLT *pos = Stream_getData((Stream *)self->pos_stream);
519 MYFLT *dur = Stream_getData((Stream *)self->dur_stream);
520
521 inc = pit * (1.0 / self->basedur) / self->sr;
522
523 for (i = 0; i < self->bufsize; i++)
524 {
525 self->data[i] = 0.0;
526 self->pointerPos += inc;
527
528 for (j = 0; j < self->ngrains; j++)
529 {
530 ppos = self->pointerPos + self->gphase[j];
531
532 if (ppos >= 1.0)
533 {
534 ppos -= 1.0;
535 }
536
537 // compute envelope
538 index = ppos * envsize;
539 ipart = (T_SIZE_T)index;
540 fpart = index - ipart;
541 x = envlist[ipart];
542 x1 = envlist[ipart + 1];
543 amp = x + (x1 - x) * fpart;
544
545 if (ppos < self->lastppos[j])
546 {
547 self->startPos[j] = pos[i];
548 self->gsize[j] = dur[i] * self->sr * self->srScale;
549 }
550
551 self->lastppos[j] = ppos;
552
553 // compute sampling
554 index = ppos * self->gsize[j] + self->startPos[j];
555
556 if (index >= 0 && index < size)
557 {
558 ipart = (T_SIZE_T)index;
559 fpart = index - ipart;
560 x = tablelist[ipart];
561 x1 = tablelist[ipart + 1];
562 val = x + (x1 - x) * fpart;
563 }
564 else
565 val = 0.0;
566
567 self->data[i] += (val * amp);
568 }
569
570 if (self->pointerPos < 0)
571 self->pointerPos += 1.0;
572 else if (self->pointerPos >= 1)
573 self->pointerPos -= 1.0;
574 }
575 }
576
577 static void
578 Granulator_transform_aaa(Granulator *self)
579 {
580 MYFLT val, x, x1, inc, index, fpart, amp, ppos, frtosamps;
581 int i, j;
582 T_SIZE_T ipart;
583
584 MYFLT *tablelist = TableStream_getData((TableStream *)self->table);
585 T_SIZE_T size = TableStream_getSize((TableStream *)self->table);
586
587 MYFLT *envlist = TableStream_getData((TableStream *)self->env);
588 T_SIZE_T envsize = TableStream_getSize((TableStream *)self->env);
589
590 MYFLT *pit = Stream_getData((Stream *)self->pitch_stream);
591 MYFLT *pos = Stream_getData((Stream *)self->pos_stream);
592 MYFLT *dur = Stream_getData((Stream *)self->dur_stream);
593
594 frtosamps = (1.0 / self->basedur) / self->sr;
595
596 for (i = 0; i < self->bufsize; i++)
597 {
598 self->data[i] = 0.0;
599 inc = pit[i] * frtosamps;
600 self->pointerPos += inc;
601
602 for (j = 0; j < self->ngrains; j++)
603 {
604 ppos = self->pointerPos + self->gphase[j];
605
606 if (ppos >= 1.0)
607 {
608 ppos -= 1.0;
609 }
610
611 // compute envelope
612 index = ppos * envsize;
613 ipart = (T_SIZE_T)index;
614 fpart = index - ipart;
615 x = envlist[ipart];
616 x1 = envlist[ipart + 1];
617 amp = x + (x1 - x) * fpart;
618
619 if (ppos < self->lastppos[j])
620 {
621 self->startPos[j] = pos[i];
622 self->gsize[j] = dur[i] * self->sr * self->srScale;
623 }
624
625 self->lastppos[j] = ppos;
626
627 // compute sampling
628 index = ppos * self->gsize[j] + self->startPos[j];
629
630 if (index >= 0 && index < size)
631 {
632 ipart = (T_SIZE_T)index;
633 fpart = index - ipart;
634 x = tablelist[ipart];
635 x1 = tablelist[ipart + 1];
636 val = x + (x1 - x) * fpart;
637 }
638 else
639 val = 0.0;
640
641 self->data[i] += (val * amp);
642 }
643
644 if (self->pointerPos < 0)
645 self->pointerPos += 1.0;
646 else if (self->pointerPos >= 1)
647 self->pointerPos -= 1.0;
648 }
649 }
650
651 static void Granulator_postprocessing_ii(Granulator *self) { POST_PROCESSING_II };
652 static void Granulator_postprocessing_ai(Granulator *self) { POST_PROCESSING_AI };
653 static void Granulator_postprocessing_ia(Granulator *self) { POST_PROCESSING_IA };
654 static void Granulator_postprocessing_aa(Granulator *self) { POST_PROCESSING_AA };
655 static void Granulator_postprocessing_ireva(Granulator *self) { POST_PROCESSING_IREVA };
656 static void Granulator_postprocessing_areva(Granulator *self) { POST_PROCESSING_AREVA };
657 static void Granulator_postprocessing_revai(Granulator *self) { POST_PROCESSING_REVAI };
658 static void Granulator_postprocessing_revaa(Granulator *self) { POST_PROCESSING_REVAA };
659 static void Granulator_postprocessing_revareva(Granulator *self) { POST_PROCESSING_REVAREVA };
660
661 static void
662 Granulator_setProcMode(Granulator *self)
663 {
664 int procmode, muladdmode;
665 procmode = self->modebuffer[2] + self->modebuffer[3] * 10 + self->modebuffer[4] * 100;
666 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
667
668 switch (procmode)
669 {
670 case 0:
671 self->proc_func_ptr = Granulator_transform_iii;
672 break;
673
674 case 1:
675 self->proc_func_ptr = Granulator_transform_aii;
676 break;
677
678 case 10:
679 self->proc_func_ptr = Granulator_transform_iai;
680 break;
681
682 case 11:
683 self->proc_func_ptr = Granulator_transform_aai;
684 break;
685
686 case 100:
687 self->proc_func_ptr = Granulator_transform_iia;
688 break;
689
690 case 101:
691 self->proc_func_ptr = Granulator_transform_aia;
692 break;
693
694 case 110:
695 self->proc_func_ptr = Granulator_transform_iaa;
696 break;
697
698 case 111:
699 self->proc_func_ptr = Granulator_transform_aaa;
700 break;
701 }
702
703 switch (muladdmode)
704 {
705 case 0:
706 self->muladd_func_ptr = Granulator_postprocessing_ii;
707 break;
708
709 case 1:
710 self->muladd_func_ptr = Granulator_postprocessing_ai;
711 break;
712
713 case 2:
714 self->muladd_func_ptr = Granulator_postprocessing_revai;
715 break;
716
717 case 10:
718 self->muladd_func_ptr = Granulator_postprocessing_ia;
719 break;
720
721 case 11:
722 self->muladd_func_ptr = Granulator_postprocessing_aa;
723 break;
724
725 case 12:
726 self->muladd_func_ptr = Granulator_postprocessing_revaa;
727 break;
728
729 case 20:
730 self->muladd_func_ptr = Granulator_postprocessing_ireva;
731 break;
732
733 case 21:
734 self->muladd_func_ptr = Granulator_postprocessing_areva;
735 break;
736
737 case 22:
738 self->muladd_func_ptr = Granulator_postprocessing_revareva;
739 break;
740 }
741 }
742
743 static void
744 Granulator_compute_next_data_frame(Granulator *self)
745 {
746 (*self->proc_func_ptr)(self);
747 (*self->muladd_func_ptr)(self);
748 }
749
750 static int
751 Granulator_traverse(Granulator *self, visitproc visit, void *arg)
752 {
753 pyo_VISIT
754 Py_VISIT(self->pitch);
755 Py_VISIT(self->pos);
756 Py_VISIT(self->dur);
757 return 0;
758 }
759
760 static int
761 Granulator_clear(Granulator *self)
762 {
763 pyo_CLEAR
764 Py_CLEAR(self->pitch);
765 Py_CLEAR(self->pos);
766 Py_CLEAR(self->dur);
767 return 0;
768 }
769
770 static void
771 Granulator_dealloc(Granulator* self)
772 {
773 pyo_DEALLOC
774 PyMem_RawFree(self->startPos);
775 PyMem_RawFree(self->gphase);
776 PyMem_RawFree(self->gsize);
777 PyMem_RawFree(self->lastppos);
778 Granulator_clear(self);
779 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
780 Py_TYPE(self)->tp_free((PyObject*)self);
781 }
782
783 static PyObject *
784 Granulator_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
785 {
786 int i;
787 MYFLT phase;
788 PyObject *tabletmp, *envtmp, *pitchtmp = NULL, *postmp = NULL, *durtmp = NULL, *multmp = NULL, *addtmp = NULL;
789 Granulator *self;
790 self = (Granulator *)type->tp_alloc(type, 0);
791
792 self->pitch = PyFloat_FromDouble(1);
793 self->pos = PyFloat_FromDouble(0.0);
794 self->dur = PyFloat_FromDouble(0.1);
795 self->ngrains = 8;
796 self->basedur = 0.1;
797 self->pointerPos = 1.0;
798 self->srScale = 1.0;
799 self->modebuffer[0] = 0;
800 self->modebuffer[1] = 0;
801 self->modebuffer[2] = 0;
802 self->modebuffer[3] = 0;
803 self->modebuffer[4] = 0;
804
805 INIT_OBJECT_COMMON
806 Stream_setFunctionPtr(self->stream, Granulator_compute_next_data_frame);
807 self->mode_func_ptr = Granulator_setProcMode;
808
809 static char *kwlist[] = {"table", "env", "pitch", "pos", "dur", "grains", "basedur", "mul", "add", NULL};
810
811 if (! PyArg_ParseTupleAndKeywords(args, kwds, TYPE_OO_OOOIFOO, kwlist, &tabletmp, &envtmp, &pitchtmp, &postmp, &durtmp, &self->ngrains, &self->basedur, &multmp, &addtmp))
812 Py_RETURN_NONE;
813
814 if ( PyObject_HasAttrString((PyObject *)tabletmp, "getTableStream") == 0 )
815 {
816 PyErr_SetString(PyExc_TypeError, "\"table\" argument of Granulator must be a PyoTableObject.\n");
817 Py_RETURN_NONE;
818 }
819
820 self->table = PyObject_CallMethod((PyObject *)tabletmp, "getTableStream", "");
821 self->srScale = TableStream_getSamplingRate((TableStream *)self->table) / self->sr;
822
823 if ( PyObject_HasAttrString((PyObject *)envtmp, "getTableStream") == 0 )
824 {
825 PyErr_SetString(PyExc_TypeError, "\"env\" argument of Granulator must be a PyoTableObject.\n");
826 Py_RETURN_NONE;
827 }
828
829 self->env = PyObject_CallMethod((PyObject *)envtmp, "getTableStream", "");
830
831 if (pitchtmp)
832 {
833 PyObject_CallMethod((PyObject *)self, "setPitch", "O", pitchtmp);
834 }
835
836 if (postmp)
837 {
838 PyObject_CallMethod((PyObject *)self, "setPos", "O", postmp);
839 }
840
841 if (durtmp)
842 {
843 PyObject_CallMethod((PyObject *)self, "setDur", "O", durtmp);
844 }
845
846 if (multmp)
847 {
848 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
849 }
850
851 if (addtmp)
852 {
853 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
854 }
855
856 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
857
858 self->startPos = (MYFLT *)PyMem_RawRealloc(self->startPos, self->ngrains * sizeof(MYFLT));
859 self->gsize = (MYFLT *)PyMem_RawRealloc(self->gsize, self->ngrains * sizeof(MYFLT));
860 self->gphase = (MYFLT *)PyMem_RawRealloc(self->gphase, self->ngrains * sizeof(MYFLT));
861 self->lastppos = (MYFLT *)PyMem_RawRealloc(self->lastppos, self->ngrains * sizeof(MYFLT));
862
863 Server_generateSeed((Server *)self->server, GRANULATOR_ID);
864
865 for (i = 0; i < self->ngrains; i++)
866 {
867 phase = ((MYFLT)i / self->ngrains) * (1.0 + ((RANDOM_UNIFORM * 2.0 - 1.0) * 0.01));
868
869 if (phase < 0.0)
870 phase = 0.0;
871 else if (phase >= 1.0)
872 phase -= 1.0;
873
874 self->gphase[i] = phase;
875 self->startPos[i] = self->gsize[i] = 0.0;
876 self->lastppos[i] = 1.0;
877 }
878
879 (*self->mode_func_ptr)(self);
880
881 return (PyObject *)self;
882 }
883
884 static PyObject * Granulator_getServer(Granulator* self) { GET_SERVER };
885 static PyObject * Granulator_getStream(Granulator* self) { GET_STREAM };
886 static PyObject * Granulator_setMul(Granulator *self, PyObject *arg) { SET_MUL };
887 static PyObject * Granulator_setAdd(Granulator *self, PyObject *arg) { SET_ADD };
888 static PyObject * Granulator_setSub(Granulator *self, PyObject *arg) { SET_SUB };
889 static PyObject * Granulator_setDiv(Granulator *self, PyObject *arg) { SET_DIV };
890
891 static PyObject * Granulator_play(Granulator *self, PyObject *args, PyObject *kwds) { PLAY };
892 static PyObject * Granulator_out(Granulator *self, PyObject *args, PyObject *kwds) { OUT };
893 static PyObject * Granulator_stop(Granulator *self, PyObject *args, PyObject *kwds) { STOP };
894
895 static PyObject * Granulator_multiply(Granulator *self, PyObject *arg) { MULTIPLY };
896 static PyObject * Granulator_inplace_multiply(Granulator *self, PyObject *arg) { INPLACE_MULTIPLY };
897 static PyObject * Granulator_add(Granulator *self, PyObject *arg) { ADD };
898 static PyObject * Granulator_inplace_add(Granulator *self, PyObject *arg) { INPLACE_ADD };
899 static PyObject * Granulator_sub(Granulator *self, PyObject *arg) { SUB };
900 static PyObject * Granulator_inplace_sub(Granulator *self, PyObject *arg) { INPLACE_SUB };
901 static PyObject * Granulator_div(Granulator *self, PyObject *arg) { DIV };
902 static PyObject * Granulator_inplace_div(Granulator *self, PyObject *arg) { INPLACE_DIV };
903
904 static PyObject * Granulator_setPitch(Granulator *self, PyObject *arg) { SET_PARAM(self->pitch, self->pitch_stream, 2); }
905 static PyObject * Granulator_setPos(Granulator *self, PyObject *arg) { SET_PARAM(self->pos, self->pos_stream, 3); }
906 static PyObject * Granulator_setDur(Granulator *self, PyObject *arg) { SET_PARAM(self->dur, self->dur_stream, 4); }
907
908 static PyObject *
909 Granulator_getTable(Granulator* self)
910 {
911 Py_INCREF(self->table);
912 return self->table;
913 };
914
915 static PyObject *
916 Granulator_setTable(Granulator *self, PyObject *arg)
917 {
918 ASSERT_ARG_NOT_NULL
919
920 Py_DECREF(self->table);
921 self->table = PyObject_CallMethod((PyObject *)arg, "getTableStream", "");
922
923 Py_RETURN_NONE;
924 }
925
926 static PyObject *
927 Granulator_getEnv(Granulator* self)
928 {
929 Py_INCREF(self->env);
930 return self->env;
931 };
932
933 static PyObject *
934 Granulator_setEnv(Granulator *self, PyObject *arg)
935 {
936 ASSERT_ARG_NOT_NULL
937
938 Py_DECREF(self->env);
939 self->env = PyObject_CallMethod((PyObject *)arg, "getTableStream", "");
940
941 Py_RETURN_NONE;
942 }
943
944 static PyObject *
945 Granulator_setBaseDur(Granulator *self, PyObject *arg)
946 {
947 if (arg != NULL)
948 self->basedur = PyFloat_AsDouble(arg);
949
950 Py_RETURN_NONE;
951 }
952
953 static PyObject *
954 Granulator_setGrains(Granulator *self, PyObject *arg)
955 {
956 int i;
957 MYFLT phase;
958
959 if (PyLong_Check(arg))
960 {
961 self->ngrains = PyLong_AsLong(arg);
962 self->startPos = (MYFLT *)PyMem_RawRealloc(self->startPos, self->ngrains * sizeof(MYFLT));
963 self->gsize = (MYFLT *)PyMem_RawRealloc(self->gsize, self->ngrains * sizeof(MYFLT));
964 self->gphase = (MYFLT *)PyMem_RawRealloc(self->gphase, self->ngrains * sizeof(MYFLT));
965 self->lastppos = (MYFLT *)PyMem_RawRealloc(self->lastppos, self->ngrains * sizeof(MYFLT));
966
967 for (i = 0; i < self->ngrains; i++)
968 {
969 phase = ((MYFLT)i / self->ngrains) * (1.0 + ((RANDOM_UNIFORM * 2.0 - 1.0) * 0.01));
970
971 if (phase < 0.0)
972 phase = 0.0;
973 else if (phase >= 1.0)
974 phase -= 1.0;
975
976 self->gphase[i] = phase;
977 self->startPos[i] = self->gsize[i] = 0.0;
978 self->lastppos[i] = 1.0;
979 }
980 }
981
982 Py_RETURN_NONE;
983 }
984
985 static PyMemberDef Granulator_members[] =
986 {
987 {"server", T_OBJECT_EX, offsetof(Granulator, server), 0, "Pyo server."},
988 {"stream", T_OBJECT_EX, offsetof(Granulator, stream), 0, "Stream object."},
989 {"table", T_OBJECT_EX, offsetof(Granulator, table), 0, "Sound table."},
990 {"env", T_OBJECT_EX, offsetof(Granulator, env), 0, "Envelope table."},
991 {"pitch", T_OBJECT_EX, offsetof(Granulator, pitch), 0, "Speed of the reading pointer."},
992 {"pos", T_OBJECT_EX, offsetof(Granulator, pos), 0, "Position in the sound table."},
993 {"dur", T_OBJECT_EX, offsetof(Granulator, dur), 0, "Duration of each grains."},
994 {"mul", T_OBJECT_EX, offsetof(Granulator, mul), 0, "Mul factor."},
995 {"add", T_OBJECT_EX, offsetof(Granulator, add), 0, "Add factor."},
996 {NULL} /* Sentinel */
997 };
998
999 static PyMethodDef Granulator_methods[] =
1000 {
1001 {"getTable", (PyCFunction)Granulator_getTable, METH_NOARGS, "Returns sound table object."},
1002 {"setTable", (PyCFunction)Granulator_setTable, METH_O, "Sets sound table."},
1003 {"getEnv", (PyCFunction)Granulator_getEnv, METH_NOARGS, "Returns envelope table object."},
1004 {"setEnv", (PyCFunction)Granulator_setEnv, METH_O, "Sets envelope table."},
1005 {"getServer", (PyCFunction)Granulator_getServer, METH_NOARGS, "Returns server object."},
1006 {"_getStream", (PyCFunction)Granulator_getStream, METH_NOARGS, "Returns stream object."},
1007 {"play", (PyCFunction)Granulator_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1008 {"out", (PyCFunction)Granulator_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
1009 {"stop", (PyCFunction)Granulator_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1010 {"setPitch", (PyCFunction)Granulator_setPitch, METH_O, "Sets global pitch factor."},
1011 {"setPos", (PyCFunction)Granulator_setPos, METH_O, "Sets position in the sound table."},
1012 {"setDur", (PyCFunction)Granulator_setDur, METH_O, "Sets the grain duration."},
1013 {"setBaseDur", (PyCFunction)Granulator_setBaseDur, METH_O, "Sets the grain base duration."},
1014 {"setGrains", (PyCFunction)Granulator_setGrains, METH_O, "Sets the number of grains."},
1015 {"setMul", (PyCFunction)Granulator_setMul, METH_O, "Sets granulator mul factor."},
1016 {"setAdd", (PyCFunction)Granulator_setAdd, METH_O, "Sets granulator add factor."},
1017 {"setSub", (PyCFunction)Granulator_setSub, METH_O, "Sets inverse add factor."},
1018 {"setDiv", (PyCFunction)Granulator_setDiv, METH_O, "Sets inverse mul factor."},
1019 {NULL} /* Sentinel */
1020 };
1021
1022 static PyNumberMethods Granulator_as_number =
1023 {
1024 (binaryfunc)Granulator_add, /*nb_add*/
1025 (binaryfunc)Granulator_sub, /*nb_subtract*/
1026 (binaryfunc)Granulator_multiply, /*nb_multiply*/
1027 0, /*nb_remainder*/
1028 0, /*nb_divmod*/
1029 0, /*nb_power*/
1030 0, /*nb_neg*/
1031 0, /*nb_pos*/
1032 0, /*(unaryfunc)array_abs,*/
1033 0, /*nb_nonzero*/
1034 0, /*nb_invert*/
1035 0, /*nb_lshift*/
1036 0, /*nb_rshift*/
1037 0, /*nb_and*/
1038 0, /*nb_xor*/
1039 0, /*nb_or*/
1040 0, /*nb_int*/
1041 0, /*nb_long*/
1042 0, /*nb_float*/
1043 (binaryfunc)Granulator_inplace_add, /*inplace_add*/
1044 (binaryfunc)Granulator_inplace_sub, /*inplace_subtract*/
1045 (binaryfunc)Granulator_inplace_multiply, /*inplace_multiply*/
1046 0, /*inplace_remainder*/
1047 0, /*inplace_power*/
1048 0, /*inplace_lshift*/
1049 0, /*inplace_rshift*/
1050 0, /*inplace_and*/
1051 0, /*inplace_xor*/
1052 0, /*inplace_or*/
1053 0, /*nb_floor_divide*/
1054 (binaryfunc)Granulator_div, /*nb_true_divide*/
1055 0, /*nb_inplace_floor_divide*/
1056 (binaryfunc)Granulator_inplace_div, /*nb_inplace_true_divide*/
1057 0, /* nb_index */
1058 };
1059
1060 PyTypeObject GranulatorType =
1061 {
1062 PyVarObject_HEAD_INIT(NULL, 0)
1063 "_pyo.Granulator_base", /*tp_name*/
1064 sizeof(Granulator), /*tp_basicpitch*/
1065 0, /*tp_itempitch*/
1066 (destructor)Granulator_dealloc, /*tp_dealloc*/
1067 0, /*tp_print*/
1068 0, /*tp_getattr*/
1069 0, /*tp_setattr*/
1070 0, /*tp_as_async (tp_compare in Python 2)*/
1071 0, /*tp_repr*/
1072 &Granulator_as_number, /*tp_as_number*/
1073 0, /*tp_as_sequence*/
1074 0, /*tp_as_mapping*/
1075 0, /*tp_hash */
1076 0, /*tp_call*/
1077 0, /*tp_str*/
1078 0, /*tp_getattro*/
1079 0, /*tp_setattro*/
1080 0, /*tp_as_buffer*/
1081 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1082 "Granulator objects. Accumulation of multiples grains of sound.", /* tp_doc */
1083 (traverseproc)Granulator_traverse, /* tp_traverse */
1084 (inquiry)Granulator_clear, /* tp_clear */
1085 0, /* tp_richcompare */
1086 0, /* tp_weaklistoffset */
1087 0, /* tp_iter */
1088 0, /* tp_iternext */
1089 Granulator_methods, /* tp_methods */
1090 Granulator_members, /* tp_members */
1091 0, /* tp_getset */
1092 0, /* tp_base */
1093 0, /* tp_dict */
1094 0, /* tp_descr_get */
1095 0, /* tp_descr_set */
1096 0, /* tp_dictoffset */
1097 0, /* tp_init */
1098 0, /* tp_alloc */
1099 Granulator_new, /* tp_new */
1100 };
1101
1102 typedef struct
1103 {
1104 pyo_audio_HEAD
1105 PyObject *table;
1106 PyObject *pitch;
1107 Stream *pitch_stream;
1108 PyObject *start;
1109 Stream *start_stream;
1110 PyObject *dur;
1111 Stream *dur_stream;
1112 PyObject *xfade;
1113 Stream *xfade_stream;
1114 MYFLT *trigsBuffer;
1115 TriggerStream *trig_stream;
1116 MYFLT *time_buffer;
1117 int xfadeshape;
1118 int startfromloop;
1119 int init;
1120 int mode[2]; /* 0 = no loop, 1 = forward, 2 = backward, 3 = back-and-forth */
1121 int tmpmode;
1122 int direction[2];
1123 double pointerPos[2];
1124 double loopDuration;
1125 int current;
1126 int active[2];
1127 long loopstart[2];
1128 long loopend[2];
1129 long crossfadedur[2];
1130 MYFLT crossfadescaling[2];
1131 long minfadepoint[2];
1132 long maxfadepoint[2];
1133 MYFLT *fader;
1134 int interp; /* 0 = default to 2, 1 = nointerp, 2 = linear, 3 = cos, 4 = cubic */
1135 MYFLT (*interp_func_ptr)(MYFLT *, T_SIZE_T, MYFLT, T_SIZE_T);
1136 int modebuffer[6];
1137 int autosmooth;
1138 int appendfade;
1139 int fadeinseconds;
1140 MYFLT lastpitch;
1141 // sample memories
1142 MYFLT y1;
1143 MYFLT y2;
1144 // variables
1145 MYFLT c1;
1146 } Looper;
1147
1148 static void
1149 Looper_endloop(Looper *self)
1150 {
1151 int which = self->current;
1152
1153 switch (self->mode[which])
1154 {
1155 case 0:
1156 self->maxfadepoint[which] = self->pointerPos[which];
1157 self->loopend[which] = self->maxfadepoint[which] + self->crossfadedur[which];
1158 break;
1159
1160 case 1:
1161 self->maxfadepoint[which] = self->pointerPos[which];
1162 self->loopend[which] = self->maxfadepoint[which] + self->crossfadedur[which];
1163 break;
1164
1165 case 2:
1166 self->maxfadepoint[which] = self->pointerPos[which];
1167 self->loopend[which] = self->maxfadepoint[which] - self->crossfadedur[which];
1168 break;
1169
1170 case 3:
1171 if (self->direction[which] == 0)
1172 {
1173 self->maxfadepoint[which] = self->pointerPos[which];
1174 self->loopend[which] = self->maxfadepoint[which] + self->crossfadedur[which];
1175 }
1176 else
1177 {
1178 self->maxfadepoint[which] = self->pointerPos[which];
1179 self->loopend[which] = self->maxfadepoint[which] - self->crossfadedur[which];
1180 }
1181
1182 break;
1183 }
1184 }
1185
1186 static void
1187 Looper_reset(Looper *self, int x, int which, int init)
1188 {
1189 MYFLT start, dur, xfade;
1190
1191 T_SIZE_T size = TableStream_getSize((TableStream *)self->table) - 1;
1192 double tableSr = TableStream_getSamplingRate((TableStream *)self->table);
1193
1194 if (self->modebuffer[3] == 0)
1195 start = PyFloat_AS_DOUBLE(self->start);
1196 else
1197 start = Stream_getData((Stream *)self->start_stream)[x];
1198
1199 if (self->modebuffer[4] == 0)
1200 dur = PyFloat_AS_DOUBLE(self->dur);
1201 else
1202 dur = Stream_getData((Stream *)self->dur_stream)[x];
1203
1204 if (self->modebuffer[5] == 0)
1205 xfade = PyFloat_AS_DOUBLE(self->xfade);
1206 else
1207 xfade = Stream_getData((Stream *)self->xfade_stream)[x];
1208
1209 if (start < 0.0)
1210 start = 0.0;
1211 else if (start > (size / tableSr))
1212 start = (MYFLT)(size / tableSr);
1213
1214 if (dur < 0.001)
1215 dur = 0.001;
1216
1217 if (self->fadeinseconds == 1)
1218 xfade = xfade / dur * 100.0;
1219
1220 if (xfade < 0.0)
1221 xfade = 0.0;
1222 else if (xfade > 50.0)
1223 xfade = 50.0;
1224
1225 if (self->xfadeshape == 0)
1226 self->fader = LOOPER_LINEAR_FADE;
1227 else if (self->xfadeshape == 1)
1228 self->fader = LOOPER_POWER_FADE;
1229 else if (self->xfadeshape == 2)
1230 self->fader = LOOPER_SIGMOID_FADE;
1231 else
1232 self->fader = LOOPER_LINEAR_FADE;
1233
1234 self->current = which;
1235
1236 if (self->tmpmode != self->mode[which])
1237 self->mode[which] = self->tmpmode;
1238
1239 if (init == 0)
1240 self->trigsBuffer[x] = 1.0;
1241
1242 switch (self->mode[which])
1243 {
1244 case 0:
1245 self->loopstart[which] = 0;
1246 self->loopend[which] = (long)size;
1247 self->crossfadedur[which] = (long)((self->loopend[which] - self->loopstart[which]) * xfade * 0.01);
1248
1249 if (self->crossfadedur[which] < 1)
1250 self->crossfadedur[which] = 1;
1251
1252 self->crossfadescaling[which] = 1.0 / self->crossfadedur[which] * 512.0;
1253
1254 if (self->appendfade == 1)
1255 self->loopend[which] += self->crossfadedur[which];
1256
1257 if (init == 1 && self->startfromloop == 0)
1258 {
1259 self->minfadepoint[which] = self->crossfadedur[which];
1260 self->maxfadepoint[which] = self->loopend[which] - self->crossfadedur[which];
1261 self->pointerPos[which] = self->loopstart[which] = 0.0;
1262 }
1263 else
1264 {
1265 self->minfadepoint[which] = self->loopstart[which] + self->crossfadedur[which];
1266 self->maxfadepoint[which] = self->loopend[which] - self->crossfadedur[which];
1267 self->pointerPos[which] = self->loopstart[which];
1268 }
1269
1270 self->loopDuration = self->maxfadepoint[which] - self->loopstart[which];
1271 break;
1272
1273 case 1:
1274 self->loopstart[which] = (long)(start * tableSr);
1275 self->loopend[which] = (long)((start + dur) * tableSr);
1276 self->crossfadedur[which] = (long)((self->loopend[which] - self->loopstart[which]) * xfade * 0.01);
1277
1278 if (self->crossfadedur[which] < 1)
1279 self->crossfadedur[which] = 1;
1280
1281 self->crossfadescaling[which] = 1.0 / self->crossfadedur[which] * 512.0;
1282
1283 if (self->appendfade == 1)
1284 self->loopend[which] += self->crossfadedur[which];
1285
1286 if (init == 1 && self->startfromloop == 0)
1287 {
1288 self->minfadepoint[which] = self->crossfadedur[which];
1289 self->maxfadepoint[which] = self->loopend[which] - self->crossfadedur[which];
1290 self->pointerPos[which] = self->loopstart[which] = 0.0;
1291 }
1292 else
1293 {
1294 self->minfadepoint[which] = self->loopstart[which] + self->crossfadedur[which];
1295 self->maxfadepoint[which] = self->loopend[which] - self->crossfadedur[which];
1296 self->pointerPos[which] = self->loopstart[which];
1297 }
1298
1299 self->loopDuration = self->maxfadepoint[which] - self->loopstart[which];
1300 break;
1301
1302 case 2:
1303 self->loopstart[which] = (long)((start + dur) * tableSr);
1304 self->loopend[which] = (long)(start * tableSr);
1305 self->crossfadedur[which] = (long)((self->loopstart[which] - self->loopend[which]) * xfade * 0.01);
1306
1307 if (self->crossfadedur[which] < 1)
1308 self->crossfadedur[which] = 1;
1309
1310 self->crossfadescaling[which] = 1.0 / self->crossfadedur[which] * 512.0;
1311
1312 if (self->appendfade == 1)
1313 self->loopend[which] -= self->crossfadedur[which];
1314
1315 if (init == 1 && self->startfromloop == 0)
1316 {
1317 self->minfadepoint[which] = size - self->crossfadedur[which];
1318 self->maxfadepoint[which] = self->loopend[which] + self->crossfadedur[which];
1319 self->pointerPos[which] = self->loopstart[which] = (MYFLT)size;
1320 }
1321 else
1322 {
1323 self->minfadepoint[which] = self->loopstart[which] - self->crossfadedur[which];
1324 self->maxfadepoint[which] = self->loopend[which] + self->crossfadedur[which];
1325 self->pointerPos[which] = self->loopstart[which];
1326 }
1327
1328 self->loopDuration = self->loopstart[which] - self->maxfadepoint[which];
1329 break;
1330
1331 case 3:
1332 if (self->direction[1 - which] == 0 && init == 0)
1333 {
1334 self->direction[which] = 1;
1335 self->loopstart[which] = (long)((start + dur) * tableSr);
1336 self->loopend[which] = (long)(start * tableSr);
1337 self->crossfadedur[which] = (long)((self->loopstart[which] - self->loopend[which]) * xfade * 0.01);
1338
1339 if (self->crossfadedur[which] < 1)
1340 self->crossfadedur[which] = 1;
1341
1342 self->crossfadescaling[which] = 1.0 / self->crossfadedur[which] * 512.0;
1343
1344 if (self->appendfade == 1)
1345 self->loopend[which] -= self->crossfadedur[which];
1346
1347 self->minfadepoint[which] = self->loopstart[which] - self->crossfadedur[which];
1348 self->maxfadepoint[which] = self->loopend[which] + self->crossfadedur[which];
1349 self->pointerPos[which] = self->loopstart[which];
1350 self->loopDuration = self->loopstart[which] - self->maxfadepoint[which];
1351 }
1352 else
1353 {
1354 self->direction[which] = 0;
1355 self->loopstart[which] = (long)(start * tableSr);
1356 self->loopend[which] = (long)((start + dur) * tableSr);
1357 self->crossfadedur[which] = (long)((self->loopend[which] - self->loopstart[which]) * xfade * 0.01);
1358
1359 if (self->crossfadedur[which] < 1)
1360 self->crossfadedur[which] = 1;
1361
1362 self->crossfadescaling[which] = 1.0 / self->crossfadedur[which] * 512.0;
1363
1364 if (self->appendfade == 1)
1365 self->loopend[which] += self->crossfadedur[which];
1366
1367 if (init == 1 && self->startfromloop == 0)
1368 {
1369 self->minfadepoint[which] = self->crossfadedur[which];
1370 self->maxfadepoint[which] = self->loopend[which] - self->crossfadedur[which];
1371 self->pointerPos[which] = self->loopstart[which] = 0.0;
1372 }
1373 else
1374 {
1375 self->minfadepoint[which] = self->loopstart[which] + self->crossfadedur[which];
1376 self->maxfadepoint[which] = self->loopend[which] - self->crossfadedur[which];
1377 self->pointerPos[which] = self->loopstart[which];
1378 }
1379
1380 self->loopDuration = self->maxfadepoint[which] - self->loopstart[which];
1381 }
1382
1383 break;
1384 }
1385
1386 self->active[which] = 1;
1387 }
1388
1389 static MYFLT
1390 Looper_get_amp(Looper *self, MYFLT pos)
1391 {
1392 int ipart = (int)pos;
1393 return self->fader[ipart] + (self->fader[ipart + 1] - self->fader[ipart]) * (pos - ipart);
1394 }
1395
1396 static void
1397 Looper_transform_i(Looper *self)
1398 {
1399 MYFLT fpart, amp, fr;
1400 double pit;
1401 int i, j, k;
1402 T_SIZE_T ipart;
1403
1404 MYFLT *tablelist = TableStream_getData((TableStream *)self->table);
1405 T_SIZE_T size = TableStream_getSize((TableStream *)self->table);
1406 double tableSr = TableStream_getSamplingRate((TableStream *)self->table);
1407
1408 MYFLT pitval = PyFloat_AS_DOUBLE(self->pitch);
1409
1410 if (pitval < 0.0)
1411 pitval = 0.0;
1412
1413 pit = pitval * tableSr / self->sr;
1414
1415 for (i = 0; i < self->bufsize; i++)
1416 {
1417 self->trigsBuffer[i] = 0.0;
1418 }
1419
1420 if (self->active[0] == 0 && self->active[1] == 0)
1421 {
1422 Looper_reset(self, 0, 0, 1);
1423 }
1424
1425 for (i = 0; i < self->bufsize; i++)
1426 {
1427 self->data[i] = 0.0;
1428
1429 for (j = 0; j < 2; j++)
1430 {
1431 if (self->active[j] == 1)
1432 {
1433 switch (self->mode[j])
1434 {
1435 case 0:
1436 if (self->pointerPos[j] >= 0 && self->pointerPos[j] < size)
1437 {
1438 if (self->pointerPos[j] < self->minfadepoint[j])
1439 {
1440 fpart = (self->pointerPos[j] - self->loopstart[j]) * self->crossfadescaling[j];
1441 amp = Looper_get_amp(self, fpart);
1442 }
1443 else if (self->pointerPos[j] > self->maxfadepoint[j])
1444 {
1445 fpart = (self->loopend[j] - self->pointerPos[j]) * self->crossfadescaling[j];
1446 amp = Looper_get_amp(self, fpart);
1447 }
1448 else
1449 amp = 1.0;
1450
1451 ipart = (T_SIZE_T)self->pointerPos[j];
1452 fpart = self->pointerPos[j] - ipart;
1453 self->data[i] += (*self->interp_func_ptr)(tablelist, ipart, fpart, size) * amp;
1454 }
1455
1456 self->pointerPos[j] += pit;
1457
1458 if (self->pointerPos[j] < 0.0)
1459 {
1460 self->pointerPos[j] = self->time_buffer[i] = 0.0;
1461 }
1462 else if (self->pointerPos[j] <= self->maxfadepoint[j])
1463 {
1464 self->time_buffer[i] = (self->pointerPos[j] - self->loopstart[j]) / self->loopDuration;
1465 }
1466 else if (self->pointerPos[j] >= self->loopend[j])
1467 {
1468 self->active[j] = 0;
1469
1470 for (k = 0; k < self->bufsize; k++)
1471 {
1472 self->time_buffer[k] = 0.0;
1473 }
1474
1475 PyObject_CallMethod((PyObject *)self, "stop", NULL);
1476 }
1477 else
1478 {
1479 self->time_buffer[i] = 1.0;
1480 }
1481
1482 break;
1483
1484 case 1:
1485 if (self->pointerPos[j] >= 0 && self->pointerPos[j] < size)
1486 {
1487 if (self->pointerPos[j] < self->minfadepoint[j])
1488 {
1489 fpart = (self->pointerPos[j] - self->loopstart[j]) * self->crossfadescaling[j];
1490 amp = Looper_get_amp(self, fpart);
1491 }
1492 else if (self->pointerPos[j] > self->maxfadepoint[j])
1493 {
1494 fpart = (self->loopend[j] - self->pointerPos[j]) * self->crossfadescaling[j];
1495 amp = Looper_get_amp(self, fpart);
1496 }
1497 else
1498 amp = 1.0;
1499
1500 ipart = (T_SIZE_T)self->pointerPos[j];
1501 fpart = self->pointerPos[j] - ipart;
1502 self->data[i] += (*self->interp_func_ptr)(tablelist, ipart, fpart, size) * amp;
1503 }
1504
1505 self->pointerPos[j] += pit;
1506
1507 if (self->pointerPos[j] < 0.0)
1508 {
1509 self->pointerPos[j] = self->time_buffer[i] = 0.0;
1510 }
1511 else if (self->pointerPos[j] <= self->maxfadepoint[j])
1512 {
1513 self->time_buffer[i] = (self->pointerPos[j] - self->loopstart[j]) / self->loopDuration;
1514 }
1515 else if (self->pointerPos[j] > self->maxfadepoint[j] && self->active[1 - j] == 0)
1516 Looper_reset(self, i, 1 - j, 0);
1517
1518 if (self->pointerPos[j] >= self->loopend[j])
1519 self->active[j] = 0;
1520
1521 break;
1522
1523 case 2:
1524 if (self->pointerPos[j] >= 0 && self->pointerPos[j] < size)
1525 {
1526 if (self->pointerPos[j] > self->minfadepoint[j])
1527 {
1528 fpart = (self->loopstart[j] - self->pointerPos[j]) * self->crossfadescaling[j];
1529 amp = Looper_get_amp(self, fpart);
1530 }
1531 else if (self->pointerPos[j] < self->maxfadepoint[j])
1532 {
1533 fpart = (self->pointerPos[j] - self->loopend[j]) * self->crossfadescaling[j];
1534 amp = Looper_get_amp(self, fpart);
1535 }
1536 else
1537 amp = 1.0;
1538
1539 ipart = (T_SIZE_T)self->pointerPos[j];
1540 fpart = self->pointerPos[j] - ipart;
1541 self->data[i] += (*self->interp_func_ptr)(tablelist, ipart, fpart, size) * amp;
1542 }
1543
1544 self->pointerPos[j] -= pit;
1545
1546 if (self->pointerPos[j] >= size)
1547 {
1548 self->pointerPos[j] = size - 1;
1549 self->time_buffer[i] = 0.0;
1550 }
1551 else if (self->pointerPos[j] >= self->maxfadepoint[j])
1552 {
1553 self->time_buffer[i] = (self->loopstart[j] - self->pointerPos[j]) / self->loopDuration;
1554 }
1555 else if (self->pointerPos[j] < self->maxfadepoint[j] && self->active[1 - j] == 0)
1556 Looper_reset(self, i, 1 - j, 0);
1557
1558 if (self->pointerPos[j] <= self->loopend[j])
1559 self->active[j] = 0;
1560
1561 break;
1562
1563 case 3:
1564 if (self->direction[j] == 0)
1565 {
1566 if (self->pointerPos[j] >= 0 && self->pointerPos[j] < size)
1567 {
1568 if (self->pointerPos[j] < self->minfadepoint[j])
1569 {
1570 fpart = (self->pointerPos[j] - self->loopstart[j]) * self->crossfadescaling[j];
1571 amp = Looper_get_amp(self, fpart);
1572 }
1573 else if (self->pointerPos[j] > self->maxfadepoint[j])
1574 {
1575 fpart = (self->loopend[j] - self->pointerPos[j]) * self->crossfadescaling[j];
1576 amp = Looper_get_amp(self, fpart);
1577 }
1578 else
1579 amp = 1.0;
1580
1581 ipart = (T_SIZE_T)self->pointerPos[j];
1582 fpart = self->pointerPos[j] - ipart;
1583 self->data[i] += (*self->interp_func_ptr)(tablelist, ipart, fpart, size) * amp;
1584 }
1585
1586 self->pointerPos[j] += pit;
1587
1588 if (self->pointerPos[j] < 0.0)
1589 {
1590 self->pointerPos[j] = self->time_buffer[i] = 0.0;
1591 }
1592 else if (self->pointerPos[j] <= self->maxfadepoint[j])
1593 {
1594 self->time_buffer[i] = (self->pointerPos[j] - self->loopstart[j]) / self->loopDuration;
1595 }
1596 else if (self->pointerPos[j] > self->maxfadepoint[j] && self->active[1 - j] == 0)
1597 Looper_reset(self, i, 1 - j, 0);
1598
1599 if (self->pointerPos[j] >= self->loopend[j])
1600 self->active[j] = 0;
1601 }
1602 else
1603 {
1604 if (self->pointerPos[j] >= 0 && self->pointerPos[j] < size)
1605 {
1606 if (self->pointerPos[j] > self->minfadepoint[j])
1607 {
1608 fpart = (self->loopstart[j] - self->pointerPos[j]) * self->crossfadescaling[j];
1609 amp = Looper_get_amp(self, fpart);
1610 }
1611 else if (self->pointerPos[j] < self->maxfadepoint[j])
1612 {
1613 fpart = (self->pointerPos[j] - self->loopend[j]) * self->crossfadescaling[j];
1614 amp = Looper_get_amp(self, fpart);
1615 }
1616 else
1617 amp = 1.0;
1618
1619 ipart = (T_SIZE_T)self->pointerPos[j];
1620 fpart = self->pointerPos[j] - ipart;
1621 self->data[i] += (*self->interp_func_ptr)(tablelist, ipart, fpart, size) * amp;
1622 }
1623
1624 self->pointerPos[j] -= pit;
1625
1626 if (self->pointerPos[j] >= size)
1627 {
1628 self->pointerPos[j] = size - 1;
1629 self->time_buffer[i] = 0.0;
1630 }
1631 else if (self->pointerPos[j] >= self->maxfadepoint[j])
1632 {
1633 self->time_buffer[i] = (self->loopstart[j] - self->pointerPos[j]) / self->loopDuration;
1634 }
1635 else if (self->pointerPos[j] < self->maxfadepoint[j] && self->active[1 - j] == 0)
1636 Looper_reset(self, i, 1 - j, 0);
1637
1638 if (self->pointerPos[j] <= self->loopend[j])
1639 self->active[j] = 0;
1640 }
1641
1642 break;
1643 } // end of switch
1644 }
1645 }
1646 }
1647
1648 /* Automatic smoothering of low transposition */
1649 if (self->autosmooth == 1 && pitval < 1.0)
1650 {
1651 if (self->lastpitch != pitval)
1652 {
1653 self->lastpitch = pitval;
1654
1655 if (pitval < 0.001)
1656 pitval = 0.001;
1657
1658 fr = pitval * self->sr * 0.45;
1659 self->c1 = MYEXP(-TWOPI * fr / self->sr);
1660 }
1661
1662 for (i = 0; i < self->bufsize; i++)
1663 {
1664 self->y1 = self->data[i] + (self->y1 - self->data[i]) * self->c1;
1665 self->y2 = self->y1 + (self->y2 - self->y1) * self->c1;
1666 self->data[i] = self->y2;
1667 }
1668 }
1669 }
1670
1671 static void
1672 Looper_transform_a(Looper *self)
1673 {
1674 MYFLT fpart, amp, fr, pitval;
1675 double pit, srFactor;
1676 int i, j, k;
1677 T_SIZE_T ipart;
1678
1679 MYFLT *tablelist = TableStream_getData((TableStream *)self->table);
1680 T_SIZE_T size = TableStream_getSize((TableStream *)self->table);
1681 double tableSr = TableStream_getSamplingRate((TableStream *)self->table);
1682
1683 MYFLT *pitch = Stream_getData((Stream *)self->pitch_stream);
1684
1685 srFactor = tableSr / self->sr;
1686
1687 if (self->active[0] == 0 && self->active[1] == 0)
1688 {
1689 Looper_reset(self, 0, 0, 1);
1690 }
1691
1692 for (i = 0; i < self->bufsize; i++)
1693 {
1694 self->trigsBuffer[i] = 0.0;
1695 }
1696
1697 for (i = 0; i < self->bufsize; i++)
1698 {
1699 self->data[i] = 0.0;
1700 pitval = pitch[i];
1701
1702 if (pitval < 0.0)
1703 pitval = 0.0;
1704
1705 pit = pitval * srFactor;
1706
1707 for (j = 0; j < 2; j++)
1708 {
1709 if (self->active[j] == 1)
1710 {
1711 switch (self->mode[j])
1712 {
1713 case 0:
1714 if (self->pointerPos[j] >= 0 && self->pointerPos[j] < size)
1715 {
1716 if (self->pointerPos[j] < self->minfadepoint[j])
1717 {
1718 fpart = (self->pointerPos[j] - self->loopstart[j]) * self->crossfadescaling[j];
1719 amp = Looper_get_amp(self, fpart);
1720 }
1721 else if (self->pointerPos[j] > self->maxfadepoint[j])
1722 {
1723 fpart = (self->loopend[j] - self->pointerPos[j]) * self->crossfadescaling[j];
1724 amp = Looper_get_amp(self, fpart);
1725 }
1726 else
1727 amp = 1.0;
1728
1729 ipart = (T_SIZE_T)self->pointerPos[j];
1730 fpart = self->pointerPos[j] - ipart;
1731 self->data[i] += (*self->interp_func_ptr)(tablelist, ipart, fpart, size) * amp;
1732 }
1733
1734 self->pointerPos[j] += pit;
1735
1736 if (self->pointerPos[j] < 0.0)
1737 {
1738 self->pointerPos[j] = self->time_buffer[i] = 0.0;
1739 }
1740 else if (self->pointerPos[j] <= self->maxfadepoint[j])
1741 {
1742 self->time_buffer[i] = (self->pointerPos[j] - self->loopstart[j]) / self->loopDuration;
1743 }
1744 else if (self->pointerPos[j] >= self->loopend[j])
1745 {
1746 self->active[j] = 0;
1747
1748 for (k = 0; k < self->bufsize; k++)
1749 {
1750 self->time_buffer[k] = 0.0;
1751 }
1752
1753 PyObject_CallMethod((PyObject *)self, "stop", NULL);
1754 }
1755
1756 break;
1757
1758 case 1:
1759 if (self->pointerPos[j] >= 0 && self->pointerPos[j] < size)
1760 {
1761 if (self->pointerPos[j] < self->minfadepoint[j])
1762 {
1763 fpart = (self->pointerPos[j] - self->loopstart[j]) * self->crossfadescaling[j];
1764 amp = Looper_get_amp(self, fpart);
1765 }
1766 else if (self->pointerPos[j] > self->maxfadepoint[j])
1767 {
1768 fpart = (self->loopend[j] - self->pointerPos[j]) * self->crossfadescaling[j];
1769 amp = Looper_get_amp(self, fpart);
1770 }
1771 else
1772 amp = 1.0;
1773
1774 ipart = (T_SIZE_T)self->pointerPos[j];
1775 fpart = self->pointerPos[j] - ipart;
1776 self->data[i] += (*self->interp_func_ptr)(tablelist, ipart, fpart, size) * amp;
1777 }
1778
1779 self->pointerPos[j] += pit;
1780
1781 if (self->pointerPos[j] < 0.0)
1782 {
1783 self->pointerPos[j] = self->time_buffer[i] = 0.0;
1784 }
1785 else if (self->pointerPos[j] <= self->maxfadepoint[j])
1786 {
1787 self->time_buffer[i] = (self->pointerPos[j] - self->loopstart[j]) / self->loopDuration;
1788 }
1789 else if (self->pointerPos[j] > self->maxfadepoint[j] && self->active[1 - j] == 0)
1790 Looper_reset(self, i, 1 - j, 0);
1791
1792 if (self->pointerPos[j] >= self->loopend[j])
1793 self->active[j] = 0;
1794
1795 break;
1796
1797 case 2:
1798 if (self->pointerPos[j] >= 0 && self->pointerPos[j] < size)
1799 {
1800 if (self->pointerPos[j] > self->minfadepoint[j])
1801 {
1802 fpart = (self->loopstart[j] - self->pointerPos[j]) * self->crossfadescaling[j];
1803 amp = Looper_get_amp(self, fpart);
1804 }
1805 else if (self->pointerPos[j] < self->maxfadepoint[j])
1806 {
1807 fpart = (self->pointerPos[j] - self->loopend[j]) * self->crossfadescaling[j];
1808 amp = Looper_get_amp(self, fpart);
1809 }
1810 else
1811 amp = 1.0;
1812
1813 ipart = (T_SIZE_T)self->pointerPos[j];
1814 fpart = self->pointerPos[j] - ipart;
1815 self->data[i] += (*self->interp_func_ptr)(tablelist, ipart, fpart, size) * amp;
1816 }
1817
1818 self->pointerPos[j] -= pit;
1819
1820 if (self->pointerPos[j] >= size)
1821 {
1822 self->pointerPos[j] = size - 1;
1823 self->time_buffer[i] = 0.0;
1824 }
1825 else if (self->pointerPos[j] >= self->maxfadepoint[j])
1826 {
1827 self->time_buffer[i] = (self->loopstart[j] - self->pointerPos[j]) / self->loopDuration;
1828 }
1829 else if (self->pointerPos[j] < self->maxfadepoint[j] && self->active[1 - j] == 0)
1830 Looper_reset(self, i, 1 - j, 0);
1831
1832 if (self->pointerPos[j] <= self->loopend[j])
1833 self->active[j] = 0;
1834
1835 break;
1836
1837 case 3:
1838 if (self->direction[j] == 0)
1839 {
1840 if (self->pointerPos[j] >= 0 && self->pointerPos[j] < size)
1841 {
1842 if (self->pointerPos[j] < self->minfadepoint[j])
1843 {
1844 fpart = (self->pointerPos[j] - self->loopstart[j]) * self->crossfadescaling[j];
1845 amp = Looper_get_amp(self, fpart);
1846 }
1847 else if (self->pointerPos[j] > self->maxfadepoint[j])
1848 {
1849 fpart = (self->loopend[j] - self->pointerPos[j]) * self->crossfadescaling[j];
1850 amp = Looper_get_amp(self, fpart);
1851 }
1852 else
1853 amp = 1.0;
1854
1855 ipart = (T_SIZE_T)self->pointerPos[j];
1856 fpart = self->pointerPos[j] - ipart;
1857 self->data[i] += (*self->interp_func_ptr)(tablelist, ipart, fpart, size) * amp;
1858 }
1859
1860 self->pointerPos[j] += pit;
1861
1862 if (self->pointerPos[j] < 0.0)
1863 {
1864 self->pointerPos[j] = self->time_buffer[i] = 0.0;
1865 }
1866 else if (self->pointerPos[j] <= self->maxfadepoint[j])
1867 {
1868 self->time_buffer[i] = (self->pointerPos[j] - self->loopstart[j]) / self->loopDuration;
1869 }
1870 else if (self->pointerPos[j] > self->maxfadepoint[j] && self->active[1 - j] == 0)
1871 Looper_reset(self, i, 1 - j, 0);
1872
1873 if (self->pointerPos[j] >= self->loopend[j])
1874 self->active[j] = 0;
1875 }
1876 else
1877 {
1878 if (self->pointerPos[j] >= 0 && self->pointerPos[j] < size)
1879 {
1880 if (self->pointerPos[j] > self->minfadepoint[j])
1881 {
1882 fpart = (self->loopstart[j] - self->pointerPos[j]) * self->crossfadescaling[j];
1883 amp = Looper_get_amp(self, fpart);
1884 }
1885 else if (self->pointerPos[j] < self->maxfadepoint[j])
1886 {
1887 fpart = (self->pointerPos[j] - self->loopend[j]) * self->crossfadescaling[j];
1888 amp = Looper_get_amp(self, fpart);
1889 }
1890 else
1891 amp = 1.0;
1892
1893 ipart = (T_SIZE_T)self->pointerPos[j];
1894 fpart = self->pointerPos[j] - ipart;
1895 self->data[i] += (*self->interp_func_ptr)(tablelist, ipart, fpart, size) * amp;
1896 }
1897
1898 self->pointerPos[j] -= pit;
1899
1900 if (self->pointerPos[j] >= size)
1901 {
1902 self->pointerPos[j] = size - 1;
1903 self->time_buffer[i] = 0.0;
1904 }
1905 else if (self->pointerPos[j] >= self->maxfadepoint[j])
1906 {
1907 self->time_buffer[i] = (self->loopstart[j] - self->pointerPos[j]) / self->loopDuration;
1908 }
1909 else if (self->pointerPos[j] < self->maxfadepoint[j] && self->active[1 - j] == 0)
1910 Looper_reset(self, i, 1 - j, 0);
1911
1912 if (self->pointerPos[j] <= self->loopend[j])
1913 self->active[j] = 0;
1914 }
1915
1916 break;
1917 } // end of switch
1918 }
1919 }
1920 }
1921
1922 /* Automatic smoothering of low transposition */
1923 if (self->autosmooth == 1)
1924 {
1925 for (i = 0; i < self->bufsize; i++)
1926 {
1927 pitval = pitch[i];
1928
1929 if (pitval < 0.001)
1930 pitval = 0.001;
1931
1932 if (pitval < 1.0)
1933 {
1934 if (self->lastpitch != pitval)
1935 {
1936 self->lastpitch = pitval;
1937 fr = pitval * self->sr * 0.45;
1938 self->c1 = MYEXP(-TWOPI * fr / self->sr);
1939 }
1940
1941 self->y1 = self->data[i] + (self->y1 - self->data[i]) * self->c1;
1942 self->y2 = self->y1 + (self->y2 - self->y1) * self->c1;
1943 self->data[i] = self->y2;
1944 }
1945 }
1946 }
1947 }
1948
1949 static void Looper_postprocessing_ii(Looper *self) { POST_PROCESSING_II };
1950 static void Looper_postprocessing_ai(Looper *self) { POST_PROCESSING_AI };
1951 static void Looper_postprocessing_ia(Looper *self) { POST_PROCESSING_IA };
1952 static void Looper_postprocessing_aa(Looper *self) { POST_PROCESSING_AA };
1953 static void Looper_postprocessing_ireva(Looper *self) { POST_PROCESSING_IREVA };
1954 static void Looper_postprocessing_areva(Looper *self) { POST_PROCESSING_AREVA };
1955 static void Looper_postprocessing_revai(Looper *self) { POST_PROCESSING_REVAI };
1956 static void Looper_postprocessing_revaa(Looper *self) { POST_PROCESSING_REVAA };
1957 static void Looper_postprocessing_revareva(Looper *self) { POST_PROCESSING_REVAREVA };
1958
1959 static void
1960 Looper_setProcMode(Looper *self)
1961 {
1962 int procmode, muladdmode;
1963 procmode = self->modebuffer[2];
1964 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
1965
1966 switch (procmode)
1967 {
1968 case 0:
1969 self->proc_func_ptr = Looper_transform_i;
1970 break;
1971
1972 case 1:
1973 self->proc_func_ptr = Looper_transform_a;
1974 break;
1975 }
1976
1977 switch (muladdmode)
1978 {
1979 case 0:
1980 self->muladd_func_ptr = Looper_postprocessing_ii;
1981 break;
1982
1983 case 1:
1984 self->muladd_func_ptr = Looper_postprocessing_ai;
1985 break;
1986
1987 case 2:
1988 self->muladd_func_ptr = Looper_postprocessing_revai;
1989 break;
1990
1991 case 10:
1992 self->muladd_func_ptr = Looper_postprocessing_ia;
1993 break;
1994
1995 case 11:
1996 self->muladd_func_ptr = Looper_postprocessing_aa;
1997 break;
1998
1999 case 12:
2000 self->muladd_func_ptr = Looper_postprocessing_revaa;
2001 break;
2002
2003 case 20:
2004 self->muladd_func_ptr = Looper_postprocessing_ireva;
2005 break;
2006
2007 case 21:
2008 self->muladd_func_ptr = Looper_postprocessing_areva;
2009 break;
2010
2011 case 22:
2012 self->muladd_func_ptr = Looper_postprocessing_revareva;
2013 break;
2014 }
2015 }
2016
2017 static void
2018 Looper_compute_next_data_frame(Looper *self)
2019 {
2020 (*self->proc_func_ptr)(self);
2021 (*self->muladd_func_ptr)(self);
2022 }
2023
2024 static MYFLT *
2025 Looper_getTimeBuffer(Looper *self)
2026 {
2027 return self->time_buffer;
2028 }
2029
2030 static int
2031 Looper_traverse(Looper *self, visitproc visit, void *arg)
2032 {
2033 pyo_VISIT
2034 Py_VISIT(self->pitch);
2035 Py_VISIT(self->start);
2036 Py_VISIT(self->dur);
2037 Py_VISIT(self->xfade);
2038 return 0;
2039 }
2040
2041 static int
2042 Looper_clear(Looper *self)
2043 {
2044 pyo_CLEAR
2045 Py_CLEAR(self->pitch);
2046 Py_CLEAR(self->start);
2047 Py_CLEAR(self->dur);
2048 Py_CLEAR(self->xfade);
2049 return 0;
2050 }
2051
2052 static void
2053 Looper_dealloc(Looper* self)
2054 {
2055 pyo_DEALLOC
2056 PyMem_RawFree(self->trigsBuffer);
2057 PyMem_RawFree(self->time_buffer);
2058 Looper_clear(self);
2059 Py_TYPE(self->trig_stream)->tp_free((PyObject*)self->trig_stream);
2060 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
2061 Py_TYPE(self)->tp_free((PyObject*)self);
2062 }
2063
2064 static PyObject *
2065 Looper_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2066 {
2067 int i;
2068 PyObject *tabletmp, *pitchtmp = NULL, *starttmp = NULL, *durtmp = NULL, *xfadetmp = NULL, *multmp = NULL, *addtmp = NULL;
2069 Looper *self;
2070 self = (Looper *)type->tp_alloc(type, 0);
2071
2072 self->pitch = PyFloat_FromDouble(1.0);
2073 self->start = PyFloat_FromDouble(0.0);
2074 self->dur = PyFloat_FromDouble(1.0);
2075 self->xfade = PyFloat_FromDouble(20.0);
2076 self->lastpitch = -1.0;
2077 self->autosmooth = 0;
2078 self->y1 = self->y2 = self->c1 = 0.0;
2079 self->xfadeshape = 0;
2080 self->startfromloop = 0;
2081 self->interp = 2;
2082 self->init = 1;
2083 self->appendfade = self->fadeinseconds = 0;
2084 self->mode[0] = self->mode[1] = self->tmpmode = 1;
2085 self->direction[0] = self->direction[1] = 0;
2086 self->pointerPos[0] = self->pointerPos[1] = 0.0;
2087 self->loopDuration = 0.0;
2088 self->active[0] = self->active[1] = self->current = 0;
2089 self->modebuffer[0] = 0;
2090 self->modebuffer[1] = 0;
2091 self->modebuffer[2] = 0;
2092 self->modebuffer[3] = 0;
2093 self->modebuffer[4] = 0;
2094
2095 INIT_OBJECT_COMMON
2096 Stream_setFunctionPtr(self->stream, Looper_compute_next_data_frame);
2097 self->mode_func_ptr = Looper_setProcMode;
2098
2099 static char *kwlist[] = {"table", "pitch", "start", "dur", "xfade", "mode", "xfadeshape", "startfromloop", "interp", "autosmooth", "mul", "add", NULL};
2100
2101 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOOOiiiiiOO", kwlist, &tabletmp, &pitchtmp, &starttmp, &durtmp, &xfadetmp, &self->tmpmode, &self->xfadeshape, &self->startfromloop, &self->interp, &self->autosmooth, &multmp, &addtmp))
2102 Py_RETURN_NONE;
2103
2104 if ( PyObject_HasAttrString((PyObject *)tabletmp, "getTableStream") == 0 )
2105 {
2106 PyErr_SetString(PyExc_TypeError, "\"table\" argument of Looper must be a PyoTableObject.\n");
2107 Py_RETURN_NONE;
2108 }
2109
2110 self->table = PyObject_CallMethod((PyObject *)tabletmp, "getTableStream", "");
2111
2112 if (pitchtmp)
2113 {
2114 PyObject_CallMethod((PyObject *)self, "setPitch", "O", pitchtmp);
2115 }
2116
2117 if (starttmp)
2118 {
2119 PyObject_CallMethod((PyObject *)self, "setStart", "O", starttmp);
2120 }
2121
2122 if (durtmp)
2123 {
2124 PyObject_CallMethod((PyObject *)self, "setDur", "O", durtmp);
2125 }
2126
2127 if (xfadetmp)
2128 {
2129 PyObject_CallMethod((PyObject *)self, "setXfade", "O", xfadetmp);
2130 }
2131
2132 if (multmp)
2133 {
2134 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
2135 }
2136
2137 if (addtmp)
2138 {
2139 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
2140 }
2141
2142 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
2143
2144 (*self->mode_func_ptr)(self);
2145
2146 self->trigsBuffer = (MYFLT *)PyMem_RawRealloc(self->trigsBuffer, self->bufsize * sizeof(MYFLT));
2147 self->time_buffer = (MYFLT *)PyMem_RawRealloc(self->time_buffer, self->bufsize * sizeof(MYFLT));
2148
2149 for (i = 0; i < self->bufsize; i++)
2150 {
2151 self->trigsBuffer[i] = self->time_buffer[i] = 0.0;
2152 }
2153
2154 MAKE_NEW_TRIGGER_STREAM(self->trig_stream, &TriggerStreamType, NULL);
2155 TriggerStream_setData(self->trig_stream, self->trigsBuffer);
2156
2157 if (self->tmpmode >= 0 && self->tmpmode < 4)
2158 self->mode[0] = self->mode[1] = self->tmpmode;
2159 else
2160 self->mode[0] = self->mode[1] = self->tmpmode = 1;
2161
2162 SET_INTERP_POINTER
2163
2164 return (PyObject *)self;
2165 }
2166
2167 static PyObject * Looper_getServer(Looper* self) { GET_SERVER };
2168 static PyObject * Looper_getStream(Looper* self) { GET_STREAM };
2169 static PyObject * Looper_getTriggerStream(Looper* self) { GET_TRIGGER_STREAM };
2170 static PyObject * Looper_setMul(Looper *self, PyObject *arg) { SET_MUL };
2171 static PyObject * Looper_setAdd(Looper *self, PyObject *arg) { SET_ADD };
2172 static PyObject * Looper_setSub(Looper *self, PyObject *arg) { SET_SUB };
2173 static PyObject * Looper_setDiv(Looper *self, PyObject *arg) { SET_DIV };
2174
2175 static PyObject * Looper_play(Looper *self, PyObject *args, PyObject *kwds) { PLAY };
2176 static PyObject * Looper_out(Looper *self, PyObject *args, PyObject *kwds) { OUT };
2177 static PyObject * Looper_stop(Looper *self, PyObject *args, PyObject *kwds) { STOP };
2178
2179 static PyObject * Looper_multiply(Looper *self, PyObject *arg) { MULTIPLY };
2180 static PyObject * Looper_inplace_multiply(Looper *self, PyObject *arg) { INPLACE_MULTIPLY };
2181 static PyObject * Looper_add(Looper *self, PyObject *arg) { ADD };
2182 static PyObject * Looper_inplace_add(Looper *self, PyObject *arg) { INPLACE_ADD };
2183 static PyObject * Looper_sub(Looper *self, PyObject *arg) { SUB };
2184 static PyObject * Looper_inplace_sub(Looper *self, PyObject *arg) { INPLACE_SUB };
2185 static PyObject * Looper_div(Looper *self, PyObject *arg) { DIV };
2186 static PyObject * Looper_inplace_div(Looper *self, PyObject *arg) { INPLACE_DIV };
2187
2188 static PyObject * Looper_setPitch(Looper *self, PyObject *arg) { SET_PARAM(self->pitch, self->pitch_stream, 2); }
2189 static PyObject * Looper_setStart(Looper *self, PyObject *arg) { SET_PARAM(self->start, self->start_stream, 3); }
2190 static PyObject * Looper_setDur(Looper *self, PyObject *arg) { SET_PARAM(self->dur, self->dur_stream, 4); }
2191 static PyObject * Looper_setXfade(Looper *self, PyObject *arg) { SET_PARAM(self->xfade, self->xfade_stream, 5); }
2192
2193 static PyObject *
2194 Looper_getTable(Looper* self)
2195 {
2196 Py_INCREF(self->table);
2197 return self->table;
2198 };
2199
2200 static PyObject *
2201 Looper_setTable(Looper *self, PyObject *arg)
2202 {
2203 ASSERT_ARG_NOT_NULL
2204
2205 Py_DECREF(self->table);
2206 self->table = PyObject_CallMethod((PyObject *)arg, "getTableStream", "");
2207
2208 Py_RETURN_NONE;
2209 }
2210
2211 static PyObject *
2212 Looper_setStartFromLoop(Looper *self, PyObject *arg)
2213 {
2214 ASSERT_ARG_NOT_NULL
2215
2216 if (PyLong_Check(arg))
2217 {
2218 self->startfromloop = PyLong_AsLong(arg);
2219 }
2220
2221 Py_RETURN_NONE;
2222 }
2223
2224 static PyObject *
2225 Looper_setXfadeShape(Looper *self, PyObject *arg)
2226 {
2227 ASSERT_ARG_NOT_NULL
2228
2229 if (PyLong_Check(arg))
2230 {
2231 self->xfadeshape = PyLong_AsLong(arg);
2232 }
2233
2234 Py_RETURN_NONE;
2235 }
2236
2237 static PyObject *
2238 Looper_setMode(Looper *self, PyObject *arg)
2239 {
2240 int tmp;
2241
2242 ASSERT_ARG_NOT_NULL
2243
2244 if (PyLong_Check(arg))
2245 {
2246 tmp = PyLong_AsLong(arg);
2247
2248 if (tmp >= 0 && tmp < 4)
2249 self->tmpmode = tmp;
2250 }
2251
2252 Py_RETURN_NONE;
2253 }
2254
2255 static PyObject *
2256 Looper_setInterp(Looper *self, PyObject *arg)
2257 {
2258 ASSERT_ARG_NOT_NULL
2259
2260 if (PyNumber_Check(arg))
2261 {
2262 self->interp = PyLong_AsLong(PyNumber_Long(arg));
2263 }
2264
2265 SET_INTERP_POINTER
2266
2267 Py_RETURN_NONE;
2268 }
2269
2270 static PyObject *
2271 Looper_setAutoSmooth(Looper *self, PyObject *arg)
2272 {
2273 ASSERT_ARG_NOT_NULL
2274
2275 if (PyLong_Check(arg))
2276 {
2277 self->autosmooth = PyLong_AsLong(arg);
2278 }
2279
2280 Py_RETURN_NONE;
2281 }
2282
2283 static PyObject *
2284 Looper_on_reset(Looper *self)
2285 {
2286 self->pointerPos[0] = self->pointerPos[1] = 0.0;
2287 self->active[0] = self->active[1] = 0;
2288 Py_RETURN_NONE;
2289 };
2290
2291 static PyObject *
2292 Looper_loopnow(Looper *self)
2293 {
2294 Looper_endloop(self);
2295 Py_RETURN_NONE;
2296 };
2297
2298 static PyObject *
2299 Looper_appendFadeTime(Looper *self, PyObject *arg)
2300 {
2301 ASSERT_ARG_NOT_NULL
2302
2303 if (PyLong_Check(arg))
2304 {
2305 self->appendfade = PyLong_AsLong(arg) == 0 ? 0 : 1;
2306 }
2307
2308 Py_RETURN_NONE;
2309 }
2310
2311 static PyObject *
2312 Looper_fadeInSeconds(Looper *self, PyObject *arg)
2313 {
2314 ASSERT_ARG_NOT_NULL
2315
2316 if (PyLong_Check(arg))
2317 {
2318 self->fadeinseconds = PyLong_AsLong(arg) == 0 ? 0 : 1;
2319 }
2320
2321 Py_RETURN_NONE;
2322 }
2323
2324 static PyMemberDef Looper_members[] =
2325 {
2326 {"server", T_OBJECT_EX, offsetof(Looper, server), 0, "Pyo server."},
2327 {"stream", T_OBJECT_EX, offsetof(Looper, stream), 0, "Stream object."},
2328 {"trig_stream", T_OBJECT_EX, offsetof(Looper, trig_stream), 0, "Trigger Stream object."},
2329 {"table", T_OBJECT_EX, offsetof(Looper, table), 0, "Sound table."},
2330 {"pitch", T_OBJECT_EX, offsetof(Looper, pitch), 0, "Speed of the reading pointer."},
2331 {"start", T_OBJECT_EX, offsetof(Looper, start), 0, "Position in the sound table."},
2332 {"dur", T_OBJECT_EX, offsetof(Looper, dur), 0, "Duration of each grains."},
2333 {"xfade", T_OBJECT_EX, offsetof(Looper, xfade), 0, "Crossfade duration in percent."},
2334 {"mul", T_OBJECT_EX, offsetof(Looper, mul), 0, "Mul factor."},
2335 {"add", T_OBJECT_EX, offsetof(Looper, add), 0, "Add factor."},
2336 {NULL} /* Sentinel */
2337 };
2338
2339 static PyMethodDef Looper_methods[] =
2340 {
2341 {"getTable", (PyCFunction)Looper_getTable, METH_NOARGS, "Returns sound table object."},
2342 {"setTable", (PyCFunction)Looper_setTable, METH_O, "Sets sound table."},
2343 {"getServer", (PyCFunction)Looper_getServer, METH_NOARGS, "Returns server object."},
2344 {"_getStream", (PyCFunction)Looper_getStream, METH_NOARGS, "Returns stream object."},
2345 {"_getTriggerStream", (PyCFunction)Looper_getTriggerStream, METH_NOARGS, "Returns trigger stream object."},
2346 {"play", (PyCFunction)Looper_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
2347 {"out", (PyCFunction)Looper_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
2348 {"stop", (PyCFunction)Looper_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
2349 {"setPitch", (PyCFunction)Looper_setPitch, METH_O, "Sets global pitch factor."},
2350 {"setStart", (PyCFunction)Looper_setStart, METH_O, "Sets position in the sound table."},
2351 {"setDur", (PyCFunction)Looper_setDur, METH_O, "Sets the grain duration."},
2352 {"setXfade", (PyCFunction)Looper_setXfade, METH_O, "Sets crossfade length in percent."},
2353 {"setXfadeShape", (PyCFunction)Looper_setXfadeShape, METH_O, "Sets crossfade shape."},
2354 {"setMode", (PyCFunction)Looper_setMode, METH_O, "Sets looping mode (0 = no loop, 1 = forward, 2 = backward, 3 = back-and-forth)."},
2355 {"setStartFromLoop", (PyCFunction)Looper_setStartFromLoop, METH_O, "Sets init pointer position."},
2356 {"setInterp", (PyCFunction)Looper_setInterp, METH_O, "Sets oscillator interpolation mode."},
2357 {"setAutoSmooth", (PyCFunction)Looper_setAutoSmooth, METH_O, "Activate lowpass filter for transposition below 1."},
2358 {"appendFadeTime", (PyCFunction)Looper_appendFadeTime, METH_O, "If positive, fade time is added to loop duration."},
2359 {"fadeInSeconds", (PyCFunction)Looper_fadeInSeconds, METH_O, "If positive, fade time is set in seconds instead of percentage."},
2360 {"reset", (PyCFunction)Looper_on_reset, METH_NOARGS, "Resets internal counters."},
2361 {"loopnow", (PyCFunction)Looper_loopnow, METH_NOARGS, "Satrts a new loop immediately."},
2362 {"setMul", (PyCFunction)Looper_setMul, METH_O, "Sets granulator mul factor."},
2363 {"setAdd", (PyCFunction)Looper_setAdd, METH_O, "Sets granulator add factor."},
2364 {"setSub", (PyCFunction)Looper_setSub, METH_O, "Sets inverse add factor."},
2365 {"setDiv", (PyCFunction)Looper_setDiv, METH_O, "Sets inverse mul factor."},
2366 {NULL} /* Sentinel */
2367 };
2368
2369 static PyNumberMethods Looper_as_number =
2370 {
2371 (binaryfunc)Looper_add, /*nb_add*/
2372 (binaryfunc)Looper_sub, /*nb_subtract*/
2373 (binaryfunc)Looper_multiply, /*nb_multiply*/
2374 0, /*nb_remainder*/
2375 0, /*nb_divmod*/
2376 0, /*nb_power*/
2377 0, /*nb_neg*/
2378 0, /*nb_pos*/
2379 0, /*(unaryfunc)array_abs,*/
2380 0, /*nb_nonzero*/
2381 0, /*nb_invert*/
2382 0, /*nb_lshift*/
2383 0, /*nb_rshift*/
2384 0, /*nb_and*/
2385 0, /*nb_xor*/
2386 0, /*nb_or*/
2387 0, /*nb_int*/
2388 0, /*nb_long*/
2389 0, /*nb_float*/
2390 (binaryfunc)Looper_inplace_add, /*inplace_add*/
2391 (binaryfunc)Looper_inplace_sub, /*inplace_subtract*/
2392 (binaryfunc)Looper_inplace_multiply, /*inplace_multiply*/
2393 0, /*inplace_remainder*/
2394 0, /*inplace_power*/
2395 0, /*inplace_lshift*/
2396 0, /*inplace_rshift*/
2397 0, /*inplace_and*/
2398 0, /*inplace_xor*/
2399 0, /*inplace_or*/
2400 0, /*nb_floor_divide*/
2401 (binaryfunc)Looper_div, /*nb_true_divide*/
2402 0, /*nb_inplace_floor_divide*/
2403 (binaryfunc)Looper_inplace_div, /*nb_inplace_true_divide*/
2404 0, /* nb_index */
2405 };
2406
2407 PyTypeObject LooperType =
2408 {
2409 PyVarObject_HEAD_INIT(NULL, 0)
2410 "_pyo.Looper_base", /*tp_name*/
2411 sizeof(Looper), /*tp_basicpitch*/
2412 0, /*tp_itempitch*/
2413 (destructor)Looper_dealloc, /*tp_dealloc*/
2414 0, /*tp_print*/
2415 0, /*tp_getattr*/
2416 0, /*tp_setattr*/
2417 0, /*tp_as_async (tp_compare in Python 2)*/
2418 0, /*tp_repr*/
2419 &Looper_as_number, /*tp_as_number*/
2420 0, /*tp_as_sequence*/
2421 0, /*tp_as_mapping*/
2422 0, /*tp_hash */
2423 0, /*tp_call*/
2424 0, /*tp_str*/
2425 0, /*tp_getattro*/
2426 0, /*tp_setattro*/
2427 0, /*tp_as_buffer*/
2428 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
2429 "Looper objects. Sound looper with crossfade.", /* tp_doc */
2430 (traverseproc)Looper_traverse, /* tp_traverse */
2431 (inquiry)Looper_clear, /* tp_clear */
2432 0, /* tp_richcompare */
2433 0, /* tp_weaklistoffset */
2434 0, /* tp_iter */
2435 0, /* tp_iternext */
2436 Looper_methods, /* tp_methods */
2437 Looper_members, /* tp_members */
2438 0, /* tp_getset */
2439 0, /* tp_base */
2440 0, /* tp_dict */
2441 0, /* tp_descr_get */
2442 0, /* tp_descr_set */
2443 0, /* tp_dictoffset */
2444 0, /* tp_init */
2445 0, /* tp_alloc */
2446 Looper_new, /* tp_new */
2447 };
2448
2449 typedef struct
2450 {
2451 pyo_audio_HEAD
2452 Looper *mainPlayer;
2453 int modebuffer[2];
2454 } LooperTimeStream;
2455
2456 static void LooperTimeStream_postprocessing_ii(LooperTimeStream *self) { POST_PROCESSING_II };
2457 static void LooperTimeStream_postprocessing_ai(LooperTimeStream *self) { POST_PROCESSING_AI };
2458 static void LooperTimeStream_postprocessing_ia(LooperTimeStream *self) { POST_PROCESSING_IA };
2459 static void LooperTimeStream_postprocessing_aa(LooperTimeStream *self) { POST_PROCESSING_AA };
2460 static void LooperTimeStream_postprocessing_ireva(LooperTimeStream *self) { POST_PROCESSING_IREVA };
2461 static void LooperTimeStream_postprocessing_areva(LooperTimeStream *self) { POST_PROCESSING_AREVA };
2462 static void LooperTimeStream_postprocessing_revai(LooperTimeStream *self) { POST_PROCESSING_REVAI };
2463 static void LooperTimeStream_postprocessing_revaa(LooperTimeStream *self) { POST_PROCESSING_REVAA };
2464 static void LooperTimeStream_postprocessing_revareva(LooperTimeStream *self) { POST_PROCESSING_REVAREVA };
2465
2466 static void
2467 LooperTimeStream_setProcMode(LooperTimeStream *self)
2468 {
2469 int muladdmode;
2470 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
2471
2472 switch (muladdmode)
2473 {
2474 case 0:
2475 self->muladd_func_ptr = LooperTimeStream_postprocessing_ii;
2476 break;
2477
2478 case 1:
2479 self->muladd_func_ptr = LooperTimeStream_postprocessing_ai;
2480 break;
2481
2482 case 2:
2483 self->muladd_func_ptr = LooperTimeStream_postprocessing_revai;
2484 break;
2485
2486 case 10:
2487 self->muladd_func_ptr = LooperTimeStream_postprocessing_ia;
2488 break;
2489
2490 case 11:
2491 self->muladd_func_ptr = LooperTimeStream_postprocessing_aa;
2492 break;
2493
2494 case 12:
2495 self->muladd_func_ptr = LooperTimeStream_postprocessing_revaa;
2496 break;
2497
2498 case 20:
2499 self->muladd_func_ptr = LooperTimeStream_postprocessing_ireva;
2500 break;
2501
2502 case 21:
2503 self->muladd_func_ptr = LooperTimeStream_postprocessing_areva;
2504 break;
2505
2506 case 22:
2507 self->muladd_func_ptr = LooperTimeStream_postprocessing_revareva;
2508 break;
2509 }
2510 }
2511
2512 static void
2513 LooperTimeStream_compute_next_data_frame(LooperTimeStream *self)
2514 {
2515 int i;
2516 MYFLT *tmp;
2517 tmp = Looper_getTimeBuffer((Looper *)self->mainPlayer);
2518
2519 for (i = 0; i < self->bufsize; i++)
2520 {
2521 self->data[i] = tmp[i];
2522 }
2523
2524 (*self->muladd_func_ptr)(self);
2525 }
2526
2527 static int
2528 LooperTimeStream_traverse(LooperTimeStream *self, visitproc visit, void *arg)
2529 {
2530 pyo_VISIT
2531 Py_VISIT(self->mainPlayer);
2532 return 0;
2533 }
2534
2535 static int
2536 LooperTimeStream_clear(LooperTimeStream *self)
2537 {
2538 pyo_CLEAR
2539 Py_CLEAR(self->mainPlayer);
2540 return 0;
2541 }
2542
2543 static void
2544 LooperTimeStream_dealloc(LooperTimeStream* self)
2545 {
2546 pyo_DEALLOC
2547 LooperTimeStream_clear(self);
2548 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
2549 Py_TYPE(self)->tp_free((PyObject*)self);
2550 }
2551
2552 static PyObject *
2553 LooperTimeStream_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2554 {
2555 int i;
2556 PyObject *maintmp = NULL;
2557 LooperTimeStream *self;
2558 self = (LooperTimeStream *)type->tp_alloc(type, 0);
2559
2560 self->modebuffer[0] = 0;
2561 self->modebuffer[1] = 0;
2562
2563 INIT_OBJECT_COMMON
2564 Stream_setFunctionPtr(self->stream, LooperTimeStream_compute_next_data_frame);
2565 self->mode_func_ptr = LooperTimeStream_setProcMode;
2566
2567 static char *kwlist[] = {"mainPlayer", NULL};
2568
2569 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist, &maintmp))
2570 Py_RETURN_NONE;
2571
2572 self->mainPlayer = (Looper *)maintmp;
2573 Py_INCREF(self->mainPlayer);
2574
2575 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
2576
2577 (*self->mode_func_ptr)(self);
2578
2579 return (PyObject *)self;
2580 }
2581
2582 static PyObject * LooperTimeStream_getServer(LooperTimeStream* self) { GET_SERVER };
2583 static PyObject * LooperTimeStream_getStream(LooperTimeStream* self) { GET_STREAM };
2584 static PyObject * LooperTimeStream_setMul(LooperTimeStream *self, PyObject *arg) { SET_MUL };
2585 static PyObject * LooperTimeStream_setAdd(LooperTimeStream *self, PyObject *arg) { SET_ADD };
2586 static PyObject * LooperTimeStream_setSub(LooperTimeStream *self, PyObject *arg) { SET_SUB };
2587 static PyObject * LooperTimeStream_setDiv(LooperTimeStream *self, PyObject *arg) { SET_DIV };
2588
2589 static PyObject * LooperTimeStream_play(LooperTimeStream *self, PyObject *args, PyObject *kwds) { PLAY };
2590 static PyObject * LooperTimeStream_out(LooperTimeStream *self, PyObject *args, PyObject *kwds) { OUT };
2591 static PyObject * LooperTimeStream_stop(LooperTimeStream *self, PyObject *args, PyObject *kwds) { STOP };
2592
2593 static PyObject * LooperTimeStream_multiply(LooperTimeStream *self, PyObject *arg) { MULTIPLY };
2594 static PyObject * LooperTimeStream_inplace_multiply(LooperTimeStream *self, PyObject *arg) { INPLACE_MULTIPLY };
2595 static PyObject * LooperTimeStream_add(LooperTimeStream *self, PyObject *arg) { ADD };
2596 static PyObject * LooperTimeStream_inplace_add(LooperTimeStream *self, PyObject *arg) { INPLACE_ADD };
2597 static PyObject * LooperTimeStream_sub(LooperTimeStream *self, PyObject *arg) { SUB };
2598 static PyObject * LooperTimeStream_inplace_sub(LooperTimeStream *self, PyObject *arg) { INPLACE_SUB };
2599 static PyObject * LooperTimeStream_div(LooperTimeStream *self, PyObject *arg) { DIV };
2600 static PyObject * LooperTimeStream_inplace_div(LooperTimeStream *self, PyObject *arg) { INPLACE_DIV };
2601
2602 static PyMemberDef LooperTimeStream_members[] =
2603 {
2604 {"server", T_OBJECT_EX, offsetof(LooperTimeStream, server), 0, "Pyo server."},
2605 {"stream", T_OBJECT_EX, offsetof(LooperTimeStream, stream), 0, "Stream object."},
2606 {"mul", T_OBJECT_EX, offsetof(LooperTimeStream, mul), 0, "Mul factor."},
2607 {"add", T_OBJECT_EX, offsetof(LooperTimeStream, add), 0, "Add factor."},
2608 {NULL} /* Sentinel */
2609 };
2610
2611 static PyMethodDef LooperTimeStream_methods[] =
2612 {
2613 {"getServer", (PyCFunction)LooperTimeStream_getServer, METH_NOARGS, "Returns server object."},
2614 {"_getStream", (PyCFunction)LooperTimeStream_getStream, METH_NOARGS, "Returns stream object."},
2615 {"play", (PyCFunction)LooperTimeStream_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
2616 {"out", (PyCFunction)LooperTimeStream_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
2617 {"stop", (PyCFunction)LooperTimeStream_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
2618 {"setMul", (PyCFunction)LooperTimeStream_setMul, METH_O, "Sets oscillator mul factor."},
2619 {"setAdd", (PyCFunction)LooperTimeStream_setAdd, METH_O, "Sets oscillator add factor."},
2620 {"setSub", (PyCFunction)LooperTimeStream_setSub, METH_O, "Sets inverse add factor."},
2621 {"setDiv", (PyCFunction)LooperTimeStream_setDiv, METH_O, "Sets inverse mul factor."},
2622 {NULL} /* Sentinel */
2623 };
2624
2625 static PyNumberMethods LooperTimeStream_as_number =
2626 {
2627 (binaryfunc)LooperTimeStream_add, /*nb_add*/
2628 (binaryfunc)LooperTimeStream_sub, /*nb_subtract*/
2629 (binaryfunc)LooperTimeStream_multiply, /*nb_multiply*/
2630 0, /*nb_remainder*/
2631 0, /*nb_divmod*/
2632 0, /*nb_power*/
2633 0, /*nb_neg*/
2634 0, /*nb_pos*/
2635 0, /*(unaryfunc)array_abs,*/
2636 0, /*nb_nonzero*/
2637 0, /*nb_invert*/
2638 0, /*nb_lshift*/
2639 0, /*nb_rshift*/
2640 0, /*nb_and*/
2641 0, /*nb_xor*/
2642 0, /*nb_or*/
2643 0, /*nb_int*/
2644 0, /*nb_long*/
2645 0, /*nb_float*/
2646 (binaryfunc)LooperTimeStream_inplace_add, /*inplace_add*/
2647 (binaryfunc)LooperTimeStream_inplace_sub, /*inplace_subtract*/
2648 (binaryfunc)LooperTimeStream_inplace_multiply, /*inplace_multiply*/
2649 0, /*inplace_remainder*/
2650 0, /*inplace_power*/
2651 0, /*inplace_lshift*/
2652 0, /*inplace_rshift*/
2653 0, /*inplace_and*/
2654 0, /*inplace_xor*/
2655 0, /*inplace_or*/
2656 0, /*nb_floor_divide*/
2657 (binaryfunc)LooperTimeStream_div, /*nb_true_divide*/
2658 0, /*nb_inplace_floor_divide*/
2659 (binaryfunc)LooperTimeStream_inplace_div, /*nb_inplace_true_divide*/
2660 0, /* nb_index */
2661 };
2662
2663 PyTypeObject LooperTimeStreamType =
2664 {
2665 PyVarObject_HEAD_INIT(NULL, 0)
2666 "_pyo.LooperTimeStream_base", /*tp_name*/
2667 sizeof(LooperTimeStream), /*tp_basicsize*/
2668 0, /*tp_itemsize*/
2669 (destructor)LooperTimeStream_dealloc, /*tp_dealloc*/
2670 0, /*tp_print*/
2671 0, /*tp_getattr*/
2672 0, /*tp_setattr*/
2673 0, /*tp_as_async (tp_compare in Python 2)*/
2674 0, /*tp_repr*/
2675 &LooperTimeStream_as_number, /*tp_as_number*/
2676 0, /*tp_as_sequence*/
2677 0, /*tp_as_mapping*/
2678 0, /*tp_hash */
2679 0, /*tp_call*/
2680 0, /*tp_str*/
2681 0, /*tp_getattro*/
2682 0, /*tp_setattro*/
2683 0, /*tp_as_buffer*/
2684 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
2685 "LooperTimeStream objects. Returns the current recording time, in samples, of a Looper object.", /* tp_doc */
2686 (traverseproc)LooperTimeStream_traverse, /* tp_traverse */
2687 (inquiry)LooperTimeStream_clear, /* tp_clear */
2688 0, /* tp_richcompare */
2689 0, /* tp_weaklistoffset */
2690 0, /* tp_iter */
2691 0, /* tp_iternext */
2692 LooperTimeStream_methods, /* tp_methods */
2693 LooperTimeStream_members, /* tp_members */
2694 0, /* tp_getset */
2695 0, /* tp_base */
2696 0, /* tp_dict */
2697 0, /* tp_descr_get */
2698 0, /* tp_descr_set */
2699 0, /* tp_dictoffset */
2700 0, /* tp_init */
2701 0, /* tp_alloc */
2702 LooperTimeStream_new, /* tp_new */
2703 };
2704
2705 static const MYFLT Granule_MAX_GRAINS = 4096;
2706 typedef struct
2707 {
2708 pyo_audio_HEAD
2709 PyObject *table;
2710 PyObject *env;
2711 PyObject *dens;
2712 Stream *dens_stream;
2713 PyObject *pitch;
2714 Stream *pitch_stream;
2715 PyObject *pos;
2716 Stream *pos_stream;
2717 PyObject *dur;
2718 Stream *dur_stream;
2719 MYFLT *gpos;
2720 MYFLT *glen;
2721 MYFLT *inc;
2722 MYFLT *phase;
2723 int *flags;
2724 int num;
2725 int sync;
2726 double timer;
2727 MYFLT oneOnSr;
2728 MYFLT srOnRandMax;
2729 int modebuffer[6];
2730 } Granule;
2731
2732 static void
2733 Granule_transform_i(Granule *self)
2734 {
2735 MYFLT dens, inc, index, amp, phase;
2736 int i, j, flag = 0;
2737 T_SIZE_T ipart;
2738 MYFLT pit = 0, pos = 0, dur = 0;
2739
2740 MYFLT *tablelist = TableStream_getData((TableStream *)self->table);
2741 T_SIZE_T size = TableStream_getSize((TableStream *)self->table);
2742
2743 MYFLT *envlist = TableStream_getData((TableStream *)self->env);
2744 T_SIZE_T envsize = TableStream_getSize((TableStream *)self->env);
2745
2746 dens = PyFloat_AS_DOUBLE(self->dens);
2747
2748 if (dens < 0.0)
2749 dens = 0.0;
2750
2751 inc = dens * self->oneOnSr;
2752
2753 for (i = 0; i < self->bufsize; i++)
2754 {
2755 self->data[i] = 0.0;
2756
2757 if (self->sync == 1)
2758 {
2759 /* synchronous */
2760 self->timer += inc;
2761
2762 if (self->timer >= 1.0)
2763 {
2764 self->timer -= 1.0;
2765 flag = 1;
2766 }
2767 }
2768 else
2769 {
2770 /* asynchronous */
2771 if ((pyorand() * self->srOnRandMax) < dens)
2772 flag = 1;
2773 }
2774
2775 /* need to start a new grain */
2776 if (flag)
2777 {
2778 for (j = 0; j < Granule_MAX_GRAINS; j++)
2779 {
2780 if (self->flags[j] == 0)
2781 {
2782 self->flags[j] = 1;
2783
2784 if (j >= self->num)
2785 self->num = j + 1;
2786
2787 if (self->modebuffer[3] == 0)
2788 pit = PyFloat_AS_DOUBLE(self->pitch);
2789 else
2790 pit = Stream_getData((Stream *)self->pitch_stream)[i];
2791
2792 if (self->modebuffer[4] == 0)
2793 pos = PyFloat_AS_DOUBLE(self->pos);
2794 else
2795 pos = Stream_getData((Stream *)self->pos_stream)[i];
2796
2797 if (self->modebuffer[5] == 0)
2798 dur = PyFloat_AS_DOUBLE(self->dur);
2799 else
2800 dur = Stream_getData((Stream *)self->dur_stream)[i];
2801
2802 if (pit < 0.0)
2803 pit = -pit;
2804
2805 if (pos < 0.0)
2806 pos = 0.0;
2807 else if (pos >= size)
2808 pos = (MYFLT)size;
2809
2810 if (dur < 0.0001)
2811 dur = 0.0001;
2812
2813 self->gpos[j] = pos;
2814 self->glen[j] = dur * self->sr * pit;
2815
2816 if ((pos + self->glen[j]) >= size || (pos + self->glen[j]) < 0)
2817 self->flags[j] = 0;
2818
2819 self->phase[j] = 0.0;
2820 self->inc[j] = 1.0 / (dur * self->sr);
2821 break;
2822 }
2823 }
2824 }
2825
2826 /* compute active grains */
2827 for (j = 0; j < self->num; j++)
2828 {
2829 if (self->flags[j])
2830 {
2831 phase = self->phase[j];
2832 /* compute envelope */
2833 index = phase * envsize;
2834 ipart = (T_SIZE_T)index;
2835 amp = envlist[ipart] + (envlist[ipart + 1] - envlist[ipart]) * (index - ipart);
2836 /* compute sampling */
2837 index = phase * self->glen[j] + self->gpos[j];
2838 ipart = (T_SIZE_T)index;
2839 self->data[i] += (tablelist[ipart] + (tablelist[ipart + 1] - tablelist[ipart]) * (index - ipart)) * amp;
2840 phase += self->inc[j];
2841
2842 if (phase >= 1.0)
2843 self->flags[j] = 0;
2844 else
2845 self->phase[j] = phase;
2846 }
2847 }
2848
2849 flag = 0;
2850 }
2851 }
2852
2853 static void
2854 Granule_transform_a(Granule *self)
2855 {
2856 MYFLT index, amp, phase;
2857 int i, j, flag = 0;
2858 T_SIZE_T ipart;
2859 MYFLT pit = 0, pos = 0, dur = 0;
2860
2861 MYFLT *tablelist = TableStream_getData((TableStream *)self->table);
2862 T_SIZE_T size = TableStream_getSize((TableStream *)self->table);
2863
2864 MYFLT *envlist = TableStream_getData((TableStream *)self->env);
2865 T_SIZE_T envsize = TableStream_getSize((TableStream *)self->env);
2866
2867 MYFLT *density = Stream_getData((Stream *)self->dens_stream);
2868
2869 for (i = 0; i < self->bufsize; i++)
2870 {
2871 self->data[i] = 0.0;
2872
2873 if (self->sync == 1)
2874 {
2875 /* synchronous */
2876 self->timer += density[i] * self->oneOnSr;
2877
2878 if (self->timer >= 1.0)
2879 {
2880 self->timer -= 1.0;
2881 flag = 1;
2882 }
2883 }
2884 else
2885 {
2886 /* asynchronous */
2887 if ((pyorand() * self->srOnRandMax) < density[i])
2888 flag = 1;
2889 }
2890
2891 /* need to start a new grain */
2892 if (flag)
2893 {
2894 for (j = 0; j < Granule_MAX_GRAINS; j++)
2895 {
2896 if (self->flags[j] == 0)
2897 {
2898 self->flags[j] = 1;
2899
2900 if (j >= self->num)
2901 self->num = j + 1;
2902
2903 if (self->modebuffer[3] == 0)
2904 pit = PyFloat_AS_DOUBLE(self->pitch);
2905 else
2906 pit = Stream_getData((Stream *)self->pitch_stream)[i];
2907
2908 if (self->modebuffer[4] == 0)
2909 pos = PyFloat_AS_DOUBLE(self->pos);
2910 else
2911 pos = Stream_getData((Stream *)self->pos_stream)[i];
2912
2913 if (self->modebuffer[5] == 0)
2914 dur = PyFloat_AS_DOUBLE(self->dur);
2915 else
2916 dur = Stream_getData((Stream *)self->dur_stream)[i];
2917
2918 if (pit < 0.0)
2919 pit = -pit;
2920
2921 if (pos < 0.0)
2922 pos = 0.0;
2923 else if (pos >= size)
2924 pos = (MYFLT)size;
2925
2926 if (dur < 0.0001)
2927 dur = 0.0001;
2928
2929 self->gpos[j] = pos;
2930 self->glen[j] = dur * self->sr * pit;
2931
2932 if ((pos + self->glen[j]) >= size || (pos + self->glen[j]) < 0)
2933 self->flags[j] = 0;
2934
2935 self->phase[j] = 0.0;
2936 self->inc[j] = 1.0 / (dur * self->sr);
2937 break;
2938 }
2939 }
2940 }
2941
2942 /* compute active grains */
2943 for (j = 0; j < self->num; j++)
2944 {
2945 if (self->flags[j])
2946 {
2947 phase = self->phase[j];
2948 // compute envelope
2949 index = phase * envsize;
2950 ipart = (T_SIZE_T)index;
2951 amp = envlist[ipart] + (envlist[ipart + 1] - envlist[ipart]) * (index - ipart);
2952 // compute sampling
2953 index = phase * self->glen[j] + self->gpos[j];
2954 ipart = (T_SIZE_T)index;
2955 self->data[i] += (tablelist[ipart] + (tablelist[ipart + 1] - tablelist[ipart]) * (index - ipart)) * amp;
2956 phase += self->inc[j];
2957
2958 if (phase >= 1.0)
2959 self->flags[j] = 0;
2960 else
2961 self->phase[j] = phase;
2962 }
2963 }
2964
2965 flag = 0;
2966 }
2967 }
2968
2969 static void Granule_postprocessing_ii(Granule *self) { POST_PROCESSING_II };
2970 static void Granule_postprocessing_ai(Granule *self) { POST_PROCESSING_AI };
2971 static void Granule_postprocessing_ia(Granule *self) { POST_PROCESSING_IA };
2972 static void Granule_postprocessing_aa(Granule *self) { POST_PROCESSING_AA };
2973 static void Granule_postprocessing_ireva(Granule *self) { POST_PROCESSING_IREVA };
2974 static void Granule_postprocessing_areva(Granule *self) { POST_PROCESSING_AREVA };
2975 static void Granule_postprocessing_revai(Granule *self) { POST_PROCESSING_REVAI };
2976 static void Granule_postprocessing_revaa(Granule *self) { POST_PROCESSING_REVAA };
2977 static void Granule_postprocessing_revareva(Granule *self) { POST_PROCESSING_REVAREVA };
2978
2979 static void
2980 Granule_setProcMode(Granule *self)
2981 {
2982 int procmode, muladdmode;
2983 procmode = self->modebuffer[2];
2984 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
2985
2986 switch (procmode)
2987 {
2988 case 0:
2989 self->proc_func_ptr = Granule_transform_i;
2990 break;
2991
2992 case 1:
2993 self->proc_func_ptr = Granule_transform_a;
2994 break;
2995 }
2996
2997 switch (muladdmode)
2998 {
2999 case 0:
3000 self->muladd_func_ptr = Granule_postprocessing_ii;
3001 break;
3002
3003 case 1:
3004 self->muladd_func_ptr = Granule_postprocessing_ai;
3005 break;
3006
3007 case 2:
3008 self->muladd_func_ptr = Granule_postprocessing_revai;
3009 break;
3010
3011 case 10:
3012 self->muladd_func_ptr = Granule_postprocessing_ia;
3013 break;
3014
3015 case 11:
3016 self->muladd_func_ptr = Granule_postprocessing_aa;
3017 break;
3018
3019 case 12:
3020 self->muladd_func_ptr = Granule_postprocessing_revaa;
3021 break;
3022
3023 case 20:
3024 self->muladd_func_ptr = Granule_postprocessing_ireva;
3025 break;
3026
3027 case 21:
3028 self->muladd_func_ptr = Granule_postprocessing_areva;
3029 break;
3030
3031 case 22:
3032 self->muladd_func_ptr = Granule_postprocessing_revareva;
3033 break;
3034 }
3035 }
3036
3037 static void
3038 Granule_compute_next_data_frame(Granule *self)
3039 {
3040 (*self->proc_func_ptr)(self);
3041 (*self->muladd_func_ptr)(self);
3042 }
3043
3044 static int
3045 Granule_traverse(Granule *self, visitproc visit, void *arg)
3046 {
3047 pyo_VISIT
3048 Py_VISIT(self->dens);
3049 Py_VISIT(self->pitch);
3050 Py_VISIT(self->pos);
3051 Py_VISIT(self->dur);
3052 return 0;
3053 }
3054
3055 static int
3056 Granule_clear(Granule *self)
3057 {
3058 pyo_CLEAR
3059 Py_CLEAR(self->dens);
3060 Py_CLEAR(self->pitch);
3061 Py_CLEAR(self->pos);
3062 Py_CLEAR(self->dur);
3063 return 0;
3064 }
3065
3066 static void
3067 Granule_dealloc(Granule* self)
3068 {
3069 pyo_DEALLOC
3070 PyMem_RawFree(self->gpos);
3071 PyMem_RawFree(self->glen);
3072 PyMem_RawFree(self->inc);
3073 PyMem_RawFree(self->flags);
3074 PyMem_RawFree(self->phase);
3075 Granule_clear(self);
3076 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
3077 Py_TYPE(self)->tp_free((PyObject*)self);
3078 }
3079
3080 static PyObject *
3081 Granule_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3082 {
3083 int i;
3084 PyObject *tabletmp, *envtmp, *denstmp = NULL, *pitchtmp = NULL, *postmp = NULL, *durtmp = NULL, *multmp = NULL, *addtmp = NULL;
3085 Granule *self;
3086 self = (Granule *)type->tp_alloc(type, 0);
3087
3088 self->dens = PyFloat_FromDouble(50);
3089 self->pitch = PyFloat_FromDouble(1);
3090 self->pos = PyFloat_FromDouble(0.0);
3091 self->dur = PyFloat_FromDouble(0.1);
3092 self->timer = 1.0;
3093 self->num = 0;
3094 self->sync = 1;
3095 self->modebuffer[0] = 0;
3096 self->modebuffer[1] = 0;
3097 self->modebuffer[2] = 0;
3098 self->modebuffer[3] = 0;
3099 self->modebuffer[4] = 0;
3100 self->modebuffer[5] = 0;
3101
3102 INIT_OBJECT_COMMON
3103
3104 self->oneOnSr = 1.0 / self->sr;
3105 self->srOnRandMax = self->sr / (MYFLT)PYO_RAND_MAX;
3106
3107 Stream_setFunctionPtr(self->stream, Granule_compute_next_data_frame);
3108 self->mode_func_ptr = Granule_setProcMode;
3109
3110 static char *kwlist[] = {"table", "env", "dens", "pitch", "pos", "dur", "mul", "add", NULL};
3111
3112 if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO|OOOOOO", kwlist, &tabletmp, &envtmp, &denstmp, &pitchtmp, &postmp, &durtmp, &multmp, &addtmp))
3113 Py_RETURN_NONE;
3114
3115 if ( PyObject_HasAttrString((PyObject *)tabletmp, "getTableStream") == 0 )
3116 {
3117 PyErr_SetString(PyExc_TypeError, "\"table\" argument of Granule must be a PyoTableObject.\n");
3118 Py_RETURN_NONE;
3119 }
3120
3121 self->table = PyObject_CallMethod((PyObject *)tabletmp, "getTableStream", "");
3122
3123 if ( PyObject_HasAttrString((PyObject *)envtmp, "getTableStream") == 0 )
3124 {
3125 PyErr_SetString(PyExc_TypeError, "\"env\" argument of Granule must be a PyoTableObject.\n");
3126 Py_RETURN_NONE;
3127 }
3128
3129 self->env = PyObject_CallMethod((PyObject *)envtmp, "getTableStream", "");
3130
3131 if (denstmp)
3132 {
3133 PyObject_CallMethod((PyObject *)self, "setDens", "O", denstmp);
3134 }
3135
3136 if (pitchtmp)
3137 {
3138 PyObject_CallMethod((PyObject *)self, "setPitch", "O", pitchtmp);
3139 }
3140
3141 if (postmp)
3142 {
3143 PyObject_CallMethod((PyObject *)self, "setPos", "O", postmp);
3144 }
3145
3146 if (durtmp)
3147 {
3148 PyObject_CallMethod((PyObject *)self, "setDur", "O", durtmp);
3149 }
3150
3151 if (multmp)
3152 {
3153 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
3154 }
3155
3156 if (addtmp)
3157 {
3158 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
3159 }
3160
3161 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
3162
3163 self->gpos = (MYFLT *)PyMem_RawRealloc(self->gpos, Granule_MAX_GRAINS * sizeof(MYFLT));
3164 self->glen = (MYFLT *)PyMem_RawRealloc(self->glen, Granule_MAX_GRAINS * sizeof(MYFLT));
3165 self->inc = (MYFLT *)PyMem_RawRealloc(self->inc, Granule_MAX_GRAINS * sizeof(MYFLT));
3166 self->phase = (MYFLT *)PyMem_RawRealloc(self->phase, Granule_MAX_GRAINS * sizeof(MYFLT));
3167 self->flags = (int *)PyMem_RawRealloc(self->flags, Granule_MAX_GRAINS * sizeof(int));
3168
3169 for (i = 0; i < Granule_MAX_GRAINS; i++)
3170 {
3171 self->gpos[i] = self->glen[i] = self->inc[i] = self->phase[i] = 0.0;
3172 self->flags[i] = 0;
3173 }
3174
3175 Server_generateSeed((Server *)self->server, GRANULE_ID);
3176
3177 (*self->mode_func_ptr)(self);
3178
3179 return (PyObject *)self;
3180 }
3181
3182 static PyObject * Granule_getServer(Granule* self) { GET_SERVER };
3183 static PyObject * Granule_getStream(Granule* self) { GET_STREAM };
3184 static PyObject * Granule_setMul(Granule *self, PyObject *arg) { SET_MUL };
3185 static PyObject * Granule_setAdd(Granule *self, PyObject *arg) { SET_ADD };
3186 static PyObject * Granule_setSub(Granule *self, PyObject *arg) { SET_SUB };
3187 static PyObject * Granule_setDiv(Granule *self, PyObject *arg) { SET_DIV };
3188
3189 static PyObject * Granule_play(Granule *self, PyObject *args, PyObject *kwds) { PLAY };
3190 static PyObject * Granule_out(Granule *self, PyObject *args, PyObject *kwds) { OUT };
3191 static PyObject * Granule_stop(Granule *self, PyObject *args, PyObject *kwds) { STOP };
3192
3193 static PyObject * Granule_multiply(Granule *self, PyObject *arg) { MULTIPLY };
3194 static PyObject * Granule_inplace_multiply(Granule *self, PyObject *arg) { INPLACE_MULTIPLY };
3195 static PyObject * Granule_add(Granule *self, PyObject *arg) { ADD };
3196 static PyObject * Granule_inplace_add(Granule *self, PyObject *arg) { INPLACE_ADD };
3197 static PyObject * Granule_sub(Granule *self, PyObject *arg) { SUB };
3198 static PyObject * Granule_inplace_sub(Granule *self, PyObject *arg) { INPLACE_SUB };
3199 static PyObject * Granule_div(Granule *self, PyObject *arg) { DIV };
3200 static PyObject * Granule_inplace_div(Granule *self, PyObject *arg) { INPLACE_DIV };
3201
3202 static PyObject * Granule_setDens(Granule *self, PyObject *arg) { SET_PARAM(self->dens, self->dens_stream, 2); }
3203 static PyObject * Granule_setPitch(Granule *self, PyObject *arg) { SET_PARAM(self->pitch, self->pitch_stream, 3); }
3204 static PyObject * Granule_setPos(Granule *self, PyObject *arg) { SET_PARAM(self->pos, self->pos_stream, 4); }
3205 static PyObject * Granule_setDur(Granule *self, PyObject *arg) { SET_PARAM(self->dur, self->dur_stream, 5); }
3206
3207 static PyObject *
3208 Granule_getTable(Granule* self)
3209 {
3210 Py_INCREF(self->table);
3211 return self->table;
3212 };
3213
3214 static PyObject *
3215 Granule_setTable(Granule *self, PyObject *arg)
3216 {
3217 ASSERT_ARG_NOT_NULL
3218
3219 Py_DECREF(self->table);
3220 self->table = PyObject_CallMethod((PyObject *)arg, "getTableStream", "");
3221
3222 Py_RETURN_NONE;
3223 }
3224
3225 static PyObject *
3226 Granule_getEnv(Granule* self)
3227 {
3228 Py_INCREF(self->env);
3229 return self->env;
3230 };
3231
3232 static PyObject *
3233 Granule_setEnv(Granule *self, PyObject *arg)
3234 {
3235 ASSERT_ARG_NOT_NULL
3236
3237 Py_DECREF(self->env);
3238 self->env = PyObject_CallMethod((PyObject *)arg, "getTableStream", "");
3239
3240 Py_RETURN_NONE;
3241 }
3242
3243 static PyObject *
3244 Granule_setSync(Granule *self, PyObject *arg)
3245 {
3246 if (PyLong_Check(arg))
3247 {
3248 self->sync = PyLong_AsLong(arg);
3249
3250 if (self->sync <= 0)
3251 self->sync = 0;
3252 else
3253 self->sync = 1;
3254 }
3255
3256 Py_RETURN_NONE;
3257 }
3258
3259 static PyMemberDef Granule_members[] =
3260 {
3261 {"server", T_OBJECT_EX, offsetof(Granule, server), 0, "Pyo server."},
3262 {"stream", T_OBJECT_EX, offsetof(Granule, stream), 0, "Stream object."},
3263 {"table", T_OBJECT_EX, offsetof(Granule, table), 0, "Sound table."},
3264 {"env", T_OBJECT_EX, offsetof(Granule, env), 0, "Envelope table."},
3265 {"dens", T_OBJECT_EX, offsetof(Granule, dens), 0, "Density of grains per second."},
3266 {"pitch", T_OBJECT_EX, offsetof(Granule, pitch), 0, "Speed of the reading pointer."},
3267 {"pos", T_OBJECT_EX, offsetof(Granule, pos), 0, "Position in the sound table."},
3268 {"dur", T_OBJECT_EX, offsetof(Granule, dur), 0, "Duration of each grains."},
3269 {"mul", T_OBJECT_EX, offsetof(Granule, mul), 0, "Mul factor."},
3270 {"add", T_OBJECT_EX, offsetof(Granule, add), 0, "Add factor."},
3271 {NULL} /* Sentinel */
3272 };
3273
3274 static PyMethodDef Granule_methods[] =
3275 {
3276 {"getTable", (PyCFunction)Granule_getTable, METH_NOARGS, "Returns sound table object."},
3277 {"setTable", (PyCFunction)Granule_setTable, METH_O, "Sets sound table."},
3278 {"getEnv", (PyCFunction)Granule_getEnv, METH_NOARGS, "Returns envelope table object."},
3279 {"setEnv", (PyCFunction)Granule_setEnv, METH_O, "Sets envelope table."},
3280 {"getServer", (PyCFunction)Granule_getServer, METH_NOARGS, "Returns server object."},
3281 {"_getStream", (PyCFunction)Granule_getStream, METH_NOARGS, "Returns stream object."},
3282 {"play", (PyCFunction)Granule_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
3283 {"out", (PyCFunction)Granule_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
3284 {"stop", (PyCFunction)Granule_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
3285 {"setDens", (PyCFunction)Granule_setDens, METH_O, "Sets the density of grains per second."},
3286 {"setPitch", (PyCFunction)Granule_setPitch, METH_O, "Sets global pitch factor."},
3287 {"setPos", (PyCFunction)Granule_setPos, METH_O, "Sets position in the sound table."},
3288 {"setDur", (PyCFunction)Granule_setDur, METH_O, "Sets the grain duration."},
3289 {"setSync", (PyCFunction)Granule_setSync, METH_O, "Sets the granulator mode: synchronous or asynchronous."},
3290 {"setMul", (PyCFunction)Granule_setMul, METH_O, "Sets Granule mul factor."},
3291 {"setAdd", (PyCFunction)Granule_setAdd, METH_O, "Sets Granule add factor."},
3292 {"setSub", (PyCFunction)Granule_setSub, METH_O, "Sets inverse add factor."},
3293 {"setDiv", (PyCFunction)Granule_setDiv, METH_O, "Sets inverse mul factor."},
3294 {NULL} /* Sentinel */
3295 };
3296
3297 static PyNumberMethods Granule_as_number =
3298 {
3299 (binaryfunc)Granule_add, /*nb_add*/
3300 (binaryfunc)Granule_sub, /*nb_subtract*/
3301 (binaryfunc)Granule_multiply, /*nb_multiply*/
3302 0, /*nb_remainder*/
3303 0, /*nb_divmod*/
3304 0, /*nb_power*/
3305 0, /*nb_neg*/
3306 0, /*nb_pos*/
3307 0, /*(unaryfunc)array_abs,*/
3308 0, /*nb_nonzero*/
3309 0, /*nb_invert*/
3310 0, /*nb_lshift*/
3311 0, /*nb_rshift*/
3312 0, /*nb_and*/
3313 0, /*nb_xor*/
3314 0, /*nb_or*/
3315 0, /*nb_int*/
3316 0, /*nb_long*/
3317 0, /*nb_float*/
3318 (binaryfunc)Granule_inplace_add, /*inplace_add*/
3319 (binaryfunc)Granule_inplace_sub, /*inplace_subtract*/
3320 (binaryfunc)Granule_inplace_multiply, /*inplace_multiply*/
3321 0, /*inplace_remainder*/
3322 0, /*inplace_power*/
3323 0, /*inplace_lshift*/
3324 0, /*inplace_rshift*/
3325 0, /*inplace_and*/
3326 0, /*inplace_xor*/
3327 0, /*inplace_or*/
3328 0, /*nb_floor_divide*/
3329 (binaryfunc)Granule_div, /*nb_true_divide*/
3330 0, /*nb_inplace_floor_divide*/
3331 (binaryfunc)Granule_inplace_div, /*nb_inplace_true_divide*/
3332 0, /* nb_index */
3333 };
3334
3335 PyTypeObject GranuleType =
3336 {
3337 PyVarObject_HEAD_INIT(NULL, 0)
3338 "_pyo.Granule_base", /*tp_name*/
3339 sizeof(Granule), /*tp_basicpitch*/
3340 0, /*tp_itempitch*/
3341 (destructor)Granule_dealloc, /*tp_dealloc*/
3342 0, /*tp_print*/
3343 0, /*tp_getattr*/
3344 0, /*tp_setattr*/
3345 0, /*tp_as_async (tp_compare in Python 2)*/
3346 0, /*tp_repr*/
3347 &Granule_as_number, /*tp_as_number*/
3348 0, /*tp_as_sequence*/
3349 0, /*tp_as_mapping*/
3350 0, /*tp_hash */
3351 0, /*tp_call*/
3352 0, /*tp_str*/
3353 0, /*tp_getattro*/
3354 0, /*tp_setattro*/
3355 0, /*tp_as_buffer*/
3356 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
3357 "Granule objects. Accumulation of multiples grains of sound.", /* tp_doc */
3358 (traverseproc)Granule_traverse, /* tp_traverse */
3359 (inquiry)Granule_clear, /* tp_clear */
3360 0, /* tp_richcompare */
3361 0, /* tp_weaklistoffset */
3362 0, /* tp_iter */
3363 0, /* tp_iternext */
3364 Granule_methods, /* tp_methods */
3365 Granule_members, /* tp_members */
3366 0, /* tp_getset */
3367 0, /* tp_base */
3368 0, /* tp_dict */
3369 0, /* tp_descr_get */
3370 0, /* tp_descr_set */
3371 0, /* tp_dictoffset */
3372 0, /* tp_init */
3373 0, /* tp_alloc */
3374 Granule_new, /* tp_new */
3375 };
3376
3377 static const MYFLT MAINPARTICLE_MAX_GRAINS = 4096;
3378 typedef struct
3379 {
3380 pyo_audio_HEAD
3381 PyObject *table;
3382 PyObject *env;
3383 PyObject *dens;
3384 Stream *dens_stream;
3385 PyObject *pitch;
3386 Stream *pitch_stream;
3387 PyObject *pos;
3388 Stream *pos_stream;
3389 PyObject *dur;
3390 Stream *dur_stream;
3391 PyObject *dev;
3392 Stream *dev_stream;
3393 PyObject *pan;
3394 Stream *pan_stream;
3395 MYFLT *gpos;
3396 MYFLT *glen;
3397 MYFLT *inc;
3398 MYFLT *phase;
3399 MYFLT *amp1;
3400 MYFLT *amp2;
3401 int *flags;
3402 int *k1;
3403 int *k2;
3404 int num;
3405 int chnls;
3406 double timer;
3407 double devFactor;
3408 double srScale;
3409 MYFLT oneOnSr;
3410 MYFLT srOnRandMax;
3411 MYFLT *buffer_streams;
3412 int modebuffer[6];
3413 } MainParticle;
3414
3415 static void
3416 MainParticle_transform_mono_i(MainParticle *self)
3417 {
3418 MYFLT dens, inc, index, amp, phase, val;
3419 int i, j, flag = 0;
3420 T_SIZE_T ipart;
3421 MYFLT pit = 0, pos = 0, dur = 0, dev = 0;
3422
3423 MYFLT *tablelist = TableStream_getData((TableStream *)self->table);
3424 T_SIZE_T size = TableStream_getSize((TableStream *)self->table);
3425
3426 MYFLT *envlist = TableStream_getData((TableStream *)self->env);
3427 T_SIZE_T envsize = TableStream_getSize((TableStream *)self->env);
3428
3429 dens = PyFloat_AS_DOUBLE(self->dens);
3430
3431 if (dens < 0.0)
3432 dens = 0.0;
3433
3434 inc = dens * self->oneOnSr * self->devFactor;
3435
3436 for (i = 0; i < self->bufsize * self->chnls; i++)
3437 {
3438 self->buffer_streams[i] = 0.0;
3439 }
3440
3441 for (i = 0; i < self->bufsize; i++)
3442 {
3443 self->timer += inc;
3444
3445 if (self->timer >= 1.0)
3446 {
3447 self->timer -= 1.0;
3448 flag = 1;
3449 }
3450
3451 /* need to start a new grain */
3452 if (flag)
3453 {
3454 for (j = 0; j < MAINPARTICLE_MAX_GRAINS; j++)
3455 {
3456 if (self->flags[j] == 0)
3457 {
3458 self->flags[j] = 1;
3459
3460 if (j >= self->num)
3461 self->num = j + 1;
3462
3463 if (self->modebuffer[1] == 0)
3464 pit = PyFloat_AS_DOUBLE(self->pitch);
3465 else
3466 pit = Stream_getData((Stream *)self->pitch_stream)[i];
3467
3468 if (self->modebuffer[2] == 0)
3469 pos = PyFloat_AS_DOUBLE(self->pos);
3470 else
3471 pos = Stream_getData((Stream *)self->pos_stream)[i];
3472
3473 if (self->modebuffer[3] == 0)
3474 dur = PyFloat_AS_DOUBLE(self->dur);
3475 else
3476 dur = Stream_getData((Stream *)self->dur_stream)[i];
3477
3478 if (self->modebuffer[4] == 0)
3479 dev = PyFloat_AS_DOUBLE(self->dev);
3480 else
3481 dev = Stream_getData((Stream *)self->dev_stream)[i];
3482
3483 if (pit < 0.0)
3484 pit = -pit;
3485
3486 if (pos < 0.0)
3487 pos = 0.0;
3488 else if (pos >= size)
3489 pos = (MYFLT)size;
3490
3491 if (dur < 0.0001)
3492 dur = 0.0001;
3493
3494 if (dev < 0.0)
3495 dev = 0.0;
3496 else if (dev > 1.0)
3497 dev = 1.0;
3498
3499 self->gpos[j] = pos;
3500 self->glen[j] = dur * self->sr * pit * self->srScale;
3501
3502 if ((pos + self->glen[j]) >= size || (pos + self->glen[j]) < 0)
3503 self->flags[j] = 0;
3504
3505 self->phase[j] = 0.0;
3506 self->inc[j] = 1.0 / (dur * self->sr);
3507 self->devFactor = (RANDOM_UNIFORM * 2.0 - 1.0) * dev + 1.0;
3508 break;
3509 }
3510 }
3511 }
3512
3513 /* compute active grains */
3514 for (j = 0; j < self->num; j++)
3515 {
3516 if (self->flags[j])
3517 {
3518 phase = self->phase[j];
3519 /* compute envelope */
3520 index = phase * envsize;
3521 ipart = (T_SIZE_T)index;
3522 amp = envlist[ipart] + (envlist[ipart + 1] - envlist[ipart]) * (index - ipart);
3523 /* compute sampling */
3524 index = phase * self->glen[j] + self->gpos[j];
3525 ipart = (T_SIZE_T)index;
3526 val = (tablelist[ipart] + (tablelist[ipart + 1] - tablelist[ipart]) * (index - ipart)) * amp;
3527 self->buffer_streams[i] += val;
3528 phase += self->inc[j];
3529
3530 if (phase >= 1.0)
3531 self->flags[j] = 0;
3532 else
3533 self->phase[j] = phase;
3534 }
3535 }
3536
3537 flag = 0;
3538 }
3539 }
3540
3541 static void
3542 MainParticle_transform_mono_a(MainParticle *self)
3543 {
3544 MYFLT dens, index, amp, phase, val;
3545 int i, j, flag = 0;
3546 T_SIZE_T ipart;
3547 MYFLT pit = 0, pos = 0, dur = 0, dev = 0;
3548
3549 MYFLT *tablelist = TableStream_getData((TableStream *)self->table);
3550 T_SIZE_T size = TableStream_getSize((TableStream *)self->table);
3551
3552 MYFLT *envlist = TableStream_getData((TableStream *)self->env);
3553 T_SIZE_T envsize = TableStream_getSize((TableStream *)self->env);
3554
3555 MYFLT *density = Stream_getData((Stream *)self->dens_stream);
3556
3557 for (i = 0; i < self->bufsize * self->chnls; i++)
3558 {
3559 self->buffer_streams[i] = 0.0;
3560 }
3561
3562 for (i = 0; i < self->bufsize; i++)
3563 {
3564 if (density[i] < 0.0)
3565 dens = 0.0;
3566 else
3567 dens = density[i];
3568
3569 self->timer += dens * self->oneOnSr * self->devFactor;
3570
3571 if (self->timer >= 1.0)
3572 {
3573 self->timer -= 1.0;
3574 flag = 1;
3575 }
3576
3577 /* need to start a new grain */
3578 if (flag)
3579 {
3580 for (j = 0; j < MAINPARTICLE_MAX_GRAINS; j++)
3581 {
3582 if (self->flags[j] == 0)
3583 {
3584 self->flags[j] = 1;
3585
3586 if (j >= self->num)
3587 self->num = j + 1;
3588
3589 if (self->modebuffer[1] == 0)
3590 pit = PyFloat_AS_DOUBLE(self->pitch);
3591 else
3592 pit = Stream_getData((Stream *)self->pitch_stream)[i];
3593
3594 if (self->modebuffer[2] == 0)
3595 pos = PyFloat_AS_DOUBLE(self->pos);
3596 else
3597 pos = Stream_getData((Stream *)self->pos_stream)[i];
3598
3599 if (self->modebuffer[3] == 0)
3600 dur = PyFloat_AS_DOUBLE(self->dur);
3601 else
3602 dur = Stream_getData((Stream *)self->dur_stream)[i];
3603
3604 if (self->modebuffer[4] == 0)
3605 dev = PyFloat_AS_DOUBLE(self->dev);
3606 else
3607 dev = Stream_getData((Stream *)self->dev_stream)[i];
3608
3609 if (pit < 0.0)
3610 pit = -pit;
3611
3612 if (pos < 0.0)
3613 pos = 0.0;
3614 else if (pos >= size)
3615 pos = (MYFLT)size;
3616
3617 if (dur < 0.0001)
3618 dur = 0.0001;
3619
3620 if (dev < 0.0)
3621 dev = 0.0;
3622 else if (dev > 1.0)
3623 dev = 1.0;
3624
3625 self->gpos[j] = pos;
3626 self->glen[j] = dur * self->sr * pit * self->srScale;
3627
3628 if ((pos + self->glen[j]) >= size || (pos + self->glen[j]) < 0)
3629 self->flags[j] = 0;
3630
3631 self->phase[j] = 0.0;
3632 self->inc[j] = 1.0 / (dur * self->sr);
3633 self->devFactor = (RANDOM_UNIFORM * 2.0 - 1.0) * dev + 1.0;
3634 break;
3635 }
3636 }
3637 }
3638
3639 /* compute active grains */
3640 for (j = 0; j < self->num; j++)
3641 {
3642 if (self->flags[j])
3643 {
3644 phase = self->phase[j];
3645 /* compute envelope */
3646 index = phase * envsize;
3647 ipart = (T_SIZE_T)index;
3648 amp = envlist[ipart] + (envlist[ipart + 1] - envlist[ipart]) * (index - ipart);
3649 /* compute sampling */
3650 index = phase * self->glen[j] + self->gpos[j];
3651 ipart = (T_SIZE_T)index;
3652 val = (tablelist[ipart] + (tablelist[ipart + 1] - tablelist[ipart]) * (index - ipart)) * amp;
3653 self->buffer_streams[i] += val;
3654 phase += self->inc[j];
3655
3656 if (phase >= 1.0)
3657 self->flags[j] = 0;
3658 else
3659 self->phase[j] = phase;
3660 }
3661 }
3662
3663 flag = 0;
3664 }
3665 }
3666
3667 static void
3668 MainParticle_transform_i(MainParticle *self)
3669 {
3670 MYFLT dens, inc, index, amp, phase, val, min = 0;
3671 int i, j, l, l1, flag = 0;
3672 T_SIZE_T ipart;
3673 MYFLT pit = 0, pos = 0, dur = 0, dev = 0, pan = 0;
3674
3675 MYFLT *tablelist = TableStream_getData((TableStream *)self->table);
3676 T_SIZE_T size = TableStream_getSize((TableStream *)self->table);
3677
3678 MYFLT *envlist = TableStream_getData((TableStream *)self->env);
3679 T_SIZE_T envsize = TableStream_getSize((TableStream *)self->env);
3680
3681 dens = PyFloat_AS_DOUBLE(self->dens);
3682
3683 if (dens < 0.0)
3684 dens = 0.0;
3685
3686 inc = dens * self->oneOnSr * self->devFactor;
3687
3688 for (i = 0; i < self->bufsize * self->chnls; i++)
3689 {
3690 self->buffer_streams[i] = 0.0;
3691 }
3692
3693 for (i = 0; i < self->bufsize; i++)
3694 {
3695 self->timer += inc;
3696
3697 if (self->timer >= 1.0)
3698 {
3699 self->timer -= 1.0;
3700 flag = 1;
3701 }
3702
3703 /* need to start a new grain */
3704 if (flag)
3705 {
3706 for (j = 0; j < MAINPARTICLE_MAX_GRAINS; j++)
3707 {
3708 if (self->flags[j] == 0)
3709 {
3710 self->flags[j] = 1;
3711
3712 if (j >= self->num)
3713 self->num = j + 1;
3714
3715 if (self->modebuffer[1] == 0)
3716 pit = PyFloat_AS_DOUBLE(self->pitch);
3717 else
3718 pit = Stream_getData((Stream *)self->pitch_stream)[i];
3719
3720 if (self->modebuffer[2] == 0)
3721 pos = PyFloat_AS_DOUBLE(self->pos);
3722 else
3723 pos = Stream_getData((Stream *)self->pos_stream)[i];
3724
3725 if (self->modebuffer[3] == 0)
3726 dur = PyFloat_AS_DOUBLE(self->dur);
3727 else
3728 dur = Stream_getData((Stream *)self->dur_stream)[i];
3729
3730 if (self->modebuffer[4] == 0)
3731 dev = PyFloat_AS_DOUBLE(self->dev);
3732 else
3733 dev = Stream_getData((Stream *)self->dev_stream)[i];
3734
3735 if (self->modebuffer[5] == 0)
3736 pan = PyFloat_AS_DOUBLE(self->pan);
3737 else
3738 pan = Stream_getData((Stream *)self->pan_stream)[i];
3739
3740 if (pit < 0.0)
3741 pit = -pit;
3742
3743 if (pos < 0.0)
3744 pos = 0.0;
3745 else if (pos >= size)
3746 pos = (MYFLT)size;
3747
3748 if (dur < 0.0001)
3749 dur = 0.0001;
3750
3751 if (dev < 0.0)
3752 dev = 0.0;
3753 else if (dev > 1.0)
3754 dev = 1.0;
3755
3756 if (pan < 0.0)
3757 pan = 0.0;
3758 else if (pan > 1.0)
3759 pan = 1.0;
3760
3761 self->gpos[j] = pos;
3762 self->glen[j] = dur * self->sr * pit * self->srScale;
3763
3764 if ((pos + self->glen[j]) >= size || (pos + self->glen[j]) < 0)
3765 self->flags[j] = 0;
3766
3767 self->phase[j] = 0.0;
3768 self->inc[j] = 1.0 / (dur * self->sr);
3769 self->devFactor = (RANDOM_UNIFORM * 2.0 - 1.0) * dev + 1.0;
3770
3771 if (self->chnls == 2)
3772 {
3773 self->k1[j] = 0;
3774 self->k2[j] = self->bufsize;
3775 self->amp1[j] = MYSQRT(1.0 - pan);
3776 self->amp2[j] = MYSQRT(pan);
3777 }
3778 else
3779 {
3780 self->amp1[j] = MYSQRT(1.0 - pan);
3781 self->amp2[j] = MYSQRT(pan);
3782 min = 0;
3783 self->k1[j] = 0;
3784 self->k2[j] = self->bufsize;
3785
3786 for (l = self->chnls; l > 0; l--)
3787 {
3788 l1 = l - 1;
3789 min = l1 / (MYFLT)self->chnls;
3790
3791 if (pan > min)
3792 {
3793 self->k1[j] = l1 * self->bufsize;
3794
3795 if (l == self->chnls)
3796 self->k2[j] = 0;
3797 else
3798 self->k2[j] = l * self->bufsize;
3799
3800 break;
3801 }
3802 }
3803 }
3804
3805 break;
3806 }
3807 }
3808 }
3809
3810 /* compute active grains */
3811 for (j = 0; j < self->num; j++)
3812 {
3813 if (self->flags[j])
3814 {
3815 phase = self->phase[j];
3816 /* compute envelope */
3817 index = phase * envsize;
3818 ipart = (T_SIZE_T)index;
3819 amp = envlist[ipart] + (envlist[ipart + 1] - envlist[ipart]) * (index - ipart);
3820 /* compute sampling */
3821 index = phase * self->glen[j] + self->gpos[j];
3822 ipart = (T_SIZE_T)index;
3823 val = (tablelist[ipart] + (tablelist[ipart + 1] - tablelist[ipart]) * (index - ipart)) * amp;
3824 self->buffer_streams[i + self->k1[j]] += val * self->amp1[j];
3825 self->buffer_streams[i + self->k2[j]] += val * self->amp2[j];
3826 phase += self->inc[j];
3827
3828 if (phase >= 1.0)
3829 self->flags[j] = 0;
3830 else
3831 self->phase[j] = phase;
3832 }
3833 }
3834
3835 flag = 0;
3836 }
3837 }
3838
3839 static void
3840 MainParticle_transform_a(MainParticle *self)
3841 {
3842 MYFLT dens, index, amp, phase, val, min = 0;
3843 int i, j, l, l1, flag = 0;
3844 T_SIZE_T ipart;
3845 MYFLT pit = 0, pos = 0, dur = 0, dev = 0, pan = 0;
3846
3847 MYFLT *tablelist = TableStream_getData((TableStream *)self->table);
3848 T_SIZE_T size = TableStream_getSize((TableStream *)self->table);
3849
3850 MYFLT *envlist = TableStream_getData((TableStream *)self->env);
3851 T_SIZE_T envsize = TableStream_getSize((TableStream *)self->env);
3852
3853 MYFLT *density = Stream_getData((Stream *)self->dens_stream);
3854
3855 for (i = 0; i < self->bufsize * self->chnls; i++)
3856 {
3857 self->buffer_streams[i] = 0.0;
3858 }
3859
3860 for (i = 0; i < self->bufsize; i++)
3861 {
3862 if (density[i] < 0.0)
3863 dens = 0.0;
3864 else
3865 dens = density[i];
3866
3867 self->timer += dens * self->oneOnSr * self->devFactor;
3868
3869 if (self->timer >= 1.0)
3870 {
3871 self->timer -= 1.0;
3872 flag = 1;
3873 }
3874
3875 /* need to start a new grain */
3876 if (flag)
3877 {
3878 for (j = 0; j < MAINPARTICLE_MAX_GRAINS; j++)
3879 {
3880 if (self->flags[j] == 0)
3881 {
3882 self->flags[j] = 1;
3883
3884 if (j >= self->num)
3885 self->num = j + 1;
3886
3887 if (self->modebuffer[1] == 0)
3888 pit = PyFloat_AS_DOUBLE(self->pitch);
3889 else
3890 pit = Stream_getData((Stream *)self->pitch_stream)[i];
3891
3892 if (self->modebuffer[2] == 0)
3893 pos = PyFloat_AS_DOUBLE(self->pos);
3894 else
3895 pos = Stream_getData((Stream *)self->pos_stream)[i];
3896
3897 if (self->modebuffer[3] == 0)
3898 dur = PyFloat_AS_DOUBLE(self->dur);
3899 else
3900 dur = Stream_getData((Stream *)self->dur_stream)[i];
3901
3902 if (self->modebuffer[4] == 0)
3903 dev = PyFloat_AS_DOUBLE(self->dev);
3904 else
3905 dev = Stream_getData((Stream *)self->dev_stream)[i];
3906
3907 if (self->modebuffer[5] == 0)
3908 pan = PyFloat_AS_DOUBLE(self->pan);
3909 else
3910 pan = Stream_getData((Stream *)self->pan_stream)[i];
3911
3912 if (pit < 0.0)
3913 pit = -pit;
3914
3915 if (pos < 0.0)
3916 pos = 0.0;
3917 else if (pos >= size)
3918 pos = (MYFLT)size;
3919
3920 if (dur < 0.0001)
3921 dur = 0.0001;
3922
3923 if (dev < 0.0)
3924 dev = 0.0;
3925 else if (dev > 1.0)
3926 dev = 1.0;
3927
3928 if (pan < 0.0)
3929 pan = 0.0;
3930 else if (pan > 1.0)
3931 pan = 1.0;
3932
3933 self->gpos[j] = pos;
3934 self->glen[j] = dur * self->sr * pit * self->srScale;
3935
3936 if ((pos + self->glen[j]) >= size || (pos + self->glen[j]) < 0)
3937 self->flags[j] = 0;
3938
3939 self->phase[j] = 0.0;
3940 self->inc[j] = 1.0 / (dur * self->sr);
3941 self->devFactor = (RANDOM_UNIFORM * 2.0 - 1.0) * dev + 1.0;
3942
3943 if (self->chnls == 2)
3944 {
3945 self->k1[j] = 0;
3946 self->k2[j] = self->bufsize;
3947 self->amp1[j] = MYSQRT(1.0 - pan);
3948 self->amp2[j] = MYSQRT(pan);
3949 }
3950 else
3951 {
3952 self->amp1[j] = MYSQRT(1.0 - pan);
3953 self->amp2[j] = MYSQRT(pan);
3954 min = 0;
3955 self->k1[j] = 0;
3956 self->k2[j] = self->bufsize;
3957
3958 for (l = self->chnls; l > 0; l--)
3959 {
3960 l1 = l - 1;
3961 min = l1 / (MYFLT)self->chnls;
3962
3963 if (pan > min)
3964 {
3965 self->k1[j] = l1 * self->bufsize;
3966
3967 if (l == self->chnls)
3968 self->k2[j] = 0;
3969 else
3970 self->k2[j] = l * self->bufsize;
3971
3972 break;
3973 }
3974 }
3975 }
3976
3977 break;
3978 }
3979 }
3980 }
3981
3982 /* compute active grains */
3983 for (j = 0; j < self->num; j++)
3984 {
3985 if (self->flags[j])
3986 {
3987 phase = self->phase[j];
3988 /* compute envelope */
3989 index = phase * envsize;
3990 ipart = (T_SIZE_T)index;
3991 amp = envlist[ipart] + (envlist[ipart + 1] - envlist[ipart]) * (index - ipart);
3992 /* compute sampling */
3993 index = phase * self->glen[j] + self->gpos[j];
3994 ipart = (T_SIZE_T)index;
3995 val = (tablelist[ipart] + (tablelist[ipart + 1] - tablelist[ipart]) * (index - ipart)) * amp;
3996 self->buffer_streams[i + self->k1[j]] += val * self->amp1[j];
3997 self->buffer_streams[i + self->k2[j]] += val * self->amp2[j];
3998 phase += self->inc[j];
3999
4000 if (phase >= 1.0)
4001 self->flags[j] = 0;
4002 else
4003 self->phase[j] = phase;
4004 }
4005 }
4006
4007 flag = 0;
4008 }
4009 }
4010
4011 static void
4012 MainParticle_setProcMode(MainParticle *self)
4013 {
4014 int procmode = self->modebuffer[0];
4015
4016 switch (procmode)
4017 {
4018 case 0:
4019 if (self->chnls == 1)
4020 self->proc_func_ptr = MainParticle_transform_mono_i;
4021 else
4022 self->proc_func_ptr = MainParticle_transform_i;
4023
4024 break;
4025
4026 case 1:
4027 if (self->chnls == 1)
4028 self->proc_func_ptr = MainParticle_transform_mono_a;
4029 else
4030 self->proc_func_ptr = MainParticle_transform_a;
4031
4032 break;
4033 }
4034 }
4035
4036 static void
4037 MainParticle_compute_next_data_frame(MainParticle *self)
4038 {
4039 (*self->proc_func_ptr)(self);
4040 }
4041
4042 static int
4043 MainParticle_traverse(MainParticle *self, visitproc visit, void *arg)
4044 {
4045 pyo_VISIT
4046 Py_VISIT(self->dens);
4047 Py_VISIT(self->pitch);
4048 Py_VISIT(self->pos);
4049 Py_VISIT(self->dur);
4050 Py_VISIT(self->dev);
4051 Py_VISIT(self->pan);
4052 return 0;
4053 }
4054
4055 static int
4056 MainParticle_clear(MainParticle *self)
4057 {
4058 pyo_CLEAR
4059 Py_CLEAR(self->dens);
4060 Py_CLEAR(self->pitch);
4061 Py_CLEAR(self->pos);
4062 Py_CLEAR(self->dur);
4063 Py_CLEAR(self->dev);
4064 Py_CLEAR(self->pan);
4065 return 0;
4066 }
4067
4068 static void
4069 MainParticle_dealloc(MainParticle* self)
4070 {
4071 pyo_DEALLOC
4072 PyMem_RawFree(self->gpos);
4073 PyMem_RawFree(self->glen);
4074 PyMem_RawFree(self->inc);
4075 PyMem_RawFree(self->flags);
4076 PyMem_RawFree(self->k1);
4077 PyMem_RawFree(self->k2);
4078 PyMem_RawFree(self->phase);
4079 PyMem_RawFree(self->amp1);
4080 PyMem_RawFree(self->amp2);
4081 PyMem_RawFree(self->buffer_streams);
4082 MainParticle_clear(self);
4083 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
4084 Py_TYPE(self)->tp_free((PyObject*)self);
4085 }
4086
4087 MYFLT *
4088 MainParticle_getSamplesBuffer(MainParticle *self)
4089 {
4090 return (MYFLT *)self->buffer_streams;
4091 }
4092
4093 static PyObject *
4094 MainParticle_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
4095 {
4096 int i;
4097 PyObject *tabletmp, *envtmp, *denstmp = NULL, *pitchtmp = NULL, *postmp = NULL, *durtmp = NULL, *devtmp = NULL, *pantmp = NULL;
4098 MainParticle *self;
4099 self = (MainParticle *)type->tp_alloc(type, 0);
4100
4101 self->dens = PyFloat_FromDouble(50);
4102 self->pitch = PyFloat_FromDouble(1);
4103 self->pos = PyFloat_FromDouble(0.0);
4104 self->dur = PyFloat_FromDouble(0.1);
4105 self->dev = PyFloat_FromDouble(0);
4106 self->pan = PyFloat_FromDouble(0.5);
4107 self->timer = self->devFactor = 1.0;
4108 self->srScale = 1.0;
4109 self->num = 0;
4110 self->chnls = 1;
4111 self->modebuffer[0] = 0;
4112 self->modebuffer[1] = 0;
4113 self->modebuffer[2] = 0;
4114 self->modebuffer[3] = 0;
4115 self->modebuffer[4] = 0;
4116 self->modebuffer[5] = 0;
4117
4118 INIT_OBJECT_COMMON
4119
4120 self->oneOnSr = 1.0 / self->sr;
4121 self->srOnRandMax = self->sr / (MYFLT)PYO_RAND_MAX;
4122
4123 Stream_setFunctionPtr(self->stream, MainParticle_compute_next_data_frame);
4124 self->mode_func_ptr = MainParticle_setProcMode;
4125
4126 static char *kwlist[] = {"table", "env", "dens", "pitch", "pos", "dur", "dev", "pan", "chnls", NULL};
4127
4128 if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO|OOOOOOi", kwlist, &tabletmp, &envtmp, &denstmp, &pitchtmp, &postmp, &durtmp, &devtmp, &pantmp, &self->chnls))
4129 Py_RETURN_NONE;
4130
4131 if ( PyObject_HasAttrString((PyObject *)tabletmp, "getTableStream") == 0 )
4132 {
4133 PyErr_SetString(PyExc_TypeError, "\"table\" argument of MainParticle must be a PyoTableObject.\n");
4134 Py_RETURN_NONE;
4135 }
4136
4137 self->table = PyObject_CallMethod((PyObject *)tabletmp, "getTableStream", "");
4138 self->srScale = TableStream_getSamplingRate((TableStream *)self->table) / self->sr;
4139
4140 if ( PyObject_HasAttrString((PyObject *)envtmp, "getTableStream") == 0 )
4141 {
4142 PyErr_SetString(PyExc_TypeError, "\"env\" argument of MainParticle must be a PyoTableObject.\n");
4143 Py_RETURN_NONE;
4144 }
4145
4146 self->env = PyObject_CallMethod((PyObject *)envtmp, "getTableStream", "");
4147
4148 if (denstmp)
4149 {
4150 PyObject_CallMethod((PyObject *)self, "setDens", "O", denstmp);
4151 }
4152
4153 if (pitchtmp)
4154 {
4155 PyObject_CallMethod((PyObject *)self, "setPitch", "O", pitchtmp);
4156 }
4157
4158 if (postmp)
4159 {
4160 PyObject_CallMethod((PyObject *)self, "setPos", "O", postmp);
4161 }
4162
4163 if (durtmp)
4164 {
4165 PyObject_CallMethod((PyObject *)self, "setDur", "O", durtmp);
4166 }
4167
4168 if (devtmp)
4169 {
4170 PyObject_CallMethod((PyObject *)self, "setDev", "O", devtmp);
4171 }
4172
4173 if (pantmp)
4174 {
4175 PyObject_CallMethod((PyObject *)self, "setPan", "O", pantmp);
4176 }
4177
4178 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
4179
4180 if (self->chnls < 1)
4181 self->chnls = 1;
4182
4183 self->gpos = (MYFLT *)PyMem_RawRealloc(self->gpos, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
4184 self->glen = (MYFLT *)PyMem_RawRealloc(self->glen, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
4185 self->inc = (MYFLT *)PyMem_RawRealloc(self->inc, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
4186 self->phase = (MYFLT *)PyMem_RawRealloc(self->phase, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
4187 self->amp1 = (MYFLT *)PyMem_RawRealloc(self->amp1, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
4188 self->amp2 = (MYFLT *)PyMem_RawRealloc(self->amp2, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
4189 self->flags = (int *)PyMem_RawRealloc(self->flags, MAINPARTICLE_MAX_GRAINS * sizeof(int));
4190 self->k1 = (int *)PyMem_RawRealloc(self->k1, MAINPARTICLE_MAX_GRAINS * sizeof(int));
4191 self->k2 = (int *)PyMem_RawRealloc(self->k2, MAINPARTICLE_MAX_GRAINS * sizeof(int));
4192
4193 for (i = 0; i < MAINPARTICLE_MAX_GRAINS; i++)
4194 {
4195 self->gpos[i] = self->glen[i] = self->inc[i] = self->phase[i] = self->amp1[i] = self->amp2[i] = 0.0;
4196 self->flags[i] = self->k1[i] = self->k2[i] = 0;
4197 }
4198
4199 self->buffer_streams = (MYFLT *)PyMem_RawRealloc(self->buffer_streams, self->bufsize * self->chnls * sizeof(MYFLT));
4200
4201 for (i = 0; i < self->bufsize * self->chnls; i++)
4202 {
4203 self->buffer_streams[i] = 0.0;
4204 }
4205
4206 Server_generateSeed((Server *)self->server, MAINPARTICLE_ID);
4207
4208 (*self->mode_func_ptr)(self);
4209
4210 return (PyObject *)self;
4211 }
4212
4213 static PyObject * MainParticle_getServer(MainParticle* self) { GET_SERVER };
4214 static PyObject * MainParticle_getStream(MainParticle* self) { GET_STREAM };
4215
4216 static PyObject * MainParticle_play(MainParticle *self, PyObject *args, PyObject *kwds) { PLAY };
4217 static PyObject * MainParticle_stop(MainParticle *self, PyObject *args, PyObject *kwds) { STOP };
4218
4219 static PyObject * MainParticle_setDens(MainParticle *self, PyObject *arg) { SET_PARAM(self->dens, self->dens_stream, 0); }
4220 static PyObject * MainParticle_setPitch(MainParticle *self, PyObject *arg) { SET_PARAM(self->pitch, self->pitch_stream, 1); }
4221 static PyObject * MainParticle_setPos(MainParticle *self, PyObject *arg) { SET_PARAM(self->pos, self->pos_stream, 2); }
4222 static PyObject * MainParticle_setDur(MainParticle *self, PyObject *arg) { SET_PARAM(self->dur, self->dur_stream, 3); }
4223 static PyObject * MainParticle_setDev(MainParticle *self, PyObject *arg) { SET_PARAM(self->dev, self->dev_stream, 4); }
4224 static PyObject * MainParticle_setPan(MainParticle *self, PyObject *arg) { SET_PARAM(self->pan, self->pan_stream, 5); }
4225
4226 static PyObject *
4227 MainParticle_getTable(MainParticle* self)
4228 {
4229 Py_INCREF(self->table);
4230 return self->table;
4231 };
4232
4233 static PyObject *
4234 MainParticle_setTable(MainParticle *self, PyObject *arg)
4235 {
4236 ASSERT_ARG_NOT_NULL
4237
4238 Py_DECREF(self->table);
4239 self->table = PyObject_CallMethod((PyObject *)arg, "getTableStream", "");
4240 self->srScale = TableStream_getSamplingRate((TableStream *)self->table) / self->sr;
4241
4242 Py_RETURN_NONE;
4243 }
4244
4245 static PyObject *
4246 MainParticle_getEnv(MainParticle* self)
4247 {
4248 Py_INCREF(self->env);
4249 return self->env;
4250 };
4251
4252 static PyObject *
4253 MainParticle_setEnv(MainParticle *self, PyObject *arg)
4254 {
4255 ASSERT_ARG_NOT_NULL
4256
4257 Py_DECREF(self->env);
4258 self->env = PyObject_CallMethod((PyObject *)arg, "getTableStream", "");
4259
4260 Py_RETURN_NONE;
4261 }
4262
4263 static PyMemberDef MainParticle_members[] =
4264 {
4265 {"server", T_OBJECT_EX, offsetof(MainParticle, server), 0, "Pyo server."},
4266 {"stream", T_OBJECT_EX, offsetof(MainParticle, stream), 0, "Stream object."},
4267 {"table", T_OBJECT_EX, offsetof(MainParticle, table), 0, "Sound table."},
4268 {"env", T_OBJECT_EX, offsetof(MainParticle, env), 0, "Envelope table."},
4269 {"dens", T_OBJECT_EX, offsetof(MainParticle, dens), 0, "Density of grains per second."},
4270 {"pitch", T_OBJECT_EX, offsetof(MainParticle, pitch), 0, "Speed of the reading pointer."},
4271 {"pos", T_OBJECT_EX, offsetof(MainParticle, pos), 0, "Position in the sound table."},
4272 {"dur", T_OBJECT_EX, offsetof(MainParticle, dur), 0, "Duration of each grains."},
4273 {"dev", T_OBJECT_EX, offsetof(MainParticle, dev), 0, "Grain start point deviation factor."},
4274 {"pan", T_OBJECT_EX, offsetof(MainParticle, pan), 0, "Grain panning factor."},
4275 {NULL} /* Sentinel */
4276 };
4277
4278 static PyMethodDef MainParticle_methods[] =
4279 {
4280 {"getTable", (PyCFunction)MainParticle_getTable, METH_NOARGS, "Returns sound table object."},
4281 {"setTable", (PyCFunction)MainParticle_setTable, METH_O, "Sets sound table."},
4282 {"getEnv", (PyCFunction)MainParticle_getEnv, METH_NOARGS, "Returns envelope table object."},
4283 {"setEnv", (PyCFunction)MainParticle_setEnv, METH_O, "Sets envelope table."},
4284 {"getServer", (PyCFunction)MainParticle_getServer, METH_NOARGS, "Returns server object."},
4285 {"_getStream", (PyCFunction)MainParticle_getStream, METH_NOARGS, "Returns stream object."},
4286 {"play", (PyCFunction)MainParticle_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
4287 {"stop", (PyCFunction)MainParticle_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
4288 {"setDens", (PyCFunction)MainParticle_setDens, METH_O, "Sets the density of grains per second."},
4289 {"setPitch", (PyCFunction)MainParticle_setPitch, METH_O, "Sets global pitch factor."},
4290 {"setPos", (PyCFunction)MainParticle_setPos, METH_O, "Sets position in the sound table."},
4291 {"setDur", (PyCFunction)MainParticle_setDur, METH_O, "Sets the grain duration."},
4292 {"setDev", (PyCFunction)MainParticle_setDev, METH_O, "Sets grain start point deviation factor."},
4293 {"setPan", (PyCFunction)MainParticle_setPan, METH_O, "Sets grain panning factor."},
4294 {NULL} /* Sentinel */
4295 };
4296
4297 PyTypeObject MainParticleType =
4298 {
4299 PyVarObject_HEAD_INIT(NULL, 0)
4300 "_pyo.MainParticle_base", /*tp_name*/
4301 sizeof(MainParticle), /*tp_basicpitch*/
4302 0, /*tp_itempitch*/
4303 (destructor)MainParticle_dealloc, /*tp_dealloc*/
4304 0, /*tp_print*/
4305 0, /*tp_getattr*/
4306 0, /*tp_setattr*/
4307 0, /*tp_as_async (tp_compare in Python 2)*/
4308 0, /*tp_repr*/
4309 0, /*tp_as_number*/
4310 0, /*tp_as_sequence*/
4311 0, /*tp_as_mapping*/
4312 0, /*tp_hash */
4313 0, /*tp_call*/
4314 0, /*tp_str*/
4315 0, /*tp_getattro*/
4316 0, /*tp_setattro*/
4317 0, /*tp_as_buffer*/
4318 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
4319 "MainParticle objects. Accumulation of multiples grains of sound.", /* tp_doc */
4320 (traverseproc)MainParticle_traverse, /* tp_traverse */
4321 (inquiry)MainParticle_clear, /* tp_clear */
4322 0, /* tp_richcompare */
4323 0, /* tp_weaklistoffset */
4324 0, /* tp_iter */
4325 0, /* tp_iternext */
4326 MainParticle_methods, /* tp_methods */
4327 MainParticle_members, /* tp_members */
4328 0, /* tp_getset */
4329 0, /* tp_base */
4330 0, /* tp_dict */
4331 0, /* tp_descr_get */
4332 0, /* tp_descr_set */
4333 0, /* tp_dictoffset */
4334 0, /* tp_init */
4335 0, /* tp_alloc */
4336 MainParticle_new, /* tp_new */
4337 };
4338
4339 typedef struct
4340 {
4341 pyo_audio_HEAD
4342 MainParticle *mainSplitter;
4343 int modebuffer[2];
4344 int chnl; // panning order
4345 } Particle;
4346
4347 static void Particle_postprocessing_ii(Particle *self) { POST_PROCESSING_II };
4348 static void Particle_postprocessing_ai(Particle *self) { POST_PROCESSING_AI };
4349 static void Particle_postprocessing_ia(Particle *self) { POST_PROCESSING_IA };
4350 static void Particle_postprocessing_aa(Particle *self) { POST_PROCESSING_AA };
4351 static void Particle_postprocessing_ireva(Particle *self) { POST_PROCESSING_IREVA };
4352 static void Particle_postprocessing_areva(Particle *self) { POST_PROCESSING_AREVA };
4353 static void Particle_postprocessing_revai(Particle *self) { POST_PROCESSING_REVAI };
4354 static void Particle_postprocessing_revaa(Particle *self) { POST_PROCESSING_REVAA };
4355 static void Particle_postprocessing_revareva(Particle *self) { POST_PROCESSING_REVAREVA };
4356
4357 static void
4358 Particle_setProcMode(Particle *self)
4359 {
4360 int muladdmode;
4361 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
4362
4363 switch (muladdmode)
4364 {
4365 case 0:
4366 self->muladd_func_ptr = Particle_postprocessing_ii;
4367 break;
4368
4369 case 1:
4370 self->muladd_func_ptr = Particle_postprocessing_ai;
4371 break;
4372
4373 case 2:
4374 self->muladd_func_ptr = Particle_postprocessing_revai;
4375 break;
4376
4377 case 10:
4378 self->muladd_func_ptr = Particle_postprocessing_ia;
4379 break;
4380
4381 case 11:
4382 self->muladd_func_ptr = Particle_postprocessing_aa;
4383 break;
4384
4385 case 12:
4386 self->muladd_func_ptr = Particle_postprocessing_revaa;
4387 break;
4388
4389 case 20:
4390 self->muladd_func_ptr = Particle_postprocessing_ireva;
4391 break;
4392
4393 case 21:
4394 self->muladd_func_ptr = Particle_postprocessing_areva;
4395 break;
4396
4397 case 22:
4398 self->muladd_func_ptr = Particle_postprocessing_revareva;
4399 break;
4400 }
4401 }
4402
4403 static void
4404 Particle_compute_next_data_frame(Particle *self)
4405 {
4406 int i;
4407 MYFLT *tmp;
4408 int offset = self->chnl * self->bufsize;
4409 tmp = MainParticle_getSamplesBuffer((MainParticle *)self->mainSplitter);
4410
4411 for (i = 0; i < self->bufsize; i++)
4412 {
4413 self->data[i] = tmp[i + offset];
4414 }
4415
4416 (*self->muladd_func_ptr)(self);
4417 }
4418
4419 static int
4420 Particle_traverse(Particle *self, visitproc visit, void *arg)
4421 {
4422 pyo_VISIT
4423 Py_VISIT(self->mainSplitter);
4424 return 0;
4425 }
4426
4427 static int
4428 Particle_clear(Particle *self)
4429 {
4430 pyo_CLEAR
4431 Py_CLEAR(self->mainSplitter);
4432 return 0;
4433 }
4434
4435 static void
4436 Particle_dealloc(Particle* self)
4437 {
4438 pyo_DEALLOC
4439 Particle_clear(self);
4440 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
4441 Py_TYPE(self)->tp_free((PyObject*)self);
4442 }
4443
4444 static PyObject *
4445 Particle_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
4446 {
4447 int i;
4448 PyObject *maintmp = NULL, *multmp = NULL, *addtmp = NULL;
4449 Particle *self;
4450 self = (Particle *)type->tp_alloc(type, 0);
4451
4452 self->modebuffer[0] = 0;
4453 self->modebuffer[1] = 0;
4454
4455 INIT_OBJECT_COMMON
4456 Stream_setFunctionPtr(self->stream, Particle_compute_next_data_frame);
4457 self->mode_func_ptr = Particle_setProcMode;
4458
4459 static char *kwlist[] = {"mainSplitter", "chnl", "mul", "add", NULL};
4460
4461 if (! PyArg_ParseTupleAndKeywords(args, kwds, "Oi|OO", kwlist, &maintmp, &self->chnl, &multmp, &addtmp))
4462 Py_RETURN_NONE;
4463
4464 self->mainSplitter = (MainParticle *)maintmp;
4465 Py_INCREF(self->mainSplitter);
4466
4467 if (multmp)
4468 {
4469 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
4470 }
4471
4472 if (addtmp)
4473 {
4474 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
4475 }
4476
4477 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
4478
4479 (*self->mode_func_ptr)(self);
4480
4481 return (PyObject *)self;
4482 }
4483
4484 static PyObject * Particle_getServer(Particle* self) { GET_SERVER };
4485 static PyObject * Particle_getStream(Particle* self) { GET_STREAM };
4486 static PyObject * Particle_setMul(Particle *self, PyObject *arg) { SET_MUL };
4487 static PyObject * Particle_setAdd(Particle *self, PyObject *arg) { SET_ADD };
4488 static PyObject * Particle_setSub(Particle *self, PyObject *arg) { SET_SUB };
4489 static PyObject * Particle_setDiv(Particle *self, PyObject *arg) { SET_DIV };
4490
4491 static PyObject * Particle_play(Particle *self, PyObject *args, PyObject *kwds) { PLAY };
4492 static PyObject * Particle_out(Particle *self, PyObject *args, PyObject *kwds) { OUT };
4493 static PyObject * Particle_stop(Particle *self, PyObject *args, PyObject *kwds) { STOP };
4494
4495 static PyObject * Particle_multiply(Particle *self, PyObject *arg) { MULTIPLY };
4496 static PyObject * Particle_inplace_multiply(Particle *self, PyObject *arg) { INPLACE_MULTIPLY };
4497 static PyObject * Particle_add(Particle *self, PyObject *arg) { ADD };
4498 static PyObject * Particle_inplace_add(Particle *self, PyObject *arg) { INPLACE_ADD };
4499 static PyObject * Particle_sub(Particle *self, PyObject *arg) { SUB };
4500 static PyObject * Particle_inplace_sub(Particle *self, PyObject *arg) { INPLACE_SUB };
4501 static PyObject * Particle_div(Particle *self, PyObject *arg) { DIV };
4502 static PyObject * Particle_inplace_div(Particle *self, PyObject *arg) { INPLACE_DIV };
4503
4504 static PyMemberDef Particle_members[] =
4505 {
4506 {"server", T_OBJECT_EX, offsetof(Particle, server), 0, "Pyo server."},
4507 {"stream", T_OBJECT_EX, offsetof(Particle, stream), 0, "Stream object."},
4508 {"mul", T_OBJECT_EX, offsetof(Particle, mul), 0, "Mul factor."},
4509 {"add", T_OBJECT_EX, offsetof(Particle, add), 0, "Add factor."},
4510 {NULL} /* Sentinel */
4511 };
4512
4513 static PyMethodDef Particle_methods[] =
4514 {
4515 {"getServer", (PyCFunction)Particle_getServer, METH_NOARGS, "Returns server object."},
4516 {"_getStream", (PyCFunction)Particle_getStream, METH_NOARGS, "Returns stream object."},
4517 {"play", (PyCFunction)Particle_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
4518 {"out", (PyCFunction)Particle_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
4519 {"stop", (PyCFunction)Particle_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
4520 {"setMul", (PyCFunction)Particle_setMul, METH_O, "Sets Particle mul factor."},
4521 {"setAdd", (PyCFunction)Particle_setAdd, METH_O, "Sets Particle add factor."},
4522 {"setSub", (PyCFunction)Particle_setSub, METH_O, "Sets inverse add factor."},
4523 {"setDiv", (PyCFunction)Particle_setDiv, METH_O, "Sets inverse mul factor."},
4524 {NULL} /* Sentinel */
4525 };
4526
4527 static PyNumberMethods Particle_as_number =
4528 {
4529 (binaryfunc)Particle_add, /*nb_add*/
4530 (binaryfunc)Particle_sub, /*nb_subtract*/
4531 (binaryfunc)Particle_multiply, /*nb_multiply*/
4532 0, /*nb_remainder*/
4533 0, /*nb_divmod*/
4534 0, /*nb_power*/
4535 0, /*nb_neg*/
4536 0, /*nb_pos*/
4537 0, /*(unaryfunc)array_abs,*/
4538 0, /*nb_nonzero*/
4539 0, /*nb_invert*/
4540 0, /*nb_lshift*/
4541 0, /*nb_rshift*/
4542 0, /*nb_and*/
4543 0, /*nb_xor*/
4544 0, /*nb_or*/
4545 0, /*nb_int*/
4546 0, /*nb_long*/
4547 0, /*nb_float*/
4548 (binaryfunc)Particle_inplace_add, /*inplace_add*/
4549 (binaryfunc)Particle_inplace_sub, /*inplace_subtract*/
4550 (binaryfunc)Particle_inplace_multiply, /*inplace_multiply*/
4551 0, /*inplace_remainder*/
4552 0, /*inplace_power*/
4553 0, /*inplace_lshift*/
4554 0, /*inplace_rshift*/
4555 0, /*inplace_and*/
4556 0, /*inplace_xor*/
4557 0, /*inplace_or*/
4558 0, /*nb_floor_divide*/
4559 (binaryfunc)Particle_div, /*nb_true_divide*/
4560 0, /*nb_inplace_floor_divide*/
4561 (binaryfunc)Particle_inplace_div, /*nb_inplace_true_divide*/
4562 0, /* nb_index */
4563 };
4564
4565 PyTypeObject ParticleType =
4566 {
4567 PyVarObject_HEAD_INIT(NULL, 0)
4568 "_pyo.Particle_base", /*tp_name*/
4569 sizeof(Particle), /*tp_basicsize*/
4570 0, /*tp_itemsize*/
4571 (destructor)Particle_dealloc, /*tp_dealloc*/
4572 0, /*tp_print*/
4573 0, /*tp_getattr*/
4574 0, /*tp_setattr*/
4575 0, /*tp_as_async (tp_compare in Python 2)*/
4576 0, /*tp_repr*/
4577 &Particle_as_number, /*tp_as_number*/
4578 0, /*tp_as_sequence*/
4579 0, /*tp_as_mapping*/
4580 0, /*tp_hash */
4581 0, /*tp_call*/
4582 0, /*tp_str*/
4583 0, /*tp_getattro*/
4584 0, /*tp_setattro*/
4585 0, /*tp_as_buffer*/
4586 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
4587 "Particle objects. Reads one band from a MainParticle object.", /* tp_doc */
4588 (traverseproc)Particle_traverse, /* tp_traverse */
4589 (inquiry)Particle_clear, /* tp_clear */
4590 0, /* tp_richcompare */
4591 0, /* tp_weaklistoffset */
4592 0, /* tp_iter */
4593 0, /* tp_iternext */
4594 Particle_methods, /* tp_methods */
4595 Particle_members, /* tp_members */
4596 0, /* tp_getset */
4597 0, /* tp_base */
4598 0, /* tp_dict */
4599 0, /* tp_descr_get */
4600 0, /* tp_descr_set */
4601 0, /* tp_dictoffset */
4602 0, /* tp_init */
4603 0, /* tp_alloc */
4604 Particle_new, /* tp_new */
4605 };
4606
4607 typedef struct
4608 {
4609 pyo_audio_HEAD
4610 PyObject *table;
4611 PyObject *env;
4612 PyObject *dens;
4613 Stream *dens_stream;
4614 PyObject *pitch;
4615 Stream *pitch_stream;
4616 PyObject *pos;
4617 Stream *pos_stream;
4618 PyObject *dur;
4619 Stream *dur_stream;
4620 PyObject *dev;
4621 Stream *dev_stream;
4622 PyObject *pan;
4623 Stream *pan_stream;
4624
4625 PyObject *filterfreq;
4626 Stream *filterfreq_stream;
4627 PyObject *filterq;
4628 Stream *filterq_stream;
4629 PyObject *filtertype;
4630 Stream *filtertype_stream;
4631 // filters last values
4632 MYFLT *last_freq;
4633 MYFLT *last_q;
4634 MYFLT *last_type;
4635 // sample memories
4636 MYFLT *x1;
4637 MYFLT *x2;
4638 MYFLT *y1;
4639 MYFLT *y2;
4640 // variables
4641 MYFLT *c;
4642 MYFLT *w0;
4643 MYFLT *alpha;
4644 MYFLT *qcomp;
4645 // coefficients
4646 MYFLT *b0;
4647 MYFLT *b1;
4648 MYFLT *b2;
4649 MYFLT *a0;
4650 MYFLT *a1;
4651 MYFLT *a2;
4652
4653 MYFLT *gpos;
4654 MYFLT *glen;
4655 MYFLT *inc;
4656 MYFLT *phase;
4657 MYFLT *amp1;
4658 MYFLT *amp2;
4659 int *flags;
4660 int *k1;
4661 int *k2;
4662 int num;
4663 int chnls;
4664 double timer;
4665 double devFactor;
4666 double srScale;
4667 MYFLT oneOnSr;
4668 MYFLT nyquist;
4669 MYFLT twopiOverSr;
4670 MYFLT srOnRandMax;
4671 MYFLT *buffer_streams;
4672 int modebuffer[9];
4673 } MainParticle2;
4674
4675 static void
4676 MainParticle2_compute_coeffs_lp(MainParticle2 *self, int which)
4677 {
4678 self->b0[which] = self->b2[which] = (1 - self->c[which]) / 2;
4679 self->b1[which] = 1 - self->c[which];
4680 self->a0[which] = 1.0 / (1 + self->alpha[which]);
4681 self->a1[which] = -2 * self->c[which];
4682 self->a2[which] = 1 - self->alpha[which];
4683 }
4684
4685 static void
4686 MainParticle2_compute_coeffs_hp(MainParticle2 *self, int which)
4687 {
4688 self->b0[which] = self->b2[which] = (1 + self->c[which]) / 2;
4689 self->b1[which] = -(1 + self->c[which]);
4690 self->a0[which] = 1.0 / (1 + self->alpha[which]);
4691 self->a1[which] = -2 * self->c[which];
4692 self->a2[which] = 1 - self->alpha[which];
4693 }
4694
4695 static void
4696 MainParticle2_compute_coeffs_bp(MainParticle2 *self, int which)
4697 {
4698 self->b0[which] = self->alpha[which];
4699 self->b1[which] = 0;
4700 self->b2[which] = -self->alpha[which];
4701 self->a0[which] = 1.0 / (1 + self->alpha[which]);
4702 self->a1[which] = -2 * self->c[which];
4703 self->a2[which] = 1 - self->alpha[which];
4704 }
4705
4706 static void
4707 MainParticle2_compute_coeffs_bs(MainParticle2 *self, int which)
4708 {
4709 self->b0[which] = self->b2[which] = 1;
4710 self->b1[which] = self->a1[which] = -2 * self->c[which];
4711 self->a0[which] = 1.0 / (1 + self->alpha[which]);
4712 self->a2[which] = 1 - self->alpha[which];
4713 }
4714
4715 static void
4716 MainParticle2_compute_coeffs_ap(MainParticle2 *self, int which)
4717 {
4718 self->b0[which] = self->a2[which] = 1 - self->alpha[which];
4719 self->b1[which] = self->a1[which] = -2 * self->c[which];
4720 self->b2[which] = 1 + self->alpha[which];
4721 self->a0[which] = 1.0 / (1 + self->alpha[which]);
4722 }
4723
4724 static void
4725 MainParticle2_compute_variables(MainParticle2 *self, MYFLT freq, MYFLT q, int which)
4726 {
4727 self->w0[which] = freq * self->twopiOverSr;
4728 self->c[which] = MYCOS(self->w0[which]);
4729 self->alpha[which] = MYSIN(self->w0[which]) / (2 * q);
4730 }
4731
4732 static void
4733 MainParticle2_transform_mono_i(MainParticle2 *self)
4734 {
4735 MYFLT dens, inc, index, amp, phase, val, fval;
4736 int i, j, flag = 0;
4737 T_SIZE_T ipart;
4738 MYFLT pit = 0, pos = 0, dur = 0, dev = 0, filterfreq = 0, filterq = 0, filtertype = 0;
4739
4740 MYFLT *tablelist = TableStream_getData((TableStream *)self->table);
4741 T_SIZE_T size = TableStream_getSize((TableStream *)self->table);
4742
4743 MYFLT *envlist = TableStream_getData((TableStream *)self->env);
4744 T_SIZE_T envsize = TableStream_getSize((TableStream *)self->env);
4745
4746 dens = PyFloat_AS_DOUBLE(self->dens);
4747
4748 if (dens < 0.0)
4749 dens = 0.0;
4750
4751 inc = dens * self->oneOnSr * self->devFactor;
4752
4753 for (i = 0; i < self->bufsize * self->chnls; i++)
4754 {
4755 self->buffer_streams[i] = 0.0;
4756 }
4757
4758 for (i = 0; i < self->bufsize; i++)
4759 {
4760 self->timer += inc;
4761
4762 if (self->timer >= 1.0)
4763 {
4764 self->timer -= 1.0;
4765 flag = 1;
4766 }
4767
4768 /* need to start a new grain */
4769 if (flag)
4770 {
4771 for (j = 0; j < MAINPARTICLE_MAX_GRAINS; j++)
4772 {
4773 if (self->flags[j] == 0)
4774 {
4775 self->flags[j] = 1;
4776
4777 if (j >= self->num)
4778 self->num = j + 1;
4779
4780 if (self->modebuffer[1] == 0)
4781 pit = PyFloat_AS_DOUBLE(self->pitch);
4782 else
4783 pit = Stream_getData((Stream *)self->pitch_stream)[i];
4784
4785 if (self->modebuffer[2] == 0)
4786 pos = PyFloat_AS_DOUBLE(self->pos);
4787 else
4788 pos = Stream_getData((Stream *)self->pos_stream)[i];
4789
4790 if (self->modebuffer[3] == 0)
4791 dur = PyFloat_AS_DOUBLE(self->dur);
4792 else
4793 dur = Stream_getData((Stream *)self->dur_stream)[i];
4794
4795 if (self->modebuffer[4] == 0)
4796 dev = PyFloat_AS_DOUBLE(self->dev);
4797 else
4798 dev = Stream_getData((Stream *)self->dev_stream)[i];
4799
4800 if (self->modebuffer[6] == 0)
4801 filterfreq = PyFloat_AS_DOUBLE(self->filterfreq);
4802 else
4803 filterfreq = Stream_getData((Stream *)self->filterfreq_stream)[i];
4804
4805 if (self->modebuffer[7] == 0)
4806 filterq = PyFloat_AS_DOUBLE(self->filterq);
4807 else
4808 filterq = Stream_getData((Stream *)self->filterq_stream)[i];
4809
4810 if (self->modebuffer[8] == 0)
4811 filtertype = PyFloat_AS_DOUBLE(self->filtertype);
4812 else
4813 filtertype = Stream_getData((Stream *)self->filtertype_stream)[i];
4814
4815 if (pit < 0.0)
4816 pit = -pit;
4817
4818 if (pos < 0.0)
4819 pos = 0.0;
4820 else if (pos >= size)
4821 pos = (MYFLT)size;
4822
4823 if (dur < 0.0001)
4824 dur = 0.0001;
4825
4826 if (dev < 0.0)
4827 dev = 0.0;
4828 else if (dev > 1.0)
4829 dev = 1.0;
4830
4831 if (filterfreq < 1.0)
4832 filterfreq = 1.0;
4833 else if (filterfreq > self->nyquist)
4834 filterfreq = self->nyquist;
4835
4836 if (filterq < 0.1)
4837 filterq = 0.1;
4838
4839 if (filtertype < 0)
4840 filtertype = 0;
4841 else if (filtertype > 4)
4842 filtertype = 4;
4843
4844 filtertype = MYROUND(filtertype);
4845 self->gpos[j] = pos;
4846 self->glen[j] = dur * self->sr * pit * self->srScale;
4847
4848 if ((pos + self->glen[j]) >= size || (pos + self->glen[j]) < 0)
4849 self->flags[j] = 0;
4850
4851 self->phase[j] = 0.0;
4852 self->inc[j] = 1.0 / (dur * self->sr);
4853 self->devFactor = (RANDOM_UNIFORM * 2.0 - 1.0) * dev + 1.0;
4854
4855 if (filtertype != self->last_type[j] || filterfreq != self->last_freq[j] || filterq != self->last_q[j])
4856 {
4857 self->last_freq[j] = filterfreq;
4858 self->last_q[j] = filterq;
4859 self->last_type[j] = filtertype;
4860 MainParticle2_compute_variables(self, filterfreq, filterq, j);
4861 self->qcomp[j] = 1.0;
4862 self->b0[j] = self->b1[j] = self->b2[j] = self->a0[j] = self->a1[j] = self->a2[j] = 0.0;
4863
4864 if (filtertype == 0)
4865 MainParticle2_compute_coeffs_lp(self, j);
4866 else if (filtertype == 1)
4867 MainParticle2_compute_coeffs_hp(self, j);
4868 else if (filtertype == 2)
4869 {
4870 MainParticle2_compute_coeffs_bp(self, j);
4871 filterq = filterq < 10.0 ? filterq : 10.0;
4872 self->qcomp[j] = MYPOW(10, (filterq - 1) * 0.1);
4873 }
4874 else if (filtertype == 3)
4875 MainParticle2_compute_coeffs_bs(self, j);
4876 else if (filtertype == 4)
4877 MainParticle2_compute_coeffs_ap(self, j);
4878 }
4879
4880 break;
4881 }
4882 }
4883 }
4884
4885 /* compute active grains */
4886 for (j = 0; j < self->num; j++)
4887 {
4888 if (self->flags[j])
4889 {
4890 phase = self->phase[j];
4891 /* compute envelope */
4892 index = phase * envsize;
4893 ipart = (T_SIZE_T)index;
4894 amp = envlist[ipart] + (envlist[ipart + 1] - envlist[ipart]) * (index - ipart);
4895 /* compute sampling */
4896 index = phase * self->glen[j] + self->gpos[j];
4897 ipart = (T_SIZE_T)index;
4898 val = (tablelist[ipart] + (tablelist[ipart + 1] - tablelist[ipart]) * (index - ipart));
4899 /* filtering */
4900 fval = ( (self->b0[j] * val) + (self->b1[j] * self->x1[j]) + (self->b2[j] * self->x2[j]) - (self->a1[j] * self->y1[j]) - (self->a2[j] * self->y2[j]) ) * self->a0[j];
4901 self->y2[j] = self->y1[j];
4902 self->y1[j] = fval;
4903 self->x2[j] = self->x1[j];
4904 self->x1[j] = val;
4905 fval *= self->qcomp[j];
4906 /* Add to output streams */
4907 fval *= amp;
4908 self->buffer_streams[i] += fval;
4909 phase += self->inc[j];
4910
4911 if (phase >= 1.0)
4912 self->flags[j] = 0;
4913 else
4914 self->phase[j] = phase;
4915 }
4916 }
4917
4918 flag = 0;
4919 }
4920 }
4921
4922 static void
4923 MainParticle2_transform_mono_a(MainParticle2 *self)
4924 {
4925 MYFLT dens, index, amp, phase, val, fval;
4926 int i, j, flag = 0;
4927 T_SIZE_T ipart;
4928 MYFLT pit = 0, pos = 0, dur = 0, dev = 0, filterfreq = 0, filterq = 0, filtertype = 0;
4929
4930 MYFLT *tablelist = TableStream_getData((TableStream *)self->table);
4931 T_SIZE_T size = TableStream_getSize((TableStream *)self->table);
4932
4933 MYFLT *envlist = TableStream_getData((TableStream *)self->env);
4934 T_SIZE_T envsize = TableStream_getSize((TableStream *)self->env);
4935
4936 MYFLT *density = Stream_getData((Stream *)self->dens_stream);
4937
4938 for (i = 0; i < self->bufsize * self->chnls; i++)
4939 {
4940 self->buffer_streams[i] = 0.0;
4941 }
4942
4943 for (i = 0; i < self->bufsize; i++)
4944 {
4945 if (density[i] < 0.0)
4946 dens = 0.0;
4947 else
4948 dens = density[i];
4949
4950 self->timer += dens * self->oneOnSr * self->devFactor;
4951
4952 if (self->timer >= 1.0)
4953 {
4954 self->timer -= 1.0;
4955 flag = 1;
4956 }
4957
4958 /* need to start a new grain */
4959 if (flag)
4960 {
4961 for (j = 0; j < MAINPARTICLE_MAX_GRAINS; j++)
4962 {
4963 if (self->flags[j] == 0)
4964 {
4965 self->flags[j] = 1;
4966
4967 if (j >= self->num)
4968 self->num = j + 1;
4969
4970 if (self->modebuffer[1] == 0)
4971 pit = PyFloat_AS_DOUBLE(self->pitch);
4972 else
4973 pit = Stream_getData((Stream *)self->pitch_stream)[i];
4974
4975 if (self->modebuffer[2] == 0)
4976 pos = PyFloat_AS_DOUBLE(self->pos);
4977 else
4978 pos = Stream_getData((Stream *)self->pos_stream)[i];
4979
4980 if (self->modebuffer[3] == 0)
4981 dur = PyFloat_AS_DOUBLE(self->dur);
4982 else
4983 dur = Stream_getData((Stream *)self->dur_stream)[i];
4984
4985 if (self->modebuffer[4] == 0)
4986 dev = PyFloat_AS_DOUBLE(self->dev);
4987 else
4988 dev = Stream_getData((Stream *)self->dev_stream)[i];
4989
4990 if (self->modebuffer[6] == 0)
4991 filterfreq = PyFloat_AS_DOUBLE(self->filterfreq);
4992 else
4993 filterfreq = Stream_getData((Stream *)self->filterfreq_stream)[i];
4994
4995 if (self->modebuffer[7] == 0)
4996 filterq = PyFloat_AS_DOUBLE(self->filterq);
4997 else
4998 filterq = Stream_getData((Stream *)self->filterq_stream)[i];
4999
5000 if (self->modebuffer[8] == 0)
5001 filtertype = PyFloat_AS_DOUBLE(self->filtertype);
5002 else
5003 filtertype = Stream_getData((Stream *)self->filtertype_stream)[i];
5004
5005 if (pit < 0.0)
5006 pit = -pit;
5007
5008 if (pos < 0.0)
5009 pos = 0.0;
5010 else if (pos >= size)
5011 pos = (MYFLT)size;
5012
5013 if (dur < 0.0001)
5014 dur = 0.0001;
5015
5016 if (dev < 0.0)
5017 dev = 0.0;
5018 else if (dev > 1.0)
5019 dev = 1.0;
5020
5021 if (filterfreq < 1.0)
5022 filterfreq = 1.0;
5023 else if (filterfreq > self->nyquist)
5024 filterfreq = self->nyquist;
5025
5026 if (filterq < 0.1)
5027 filterq = 0.1;
5028
5029 if (filtertype < 0)
5030 filtertype = 0;
5031 else if (filtertype > 4)
5032 filtertype = 4;
5033
5034 filtertype = MYROUND(filtertype);
5035 self->gpos[j] = pos;
5036 self->glen[j] = dur * self->sr * pit * self->srScale;
5037
5038 if ((pos + self->glen[j]) >= size || (pos + self->glen[j]) < 0)
5039 self->flags[j] = 0;
5040
5041 self->phase[j] = 0.0;
5042 self->inc[j] = 1.0 / (dur * self->sr);
5043 self->devFactor = (RANDOM_UNIFORM * 2.0 - 1.0) * dev + 1.0;
5044
5045 if (filtertype != self->last_type[j] || filterfreq != self->last_freq[j] || filterq != self->last_q[j])
5046 {
5047 self->last_freq[j] = filterfreq;
5048 self->last_q[j] = filterq;
5049 self->last_type[j] = filtertype;
5050 MainParticle2_compute_variables(self, filterfreq, filterq, j);
5051 self->qcomp[j] = 1.0;
5052 self->b0[j] = self->b1[j] = self->b2[j] = self->a0[j] = self->a1[j] = self->a2[j] = 0.0;
5053
5054 if (filtertype == 0)
5055 MainParticle2_compute_coeffs_lp(self, j);
5056 else if (filtertype == 1)
5057 MainParticle2_compute_coeffs_hp(self, j);
5058 else if (filtertype == 2)
5059 {
5060 MainParticle2_compute_coeffs_bp(self, j);
5061 filterq = filterq < 10.0 ? filterq : 10.0;
5062 self->qcomp[j] = MYPOW(10, (filterq - 1) * 0.1);
5063 }
5064 else if (filtertype == 3)
5065 MainParticle2_compute_coeffs_bs(self, j);
5066 else if (filtertype == 4)
5067 MainParticle2_compute_coeffs_ap(self, j);
5068 }
5069
5070 break;
5071 }
5072 }
5073 }
5074
5075 /* compute active grains */
5076 for (j = 0; j < self->num; j++)
5077 {
5078 if (self->flags[j])
5079 {
5080 phase = self->phase[j];
5081 /* compute envelope */
5082 index = phase * envsize;
5083 ipart = (T_SIZE_T)index;
5084 amp = envlist[ipart] + (envlist[ipart + 1] - envlist[ipart]) * (index - ipart);
5085 /* compute sampling */
5086 index = phase * self->glen[j] + self->gpos[j];
5087 ipart = (T_SIZE_T)index;
5088 val = (tablelist[ipart] + (tablelist[ipart + 1] - tablelist[ipart]) * (index - ipart));
5089 /* filtering */
5090 fval = ( (self->b0[j] * val) + (self->b1[j] * self->x1[j]) + (self->b2[j] * self->x2[j]) - (self->a1[j] * self->y1[j]) - (self->a2[j] * self->y2[j]) ) * self->a0[j];
5091 self->y2[j] = self->y1[j];
5092 self->y1[j] = fval;
5093 self->x2[j] = self->x1[j];
5094 self->x1[j] = val;
5095 fval *= self->qcomp[j];
5096 /* Add to output streams */
5097 fval *= amp;
5098 self->buffer_streams[i] += fval;
5099 phase += self->inc[j];
5100
5101 if (phase >= 1.0)
5102 self->flags[j] = 0;
5103 else
5104 self->phase[j] = phase;
5105 }
5106 }
5107
5108 flag = 0;
5109 }
5110 }
5111
5112 static void
5113 MainParticle2_transform_i(MainParticle2 *self)
5114 {
5115 MYFLT dens, inc, index, amp, phase, val, fval, min = 0;
5116 int i, j, l, l1, flag = 0;
5117 T_SIZE_T ipart;
5118 MYFLT pit = 0, pos = 0, dur = 0, dev = 0, pan = 0, filterfreq = 0, filterq = 0, filtertype = 0;
5119
5120 MYFLT *tablelist = TableStream_getData((TableStream *)self->table);
5121 T_SIZE_T size = TableStream_getSize((TableStream *)self->table);
5122
5123 MYFLT *envlist = TableStream_getData((TableStream *)self->env);
5124 T_SIZE_T envsize = TableStream_getSize((TableStream *)self->env);
5125
5126 dens = PyFloat_AS_DOUBLE(self->dens);
5127
5128 if (dens < 0.0)
5129 dens = 0.0;
5130
5131 inc = dens * self->oneOnSr * self->devFactor;
5132
5133 for (i = 0; i < self->bufsize * self->chnls; i++)
5134 {
5135 self->buffer_streams[i] = 0.0;
5136 }
5137
5138 for (i = 0; i < self->bufsize; i++)
5139 {
5140 self->timer += inc;
5141
5142 if (self->timer >= 1.0)
5143 {
5144 self->timer -= 1.0;
5145 flag = 1;
5146 }
5147
5148 /* need to start a new grain */
5149 if (flag)
5150 {
5151 for (j = 0; j < MAINPARTICLE_MAX_GRAINS; j++)
5152 {
5153 if (self->flags[j] == 0)
5154 {
5155 self->flags[j] = 1;
5156
5157 if (j >= self->num)
5158 self->num = j + 1;
5159
5160 if (self->modebuffer[1] == 0)
5161 pit = PyFloat_AS_DOUBLE(self->pitch);
5162 else
5163 pit = Stream_getData((Stream *)self->pitch_stream)[i];
5164
5165 if (self->modebuffer[2] == 0)
5166 pos = PyFloat_AS_DOUBLE(self->pos);
5167 else
5168 pos = Stream_getData((Stream *)self->pos_stream)[i];
5169
5170 if (self->modebuffer[3] == 0)
5171 dur = PyFloat_AS_DOUBLE(self->dur);
5172 else
5173 dur = Stream_getData((Stream *)self->dur_stream)[i];
5174
5175 if (self->modebuffer[4] == 0)
5176 dev = PyFloat_AS_DOUBLE(self->dev);
5177 else
5178 dev = Stream_getData((Stream *)self->dev_stream)[i];
5179
5180 if (self->modebuffer[5] == 0)
5181 pan = PyFloat_AS_DOUBLE(self->pan);
5182 else
5183 pan = Stream_getData((Stream *)self->pan_stream)[i];
5184
5185 if (self->modebuffer[6] == 0)
5186 filterfreq = PyFloat_AS_DOUBLE(self->filterfreq);
5187 else
5188 filterfreq = Stream_getData((Stream *)self->filterfreq_stream)[i];
5189
5190 if (self->modebuffer[7] == 0)
5191 filterq = PyFloat_AS_DOUBLE(self->filterq);
5192 else
5193 filterq = Stream_getData((Stream *)self->filterq_stream)[i];
5194
5195 if (self->modebuffer[8] == 0)
5196 filtertype = PyFloat_AS_DOUBLE(self->filtertype);
5197 else
5198 filtertype = Stream_getData((Stream *)self->filtertype_stream)[i];
5199
5200 if (pit < 0.0)
5201 pit = -pit;
5202
5203 if (pos < 0.0)
5204 pos = 0.0;
5205 else if (pos >= size)
5206 pos = (MYFLT)size;
5207
5208 if (dur < 0.0001)
5209 dur = 0.0001;
5210
5211 if (dev < 0.0)
5212 dev = 0.0;
5213 else if (dev > 1.0)
5214 dev = 1.0;
5215
5216 if (pan < 0.0)
5217 pan = 0.0;
5218 else if (pan > 1.0)
5219 pan = 1.0;
5220
5221 if (filterfreq < 1.0)
5222 filterfreq = 1.0;
5223 else if (filterfreq > self->nyquist)
5224 filterfreq = self->nyquist;
5225
5226 if (filterq < 0.1)
5227 filterq = 0.1;
5228
5229 if (filtertype < 0)
5230 filtertype = 0;
5231 else if (filtertype > 4)
5232 filtertype = 4;
5233
5234 filtertype = MYROUND(filtertype);
5235 self->gpos[j] = pos;
5236 self->glen[j] = dur * self->sr * pit * self->srScale;
5237
5238 if ((pos + self->glen[j]) >= size || (pos + self->glen[j]) < 0)
5239 self->flags[j] = 0;
5240
5241 self->phase[j] = 0.0;
5242 self->inc[j] = 1.0 / (dur * self->sr);
5243 self->devFactor = (RANDOM_UNIFORM * 2.0 - 1.0) * dev + 1.0;
5244
5245 if (self->chnls == 2)
5246 {
5247 self->k1[j] = 0;
5248 self->k2[j] = self->bufsize;
5249 self->amp1[j] = MYSQRT(1.0 - pan);
5250 self->amp2[j] = MYSQRT(pan);
5251 }
5252 else
5253 {
5254 self->amp1[j] = MYSQRT(1.0 - pan);
5255 self->amp2[j] = MYSQRT(pan);
5256 min = 0;
5257 self->k1[j] = 0;
5258 self->k2[j] = self->bufsize;
5259
5260 for (l = self->chnls; l > 0; l--)
5261 {
5262 l1 = l - 1;
5263 min = l1 / (MYFLT)self->chnls;
5264
5265 if (pan > min)
5266 {
5267 self->k1[j] = l1 * self->bufsize;
5268
5269 if (l == self->chnls)
5270 self->k2[j] = 0;
5271 else
5272 self->k2[j] = l * self->bufsize;
5273
5274 break;
5275 }
5276 }
5277 }
5278
5279 if (filtertype != self->last_type[j] || filterfreq != self->last_freq[j] || filterq != self->last_q[j])
5280 {
5281 self->last_freq[j] = filterfreq;
5282 self->last_q[j] = filterq;
5283 self->last_type[j] = filtertype;
5284 MainParticle2_compute_variables(self, filterfreq, filterq, j);
5285 self->qcomp[j] = 1.0;
5286 self->b0[j] = self->b1[j] = self->b2[j] = self->a0[j] = self->a1[j] = self->a2[j] = 0.0;
5287
5288 if (filtertype == 0)
5289 MainParticle2_compute_coeffs_lp(self, j);
5290 else if (filtertype == 1)
5291 MainParticle2_compute_coeffs_hp(self, j);
5292 else if (filtertype == 2)
5293 {
5294 MainParticle2_compute_coeffs_bp(self, j);
5295 filterq = filterq < 10.0 ? filterq : 10.0;
5296 self->qcomp[j] = MYPOW(10, (filterq - 1) * 0.1);
5297 }
5298 else if (filtertype == 3)
5299 MainParticle2_compute_coeffs_bs(self, j);
5300 else if (filtertype == 4)
5301 MainParticle2_compute_coeffs_ap(self, j);
5302 }
5303
5304 break;
5305 }
5306 }
5307 }
5308
5309 /* compute active grains */
5310 for (j = 0; j < self->num; j++)
5311 {
5312 if (self->flags[j])
5313 {
5314 phase = self->phase[j];
5315 /* compute envelope */
5316 index = phase * envsize;
5317 ipart = (T_SIZE_T)index;
5318 amp = envlist[ipart] + (envlist[ipart + 1] - envlist[ipart]) * (index - ipart);
5319 /* compute sampling */
5320 index = phase * self->glen[j] + self->gpos[j];
5321 ipart = (T_SIZE_T)index;
5322 val = (tablelist[ipart] + (tablelist[ipart + 1] - tablelist[ipart]) * (index - ipart));
5323 /* filtering */
5324 fval = ( (self->b0[j] * val) + (self->b1[j] * self->x1[j]) + (self->b2[j] * self->x2[j]) - (self->a1[j] * self->y1[j]) - (self->a2[j] * self->y2[j]) ) * self->a0[j];
5325 self->y2[j] = self->y1[j];
5326 self->y1[j] = fval;
5327 self->x2[j] = self->x1[j];
5328 self->x1[j] = val;
5329 fval *= self->qcomp[j];
5330 /* Add to output streams */
5331 fval *= amp;
5332 self->buffer_streams[i + self->k1[j]] += fval * self->amp1[j];
5333 self->buffer_streams[i + self->k2[j]] += fval * self->amp2[j];
5334 phase += self->inc[j];
5335
5336 if (phase >= 1.0)
5337 self->flags[j] = 0;
5338 else
5339 self->phase[j] = phase;
5340 }
5341 }
5342
5343 flag = 0;
5344 }
5345 }
5346
5347 static void
5348 MainParticle2_transform_a(MainParticle2 *self)
5349 {
5350 MYFLT dens, index, amp, phase, val, fval, min = 0;
5351 int i, j, l, l1, flag = 0;
5352 T_SIZE_T ipart;
5353 MYFLT pit = 0, pos = 0, dur = 0, dev = 0, pan = 0, filterfreq = 0, filterq = 0, filtertype = 0;
5354
5355 MYFLT *tablelist = TableStream_getData((TableStream *)self->table);
5356 T_SIZE_T size = TableStream_getSize((TableStream *)self->table);
5357
5358 MYFLT *envlist = TableStream_getData((TableStream *)self->env);
5359 T_SIZE_T envsize = TableStream_getSize((TableStream *)self->env);
5360
5361 MYFLT *density = Stream_getData((Stream *)self->dens_stream);
5362
5363 for (i = 0; i < self->bufsize * self->chnls; i++)
5364 {
5365 self->buffer_streams[i] = 0.0;
5366 }
5367
5368 for (i = 0; i < self->bufsize; i++)
5369 {
5370 if (density[i] < 0.0)
5371 dens = 0.0;
5372 else
5373 dens = density[i];
5374
5375 self->timer += dens * self->oneOnSr * self->devFactor;
5376
5377 if (self->timer >= 1.0)
5378 {
5379 self->timer -= 1.0;
5380 flag = 1;
5381 }
5382
5383 /* need to start a new grain */
5384 if (flag)
5385 {
5386 for (j = 0; j < MAINPARTICLE_MAX_GRAINS; j++)
5387 {
5388 if (self->flags[j] == 0)
5389 {
5390 self->flags[j] = 1;
5391
5392 if (j >= self->num)
5393 self->num = j + 1;
5394
5395 if (self->modebuffer[1] == 0)
5396 pit = PyFloat_AS_DOUBLE(self->pitch);
5397 else
5398 pit = Stream_getData((Stream *)self->pitch_stream)[i];
5399
5400 if (self->modebuffer[2] == 0)
5401 pos = PyFloat_AS_DOUBLE(self->pos);
5402 else
5403 pos = Stream_getData((Stream *)self->pos_stream)[i];
5404
5405 if (self->modebuffer[3] == 0)
5406 dur = PyFloat_AS_DOUBLE(self->dur);
5407 else
5408 dur = Stream_getData((Stream *)self->dur_stream)[i];
5409
5410 if (self->modebuffer[4] == 0)
5411 dev = PyFloat_AS_DOUBLE(self->dev);
5412 else
5413 dev = Stream_getData((Stream *)self->dev_stream)[i];
5414
5415 if (self->modebuffer[5] == 0)
5416 pan = PyFloat_AS_DOUBLE(self->pan);
5417 else
5418 pan = Stream_getData((Stream *)self->pan_stream)[i];
5419
5420 if (self->modebuffer[6] == 0)
5421 filterfreq = PyFloat_AS_DOUBLE(self->filterfreq);
5422 else
5423 filterfreq = Stream_getData((Stream *)self->filterfreq_stream)[i];
5424
5425 if (self->modebuffer[7] == 0)
5426 filterq = PyFloat_AS_DOUBLE(self->filterq);
5427 else
5428 filterq = Stream_getData((Stream *)self->filterq_stream)[i];
5429
5430 if (self->modebuffer[8] == 0)
5431 filtertype = PyFloat_AS_DOUBLE(self->filtertype);
5432 else
5433 filtertype = Stream_getData((Stream *)self->filtertype_stream)[i];
5434
5435 if (pit < 0.0)
5436 pit = -pit;
5437
5438 if (pos < 0.0)
5439 pos = 0.0;
5440 else if (pos >= size)
5441 pos = (MYFLT)size;
5442
5443 if (dur < 0.0001)
5444 dur = 0.0001;
5445
5446 if (dev < 0.0)
5447 dev = 0.0;
5448 else if (dev > 1.0)
5449 dev = 1.0;
5450
5451 if (pan < 0.0)
5452 pan = 0.0;
5453 else if (pan > 1.0)
5454 pan = 1.0;
5455
5456 if (filterfreq < 1.0)
5457 filterfreq = 1.0;
5458 else if (filterfreq > self->nyquist)
5459 filterfreq = self->nyquist;
5460
5461 if (filterq < 0.1)
5462 filterq = 0.1;
5463
5464 if (filtertype < 0)
5465 filtertype = 0;
5466 else if (filtertype > 4)
5467 filtertype = 4;
5468
5469 filtertype = MYROUND(filtertype);
5470 self->gpos[j] = pos;
5471 self->glen[j] = dur * self->sr * pit * self->srScale;
5472
5473 if ((pos + self->glen[j]) >= size || (pos + self->glen[j]) < 0)
5474 self->flags[j] = 0;
5475
5476 self->phase[j] = 0.0;
5477 self->inc[j] = 1.0 / (dur * self->sr);
5478 self->devFactor = (RANDOM_UNIFORM * 2.0 - 1.0) * dev + 1.0;
5479
5480 if (self->chnls == 2)
5481 {
5482 self->k1[j] = 0;
5483 self->k2[j] = self->bufsize;
5484 self->amp1[j] = MYSQRT(1.0 - pan);
5485 self->amp2[j] = MYSQRT(pan);
5486 }
5487 else
5488 {
5489 self->amp1[j] = MYSQRT(1.0 - pan);
5490 self->amp2[j] = MYSQRT(pan);
5491 min = 0;
5492 self->k1[j] = 0;
5493 self->k2[j] = self->bufsize;
5494
5495 for (l = self->chnls; l > 0; l--)
5496 {
5497 l1 = l - 1;
5498 min = l1 / (MYFLT)self->chnls;
5499
5500 if (pan > min)
5501 {
5502 self->k1[j] = l1 * self->bufsize;
5503
5504 if (l == self->chnls)
5505 self->k2[j] = 0;
5506 else
5507 self->k2[j] = l * self->bufsize;
5508
5509 break;
5510 }
5511 }
5512 }
5513
5514 if (filtertype != self->last_type[j] || filterfreq != self->last_freq[j] || filterq != self->last_q[j])
5515 {
5516 self->last_freq[j] = filterfreq;
5517 self->last_q[j] = filterq;
5518 self->last_type[j] = filtertype;
5519 MainParticle2_compute_variables(self, filterfreq, filterq, j);
5520 self->qcomp[j] = 1.0;
5521 self->b0[j] = self->b1[j] = self->b2[j] = self->a0[j] = self->a1[j] = self->a2[j] = 0.0;
5522
5523 if (filtertype == 0)
5524 MainParticle2_compute_coeffs_lp(self, j);
5525 else if (filtertype == 1)
5526 MainParticle2_compute_coeffs_hp(self, j);
5527 else if (filtertype == 2)
5528 {
5529 MainParticle2_compute_coeffs_bp(self, j);
5530 filterq = filterq < 10.0 ? filterq : 10.0;
5531 self->qcomp[j] = MYPOW(10, (filterq - 1) * 0.1);
5532 }
5533 else if (filtertype == 3)
5534 MainParticle2_compute_coeffs_bs(self, j);
5535 else if (filtertype == 4)
5536 MainParticle2_compute_coeffs_ap(self, j);
5537 }
5538
5539 break;
5540 }
5541 }
5542 }
5543
5544 /* compute active grains */
5545 for (j = 0; j < self->num; j++)
5546 {
5547 if (self->flags[j])
5548 {
5549 phase = self->phase[j];
5550 /* compute envelope */
5551 index = phase * envsize;
5552 ipart = (T_SIZE_T)index;
5553 amp = envlist[ipart] + (envlist[ipart + 1] - envlist[ipart]) * (index - ipart);
5554 /* compute sampling */
5555 index = phase * self->glen[j] + self->gpos[j];
5556 ipart = (T_SIZE_T)index;
5557 val = (tablelist[ipart] + (tablelist[ipart + 1] - tablelist[ipart]) * (index - ipart));
5558 /* filtering */
5559 fval = ( (self->b0[j] * val) + (self->b1[j] * self->x1[j]) + (self->b2[j] * self->x2[j]) - (self->a1[j] * self->y1[j]) - (self->a2[j] * self->y2[j]) ) * self->a0[j];
5560 self->y2[j] = self->y1[j];
5561 self->y1[j] = fval;
5562 self->x2[j] = self->x1[j];
5563 self->x1[j] = val;
5564 fval *= self->qcomp[j];
5565 /* Add to output streams */
5566 fval *= amp;
5567 self->buffer_streams[i + self->k1[j]] += fval * self->amp1[j];
5568 self->buffer_streams[i + self->k2[j]] += fval * self->amp2[j];
5569 phase += self->inc[j];
5570
5571 if (phase >= 1.0)
5572 self->flags[j] = 0;
5573 else
5574 self->phase[j] = phase;
5575 }
5576 }
5577
5578 flag = 0;
5579 }
5580 }
5581
5582 static void
5583 MainParticle2_setProcMode(MainParticle2 *self)
5584 {
5585 int procmode = self->modebuffer[0];
5586
5587 switch (procmode)
5588 {
5589 case 0:
5590 if (self->chnls == 1)
5591 self->proc_func_ptr = MainParticle2_transform_mono_i;
5592 else
5593 self->proc_func_ptr = MainParticle2_transform_i;
5594
5595 break;
5596
5597 case 1:
5598 if (self->chnls == 1)
5599 self->proc_func_ptr = MainParticle2_transform_mono_a;
5600 else
5601 self->proc_func_ptr = MainParticle2_transform_a;
5602
5603 break;
5604 }
5605 }
5606
5607 static void
5608 MainParticle2_compute_next_data_frame(MainParticle2 *self)
5609 {
5610 (*self->proc_func_ptr)(self);
5611 }
5612
5613 static int
5614 MainParticle2_traverse(MainParticle2 *self, visitproc visit, void *arg)
5615 {
5616 pyo_VISIT
5617 Py_VISIT(self->dens);
5618 Py_VISIT(self->pitch);
5619 Py_VISIT(self->pos);
5620 Py_VISIT(self->dur);
5621 Py_VISIT(self->dev);
5622 Py_VISIT(self->pan);
5623 Py_VISIT(self->filterfreq);
5624 Py_VISIT(self->filterq);
5625 Py_VISIT(self->filtertype);
5626 return 0;
5627 }
5628
5629 static int
5630 MainParticle2_clear(MainParticle2 *self)
5631 {
5632 pyo_CLEAR
5633 Py_CLEAR(self->dens);
5634 Py_CLEAR(self->pitch);
5635 Py_CLEAR(self->pos);
5636 Py_CLEAR(self->dur);
5637 Py_CLEAR(self->dev);
5638 Py_CLEAR(self->pan);
5639 Py_CLEAR(self->filterfreq);
5640 Py_CLEAR(self->filterq);
5641 Py_CLEAR(self->filtertype);
5642 return 0;
5643 }
5644
5645 static void
5646 MainParticle2_dealloc(MainParticle2* self)
5647 {
5648 pyo_DEALLOC
5649 PyMem_RawFree(self->gpos);
5650 PyMem_RawFree(self->glen);
5651 PyMem_RawFree(self->inc);
5652 PyMem_RawFree(self->flags);
5653 PyMem_RawFree(self->k1);
5654 PyMem_RawFree(self->k2);
5655 PyMem_RawFree(self->phase);
5656 PyMem_RawFree(self->amp1);
5657 PyMem_RawFree(self->amp2);
5658 PyMem_RawFree(self->last_freq);
5659 PyMem_RawFree(self->last_q);
5660 PyMem_RawFree(self->last_type);
5661 PyMem_RawFree(self->x1);
5662 PyMem_RawFree(self->x2);
5663 PyMem_RawFree(self->y1);
5664 PyMem_RawFree(self->y2);
5665 PyMem_RawFree(self->c);
5666 PyMem_RawFree(self->w0);
5667 PyMem_RawFree(self->alpha);
5668 PyMem_RawFree(self->qcomp);
5669 PyMem_RawFree(self->b0);
5670 PyMem_RawFree(self->b1);
5671 PyMem_RawFree(self->b2);
5672 PyMem_RawFree(self->a0);
5673 PyMem_RawFree(self->a1);
5674 PyMem_RawFree(self->a2);
5675 PyMem_RawFree(self->buffer_streams);
5676 MainParticle2_clear(self);
5677 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
5678 Py_TYPE(self)->tp_free((PyObject*)self);
5679 }
5680
5681 MYFLT *
5682 MainParticle2_getSamplesBuffer(MainParticle2 *self)
5683 {
5684 return (MYFLT *)self->buffer_streams;
5685 }
5686
5687 static PyObject *
5688 MainParticle2_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
5689 {
5690 int i;
5691 PyObject *tabletmp, *envtmp, *denstmp = NULL, *pitchtmp = NULL, *postmp = NULL, *durtmp = NULL;
5692 PyObject *devtmp = NULL, *pantmp = NULL, *filterfreqtmp = NULL, *filterqtmp = NULL, *filtertypetmp = NULL;
5693 MainParticle2 *self;
5694 self = (MainParticle2 *)type->tp_alloc(type, 0);
5695
5696 self->dens = PyFloat_FromDouble(50);
5697 self->pitch = PyFloat_FromDouble(1);
5698 self->pos = PyFloat_FromDouble(0.0);
5699 self->dur = PyFloat_FromDouble(0.1);
5700 self->dev = PyFloat_FromDouble(0);
5701 self->pan = PyFloat_FromDouble(0.5);
5702 self->filterq = PyFloat_FromDouble(0.7);
5703 self->filtertype = PyFloat_FromDouble(0);
5704 self->timer = self->devFactor = 1.0;
5705 self->srScale = 1.0;
5706 self->num = 0;
5707 self->chnls = 1;
5708 self->modebuffer[0] = 0;
5709 self->modebuffer[1] = 0;
5710 self->modebuffer[2] = 0;
5711 self->modebuffer[3] = 0;
5712 self->modebuffer[4] = 0;
5713 self->modebuffer[5] = 0;
5714 self->modebuffer[6] = 0;
5715 self->modebuffer[7] = 0;
5716 self->modebuffer[8] = 0;
5717
5718 INIT_OBJECT_COMMON
5719
5720 self->oneOnSr = 1.0 / self->sr;
5721 self->srOnRandMax = self->sr / (MYFLT)PYO_RAND_MAX;
5722 self->nyquist = (MYFLT)self->sr * 0.49;
5723 self->twopiOverSr = TWOPI / (MYFLT)self->sr;
5724
5725 self->filterfreq = PyFloat_FromDouble(self->nyquist);
5726
5727 Stream_setFunctionPtr(self->stream, MainParticle2_compute_next_data_frame);
5728 self->mode_func_ptr = MainParticle2_setProcMode;
5729
5730 static char *kwlist[] = {"table", "env", "dens", "pitch", "pos", "dur", "dev", "pan", "filterfreq", "filterq", "filtertype", "chnls", NULL};
5731
5732 if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO|OOOOOOOOOi", kwlist, &tabletmp, &envtmp, &denstmp, &pitchtmp, &postmp, &durtmp, &devtmp, &pantmp, &filterfreqtmp, &filterqtmp, &filtertypetmp, &self->chnls))
5733 Py_RETURN_NONE;
5734
5735 if ( PyObject_HasAttrString((PyObject *)tabletmp, "getTableStream") == 0 )
5736 {
5737 PyErr_SetString(PyExc_TypeError, "\"table\" argument of MainParticle2 must be a PyoTableObject.\n");
5738 Py_RETURN_NONE;
5739 }
5740
5741 self->table = PyObject_CallMethod((PyObject *)tabletmp, "getTableStream", "");
5742 self->srScale = TableStream_getSamplingRate((TableStream *)self->table) / self->sr;
5743
5744 if ( PyObject_HasAttrString((PyObject *)envtmp, "getTableStream") == 0 )
5745 {
5746 PyErr_SetString(PyExc_TypeError, "\"env\" argument of MainParticle2 must be a PyoTableObject.\n");
5747 Py_RETURN_NONE;
5748 }
5749
5750 self->env = PyObject_CallMethod((PyObject *)envtmp, "getTableStream", "");
5751
5752 if (denstmp)
5753 {
5754 PyObject_CallMethod((PyObject *)self, "setDens", "O", denstmp);
5755 }
5756
5757 if (pitchtmp)
5758 {
5759 PyObject_CallMethod((PyObject *)self, "setPitch", "O", pitchtmp);
5760 }
5761
5762 if (postmp)
5763 {
5764 PyObject_CallMethod((PyObject *)self, "setPos", "O", postmp);
5765 }
5766
5767 if (durtmp)
5768 {
5769 PyObject_CallMethod((PyObject *)self, "setDur", "O", durtmp);
5770 }
5771
5772 if (devtmp)
5773 {
5774 PyObject_CallMethod((PyObject *)self, "setDev", "O", devtmp);
5775 }
5776
5777 if (pantmp)
5778 {
5779 PyObject_CallMethod((PyObject *)self, "setPan", "O", pantmp);
5780 }
5781
5782 if (filterfreqtmp)
5783 {
5784 PyObject_CallMethod((PyObject *)self, "setFilterfreq", "O", filterfreqtmp);
5785 }
5786
5787 if (filterqtmp)
5788 {
5789 PyObject_CallMethod((PyObject *)self, "setFilterq", "O", filterqtmp);
5790 }
5791
5792 if (filtertypetmp)
5793 {
5794 PyObject_CallMethod((PyObject *)self, "setFiltertype", "O", filtertypetmp);
5795 }
5796
5797 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
5798
5799 if (self->chnls < 1)
5800 self->chnls = 1;
5801
5802 self->gpos = (MYFLT *)PyMem_RawRealloc(self->gpos, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
5803 self->glen = (MYFLT *)PyMem_RawRealloc(self->glen, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
5804 self->inc = (MYFLT *)PyMem_RawRealloc(self->inc, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
5805 self->phase = (MYFLT *)PyMem_RawRealloc(self->phase, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
5806 self->amp1 = (MYFLT *)PyMem_RawRealloc(self->amp1, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
5807 self->amp2 = (MYFLT *)PyMem_RawRealloc(self->amp2, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
5808 self->flags = (int *)PyMem_RawRealloc(self->flags, MAINPARTICLE_MAX_GRAINS * sizeof(int));
5809 self->k1 = (int *)PyMem_RawRealloc(self->k1, MAINPARTICLE_MAX_GRAINS * sizeof(int));
5810 self->k2 = (int *)PyMem_RawRealloc(self->k2, MAINPARTICLE_MAX_GRAINS * sizeof(int));
5811 self->last_freq = (MYFLT *)PyMem_RawRealloc(self->last_freq, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
5812 self->last_q = (MYFLT *)PyMem_RawRealloc(self->last_q, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
5813 self->last_type = (MYFLT *)PyMem_RawRealloc(self->last_type, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
5814 self->x1 = (MYFLT *)PyMem_RawRealloc(self->x1, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
5815 self->x2 = (MYFLT *)PyMem_RawRealloc(self->x2, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
5816 self->y1 = (MYFLT *)PyMem_RawRealloc(self->y1, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
5817 self->y2 = (MYFLT *)PyMem_RawRealloc(self->y2, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
5818 self->c = (MYFLT *)PyMem_RawRealloc(self->c, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
5819 self->w0 = (MYFLT *)PyMem_RawRealloc(self->w0, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
5820 self->alpha = (MYFLT *)PyMem_RawRealloc(self->alpha, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
5821 self->qcomp = (MYFLT *)PyMem_RawRealloc(self->qcomp, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
5822 self->b0 = (MYFLT *)PyMem_RawRealloc(self->b0, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
5823 self->b1 = (MYFLT *)PyMem_RawRealloc(self->b1, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
5824 self->b2 = (MYFLT *)PyMem_RawRealloc(self->b2, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
5825 self->a0 = (MYFLT *)PyMem_RawRealloc(self->a0, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
5826 self->a1 = (MYFLT *)PyMem_RawRealloc(self->a1, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
5827 self->a2 = (MYFLT *)PyMem_RawRealloc(self->a2, MAINPARTICLE_MAX_GRAINS * sizeof(MYFLT));
5828
5829 for (i = 0; i < MAINPARTICLE_MAX_GRAINS; i++)
5830 {
5831 self->gpos[i] = self->glen[i] = self->inc[i] = self->phase[i] = self->amp1[i] = self->amp2[i] = 0.0;
5832 self->last_freq[i] = self->last_q[i] = self->x1[i] = self->x2[i] = self->y1[i] = self->y2[i] = 0.0;
5833 self->last_type[i] = -1.0;
5834 self->qcomp[i] = 1.0;
5835 self->c[i] = self->w0[i] = self->alpha[i] = self->b0[i] = self->b1[i] = self->b2[i] = self->a0[i] = self->a1[i] = self->a2[i] = 0.0;
5836 self->flags[i] = self->k1[i] = self->k2[i] = 0;
5837 }
5838
5839 self->buffer_streams = (MYFLT *)PyMem_RawRealloc(self->buffer_streams, self->bufsize * self->chnls * sizeof(MYFLT));
5840
5841 for (i = 0; i < self->bufsize * self->chnls; i++)
5842 {
5843 self->buffer_streams[i] = 0.0;
5844 }
5845
5846 Server_generateSeed((Server *)self->server, MAINPARTICLE_ID);
5847
5848 (*self->mode_func_ptr)(self);
5849
5850 return (PyObject *)self;
5851 }
5852
5853 static PyObject * MainParticle2_getServer(MainParticle2* self) { GET_SERVER };
5854 static PyObject * MainParticle2_getStream(MainParticle2* self) { GET_STREAM };
5855
5856 static PyObject * MainParticle2_play(MainParticle2 *self, PyObject *args, PyObject *kwds) { PLAY };
5857 static PyObject * MainParticle2_stop(MainParticle2 *self, PyObject *args, PyObject *kwds) { STOP };
5858
5859 static PyObject * MainParticle2_setDens(MainParticle2 *self, PyObject *arg) { SET_PARAM(self->dens, self->dens_stream, 0); }
5860 static PyObject * MainParticle2_setPitch(MainParticle2 *self, PyObject *arg) { SET_PARAM(self->pitch, self->pitch_stream, 1); }
5861 static PyObject * MainParticle2_setPos(MainParticle2 *self, PyObject *arg) { SET_PARAM(self->pos, self->pos_stream, 2); }
5862 static PyObject * MainParticle2_setDur(MainParticle2 *self, PyObject *arg) { SET_PARAM(self->dur, self->dur_stream, 3); }
5863 static PyObject * MainParticle2_setDev(MainParticle2 *self, PyObject *arg) { SET_PARAM(self->dev, self->dev_stream, 4); }
5864 static PyObject * MainParticle2_setPan(MainParticle2 *self, PyObject *arg) { SET_PARAM(self->pan, self->pan_stream, 5); }
5865 static PyObject * MainParticle2_setFilterfreq(MainParticle2 *self, PyObject *arg) { SET_PARAM(self->filterfreq, self->filterfreq_stream, 6); }
5866 static PyObject * MainParticle2_setFilterq(MainParticle2 *self, PyObject *arg) { SET_PARAM(self->filterq, self->filterq_stream, 7); }
5867 static PyObject * MainParticle2_setFiltertype(MainParticle2 *self, PyObject *arg) { SET_PARAM(self->filtertype, self->filtertype_stream, 8); }
5868
5869 static PyObject *
5870 MainParticle2_getTable(MainParticle2* self)
5871 {
5872 Py_INCREF(self->table);
5873 return self->table;
5874 };
5875
5876 static PyObject *
5877 MainParticle2_setTable(MainParticle2 *self, PyObject *arg)
5878 {
5879 ASSERT_ARG_NOT_NULL
5880
5881 Py_DECREF(self->table);
5882 self->table = PyObject_CallMethod((PyObject *)arg, "getTableStream", "");
5883 self->srScale = TableStream_getSamplingRate((TableStream *)self->table) / self->sr;
5884
5885 Py_RETURN_NONE;
5886 }
5887
5888 static PyObject *
5889 MainParticle2_getEnv(MainParticle2* self)
5890 {
5891 Py_INCREF(self->env);
5892 return self->env;
5893 };
5894
5895 static PyObject *
5896 MainParticle2_setEnv(MainParticle2 *self, PyObject *arg)
5897 {
5898 ASSERT_ARG_NOT_NULL
5899
5900 Py_DECREF(self->env);
5901 self->env = PyObject_CallMethod((PyObject *)arg, "getTableStream", "");
5902
5903 Py_RETURN_NONE;
5904 }
5905
5906 static PyMemberDef MainParticle2_members[] =
5907 {
5908 {"server", T_OBJECT_EX, offsetof(MainParticle2, server), 0, "Pyo server."},
5909 {"stream", T_OBJECT_EX, offsetof(MainParticle2, stream), 0, "Stream object."},
5910 {"table", T_OBJECT_EX, offsetof(MainParticle2, table), 0, "Sound table."},
5911 {"env", T_OBJECT_EX, offsetof(MainParticle2, env), 0, "Envelope table."},
5912 {"dens", T_OBJECT_EX, offsetof(MainParticle2, dens), 0, "Density of grains per second."},
5913 {"pitch", T_OBJECT_EX, offsetof(MainParticle2, pitch), 0, "Speed of the reading pointer."},
5914 {"pos", T_OBJECT_EX, offsetof(MainParticle2, pos), 0, "Position in the sound table."},
5915 {"dur", T_OBJECT_EX, offsetof(MainParticle2, dur), 0, "Duration of each grains."},
5916 {"dev", T_OBJECT_EX, offsetof(MainParticle2, dev), 0, "Grain start point deviation factor."},
5917 {"pan", T_OBJECT_EX, offsetof(MainParticle2, pan), 0, "Grain panning factor."},
5918 {"filterfreq", T_OBJECT_EX, offsetof(MainParticle2, filterfreq), 0, "Grain filter center/cutoff frequency."},
5919 {"filterq", T_OBJECT_EX, offsetof(MainParticle2, filterq), 0, "Grain filter Q."},
5920 {"filtertype", T_OBJECT_EX, offsetof(MainParticle2, filtertype), 0, "Grain filter type."},
5921 {NULL} /* Sentinel */
5922 };
5923
5924 static PyMethodDef MainParticle2_methods[] =
5925 {
5926 {"getTable", (PyCFunction)MainParticle2_getTable, METH_NOARGS, "Returns sound table object."},
5927 {"setTable", (PyCFunction)MainParticle2_setTable, METH_O, "Sets sound table."},
5928 {"getEnv", (PyCFunction)MainParticle2_getEnv, METH_NOARGS, "Returns envelope table object."},
5929 {"setEnv", (PyCFunction)MainParticle2_setEnv, METH_O, "Sets envelope table."},
5930 {"getServer", (PyCFunction)MainParticle2_getServer, METH_NOARGS, "Returns server object."},
5931 {"_getStream", (PyCFunction)MainParticle2_getStream, METH_NOARGS, "Returns stream object."},
5932 {"play", (PyCFunction)MainParticle2_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
5933 {"stop", (PyCFunction)MainParticle2_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
5934 {"setDens", (PyCFunction)MainParticle2_setDens, METH_O, "Sets the density of grains per second."},
5935 {"setPitch", (PyCFunction)MainParticle2_setPitch, METH_O, "Sets global pitch factor."},
5936 {"setPos", (PyCFunction)MainParticle2_setPos, METH_O, "Sets position in the sound table."},
5937 {"setDur", (PyCFunction)MainParticle2_setDur, METH_O, "Sets the grain duration."},
5938 {"setDev", (PyCFunction)MainParticle2_setDev, METH_O, "Sets grain start point deviation factor."},
5939 {"setPan", (PyCFunction)MainParticle2_setPan, METH_O, "Sets grain panning factor."},
5940 {"setFilterfreq", (PyCFunction)MainParticle2_setFilterfreq, METH_O, "Sets grain filter center/cutoff frequency."},
5941 {"setFilterq", (PyCFunction)MainParticle2_setFilterq, METH_O, "Sets grain filter Q."},
5942 {"setFiltertype", (PyCFunction)MainParticle2_setFiltertype, METH_O, "Sets grain filter type."},
5943 {NULL} /* Sentinel */
5944 };
5945
5946 PyTypeObject MainParticle2Type =
5947 {
5948 PyVarObject_HEAD_INIT(NULL, 0)
5949 "_pyo.MainParticle2_base", /*tp_name*/
5950 sizeof(MainParticle2), /*tp_basicpitch*/
5951 0, /*tp_itempitch*/
5952 (destructor)MainParticle2_dealloc, /*tp_dealloc*/
5953 0, /*tp_print*/
5954 0, /*tp_getattr*/
5955 0, /*tp_setattr*/
5956 0, /*tp_as_async (tp_compare in Python 2)*/
5957 0, /*tp_repr*/
5958 0, /*tp_as_number*/
5959 0, /*tp_as_sequence*/
5960 0, /*tp_as_mapping*/
5961 0, /*tp_hash */
5962 0, /*tp_call*/
5963 0, /*tp_str*/
5964 0, /*tp_getattro*/
5965 0, /*tp_setattro*/
5966 0, /*tp_as_buffer*/
5967 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
5968 "MainParticle2 objects. Accumulation of multiples bandpass filtered grains of sound.", /* tp_doc */
5969 (traverseproc)MainParticle2_traverse, /* tp_traverse */
5970 (inquiry)MainParticle2_clear, /* tp_clear */
5971 0, /* tp_richcompare */
5972 0, /* tp_weaklistoffset */
5973 0, /* tp_iter */
5974 0, /* tp_iternext */
5975 MainParticle2_methods, /* tp_methods */
5976 MainParticle2_members, /* tp_members */
5977 0, /* tp_getset */
5978 0, /* tp_base */
5979 0, /* tp_dict */
5980 0, /* tp_descr_get */
5981 0, /* tp_descr_set */
5982 0, /* tp_dictoffset */
5983 0, /* tp_init */
5984 0, /* tp_alloc */
5985 MainParticle2_new, /* tp_new */
5986 };
5987
5988 typedef struct
5989 {
5990 pyo_audio_HEAD
5991 MainParticle2 *mainSplitter;
5992 int modebuffer[2];
5993 int chnl; // panning order
5994 } Particle2;
5995
5996 static void Particle2_postprocessing_ii(Particle2 *self) { POST_PROCESSING_II };
5997 static void Particle2_postprocessing_ai(Particle2 *self) { POST_PROCESSING_AI };
5998 static void Particle2_postprocessing_ia(Particle2 *self) { POST_PROCESSING_IA };
5999 static void Particle2_postprocessing_aa(Particle2 *self) { POST_PROCESSING_AA };
6000 static void Particle2_postprocessing_ireva(Particle2 *self) { POST_PROCESSING_IREVA };
6001 static void Particle2_postprocessing_areva(Particle2 *self) { POST_PROCESSING_AREVA };
6002 static void Particle2_postprocessing_revai(Particle2 *self) { POST_PROCESSING_REVAI };
6003 static void Particle2_postprocessing_revaa(Particle2 *self) { POST_PROCESSING_REVAA };
6004 static void Particle2_postprocessing_revareva(Particle2 *self) { POST_PROCESSING_REVAREVA };
6005
6006 static void
6007 Particle2_setProcMode(Particle2 *self)
6008 {
6009 int muladdmode;
6010 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
6011
6012 switch (muladdmode)
6013 {
6014 case 0:
6015 self->muladd_func_ptr = Particle2_postprocessing_ii;
6016 break;
6017
6018 case 1:
6019 self->muladd_func_ptr = Particle2_postprocessing_ai;
6020 break;
6021
6022 case 2:
6023 self->muladd_func_ptr = Particle2_postprocessing_revai;
6024 break;
6025
6026 case 10:
6027 self->muladd_func_ptr = Particle2_postprocessing_ia;
6028 break;
6029
6030 case 11:
6031 self->muladd_func_ptr = Particle2_postprocessing_aa;
6032 break;
6033
6034 case 12:
6035 self->muladd_func_ptr = Particle2_postprocessing_revaa;
6036 break;
6037
6038 case 20:
6039 self->muladd_func_ptr = Particle2_postprocessing_ireva;
6040 break;
6041
6042 case 21:
6043 self->muladd_func_ptr = Particle2_postprocessing_areva;
6044 break;
6045
6046 case 22:
6047 self->muladd_func_ptr = Particle2_postprocessing_revareva;
6048 break;
6049 }
6050 }
6051
6052 static void
6053 Particle2_compute_next_data_frame(Particle2 *self)
6054 {
6055 int i;
6056 MYFLT *tmp;
6057 int offset = self->chnl * self->bufsize;
6058 tmp = MainParticle2_getSamplesBuffer((MainParticle2 *)self->mainSplitter);
6059
6060 for (i = 0; i < self->bufsize; i++)
6061 {
6062 self->data[i] = tmp[i + offset];
6063 }
6064
6065 (*self->muladd_func_ptr)(self);
6066 }
6067
6068 static int
6069 Particle2_traverse(Particle2 *self, visitproc visit, void *arg)
6070 {
6071 pyo_VISIT
6072 Py_VISIT(self->mainSplitter);
6073 return 0;
6074 }
6075
6076 static int
6077 Particle2_clear(Particle2 *self)
6078 {
6079 pyo_CLEAR
6080 Py_CLEAR(self->mainSplitter);
6081 return 0;
6082 }
6083
6084 static void
6085 Particle2_dealloc(Particle2* self)
6086 {
6087 pyo_DEALLOC
6088 Particle2_clear(self);
6089 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
6090 Py_TYPE(self)->tp_free((PyObject*)self);
6091 }
6092
6093 static PyObject *
6094 Particle2_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
6095 {
6096 int i;
6097 PyObject *maintmp = NULL, *multmp = NULL, *addtmp = NULL;
6098 Particle2 *self;
6099 self = (Particle2 *)type->tp_alloc(type, 0);
6100
6101 self->modebuffer[0] = 0;
6102 self->modebuffer[1] = 0;
6103
6104 INIT_OBJECT_COMMON
6105 Stream_setFunctionPtr(self->stream, Particle2_compute_next_data_frame);
6106 self->mode_func_ptr = Particle2_setProcMode;
6107
6108 static char *kwlist[] = {"mainSplitter", "chnl", "mul", "add", NULL};
6109
6110 if (! PyArg_ParseTupleAndKeywords(args, kwds, "Oi|OO", kwlist, &maintmp, &self->chnl, &multmp, &addtmp))
6111 Py_RETURN_NONE;
6112
6113 self->mainSplitter = (MainParticle2 *)maintmp;
6114 Py_INCREF(self->mainSplitter);
6115
6116 if (multmp)
6117 {
6118 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
6119 }
6120
6121 if (addtmp)
6122 {
6123 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
6124 }
6125
6126 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
6127
6128 (*self->mode_func_ptr)(self);
6129
6130 return (PyObject *)self;
6131 }
6132
6133 static PyObject * Particle2_getServer(Particle2* self) { GET_SERVER };
6134 static PyObject * Particle2_getStream(Particle2* self) { GET_STREAM };
6135 static PyObject * Particle2_setMul(Particle2 *self, PyObject *arg) { SET_MUL };
6136 static PyObject * Particle2_setAdd(Particle2 *self, PyObject *arg) { SET_ADD };
6137 static PyObject * Particle2_setSub(Particle2 *self, PyObject *arg) { SET_SUB };
6138 static PyObject * Particle2_setDiv(Particle2 *self, PyObject *arg) { SET_DIV };
6139
6140 static PyObject * Particle2_play(Particle2 *self, PyObject *args, PyObject *kwds) { PLAY };
6141 static PyObject * Particle2_out(Particle2 *self, PyObject *args, PyObject *kwds) { OUT };
6142 static PyObject * Particle2_stop(Particle2 *self, PyObject *args, PyObject *kwds) { STOP };
6143
6144 static PyObject * Particle2_multiply(Particle2 *self, PyObject *arg) { MULTIPLY };
6145 static PyObject * Particle2_inplace_multiply(Particle2 *self, PyObject *arg) { INPLACE_MULTIPLY };
6146 static PyObject * Particle2_add(Particle2 *self, PyObject *arg) { ADD };
6147 static PyObject * Particle2_inplace_add(Particle2 *self, PyObject *arg) { INPLACE_ADD };
6148 static PyObject * Particle2_sub(Particle2 *self, PyObject *arg) { SUB };
6149 static PyObject * Particle2_inplace_sub(Particle2 *self, PyObject *arg) { INPLACE_SUB };
6150 static PyObject * Particle2_div(Particle2 *self, PyObject *arg) { DIV };
6151 static PyObject * Particle2_inplace_div(Particle2 *self, PyObject *arg) { INPLACE_DIV };
6152
6153 static PyMemberDef Particle2_members[] =
6154 {
6155 {"server", T_OBJECT_EX, offsetof(Particle2, server), 0, "Pyo server."},
6156 {"stream", T_OBJECT_EX, offsetof(Particle2, stream), 0, "Stream object."},
6157 {"mul", T_OBJECT_EX, offsetof(Particle2, mul), 0, "Mul factor."},
6158 {"add", T_OBJECT_EX, offsetof(Particle2, add), 0, "Add factor."},
6159 {NULL} /* Sentinel */
6160 };
6161
6162 static PyMethodDef Particle2_methods[] =
6163 {
6164 {"getServer", (PyCFunction)Particle2_getServer, METH_NOARGS, "Returns server object."},
6165 {"_getStream", (PyCFunction)Particle2_getStream, METH_NOARGS, "Returns stream object."},
6166 {"play", (PyCFunction)Particle2_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
6167 {"out", (PyCFunction)Particle2_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
6168 {"stop", (PyCFunction)Particle2_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
6169 {"setMul", (PyCFunction)Particle2_setMul, METH_O, "Sets Particle2 mul factor."},
6170 {"setAdd", (PyCFunction)Particle2_setAdd, METH_O, "Sets Particle2 add factor."},
6171 {"setSub", (PyCFunction)Particle2_setSub, METH_O, "Sets inverse add factor."},
6172 {"setDiv", (PyCFunction)Particle2_setDiv, METH_O, "Sets inverse mul factor."},
6173 {NULL} /* Sentinel */
6174 };
6175
6176 static PyNumberMethods Particle2_as_number =
6177 {
6178 (binaryfunc)Particle2_add, /*nb_add*/
6179 (binaryfunc)Particle2_sub, /*nb_subtract*/
6180 (binaryfunc)Particle2_multiply, /*nb_multiply*/
6181 0, /*nb_remainder*/
6182 0, /*nb_divmod*/
6183 0, /*nb_power*/
6184 0, /*nb_neg*/
6185 0, /*nb_pos*/
6186 0, /*(unaryfunc)array_abs,*/
6187 0, /*nb_nonzero*/
6188 0, /*nb_invert*/
6189 0, /*nb_lshift*/
6190 0, /*nb_rshift*/
6191 0, /*nb_and*/
6192 0, /*nb_xor*/
6193 0, /*nb_or*/
6194 0, /*nb_int*/
6195 0, /*nb_long*/
6196 0, /*nb_float*/
6197 (binaryfunc)Particle2_inplace_add, /*inplace_add*/
6198 (binaryfunc)Particle2_inplace_sub, /*inplace_subtract*/
6199 (binaryfunc)Particle2_inplace_multiply, /*inplace_multiply*/
6200 0, /*inplace_remainder*/
6201 0, /*inplace_power*/
6202 0, /*inplace_lshift*/
6203 0, /*inplace_rshift*/
6204 0, /*inplace_and*/
6205 0, /*inplace_xor*/
6206 0, /*inplace_or*/
6207 0, /*nb_floor_divide*/
6208 (binaryfunc)Particle2_div, /*nb_true_divide*/
6209 0, /*nb_inplace_floor_divide*/
6210 (binaryfunc)Particle2_inplace_div, /*nb_inplace_true_divide*/
6211 0, /* nb_index */
6212 };
6213
6214 PyTypeObject Particle2Type =
6215 {
6216 PyVarObject_HEAD_INIT(NULL, 0)
6217 "_pyo.Particle2_base", /*tp_name*/
6218 sizeof(Particle2), /*tp_basicsize*/
6219 0, /*tp_itemsize*/
6220 (destructor)Particle2_dealloc, /*tp_dealloc*/
6221 0, /*tp_print*/
6222 0, /*tp_getattr*/
6223 0, /*tp_setattr*/
6224 0, /*tp_as_async (tp_compare in Python 2)*/
6225 0, /*tp_repr*/
6226 &Particle2_as_number, /*tp_as_number*/
6227 0, /*tp_as_sequence*/
6228 0, /*tp_as_mapping*/
6229 0, /*tp_hash */
6230 0, /*tp_call*/
6231 0, /*tp_str*/
6232 0, /*tp_getattro*/
6233 0, /*tp_setattro*/
6234 0, /*tp_as_buffer*/
6235 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
6236 "Particle2 objects. Reads one band from a MainParticle2 object.", /* tp_doc */
6237 (traverseproc)Particle2_traverse, /* tp_traverse */
6238 (inquiry)Particle2_clear, /* tp_clear */
6239 0, /* tp_richcompare */
6240 0, /* tp_weaklistoffset */
6241 0, /* tp_iter */
6242 0, /* tp_iternext */
6243 Particle2_methods, /* tp_methods */
6244 Particle2_members, /* tp_members */
6245 0, /* tp_getset */
6246 0, /* tp_base */
6247 0, /* tp_dict */
6248 0, /* tp_descr_get */
6249 0, /* tp_descr_set */
6250 0, /* tp_dictoffset */
6251 0, /* tp_init */
6252 0, /* tp_alloc */
6253 Particle2_new, /* tp_new */
6254 };