ssss

Audio.cpp

Committer:
irsanjul
Date:
2017-08-07
Revision:
0:e9c48701a273

File content as of revision 0:e9c48701a273:

#include "Audio.h"

extern Serial dbg;

double nada[10] = {0, 247, 262, 294, 330, 349, 392, 440, 494, 523};
//double nadas[16] = { 6, 6, 0, 6,
//                     0, 4, 6, 0,
//                     8, 0, 0, 0,
//                     1, 0, 0, 0 };
//                     
unsigned int nadas[8] = { 1, 2, 3, 4,
                     5, 6, 7, 8 };
                     
Audio::Audio(PinName buzzer): _out(buzzer)
{
}
    
void Audio::SetVolume(double volume)
{
    vol = volume;
}   

void Audio::SetDuration(unsigned int milisec)
{
    ms = milisec;
}
        
void Audio::PlayNote(unsigned int angka)
{
    if(angka > 8) angka -= 8;
    
    double Nada = nada[angka];
    PlayNote(Nada*4.24, ms, vol);
}

void Audio::mute()
{
    DigitalOut *out = new DigitalOut(_out);
    out->write(0);
    delete out;
}

void Audio::PlayNote(double frequency, double duration, double volume)
{
    PwmOut *out = new PwmOut(_out);
    out->period(1.0/frequency);
    out->write(volume/2.0);
    wait_ms(duration);
    out->write(0.0);
    delete out;
}

// -----------------------------------------------------------------------------
MyAudio::MyAudio(PinName buzz) : Audio (buzz)
{}

void MyAudio::play(const int &code)
{
    switch(code)
    {
        case 0 :
            mute();
            break;
        case 1 :
            playing(6);
            set();
            playing(1);
            set();
            mute();
            break;
        case 2 :
            
//            break;
        case 3 :
            
//            break;
        case 4 :
            set();
            playing(1);
            break;
        case 5 :
            set();
            for(int i=0; i<3; i++)playing(4);
            break;
        default:
            set(100, 0.5);
            for(int j=0; j<8; j++)
            {
                playing(nadas[j]);
            }
            set();
            mute();
            break;
    }
}

void MyAudio::set(double dur, double vol)
{
    Audio::SetVolume(vol);
    Audio::SetDuration(dur);
}

void MyAudio::playing(unsigned int note)
{
    Audio::PlayNote(note);
}

void MyAudio::mute()
{
    Audio::mute();
}