| MD_SN76489 Library 1.0 Library for SN76489 sound generator

Classes | Public Types | Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | List of all members
MD_SN76489 Class Reference

`#include <MD_SN76489.h>`

Inheritance diagram for MD_SN76489:
[legend]

## Classes

struct | adsrEnvelope_t

## Public Types

enum | noiseType_t { PERIODIC_0 = 0x0, PERIODIC_1 = 0x1, PERIODIC_2 = 0x2, PERIODIC_3 = 0x3, WHITE_0 = 0x4, WHITE_1 = 0x5, WHITE_2 = 0x6, WHITE_3 = 0x7, NOISE_OFF = 0xf }

## Public Member Functions

| MD_SN76489 (bool clock)
| ~MD_SN76489 (void)
virtual void | begin (void)
Hardware control basics.
void | setVolume (uint8_t chan, uint8_t v)
void | setVolume (uint8_t v)
void | setFrequency (uint8_t chan, uint16_t freq)
void | setNoise (noiseType_t noise)
Methods for notes and tones.
void | tone (uint8_t chan, uint16_t freq, uint8_t volume, uint16_t duration=0)
void | note (uint8_t chan, uint16_t freq, uint8_t volume, uint16_t duration=0)
void | noise (noiseType_t noise, uint8_t volume, uint16_t duration=0)
bool | setADSR (uint8_t chan, adsrEnvelope_t *padsr)
bool | setADSR (adsrEnvelope_t *padsr)
bool | isIdle (uint8_t chan)
void | play (void)

## Static Public Attributes

static const uint8_t | MAX_CHANNELS = 4
| Number of available sound channels.
static const uint8_t | NOISE_CHANNEL = 3
| The channel for periodic/white noise.
static const uint8_t | VOL_OFF = 0x0
| Convenience constant for volume off.
static const uint8_t | VOL_MAX = 0xf
| Convenience constant for volume on.

## Protected Member Functions

virtual void | send (uint8_t data)

## Protected Attributes

const uint8_t | DATA_BITS = 8
| Number of bits in the byte (for loops)

## Detailed Description

Base class for the MD_SN76489 library

## Member Enumeration Documentation

## ◆ noiseType_t

enum MD_SN76489::noiseType_t

Noise type enumerated definitions The NOISE_CHANNEL can produce either white or periodic noise. This enumerated type is used to define the setting for the channel.

Enumerator
PERIODIC_0 | Periodic noise, output/512.
PERIODIC_1 | Periodic noise, output/1024.
PERIODIC_2 | Periodic noise, output/2048.
PERIODIC_3 | Periodic noise, output/(Channel 2 freq)
WHITE_0 | White noise, output/512.
WHITE_1 | White noise, output/1024.
WHITE_2 | White noise, output/2048.
WHITE_3 | White noise, output/(Channel 2 freq)
NOISE_OFF | Special indicator to turn noise off if not timed.

## Constructor & Destructor Documentation

## ◆ MD_SN76489()

MD_SN76489::MD_SN76489 | ( | bool | clock | ) |

Class Constructor.

Instantiate a new instance of the class. The parameters passed are used to connect the software to the hardware. Multiple instances may co-exist.

Parameters

  clock | if true the 4MHz clock signal is generated using MCU timers (hardware dependency)

## ◆ ~MD_SN76489()

MD_SN76489::~MD_SN76489 | ( | void |  | ) |

Class Destructor.

Does the necessary to clean up once the object is no longer required.

## Member Function Documentation

## ◆ begin()

void MD_SN76489::begin | ( | void |  | ) |

virtual

Initialize the object.

Initialize the object data. This needs to be called during setup() to initialize new data for the class that cannot be done during the object creation.

User code will invoke begin() method from the derived class to initialise, which will in turn call this base class method.

In this base class method, the 4MHz clock signal is started, the ADSR envelope initialzed and sound volume turned off.

Reimplemented in MD_SN76489_SPI, and MD_SN76489_Direct.

## ◆ isIdle()

bool MD_SN76489::isIdle | ( | uint8_t | chan | ) |

Return the idle state of a channel.

Used to check if a channel is currently idle (ie, not playing a note).

Parameters

  chan | channel to check [0..MAX_CHANNELS-1]

Returns
  true if the channel is idle, false otherwise.

## ◆ noise()

void MD_SN76489::noise | ( | noiseType_t | noise,
|  | uint8_t | volume,
|  | uint16_t | duration = `0`
| ) |  |

Play a noise using ADSR.

Output a noise as specified on NOISE_CHANNEL, using the ADSR envelope curently defined for the channel. If another noise is playing on NOISE_CHANNEL it will be immediately replaced by the new noise.

If specified, the duration will cause an automatic note off event when the total time has expired. Note that the duration is measured from the start of the A to the end of the R phases of the playing envelope.

This method is ONLY supported by the NOISE_CHANNEL.

Parameters

  noise | one of the valid noise types in noiseType_t.
  volume | volume to play, default to VOL_MAX, in the range [0..VOL_MAX]
  duration | length of time in ms for the whole note to last.

## ◆ note()

void MD_SN76489::note | ( | uint8_t | chan,
|  | uint16_t | freq,
|  | uint8_t | volume,
|  | uint16_t | duration = `0`
| ) |  |

