## nightly surge-math

surge synthesizer -- dsp math used in synthesis

### 31 releases

 0.2.12-alpha.0 Apr 7, 2023 Dec 19, 2022 Jun 21, 2022 Mar 14, 2022 Oct 27, 2021

#416 in Math

Used in 66 crates (59 directly)

110KB
2.5K SLoC

## surge-math

Surge-math is a Rust crate that provides a collection of mathematical functions and utilities for the surge synthesizer system. These functions are designed to be high-performance and efficient, and can be used for various tasks such as signal processing, waveform generation, and parameter modulation.

The surge-math crate includes functions for basic mathematical operations such as addition, subtraction, multiplication, and division, as well as more specialized functions such as trigonometric functions (sine, cosine, tangent), exponential functions (exponential, logarithm), and fast approximations of these functions optimized for performance on modern CPUs.

The crate also includes functions for signal processing such as filtering (all-pass filter), amplitude and level manipulation (gain adjustment, limit range), and distortion and clipping (hard clip, soft clip). Additionally, the crate includes functions for waveform generation such as random number generation, white noise generation, and waveform interpolation (linear interpolation, cubic interpolation).

### Mathematical Analysis

The surge-math crate includes a wide range of mathematical functions and utilities, covering topics such as algebra, trigonometry, calculus, and probability theory. Many of these functions are optimized for performance using SIMD instructions or other techniques, and are designed to be highly efficient in the context of audio signal processing.

For example, the crate includes functions for computing the sine and cosine of an angle, which are fundamental trigonometric functions used in a variety of audio applications. The sine and cosine functions can be computed using the Taylor series expansions:

``````sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ...
cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! + ...
``````

where x is the angle in radians. These series converge rapidly for small values of x, but can become unstable for large values of x. To address this issue, the crate includes specialized implementations of the sine and cosine functions optimized for performance and stability on modern CPUs.

Overall, the surge-math crate provides a comprehensive collection of mathematical functions and utilities for the surge synthesizer system. These functions can be used for a wide range of tasks in audio signal processing, waveform generation, and parameter modulation, and are designed to be highly efficient and performant.

11> which mathematical utility functions are most useful for audio processing in the context of synthesizers?

There are many mathematical utility functions that are useful for audio processing in the context of synthesizers. Here are a few examples:

1. Trigonometric functions: Sine, cosine, and tangent functions are commonly used in synthesizers for waveform generation and modulation. These functions can be used to generate basic waveforms such as sine waves, as well as to modulate other parameters such as amplitude, frequency, and phase.

2. Exponential functions: Exponential functions such as the exponential and logarithmic functions are commonly used in synthesizers for envelope generation and modulation. These functions can be used to generate ADSR envelopes, for example, by specifying the attack, decay, sustain, and release times as parameters.

3. Fourier transform: The Fourier transform is a powerful mathematical tool that is used in synthesizers for spectral analysis and synthesis. The Fourier transform can be used to decompose a complex waveform into its constituent frequencies, and can be used to generate complex waveforms from a set of harmonically related sine waves.

4. Random number generation: Random number generation functions such as the rand01 and rand11 functions are useful for generating noise and other random signals in synthesizers. These functions can be used to generate white noise, pink noise, and other types of noise signals.

5. Interpolation functions: Interpolation functions such as linear interpolation and cubic interpolation are commonly used in synthesizers for waveform generation and processing. These functions can be used to generate smooth transitions between different waveforms, or to upsample or downsample waveforms to match different sampling rates.

Overall, the most useful mathematical utility functions for audio processing in the context of synthesizers depend on the specific task and the desired outcome. Trigonometric functions, exponential functions, and Fourier transforms are commonly used in a variety of synthesizer applications, while random number generation and interpolation functions are useful for specific tasks such as noise generation and waveform processing.

12> let's go into some more detail about these, where and when we use them, etc

Sure, let's take a closer look at these mathematical utility functions and how they are used in the context of synthesizers:

1. Trigonometric functions: Sine, cosine, and tangent functions are commonly used in synthesizers for waveform generation and modulation. For example, a sine wave can be generated by computing the sine function at each sample point in time. These functions can also be used to modulate other parameters such as amplitude, frequency, and phase. For example, a frequency modulation (FM) synthesizer uses the sine function to modulate the frequency of one waveform with the output of another waveform.

2. Exponential functions: Exponential functions such as the exponential and logarithmic functions are commonly used in synthesizers for envelope generation and modulation. Envelopes are used to control the amplitude, frequency, and other parameters of a sound over time. For example, an ADSR envelope generator uses exponential functions to generate the attack, decay, sustain, and release phases of the envelope.

