![]() |
MD_SN76489 Library
1.1
Library for SN76489 sound generator
|
#include <MD_SN76489.h>
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) | |
Base class for the MD_SN76489 library
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.
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.
clock | if true the 4MHz clock signal is generated using MCU timers (hardware dependency) |
MD_SN76489::~MD_SN76489 | ( | void | ) |
Class Destructor.
Does the necessary to clean up once the object is no longer required.
|
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.
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).
chan | channel to check [0..MAX_CHANNELS-1] |
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.
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. |
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.
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. |
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.
|
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.
data | the data byte to transmit |
Reimplemented in MD_SN76489_SPI, and MD_SN76489_Direct.
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.
padsr | pointer to the ADSR structure to be used. |
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).
chan | channel number on which to play this note [0..MAX_CHANNELS-1]. |
padsr | pointer to the ADSR structure to be used. |
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.
chan | channel number on which to play this note [0..MAX_CHANNELS-2]. |
freq | frequency to set for the specified channel. |
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.
noise | one of the valid noise types in noiseType_t. |
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].
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]. |
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].
v | volume to set for all channels in range [VOL_MIN..VOL_MAX]. |
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.
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. |
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).
data | the 8 bit data value to write to the device. |