audio_stream.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*************************************************************************/
  2. /* audio_stream.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/config/project_settings.h"
  32. #include "core/os/os.h"
  33. void AudioStreamPlayback::start(float p_from_pos) {
  34. if (GDVIRTUAL_CALL(_start, p_from_pos)) {
  35. return;
  36. }
  37. ERR_FAIL_MSG("AudioStreamPlayback::start unimplemented!");
  38. }
  39. void AudioStreamPlayback::stop() {
  40. if (GDVIRTUAL_CALL(_stop)) {
  41. return;
  42. }
  43. ERR_FAIL_MSG("AudioStreamPlayback::stop unimplemented!");
  44. }
  45. bool AudioStreamPlayback::is_playing() const {
  46. bool ret;
  47. if (GDVIRTUAL_CALL(_is_playing, ret)) {
  48. return ret;
  49. }
  50. ERR_FAIL_V_MSG(false, "AudioStreamPlayback::is_playing unimplemented!");
  51. }
  52. int AudioStreamPlayback::get_loop_count() const {
  53. int ret;
  54. if (GDVIRTUAL_CALL(_get_loop_count, ret)) {
  55. return ret;
  56. }
  57. return 0;
  58. }
  59. float AudioStreamPlayback::get_playback_position() const {
  60. float ret;
  61. if (GDVIRTUAL_CALL(_get_playback_position, ret)) {
  62. return ret;
  63. }
  64. ERR_FAIL_V_MSG(0, "AudioStreamPlayback::get_playback_position unimplemented!");
  65. }
  66. void AudioStreamPlayback::seek(float p_time) {
  67. if (GDVIRTUAL_CALL(_seek, p_time)) {
  68. return;
  69. }
  70. }
  71. int AudioStreamPlayback::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
  72. int ret;
  73. if (GDVIRTUAL_CALL(_mix, p_buffer, p_rate_scale, p_frames, ret)) {
  74. return ret;
  75. }
  76. WARN_PRINT_ONCE("AudioStreamPlayback::mix unimplemented!");
  77. return 0;
  78. }
  79. void AudioStreamPlayback::_bind_methods() {
  80. GDVIRTUAL_BIND(_start, "from_pos")
  81. GDVIRTUAL_BIND(_stop)
  82. GDVIRTUAL_BIND(_is_playing)
  83. GDVIRTUAL_BIND(_get_loop_count)
  84. GDVIRTUAL_BIND(_get_playback_position)
  85. GDVIRTUAL_BIND(_seek, "position")
  86. GDVIRTUAL_BIND(_mix, "buffer", "rate_scale", "frames");
  87. }
  88. //////////////////////////////
  89. void AudioStreamPlaybackResampled::_begin_resample() {
  90. //clear cubic interpolation history
  91. internal_buffer[0] = AudioFrame(0.0, 0.0);
  92. internal_buffer[1] = AudioFrame(0.0, 0.0);
  93. internal_buffer[2] = AudioFrame(0.0, 0.0);
  94. internal_buffer[3] = AudioFrame(0.0, 0.0);
  95. //mix buffer
  96. _mix_internal(internal_buffer + 4, INTERNAL_BUFFER_LEN);
  97. mix_offset = 0;
  98. }
  99. int AudioStreamPlaybackResampled::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
  100. float target_rate = AudioServer::get_singleton()->get_mix_rate();
  101. float playback_speed_scale = AudioServer::get_singleton()->get_playback_speed_scale();
  102. uint64_t mix_increment = uint64_t(((get_stream_sampling_rate() * p_rate_scale * playback_speed_scale) / double(target_rate)) * double(FP_LEN));
  103. int mixed_frames_total = p_frames;
  104. for (int i = 0; i < p_frames; i++) {
  105. uint32_t idx = CUBIC_INTERP_HISTORY + uint32_t(mix_offset >> FP_BITS);
  106. //standard cubic interpolation (great quality/performance ratio)
  107. //this used to be moved to a LUT for greater performance, but nowadays CPU speed is generally faster than memory.
  108. float mu = (mix_offset & FP_MASK) / float(FP_LEN);
  109. AudioFrame y0 = internal_buffer[idx - 3];
  110. AudioFrame y1 = internal_buffer[idx - 2];
  111. AudioFrame y2 = internal_buffer[idx - 1];
  112. AudioFrame y3 = internal_buffer[idx - 0];
  113. if (idx <= internal_buffer_end && idx >= internal_buffer_end && mixed_frames_total == p_frames) {
  114. // The internal buffer ends somewhere in this range, and we haven't yet recorded the number of good frames we have.
  115. mixed_frames_total = i;
  116. }
  117. float mu2 = mu * mu;
  118. AudioFrame a0 = 3 * y1 - 3 * y2 + y3 - y0;
  119. AudioFrame a1 = 2 * y0 - 5 * y1 + 4 * y2 - y3;
  120. AudioFrame a2 = y2 - y0;
  121. AudioFrame a3 = 2 * y1;
  122. p_buffer[i] = (a0 * mu * mu2 + a1 * mu2 + a2 * mu + a3) / 2;
  123. mix_offset += mix_increment;
  124. while ((mix_offset >> FP_BITS) >= INTERNAL_BUFFER_LEN) {
  125. internal_buffer[0] = internal_buffer[INTERNAL_BUFFER_LEN + 0];
  126. internal_buffer[1] = internal_buffer[INTERNAL_BUFFER_LEN + 1];
  127. internal_buffer[2] = internal_buffer[INTERNAL_BUFFER_LEN + 2];
  128. internal_buffer[3] = internal_buffer[INTERNAL_BUFFER_LEN + 3];
  129. if (is_playing()) {
  130. int mixed_frames = _mix_internal(internal_buffer + 4, INTERNAL_BUFFER_LEN);
  131. if (mixed_frames != INTERNAL_BUFFER_LEN) {
  132. // internal_buffer[mixed_frames] is the first frame of silence.
  133. internal_buffer_end = mixed_frames;
  134. } else {
  135. // The internal buffer does not contain the first frame of silence.
  136. internal_buffer_end = -1;
  137. }
  138. } else {
  139. //fill with silence, not playing
  140. for (int j = 0; j < INTERNAL_BUFFER_LEN; ++j) {
  141. internal_buffer[j + 4] = AudioFrame(0, 0);
  142. }
  143. }
  144. mix_offset -= (INTERNAL_BUFFER_LEN << FP_BITS);
  145. }
  146. }
  147. return mixed_frames_total;
  148. }
  149. ////////////////////////////////
  150. Ref<AudioStreamPlayback> AudioStream::instance_playback() {
  151. Ref<AudioStreamPlayback> ret;
  152. if (GDVIRTUAL_CALL(_instance_playback, ret)) {
  153. return ret;
  154. }
  155. ERR_FAIL_V_MSG(Ref<AudioStreamPlayback>(), "Method must be implemented!");
  156. }
  157. String AudioStream::get_stream_name() const {
  158. String ret;
  159. if (GDVIRTUAL_CALL(_get_stream_name, ret)) {
  160. return ret;
  161. }
  162. return String();
  163. }
  164. float AudioStream::get_length() const {
  165. float ret;
  166. if (GDVIRTUAL_CALL(_get_length, ret)) {
  167. return ret;
  168. }
  169. return 0;
  170. }
  171. bool AudioStream::is_monophonic() const {
  172. bool ret;
  173. if (GDVIRTUAL_CALL(_is_monophonic, ret)) {
  174. return ret;
  175. }
  176. return true;
  177. }
  178. void AudioStream::_bind_methods() {
  179. ClassDB::bind_method(D_METHOD("get_length"), &AudioStream::get_length);
  180. ClassDB::bind_method(D_METHOD("is_monophonic"), &AudioStream::is_monophonic);
  181. GDVIRTUAL_BIND(_instance_playback);
  182. GDVIRTUAL_BIND(_get_stream_name);
  183. GDVIRTUAL_BIND(_get_length);
  184. GDVIRTUAL_BIND(_is_monophonic);
  185. }
  186. ////////////////////////////////
  187. Ref<AudioStreamPlayback> AudioStreamMicrophone::instance_playback() {
  188. Ref<AudioStreamPlaybackMicrophone> playback;
  189. playback.instantiate();
  190. playbacks.insert(playback.ptr());
  191. playback->microphone = Ref<AudioStreamMicrophone>((AudioStreamMicrophone *)this);
  192. playback->active = false;
  193. return playback;
  194. }
  195. String AudioStreamMicrophone::get_stream_name() const {
  196. //if (audio_stream.is_valid()) {
  197. //return "Random: " + audio_stream->get_name();
  198. //}
  199. return "Microphone";
  200. }
  201. float AudioStreamMicrophone::get_length() const {
  202. return 0;
  203. }
  204. bool AudioStreamMicrophone::is_monophonic() const {
  205. return true;
  206. }
  207. void AudioStreamMicrophone::_bind_methods() {
  208. }
  209. AudioStreamMicrophone::AudioStreamMicrophone() {
  210. }
  211. int AudioStreamPlaybackMicrophone::_mix_internal(AudioFrame *p_buffer, int p_frames) {
  212. AudioDriver::get_singleton()->lock();
  213. Vector<int32_t> buf = AudioDriver::get_singleton()->get_input_buffer();
  214. unsigned int input_size = AudioDriver::get_singleton()->get_input_size();
  215. int mix_rate = AudioDriver::get_singleton()->get_mix_rate();
  216. unsigned int playback_delay = MIN(((50 * mix_rate) / 1000) * 2, buf.size() >> 1);
  217. #ifdef DEBUG_ENABLED
  218. unsigned int input_position = AudioDriver::get_singleton()->get_input_position();
  219. #endif
  220. int mixed_frames = p_frames;
  221. if (playback_delay > input_size) {
  222. for (int i = 0; i < p_frames; i++) {
  223. p_buffer[i] = AudioFrame(0.0f, 0.0f);
  224. }
  225. input_ofs = 0;
  226. } else {
  227. for (int i = 0; i < p_frames; i++) {
  228. if (input_size > input_ofs && (int)input_ofs < buf.size()) {
  229. float l = (buf[input_ofs++] >> 16) / 32768.f;
  230. if ((int)input_ofs >= buf.size()) {
  231. input_ofs = 0;
  232. }
  233. float r = (buf[input_ofs++] >> 16) / 32768.f;
  234. if ((int)input_ofs >= buf.size()) {
  235. input_ofs = 0;
  236. }
  237. p_buffer[i] = AudioFrame(l, r);
  238. } else {
  239. if (mixed_frames == p_frames) {
  240. mixed_frames = i;
  241. }
  242. p_buffer[i] = AudioFrame(0.0f, 0.0f);
  243. }
  244. }
  245. }
  246. #ifdef DEBUG_ENABLED
  247. if (input_ofs > input_position && (int)(input_ofs - input_position) < (p_frames * 2)) {
  248. print_verbose(String(get_class_name()) + " buffer underrun: input_position=" + itos(input_position) + " input_ofs=" + itos(input_ofs) + " input_size=" + itos(input_size));
  249. }
  250. #endif
  251. AudioDriver::get_singleton()->unlock();
  252. return mixed_frames;
  253. }
  254. int AudioStreamPlaybackMicrophone::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
  255. return AudioStreamPlaybackResampled::mix(p_buffer, p_rate_scale, p_frames);
  256. }
  257. float AudioStreamPlaybackMicrophone::get_stream_sampling_rate() {
  258. return AudioDriver::get_singleton()->get_mix_rate();
  259. }
  260. void AudioStreamPlaybackMicrophone::start(float p_from_pos) {
  261. if (active) {
  262. return;
  263. }
  264. if (!GLOBAL_GET("audio/driver/enable_input")) {
  265. WARN_PRINT("Need to enable Project settings > Audio > Enable Audio Input option to use capturing.");
  266. return;
  267. }
  268. input_ofs = 0;
  269. if (AudioDriver::get_singleton()->capture_start() == OK) {
  270. active = true;
  271. _begin_resample();
  272. }
  273. }
  274. void AudioStreamPlaybackMicrophone::stop() {
  275. if (active) {
  276. AudioDriver::get_singleton()->capture_stop();
  277. active = false;
  278. }
  279. }
  280. bool AudioStreamPlaybackMicrophone::is_playing() const {
  281. return active;
  282. }
  283. int AudioStreamPlaybackMicrophone::get_loop_count() const {
  284. return 0;
  285. }
  286. float AudioStreamPlaybackMicrophone::get_playback_position() const {
  287. return 0;
  288. }
  289. void AudioStreamPlaybackMicrophone::seek(float p_time) {
  290. // Can't seek a microphone input
  291. }
  292. AudioStreamPlaybackMicrophone::~AudioStreamPlaybackMicrophone() {
  293. microphone->playbacks.erase(this);
  294. stop();
  295. }
  296. AudioStreamPlaybackMicrophone::AudioStreamPlaybackMicrophone() {
  297. }
  298. ////////////////////////////////
  299. void AudioStreamRandomPitch::set_audio_stream(const Ref<AudioStream> &p_audio_stream) {
  300. audio_stream = p_audio_stream;
  301. if (audio_stream.is_valid()) {
  302. for (Set<AudioStreamPlaybackRandomPitch *>::Element *E = playbacks.front(); E; E = E->next()) {
  303. E->get()->playback = audio_stream->instance_playback();
  304. }
  305. }
  306. }
  307. Ref<AudioStream> AudioStreamRandomPitch::get_audio_stream() const {
  308. return audio_stream;
  309. }
  310. void AudioStreamRandomPitch::set_random_pitch(float p_pitch) {
  311. if (p_pitch < 1) {
  312. p_pitch = 1;
  313. }
  314. random_pitch = p_pitch;
  315. }
  316. float AudioStreamRandomPitch::get_random_pitch() const {
  317. return random_pitch;
  318. }
  319. Ref<AudioStreamPlayback> AudioStreamRandomPitch::instance_playback() {
  320. Ref<AudioStreamPlaybackRandomPitch> playback;
  321. playback.instantiate();
  322. if (audio_stream.is_valid()) {
  323. playback->playback = audio_stream->instance_playback();
  324. }
  325. playbacks.insert(playback.ptr());
  326. playback->random_pitch = Ref<AudioStreamRandomPitch>((AudioStreamRandomPitch *)this);
  327. return playback;
  328. }
  329. String AudioStreamRandomPitch::get_stream_name() const {
  330. if (audio_stream.is_valid()) {
  331. return "Random: " + audio_stream->get_name();
  332. }
  333. return "RandomPitch";
  334. }
  335. float AudioStreamRandomPitch::get_length() const {
  336. if (audio_stream.is_valid()) {
  337. return audio_stream->get_length();
  338. }
  339. return 0;
  340. }
  341. bool AudioStreamRandomPitch::is_monophonic() const {
  342. if (audio_stream.is_valid()) {
  343. return audio_stream->is_monophonic();
  344. }
  345. return true; // It doesn't really matter what we return here, but no sense instancing a many playbacks of a null stream.
  346. }
  347. void AudioStreamRandomPitch::_bind_methods() {
  348. ClassDB::bind_method(D_METHOD("set_audio_stream", "stream"), &AudioStreamRandomPitch::set_audio_stream);
  349. ClassDB::bind_method(D_METHOD("get_audio_stream"), &AudioStreamRandomPitch::get_audio_stream);
  350. ClassDB::bind_method(D_METHOD("set_random_pitch", "scale"), &AudioStreamRandomPitch::set_random_pitch);
  351. ClassDB::bind_method(D_METHOD("get_random_pitch"), &AudioStreamRandomPitch::get_random_pitch);
  352. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "audio_stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"), "set_audio_stream", "get_audio_stream");
  353. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "random_pitch", PROPERTY_HINT_RANGE, "1,16,0.01"), "set_random_pitch", "get_random_pitch");
  354. }
  355. AudioStreamRandomPitch::AudioStreamRandomPitch() {
  356. random_pitch = 1.1;
  357. }
  358. void AudioStreamPlaybackRandomPitch::start(float p_from_pos) {
  359. playing = playback;
  360. float range_from = 1.0 / random_pitch->random_pitch;
  361. float range_to = random_pitch->random_pitch;
  362. pitch_scale = range_from + Math::randf() * (range_to - range_from);
  363. if (playing.is_valid()) {
  364. playing->start(p_from_pos);
  365. }
  366. }
  367. void AudioStreamPlaybackRandomPitch::stop() {
  368. if (playing.is_valid()) {
  369. playing->stop();
  370. ;
  371. }
  372. }
  373. bool AudioStreamPlaybackRandomPitch::is_playing() const {
  374. if (playing.is_valid()) {
  375. return playing->is_playing();
  376. }
  377. return false;
  378. }
  379. int AudioStreamPlaybackRandomPitch::get_loop_count() const {
  380. if (playing.is_valid()) {
  381. return playing->get_loop_count();
  382. }
  383. return 0;
  384. }
  385. float AudioStreamPlaybackRandomPitch::get_playback_position() const {
  386. if (playing.is_valid()) {
  387. return playing->get_playback_position();
  388. }
  389. return 0;
  390. }
  391. void AudioStreamPlaybackRandomPitch::seek(float p_time) {
  392. if (playing.is_valid()) {
  393. playing->seek(p_time);
  394. }
  395. }
  396. int AudioStreamPlaybackRandomPitch::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
  397. if (playing.is_valid()) {
  398. return playing->mix(p_buffer, p_rate_scale * pitch_scale, p_frames);
  399. } else {
  400. for (int i = 0; i < p_frames; i++) {
  401. p_buffer[i] = AudioFrame(0, 0);
  402. }
  403. return p_frames;
  404. }
  405. }
  406. AudioStreamPlaybackRandomPitch::~AudioStreamPlaybackRandomPitch() {
  407. random_pitch->playbacks.erase(this);
  408. }
  409. /////////////////////////////////////////////