Changeset 423

Show
Ignore:
Timestamp:
07/02/07 01:53:18 (1 year ago)
Author:
krobillard
Message:

gl/audio - Music is now streamed rather than using AL_EXT_vorbis.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/thune/thread_safe/gl/audio.c

    r390 r423  
    22// 
    33// Thune OpenAL Module 
    4 // Copyright (C) 2005-2006  Karl Robillard 
     4// Copyright (C) 2005-2007  Karl Robillard 
    55// 
    66//  This library is free software; you can redistribute it and/or 
     
    3535#include <AL/alc.h> 
    3636#include <AL/alut.h> 
    37 #include <AL/alext.h> 
    38 #endif 
     37#endif 
     38#include <vorbis/codec.h> 
     39#include <vorbis/vorbisfile.h> 
    3940#include "os.h" 
    4041#include "urlan.h" 
     
    6162 
    6263 
    63 #define MUSIC_BUFFERS       1 
    64 static int     _hasVorbis = 0; 
     64#define MUSIC_BUFFERS       2 
    6565static ALuint  _musicSource = 0; 
    6666static ALfloat _musicGain = 1.0f; 
    6767static ALuint  _musicBuffers[ MUSIC_BUFFERS ]; 
     68 
     69static FILE* _bgStream = 0; 
     70static OggVorbis_File _vf; 
     71static 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) 
     79static char _oggPCM[ OGG_BUFFER_SIZE ]; 
     80 
     81/* 
     82  Returns false if all data has been read or an error occurs. 
     83*/ 
     84static 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 
     115static 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} 
    68135 
    69136 
     
    91158    _audioUp = 1; 
    92159 
    93     if( alIsExtensionPresent( "AL_EXT_vorbis" ) ) 
    94         _hasVorbis = 1; 
    95     else 
    96         dprint( "OpenAL AL_EXT_vorbis not present - music disabled\n" ); 
    97  
    98160    return 1; 
    99161} 
     
    124186 
    125187 
    126 #if 0 
    127188/** 
    128189  Called periodically (once per frame) to drive sound engine. 
     
    130191void aud_update() 
    131192{ 
    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 
    135265static int wav_sig( uint8_t* sig, int sigLen ) 
    136266{ 
     
    284414void aud_playMusic( const char* file ) 
    285415{ 
    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 ); 
    294419 
    295420        aud_stopMusic(); 
    296421 
    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 
    315438            alGenSources( 1, &_musicSource ); 
    316439 
     
    323446            alSourcef ( _musicSource, AL_GAIN, _musicGain ); 
    324447 
    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    } 
    340451} 
    341452 
     
    350461            alDeleteSources( 1, &_musicSource ); 
    351462            _musicSource = 0; 
     463        } 
     464 
     465        if( _bgStream ) 
     466        { 
     467            ov_clear( &_vf );   // Closes _bgStream for us. 
     468            _bgStream = 0; 
    352469        } 
    353470    } 
  • branches/thune/thread_safe/gl/audio.h

    r236 r423  
    2727extern int  aud_startup(); 
    2828extern void aud_shutdown(); 
     29extern void aud_update(); 
    2930extern int  aud_playSound( UCell* ); 
    3031extern void aud_playMusic( const char* file ); 
  • branches/thune/thread_safe/gl/gx.c

    r420 r423  
    653653    //glClear( GL_DEPTH_BUFFER_BIT ); 
    654654    //glClear( gEnv.state.clear ); 
     655 
     656    aud_update(); 
    655657 
    656658    UR_S_DROP; 
  • branches/thune/thread_safe/gl/project.r

    r375 r423  
    5454 
    5555        libs [%freetype %bz2 %png] 
    56         libs [%openal %alut %vorbis
     56        libs [%openal %alut %vorbis %vorbisfile
    5757    ] 
    5858    win32 [ 
  • branches/thune/thread_safe/gl/scripts/view.t

    r420 r423  
    167167 
    168168 
     169; Make Models 
    169170d7  do  light-fb/texture :d7-shader/smap 
    170171ent do  light-fb/texture :ent-shader/smap 
     
    216217 
    2172180 0 move-light 
     219 
     220;"music/Carl_Leth_211.ogg" play 
    218221 
    219222[ 
  • branches/thune/thread_safe/gl/test_fw.t

    r420 r423  
    7171    [ 
    7272        wid wait-events 
    73         ;wid handle.events 
     73        ;wid handle.events   ; Use this when audio enabled 
    7474 
    7575        sim-update