MD_SN76489 Library  1.1
Library for SN76489 sound generator
MD_SN76489 Class Reference

#include <MD_SN76489.h>

Inheritance diagram for MD_SN76489:

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)
 
void write (uint8_t data)
 

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

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
clockif 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
chanchannel 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
noiseone of the valid noise types in noiseType_t.
volumevolume to play, default to VOL_MAX, in the range [0..VOL_MAX]
durationlength 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
chanchannel number on which to play this note [0..MAX_CHANNELS-2].
freqfrequency to play.
volumevolume to play, default to VOL_MAX, in the range [0..VOL_MAX]
durationlength 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)
protectedvirtual

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
datathe 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
padsrpointer 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
chanchannel number on which to play this note [0..MAX_CHANNELS-1].
padsrpointer 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
chanchannel number on which to play this note [0..MAX_CHANNELS-2].
freqfrequency 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
noiseone 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
chanchannel number on which to play this note [0..MAX_CHANNELS-1].
vvolume 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
vvolume 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
chanchannel number on which to play this note [0..MAX_CHANNELS-1].
freqfrequency to play.
volumevolume to play, default to VOL_MAX, in the range [0..VOL_MAX]
durationlength of time in ms for the whole note to last.

◆ write()

void MD_SN76489::write ( uint8_t  data)

Write a byte directly to the device

This method should be used with caution, as it bypasses all the checks and buffering built into the library. It is provided to support applications that are a collection of register setting to be written to hardware at set time intervals (eg, VGM files).

Parameters
datathe 8 bit data value to write to the device.

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