src/sgp/SoundMan.cc
/* Loads a sound from a buffer into the cache, allocating memory and a slot for storage.
 *
 * Returns: The sample if successful, NULL otherwise. */
Loads a sound from a buffer into the cache, allocating memory and a slot for storage.
Returns: The sample if successful, NULL otherwise.
Loads a sound from a buffer into the cache allocating memory and a slot for storage . Returns : The sample if successful NULL otherwise .
static SAMPLETAG* SoundLoadBuffer(SDL_AudioFormat format, UINT8 channels, int freq, UINT8* buffer, UINT32 size)
{
	SDL_AudioCVT cvt;
	int ret;
	UINT8* sampledata = NULL;
	UINT32 samplesize = 0;
	UINT8  samplechannels;
	if (buffer == NULL || size == 0)
	{
		SLOGE("SoundLoadBuffer Error: buffer is empty - Buffer: %p, Size: %u", buffer, size);
		return NULL;
	}
	samplechannels = __min(channels, gTargetAudioSpec.channels);
	ret = SDL_BuildAudioCVT(&cvt, format, channels, freq, gTargetAudioSpec.format, samplechannels, gTargetAudioSpec.freq);
	if (ret == -1)
	{
		SLOGE("SoundLoadBuffer Error: unsupported audio conversion - %s", SDL_GetError());
		return NULL;
	}
	if (cvt.needed)
	{
		UINT32 bufsize = size * cvt.len_mult;
		UINT32 cvtsize = size * cvt.len_ratio;
		Assert(bufsize >= size);
		cvt.len = size;
		cvt.buf = MALLOCN(UINT8, bufsize);
		memcpy(cvt.buf, buffer, size);
		if (SDL_ConvertAudio(&cvt) == -1) {
			SLOGE("SoundLoadBuffer Error: error converting audio - %s", SDL_GetError());
			MemFree(cvt.buf);
			return NULL;
		}
		if (cvtsize == bufsize)
		{
			Assert(cvtsize == cvt.len_cvt);
			sampledata = cvt.buf;
			samplesize = cvtsize;
		}
		else
		{
			Assert(cvtsize < bufsize);
			sampledata = MALLOCN(UINT8, cvtsize);
			memcpy(sampledata, cvt.buf, cvtsize);
			samplesize = cvtsize;
			MemFree(cvt.buf);
		}
	}
	else
	{
		sampledata = MALLOCN(UINT8, size);
		memcpy(sampledata, buffer, size);
		samplesize = size;
	}
	while (samplesize + guiSoundMemoryUsed > guiSoundMemoryLimit)
	{
		if (!SoundCleanCache())
		{
			SLOGE("SoundLoadBuffer Error: not enough memory - Size: %u, Used: %u, Max: %u", samplesize, guiSoundMemoryUsed, guiSoundMemoryLimit);
			MemFree(sampledata);
			return NULL;
		}
	}
	SAMPLETAG* s = SoundGetEmptySample();
	if (s == NULL)
	{
		SoundCleanCache();
		s = SoundGetEmptySample();
	}
	if (s == NULL)
	{
		SLOGE("SoundLoadBuffer Error: sound channels are full");
		MemFree(sampledata);
		return NULL;
	}
	s->pData = sampledata;
	s->uiFlags |= SAMPLE_ALLOCATED;
	if (samplechannels != 1) {
		Assert(samplechannels == 2);
		s->uiFlags |= SAMPLE_STEREO;
	}
	s->n_samples = UINT32(samplesize / GetSampleSize(s));
	IncreaseSoundMemoryUsedBySample(s);
	return s;
}
static SAMPLETAG * SoundLoadBuffer ( SDL_AudioFormat format , UINT8 channels , int freq , UINT8 * buffer , UINT32 size ) { SDL_AudioCVT cvt ; int ret ; UINT8 * sampledata = NULL ; UINT32 samplesize = 0 ; UINT8 samplechannels ; if ( buffer == NULL || size == 0 ) { SLOGE ( " " , buffer , size ) ; return NULL ; } samplechannels = __min ( channels , gTargetAudioSpec . channels ) ; ret = SDL_BuildAudioCVT ( & cvt , format , channels , freq , gTargetAudioSpec . format , samplechannels , gTargetAudioSpec . freq ) ; if ( ret == -1 ) { SLOGE ( " " , SDL_GetError ( ) ) ; return NULL ; } if ( cvt . needed ) { UINT32 bufsize = size * cvt . len_mult ; UINT32 cvtsize = size * cvt . len_ratio ; Assert ( bufsize >= size ) ; cvt . len = size ; cvt . buf = MALLOCN ( UINT8 , bufsize ) ; memcpy ( cvt . buf , buffer , size ) ; if ( SDL_ConvertAudio ( & cvt ) == -1 ) { SLOGE ( " " , SDL_GetError ( ) ) ; MemFree ( cvt . buf ) ; return NULL ; } if ( cvtsize == bufsize ) { Assert ( cvtsize == cvt . len_cvt ) ; sampledata = cvt . buf ; samplesize = cvtsize ; } else { Assert ( cvtsize < bufsize ) ; sampledata = MALLOCN ( UINT8 , cvtsize ) ; memcpy ( sampledata , cvt . buf , cvtsize ) ; samplesize = cvtsize ; MemFree ( cvt . buf ) ; } } else { sampledata = MALLOCN ( UINT8 , size ) ; memcpy ( sampledata , buffer , size ) ; samplesize = size ; } while ( samplesize + guiSoundMemoryUsed > guiSoundMemoryLimit ) { if ( ! SoundCleanCache ( ) ) { SLOGE ( " " , samplesize , guiSoundMemoryUsed , guiSoundMemoryLimit ) ; MemFree ( sampledata ) ; return NULL ; } } SAMPLETAG * s = SoundGetEmptySample ( ) ; if ( s == NULL ) { SoundCleanCache ( ) ; s = SoundGetEmptySample ( ) ; } if ( s == NULL ) { SLOGE ( " " ) ; MemFree ( sampledata ) ; return NULL ; } s -> pData = sampledata ; s -> uiFlags |= SAMPLE_ALLOCATED ; if ( samplechannels != 1 ) { Assert ( samplechannels == 2 ) ; s -> uiFlags |= SAMPLE_STEREO ; } s -> n_samples = UINT32 ( samplesize / GetSampleSize ( s ) ) ; IncreaseSoundMemoryUsedBySample ( s ) ; return s ; }
Loads a sound from a buffer into the cache, allocating memory and a slot for storage.
Loads a sound from a buffer into the cache allocating memory and a slot for storage .
// if (cvtsize < bufsize) // if (!cvt.needed) // cvt is invalid from this point forward // if insufficient memory, start unloading old samples until either // there's nothing left to unload, or we fit // if all the sample slots are full, unloading one // if we still don't have a sample slot