RobinSchmidt / RS-MET

Codebase for RS-MET products (Robin Schmidt's Music Engineering Tools)
Other
56 stars 6 forks source link

Snowflake - fun with fractals #194

Open RobinSchmidt opened 6 years ago

RobinSchmidt commented 6 years ago

i'm working on a new module that can create waveshapes that trace out a closed curve (i.e. loop) in 2D with a self-similar boundary. here's an example:

image

here are the x- and y-coordinates as function of time:

image

as can be seen, the waveforms are periodic, i.e. they end where they begin, indicating a nicely loopable repetitive waveform. someone posted on facebook a link to the gif from here:

https://www.reddit.com/r/visualizedmath/comments/7xtxgb/hilbert_curve/

which got me interested in what the fuck is going on there. a bit of research revealed that they are drawing a Moore curve there:

https://en.wikipedia.org/wiki/Moore_curve

and doing some more research was dragging me into a rabbit hole. the moore curve is one of the infinitely many curve types that a Lindenmayer-system:

https://en.wikipedia.org/wiki/L-system

will be able to generate. so i implemented one. among the possible curves is also the koch snowflake:

https://en.wikipedia.org/wiki/Koch_snowflake

which i elected for the name of this new upcoming generator.

RobinSchmidt commented 6 years ago

here are more examples:

http://mathforum.org/advanced/robertd/lsys2d.html

not all of the curves shown there loop back to their beginning though, but with some care in setting up the L-system, this can be ensured (it's a useful property when it's going to be used as audio oscillator waveform). here's a little web-app to play around with L-systems until my new module is ready:

http://www.kevs3d.co.uk/dev/lsystems/

RobinSchmidt commented 6 years ago

i have no idea, how it will sound like, but i'm pretty sure, it will make for great visuals on the scope :-)

elanhickler commented 6 years ago

The stereo 1D image version of the waveform looks like a supersaw. If you can do subtle modulation of the details of the waveform this could make for an interesting chorusy waveform.

RobinSchmidt commented 6 years ago

i currently can't see any straightforward way to modulate the synthesis (i.e. rendering) parameters as these parameters are either strings or an integer (the number of iterations) and also, re-rendering of the (wave) tables is computationally expensive as hell. the only continuous parameter is the angle, but it doesn't seem to make much sense to modulate it - it must be set to a very specific value in accordance to chosen L-system rules in order to get a closed loop.

but maybe some real-time post-processing can be slapped on the waveform. maybe an allpass with different frequency for left and right channel. i think, that would shift the overtones a bit back and forth

RobinSchmidt commented 6 years ago

...but perhaps it could make sense to do a kind of crossfading between various similar pre-rendered wavetables (for example, those obtained from the same L-system with different number of iterations) as in the waldorf sense of wavetable synthesis

RobinSchmidt commented 6 years ago

the only continuous parameter is the angle, but it doesn't seem to make much sense to modulate it

thinking about it some more, i think modulating the turning angle in realtime (if that would be computationally possible in any meaningful way) would give results similar to just modulating a post-processed 2D rotation of the resulting picture. maybe not quite the same, but similar

RobinSchmidt commented 6 years ago

perhaps it could make sense to do a kind of crossfading between various similar pre-rendered wavetables

..when i integrate this generator into my upcoming dream-synth with its 4 sources that are crossfadable via a vector pad, i could set all 4 sources to similar L-system shapes and modulate the x/y coordinates of the vector mixer pad

elanhickler commented 6 years ago

Make it modulatable, I can't imagine it is more expensive than my synths Radar, and Spiral Generator.

RobinSchmidt commented 6 years ago

hmmm...i really need to think about it. maybe it's possible to make the turning angle of the L-system modulatable. ...because it's actually not a parameter of the L-system itself but of the "turtle graphics" interpreter (which interprets the output string of the L-system as drawing commands, i.e. "go forward", "turn left", "turn right"). i think, the turtle can be modified to produce one point at a time (instead of rendering the whole table of points at once). i have an idea how that may be possible....but first things first

RobinSchmidt commented 6 years ago

first audio signals produced. image maybe in a few hours or tomorrow, it's ready to actually play with it and explore the range of possibilities (some important basics like state-recall are still missing). first impression of the sound: it has a sort of organ-like quality to it with more iterations having the effect of adding in more upper registers or something - which is actually not really surprising at all, when thinking about it.

edit: so, as one option for modulation, it may really make a lot of sense to modulate a crossfade between various iterations of the system, i.e. various levels of detail. and the lower-detail versions are not necessarily much duller since the shapes of the details and basic shapes are non-sinusoidal - they can even have edges, so it's not like crossfading with a lowpassed version

RobinSchmidt commented 6 years ago

i just found a link that i need to save: http://algorithmicbotany.org/papers/#abop i feel like having stumbled upon a whole new branch of mathematics that has nothing to do with what i have seen before (apart from some introduction into formal languages in computer science lectures during study). ...but all the "normal" math stuff - geometry, linear algebra, calculus, complex numbers - even number theory - all that seems to be irrelevant here - but somehow it's still math. math is sooo diverse. amazing! but maybe it's not mathematics but algorithmics? but that latter term seems to be not be widely used

RobinSchmidt commented 6 years ago

even the fractals that i have encountered so far were based on a totally different mechanism. in the most general case, you had a vector valued function y = f(x) where x and y are vectors in the same space and f is some nonlinear function that maps an input vector onto an output vector...and then you apply that function again and again and again to some initial vector...that makes a nonlinear recursive system. and for some systems, the iteration diverges for some initial vectors and doesn't diverge for others. the boundary between those classes of starting points has a fractal shape. the well known mandelbrot set:

https://en.wikipedia.org/wiki/Mandelbrot_set

is a famous example of that (the complex-number formulation of the function f(z) can straightforwardly be translated into a function mapping 2D vectors onto 2D vectors). but all of this is rooted in "normal" math ...this L-system stuff here is conceptually completely different

RobinSchmidt commented 6 years ago

the complex-number formulation of the function f(z) can straightforwardly be translated into a function mapping 2D vectors onto 2D vectors.

i think:

z_new = f(z) = z^2 + c

translates to:

x_new = f(x,y) = x^2 - y^2 + cx  // cx == Re(c)
y_new = f(x,y) = 2 * x * y + cy  // cy == Im(c)

when you want to do it without complex numbers. iterate that and observe, if the vector explodes or not. paint starting-points for which it doesn't explode black and points where is does explode white and you get a basic (black/white) picture of the mandelbrot set. artistic renderings often take into account, how fast it diverges and choose a color according to that value.

edit: wait: no! the mandelbrot set is the set of (complex) values of c for which the iteration (started at z=0) doesn't diverge. i always confuse that

RobinSchmidt commented 6 years ago

maybe it's not mathematics but algorithmics?

...but why does it feel so totally different? i think, maybe it's because one of the most fundamental concepts of math - the equation - does not really play a big role there? ...and also because it's not numeric (it operates on strings)

RobinSchmidt commented 6 years ago

by the way - i just noticed that my text editor seems to behave buggy as hell. does it work for you as it should in the comment section of chaosfly?

RobinSchmidt commented 6 years ago

ooookaaaay....the fun can begin! there are still a couple of quirks and bugs but at least it's now possible to explore L-systems. i created a new repository with presets and i already created some quite interesting shapes here:

https://github.com/RobinSchmidt/Presets/tree/master/Snowflake

very interesting is to load MooreCurve4, play a very low note and put an EngineersFilter after that, set it to bessel lowpass (of somewhat high order) and tweak the cutoff frequency. if that reminds you of the gif of the rolling circles (https://i.redd.it/9pa7fosfjjgz.gif) that initially sparked my interest in all this stuff, it should. it's a sort of replication of what's going on there (just with a bessel filter instead of a perfect linear-phase brickwall filter which would correspond to the gif). very satisfying. ok...tomorrow maybe a bit more exploration and playtime and then back to work on the multicomp gui. but i just had to do this

RobinSchmidt commented 6 years ago

btw. - can you recommend me a software for doing screen capture videos? i may want to make some in the future as well....

RobinSchmidt commented 6 years ago

endless variation of interesting shapes, not necessarily self-similar image

RobinSchmidt commented 6 years ago

damn! you are right! i have to make the turning angle modulatable. this is not like just rotating the whole picture. it's actually much more interesting. if you want to try it, i recommend to load the patch PentaT (it's the patch that created the pic above) and move the "TurningAngle" slider by hand. of course, there are lots of artifacts now because changing the angle triggers a re-rendering. but if i implement it in a way that expects the angle to be modulated, i think, that could make for a great effect - both sonically and visually.

elanhickler commented 6 years ago

by the way - i just noticed that my text editor seems to behave buggy as hell. does it work for you as it should in the comment section of chaosfly?

It has been buggy from the beginning.

btw. - can you recommend me a software for doing screen capture videos? i may want to make some in the future as well....

if you have a good or modern video card: https://obsproject.com/ (you have to set video card capturing option if you have a modern video card, nvidia GTX or radeon RX to take advantage of video card based capture).

if you have an old or crappy video card: https://getsharex.com/

elanhickler commented 6 years ago

DO NOT USE THE AUDIO CAPTURE FROM EITHER PROGRAM! The audio sucks... I haven't figured out how to record good audio from them. Use another software for uncompressed audio capture.

edit: oh but then you need a program to align the audio and video. If you don't have that, then best to capture audio with the video software. Also, always capture audio with the video software so it is easier to align audio/video if you record audio separately as well.

RobinSchmidt commented 6 years ago

i have many ideas, how to extend the turtle-graphics and lindenmayer-system syntax in meaningful ways to allow for even more variation of the geometry. i think, a whole new synthesis paradigm can be created from this...i'm very excited about this stuff (as you have undoubtetly noticed). i think, i'll call it Fractal Geometry (FG) synthesis. ..it needs a catchy name

ah: and thank you for the links!

RobinSchmidt commented 6 years ago

an evil alien from independence day :-O

image

... and this patch also sounds good (AlienFace.xml). edit: i mean, "patch" is a bit exaggerated. these presets are "raw" oscillator waveforms, i.e. starting points

elanhickler commented 6 years ago

pooooost auudioooooooooooo

looks great.

can i sell any of this as a soundemote product? what do you want to do? do you want to work together at all?

I can make you big bucks. My company will grow.

RobinSchmidt commented 6 years ago

i do not yet have the means to produce video with audio - but will hopefully do soon. you can try it yourself. just build ToolChain, plug in Snowflake into the first slot (and trigger a (preferably low) note) and skip through the presets (just download the preset repo into standard preset folder C:\Users[Username]\AppData\Roaming\RS-MET\Presets). maybe plug a BreakpointModulator into another slot and route it multiplicatively to the amplitude for basic amp-enveloping (but it behaves sometimes a bit buggy).

...i want to fix a couple of bugs and then also get back to the multicomp gui. ...but it's really a rabbithole. i'm just scratching the surface. using filters on the output is also sometimes very useful - especially for the moore curve. if you want to preserve the shape best, use bessel (lowpass) filters - check out MooreRotor.xml - and slap a bessel lowpass on it and open it slowly. instant scifi drones :-D

RobinSchmidt commented 6 years ago

...and i really need to write up a tutorial at some point to explain what (the fuck) is going on there, so a sound designer can actually know what (the fuck) he's doing. it's very unlike common synthesis methods

RobinSchmidt commented 6 years ago

can i sell any of this as a soundemote product? what do you want to do? do you want to work together at all?

i want to use it as one of the generators in my upcoming dream synth - but yeah, sure, if you want to also make a synth from it for your oscilloscope-music synth series, we can certainly work together on that. maybe you can commission the further development of the algorithms and/or we do some revenue sharing or something. we have to figure something out that is practical and fair for both of us

I can make you big bucks. My company will grow.

that would be great!. ...could be advertised as novel synthesis method. ...fractal pattern synthesis

elanhickler commented 6 years ago

ok, it is good to know you are open to the idea. Nothing has to happen right away, I will let my mind work on this.

RobinSchmidt commented 6 years ago

i just fixed a bug - there was a sort of buzzing artifact when CyclicReset was 0. it didn't sound necessarily bad, but it was wrong anyway. this is fixed now. the MooreRotor had it before - it's gone now

elanhickler commented 6 years ago

Ok this is pretty cool. I tried it. You need phase and frequency modulation! You also need LFOs and my feedback system.

You need to...... just copy what I'm doing with Jerobeam stuff 😄

Or I can take your core module and put it inside an OMS product and give you the same (or similar royalties) deal I gave Jerobeam.

elanhickler commented 6 years ago

I will have suggestions before I make it a Soundemote product.

  1. Add a control for "the length of the pattern" from 0 to 100%, but the frequency should remain the same. Essentially, moving this control will give you a "slowly being drawn" effect on an oscilloscope.

  2. Can you think of a way, without a filter, to add/remove harmonic content? Like some modulation of some part of the algorithm that happens to make the shape less spikey, more rounded. If not, then make a dedicated non-resonant filter for snowflake.

RobinSchmidt commented 6 years ago

Or I can take your core module and put it inside an OMS product and give you the same (or similar royalties) deal I gave Jerobeam.

yes - that sounds good. i think, you have the surrounding environment already in place and would just have to plug in the core sound generator at the center. what is your arrangement with jerobeam?

Add a control for "the length of the pattern" from 0 to 100%, but the frequency should remain the same. Essentially, moving this control will give you a "slowly being drawn" effect on an oscilloscope.

how would that work? are you talking about traversing the pattern only partially and then reset? "slowly being drawn" is something i would just associate with playing a low frequency but i guess i'm misunderstanding you here

Can you think of a way, without a filter, to add/remove harmonic content? Like some modulation of some part of the algorithm that happens to make the shape less spikey, more rounded. If not, then make a dedicated non-resonant filter for snowflake.

yes, just a few hours ago i was thinking about that. for many patterns, the number of iterations adds harmonic content (well, for some you get totally different patterns, but that may also be due some mistakes in defining it via this turtle/lindenmayer syntax - i'm just learning all that myself - but for the AlienFace patch, increasing the iterations indeed adds harmonic content in a useful way). ...sooo a continuous number-of-iterations would be nice. when it's set to 3.5, you'd get a 50/50 crossfade between 3 and 4 iterations. well, this is my idea. but a non-resonant bessel filter can be plugged after it anyway. also, regular resonant filters (like my ladder) are also nice. you loose some of the symmetries that bessel filters would preserve but it often looks good anyway (and sometimes because of it). but yes, in the back of my head i was already thinking about making a modulatble bessel filter. (the current implementation is expensive to modulate and doesn't really use a modulatable topology - you can modulate it anyway, but i think, i should make a dedicated version with good modulatability properties desgned in)

RobinSchmidt commented 6 years ago

some modulation of some part of the algorithm that happens to make the shape less spikey, more rounded.

i also want to include cubic interpolation later. it currently works like that: the drawing algorithm (the "turtle") generates a sequence of x,y points, between which i linearly interpolate to generate the time signal. using cubic interpolation instead would also round edges. ...and one could also crossfade beween linear and cubic interpolation. and/or left/right/nearest neighbour interpolation. there, the signal would jump from spot to spot, so on the scope only the corner points would be visible and in the time-signal, we would have discontinuous jumps - with all their harmonic creating properties

elanhickler commented 6 years ago

that all sounds good.

my "appears like it's slowly being drawn" idea is... I guess it would be like a partial reset. However, resetting causes ugly discontinuity (only useful sometimes). So what you do is traverse the pattern forward, then traverse in reverse. (This is achieved by doing phase modulation with a tri-saw shape oscillator)

Imagine drawing a half circle. Instead of jumping to the other side to draw the half circle once again, you reverse directions, you re-trace/trace over in order to not jump and cause discontinuity.

...i better create a demonstration of this with my OMS.

elanhickler commented 6 years ago

my arrangement with Jerobeam is 30% royalties.

Edit: So I can give you 30% royalties unless you are responsible for more of the synth (if you basically create the final product mostly or completely on your own then I would give you more). But consider that I will be doing marketing, GUI design, including all my personal DSP, etc.

RobinSchmidt commented 6 years ago

this DragonCurve.xml here looks and sounds quite good: image also, the number of iterations increases harmonic content in a useful way. https://en.wikipedia.org/wiki/Dragon_curve (i'm using a closed loop version of it)

elanhickler commented 6 years ago

very nice. You frequency is too high for that image to be drawn accurately though. :)

