Polyphonic Faust?

After my Physical Modeling Flute Faust experience I decided to try to model a virtual analog synthesizer in Faust. I looked at several synth structures and came up with this fairly standard wiring :

2 LFOS, 2 Oscillators, Noise generation, 2 Filters, 2 Amplifiers and 4 ADSR envelopes (integrated in Filters and Amplifiers).

Designing this in monophonic form was fairly easy, as no recursion in top level elements was needed like in many physical modeling designs. For oscillators, filters and envelopes standard components of the Faust libraries were used. Finishing a monophonic synth was easy.

Changing the monophonic synth into a polyphonic one was also quite straight forward. The single gate, gain and pitch controls for a single voice needed to be duplicated. I moved the original process declaration into a single voice with the following definition

voice(gate, gain, pitch) = (lfo1, lfo2) <: (_,_,osc1,osc2,noisegen)
  : (_,_,pre_filter_mix) // l1, l2, f1_in, f2_in
  // to filters
  <: ((_,_,!,_), ((_,!,_,!) : filter1)) // l1, l2, f2_in, filter1, filter1_to_f2
  <: ((_,!,!,_,!), (!,_,!,!,!), ((!,_,_,!,_) : (_,_+_) : filter2)) // l1,   f1_out, l2, f2_out
  // to amps
  : (amp1, amp2)
