/* ====================== S_UpdateBackgroundTrack ====================== */ void S_UpdateBackgroundTrack( void ) { int bufferSamples; int fileSamples; byte raw[30000]; // just enough to fit in a mac stack frame int fileBytes; int r; if(!s_backgroundStream) { return; } // don't bother playing anything if musicvolume is 0 if ( s_musicVolume->value <= 0 ) { return; } // see how many samples should be copied into the raw buffer if ( s_rawend[0] < s_soundtime ) { s_rawend[0] = s_soundtime; } while ( s_rawend[0] < s_soundtime + MAX_RAW_SAMPLES ) { bufferSamples = MAX_RAW_SAMPLES - (s_rawend[0] - s_soundtime); // decide how much data needs to be read from the file fileSamples = bufferSamples * s_backgroundStream->info.rate / dma.speed; if (!fileSamples) return; // our max buffer size fileBytes = fileSamples * (s_backgroundStream->info.width * s_backgroundStream->info.channels); if ( fileBytes > sizeof(raw) ) { fileBytes = sizeof(raw); fileSamples = fileBytes / (s_backgroundStream->info.width * s_backgroundStream->info.channels); } // Read r = S_CodecReadStream(s_backgroundStream, fileBytes, raw); if(r < fileBytes) { fileBytes = r; fileSamples = r / (s_backgroundStream->info.width * s_backgroundStream->info.channels); } if(r > 0) { // add to raw buffer S_Base_RawSamples(0, fileSamples, s_backgroundStream->info.rate, s_backgroundStream->info.width, s_backgroundStream->info.channels, raw, s_musicVolume->value, -1); } else { // loop if(s_backgroundLoop[0]) { S_CodecCloseStream(s_backgroundStream); s_backgroundStream = NULL; S_Base_StartBackgroundTrack( s_backgroundLoop, s_backgroundLoop ); if(!s_backgroundStream) return; } else { S_Base_StopBackgroundTrack(); return; } } } }
void S_UpdateStreamingSounds(void) { int bufferSamples; int fileSamples; byte raw[30000]; // just enough to fit in a mac stack frame int fileBytes; int rs; int i, j; float lvol, rvol; float streamingVol; streamingSound_t *ss; for (i = 0; i < MAX_STREAMING_SOUNDS; i++) { ss = &streamingSounds[i]; if (!ss->stream) { continue; } // don't bother playing anything if musicvolume is 0 if (i == 0 && s_musicVolume->value <= 0.0f) { continue; } // get the raw stream index j = RAW_STREAM(i); // see how many samples should be copied into the raw buffer if (s_rawend[j] < s_soundtime) { s_rawend[j] = s_soundtime; } while (s_rawend[j] < s_soundtime + MAX_RAW_SAMPLES) { bufferSamples = MAX_RAW_SAMPLES - (s_rawend[j] - s_soundtime); // decide how much data needs to be read from the file fileSamples = bufferSamples * ss->stream->info.rate / dma.speed; if (!fileSamples) { break; } // our max buffer size fileBytes = fileSamples * (ss->stream->info.width * ss->stream->info.channels); if (fileBytes > sizeof(raw)) { fileBytes = sizeof(raw); fileSamples = fileBytes / (ss->stream->info.width * ss->stream->info.channels); } // read stream rs = S_CodecReadStream(ss->stream, fileBytes, raw); if (rs < fileBytes) { fileSamples = rs / (ss->stream->info.width * ss->stream->info.channels); } // calculate the streaming volume based on stream fade and global fade streamingVol = S_GetStreamingFade(ss); // stop the stream if we had faded out of existence if (streamingVol == 0.0f) { S_StopStreamingSound(i); break; } streamingVol *= s_volCurrent; if (rs > 0) { if (i == 0) { lvol = rvol = s_musicVolume->value * streamingVol; } else { // attenuate if required if (ss->entnum >= 0 && ss->attenuation) { int r, l; S_SpatializeOrigin(entityPositions[ss->entnum], s_volume->value * 255.0f, &l, &r, SOUND_RANGE_DEFAULT, qfalse); if ((lvol = ((float)l / 255.0f)) > 1.0f) { lvol = 1.0f; } if ((rvol = ((float)r / 255.0f)) > 1.0f) { rvol = 1.0f; } lvol *= streamingVol; rvol *= streamingVol; } else { lvol = rvol = streamingVol; } } // add to raw buffer S_Base_RawSamples(j, fileSamples, ss->stream->info.rate, ss->stream->info.width, ss->stream->info.channels, raw, lvol, rvol); } else { if (s_debugStreams->integer) { Com_Printf("STREAM %d: ending...\n", i); Com_Printf("Queue stream: %s\nLoopStream: %s\n", ss->queueStream, ss->loopStream); } // special case for music track queue if (i == 0 && ss->queueStreamType && *ss->queueStream) { switch (ss->queueStreamType) { case QUEUED_PLAY_ONCE_SILENT: break; case QUEUED_PLAY_ONCE: S_StartBackgroundTrack(ss->queueStream, ss->name, 0); break; case QUEUED_PLAY_LOOPED: S_StartBackgroundTrack(ss->queueStream, ss->queueStream, 0); break; } // queue is done, clear it ss->queueStream[0] = '\0'; ss->queueStreamType = 0; break; } // loop if (*ss->loopStream) { // TODO: Implement a rewind? char loopStream[MAX_QPATH]; Q_strncpyz(loopStream, ss->loopStream, MAX_QPATH); S_StartStreamingSoundEx(loopStream, loopStream, ss->entnum, ss->channel, i == 0, i == 0 ? 0 : ss->attenuation); } else { S_FreeStreamingSound(i); } break; } } } }
/* ====================== S_UpdateBackgroundTrack ====================== */ void S_UpdateBackgroundTrack( void ) { int bufferSamples; int fileSamples; byte raw[30000]; // just enough to fit in a mac stack frame int fileBytes; int r; static float musicVolume = 0.5f; if(!s_backgroundStream) { return; } // graeme see if this is OK musicVolume = (musicVolume + (s_musicVolume->value * 2))/4.0f; // don't bother playing anything if musicvolume is 0 if ( musicVolume <= 0 ) { return; } // see how many samples should be copied into the raw buffer if ( s_rawend < s_soundtime ) { s_rawend = s_soundtime; } while ( s_rawend < s_soundtime + MAX_RAW_SAMPLES ) { bufferSamples = MAX_RAW_SAMPLES - (s_rawend - s_soundtime); // decide how much data needs to be read from the file fileSamples = (bufferSamples * dma.speed) / s_backgroundStream->info.rate; // our max buffer size fileBytes = fileSamples * (s_backgroundStream->info.width * s_backgroundStream->info.channels); if ( fileBytes > sizeof(raw) ) { fileBytes = sizeof(raw); fileSamples = fileBytes / (s_backgroundStream->info.width * s_backgroundStream->info.channels); } // Read r = S_CodecReadStream(s_backgroundStream, fileBytes, raw); if(r < fileBytes) { fileBytes = r; fileSamples = r / (s_backgroundStream->info.width * s_backgroundStream->info.channels); } if(r > 0) { // add to raw buffer S_Base_RawSamples( fileSamples, s_backgroundStream->info.rate, s_backgroundStream->info.width, s_backgroundStream->info.channels, raw, musicVolume ); } else { // loop if(s_backgroundLoop[0]) { S_OpenBackgroundStream( s_backgroundLoop ); if(!s_backgroundStream) return; } else { S_Base_StopBackgroundTrack(); return; } } } }
void sndupdatebackgroundtrack(void) { int bufferSamples; int fileSamples; byte raw[30000]; /* just enough to fit in a mac stack frame */ int fileBytes; int r; if(!s_backgroundStream) return; if(s_musicVolume->value <= 0) return; /* see how many samples should be copied into the raw buffer */ if(s_rawend[0] < s_soundtime) s_rawend[0] = s_soundtime; while(s_rawend[0] < s_soundtime + MAX_RAW_SAMPLES) { bufferSamples = MAX_RAW_SAMPLES - (s_rawend[0] - s_soundtime); /* decide how much data needs to be read from the file */ fileSamples = bufferSamples * s_backgroundStream->info.rate / dma.speed; if(!fileSamples) return; /* our max buffer size */ fileBytes = fileSamples * (s_backgroundStream->info.width * s_backgroundStream->info.channels); if(fileBytes > sizeof(raw)) { fileBytes = sizeof(raw); fileSamples = fileBytes / (s_backgroundStream->info.width * s_backgroundStream->info.channels); } r = S_CodecReadStream(s_backgroundStream, fileBytes, raw); if(r < fileBytes) { fileBytes = r; fileSamples = r / (s_backgroundStream->info.width * s_backgroundStream->info.channels); } if(r > 0) /* add to raw buffer */ S_Base_RawSamples(0, fileSamples, s_backgroundStream->info.rate, s_backgroundStream->info.width, s_backgroundStream->info.channels, raw, s_musicVolume->value, -1); else { /* loop */ if(s_backgroundLoop[0]) { S_CodecCloseStream(s_backgroundStream); s_backgroundStream = NULL; S_Base_StartBackgroundTrack(s_backgroundLoop, s_backgroundLoop); if(!s_backgroundStream) return; } else { S_Base_StopBackgroundTrack(); return; } } } }