RobinSchmidt commented 6 years ago

indeed! here's a better rendering using a very low frequency and different colormap: image haha! this is really fun! btw. the number of iterations is already maxed out at 10 here in this pic. it could actually use one or two more levels of detail

elanhickler commented 6 years ago

Are you doing or can you do anything to lessen harmonics as frequency increases (as an antialiasing technique)? It's always good to reduce harmonics before going to oversampling / antialiasing filter.

RobinSchmidt commented 6 years ago

there isn't any kind of anti-aliasing at work yet - and that thing does alias as hell. i just had some idea what i could do that wouldn't require any (explicit) oversampling. when playing a high frequency, the turtle itself may move over various line segments within one sample instant. that is: the turtle output already is an oversampled signal with respect to our audio sample rate. i think, i should apply a lowpass to that while i let the turtle walk around. the (normalized) frequency of that lowpass should depend on our readout frequency, so as to keep the absolute frequency of the lowpass constant. that is: in this case, not the cutoff frequency but the sample-rate of the lowpass is modulated according to the signal frequency (which is in effect the same because the numbers (filter coefficients, etc.) depend only on the ratio of cutoff/sampleRate)

RobinSchmidt commented 6 years ago

OMG! FUCK YEAH!!! i just added a feature for resetting the turtle after a given number of produced lines - and this is the KILLER feature! pull an update and check out the new patches AlienFace2 and 3 and BuzzingTriangles. sounds and looks already amazing. now tweak the TurningAngle parameter! AlienFace3 is my favourite!

