/* ================= S_WAV_CodecReadStream ================= */ int S_WAV_CodecReadStream(snd_stream_t *stream, int bytes, void *buffer) { int remaining = stream->info.size - stream->pos; int samples; if(remaining <= 0) return 0; if(bytes > remaining) bytes = remaining; stream->pos += bytes; samples = (bytes / stream->info.width) / stream->info.channels; FS_Read(buffer, bytes, stream->file); S_ByteSwapRawSamples(samples, stream->info.width, stream->info.channels, buffer); return bytes; }
/* ================= S_WAV_CodecLoad ================= */ void *S_WAV_CodecLoad(const char *filename, snd_info_t *info) { fileHandle_t file; void *buffer; // Try to open the file FS_FOpenFileRead(filename, &file, qtrue); if (!file) { if (com_developer->integer) { Com_Printf(S_COLOR_RED "ERROR: Could not open \"%s\"\n", filename); } return NULL; } // Read the RIFF header if (!S_ReadRIFFHeader(file, info)) { FS_FCloseFile(file); if (com_developer->integer) { Com_Printf(S_COLOR_RED "ERROR: Incorrect/unsupported format in \"%s\"\n", filename); } return NULL; } // Allocate some memory buffer = Z_Malloc(info->size); if (!buffer) { FS_FCloseFile(file); Com_Printf(S_COLOR_RED "ERROR: Out of memory reading \"%s\"\n", filename); return NULL; } // Read, byteswap FS_Read(buffer, info->size, file); S_ByteSwapRawSamples(info->samples, info->width, info->channels, (byte *)buffer); // Close and return FS_FCloseFile(file); return buffer; }
/* ====================== 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_backgroundFile ) { 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 * s_backgroundInfo.rate / dma.speed; // don't try and read past the end of the file if ( fileSamples > s_backgroundSamples ) { fileSamples = s_backgroundSamples; } // our max buffer size fileBytes = fileSamples * (s_backgroundInfo.width * s_backgroundInfo.channels); if ( fileBytes > sizeof(raw) ) { fileBytes = sizeof(raw); fileSamples = fileBytes / (s_backgroundInfo.width * s_backgroundInfo.channels); } r = Sys_StreamedRead( raw, 1, fileBytes, s_backgroundFile ); if ( r != fileBytes ) { Com_Printf("StreamedRead failure on music track\n"); S_StopBackgroundTrack(); return; } // byte swap if needed S_ByteSwapRawSamples( fileSamples, s_backgroundInfo.width, s_backgroundInfo.channels, raw ); // add to raw buffer S_RawSamples( fileSamples, s_backgroundInfo.rate, s_backgroundInfo.width, s_backgroundInfo.channels, raw, musicVolume ); s_backgroundSamples -= fileSamples; if ( !s_backgroundSamples ) { // loop if (s_backgroundLoop[0]) { Sys_EndStreamedFile( s_backgroundFile ); FS_FCloseFile( s_backgroundFile ); s_backgroundFile = 0; S_StartBackgroundTrack( s_backgroundLoop, s_backgroundLoop ); if ( !s_backgroundFile ) { return; // loop failed to restart } } else { s_backgroundFile = 0; return; } } } }