Simple starter skeleton for asteroids video game.

Dependencies:   PinDetect

Sounds/Speaker.h

Committer:
jhurley31
Date:
2019-02-23
Revision:
1:a6872783beca
Parent:
Speaker.h@ 0:0c450cb95a1e
Child:
2:30020ddfccf6

File content as of revision 1:a6872783beca:

#include "mbed.h"
#include "buzzy_siren.h"
#include "buzzy_begin.h"
#include "buzzy_chomp.h"
#include "buzzy_eatghost.h"
#include "buzzy_death.h"
#include "buzzy_eatfruit.h"


class Speaker : public PwmOut
{
public:
    enum BUZZY_Sounds {BEGIN, SIREN, CHOMP, EAT_GHOST, DEATH, EAT_FRUIT};
    
    Speaker(PinName nPin):PwmOut(nPin)
    {
       m_ulAudioArrayIndex = 0;
       m_enActiveAudioArray = BEGIN;    
       pAudioArray = NULL;
    }
    
    void PlayNextValue()
    {
        if (pAudioArray != NULL)
        {
            write(pAudioArray[ GetNextValue() ]);
        }       
    }
    
    void SwitchAudioFile ( const BUZZY_Sounds &newSound)
    {
        switch (newSound)
        {           
            case BEGIN:
                pAudioArray = &data_BEGIN[0];
                break;   
            case CHOMP:
                 pAudioArray = &data_CHOMP[0];
                break;
            case EAT_GHOST:
                pAudioArray = &data_GHOST[0];
                break;
            case DEATH:
                pAudioArray = &data_DEATH[0];
                break;
            case EAT_FRUIT:
                pAudioArray = &data_EAT_FRUIT[0];
                break;              
            case SIREN:
            default:
                pAudioArray = &data_SIREN[0];
                break; 
        }
                             
    }
    
    unsigned int GetNextValue()
    {
        m_ulAudioArrayIndex++;
        switch (m_enActiveAudioArray)
        {           
            case BEGIN:
                m_ulAudioArrayIndex %= NUM_BEGIN_ELEMENTS;
                break;   
            case CHOMP:
                 m_ulAudioArrayIndex %= NUM_CHOMP_ELEMENTS;
                break;
            case EAT_GHOST:
                 m_ulAudioArrayIndex %= NUM_GHOST_ELEMENTS;
                break;
            case DEATH:
                 m_ulAudioArrayIndex %= NUM_DEATH_ELEMENTS;
                break;
            case EAT_FRUIT:
                 m_ulAudioArrayIndex %= NUM_EAT_FRUIT_ELEMENTS;
                break;              
            case SIREN:
            default:
                 m_ulAudioArrayIndex %= NUM_SIREN_ELEMENTS;
                break;          
        }
        return m_ulAudioArrayIndex;
    }
private:
    unsigned long m_ulAudioArrayIndex;
    BUZZY_Sounds m_enActiveAudioArray;
    float *pAudioArray;
    
};