return to src
 
Oscillator Class

Design idea from:
https://python.plainenglish.io/making-a-synth-with-python-oscillators-2cb8e68e9c3b
 
The provided code defines four different types of oscillators: SineOscillatorSquareOscillator
TriangleOscillator, and SawtoothOscillator. Each oscillator generates a specific waveform and 
provides methods for generating and playing audio waves. The Oscillator class is an abstract
base class which each oscillator pulls common functionality from.
 
Usage
To use the oscillators in your project, follow these steps:
 
1. Import the desired oscillator class(es) from the oscillators module.
2. Create an instance of the desired oscillator class, optionally specifying custom parameters 
such as frequency, sample rate, amplitude, and duration.
3. Call the generate_wave() method to generate the audio wave.
4. Optionally, you can call the play() method to play the generated wave using the default audio output device.
5. To stop the audio playback, call the stop() method.

 
Modules
       
numpy

 
Classes
       
abc.ABC
Oscillator
SawtoothOscillator
SineOscillator
SquareOscillator
TriangleOscillator

 
class Oscillator
    Oscillator(frequency: float = 220.0, sample_rate: int = 48000, amplitude: float = 8191.75, duration: float = 1.0) -> None
 
Oscillator is an abstract base class that implements common functionality for
concrete oscillator implementations.
 
Methods:
    __init__: constructor
    generate_wave: abstract method for generating wave
    crop_samples: method for cropping wave
 
 
Method resolution order:
Oscillator
abc.ABC
builtins.object

Methods defined here:
__init__(self, frequency: float = 220.0, sample_rate: int = 48000, amplitude: float = 8191.75, duration: float = 1.0) -> None
Initialization for common oscillator properties
Extend to create a custom oscillator
 
Args:
    frequency: the frequency of the oscillator (note)
    sample_rate: the sample rate (combines with frequency to determine step size)
    amplitude: the amplitude of the oscillator (volume)
    duration: the duration of the oscillator (combines with sample rate to determine time)
crop_samples(self, samples: numpy.ndarray) -> numpy.ndarray
This function crops the samples to the final zero crossing,
so the end of a wave matches up with the beginning.
Side effects: final signal is very slightly shorter.
 
Args:
    samples: wave to be trimmed
 
Returns:
    a trimmed wave
generate_wave(self) -> numpy.ndarray
override to generate a wave from the settings established
upon construction.

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

Data and other attributes defined here:
__abstractmethods__ = frozenset()

 
class SawtoothOscillator(Oscillator)
    SawtoothOscillator(frequency: float = 440.0, sample_rate: int = 48000, amplitude: float = 8191.75, duration: float = 1.0) -> None
 
# SAW TOOTH OSCILLATOR
 
 
Method resolution order:
SawtoothOscillator
Oscillator
abc.ABC
builtins.object

Methods defined here:
__init__(self, frequency: float = 440.0, sample_rate: int = 48000, amplitude: float = 8191.75, duration: float = 1.0) -> None
Extends Oscillator.__init__
generate_wave(self) -> numpy.ndarray
Generates a sawtooth wave

Data and other attributes defined here:
__abstractmethods__ = frozenset()

Methods inherited from Oscillator:
crop_samples(self, samples: numpy.ndarray) -> numpy.ndarray
This function crops the samples to the final zero crossing,
so the end of a wave matches up with the beginning.
Side effects: final signal is very slightly shorter.
 
Args:
    samples: wave to be trimmed
 
Returns:
    a trimmed wave

Data descriptors inherited from Oscillator:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class SineOscillator(Oscillator)
    SineOscillator(frequency: float = 440.0, sample_rate: int = 48000, amplitude: float = 8191.75, duration: float = 1.0) -> None
 
# SINE OSCILLATOR
 
 
Method resolution order:
SineOscillator
Oscillator
abc.ABC
builtins.object

Methods defined here:
__init__(self, frequency: float = 440.0, sample_rate: int = 48000, amplitude: float = 8191.75, duration: float = 1.0) -> None
Extends Oscillator.__init__
generate_wave(self) -> numpy.ndarray
Generates a sine wave

Data and other attributes defined here:
__abstractmethods__ = frozenset()

Methods inherited from Oscillator:
crop_samples(self, samples: numpy.ndarray) -> numpy.ndarray
This function crops the samples to the final zero crossing,
so the end of a wave matches up with the beginning.
Side effects: final signal is very slightly shorter.
 
Args:
    samples: wave to be trimmed
 
Returns:
    a trimmed wave

Data descriptors inherited from Oscillator:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class SquareOscillator(SineOscillator)
    SquareOscillator(frequency: float = 440.0, sample_rate: int = 48000, amplitude: float = 8191.75, duration: float = 1.0) -> None
 

 
 
Method resolution order:
SquareOscillator
SineOscillator
Oscillator
abc.ABC
builtins.object

Methods defined here:
__init__(self, frequency: float = 440.0, sample_rate: int = 48000, amplitude: float = 8191.75, duration: float = 1.0) -> None
Extends Oscillator.__init__
generate_wave(self) -> numpy.ndarray
Generates a square wave

Data and other attributes defined here:
__abstractmethods__ = frozenset()

Methods inherited from Oscillator:
crop_samples(self, samples: numpy.ndarray) -> numpy.ndarray
This function crops the samples to the final zero crossing,
so the end of a wave matches up with the beginning.
Side effects: final signal is very slightly shorter.
 
Args:
    samples: wave to be trimmed
 
Returns:
    a trimmed wave

Data descriptors inherited from Oscillator:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class TriangleOscillator(Oscillator)
    TriangleOscillator(frequency: float = 440.0, sample_rate: int = 48000, amplitude: float = 8191.75, duration: float = 1.0) -> None
 
# TRIANGLE OSCILLATOR
# Source: https://stackoverflow.com/questions/1073606/is-there-a-one-line-function-that-generates-a-triangle-wave
 
 
Method resolution order:
TriangleOscillator
Oscillator
abc.ABC
builtins.object

Methods defined here:
__init__(self, frequency: float = 440.0, sample_rate: int = 48000, amplitude: float = 8191.75, duration: float = 1.0) -> None
Extends Oscillator.__init__
generate_wave(self) -> numpy.ndarray
Generates a triangle wave

Data and other attributes defined here:
__abstractmethods__ = frozenset()

Methods inherited from Oscillator:
crop_samples(self, samples: numpy.ndarray) -> numpy.ndarray
This function crops the samples to the final zero crossing,
so the end of a wave matches up with the beginning.
Side effects: final signal is very slightly shorter.
 
Args:
    samples: wave to be trimmed
 
Returns:
    a trimmed wave

Data descriptors inherited from Oscillator:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)