Example #1
0
/*
======================
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;
			}
		}

	}
}
/*
=================
S_AL_MusicProcess
=================
*/
static
void S_AL_MusicProcess(ALuint b)
{
	ALenum error;
	int l;
	ALuint format;
	snd_stream_t *curstream;

	if(intro_stream)
		curstream = intro_stream;
	else
		curstream = mus_stream;

	if(!curstream)
		return;

	l = S_CodecReadStream(curstream, MUSIC_BUFFER_SIZE, decode_buffer);

	// Run out data to read, start at the beginning again
	if(l == 0)
	{
		S_CodecCloseStream(curstream);

		// the intro stream just finished playing so we don't need to reopen
		// the music stream.
		if(intro_stream)
			intro_stream = NULL;
		else
			mus_stream = S_CodecOpenStream(s_backgroundLoop);
		
		curstream = mus_stream;

		if(!curstream)
		{
			S_AL_StopBackgroundTrack();
			return;
		}

		l = S_CodecReadStream(curstream, MUSIC_BUFFER_SIZE, decode_buffer);
	}

	format = S_AL_Format(curstream->info.width, curstream->info.channels);

	if( l == 0 )
	{
		// We have no data to buffer, so buffer silence
		byte dummyData[ 2 ] = { 0 };

		qalBufferData( b, AL_FORMAT_MONO16, (void *)dummyData, 2, 22050 );
	}
	else
		qalBufferData(b, format, decode_buffer, l, curstream->info.rate);

	if( ( error = qalGetError( ) ) != AL_NO_ERROR )
	{
		S_AL_StopBackgroundTrack( );
		Com_Printf( S_COLOR_RED "ERROR: while buffering data for music stream - %s\n",
				S_AL_ErrorMsg( error ) );
		return;
	}
}
/*
======================
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;
			}
		}

	}
}
Example #4
0
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;
			}
		}
	}
}
Example #5
0
static void BGM_UpdateStream (void)
{
	int	res;	/* Number of bytes read. */
	int	bufferSamples;
	int	fileSamples;
	int	fileBytes;
	byte	raw[16384];

	if (bgmstream->status != STREAM_PLAY)
		return;

	/* don't bother playing anything if musicvolume is 0 */
	if (bgmvolume.value <= 0)
		return;

	/* see how many samples should be copied into the raw buffer */
	if (s_rawend < paintedtime)
		s_rawend = paintedtime;

	while (s_rawend < paintedtime + MAX_RAW_SAMPLES)
	{
		bufferSamples = MAX_RAW_SAMPLES - (s_rawend - paintedtime);

		/* decide how much data needs to be read from the file */
		fileSamples = bufferSamples * bgmstream->info.rate / shm->speed;
		if (!fileSamples)
			return;

		/* our max buffer size */
		fileBytes = fileSamples * (bgmstream->info.width * bgmstream->info.channels);
		if (fileBytes > (int) sizeof(raw))
		{
			fileBytes = (int) sizeof(raw);
			fileSamples = fileBytes /
					  (bgmstream->info.width * bgmstream->info.channels);
		}

		/* Read */
		res = S_CodecReadStream(bgmstream, fileBytes, raw);
		if (res < fileBytes)
		{
			fileBytes = res;
			fileSamples = res / (bgmstream->info.width * bgmstream->info.channels);
		}

		if (res > 0)	/* data: add to raw buffer */
		{
			S_RawSamples(fileSamples, bgmstream->info.rate,
							bgmstream->info.width,
							bgmstream->info.channels,
							raw, bgmvolume.value);
		}
		else if (res == 0)	/* EOF */
		{
			if (bgmloop)
			{
				if (S_CodecRewindStream(bgmstream) < 0)
				{
					BGM_Stop();
					return;
				}
			}
			else
			{
				BGM_Stop();
				return;
			}
		}
		else	/* res < 0: some read error */
		{
			Con_Printf("Stream read error (%i), stopping.\n", res);
			BGM_Stop();
			return;
		}
	}
}
Example #6
0
File: dma.c Project: icanhas/yantar
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;
            }
        }

    }
}