ToneAlarm_SF.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #ifdef WITH_SITL_TONEALARM
  2. #include "ToneAlarm_SF.h"
  3. #include <stdio.h>
  4. #ifdef HAVE_SFML_AUDIO_H
  5. #include <SFML/Audio.h>
  6. #else
  7. #include <SFML/Audio.hpp>
  8. #endif
  9. #include "Synth.hpp"
  10. using namespace HALSITL;
  11. sf::SoundBuffer soundBuffer;
  12. sf::Sound demoSound;
  13. Synth::sEnvelope envelope;
  14. void ToneAlarm_SF::set_buzzer_tone(float frequency, float volume, float duration_ms)
  15. {
  16. if (frequency <= 0) {
  17. return;
  18. }
  19. Synth::sTone tone;
  20. tone.waveType = Synth::OSC_SQUARE;
  21. tone.dStartFrequency = frequency;
  22. tone.dEndFrequency = frequency;
  23. tone.dAmplitude = 1;
  24. envelope.dSustainTime = duration_ms/1000.0f;
  25. Synth::generate(&soundBuffer, envelope, tone, 20000, 44100);
  26. demoSound.setBuffer(soundBuffer);
  27. demoSound.play();
  28. }
  29. bool ToneAlarm_SF::init()
  30. {
  31. // LASER
  32. envelope.dAttackTime = 0.00001;
  33. envelope.dDecayTime = 0.00001;
  34. envelope.dSustainTime = 0.3;
  35. envelope.dReleaseTime = 0.00001;
  36. envelope.dStartAmplitude = 1.0;
  37. envelope.dSustainAmplitude = 1.0;
  38. return true;
  39. }
  40. #endif