btw - ignore that AntiAlias button - this doesn't work right yet

RobinSchmidt commented 6 years ago

i actually think, that the resetting strategy should be considered as another major aspect of the synthesis method. just resetting after completing a given number of cycles through the curve and (additionally, independently) resetting after some line counter has reached its maximum is very simple. i think, i should come up with more sophisticated resetting algorithms. i'm thinking of using something like my line-count based resetter, but not reset every time, the number of lines is reached, but let that depend on the value of some binary sequence (1 means: do reset, 0 means: don't reset) ...and then look into interesting ways to generate such sequences. one possibility would be to use L-systems once again and interpret '+' as '1' and '-' as '0'. but there are other ways as well. on monday, i received my copy of this wonderful book:

http://www.abrazol.com/books/patterngen/

...for 14 euros at amazon, that was a no brainer - and it's a gold-mine. containing lots of example pictures with construction information. the AlienFace patch is actually one of those. ...and - besides L-systems - it also talks about other methods to generate binary sequences. i may implement some of them as well in the future

RobinSchmidt commented 6 years ago

ah - the examples are online is well - it's actually this one:

http://www.abrazol.com/books/patterngen/image/trees60d04.svg

from here (almost at the bottom):

http://www.abrazol.com/books/patterngen/images_lsys.html