Play a note on using ADSR.

Output a sound with frequency freq on the specified channel using the ADSR envelope curently defined for the channel. If another note or tone is playing on the channel this will be immediately replaced by the new note.

If specified, the duration will cause an automatic note off event when the total time has expired. Note that the duration is measured from the start of the A to the end of the R phases of the playing envelope.

If a freq is 0, the note is turned off. If duration is 0, the note remains in the sustain phase until it is turned off.

This method is not supported by the NOISE_CHANNEL.

See also
  tone()

Parameters

  chan | channel number on which to play this note [0..MAX_CHANNELS-2].
  freq | frequency to play.
  volume | volume to play, default to VOL_MAX, in the range [0..VOL_MAX]
  duration | length of time in ms for the whole note to last.

## ◆ play()

void MD_SN76489::play | ( | void |  | ) |

Play the music machine.

Runs the ADSR finite state machine for all channels. This should be called from the main loop() as frequently as possible to allow the library to execute the note required timing for the note envelopes.

## ◆ send()

void MD_SN76489::send | ( | uint8_t | data | ) |

protected virtual

Send a byte to the SN76489IC.

Sending a byte to the SN76489 IC depends on how it is connected to the MCU. This base class method must be replaced by derived class methods that implements the appropriate data transfer method.

Parameters

  data | the data byte to transmit

Reimplemented in MD_SN76489_SPI, and MD_SN76489_Direct.

## ◆ setADSR() [1/2]

bool MD_SN76489::setADSR | ( | adsrEnvelope_t * | padsr | ) |

Set the same ADSR envelope for all channels.

Sets the same ADSR envelope for all channels by passing in an application copy of the envelope definition. The application may change values in the envelope and they will be immediately reflected in the sound.

See also
  setADSR()

Parameters

  padsr | pointer to the ADSR structure to be used.

Returns
  true if all changes were possible, false otherwise.

## ◆ setADSR() [2/2]

bool MD_SN76489::setADSR | ( | uint8_t | chan,
|  | adsrEnvelope_t * | padsr
| ) |  |

Set the ADSR envelope for a channel.

Sets the ADSR envelope for a channel. The envelope is defined in a structure of type adsrEnvelope_t. The envelope definition is not copied and must remain in scope while it is in use. The application may change elements of the envelope defintion and they will be immediately reflected in sound output.

A null pointer changes the ADSR definition back to the library default. The ADSR profile cannot be changed while it is in use (ie, channel not idle).

See also
  isIdle()

Parameters

  chan | channel number on which to play this note [0..MAX_CHANNELS-1].
  padsr | pointer to the ADSR structure to be used.

Returns
  true if the change was possible, false otherwise.

## ◆ setFrequency()

void MD_SN76489::setFrequency | ( | uint8_t | chan,
|  | uint16_t | freq
| ) |  |

Set the frequency for a channel.

Set the frequency output for a channel to be the value specified.

This method is not supported by the NOISE_CHANNEL.

See also
  setNoise()

Parameters

  chan | channel number on which to play this note [0..MAX_CHANNELS-2].
  freq | frequency to set for the specified channel.

## ◆ setNoise()

void MD_SN76489::setNoise | ( | noiseType_t | noise | ) |

Set the noise channel parameters.

Set the noise parameters for NOISE_CHANNEL. This channel cannot play notes. Noise may be one of the noiseType_t types.

New settings will immediately replace old settings for for NOISE_CHANNEL.

Parameters

  noise | one of the valid noise types in noiseType_t.

## ◆ setVolume() [1/2]

void MD_SN76489::setVolume | ( | uint8_t | chan,
|  | uint8_t | v
| ) |  |

Set the volume for a channel.

Set the volume for a channel to be the value specified. Valid values are all the values in the range [VOL_MIN..VOL_MAX].

Parameters

  chan | channel number on which to play this note [0..MAX_CHANNELS-1].
  v | volume to set for the specificed channel in range [VOL_MIN..VOL_MAX].

## ◆ setVolume() [2/2]

void MD_SN76489::setVolume | ( | uint8_t | v | ) |

Set the volume for all channels.

Set the volume for all channels to be the speified value. Valid values are all the values in the range [VOL_MIN..VOL_MAX].

Parameters

  v | volume to set for all channels in range [VOL_MIN..VOL_MAX].

## ◆ tone()

void MD_SN76489::tone | ( | uint8_t | chan,
|  | uint16_t | freq,
|  | uint8_t | volume,
|  | uint16_t | duration = `0`
| ) |  |

Play a tone without ADSR.

Output a sound with frequency freq on the specified channel. If another note or tone is playing on the channel this will be immediately replaced by the new tone.

If specified, the duration will cause an automatic note off event when the total time has expired.

If a freq is 0, the tone is turned off. If duration is 0, the tone remains in the sustain phase until it is turned off.

This method is not supported by the NOISE_CHANNEL.

See also
  note()

Parameters

  chan | channel number on which to play this note [0..MAX_CHANNELS-1].
  freq | frequency to play.
  volume | volume to play, default to VOL_MAX, in the range [0..VOL_MAX]
  duration | length of time in ms for the whole note to last.

The documentation for this class was generated from the following files:

- src/MD_SN76489.h
- src/MD_SN76489.cpp