
The article is devoted to the synthesis of four main sound waves: sawtooth (saw), square (square), triangular (triangle) and sinusoidal (sine). To work, you need a software environment for processing sound
Reaktor from the company Native Instruments. The demo version can be downloaded
on the official website of Native Instruments (the work limit is 30 minutes, project saving is disabled). Information will be useful not only to the owners of this software, but also to those who are interested in programming and sound processing in general. Tackle decently screenshots, careful traffic!
Theory
It is assumed that before starting work, you familiarized yourself with the manual, main modules and Reaktor interface. The program has two levels of work:
Primary Level (primary) and
Core Level (internal). The differences speak for themselves - at the Primary level, all solutions are ready (wave generators, amplitude envelopes, effects, etc.), while at the Core level, you can create everything yourself. Core Level is your own programming language created by the NI developer company. It contains the same operators, cycles and variables as in other programming environments.
We know that digital audio is different from analog. An analog signal is continuous in contrast to digital signal, therefore such a concept as the sampling frequency
SR.R (Sample Rate) is used to convert the first to the second. The sampling frequency (or sampling frequency) is the number of samples per second required to convert a continuous signal to a discrete one. Accordingly, the greater the sampling frequency, the richer the spectrum of the discrete signal. In Reaktor, for convenience, there is still such a thing as a
SR.C (Sample Rate Clock) sampling frequency event: SR.C = 1 / SR.R.
')
If we have a standard sampling frequency of 44100 Hz, this means that the sampling frequency event occurs 44100 times in 1 second. Thus, Reaktor recognizes two signal levels - the audio signal (Audio) and the event signal (Event). With the first, everything is clear, and the second type of signal can be compared with a flash that occurs when fireworks are launched. The event signal does not sound at all - it is just a figure with a value at a certain time interval, therefore SR.C is an event with a value of 0.
Sawtooth wave
Synthesis in Reaktor is the easiest to start with a saw wave. The fact is that its phase is fundamental for constructing the phases of all other waves.

From the figure it is clear that the sound wave as a whole is of itself and it is characterized by two main indicators -
phase and
amplitude . This is a kind of function with repeated cycles (phase with a certain frequency), which in this case takes values from 0 to 1 (amplitude). Phase frequency can be expressed both in Hertz and in pitch. The pitch of the sound is measured in tones and takes values from 0 to 127. For example, note C (to) on the 3rd octave corresponds to tone 48, which is approximately equal to 130.8137 Hz. The amplitude can also be represented both as a numerical value (1), and in decibels (0 dB). In practice, we need that each time at a sampling frequency event we get some value from 0 to 1 in accordance with the function shown in the figure. Using ready-made solutions in Reaktor is strictly punishable with unwritten rules.



* -
for work, I used the new version of Reaktor, as I became the owner of a new licensed beta version after buying an old one, but the interface of Core and Primary levels is no different from the previous one.To begin, we need to create a new project (File - New Ensemble). Screenshots are presented in accordance with the depth of the open level of the project. The first screenshot shows the main tool connected to the outputs of the audio card (in the old version it looks a little different). The second screenshot already shows the insides of the main instrument (Primary level). All modules can be called up with the right mouse button. The Note Pitch and Gate modules located in the Built-in Module - MIDI In section are responsible for receiving a MIDI signal from a keyboard or MIDI controller, i.e. Note Pitch receives values from 0 to 127 and is responsible for the pitch of the sound, and Gate is from 0 to 1 and is responsible for the amplitude of the sound (on the keyboard, as a rule, from 0 to 0.6299). Inside the Saw Waveform module, there is a continuous phase of the sawtooth wave, so we use the Multiply module (multiplication) in conjunction with the Gate module so that the signal is controlled by the MIDI device, rather than being played continuously. The third screenshot is the most interesting, because it is here that the phase of the wave occurs. The first part of the structure is the conversion of the pitch to the phase frequency. The calculation is made according to the following formula:

Consider the structure of a specific example. At pitch 48, our cycle, which begins with the SR.C sampling frequency event, receives the following data: F = 130.8128, 2 / SR.R = 0.000045351 (with a standard sampling frequency of 441000 Hz) - Nyquist frequency ( equal to half the sampling frequency), F * Nyquist frequency = 0.0059325.
If the value coming into the modulus sum (+) exceeds one, it is sent to the difference modulus, where one is subtracted from it. Thus, our function takes a strict range from 0 to 1. After that, the value is stored in the memory module (Write) and again enters the memory reading module (Read), where at the next sample frequency event is added to the previous value. If we connect an oscilloscope (Micro Scope module) to the output of the Core module (Phase), we will get exactly the same function as on the sawtooth wave graph. In the next cycle, we have: 0.0059325 + 0.0059325 = 0.018665, 0.018665 + 0.0059325 = 0.01777975, etc.
* -
important note: the article omits the question of antialiasing (anti-aliasing), since This is a topic for individual discussions.Square wave

Unlike a sawtooth, a square wave takes values equal to only 0 or 1. It is not difficult to guess how to synthesize this wave using the previous phase: if the sawtooth wave phase is greater than 0.5, then the square wave phase value is 1, if it is less, 0.


Triangular wave

In order to transform the phase of the sawtooth wave into a triangular wave, it is necessary to transform the first one so that it takes negative values, and then take its module. Everything is quite simple - we subtract 0.5 from the phase of the sawtooth wave, getting the range of values from -0.5 to 0.5, take the absolute value of this range and multiply by 2, returning to the range of values from 0 to 1. Transformations occur inside the same Core module Square .

Sine wave

With the synthesis of a sine wave, things are a little more complicated. The point is that the sine function is continuous and periodic with a fundamental period of 2π, unlike other functions. One solution is to decompose the sine function using the Taylor Series, i.e. its transformation to an infinite sum of power functions. It looks like this:

Transforming the phase of the sawtooth wave to 2π and shifting the function for conversion, we obtain the following picture in Reaktor:

In this scheme, the sums 6, 120, 5040 and 362880 are the factorials of the numbers 3 !, 5, 7! and 9! accordingly, x is the Quick Bus used for convenience (added by calling the additional menu), and numeric conversions at the end of the formula are conversions to functions in the usual range from 0 to 1.
Resources used
Wikipedia :
WaveformPitchFrequencyAmplitudeNyquist frequencyTaylor seriesOther resources (including my projects) are in English:Reaktor For You Archive (my archive of articles for Reaktor) * - available only to users of licensed versions
My threads on Reaktor’s official forum:Beginner LessonsCore sound waves creationAdditional resource:Reaktor Community in Russia