void al_mus_update( void ) { int processed; ALint state; if ( !mus_playing ) { return; } qalGetSourcei( source, AL_BUFFERS_PROCESSED, &processed ); if ( processed ) { while ( processed-- ) { ALuint b; qalSourceUnqueueBuffers( source, 1, &b ); al_mus_process( b ); qalSourceQueueBuffers( source, 1, &b ); } } // If it's not still playing, give it a kick qalGetSourcei( source, AL_SOURCE_STATE, &state ); if ( state == AL_STOPPED ) { si.Printf( PRINT_ALL, "Musical kick\n" ); qalSourcePlay( source ); } // Set the gain property qalSourcef( source, AL_GAIN, s_gain->value * s_musicVolume->value ); }
/* * Updates stream sources by removing all played * buffers and restarting playback if necessary. */ static void AL_StreamUpdate(void) { int numBuffers; ALint state; /* Un-queue any buffers, and delete them */ qalGetSourcei(streamSource, AL_BUFFERS_PROCESSED, &numBuffers); while (numBuffers--) { ALuint buffer; qalSourceUnqueueBuffers(streamSource, 1, &buffer); qalDeleteBuffers(1, &buffer); active_buffers--; } /* Start the streamSource playing if necessary */ qalGetSourcei(streamSource, AL_BUFFERS_QUEUED, &numBuffers); qalGetSourcei(streamSource, AL_SOURCE_STATE, &state); if (state == AL_STOPPED) { streamPlaying = false; } if (!streamPlaying && numBuffers) { qalSourcePlay(streamSource); streamPlaying = true; } }
/* ================= S_AL_MusicUpdate ================= */ static void S_AL_MusicUpdate( void ) { int numBuffers; ALint state; if(!musicPlaying) return; qalGetSourcei( musicSource, AL_BUFFERS_PROCESSED, &numBuffers ); while( numBuffers-- ) { ALuint b; qalSourceUnqueueBuffers(musicSource, 1, &b); S_AL_MusicProcess(b); qalSourceQueueBuffers(musicSource, 1, &b); } // Hitches can cause OpenAL to be starved of buffers when streaming. // If this happens, it will stop playback. This restarts the source if // it is no longer playing, and if there are buffers available qalGetSourcei( musicSource, AL_SOURCE_STATE, &state ); qalGetSourcei( musicSource, AL_BUFFERS_QUEUED, &numBuffers ); if( state == AL_STOPPED && numBuffers ) { Com_DPrintf( S_COLOR_YELLOW "Restarted OpenAL music\n" ); qalSourcePlay(musicSource); } // Set the gain property qalSourcef(musicSource, AL_GAIN, s_alGain->value * s_musicVolume->value); }
/* * S_StartSound */ static void S_StartSound( sfx_t *sfx, const vec3_t origin, int entNum, int channel, float fvol, float attenuation ) { src_t *src; if( !sfx ) return; src = S_AllocSource( SRCPRI_ONESHOT, entNum, channel ); if( !src ) return; source_setup( src, sfx, SRCPRI_ONESHOT, entNum, channel, fvol, attenuation ); if( src->attenuation ) { if( origin ) VectorCopy( origin, src->origin ); else src->isTracking = qtrue; } source_spatialize( src ); qalSourcePlay( src->source ); }
void AL_PlayChannel(channel_t *ch) { sfxcache_t *sc = ch->sfx->cache; #ifdef _DEBUG if (s_show->integer > 1) Com_Printf("%s: %s\n", __func__, ch->sfx->name); #endif ch->srcnum = s_srcnums[ch - channels]; qalGetError(); qalSourcei(ch->srcnum, AL_BUFFER, sc->bufnum); //qalSourcei(ch->srcnum, AL_LOOPING, sc->loopstart == -1 ? AL_FALSE : AL_TRUE); qalSourcei(ch->srcnum, AL_LOOPING, ch->autosound ? AL_TRUE : AL_FALSE); qalSourcef(ch->srcnum, AL_GAIN, ch->master_vol); qalSourcef(ch->srcnum, AL_REFERENCE_DISTANCE, SOUND_FULLVOLUME); qalSourcef(ch->srcnum, AL_MAX_DISTANCE, 8192); qalSourcef(ch->srcnum, AL_ROLLOFF_FACTOR, ch->dist_mult * (8192 - SOUND_FULLVOLUME)); AL_Spatialize(ch); // play it qalSourcePlay(ch->srcnum); if (qalGetError() != AL_NO_ERROR) { AL_StopChannel(ch); } }
/* * source_loop */ static void source_loop( int priority, sfx_t *sfx, int entNum, float fvol, float attenuation ) { src_t *src; qboolean new_source = qfalse; if( !sfx ) return; if( entNum < 0 ) return; // Do we need to start a new sound playing? if( !entlist[entNum].src ) { src = S_AllocSource( priority, entNum, 0 ); if( !src ) return; new_source = qtrue; } else if( entlist[entNum].src->sfx != sfx ) { // Need to restart. Just re-use this channel src = entlist[entNum].src; source_kill( src ); new_source = qtrue; } else { src = entlist[entNum].src; } if( new_source ) { source_setup( src, sfx, priority, entNum, -1, fvol, attenuation ); qalSourcei( src->source, AL_LOOPING, AL_TRUE ); src->isLooping = qtrue; entlist[entNum].src = src; } qalSourcef( src->source, AL_GAIN, src->fvol * s_volume->value ); qalSourcef( src->source, AL_REFERENCE_DISTANCE, s_attenuation_refdistance ); qalSourcef( src->source, AL_MAX_DISTANCE, s_attenuation_maxdistance ); qalSourcef( src->source, AL_ROLLOFF_FACTOR, attenuation ); if( new_source ) { if( src->attenuation ) src->isTracking = qtrue; source_spatialize( src ); qalSourcePlay( src->source ); } entlist[entNum].touched = qtrue; }
/** * Background music playback */ void SndAl_StartBackgroundTrack( const char *intro, const char *loop ) { int i; // Stop any existing music that might be playing SndAl_StopBackgroundTrack(); if ( !intro || !intro[ 0 ] ) { intro = loop; } if ( !loop || !loop[ 0 ] ) { loop = intro; } if ( ( !intro || !intro[ 0 ] ) && ( !intro || !intro[ 0 ] ) ) { return; } // Copy the loop over strncpy( s_backgroundLoop, loop, sizeof( s_backgroundLoop ) ); // Open the intro mus_stream = si.StreamOpen( intro ); if ( !mus_stream ) { return; } // Allocate a source al_mus_source_get(); if ( source_handle == -1 ) { return; } // Generate the buffers qalGenBuffers( BUFFERS, buffers ); // Queue the buffers up for ( i = 0; i < BUFFERS; i++ ) { al_mus_process( buffers[ i ] ); } qalSourceQueueBuffers( source, BUFFERS, buffers ); // Start playing qalSourcePlay( source ); mus_playing = qtrue; }
/* * Plays a channel (in the frontends * sense) with OpenAL. */ void AL_PlayChannel(channel_t *ch) { sfxcache_t *sc; float vol; /* Debug */ if (s_show->value > 1) { Com_Printf("%s: %s\n", __func__, ch->sfx->name); } /* Clamp volume */ vol = ch->oal_vol; if (vol > 1.0f) { vol = 1.0f; } sc = ch->sfx->cache; ch->srcnum = s_srcnums[ch - channels]; qalGetError(); qalSourcef(ch->srcnum, AL_REFERENCE_DISTANCE, SOUND_FULLVOLUME); qalSourcef(ch->srcnum, AL_MAX_DISTANCE, 8192); qalSourcef(ch->srcnum, AL_ROLLOFF_FACTOR, ch->dist_mult * (8192 - SOUND_FULLVOLUME)); qalSourcef(ch->srcnum, AL_GAIN, vol); qalSourcei(ch->srcnum, AL_BUFFER, sc->bufnum); qalSourcei(ch->srcnum, AL_LOOPING, ch->autosound ? AL_TRUE : AL_FALSE); if ((ch->entnum == -1) || (ch->entnum == cl.playernum + 1) || !ch->dist_mult) { /* anything coming from the view entity will always * be full volume and at the listeners position */ qalSource3f(ch->srcnum, AL_POSITION, 0.0f, 0.0f, 0.0f); qalSourcei(ch->srcnum, AL_SOURCE_RELATIVE, AL_TRUE); } else { /* all other sources are *not* relative */ qalSourcei(ch->srcnum, AL_SOURCE_RELATIVE, AL_FALSE); } /* Spatialize it */ AL_Spatialize(ch); /* Play it */ qalSourcePlay(ch->srcnum); /* Do not play broken channels */ if (qalGetError() != AL_NO_ERROR) { AL_StopChannel(ch); } }
/* ================= S_StreamRawSamples Cinematic streaming ================= */ void S_StreamRawSamples (const byte *data, int samples, int rate, int width, int channels) { int processed, state, size; unsigned format, buffer; if (!s_initialized) return; if (!s_streamingChannel) return; // Unqueue and delete any processed buffers qalGetSourcei(s_streamingChannel->sourceNum, AL_BUFFERS_PROCESSED, &processed); if (processed > 0){ while (processed--){ qalSourceUnqueueBuffers(s_streamingChannel->sourceNum, 1, &buffer); qalDeleteBuffers(1, &buffer); } } // Calculate buffer size size = samples * width * channels; // Set buffer format if (width == 2) { if (channels == 2) format = AL_FORMAT_STEREO16; else format = AL_FORMAT_MONO16; } else { if (channels == 2) format = AL_FORMAT_STEREO8; else format = AL_FORMAT_MONO8; } // Upload and queue the new buffer qalGenBuffers(1, &buffer); qalBufferData(buffer, format, (byte *)data, size, rate); qalSourceQueueBuffers(s_streamingChannel->sourceNum, 1, &buffer); // Update volume qalSourcef(s_streamingChannel->sourceNum, AL_GAIN, s_sfxVolume->value); // If not playing, then do so qalGetSourcei(s_streamingChannel->sourceNum, AL_SOURCE_STATE, &state); if (state != AL_PLAYING) qalSourcePlay(s_streamingChannel->sourceNum); }
/* * S_StartLocalSound */ void S_StartLocalSound( const char *name ) { sfx_t *sfx; src_t *src; src = S_AllocSource( SRCPRI_LOCAL, -1, S_CHANNEL_AUTO ); if( !src ) return; sfx = S_RegisterSound( name ); if( !sfx ) return; source_setup( src, sfx, SRCPRI_LOCAL, -1, S_CHANNEL_AUTO, 1.0, ATTN_NONE ); qalSourcei( src->source, AL_SOURCE_RELATIVE, AL_TRUE ); qalSourcePlay( src->source ); }
/* ================= S_AL_StartSound Play a one-shot sound effect ================= */ static void S_AL_StartSound( vec3_t origin, int entnum, int entchannel, sfxHandle_t sfx ) { vec3_t sorigin; srcHandle_t src; if(S_AL_CheckInput(origin ? 0 : entnum, sfx)) return; // Try to grab a source src = S_AL_SrcAlloc(SRCPRI_ONESHOT, entnum, entchannel); if(src == -1) return; // Set up the effect if( origin == NULL ) { if( S_AL_HearingThroughEntity( entnum ) ) { // Where the entity is the local player, play a local sound S_AL_SrcSetup( src, sfx, SRCPRI_ONESHOT, entnum, entchannel, qtrue ); VectorClear( sorigin ); } else { S_AL_SrcSetup( src, sfx, SRCPRI_ONESHOT, entnum, entchannel, qfalse ); VectorCopy( entityList[ entnum ].origin, sorigin ); } srcList[ src ].isTracking = qtrue; } else { S_AL_SrcSetup( src, sfx, SRCPRI_ONESHOT, entnum, entchannel, qfalse ); VectorCopy( origin, sorigin ); } S_AL_SanitiseVector( sorigin ); qalSourcefv( srcList[ src ].alSource, AL_POSITION, sorigin ); S_AL_ScaleGain(&srcList[src], sorigin); // Start it playing qalSourcePlay(srcList[src].alSource); }
/* * Plays a channel (in the frontends * sense) with OpenAL. */ void AL_PlayChannel(channel_t *ch) { sfxcache_t *sc; float vol; /* Debug */ if (s_show->value > 1) { Com_Printf("%s: %s\n", __func__, ch->sfx->name); } /* Clamp volume */ vol = ch->oal_vol + s_volume->value; if (vol > 1.0f) { vol = 1.0f; } sc = ch->sfx->cache; ch->srcnum = s_srcnums[ch - channels]; qalGetError(); qalSourcef(ch->srcnum, AL_REFERENCE_DISTANCE, SOUND_FULLVOLUME); qalSourcef(ch->srcnum, AL_MAX_DISTANCE, 8192); qalSourcef(ch->srcnum, AL_ROLLOFF_FACTOR, ch->dist_mult * (8192 - SOUND_FULLVOLUME)); qalSourcef(ch->srcnum, AL_GAIN, vol); qalSourcei(ch->srcnum, AL_BUFFER, sc->bufnum); qalSourcei(ch->srcnum, AL_LOOPING, ch->autosound ? AL_TRUE : AL_FALSE); /* Spatialize it */ AL_Spatialize(ch); /* Play it */ qalSourcePlay(ch->srcnum); /* Do not play broken channels */ if (qalGetError() != AL_NO_ERROR) { AL_StopChannel(ch); } }
/* ================= S_AL_StartLocalSound Play a local (non-spatialized) sound effect ================= */ static void S_AL_StartLocalSound(sfxHandle_t sfx, int channel) { srcHandle_t src; if(S_AL_CheckInput(0, sfx)) return; // Try to grab a source src = S_AL_SrcAlloc(SRCPRI_LOCAL, -1, channel); if(src == -1) return; // Set up the effect S_AL_SrcSetup(src, sfx, SRCPRI_LOCAL, -1, channel, qtrue); // Start it playing qalSourcePlay(srcList[src].alSource); }
/* ================= S_AL_StreamUpdate ================= */ static void S_AL_StreamUpdate( int stream ) { int numBuffers; ALint state; if ((stream < 0) || (stream >= MAX_RAW_STREAMS)) return; if(streamSourceHandles[stream] == -1) return; // Un-queue any buffers, and delete them qalGetSourcei( streamSources[stream], AL_BUFFERS_PROCESSED, &numBuffers ); while( numBuffers-- ) { ALuint buffer; qalSourceUnqueueBuffers(streamSources[stream], 1, &buffer); qalDeleteBuffers(1, &buffer); } // Start the streamSource playing if necessary qalGetSourcei( streamSources[stream], AL_BUFFERS_QUEUED, &numBuffers ); qalGetSourcei(streamSources[stream], AL_SOURCE_STATE, &state); if(state == AL_STOPPED) { streamPlaying[stream] = qfalse; // If there are no buffers queued up, release the streamSource if( !numBuffers ) S_AL_FreeStreamChannel( stream ); } if( !streamPlaying[stream] && numBuffers ) { qalSourcePlay( streamSources[stream] ); streamPlaying[stream] = qtrue; } }
void S_UpdateMusic( void ) { int i, processed; ALint state; ALenum error; ALuint b; int num_processed_buffers; ALuint processed_buffers[MUSIC_BUFFERS]; if( !s_bgTrack ) return; if( !s_musicvolume->value && !s_bgTrack->isUrl ) return; if( s_bgTrackPaused || s_bgTrackLocked ) return; if( s_bgTrackBuffering ) { if( S_EoStream( s_bgTrack->stream ) ) { // we should now advance to the next track S_CloseMusicTrack( s_bgTrack ); } else { if( S_FTellSteam( s_bgTrack->stream ) < BACKGROUND_TRACK_BUFFERING_SIZE ) return; // in case we delayed openening to let the stream be cached for a while, // start actually reading from it now if( !S_ContOpenStream( s_bgTrack->stream ) ) { // let music_process do the dirty job of advancing to the next track S_CloseMusicTrack( s_bgTrack ); s_bgTrack->ignore = qtrue; } } s_bgTrackBuffering = qfalse; } if( !queued_buffers ) { // if we haven't queued any buffers yet, do it now num_processed_buffers = MUSIC_BUFFERS; memcpy( processed_buffers, buffers, sizeof( buffers ) ); } else { num_processed_buffers = 0; processed = 0; qalGetSourcei( source, AL_BUFFERS_PROCESSED, &processed ); while( processed-- ) { qalSourceUnqueueBuffers( source, 1, &b ); processed_buffers[num_processed_buffers++] = b; } } for( i = 0; i < num_processed_buffers; i++ ) { b = processed_buffers[i]; if( !music_process( b ) ) { Com_Printf( "Error processing music data\n" ); S_StopBackgroundTrack(); return; } qalSourceQueueBuffers( source, 1, &b ); if( ( error = qalGetError() ) != AL_NO_ERROR ) { Com_Printf( "Couldn't queue music data (%s)\n", S_ErrorMessage( error ) ); S_StopBackgroundTrack(); return; } } // If it's not still playing, give it a kick qalGetSourcei( source, AL_SOURCE_STATE, &state ); if( !queued_buffers || state != AL_PLAYING ) { queued_buffers = qtrue; qalSourcePlay( source ); } if( s_musicvolume->modified ) qalSourcef( source, AL_GAIN, s_musicvolume->value ); }
/* ================= S_AL_StartBackgroundTrack ================= */ static void S_AL_StartBackgroundTrack( const char *intro, const char *loop ) { int i; qboolean issame; // Stop any existing music that might be playing S_AL_StopBackgroundTrack(); if((!intro || !*intro) && (!loop || !*loop)) return; // Allocate a musicSource S_AL_MusicSourceGet(); if(musicSourceHandle == -1) return; if (!loop || !*loop) { loop = intro; issame = qtrue; } else if(intro && *intro && !strcmp(intro, loop)) issame = qtrue; else issame = qfalse; // Copy the loop over strncpy( s_backgroundLoop, loop, sizeof( s_backgroundLoop ) ); if(!issame) { // Open the intro and don't mind whether it succeeds. // The important part is the loop. intro_stream = S_CodecOpenStream(intro); } else intro_stream = NULL; mus_stream = S_CodecOpenStream(s_backgroundLoop); if(!mus_stream) { S_AL_CloseMusicFiles(); S_AL_MusicSourceFree(); return; } // Generate the musicBuffers qalGenBuffers(NUM_MUSIC_BUFFERS, musicBuffers); // Queue the musicBuffers up for(i = 0; i < NUM_MUSIC_BUFFERS; i++) { S_AL_MusicProcess(musicBuffers[i]); } qalSourceQueueBuffers(musicSource, NUM_MUSIC_BUFFERS, musicBuffers); // Set the initial gain property qalSourcef(musicSource, AL_GAIN, s_alGain->value * s_musicVolume->value); // Start playing qalSourcePlay(musicSource); musicPlaying = qtrue; }
/* ================= S_AL_SrcUpdate Update state (move things around, manage sources, and so on) ================= */ static void S_AL_SrcUpdate( void ) { int i; int entityNum; ALint state; src_t *curSource; for(i = 0; i < srcCount; i++) { entityNum = srcList[i].entity; curSource = &srcList[i]; if(curSource->isLocked) continue; if(!curSource->isActive) continue; // Update source parameters if((s_alGain->modified)||(s_volume->modified)) curSource->curGain = s_alGain->value * s_volume->value; if((s_alRolloff->modified)&&(!curSource->local)) qalSourcef(curSource->alSource, AL_ROLLOFF_FACTOR, s_alRolloff->value); if(s_alMinDistance->modified) qalSourcef(curSource->alSource, AL_REFERENCE_DISTANCE, s_alMinDistance->value); if(curSource->isLooping) { sentity_t *sent = &entityList[ entityNum ]; // If a looping effect hasn't been touched this frame, kill it if(sent->loopAddedThisFrame) { // The sound has changed without an intervening removal if(curSource->isActive && !sent->startLoopingSound && curSource->sfx != sent->loopSfx) { qalSourceStop(curSource->alSource); qalSourcei(curSource->alSource, AL_BUFFER, 0); sent->startLoopingSound = qtrue; } // The sound hasn't been started yet if(sent->startLoopingSound) { S_AL_SrcSetup(i, sent->loopSfx, sent->loopPriority, entityNum, -1, curSource->local); curSource->isLooping = qtrue; qalSourcei(curSource->alSource, AL_LOOPING, AL_TRUE); qalSourcePlay(curSource->alSource); sent->startLoopingSound = qfalse; } // Update locality if(curSource->local) { qalSourcei(curSource->alSource, AL_SOURCE_RELATIVE, AL_TRUE); qalSourcef(curSource->alSource, AL_ROLLOFF_FACTOR, 0.0f); } else { qalSourcei(curSource->alSource, AL_SOURCE_RELATIVE, AL_FALSE); qalSourcef(curSource->alSource, AL_ROLLOFF_FACTOR, s_alRolloff->value); } } else S_AL_SrcKill( i ); continue; } // Check if it's done, and flag it qalGetSourcei(curSource->alSource, AL_SOURCE_STATE, &state); if(state == AL_STOPPED) { S_AL_SrcKill(i); continue; } // Query relativity of source, don't move if it's true qalGetSourcei(curSource->alSource, AL_SOURCE_RELATIVE, &state); // See if it needs to be moved if(curSource->isTracking && !state) { qalSourcefv(curSource->alSource, AL_POSITION, entityList[entityNum].origin); S_AL_ScaleGain(curSource, entityList[entityNum].origin); } } }
void SndAl_RawSamples( int stream, int samples, int rate, int width, int channels, const byte *data, float volume, int entityNum ) { ALuint buffer; ALuint format = AL_FORMAT_STEREO16; ALint state; // Work out AL format if ( width == 1 ) { if ( channels == 1 ) { format = AL_FORMAT_MONO8; } else if ( channels == 2 ) { format = AL_FORMAT_STEREO8; } } else if ( width == 2 ) { if ( channels == 1 ) { format = AL_FORMAT_MONO16; } else if ( channels == 2 ) { format = AL_FORMAT_STEREO16; } } // Create the source if necessary if ( source_handle == -1 ) { allocate_channel(); // Failed? if ( source_handle == -1 ) { si.Printf( PRINT_ALL, "Can't allocate streaming source\n" ); return; } } // Create a buffer, and stuff the data into it qalGenBuffers( 1, &buffer ); qalBufferData( buffer, format, data, ( samples * width * channels ), rate ); // Shove the data onto the source qalSourceQueueBuffers( source, 1, &buffer ); // Start the source playing if necessary qalGetSourcei( source, AL_SOURCE_STATE, &state ); // Volume qalSourcef( source, AL_GAIN, volume * s_volume->value * s_gain->value ); if ( !is_playing ) { qalSourcePlay( source ); is_playing = qtrue; } }