1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
#include "music.h"
#include <string>
#include <aldumb.h>
#include <iostream>
#include "defs.h"
#ifdef WINDOWS
#include <winalleg.h>
#endif
#include <pthread.h>
using namespace std;
static Music * instance = NULL;
static double volume = 1.0;
// static bool muted = false;
static pthread_t musicThread;
static pthread_mutex_t musicMutex;
static bool alive = true;
static void * playMusic( void * );
#define synchronized for( int __l( ! pthread_mutex_lock( &musicMutex ) ); __l; __l = 0, pthread_mutex_unlock( &musicMutex ) )
#define LOCK pthread_mutex_lock( &musicMutex );
#define UNLOCK pthread_mutex_unlock( &musicMutex );
/*
#undef LOCK
#undef UNLOCK
#define LOCK
#define UNLOCK
*/
Music::Music():
playing( false ),
player( NULL ),
music_file( NULL ){
if ( instance != NULL ){
cerr << "Trying to instantiate music object twice!" << endl;
return;
}
instance = this;
pthread_mutex_init( &musicMutex, NULL );
pthread_create( &musicThread, NULL, playMusic, (void *)instance );
}
/*
static bool isAlive(){
bool f = false;
synchronized{
f = alive;
}
return f;
}
*/
static void * playMusic( void * _music ){
Music * music = (Music *) _music;
cout << "Playing music" << endl;
unsigned int tick = 0;
unsigned int counter;
bool playing = true;
while ( playing ){
LOCK;{
playing = alive;
music->doPlay();
}
UNLOCK;
rest( 50 );
// Util::YIELD();
// pthread_yield();
}
// cout << "Done with music thread" << endl;
return NULL;
}
double Music::getVolume(){
return volume;
}
void Music::doPlay(){
if ( this->playing ){
if ( al_poll_duh( this->player ) != 0 ){
}
}
}
/*
Music::Music( const char * song ):
volume( 1.0 ),
muted( false ),
player( NULL ),
music_file( NULL ){
loadSong( song );
}
Music::Music( const string & song ):
volume( 1.0 ),
muted( false ),
player( NULL ),
music_file( NULL ){
loadSong( song );
}
*/
bool Music::loadSong( const char * song ){
bool loaded = false;
LOCK;{
loaded = instance->internal_loadSong( song );
}
UNLOCK;
return loaded;
// muted = false;
}
/* remove an element from a vector at index 'pos' and return it */
template< class Tx_ >
static Tx_ removeVectorElement( vector< Tx_ > & toRemove, int pos ){
int count = 0;
typename vector< Tx_ >::iterator it;
for ( it = toRemove.begin(); it != toRemove.end() && count < pos; count++, it++ );
if ( it == toRemove.end() ){
/* this isnt right, but whatever */
return toRemove.front();
}
const Tx_ & removed = toRemove[ pos ];
toRemove.erase( it );
return removed;
}
void Music::loadSong( const vector< string > & Songs ){
/*
cout << "Songs = " << &Songs << endl;
if ( ! loadSong( "music/song5.xm" ) ){
cerr << "Could not load music/song5.xm" << endl;
}
return;
*/
vector< string > _songs = Songs;
vector< string > songs;
while ( ! _songs.empty() ){
int i = Util::rnd( _songs.size() );
songs.push_back( removeVectorElement< string >( _songs, i ) );
}
/*
songs.clear();
songs.push_back( "music/song3.xm" );
*/
for ( vector< string >::iterator it = songs.begin(); it != songs.end(); it++ ){
cout << "Trying to load song " << *it << endl;
if ( loadSong( *it ) ){
break;
}
}
}
bool Music::loadSong( const string & song ){
return loadSong( song.c_str() );
}
void Music::_play(){
if ( playing == false && this->player != NULL ){
al_resume_duh( this->player );
}
playing = true;
}
void Music::play(){
LOCK;{
instance->_play();
}
UNLOCK;
}
void Music::_pause(){
playing = false;
if ( this->player != NULL ){
al_pause_duh( this->player );
}
}
void Music::pause(){
LOCK;{
instance->_pause();
}
UNLOCK;
}
void Music::soften(){
instance->_soften();
}
void Music::_soften(){
if ( volume > 0.1 ) volume -= 0.1;
else volume = 0.0;
setVolume( volume );
}
void Music::louden(){
instance->_louden();
}
void Music::_louden(){
if ( volume < 0.9 ) volume += 0.1;
else volume = 1.0;
setVolume( volume );
}
void Music::mute(){
setVolume( 0 );
}
void Music::setVolume( double vol ){
volume = vol;
LOCK;{
instance->_setVolume( volume );
}
UNLOCK;
}
void Music::_setVolume( double vol ){
if ( player ){
al_duh_set_volume( player, vol );
}
}
Music::~Music(){
LOCK;{
if ( player ){
al_stop_duh( player );
unload_duh( music_file );
}
alive = false;
}
UNLOCK;
cout << "Waiting for music thread to die" << endl;
pthread_join( musicThread, NULL );
}
/*
void Music::pause(){
al_pause_duh( player );
}
*/
/*
void Music::resume(){
al_resume_duh( player );
}
*/
bool Music::internal_loadSong( const char * path ){
// cout << "Trying to load '" << path << "'" << endl;
if ( player != NULL ){
al_stop_duh( player );
unload_duh( music_file );
player = NULL;
music_file = NULL;
}
// music_file = dumb_load_mod( path );
/*
music_file = dumb_load_mod( path );
if ( !music_file ){
music_file = dumb_load_xm( path );
}
if ( !music_file ){
music_file = dumb_load_s3m( path );
}
if ( !music_file ){
music_file = dumb_load_it( path );
}
*/
for ( int i = 0; i < 4; i++ ){
switch ( i ){
case 0 : {
music_file = dumb_load_xm( path );
break;
}
case 1 : {
music_file = dumb_load_s3m( path );
break;
}
case 2 : {
music_file = dumb_load_it( path );
break;
}
case 3 : {
music_file = dumb_load_mod( path );
break;
}
}
if ( music_file != NULL ){
cout << "Loaded " << path << " type " << i << endl;
break;
}
}
if ( music_file ){
int buf = 1 << 14;
player = al_start_duh( music_file, 2, 0, volume, buf, 22050 );
// cout << "Loaded music player " << player << endl;
/*
while ( 1 ){
al_poll_duh( player );
rest( 1 );
}
*/
playing = true;
} else {
cout<<"Could not load "<<path<<endl;
return false;
}
return true;
}
#undef synchronized
#undef LOCK
#undef UNLOCK
|