Nsound Generators

Note

These examples use a small sample rate (between 100 and 1000) to keep the generation of this documentation relitively quick. In practice, higher quality sample rates should be used (44100, 48000, 96000 etc.).

The Generator Draw Routines

All Nsound Generators include basic drawing functions. Some basic Generator function documentation:

Generator(sample_rate)
Generator.drawDecay(duration, alpha=2.0*pi)
Generator.drawLine(duration, y1, y2)
Generator.drawGaussian(duration, mu, sigma, normalize=True)
Generator.drawFatGaussian(duration, pass_band_percent=0.01)
Generator.drawParabola(duration, y1, x2, y2, y3)
Generator.drawSine(duration, frequency)
Generator.drawSine2(duration, frequency, phase)
Generator.drawWindow(duration, window_type)

Drawing lines:

from Nsound import *

g = Generator(100.0)

b = Buffer()
b << g.drawLine(1.0, 0.0, 1.0)
b << g.drawLine(1.0, 1.0, 1.0)
b << g.drawLine(1.0, 1.0, 0.0)

b.plot("Basic Lines")

[hires.png, pdf]

_images/abc8e35e17.png

Drawing a decaying curve:

from Nsound import *

g = Generator(100.0)

b = Buffer()
b << g.drawDecay(1.0)

b.plot("Exponential Decay")

[hires.png, pdf]

_images/527f403398.png

Drawing Gaussians:

from Nsound import *

g = Generator(100.0)

b = Buffer()
b << g.drawGaussian(1.0, 0.5, 0.15)

b.plot("A Gaussian Curve")

b2 = Buffer()
b2 << g.drawFatGaussian(1.0, 0.10)

b2.plot("A Fat Gaussian Curve")

[hires.png, pdf]

_images/1d34acc02f_00.png

[hires.png, pdf]

_images/1d34acc02f_01.png

Drawing parabolas, note that an AudioStream is used so the x axis has the units of seconds:

from Nsound import *

from matplotlib import pylab

sr = 1000.0

g = Generator(sr)

###########################################################################
# First Parabola

a = AudioStream(sr, 1)
a << g.drawParabola(1.0, 0.0, 0.5, 1.0, 0.0)

a.plot("A Parabola")

# Plot red cross hairs at the three points
pylab.plot(
    [0.0, 0.5, 1.0],
    [0.0, 1.0, 0.0],
    "r+", markersize = 10.0)

pylab.xlim(-0.05, 1.05)
pylab.ylim(-0.05, 1.05)

###########################################################################
# Seconds Parabola

a = AudioStream(sr, 1)
a << g.drawParabola(1.0, 0.333, 0.666, 0.666, 0.0)

a.plot("Another Parabola")

# Plot red cross hairs at the three points
pylab.plot(
    [0.000, 0.666, 1.000],
    [0.333, 0.666, 0.000],
    "r+", markersize = 10.0)

pylab.xlim(-0.05, 1.05)
pylab.ylim(-0.05, 1.05)

[hires.png, pdf]

_images/21201731d0_00.png

[hires.png, pdf]

_images/21201731d0_01.png

Drawing sine waves. The drawSine() and drawSine2() functions use the C++ std::sin() function to generate samples. These functions do not use a wavetable, so there will not be any interpolation or aliasing problems that may occur for wavetable oscillator classes that derive from Generator. Some examples:

from Nsound import *

sr = 1000.0

g = Generator(sr)

###########################################################################
# 3 Hz
a = AudioStream(sr, 1)
a << g.drawSine(1.0, 3.0)

a.plot("3 Hz")

###########################################################################
# Dynamic Frequency
a = AudioStream(sr, 1)
a << g.drawSine(1.0, g.drawLine(1.0, 0.0, 10.0))

a.plot("Dynamic Frequency")

###########################################################################
# Dynamic Phase
a = AudioStream(sr, 1)
a << g.drawSine2(1.0, 3.0, g.drawLine(1.0, 0.0, 1.0))

a.plot("Dynamic Phase")

[hires.png, pdf]

_images/e3867891d7_00.png

[hires.png, pdf]

_images/e3867891d7_01.png

[hires.png, pdf]

_images/e3867891d7_02.png

The Sine Generator

The Sine class is derived from Generator. Some of its documentation:

Sine(sample_rate)
Sine.generator(duration, frequency)

It inherits all the draw functions for convience. Lets generate a 3 Hz signal.

from Nsound import *

s = Sine(100.0)

b = Buffer()
b << s.generate(1.0, 3.0)

b.plot("1 second at 3 Hz")

[hires.png, pdf]

_images/4bca579f86.png

Now lets multiply the 3 Hz signal by Gaussian and decaying envelopes.

from Nsound import *

s = Sine(100.0)

g = Buffer()
g << s.drawGaussian(1.0, 0.5, 0.15)

d = Buffer()
d << s.drawDecay(1.0)

b = Buffer()
b << s.generate(1.0, 3.0)

gauss = b * g
decay = b * d

gauss.plot("3 Hz With Gaussian Envelope")
decay.plot("3 Hz With Decaying Envelope")

[hires.png, pdf]

_images/fd183a1923_00.png

[hires.png, pdf]

_images/fd183a1923_01.png

The Generator class also allow dynamically changing frequencies. Simply create a Buffer to hold frequency values and pass the Buffer to the generate function. Below the frequency will change from 1 to 10 back to 1.

from Nsound import *

s = Sine(1000.0)

freqs = Buffer()
freqs << s.drawLine(1.0,  0.0, 10.0) \
      << s.drawLine(1.0, 10.0,  0.0)

freqs.plot("Frequencies in Hz")

b = Buffer()
b << s.generate(2.0, freqs)

b.plot("Dynamic frequencies")

[hires.png, pdf]

_images/2d9af83482_00.png

[hires.png, pdf]

_images/2d9af83482_01.png

The Sawtooth Generator

As you would expect, the Sawtooth Generator draws sawtooths, with the specified number of harmonics.

Sawtooth(sample_rate, n_harmonics)
Sawtooth.generator(duration, frequency)
from Nsound import *

saw = Sawtooth(100, 3)

b = Buffer()
b << saw.generate(3.0, 1.0)

b.plot("Sawtooth, 3 harmonics")

saw = Sawtooth(100, 12)

b = Buffer()
b << saw.generate(3.0, 1.0)

b.plot("Sawtooth, 12 harmonics")

[hires.png, pdf]

_images/41e1b53603_00.png

[hires.png, pdf]

_images/41e1b53603_01.png

The Square Generator

As you would expect, the Square Generator draws square waves, with the specified number of harmonics.

Square(sample_rate, n_harmonics)
Square.generator(duration, frequency)
from Nsound import *

square = Square(100, 3)

b = Buffer()
b << square.generate(3.0, 1.0)

b.plot("Square wave, 3 harmonics")

square = Square(100, 12)

b = Buffer()
b << square.generate(3.0, 1.0)

b.plot("Square wave, 12 harmonics")

[hires.png, pdf]

_images/73e0600630_00.png

[hires.png, pdf]

_images/73e0600630_01.png

Table Of Contents

Previous topic

Nsound Instruments

Next topic

Nsound Filters

This Page