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;
			}
		}

	}
}
Example #2
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;
            }
        }

    }
}