Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
music.h
Go to the documentation of this file.
1 #ifndef _MUSIC_H_
2 #define _MUSIC_H_
3 #include "container.h"
4 #include <vector>
5 #include <string>
6 #include <list>
7 #if defined(__HAIKU__)
8 #include <pthread.h>
9 #endif
10 
11 namespace Muzak
12 {
13 #ifndef _WIN32
14 extern void * readerThread( void *input );
15 #else
16 extern DWORD WINAPI readerThread( PVOID input );
17 #endif
18 };
19 
20 class Music
21 {
22  bool random;
23  float maxhull;
24  int socketr;
25  int socketw;
26  UnitContainer p;
27  int song; //currently playing song
28  int lastlist;
29  float vol;
30  float soft_vol;
31  float soft_vol_up_latency;
32  float soft_vol_down_latency;
33 
34  std::list< int >playingSource;
35 
36  bool LoadMusic( const char *file );
37  struct PlayList
38  {
39  typedef std::vector< std::string > SongList;
40  typedef std::map< std::string, std::string >PragmaList;
41 
42  SongList songs;
43  PragmaList pragmas;
44 
45  unsigned int counter;
46  PlayList()
47  {
48  counter = 0;
49  }
50  bool empty() const
51  {
52  return songs.empty();
53  }
54  size_t size() const
55  {
56  return songs.size();
57  }
58  std::string&operator[]( size_t index )
59  {
60  return songs[index];
61  }
62  const std::string&operator[]( size_t index ) const
63  {
64  return songs[index];
65  }
66  void push_back( const std::string &s )
67  {
68  songs.push_back( s );
69  }
70 
71  bool haspragma( const std::string &name ) const
72  {
73  return pragmas.find( name ) != pragmas.end();
74  }
75  const std::string& pragma( const std::string &name, const std::string &def ) const
76  {
77  PragmaList::const_iterator it = pragmas.find( name );
78  if ( it != pragmas.end() )
79  return it->second;
80 
81  else
82  return def;
83  }
84  };
85  std::vector< PlayList >playlist;
86 public:
88  Music( Unit *parent = NULL );
89  int loopsleft;
90  void Listen();
91  ~Music();
92  void SetParent( Unit *parent );
93 
94  void GotoSong( int whichlist, int whichsong, bool skip, int layer = -1 );
95 
96  static int Addlist( std::string listfile );
97  static void SetLoops( int numloops, int layer = -1 );
98  static void ChangeVolume( float inc = 0, int layer = -1 );
99 
100  static void Skip( int layer = -1 );
101  static void Stop( int layer = -1 );
102  static void SkipRandSong( int whichlist, int layer = -1 );
103  static void SkipRandList( int layer = -1 );
104 
105  static void GotoSong( std::string mus, int layer = -1 );
106 
107  static void InitMuzak();
108  static void CleanupMuzak();
109  static void MuzakCycle();
110 
111  static void SetVolume( float vol, int layer = -1, bool hardware = false, float latency_override = -1 );
112  static void Mute( bool mute = true, int layer = -1 );
113  std::vector< int >sounds_to_stop;
114 private:
115  void _StopLater();
116  void _StopNow();
117  void _GotoSong( std::string mus );
118  int _Addlist( std::string listfile );
119  void _SetVolume( float vol = 0, bool hardware = false, float latency_override = -1 );
120  void _SkipRandSong( int whichlist, int layer = -1 );
121  void _SkipRandList( int layer = -1 );
122  void _Skip( int layer = -1 );
123  void _Stop();
124 
125  std::list< std::string >recent_songs;
126 
127  int SelectTracks( int layer = -1 );
128 
129 #ifndef _WIN32
130  friend void* Muzak::readerThread( void *input );
131 #else
132  friend DWORD WINAPI Muzak::readerThread( PVOID input );
133 #endif
134 
135 #ifdef _WIN32
136  void *a_thread;
137  HANDLE musicinfo_mutex;
138 #else
139  pthread_t a_thread;
140  pthread_mutex_t musicinfo_mutex;
141 #endif
142 
143  int thread_initialized;
144  volatile bool moredata;
145  volatile bool music_loaded;
146  volatile bool killthread;
147  volatile bool threadalive;
148  volatile bool music_loading; //Opposite order of music_loaded.
149  bool freeWav;
150  struct AUDSoundProperties *music_load_info;
151  vector< std::string >music_load_list; //reverse order.
152 
153  void _LoadLastSongAsync();
154 
155  std::string cur_song_file;
156 };
157 extern Music *muzak;
158 extern int muzak_count;
159 
160 #endif
161