# Pysound oscillators module

Martin McBride
2021-09-11

The Pysound oscillators module contains some functions for creating oscillator tones:

• sine_wave - generates a sine wave.
• square_wave - generates square and pulse waves.
• saw_wave - generates sawtooth and triangle waves.
• table_wave - generates a wave based on a table of values.
• noise - generates white noise.

Most functions accept a BufferParams object to control the length and sample rate of the sound.

These functions can accept sound buffers for certain parameters, which allows them to create time varying sounds.

## sine_wave

Creates a sine wave signal.

sine_wave(params, frequency=400, amplitude=1, offset=0)

Parameter Type Description
params BufferParams Length and sample rate of output sound
frequency Number or sound buffer Frequency of wave, Hz
amplitude Number or sound buffer Amplitude of wave
offset Number or sound buffer Offset of wave signal

Generates a sine wave of the specified frequency.

The amplitude is the peak amplitude of the wave. For example, with an amplitude of 1.0, the wave oscillates between values +1.0 and -1.0.

The offset shifts the entire wave up or down. For example, with amplitude 1.0 and offset 3.0, the wave will oscillate between values +4.0 and +2.0

## square_wave

Creates a square wave signal.

square_wave(params, frequency=400, amplitude=1, offset=0, ratio=0.5)

Parameter Type Description
params BufferParams Length and sample rate of output sound
frequency Number or sound buffer Frequency of wave, Hz
amplitude Number or sound buffer Amplitude of wave
offset Number or sound buffer Offset of wave signal
ratio Number or sound buffer High time as a fraction of total wave time

Generates a square or pulse wave of the specified frequency.

The amplitude is the peak amplitude of the wave. For example, with an amplitude of 1.0, the wave oscillates between values +1.0 and -1.0.

The offset shifts the entire wave up or down. For example, with amplitude 1.0 and offset 3.0, the wave will oscillate between values +4.0 and +2.0

The ratio controls the high and low times. The default ratio of 0.5 means that the signal is high for 50% of the time, low for 50% of the time (a square wave). A ratio of 0.2, for example, would mean that the signal would be high for 20% of the time, low for 80% of the time. The ratio affects the sound of the wave.

## saw_wave

Creates a saw wave signal.

square_wave(params, frequency=400, amplitude=1, offset=0, ratio=0.5)

Parameter Type Description
params BufferParams Length and sample rate of output sound
frequency Number or sound buffer Frequency of wave, Hz
amplitude Number or sound buffer Amplitude of wave
offset Number or sound buffer Offset of wave signal
ratio Number or sound buffer Rise time as a fraction of total wave time

Generates a sawtooth or triangle wave of the specified frequency.

The amplitude is the peak amplitude of the wave. For example, with an amplitude of 1.0, the wave oscillates between values +1.0 and -1.0.

The offset shifts the entire wave up or down. For example, with amplitude 1.0 and offset 3.0, the wave will oscillate between values +4.0 and +2.0

The ratio controls the rise and fall times. The default ratio of 0.5 means that the signal is rises for 50% of the time, falls for 50% of the time (a triangle wave). A ratio of 0, for example, would mean that the signal would start high, and spend the whole cycle falling, before jumping back to the high value to start the next wave (a sawtooth wave).

Other values of ratio create triangle waves wit unequal rise and fall times.

## table_wave

Creates a wave signal based on a table of values.

table_wave(params, frequency=400, amplitude=1, offset=0, table=None)

Parameter Type Description
params BufferParams Length and sample rate of output sound
frequency Number or sound buffer Frequency of wave, Hz
amplitude Number or sound buffer Amplitude of wave
offset Number or sound buffer Offset of wave signal
table Array Array of values

Generates a wave of the specified frequency based on a table of values.

table is set of values that define the shape of the wave. The wave is created by taking samples from the table. The table can be a NumPy array, Python list, or any other array-like object.

The array can be of any length, but it must contain exactly one cycle of the wave shape. For good quality sound you should uses a reasonably large array (several thousand elements).

If no table is supplied, a default sine wave will be provided (this is how the sine_wave function is implemented).

The amplitude is applied as a multiplier to the wave table. The offset is added to the wave table. If the data in the wave table varies between +1.0 and -1.0, the amplitude and offset will work in a similar way to the other wave functions.

## noise

Creates a wave signal based on a table of values.

noise(params, amplitude=1, offset=0#e)

Parameter Type Description
params BufferParams Length and sample rate of output sound
amplitude Number or sound buffer Amplitude of wave
offset Number or sound buffer Offset of wave signal

Generates noise. Each sample of the output array will be a random value in the range +1.0 to -1.0.

This creates white noise, a type of noise that contains equal amounts of every audible frequency.

amplitude and offset work in a similar way to sine_wave.

## Sign up to the Creative Coding Newletter

Join my newsletter to receive occasional emails when new content is added, using the form below:

## Popular tags

ebooks fractal generative art generativepy generativepy tutorials github koch curve l systems mandelbrot open source productivity pysound python recursion scipy sine sound spriograph tinkerbell turtle writing