melder_audio.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #ifndef _melder_audio_h_
  2. #define _melder_audio_h_
  3. /* melder_audio.h
  4. *
  5. * Copyright (C) 1992-2018 Paul Boersma
  6. *
  7. * This code is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * This code is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. void MelderAudio_setInputSoundSystem (enum kMelder_inputSoundSystem inputSoundSystem);
  21. enum kMelder_inputSoundSystem MelderAudio_getInputSoundSystem ();
  22. void MelderAudio_setOutputSoundSystem (enum kMelder_outputSoundSystem outputSoundSystem);
  23. enum kMelder_outputSoundSystem MelderAudio_getOutputSoundSystem ();
  24. #if defined (_WIN32)
  25. #define kMelderAudio_outputSilenceBefore_DEFAULT 0.0
  26. #define kMelderAudio_outputSilenceAfter_DEFAULT 0.1
  27. // in order to get rid of the click on some cards
  28. #elif defined (macintosh)
  29. #define kMelderAudio_outputSilenceBefore_DEFAULT 0.0
  30. // in order to switch off the BOING caused by the automatic gain control
  31. #define kMelderAudio_outputSilenceAfter_DEFAULT 0.0
  32. // in order to reduce the BOING caused by the automatic gain control when the user replays immediately after a sound has finished
  33. #elif defined (linux)
  34. #define kMelderAudio_outputSilenceBefore_DEFAULT 0.0
  35. #define kMelderAudio_outputSilenceAfter_DEFAULT 0.1
  36. // in order to get rid of double playing of a sounding buffer (?)
  37. #else
  38. #define kMelderAudio_outputSilenceBefore_DEFAULT 0.0
  39. #define kMelderAudio_outputSilenceAfter_DEFAULT 0.0
  40. #endif
  41. void MelderAudio_setOutputSilenceBefore (double silenceBefore);
  42. double MelderAudio_getOutputSilenceBefore ();
  43. void MelderAudio_setOutputSilenceAfter (double silenceAfter);
  44. double MelderAudio_getOutputSilenceAfter ();
  45. void MelderAudio_setUseInternalSpeaker (bool useInternalSpeaker); // for HP-UX and Sun
  46. bool MelderAudio_getUseInternalSpeaker ();
  47. integer MelderAudio_getOutputBestSampleRate (integer fsamp);
  48. extern bool MelderAudio_isPlaying;
  49. void MelderAudio_play16 (int16 *buffer, integer sampleRate, integer numberOfSamples, int numberOfChannels,
  50. bool (*playCallback) (void *playClosure, integer numberOfSamplesPlayed), // return true to continue, false to stop
  51. void *playClosure);
  52. bool MelderAudio_stopPlaying (bool isExplicit); // returns true if sound was playing
  53. #define MelderAudio_IMPLICIT false
  54. #define MelderAudio_EXPLICIT true
  55. integer MelderAudio_getSamplesPlayed ();
  56. bool MelderAudio_stopWasExplicit ();
  57. void Melder_audio_prefs (); // in init file
  58. #pragma mark - ASYNCHRONICITY
  59. void MelderAudio_setOutputMaximumAsynchronicity (enum kMelder_asynchronicityLevel maximumAsynchronicity);
  60. enum kMelder_asynchronicityLevel MelderAudio_getOutputMaximumAsynchronicity ();
  61. class autoMelderAudioSaveMaximumAsynchronicity {
  62. bool _disowned;
  63. enum kMelder_asynchronicityLevel _savedAsynchronicity;
  64. public:
  65. autoMelderAudioSaveMaximumAsynchronicity () {
  66. our _savedAsynchronicity = MelderAudio_getOutputMaximumAsynchronicity ();
  67. trace (U"value was ", (int) our _savedAsynchronicity);
  68. our _disowned = false;
  69. }
  70. ~autoMelderAudioSaveMaximumAsynchronicity () {
  71. MelderAudio_setOutputMaximumAsynchronicity (our _savedAsynchronicity);
  72. trace (U"value set to ", (int) our _savedAsynchronicity);
  73. }
  74. /*
  75. Disable copying.
  76. */
  77. autoMelderAudioSaveMaximumAsynchronicity (const autoMelderAudioSaveMaximumAsynchronicity&) = delete; // disable copy constructor
  78. autoMelderAudioSaveMaximumAsynchronicity& operator= (const autoMelderAudioSaveMaximumAsynchronicity&) = delete; // disable copy assignment
  79. /*
  80. Enable moving.
  81. */
  82. autoMelderAudioSaveMaximumAsynchronicity (autoMelderAudioSaveMaximumAsynchronicity&& other) noexcept { // enable move constructor
  83. our _disowned = other._disowned;
  84. our _savedAsynchronicity = other._savedAsynchronicity;
  85. other._disowned = true;
  86. }
  87. autoMelderAudioSaveMaximumAsynchronicity& operator= (autoMelderAudioSaveMaximumAsynchronicity&& other) noexcept { // enable move assignment
  88. if (& other != this) {
  89. our _disowned = other._disowned;
  90. our _savedAsynchronicity = other._savedAsynchronicity;
  91. other._disowned = true; // needed only if you insist on keeping the source in a valid state
  92. }
  93. return *this;
  94. }
  95. autoMelderAudioSaveMaximumAsynchronicity&& move () noexcept { return static_cast <autoMelderAudioSaveMaximumAsynchronicity&&> (*this); }
  96. void releaseToAmbiguousOwner () {
  97. our _disowned = true;
  98. }
  99. };
  100. /*
  101. True if specified by the "asynchronous" directive in a script.
  102. TODO: change to inline variable once C++17 is implemented completely on all platforms.
  103. */
  104. extern bool Melder_asynchronous;
  105. class autoMelderAsynchronous {
  106. bool _disowned;
  107. bool _savedAsynchronicity;
  108. public:
  109. autoMelderAsynchronous () {
  110. our _savedAsynchronicity = Melder_asynchronous;
  111. Melder_asynchronous = true;
  112. our _disowned = false;
  113. }
  114. ~autoMelderAsynchronous () {
  115. if (! _disowned) {
  116. Melder_asynchronous = _savedAsynchronicity;
  117. }
  118. }
  119. /*
  120. Disable copying.
  121. */
  122. autoMelderAsynchronous (const autoMelderAsynchronous&) = delete; // disable copy constructor
  123. autoMelderAsynchronous& operator= (const autoMelderAsynchronous&) = delete; // disable copy assignment
  124. /*
  125. Enable moving.
  126. */
  127. autoMelderAsynchronous (autoMelderAsynchronous&& other) noexcept { // enable move constructor
  128. our _disowned = other._disowned;
  129. our _savedAsynchronicity = other._savedAsynchronicity;
  130. other._disowned = true;
  131. }
  132. autoMelderAsynchronous& operator= (autoMelderAsynchronous&& other) noexcept { // enable move assignment
  133. if (& other != this) {
  134. our _disowned = other._disowned;
  135. our _savedAsynchronicity = other._savedAsynchronicity;
  136. other._disowned = true; // needed only if you insist on keeping the source in a valid state
  137. }
  138. return *this;
  139. }
  140. autoMelderAsynchronous&& move () noexcept { return static_cast <autoMelderAsynchronous&&> (*this); }
  141. void releaseToAmbiguousOwner () {
  142. our _disowned = true;
  143. }
  144. };
  145. /* End of file melder_audio.h */
  146. #endif