audio_stream.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*************************************************************************/
  2. /* audio_stream.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. uint64_t mix_increment = uint64_t(((get_stream_sampling_rate() * p_rate_scale) / double(target_rate)) * double(FP_LEN));
  47. for (int i = 0; i < p_frames; i++) {
  48. uint32_t idx = CUBIC_INTERP_HISTORY + uint32_t(mix_offset >> FP_BITS);
  49. //standard cubic interpolation (great quality/performance ratio)
  50. //this used to be moved to a LUT for greater performance, but nowadays CPU speed is generally faster than memory.
  51. float mu = (mix_offset & FP_MASK) / float(FP_LEN);
  52. AudioFrame y0 = internal_buffer[idx - 3];
  53. AudioFrame y1 = internal_buffer[idx - 2];
  54. AudioFrame y2 = internal_buffer[idx - 1];
  55. AudioFrame y3 = internal_buffer[idx - 0];
  56. float mu2 = mu * mu;
  57. AudioFrame a0 = y3 - y2 - y0 + y1;
  58. AudioFrame a1 = y0 - y1 - a0;
  59. AudioFrame a2 = y2 - y0;
  60. AudioFrame a3 = y1;
  61. p_buffer[i] = (a0 * mu * mu2 + a1 * mu2 + a2 * mu + a3);
  62. mix_offset += mix_increment;
  63. while ((mix_offset >> FP_BITS) >= INTERNAL_BUFFER_LEN) {
  64. internal_buffer[0] = internal_buffer[INTERNAL_BUFFER_LEN + 0];
  65. internal_buffer[1] = internal_buffer[INTERNAL_BUFFER_LEN + 1];
  66. internal_buffer[2] = internal_buffer[INTERNAL_BUFFER_LEN + 2];
  67. internal_buffer[3] = internal_buffer[INTERNAL_BUFFER_LEN + 3];
  68. if (is_playing()) {
  69. _mix_internal(internal_buffer + 4, INTERNAL_BUFFER_LEN);
  70. } else {
  71. //fill with silence, not playing
  72. for (int j = 0; j < INTERNAL_BUFFER_LEN; ++j) {
  73. internal_buffer[j + 4] = AudioFrame(0, 0);
  74. }
  75. }
  76. mix_offset -= (INTERNAL_BUFFER_LEN << FP_BITS);
  77. }
  78. }
  79. }
  80. ////////////////////////////////
  81. void AudioStream::_bind_methods() {
  82. ClassDB::bind_method(D_METHOD("get_length"), &AudioStream::get_length);
  83. }
  84. ////////////////////////////////
  85. Ref<AudioStreamPlayback> AudioStreamMicrophone::instance_playback() {
  86. Ref<AudioStreamPlaybackMicrophone> playback;
  87. playback.instance();
  88. playbacks.insert(playback.ptr());
  89. playback->microphone = Ref<AudioStreamMicrophone>((AudioStreamMicrophone *)this);
  90. playback->active = false;
  91. return playback;
  92. }
  93. String AudioStreamMicrophone::get_stream_name() const {
  94. //if (audio_stream.is_valid()) {
  95. //return "Random: " + audio_stream->get_name();
  96. //}
  97. return "Microphone";
  98. }
  99. float AudioStreamMicrophone::get_length() const {
  100. return 0;
  101. }
  102. void AudioStreamMicrophone::_bind_methods() {
  103. }
  104. AudioStreamMicrophone::AudioStreamMicrophone() {
  105. }
  106. void AudioStreamPlaybackMicrophone::_mix_internal(AudioFrame *p_buffer, int p_frames) {
  107. AudioDriver::get_singleton()->lock();
  108. Vector<int32_t> buf = AudioDriver::get_singleton()->get_input_buffer();
  109. unsigned int input_size = AudioDriver::get_singleton()->get_input_size();
  110. int mix_rate = AudioDriver::get_singleton()->get_mix_rate();
  111. unsigned int playback_delay = MIN(((50 * mix_rate) / 1000) * 2, buf.size() >> 1);
  112. #ifdef DEBUG_ENABLED
  113. unsigned int input_position = AudioDriver::get_singleton()->get_input_position();
  114. #endif
  115. if (playback_delay > input_size) {
  116. for (int i = 0; i < p_frames; i++) {
  117. p_buffer[i] = AudioFrame(0.0f, 0.0f);
  118. }
  119. input_ofs = 0;
  120. } else {
  121. for (int i = 0; i < p_frames; i++) {
  122. if (input_size > input_ofs && (int)input_ofs < buf.size()) {
  123. float l = (buf[input_ofs++] >> 16) / 32768.f;
  124. if ((int)input_ofs >= buf.size()) {
  125. input_ofs = 0;
  126. }
  127. float r = (buf[input_ofs++] >> 16) / 32768.f;
  128. if ((int)input_ofs >= buf.size()) {
  129. input_ofs = 0;
  130. }
  131. p_buffer[i] = AudioFrame(l, r);
  132. } else {
  133. p_buffer[i] = AudioFrame(0.0f, 0.0f);
  134. }
  135. }
  136. }
  137. #ifdef DEBUG_ENABLED
  138. if (input_ofs > input_position && (int)(input_ofs - input_position) < (p_frames * 2)) {
  139. print_verbose(String(get_class_name()) + " buffer underrun: input_position=" + itos(input_position) + " input_ofs=" + itos(input_ofs) + " input_size=" + itos(input_size));
  140. }
  141. #endif
  142. AudioDriver::get_singleton()->unlock();
  143. }
  144. void AudioStreamPlaybackMicrophone::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
  145. AudioStreamPlaybackResampled::mix(p_buffer, p_rate_scale, p_frames);
  146. }
  147. float AudioStreamPlaybackMicrophone::get_stream_sampling_rate() {
  148. return AudioDriver::get_singleton()->get_mix_rate();
  149. }
  150. void AudioStreamPlaybackMicrophone::start(float p_from_pos) {
  151. if (active) {
  152. return;
  153. }
  154. if (!GLOBAL_GET("audio/enable_audio_input")) {
  155. WARN_PRINTS("Need to enable Project settings > Audio > Enable Audio Input option to use capturing.");
  156. return;
  157. }
  158. input_ofs = 0;
  159. if (AudioDriver::get_singleton()->capture_start() == OK) {
  160. active = true;
  161. _begin_resample();
  162. }
  163. }
  164. void AudioStreamPlaybackMicrophone::stop() {
  165. if (active) {
  166. AudioDriver::get_singleton()->capture_stop();
  167. active = false;
  168. }
  169. }
  170. bool AudioStreamPlaybackMicrophone::is_playing() const {
  171. return active;
  172. }
  173. int AudioStreamPlaybackMicrophone::get_loop_count() const {
  174. return 0;
  175. }
  176. float AudioStreamPlaybackMicrophone::get_playback_position() const {
  177. return 0;
  178. }
  179. void AudioStreamPlaybackMicrophone::seek(float p_time) {
  180. return; // Can't seek a microphone input
  181. }
  182. AudioStreamPlaybackMicrophone::~AudioStreamPlaybackMicrophone() {
  183. microphone->playbacks.erase(this);
  184. stop();
  185. }
  186. AudioStreamPlaybackMicrophone::AudioStreamPlaybackMicrophone() {
  187. }
  188. ////////////////////////////////
  189. void AudioStreamRandomPitch::set_audio_stream(const Ref<AudioStream> &p_audio_stream) {
  190. audio_stream = p_audio_stream;
  191. if (audio_stream.is_valid()) {
  192. for (Set<AudioStreamPlaybackRandomPitch *>::Element *E = playbacks.front(); E; E = E->next()) {
  193. E->get()->playback = audio_stream->instance_playback();
  194. }
  195. }
  196. }
  197. Ref<AudioStream> AudioStreamRandomPitch::get_audio_stream() const {
  198. return audio_stream;
  199. }
  200. void AudioStreamRandomPitch::set_random_pitch(float p_pitch) {
  201. if (p_pitch < 1)
  202. p_pitch = 1;
  203. random_pitch = p_pitch;
  204. }
  205. float AudioStreamRandomPitch::get_random_pitch() const {
  206. return random_pitch;
  207. }
  208. Ref<AudioStreamPlayback> AudioStreamRandomPitch::instance_playback() {
  209. Ref<AudioStreamPlaybackRandomPitch> playback;
  210. playback.instance();
  211. if (audio_stream.is_valid())
  212. playback->playback = audio_stream->instance_playback();
  213. playbacks.insert(playback.ptr());
  214. playback->random_pitch = Ref<AudioStreamRandomPitch>((AudioStreamRandomPitch *)this);
  215. return playback;
  216. }
  217. String AudioStreamRandomPitch::get_stream_name() const {
  218. if (audio_stream.is_valid()) {
  219. return "Random: " + audio_stream->get_name();
  220. }
  221. return "RandomPitch";
  222. }
  223. float AudioStreamRandomPitch::get_length() const {
  224. if (audio_stream.is_valid()) {
  225. return audio_stream->get_length();
  226. }
  227. return 0;
  228. }
  229. void AudioStreamRandomPitch::_bind_methods() {
  230. ClassDB::bind_method(D_METHOD("set_audio_stream", "stream"), &AudioStreamRandomPitch::set_audio_stream);
  231. ClassDB::bind_method(D_METHOD("get_audio_stream"), &AudioStreamRandomPitch::get_audio_stream);
  232. ClassDB::bind_method(D_METHOD("set_random_pitch", "scale"), &AudioStreamRandomPitch::set_random_pitch);
  233. ClassDB::bind_method(D_METHOD("get_random_pitch"), &AudioStreamRandomPitch::get_random_pitch);
  234. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "audio_stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"), "set_audio_stream", "get_audio_stream");
  235. ADD_PROPERTY(PropertyInfo(Variant::REAL, "random_pitch", PROPERTY_HINT_RANGE, "1,16,0.01"), "set_random_pitch", "get_random_pitch");
  236. }
  237. AudioStreamRandomPitch::AudioStreamRandomPitch() {
  238. random_pitch = 1.1;
  239. }
  240. void AudioStreamPlaybackRandomPitch::start(float p_from_pos) {
  241. playing = playback;
  242. float range_from = 1.0 / random_pitch->random_pitch;
  243. float range_to = random_pitch->random_pitch;
  244. pitch_scale = range_from + Math::randf() * (range_to - range_from);
  245. if (playing.is_valid()) {
  246. playing->start(p_from_pos);
  247. }
  248. }
  249. void AudioStreamPlaybackRandomPitch::stop() {
  250. if (playing.is_valid()) {
  251. playing->stop();
  252. ;
  253. }
  254. }
  255. bool AudioStreamPlaybackRandomPitch::is_playing() const {
  256. if (playing.is_valid()) {
  257. return playing->is_playing();
  258. }
  259. return false;
  260. }
  261. int AudioStreamPlaybackRandomPitch::get_loop_count() const {
  262. if (playing.is_valid()) {
  263. return playing->get_loop_count();
  264. }
  265. return 0;
  266. }
  267. float AudioStreamPlaybackRandomPitch::get_playback_position() const {
  268. if (playing.is_valid()) {
  269. return playing->get_playback_position();
  270. }
  271. return 0;
  272. }
  273. void AudioStreamPlaybackRandomPitch::seek(float p_time) {
  274. if (playing.is_valid()) {
  275. playing->seek(p_time);
  276. }
  277. }
  278. void AudioStreamPlaybackRandomPitch::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
  279. if (playing.is_valid()) {
  280. playing->mix(p_buffer, p_rate_scale * pitch_scale, p_frames);
  281. } else {
  282. for (int i = 0; i < p_frames; i++) {
  283. p_buffer[i] = AudioFrame(0, 0);
  284. }
  285. }
  286. }
  287. AudioStreamPlaybackRandomPitch::~AudioStreamPlaybackRandomPitch() {
  288. random_pitch->playbacks.erase(this);
  289. }