Changeset 423
- Timestamp:
- 07/02/07 01:53:18 (1 year ago)
- Files:
-
- branches/thune/thread_safe/gl/audio.c (modified) (9 diffs)
- branches/thune/thread_safe/gl/audio.h (modified) (1 diff)
- branches/thune/thread_safe/gl/gx.c (modified) (1 diff)
- branches/thune/thread_safe/gl/project.r (modified) (1 diff)
- branches/thune/thread_safe/gl/scripts/view.t (modified) (2 diffs)
- branches/thune/thread_safe/gl/test_fw.t (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/thune/thread_safe/gl/audio.c
r390 r423 2 2 // 3 3 // Thune OpenAL Module 4 // Copyright (C) 2005-200 6Karl Robillard4 // Copyright (C) 2005-2007 Karl Robillard 5 5 // 6 6 // This library is free software; you can redistribute it and/or … … 35 35 #include <AL/alc.h> 36 36 #include <AL/alut.h> 37 #include <AL/alext.h> 38 #endif 37 #endif 38 #include <vorbis/codec.h> 39 #include <vorbis/vorbisfile.h> 39 40 #include "os.h" 40 41 #include "urlan.h" … … 61 62 62 63 63 #define MUSIC_BUFFERS 1 64 static int _hasVorbis = 0; 64 #define MUSIC_BUFFERS 2 65 65 static ALuint _musicSource = 0; 66 66 static ALfloat _musicGain = 1.0f; 67 67 static ALuint _musicBuffers[ MUSIC_BUFFERS ]; 68 69 static FILE* _bgStream = 0; 70 static OggVorbis_File _vf; 71 static vorbis_info* _vinfo; 72 73 #if BYTE_ORDER == LITTLE_ENDIAN 74 #define OGG_BIG_ENDIAN 0 75 #else 76 #define OGG_BIG_ENDIAN 1 77 #endif 78 #define OGG_BUFFER_SIZE (4096 * 4) 79 static char _oggPCM[ OGG_BUFFER_SIZE ]; 80 81 /* 82 Returns false if all data has been read or an error occurs. 83 */ 84 static int _readOgg( ALuint buffer, OggVorbis_File* vf, vorbis_info* vinfo ) 85 { 86 int stream; 87 unsigned int count = 0; 88 89 while( count < OGG_BUFFER_SIZE ) 90 { 91 int amt = ov_read( vf, _oggPCM + count, OGG_BUFFER_SIZE - count, 92 OGG_BIG_ENDIAN, 2, 1, &stream ); 93 if( amt <= 0 ) 94 { 95 if( amt < 0 ) 96 fprintf( stderr, "ov_read" ); 97 break; 98 } 99 count += amt; 100 } 101 102 if( ! count ) 103 return 0; 104 105 ALint format = (vinfo->channels == 1) ? AL_FORMAT_MONO16 : 106 AL_FORMAT_STEREO16; 107 108 //printf( "KR ogg count %d\n", count ); 109 alBufferData( buffer, format, _oggPCM, count, vinfo->rate ); 110 111 return 1; 112 } 113 114 115 static void _startMusic() 116 { 117 int i; 118 for( i = 0; i < MUSIC_BUFFERS; ++i ) 119 { 120 if( ! _readOgg( _musicBuffers[i], &_vf, _vinfo ) ) 121 { 122 ov_clear( &_vf ); // Closes _bgStream for us. 123 _bgStream = 0; 124 break; 125 } 126 } 127 128 if( i > 0 ) 129 { 130 alSourceQueueBuffers( _musicSource, i, _musicBuffers ); 131 alSourcePlay( _musicSource ); 132 //printf( "KR _startMusic %d\n", i ); 133 } 134 } 68 135 69 136 … … 91 158 _audioUp = 1; 92 159 93 if( alIsExtensionPresent( "AL_EXT_vorbis" ) )94 _hasVorbis = 1;95 else96 dprint( "OpenAL AL_EXT_vorbis not present - music disabled\n" );97 98 160 return 1; 99 161 } … … 124 186 125 187 126 #if 0127 188 /** 128 189 Called periodically (once per frame) to drive sound engine. … … 130 191 void aud_update() 131 192 { 132 } 133 134 193 #if 0 194 static Millisec prev = 0; 195 Millisec now, diff; 196 now = osNow(); 197 diff = now - prev; 198 prev = now; 199 if( diff > (16 * 5) ) 200 printf( "%ld\n", diff ); 201 #endif 202 203 if( _musicSource ) 204 { 205 #if 0 206 ALint sourcestate; 207 alGetSourcei( _musicSource, AL_SOURCE_STATE, &sourcestate ); 208 if( sourcestate != AL_PLAYING ) 209 { 210 if( _bgStream ) 211 { 212 // The music source will stop playing when the buffers 213 // have been emptied. This can easily occur if the X11 window 214 // is moved vigorously around or a long load happens between 215 // audioUpdate() calls. 216 217 printf( "KR audioUpdate - _musicSource not playing!\n" ); 218 219 // For some reason this solution causes the stream quality 220 // to degrade (noise and echos occur). 221 _startMusic(); 222 } 223 else 224 { 225 aud_stopMusic(); 226 } 227 return; 228 } 229 #endif 230 231 if( _bgStream ) 232 { 233 ALint processed; 234 ALuint freeBuf; 235 236 alGetSourcei( _musicSource, AL_BUFFERS_PROCESSED, &processed ); 237 238 //if( processed ) printf( "KR proc %d\n", processed ); 239 240 while( processed ) 241 { 242 alSourceUnqueueBuffers( _musicSource, 1, &freeBuf ); 243 //printf( " buf %d\n", freeBuf ); 244 245 if( _readOgg( freeBuf, &_vf, _vinfo ) ) 246 { 247 alSourceQueueBuffers( _musicSource, 1, &freeBuf ); 248 } 249 else 250 { 251 printf( "KR audioUpdate - end of stream\n" ); 252 ov_clear( &_vf ); // Closes _bgStream for us. 253 _bgStream = 0; 254 break; 255 } 256 257 --processed; 258 } 259 } 260 } 261 } 262 263 264 #if 0 135 265 static int wav_sig( uint8_t* sig, int sigLen ) 136 266 { … … 284 414 void aud_playMusic( const char* file ) 285 415 { 286 #ifdef AL_FORMAT_VORBIS_EXT 287 if( _audioUp && _hasVorbis ) 288 { 289 int size; 290 void* fileBuf = 0; 291 int ok = 0; 292 293 //dprint( "osPlayMusic( %s )", file ); 416 if( _audioUp ) 417 { 418 //dprint( "aud_playMusic( %s )", file ); 294 419 295 420 aud_stopMusic(); 296 421 297 size = ur_fileSize( file ); 298 if( size > 0 ) 299 { 300 fileBuf = memAlloc( size ); 301 if( fileBuf ) 302 { 303 FileHandle fp = fileOpen( file, FILE_READ_BINARY ); 304 if( fp ) 305 { 306 if( fileRead( fp, fileBuf, size ) == (size_t) size ) 307 ok = 1; 308 fileClose( fp ); 309 } 310 } 311 } 312 313 if( ok ) 314 { 422 _bgStream = fopen( file, "rb" ); 423 if( ! _bgStream ) 424 return; 425 426 if( ov_open( _bgStream, &_vf, NULL, 0 ) < 0 ) 427 { 428 fclose( _bgStream ); 429 _bgStream = 0; 430 431 fprintf( stderr, "aud_playMusic - %s is not a valid Ogg file\n", 432 file ); 433 } 434 else 435 { 436 _vinfo = ov_info( &_vf, -1 ); 437 315 438 alGenSources( 1, &_musicSource ); 316 439 … … 323 446 alSourcef ( _musicSource, AL_GAIN, _musicGain ); 324 447 325 alBufferData( _musicBuffers[0], AL_FORMAT_VORBIS_EXT, fileBuf, 326 size, 44100 ); 327 alSourcei( _musicSource, AL_BUFFER, _musicBuffers[0] ); 328 329 alSourcePlay( _musicSource ); 330 } 331 else 332 { 333 dprint( "aud_playMusic - could not open %s\n", file ); 334 } 335 336 if( fileBuf ) 337 memFree( fileBuf ); 338 } 339 #endif 448 _startMusic(); 449 } 450 } 340 451 } 341 452 … … 350 461 alDeleteSources( 1, &_musicSource ); 351 462 _musicSource = 0; 463 } 464 465 if( _bgStream ) 466 { 467 ov_clear( &_vf ); // Closes _bgStream for us. 468 _bgStream = 0; 352 469 } 353 470 } branches/thune/thread_safe/gl/audio.h
r236 r423 27 27 extern int aud_startup(); 28 28 extern void aud_shutdown(); 29 extern void aud_update(); 29 30 extern int aud_playSound( UCell* ); 30 31 extern void aud_playMusic( const char* file ); branches/thune/thread_safe/gl/gx.c
r420 r423 653 653 //glClear( GL_DEPTH_BUFFER_BIT ); 654 654 //glClear( gEnv.state.clear ); 655 656 aud_update(); 655 657 656 658 UR_S_DROP; branches/thune/thread_safe/gl/project.r
r375 r423 54 54 55 55 libs [%freetype %bz2 %png] 56 libs [%openal %alut %vorbis ]56 libs [%openal %alut %vorbis %vorbisfile] 57 57 ] 58 58 win32 [ branches/thune/thread_safe/gl/scripts/view.t
r420 r423 167 167 168 168 169 ; Make Models 169 170 d7 do light-fb/texture :d7-shader/smap 170 171 ent do light-fb/texture :ent-shader/smap … … 216 217 217 218 0 0 move-light 219 220 ;"music/Carl_Leth_211.ogg" play 218 221 219 222 [ branches/thune/thread_safe/gl/test_fw.t
r420 r423 71 71 [ 72 72 wid wait-events 73 ;wid handle.events 73 ;wid handle.events ; Use this when audio enabled 74 74 75 75 sim-update