with {
    // ...
    // in : o11, o12, o21, o22, n1, n2
    // out : f1_in, f2_in
    pre_filter_mix = bus6 <: (((_,!,_,!,_,!) : _+_+_), ((!,_,!,_,!,_) : _+_+_));

The new process looks like this

process = par(i, 8, voice(i)) :> (_,_,_,_) : effects :> (main_out * _, main_out * _);

8 voices are run in parallel and are then mixed into a 4 channel signal which is fed to the effects section and then fed to Stereo out.

A problem with this design is that all the 8 voices are always on. And the resulting Faust structure is quite large and takes a long time to compile. With several compiler level optimizations this design can be made runnable.

Other performance issues are on a lower level with select2 and select3 constructs, which are Faust’s if-else equivalents. Non-active branches can only be skipped in Faust if they have no memory. Memory, or state, is involved if you use any kind of delays, either explicitly or via usage of recursion. The “always on” philosophy needs to be taken into account on every level, if low CPU usage is a goal.

I defined a single multi-waveform oscillator like this

oscillator(type, freq, width) = select5(type,
    squarewave(freq, width),

As all of these components use state internally to keep track of the current phase all of them are always run.

One way to fix this is to separate the phase which is stateful and needs to be memorized from the wave shape generation. This is another oscillator design which has stateless functions in the wave selection :

oscillator(type, freq, width) = phase(freq) : phase_to_osc(type, width);

phase(freq) = (+(q) : mod1) ~ _
with {
    q = float(freq)/float(SR);

phase_to_osc(type, width) = _ <: select5(type,

square(w) = select2(_ < w, -1.0, 1.0);

saw = bi;

tri = bi : fabs : bi;

bi = 2.0*_ - 1.0;

mod1 = fmod(_, 1.0);

phase is the phase generation and phase_to_osc is the wave shaper.

I will probably use this kind of phaseshaping oscillator structure also for my coming synth projects, Faust and non-Faust, because it is really flexible. More about this approach is explained in this paper : Phaseshaping Oscillator Algorithms for Musical Sound Synthesis

With the multi-mode filter there is a similar issue. My current naive form looks like this

reson_filter(type, freq, res) = _ <: select4(type,
    resonlp(freq, res, 1.0), // lowpass
    resonhp(freq, res, 1.0), // highpass
    resonbp(freq, res, 1.0), // bandpass
    resonbr(freq, res, 1.0)) // bandreject
with {
    resonbr(fc,Q,gain,x) = (gain * x) - resonbp(fc,Q,gain,x);

I have experienced a little bit with Moog ladder filter designs in Faust, but nothing really properly working has come out yet.

This synth is again wrapped as LV2 synth plugin with a GTK+ gui. I used Cairo to draw the knobs. Here is a screenshot :

The code is hosted at GitHub, so take a look if you are interested. I do not plan to maintain this project much, as I don’t see Faust in it’s current form the right tool for virtual analog synth design. Things I intended but didn’t manage to do are listed in the Issues section.

Polyphonic operation inside Faust is slow and I expect modulation matrices, alternative wirings and unison modes to be difficult to implement. Faust works quite well though for monophonic synths with a fairly static wiring, since the automatic SVG generation from Faust code makes such structures much more accessible than code.

Flauta is a project I am currently working on. It is a port of a STK based Flute model by Patricio de la Cuadra to Faust. Flauta is monophonic, uses a complex but static structure and most of the elements are mandatory, so Faust is a great match. I will write more about Flauta when we get some proper releases done.

Have you tried similar things with Faust? I am interested to hear more about Synth projects with Faust.

Concerning Virtual Analog in Faust, there is also the foo-yc20 project by Sampo Savolainen, which is a virtual model of the Yamaha YC-20 organ. It also uses a polyphonic design on the Faust level and has a much nicer GUI than mine 😉


Flute synthesis with Faust

A while ago I purchased an Akai EWI 4000s wind controller and as a Ubuntu user I tried to find suitable synthesizers to be used with my controller, since the built in sounds are fairly synthetic sounding.

After having searched for a while I decided that I could write my own. Java is my strongest programming language, but Java didn’t feel like a good tool to write a synthesizer, because of my fear that garbage collection cycles would cause lots of clicks and glitches in the audio output.

Now I delved a bit more into synth programming. I read some books on C and C++ programming and picked the LV2 audio plugin standard as the glue library between my DSP code and a synth host. LV2 is the successor to the LADSPA standard, and as we are talking about Linux standards here, it is not the only one, and seems to be competing at least with VST for Linux and DSSI.

The C API for LV2 is fairly understandable, but I picked the object oriented way, because I found the excellent LV2 C++ tools wrapper library. LV2 programming for the complete idiot is a very good tutorial for this library. Also the study of the lv2-mdaEPiano sources was helpful.

After I had mastered enough C++ and had picked the right libraries I was ready to get started. I was already before familiar with Physical modeling synthesis, and picked it as the synthesis paradigm. To be more exact, Waveguide synthesis.

I picked the STK library as another dependency, to get the proper building blocks for waveguide synthesis and some example models. While the STK source code is quite nice to read, the library is a little bit verbose and after some time I got frustrated and decided to replace the STK library with something more agile.

I searched around and found the stk-faust project, a port of the STK instruments to the Faust programming language. Faust is a functional programming realtime audio signal processing. I read some tutorials on Faust and decided that the compact syntax, the block diagram auto-generation and benchmarks were convincing enough to try it out.

After that I studied the Faust based STK instrument ports and tried to integrate the flute model into LV2 context. This proved to be quite easy, all I had to was connecting the audio buffers and sync the control parameters. But I wanted to try a little bit more expressive model of the Flute. I picked an improved Waveguide model of a Flute and began to port it into Faust.

This took some while as modeling feedback loops and crossing connections in Faust is well supported, but syntactically weird. Nevertheless I got it finished after some time and managed to play some Flute like sounds with it.

Here is the top level diagram for it :

The sources are accessible from GitHub

The model has several shortcomings though. I have not yet been able to model the involved lowpass filters properly, toneholes are not included in the model which makes pitch changes sound unnatural and the excitation is pure white noise.

Concerning using Faust for your DSP projects, I believe that it is a very good language for structurally unchanging models. If there lots of dynamic components in your model that can be added, removed and moved around than Faust is probably not the right tool. If your model is fairly static and C++ works for you as an integration language, then give it a try.

I was very convinced by the Faust syntax, block diagram generation, UI building approach and easy integration into various contexts.