...but my rendering looks different...hmm...have i messed up something? i need to figure that out. if so, i think, it would count as happy accident

elanhickler commented 6 years ago

have an option to reverse direction instead of just reset. Although I could add that myself with phase modulation. You have a phase variable?

Edit: this is the reason you need a tri-saw shaper for phase modulation, so you can reset, or variably reverse direction at variable forward/backward speed. Even better would be a variably curved triangle, where backwards-forwards have individual curve settings.

Edit: and for the "length to draw" variable, you can't just rely on hard reset otherwise you have an ugly jump. So that's where reversing direction is super useful.

RobinSchmidt commented 6 years ago

yes - that's a good idea. i'll have to see how hard or easy that will turn out to implement. i do have a "phase" variable (it's pos in class TurtleSource)...but there's no easy and straightforward way to just "set" it in a random access way. to set it to some arbitrary value, i would actually have to traverse the curve from the current position to the desired target position - which may be expensive. ...sooo...i guess...phase-modulation is difficult (or i must think harder)...but frequency modulation seems to be no big deal, i think

elanhickler commented 6 years ago

thennnn forget about phase modulation!

then make a phase mod to frequency mod converter... haha... I was going to pay you for this when I can.

RobinSchmidt commented 6 years ago

...unless the optional table-mode is used (where i just pre-render the whole curve into a table and read from that) - in this case, randomly setting the position is trivial. but that mode is limited in other ways (no turn-angle modulation, for example :-( )

elanhickler commented 6 years ago

don't do table mode.

RobinSchmidt commented 6 years ago

that's why it's optional. it's actually a remnant of the first implementation - it was just simpler to do (it was surprisingly tricky to implement a realtime turtle - you have to keep track of the index in the command-string which not equal to the index of the line being drawn etc...). but i think i'll don't throw the table code away but keep it as option. it's also useful for debugging - writing unit tests and compare outputs from table mode and non-table mode. when they don't match, there's a bug somewhere

elanhickler commented 6 years ago

you can have negative frequency, right? wouldn't that reverse the direction? so that's part of the function of converting phase mod to freq mod.