8 releases

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

#5 in #sine

33 downloads per month
Used in surge-synthesizer

GPL-3.0 license

230KB
3K SLoC

SineShaper

The SineShaper is a digital signal processing (DSP) component used for shaping the amplitude of an input signal using a sine wave. It is a type of waveshaping technique that can be used to add harmonics and distortion to a sound signal.

The shape parameter controls the amount of distortion applied to the input signal, with higher values leading to more distortion. The shape parameter ranges from 0 to 1, with a value of 0 resulting in no distortion and a value of 1 resulting in maximum distortion.

The SineShaper works by using a sine wave as a transfer function to shape the input signal. The input signal is multiplied by the sine wave and then passed through a low-pass filter to remove any high-frequency components that may have been introduced by the shaping process.

Mathematically, the SineShaper can be expressed as:

y[n] = sin(shape * x[n]) * x[n]

where x[n] is the input signal, y[n] is the output signal, and shape is the distortion amount.

In summary, the SineShaper is a useful tool for adding distortion and harmonics to a sound signal, and can be used in a variety of applications such as music production and sound design.

87> could you write me some python which illustrates this effect?

Sure! Here's some Python code that generates a sine wave and applies the SineShaper effect to it:

import numpy as np
import matplotlib.pyplot as plt

def sine_shaper(x, shape):
    return np.sin(np.arcsin(x) * shape)

# Generate a sine wave
fs = 44100
f = 440
duration = 1
t = np.linspace(0, duration, int(fs * duration))
x = np.sin(2 * np.pi * f * t)

# Apply SineShaper effect
shape = 2.0
output_signal = sine_shaper(x, shape)

# Plot the input and output signals
plt.plot(t, x, label='Input')
plt.plot(t, output_signal, label='Output')
plt.legend()
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.show()

This code generates a sine wave with a frequency of 440 Hz and a duration of 1 second, applies the SineShaper effect with a shape value of 2.0, and plots the input and output signals on a graph. The sine_shaper function applies the SineShaper effect to the input signal.

88> what about some verilog-a code implementing this effect?

Sure, here's an example of a SineShaper implemented in Verilog-A:

`include "constants.vams"

module SineShaper(x, y, shape);
  input x;
  output y;
  parameter REAL shape = 0.5;
  REAL x_norm = x / pi;
  REAL y_norm = sin(pi * (x_norm + shape * (x_norm - (x_norm^3) / 3)));
  assign y = y_norm * pi;
endmodule

This code uses the same mathematical formula as the Python example to implement the SineShaper effect. The input signal is first normalized to the range [-1, 1], then the sine function is applied with an argument that depends on the shape parameter. The output is then scaled back up by pi to match the input range.

89> why would we choose this shaper over the others?

The SineShaper is a different type of shaper compared to the other shapers such as Clipper, AsymShaper, and DigiShaper. Rather than introducing distortion or saturation, the SineShaper applies a sine wave modulation to the input signal. This results in the addition of new frequencies to the input signal, which can create a richer and more complex timbre.

The choice of shaper depends on the desired sound and musical context. If the goal is to add harmonic content and create a more complex timbre, the SineShaper may be a good choice. However, if the goal is to create distortion or saturation, then one of the other shapers may be a better choice. It ultimately depends on the specific musical application and desired outcome.

90> so, we can vary the frequency of the sine wave to determine the character of the resulting timbre. is this correct?

That is correct. The frequency of the sine wave used in the SineShaper affects the harmonic content of the output signal. Lower frequencies produce more even-order harmonics, while higher frequencies produce more odd-order harmonics. By adjusting the frequency of the sine wave, you can tailor the harmonic content to achieve the desired timbre.

91> how much latency does this shaper introduce into the signal path? how do we measure it?

The amount of latency introduced by the SineShaper will depend on the implementation details, such as the sample rate of the audio signal, the size of the processing buffer, and the number of operations performed per sample.

One way to measure the latency is to perform a test with a known audio signal and measure the time it takes for the processed signal to be outputted relative to the input signal. This can be done using a oscilloscope or other audio measurement tools.

It's important to note that any amount of processing, including the SineShaper, will introduce some amount of latency, which can be problematic for real-time applications such as live performance or interactive audio. It's generally a good practice to minimize the latency as much as possible to ensure a good user experience.

92> how does this shaper compare to, say, using a triangle, a square, or a saw to implement?

Using a sine wave as the shaper in a wave shaping circuit can produce a smoother and more rounded sound compared to using a triangle, square, or saw wave. This is because a sine wave has no sharp edges or corners, which can result in fewer high-frequency harmonics being generated by the shaping process.

On the other hand, using a triangle, square, or saw wave can produce more complex and harmonically rich sounds due to the presence of more high-frequency harmonics. The resulting sound can be more aggressive and distorted compared to using a sine wave as the shaper.

In general, the choice of wave shape for a shaper depends on the desired sound and the specific application. A sine wave shaper may be more suitable for producing subtle harmonic variations or for applications where a smoother, more rounded sound is desired. A triangle, square, or saw wave shaper may be more suitable for producing more complex, aggressive sounds.

Dependencies

~12–20MB
~302K SLoC