#include <XT_DAC_Audio.h>

#include "alarm.h"
#include "Arduino.h"
#include "sound.h"
#include "soc/rtc_io_reg.h"
#include "alarmSound.h"

// XT_Wav_Class SirenSound(sampleSound); 
XT_Wav_Class SirenSound(alarmSound); 
XT_DAC_Audio_Class DacAudio(25, 0);
uint32_t DemoCounter=0;  

class Siren{
private:
  int sirenPin = 25;
  AlarmStatus* status;
  float sinVal;
  int toneVal;

  

  void playSound(){
    int max = 180;
    Serial.println("Sound on " + String(sirenPin));

    if(status->silent)
      max = 2;
    for(int x = 0; x < max ; x++){
      //convert angle of sinusoidal to radian measure
      sinVal = (sin(x*(3.1412/180)));
      //generate sound of different frequencies by sinusoidal value
      toneVal = 2000+(int(sinVal*1000));
      //Set a frequency for Pin-out 8

      // ledcAttach(sirenPin, toneVal, 3);
      tone(sirenPin, toneVal, 3);
      // buzzer->tone(toneVal, 3);
      // TimerFreeTone(TONE_PIN, toneVal, 2, 5);
      delay(2);
    }
    noTone(sirenPin);
    // buzzer->noTone();
    // ledcWrite(sirenPin,0); // No sound 
    // ledcDetach(sirenPin);  
  }

public:
  Siren(AlarmStatus* statusRef){
    status=statusRef;
  }
    
  Siren(AlarmStatus* statusRef, int pin){
    status=statusRef;
    sirenPin=pin;
  }


  void Init(){
    // pinMode(sirenPin, OUTPUT);
    // DacAudio = XT_DAC_Audio_Class(sirenPin, 0);
  }
  
  

  void SoundSiren(){
    if(status->muted)
      return;

    // playSound();
    // delay(500);

    DacAudio.FillBuffer();                // Fill the sound buffer with data
    if(SirenSound.Playing==false)       // if not playing,
      DacAudio.Play(&SirenSound);       // play it, this will cause it to repeat and repeat...
    Serial.println(DemoCounter++);      
  }
};