audio_stream.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*************************************************************************/
  2. /* audio_stream.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "audio_stream.h"
  31. #include "core/os/os.h"
  32. #include "core/project_settings.h"
  33. //////////////////////////////
  34. void AudioStreamPlaybackResampled::_begin_resample() {
  35. //clear cubic interpolation history
  36. internal_buffer[0] = AudioFrame(0.0, 0.0);
  37. internal_buffer[1] = AudioFrame(0.0, 0.0);
  38. internal_buffer[2] = AudioFrame(0.0, 0.0);
  39. internal_buffer[3] = AudioFrame(0.0, 0.0);
  40. //mix buffer
  41. _mix_internal(internal_buffer + 4, INTERNAL_BUFFER_LEN);
  42. mix_offset = 0;
  43. }
  44. void AudioStreamPlaybackResampled::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
  45. float target_rate = AudioServer::get_singleton()->get_mix_rate();
  46. float global_rate_scale = AudioServer::get_singleton()->get_global_rate_scale();
  47. uint64_t mix_increment = uint64_t(((get_stream_sampling_rate() * p_rate_scale) / double(target_rate * global_rate_scale)) * double(FP_LEN));
  48. for (int i = 0; i < p_frames; i++) {
  49. uint32_t idx = CUBIC_INTERP_HISTORY + uint32_t(mix_offset >> FP_BITS);
  50. //standard cubic interpolation (great quality/performance ratio)
  51. //this used to be moved to a LUT for greater performance, but nowadays CPU speed is generally faster than memory.
  52. float mu = (mix_offset & FP_MASK) / float(FP_LEN);
  53. AudioFrame y0 = internal_buffer[idx - 3];
  54. AudioFrame y1 = internal_buffer[idx - 2];
  55. AudioFrame y2 = internal_buffer[idx - 1];
  56. AudioFrame y3 = internal_buffer[idx - 0];
  57. float mu2 = mu * mu;
  58. AudioFrame a0 = 3 * y1 - 3 * y2 + y3 - y0;
  59. AudioFrame a1 = 2 * y0 - 5 * y1 + 4 * y2 - y3;
  60. AudioFrame a2 = y2 - y0;
  61. AudioFrame a3 = 2 * y1;
  62. p_buffer[i] = (a0 * mu * mu2 + a1 * mu2 + a2 * mu + a3) / 2;
  63. mix_offset += mix_increment;
  64. while ((mix_offset >> FP_BITS) >= INTERNAL_BUFFER_LEN) {
  65. internal_buffer[0] = internal_buffer[INTERNAL_BUFFER_LEN + 0];
  66. internal_buffer[1] = internal_buffer[INTERNAL_BUFFER_LEN + 1];
  67. internal_buffer[2] = internal_buffer[INTERNAL_BUFFER_LEN + 2];
  68. internal_buffer[3] = internal_buffer[INTERNAL_BUFFER_LEN + 3];
  69. if (is_playing()) {
  70. _mix_internal(internal_buffer + 4, INTERNAL_BUFFER_LEN);
  71. } else {
  72. //fill with silence, not playing
  73. for (int j = 0; j < INTERNAL_BUFFER_LEN; ++j) {
  74. internal_buffer[j + 4] = AudioFrame(0, 0);
  75. }
  76. }
  77. mix_offset -= (INTERNAL_BUFFER_LEN << FP_BITS);
  78. }
  79. }
  80. }
  81. ////////////////////////////////
  82. void AudioStream::_bind_methods() {
  83. ClassDB::bind_method(D_METHOD("get_length"), &AudioStream::get_length);
  84. }
  85. ////////////////////////////////
  86. Ref<AudioStreamPlayback> AudioStreamMicrophone::instance_playback() {
  87. Ref<AudioStreamPlaybackMicrophone> playback;
  88. playback.instance();
  89. playbacks.insert(playback.ptr());
  90. playback->microphone = Ref<AudioStreamMicrophone>((AudioStreamMicrophone *)this);
  91. playback->active = false;
  92. return playback;
  93. }
  94. String AudioStreamMicrophone::get_stream_name() const {
  95. //if (audio_stream.is_valid()) {
  96. //return "Random: " + audio_stream->get_name();
  97. //}
  98. return "Microphone";
  99. }
  100. float AudioStreamMicrophone::get_length() const {
  101. return 0;
  102. }
  103. void AudioStreamMicrophone::_bind_methods() {
  104. }
  105. AudioStreamMicrophone::AudioStreamMicrophone() {
  106. }
  107. void AudioStreamPlaybackMicrophone::_mix_internal(AudioFrame *p_buffer, int p_frames) {
  108. AudioDriver::get_singleton()->lock();
  109. Vector<int32_t> buf = AudioDriver::get_singleton()->get_input_buffer();
  110. unsigned int input_size = AudioDriver::get_singleton()->get_input_size();
  111. int mix_rate = AudioDriver::get_singleton()->get_mix_rate();
  112. unsigned int playback_delay = MIN(((50 * mix_rate) / 1000) * 2, buf.size() >> 1);
  113. #ifdef DEBUG_ENABLED
  114. unsigned int input_position = AudioDriver::get_singleton()->get_input_position();
  115. #endif
  116. if (playback_delay > input_size) {
  117. for (int i = 0; i < p_frames; i++) {
  118. p_buffer[i] = AudioFrame(0.0f, 0.0f);
  119. }
  120. input_ofs = 0;
  121. } else {
  122. for (int i = 0; i < p_frames; i++) {
  123. if (input_size > input_ofs && (int)input_ofs < buf.size()) {
  124. float l = (buf[input_ofs++] >> 16) / 32768.f;
  125. if ((int)input_ofs >= buf.size()) {
  126. input_ofs = 0;
  127. }
  128. float r = (buf[input_ofs++] >> 16) / 32768.f;
  129. if ((int)input_ofs >= buf.size()) {
  130. input_ofs = 0;
  131. }
  132. p_buffer[i] = AudioFrame(l, r);
  133. } else {
  134. p_buffer[i] = AudioFrame(0.0f, 0.0f);
  135. }
  136. }
  137. }
  138. #ifdef DEBUG_ENABLED
  139. if (input_ofs > input_position && (int)(input_ofs - input_position) < (p_frames * 2)) {
  140. print_verbose(String(get_class_name()) + " buffer underrun: input_position=" + itos(input_position) + " input_ofs=" + itos(input_ofs) + " input_size=" + itos(input_size));
  141. }
  142. #endif
  143. AudioDriver::get_singleton()->unlock();
  144. }
  145. void AudioStreamPlaybackMicrophone::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
  146. AudioStreamPlaybackResampled::mix(p_buffer, p_rate_scale, p_frames);
  147. }
  148. float AudioStreamPlaybackMicrophone::get_stream_sampling_rate() {
  149. return AudioDriver::get_singleton()->get_mix_rate();
  150. }
  151. void AudioStreamPlaybackMicrophone::start(float p_from_pos) {
  152. if (active) {
  153. return;
  154. }
  155. if (!GLOBAL_GET("audio/enable_audio_input")) {
  156. WARN_PRINT("Need to enable Project settings > Audio > Enable Audio Input option to use capturing.");
  157. return;
  158. }
  159. input_ofs = 0;
  160. if (AudioDriver::get_singleton()->capture_start() == OK) {
  161. active = true;
  162. _begin_resample();
  163. }
  164. }
  165. void AudioStreamPlaybackMicrophone::stop() {
  166. if (active) {
  167. AudioDriver::get_singleton()->capture_stop();
  168. active = false;
  169. }
  170. }
  171. bool AudioStreamPlaybackMicrophone::is_playing() const {
  172. return active;
  173. }
  174. int AudioStreamPlaybackMicrophone::get_loop_count() const {
  175. return 0;
  176. }
  177. float AudioStreamPlaybackMicrophone::get_playback_position() const {
  178. return 0;
  179. }
  180. void AudioStreamPlaybackMicrophone::seek(float p_time) {
  181. // Can't seek a microphone input
  182. }
  183. AudioStreamPlaybackMicrophone::~AudioStreamPlaybackMicrophone() {
  184. microphone->playbacks.erase(this);
  185. stop();
  186. }
  187. AudioStreamPlaybackMicrophone::AudioStreamPlaybackMicrophone() {
  188. }
  189. ////////////////////////////////
  190. void AudioStreamRandomPitch::set_audio_stream(const Ref<AudioStream> &p_audio_stream) {
  191. audio_stream = p_audio_stream;
  192. if (audio_stream.is_valid()) {
  193. for (Set<AudioStreamPlaybackRandomPitch *>::Element *E = playbacks.front(); E; E = E->next()) {
  194. E->get()->playback = audio_stream->instance_playback();
  195. }
  196. }
  197. }
  198. Ref<AudioStream> AudioStreamRandomPitch::get_audio_stream() const {
  199. return audio_stream;
  200. }
  201. void AudioStreamRandomPitch::set_random_pitch(float p_pitch) {
  202. if (p_pitch < 1) {
  203. p_pitch = 1;
  204. }
  205. random_pitch = p_pitch;
  206. }
  207. float AudioStreamRandomPitch::get_random_pitch() const {
  208. return random_pitch;
  209. }
  210. Ref<AudioStreamPlayback> AudioStreamRandomPitch::instance_playback() {
  211. Ref<AudioStreamPlaybackRandomPitch> playback;
  212. playback.instance();
  213. if (audio_stream.is_valid()) {
  214. playback->playback = audio_stream->instance_playback();
  215. }
  216. playbacks.insert(playback.ptr());
  217. playback->random_pitch = Ref<AudioStreamRandomPitch>((AudioStreamRandomPitch *)this);
  218. return playback;
  219. }
  220. String AudioStreamRandomPitch::get_stream_name() const {
  221. if (audio_stream.is_valid()) {
  222. return "Random: " + audio_stream->get_name();
  223. }
  224. return "RandomPitch";
  225. }
  226. float AudioStreamRandomPitch::get_length() const {
  227. if (audio_stream.is_valid()) {
  228. return audio_stream->get_length();
  229. }
  230. return 0;
  231. }
  232. void AudioStreamRandomPitch::_bind_methods() {
  233. ClassDB::bind_method(D_METHOD("set_audio_stream", "stream"), &AudioStreamRandomPitch::set_audio_stream);
  234. ClassDB::bind_method(D_METHOD("get_audio_stream"), &AudioStreamRandomPitch::get_audio_stream);
  235. ClassDB::bind_method(D_METHOD("set_random_pitch", "scale"), &AudioStreamRandomPitch::set_random_pitch);
  236. ClassDB::bind_method(D_METHOD("get_random_pitch"), &AudioStreamRandomPitch::get_random_pitch);
  237. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "audio_stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"), "set_audio_stream", "get_audio_stream");
  238. ADD_PROPERTY(PropertyInfo(Variant::REAL, "random_pitch", PROPERTY_HINT_RANGE, "1,16,0.01"), "set_random_pitch", "get_random_pitch");
  239. }
  240. AudioStreamRandomPitch::AudioStreamRandomPitch() {
  241. random_pitch = 1.1;
  242. }
  243. void AudioStreamPlaybackRandomPitch::start(float p_from_pos) {
  244. playing = playback;
  245. float range_from = 1.0 / random_pitch->random_pitch;
  246. float range_to = random_pitch->random_pitch;
  247. pitch_scale = range_from + Math::randf() * (range_to - range_from);
  248. if (playing.is_valid()) {
  249. playing->start(p_from_pos);
  250. }
  251. }
  252. void AudioStreamPlaybackRandomPitch::stop() {
  253. if (playing.is_valid()) {
  254. playing->stop();
  255. ;
  256. }
  257. }
  258. bool AudioStreamPlaybackRandomPitch::is_playing() const {
  259. if (playing.is_valid()) {
  260. return playing->is_playing();
  261. }
  262. return false;
  263. }
  264. int AudioStreamPlaybackRandomPitch::get_loop_count() const {
  265. if (playing.is_valid()) {
  266. return playing->get_loop_count();
  267. }
  268. return 0;
  269. }
  270. float AudioStreamPlaybackRandomPitch::get_playback_position() const {
  271. if (playing.is_valid()) {
  272. return playing->get_playback_position();
  273. }
  274. return 0;
  275. }
  276. void AudioStreamPlaybackRandomPitch::seek(float p_time) {
  277. if (playing.is_valid()) {
  278. playing->seek(p_time);
  279. }
  280. }
  281. void AudioStreamPlaybackRandomPitch::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
  282. if (playing.is_valid()) {
  283. playing->mix(p_buffer, p_rate_scale * pitch_scale, p_frames);
  284. } else {
  285. for (int i = 0; i < p_frames; i++) {
  286. p_buffer[i] = AudioFrame(0, 0);
  287. }
  288. }
  289. }
  290. AudioStreamPlaybackRandomPitch::~AudioStreamPlaybackRandomPitch() {
  291. random_pitch->playbacks.erase(this);
  292. }