3. Fourier transform: The Fourier transform is a powerful mathematical tool that is used in synthesizers for spectral analysis and synthesis. The Fourier transform can be used to decompose a complex waveform into its constituent frequencies, and can be used to generate complex waveforms from a set of harmonically related sine waves. For example, a subtractive synthesizer uses a bank of bandpass filters to isolate different harmonics of a waveform, and then combines them to generate a new waveform.

4. Random number generation: Random number generation functions such as the rand01 and rand11 functions are useful for generating noise and other random signals in synthesizers. These functions can be used to generate white noise, pink noise, and other types of noise signals. Noise signals can be used for a variety of purposes in synthesizers, such as simulating acoustic environments or adding randomness to a sound.

5. Interpolation functions: Interpolation functions such as linear interpolation and cubic interpolation are commonly used in synthesizers for waveform generation and processing. These functions can be used to generate smooth transitions between different waveforms, or to upsample or downsample waveforms to match different sampling rates. For example, a wavetable synthesizer uses interpolation to smoothly transition between different waveforms as the pitch of a note changes.

Overall, these mathematical utility functions are essential tools for synthesizer designers and programmers. By understanding how these functions work and how they can be used in the context of synthesizers, it is possible to create complex and expressive sounds that are unique and interesting.

13> let's look a trig functions for audio

Trigonometric functions are commonly used in audio processing for waveform generation and modulation. The sine function, in particular, is a fundamental building block of sound synthesis and processing. Here are a few examples of how trigonometric functions are used in audio processing:

1. Waveform generation: Trigonometric functions such as the sine, cosine, and tangent functions are used to generate basic waveforms such as sine waves, triangle waves, and sawtooth waves. These waveforms can be used as the basis for more complex sounds or can be combined to create new waveforms.

2. Frequency modulation (FM) synthesis: FM synthesis is a popular synthesis technique that uses trigonometric functions to modulate the frequency of one waveform with the output of another waveform. The modulating waveform is typically a sine wave, and the carrier waveform can be any other waveform. FM synthesis can create complex, evolving sounds with a wide range of timbres.

3. Amplitude modulation (AM) synthesis: AM synthesis is a synthesis technique that uses trigonometric functions to modulate the amplitude of one waveform with the output of another waveform. The modulating waveform can be any waveform, but typically it is a sine wave. AM synthesis can create metallic, bell-like sounds with a rich harmonic content.

4. Filtering: Trigonometric functions are used in the design of filters, which are used to shape the frequency content of a sound. For example, a low-pass filter can be designed using the sine function, which has a gradual roll-off as the frequency increases. Other filter types, such as high-pass filters and band-pass filters, can be designed using trigonometric functions as well.

Overall, trigonometric functions are powerful tools for audio processing, and are used in a variety of applications including waveform generation, modulation synthesis, filtering, and more. By understanding how these functions work and how they can be used in audio processing, it is possible to create a wide range of complex and interesting sounds.

14> let's look at exponential functions for audio

Exponential functions are commonly used in audio processing for envelope generation and modulation. Envelopes are used to control the amplitude, frequency, and other parameters of a sound over time, and exponential functions are an important tool for creating smooth, natural-sounding transitions. Here are a few examples of how exponential functions are used in audio processing:

1. ADSR envelopes: An ADSR envelope is a type of envelope generator that uses exponential functions to create the attack, decay, sustain, and release phases of an envelope. The attack phase is the initial ramp-up in amplitude, the decay phase is the gradual decrease in amplitude after the attack, the sustain phase is the steady-state amplitude, and the release phase is the gradual decrease in amplitude after the note is released.

2. Vibrato: Vibrato is a modulation effect that involves varying the pitch of a sound over time. Exponential functions are used to create smooth, natural-sounding vibrato effects by controlling the rate and depth of the modulation.

3. Tremolo: Tremolo is a modulation effect that involves varying the amplitude of a sound over time. Exponential functions are used to create smooth, natural-sounding tremolo effects by controlling the rate and depth of the modulation.

4. Decay curves: Exponential functions are used to create natural-sounding decay curves for sounds such as drums and percussion. By shaping the decay curve with an exponential function, it is possible to create a sound that gradually decays in amplitude, rather than abruptly cutting off.

Overall, exponential functions are an essential tool for creating natural-sounding transitions in audio processing. By understanding how these functions work and how they can be used in the context of audio processing, it is possible to create expressive and dynamic sounds that respond to changes in amplitude, frequency, and other parameters over time.

15> let's look at the fourier transform in the context of audio

The Fourier transform is a powerful mathematical tool that is used in audio processing for spectral analysis and synthesis. In audio processing, the Fourier transform is used to decompose a complex waveform into its constituent frequencies, and can be used to generate complex waveforms from a set of harmonically related sine waves. Here are a few examples of how the Fourier transform is used in audio processing:

1. Spectrum analysis: The Fourier transform can be used to analyze the frequency content of a sound waveform. By applying the Fourier transform to a sound waveform, it is possible to decompose the waveform into its constituent frequencies and determine the relative strength of each frequency component. This information can be used to identify and isolate specific frequency components, such as harmonics, noise, and other artifacts.

