Exemple #1
0
	ErrorType SoundSource::LoadResource(const String &name,
		const SoundData &soundData, SoundCollection *collection)
	{
		// Check for existing sound with matching checksum
		Checksum checksum(soundData.GetSamples().GetData(),
			soundData.GetSamples().GetElementCount());
		Bind(collection, collection->GetIDByChecksum(checksum));
		if (-1 < mID)
			return kErrorNone;
		// No sound was found, generate new buffer
		UInt openALID;
		alGenBuffers(1, &openALID);
		if (!openALID)
			return Error::Throw(kErrorOpenALGenBuffersFailure,
				String("[%s(\"%s\", %p)]", FastFunctionName,
				name.GetCString(), &soundData));
		SoundResource soundResource;
		soundResource.SetName(name);
		soundResource.SetChecksum(checksum);
		soundResource.GetProperties()->SetFormat(soundData.GetProperties().GetFormat());
		soundResource.GetProperties()->SetFrequency(soundData.GetProperties().GetFrequency());
		soundResource.GetProperties()->SetSampleCount(soundData.GetProperties().GetSampleCount());
		soundResource.SetOpenALBufferID(openALID);
		soundResource.SetActive(true);
		mID = collection->AddResource(soundResource, this);
		mCollection = collection;
		{
			alBufferData(GetResource().GetOpenALBufferID(),
				GetResource().GetProperties().GetFormat(),
				soundData.GetSamples().GetData(),
				soundData.GetSamples().GetElementCount(),
				GetResource().GetProperties().GetFrequency());
			ALint alErr = alGetError();
			switch (alErr)
			{
			case AL_OUT_OF_MEMORY:
				Unbind();
				return Error::Throw(kErrorOutOfMemory,
					String("[%s(\"%s\", %p)]", FastFunctionName,
					name.GetCString(), &soundData));
			case AL_INVALID_ENUM:
			case AL_INVALID_VALUE:
				Unbind();
				return Error::Throw(kErrorInvalidValue,
					String("[%s(\"%s\", %p)]", FastFunctionName,
					name.GetCString(), &soundData));
			}
		}
		return kErrorNone;
	}