cp_player_data.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*************************************************************************/
  2. /* cp_player_data.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "cp_player_data.h"
  31. #include <stdio.h>
  32. CPPlayer::CPPlayer(CPMixer *p_mixer, CPSong *p_song) {
  33. song = p_song;
  34. mixer = p_mixer;
  35. control.max_voices = p_mixer->get_total_voice_count() - 1; //leave one for the sample
  36. control.force_no_nna = false;
  37. control.external_vibrato = false;
  38. control.filters = true;
  39. control.random_seed = 128364; //anything
  40. control.play_mode = 0;
  41. set_virtual_channels(p_mixer->get_total_voice_count());
  42. mixer->set_callback(&CPPlayer::callback_function, this);
  43. reset();
  44. }
  45. CPPlayer::~CPPlayer() {
  46. }
  47. void CPPlayer::set_virtual_channels(int p_amount) {
  48. if (p_amount < 1) return;
  49. if (p_amount > mixer->get_total_voice_count())
  50. return;
  51. control.max_voices = p_amount;
  52. }
  53. void CPPlayer::callback_function(void *p_userdata) {
  54. CPPlayer *pd = (CPPlayer *)p_userdata;
  55. pd->process_tick();
  56. }
  57. void CPPlayer::process_tick() {
  58. handle_tick();
  59. mixer->set_callback_interval(2500000 / control.tempo);
  60. song_usecs += 2500000 / control.tempo;
  61. }
  62. void CPPlayer::reset() {
  63. if (mixer == NULL) return;
  64. if (song == NULL) return;
  65. int i;
  66. for (i = 0; i < control.max_voices; i++) {
  67. voice[i].reset();
  68. mixer->stop_voice(i);
  69. }
  70. for (i = 0; i < CPPattern::WIDTH; i++) {
  71. control.channel[i].reset();
  72. control.channel[i].channel_volume = song->get_channel_volume(i);
  73. control.channel[i].channel_panning = ((int)song->get_channel_pan(i) * PAN_RIGHT / 64);
  74. if (song->is_channel_surround(i))
  75. control.channel[i].channel_panning = PAN_SURROUND;
  76. control.channel[i].mute = song->is_channel_mute(i);
  77. control.channel[i].chorus_send = song->get_channel_chorus(i) * 0xFF / 64;
  78. control.channel[i].reverb_send = song->get_channel_reverb(i) * 0xFF / 64;
  79. }
  80. control.speed = song->get_speed();
  81. control.tempo = song->get_tempo();
  82. control.global_volume = song->get_global_volume();
  83. control.position.current_pattern = 0;
  84. control.position.current_row = 0;
  85. control.position.current_order = 0;
  86. control.position.force_next_order = -1;
  87. control.ticks_counter = control.speed;
  88. control.position.forbid_jump = false;
  89. song_usecs = 0;
  90. }
  91. int64_t CPPlayer::get_channel_last_note_time_usec(int p_channel) const {
  92. CP_FAIL_INDEX_V(p_channel, 64, -1);
  93. return control.channel[p_channel].last_event_usecs;
  94. }
  95. void CPPlayer::set_channel_global_volume(int p_channel, int p_volume) {
  96. CP_FAIL_INDEX(p_channel, 64);
  97. control.channel[p_channel].channel_global_volume = CLAMP(p_volume, 0, 255);
  98. }
  99. int CPPlayer::get_channel_global_volume(int p_channel) const {
  100. CP_FAIL_INDEX_V(p_channel, 64, -1);
  101. return control.channel[p_channel].channel_global_volume;
  102. }
  103. bool CPPlayer::reached_end_of_song() {
  104. return control.reached_end;
  105. }
  106. void CPPlayer::set_force_external_vibratos(bool p_force) {
  107. control.external_vibrato = p_force;
  108. }
  109. void CPPlayer::set_force_no_nna(bool p_force) {
  110. control.force_no_nna = p_force;
  111. }