event_stream_chibi.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*************************************************************************/
  2. /* event_stream_chibi.h */
  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. #ifndef EVENT_STREAM_CHIBI_H
  31. #define EVENT_STREAM_CHIBI_H
  32. #include "cp_file_access_wrapper.h"
  33. #include "cp_mixer.h"
  34. #include "cp_player_data.h"
  35. #include "cp_sample_manager.h"
  36. #include "cp_song.h"
  37. #include "io/resource_loader.h"
  38. #include "os/file_access.h"
  39. #include "resource.h"
  40. #include "scene/resources/event_stream.h"
  41. #include "servers/audio_server.h"
  42. /** SAMPLE MANAGER **/
  43. class CPSampleManagerImpl : public CPSampleManager {
  44. struct SampleData {
  45. RID rid;
  46. bool stereo;
  47. bool is16;
  48. int len;
  49. int mixfreq;
  50. int loop_begin;
  51. int loop_end;
  52. int locks;
  53. DVector<uint8_t> lock;
  54. DVector<uint8_t>::Write w;
  55. CPSample_Loop_Type loop_type;
  56. };
  57. _FORCE_INLINE_ SampleData *_getsd(CPSample_ID p_id) {
  58. return ((SampleData *)p_id._private);
  59. }
  60. Set<SampleData *> valid;
  61. public:
  62. _FORCE_INLINE_ RID get_rid(CPSample_ID p_id) { return _getsd(p_id)->rid; }
  63. virtual CPSample_ID create(bool p_16bits, bool p_stereo, int32_t p_len);
  64. virtual void recreate(CPSample_ID p_id, bool p_16bits, bool p_stereo, int32_t p_len);
  65. virtual void destroy(CPSample_ID p_id);
  66. virtual bool check(CPSample_ID p_id); // return false if invalid
  67. virtual void set_c5_freq(CPSample_ID p_id, int32_t p_freq);
  68. virtual void set_loop_begin(CPSample_ID p_id, int32_t p_begin);
  69. virtual void set_loop_end(CPSample_ID p_id, int32_t p_end);
  70. virtual void set_loop_type(CPSample_ID p_id, CPSample_Loop_Type p_type);
  71. virtual void set_chunk(CPSample_ID p_id, int32_t p_index, void *p_data, int p_data_len);
  72. virtual int32_t get_loop_begin(CPSample_ID p_id);
  73. virtual int32_t get_loop_end(CPSample_ID p_id);
  74. virtual CPSample_Loop_Type get_loop_type(CPSample_ID p_id);
  75. virtual int32_t get_c5_freq(CPSample_ID p_id);
  76. virtual int32_t get_size(CPSample_ID p_id);
  77. virtual bool is_16bits(CPSample_ID p_id);
  78. virtual bool is_stereo(CPSample_ID p_id);
  79. virtual bool lock_data(CPSample_ID p_id);
  80. virtual void *get_data(CPSample_ID p_id); /* WARNING: Not all sample managers
  81. may be able to implement this, it depends on the mixer in use! */
  82. virtual int16_t get_data(CPSample_ID p_id, int p_sample, int p_channel = 0); /// Does not need locking
  83. virtual void set_data(CPSample_ID p_id, int p_sample, int16_t p_data, int p_channel = 0); /// Does not need locking
  84. virtual void unlock_data(CPSample_ID p_id);
  85. virtual void get_chunk(CPSample_ID p_id, int32_t p_index, void *p_data, int p_data_len);
  86. };
  87. /** MIXER **/
  88. class CPMixerImpl : public CPMixer {
  89. enum {
  90. MAX_VOICES = 64
  91. };
  92. struct Voice {
  93. AudioMixer::ChannelID channel;
  94. CPSample_ID sample;
  95. float freq_mult;
  96. float reverb;
  97. Voice() { reverb = 0.0; }
  98. };
  99. Voice voices[MAX_VOICES];
  100. int callback_interval;
  101. int callback_timeout;
  102. void (*callback)(void *);
  103. void *userdata;
  104. float voice_scale;
  105. float tempo_scale;
  106. float pitch_scale;
  107. AudioMixer::ReverbRoomType reverb_type;
  108. AudioMixer *mixer;
  109. public:
  110. void process_usecs(int p_usec, float p_volume, float p_pitch_scale, float p_tempo_scale);
  111. /* Callback */
  112. virtual void set_callback_interval(int p_interval_us); //in usecs, for tracker it's 2500000/tempo
  113. virtual void set_callback(void (*p_callback)(void *), void *p_userdata);
  114. /* Voice Control */
  115. virtual void setup_voice(int p_voice_index, CPSample_ID p_sample_id, int32_t p_start_index);
  116. virtual void stop_voice(int p_voice_index);
  117. virtual void set_voice_frequency(int p_voice_index, int32_t p_freq); //in freq*FREQUENCY_BITS
  118. virtual void set_voice_panning(int p_voice_index, int p_pan);
  119. virtual void set_voice_volume(int p_voice_index, int p_vol);
  120. virtual void set_voice_filter(int p_filter, bool p_enabled, uint8_t p_cutoff, uint8_t p_resonance);
  121. virtual void set_voice_reverb_send(int p_voice_index, int p_reverb);
  122. virtual void set_voice_chorus_send(int p_voice_index, int p_chorus); /* 0 - 255 */
  123. virtual void set_reverb_mode(ReverbMode p_mode);
  124. virtual void set_chorus_params(unsigned int p_delay_ms, unsigned int p_separation_ms, unsigned int p_depth_ms10, unsigned int p_speed_hz10);
  125. /* Info retrieving */
  126. virtual int32_t get_voice_sample_pos_index(int p_voice_index);
  127. virtual int get_voice_panning(int p_voice_index);
  128. virtual int get_voice_volume(int p_voice_index);
  129. virtual CPSample_ID get_voice_sample_id(int p_voice_index);
  130. virtual bool is_voice_active(int p_voice_index);
  131. virtual int get_active_voice_count() { return 0; }
  132. virtual int get_total_voice_count() { return MAX_VOICES; }
  133. virtual uint32_t get_mix_frequency() { return 0; }
  134. /* Methods below only work with software mixers, meant for software-based sound drivers, hardware mixers ignore them */
  135. virtual int32_t process(int32_t p_frames) { return 0; }
  136. virtual int32_t *get_mixdown_buffer_ptr() { return NULL; }
  137. virtual void set_mix_frequency(int32_t p_mix_frequency){};
  138. CPMixerImpl(AudioMixer *p_mixer = NULL);
  139. virtual ~CPMixerImpl() {}
  140. };
  141. /** FILE ACCESS **/
  142. class CPFileAccessWrapperImpl : public CPFileAccessWrapper {
  143. FileAccess *f;
  144. public:
  145. virtual Error open(const char *p_filename, int p_mode_flags);
  146. virtual void close();
  147. virtual void seek(uint32_t p_position);
  148. virtual void seek_end();
  149. virtual uint32_t get_pos();
  150. virtual bool eof_reached();
  151. virtual uint8_t get_byte();
  152. virtual void get_byte_array(uint8_t *p_dest, int p_elements);
  153. virtual void get_word_array(uint16_t *p_dest, int p_elements);
  154. virtual uint16_t get_word();
  155. virtual uint32_t get_dword();
  156. virtual void set_endian_conversion(bool p_swap);
  157. virtual bool is_open();
  158. virtual Error get_error();
  159. virtual void store_byte(uint8_t p_dest);
  160. virtual void store_byte_array(const uint8_t *p_dest, int p_elements);
  161. virtual void store_word(uint16_t p_dest);
  162. virtual void store_dword(uint32_t p_dest);
  163. CPFileAccessWrapperImpl() { f = NULL; }
  164. virtual ~CPFileAccessWrapperImpl() {
  165. if (f) memdelete(f);
  166. }
  167. };
  168. /////////////////////
  169. class EventStreamChibi;
  170. class EventStreamPlaybackChibi : public EventStreamPlayback {
  171. OBJ_TYPE(EventStreamPlaybackChibi, EventStreamPlayback);
  172. CPMixerImpl mixer;
  173. uint64_t total_usec;
  174. Ref<EventStreamChibi> stream;
  175. mutable CPPlayer *player;
  176. bool loop;
  177. int last_order;
  178. int loops;
  179. virtual Error _play();
  180. virtual bool _update(AudioMixer *p_mixer, uint64_t p_usec);
  181. virtual void _stop();
  182. float volume;
  183. float tempo_scale;
  184. float pitch_scale;
  185. public:
  186. virtual void set_paused(bool p_paused);
  187. virtual bool is_paused() const;
  188. virtual void set_loop(bool p_loop);
  189. virtual bool is_loop_enabled() const;
  190. virtual int get_loop_count() const;
  191. virtual float get_pos() const;
  192. virtual void seek_pos(float p_time);
  193. virtual void set_volume(float p_vol);
  194. virtual float get_volume() const;
  195. virtual void set_pitch_scale(float p_pitch_scale);
  196. virtual float get_pitch_scale() const;
  197. virtual void set_tempo_scale(float p_tempo_scale);
  198. virtual float get_tempo_scale() const;
  199. virtual void set_channel_volume(int p_channel, float p_volume);
  200. virtual float get_channel_volume(int p_channel) const;
  201. virtual float get_last_note_time(int p_channel) const;
  202. EventStreamPlaybackChibi(Ref<EventStreamChibi> p_stream = Ref<EventStreamChibi>());
  203. ~EventStreamPlaybackChibi();
  204. };
  205. class EventStreamChibi : public EventStream {
  206. OBJ_TYPE(EventStreamChibi, EventStream);
  207. friend class ResourceFormatLoaderChibi;
  208. friend class EventStreamPlaybackChibi;
  209. //I think i didn't know what const was when i wrote this more than a decade ago
  210. //so it goes mutable :(
  211. mutable CPSong song;
  212. public:
  213. virtual Ref<EventStreamPlayback> instance_playback();
  214. virtual String get_stream_name() const;
  215. virtual float get_length() const;
  216. virtual int get_channel_count() const { return 64; } //tracker limit
  217. EventStreamChibi();
  218. };
  219. class ResourceFormatLoaderChibi : public ResourceFormatLoader {
  220. public:
  221. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  222. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  223. virtual bool handles_type(const String &p_type) const;
  224. virtual String get_resource_type(const String &p_path) const;
  225. };
  226. void initialize_chibi();
  227. void finalize_chibi();
  228. #endif // EVENT_STREAM_CHIBI_H