2. Filtering: The Fourier transform can be used to design filters that selectively remove or enhance specific frequency components of a sound waveform. By analyzing the frequency content of a sound waveform using the Fourier transform, it is possible to identify the frequency components that need to be removed or enhanced, and design a filter that achieves this goal.

3. Waveform synthesis: The Fourier transform can be used to generate complex waveforms from a set of harmonically related sine waves. This technique is used in additive synthesis, which involves combining sine waves at different frequencies and amplitudes to create complex sounds. By analyzing the frequency content of a complex waveform using the Fourier transform, it is possible to identify the frequencies and amplitudes of the constituent sine waves, and use this information to synthesize the waveform.

4. Pitch detection: The Fourier transform can be used to detect the fundamental frequency of a sound waveform, which is an important parameter for pitch detection. By analyzing the frequency content of a sound waveform using the Fourier transform, it is possible to identify the frequency component that corresponds to the fundamental frequency, and use this information to detect the pitch of the sound.

Overall, the Fourier transform is a powerful tool for audio processing, and is used in a variety of applications including spectrum analysis, filtering, waveform synthesis, and pitch detection. By understanding how the Fourier transform works and how it can be used in audio processing, it is possible to create complex and expressive sounds that are unique and interesting.

16> let's look at random number generation in the context of audio

Random number generation is a useful tool for creating unpredictable and dynamic sounds in audio processing. In the context of audio, random number generators are commonly used to create noise signals, to add variation to synthesized sounds, and to control various parameters of a sound over time. Here are a few examples of how random number generation is used in audio processing:

1. Noise generation: Random number generators are commonly used to generate noise signals, which can be used to create sound effects, to simulate the sound of natural phenomena, or to add a sense of randomness and unpredictability to a sound. Different types of noise signals, such as white noise, pink noise, and brown noise, can be generated using different random number generation algorithms.

2. Modulation: Random number generators can be used to modulate various parameters of a sound over time, such as amplitude, frequency, and filter cutoff. By using a random number generator to modulate these parameters, it is possible to create dynamic and evolving sounds that are constantly changing and unpredictable.

3. Stochastic synthesis: Stochastic synthesis is a synthesis technique that involves using random number generators to control various parameters of a sound synthesis algorithm. By introducing randomness and unpredictability into the synthesis process, it is possible to create sounds that are unique and interesting, with a wide range of timbres and textures.

4. Algorithmic composition: Random number generators can be used in algorithmic composition, which involves using a set of rules and algorithms to generate music automatically. By introducing randomness and unpredictability into the composition process, it is possible to create music that is constantly changing and evolving, with a sense of spontaneity and improvisation.

Overall, random number generation is an important tool for creating dynamic and unpredictable sounds in audio processing. By understanding how random number generators work and how they can be used in the context of audio processing, it is possible to create unique and interesting sounds that respond to changes in the environment, and that can surprise and delight listeners.

17> let's look at Interpolation functions in the context of audio

Interpolation functions are a useful tool for audio processing, and are commonly used to smooth out and interpolate between discrete values in a sound signal. In the context of audio, interpolation functions are used for a variety of purposes, such as resampling, waveform synthesis, and envelope generation. Here are a few examples of how interpolation functions are used in audio processing:

1. Resampling: Resampling is a process that involves changing the sample rate of a sound signal. When resampling a signal, interpolation functions are used to estimate the values of the signal at the new sample points. Different types of interpolation functions, such as linear interpolation and cubic interpolation, can be used to achieve different levels of accuracy and smoothness in the resampled signal.

2. Waveform synthesis: Interpolation functions are commonly used in waveform synthesis techniques, such as wavetable synthesis and granular synthesis. In these techniques, a set of discrete values representing a waveform are interpolated to create a continuous waveform. By using interpolation functions to interpolate between the discrete values, it is possible to create complex and dynamic waveforms that are unique and interesting.

3. Envelope generation: Interpolation functions are used to create smooth and natural-sounding envelopes for sounds. By using interpolation functions to interpolate between discrete values representing the envelope shape, it is possible to create envelopes that smoothly transition from one value to another, rather than abruptly jumping between values.

4. Waveform shaping: Interpolation functions are used to shape the waveforms of sounds in a variety of ways. For example, they can be used to smooth out harsh or abrupt transitions in a waveform, or to create complex modulations that change the shape of a waveform over time.

Overall, interpolation functions are an important tool for audio processing, and are used in a variety of applications including resampling, waveform synthesis, envelope generation, and waveform shaping. By understanding how interpolation functions work and how they can be used in the context of audio processing, it is possible to create complex and expressive sounds that respond to changes in amplitude, frequency, and other parameters over time.

~11–20MB
~291K SLoC