Music.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /******************************************************************************
  2. Use 'MusicManager's 'Music' and 'Ambient' to play different 'Playlist's.
  3. A 'Playlist' is a list of multiple songs.
  4. /******************************************************************************/
  5. const_mem_addr struct Playlist // List of Music Tracks !! must be stored in constant memory address !!
  6. {
  7. // get
  8. Int songs ( )C {return _songs.elms();} // get number of songs in this playlist
  9. Str song (Int i)C; // get the file name of i-th song in this playlist, null on fail
  10. UID songID(Int i)C; // get the file name ID of i-th song in this playlist, 'UIDZero' on fail
  11. // operations
  12. void operator+=(C Str &name); // add song to playlist, 'name'=song file name
  13. void operator+=(C UID &id ); // add song to playlist, 'id' =song file name ID
  14. void operator-=(C Str &name); // remove song from playlist, 'name'=song file name
  15. void operator-=(C UID &id ); // remove song from playlist, 'id' =song file name ID
  16. #if EE_PRIVATE
  17. Int globalSong(Int song, Bool shuffle, Randomizer &random); // get global song index, from desired local/playlist song
  18. Int nextSong( Bool shuffle, Randomizer &random); // get global song index
  19. Int prevSong( Bool shuffle, Randomizer &random); // get global song index
  20. #endif
  21. ~Playlist();
  22. Playlist();
  23. #if !EE_PRIVATE
  24. private:
  25. #endif
  26. Int _cur;
  27. Memc<Int> _songs;
  28. };
  29. /******************************************************************************/
  30. struct MusicManager
  31. {
  32. Bool shuffle ; // if select songs from Playlist in random order, default=true
  33. FADE_CURVE fade_curve; // curve used for song crossfade , default=FADE_LINEAR
  34. Flt fade_in , // fade in time for starting new songs , default=0.5
  35. fade_out , // fade out time for finishing old songs , default=3.0
  36. time_reset; // minimum time left needed to play a song again from its last position instead of playing it from the start, default=10, for example: if song "A" was playing, and user manually switched to song "B", the last position of "A" song is remembered before switching to "B" song, then if user switches back to "A" song, the engine first checks if last position of "A" song has at least 'time_reset' seconds before the end of the song, if yes then song "A" is played from its last remembered position, if not then it is played from the start of the song
  37. UID (*select_song)(C UID &next); // pointer to custom function (may be null) called when next song needs to be selected, return 'UID' of the song you wish to play, or 'UIDZero' for no song. 'next'=next song ID queued in the history, set to 'UIDZero' if there's no next song available. If this function is not specified then song will be selected based on active playlist. !! Warning: this may get called on a secondary thread !!
  38. // get
  39. Str name ()C; // get current song file name
  40. UID id ()C; // get current song file name ID
  41. Flt frac ()C; void frac(Flt frac); // get/set current song fraction position, 0..1
  42. Flt time ()C; void time(Flt time); // get/set current song time position, 0..length
  43. Flt length ()C; // get current song length in seconds
  44. Flt fade ()C; // get current song fade value
  45. Playlist* playlist()C {return _playlist;} // get active playlist
  46. // set
  47. void set (const_mem_addr Playlist *playlist ); // set active playlist, this playlist will be used after current song finishes playing
  48. void set (const_mem_addr Playlist &playlist ) {set (&playlist );} // set active playlist, this playlist will be used after current song finishes playing
  49. void play(const_mem_addr Playlist *playlist, Int song=-1); // set and play playlist with 'song' i-th song in the playlist (-1=last played song in the playlist)
  50. void play(const_mem_addr Playlist &playlist, Int song=-1) {play(&playlist, song);} // set and play playlist with 'song' i-th song in the playlist (-1=last played song in the playlist)
  51. void play(C Str &song_name); // play song by its file name , without changing current playlist
  52. void play(C UID &song_id ); // play song by its file name ID, without changing current playlist
  53. void stop(); // stop any playback, without changing current playlist
  54. void next(); // skip to the next song from active playlist
  55. void prev(); // skip to the previous song from active playlist
  56. void maxHistory(Int max_history); // set max number of songs to store in the history for 'prev/next' methods, use <=0 to disable history
  57. // data callback
  58. MusicManager& callback(SoundDataCallback *callback); SoundDataCallback* callback()C {return _callback;} // set/get data callback, it will be called every time a new portion of data is processed by the sound
  59. #if EE_PRIVATE
  60. void del ();
  61. void playSong(Int global_song);
  62. // lock required
  63. Flt lockedTimeLeft()C; // get current song remaining time
  64. void lockedUpdate ();
  65. void swap ();
  66. void storePos(Bool i);
  67. void fadeIn (Bool i);
  68. void fadeOut (Bool i);
  69. void del (Bool i);
  70. void set (Bool i, Int global_song);
  71. #endif
  72. #if !EE_PRIVATE
  73. private:
  74. #endif
  75. Int _song [2], _history_max, _history_pos;
  76. Sound _sound[2];
  77. VOLUME_GROUP _volume_group;
  78. Playlist *_playlist;
  79. SoundDataCallback *_callback;
  80. Randomizer _random;
  81. Memc<Int> _history;
  82. explicit MusicManager(VOLUME_GROUP volume_group);
  83. }extern
  84. Music , // Music MusicManager with VOLUME_MUSIC sound VOLUME_GROUP
  85. Ambient; // Ambient MusicManager with VOLUME_AMBIENT sound VOLUME_GROUP
  86. /******************************************************************************/
  87. #if EE_PRIVATE
  88. void ShutMusic();
  89. void UpdateMusic();
  90. #endif
  91. /******************************************************************************/