Parcourir la source

Fixing 24-bit audio clip import
Fixing decompressed audio playback
Fixing audio clip cleanup

BearishSun il y a 9 ans
Parent
commit
e02f45f2f0

+ 9 - 13
Source/BansheeCore/Source/BsAudioUtility.cpp

@@ -39,23 +39,23 @@ namespace BansheeEngine
 	void convert32To24Bits(const INT32 input, UINT8* output)
 	{
 		UINT32 valToEncode = *(UINT32*)&input;
-		output[0] = valToEncode & 0x000000FF;
-		output[1] = (valToEncode >> 8) & 0x000000FF;
-		output[2] = (valToEncode >> 16) & 0x000000FF;
+		output[0] = (valToEncode >> 8) & 0x000000FF;
+		output[1] = (valToEncode >> 16) & 0x000000FF;
+		output[2] = (valToEncode >> 24) & 0x000000FF;
 	}
 
 	void convertToMono24(const UINT8* input, UINT8* output, UINT32 numSamples, UINT32 numChannels)
 	{
 		for (UINT32 i = 0; i < numSamples; i++)
 		{
-			INT32 sum = 0;
+			INT64 sum = 0;
 			for (UINT32 j = 0; j < numChannels; j++)
 			{
 				sum += AudioUtility::convert24To32Bits(input);
 				input += 3;
 			}
 
-			INT32 avg = sum / numChannels;
+			INT32 avg = (INT32)(sum / numChannels);
 			convert32To24Bits(avg, output);
 			output += 3;
 		}
@@ -169,7 +169,7 @@ namespace BansheeEngine
 			convert16To32Bits((INT16*)input, srcBuffer, numSamples);
 			break;
 		case 24:
-			convert8To32Bits(input, srcBuffer, numSamples);
+			BansheeEngine::convert24To32Bits(input, srcBuffer, numSamples);
 			break;
 		case 32:
 			// Do nothing
@@ -231,8 +231,8 @@ namespace BansheeEngine
 		{
 			for (UINT32 i = 0; i < numSamples; i++)
 			{
-				INT32 sample = AudioUtility::convert24To32Bits(input);
-				output[i] = sample / 8388607.0f;
+				INT32 sample = convert24To32Bits(input);
+				output[i] = sample / 2147483647.0f;
 
 				input += 3;
 			}
@@ -253,10 +253,6 @@ namespace BansheeEngine
 
 	INT32 AudioUtility::convert24To32Bits(const UINT8* input)
 	{
-		bool isNegative = (input[2] & 0x80) != 0;
-		if (isNegative) // Sign extend if negative
-			return (0xFF << 24) | (input[2] << 16) | (input[1] << 8) | input[0];
-		else
-			return (input[2] << 16) | (input[1] << 8) | input[0];
+		return (input[2] << 24) | (input[1] << 16) | (input[0] << 8);
 	}
 }

+ 1 - 1
Source/BansheeOpenAudio/Source/BsOAAudio.cpp

@@ -207,7 +207,7 @@ namespace BansheeEngine
 	SPtr<AudioClip> OAAudio::createClip(const SPtr<DataStream>& samples, UINT32 streamSize, UINT32 numSamples,
 		const AUDIO_CLIP_DESC& desc)
 	{
-		return bs_shared_ptr_new<OAAudioClip>(samples, streamSize, numSamples, desc);
+		return bs_core_ptr_new<OAAudioClip>(samples, streamSize, numSamples, desc);
 	}
 
 	SPtr<AudioListener> OAAudio::createListener()

+ 6 - 3
Source/BansheeOpenAudio/Source/BsOAAudioSource.cpp

@@ -176,6 +176,7 @@ namespace BansheeEngine
 		if (mGloballyPaused)
 			return;
 
+		if(requiresStreaming())
 		{
 			Lock(mMutex);
 			
@@ -269,11 +270,12 @@ namespace BansheeEngine
 		AudioSourceState state = getState();
 		stop();
 
-		float clipTime = 0.0f;
+		bool needsStreaming = requiresStreaming();
+		float clipTime;
 		{
 			Lock(mMutex);
 
-			if (!mIsStreaming)
+			if (!needsStreaming)
 				clipTime = time;
 			else
 			{
@@ -312,8 +314,9 @@ namespace BansheeEngine
 		if (contexts.size() > 1) // If only one context is available it is guaranteed it is always active, so we can avoid setting it
 			alcMakeContextCurrent(contexts[0]);
 
+		bool needsStreaming = requiresStreaming();
 		float time;
-		if (!mIsStreaming)
+		if (!needsStreaming)
 		{
 			alGetSourcef(mSourceIDs[0], AL_SEC_OFFSET, &time);
 			return time;

+ 3 - 1
Source/BansheeOpenAudio/Source/BsOAOggVorbisReader.cpp

@@ -40,7 +40,9 @@ namespace BansheeEngine
 
 	OAOggVorbisReader::OAOggVorbisReader()
 		:mChannelCount(0)
-	{ }
+	{
+		mOggVorbisFile.datasource = nullptr;
+	}
 
 	OAOggVorbisReader::~OAOggVorbisReader()
 	{

+ 1 - 1
Source/BansheeOpenAudio/Source/BsOAOggVorbisWriter.cpp

@@ -118,7 +118,7 @@ namespace BansheeEngine
 				for (UINT32 j = 0; j < mNumChannels; j++)
 				{
 					INT32 sample = AudioUtility::convert24To32Bits(samples);
-					float encodedSample = sample / 8388607.0f;
+					float encodedSample = sample / 2147483647.0f;
 					buffer[j][i] = encodedSample;
 
 					samples += 3;

+ 2 - 2
Source/MBansheeEditor/Inspectors/AudioClipInspector.cs

@@ -18,7 +18,7 @@ namespace BansheeEditor
         private GUIEnumField readModeField = new GUIEnumField(typeof(AudioReadMode), new LocEdString("Read mode"));
         private GUIEnumField bitDepthField = new GUIEnumField(typeof(AudioBitDepth), new LocEdString("Bit depth"));
         private GUIToggleField is3DField = new GUIToggleField(new LocEdString("3D"));
-        
+
         private GUIButton reimportButton = new GUIButton(new LocEdString("Reimport"));
 
         private AudioClipImportOptions importOptions;
@@ -34,7 +34,7 @@ namespace BansheeEditor
                 readModeField.OnSelectionChanged += x => importOptions.ReadMode = (AudioReadMode)x;
                 bitDepthField.OnSelectionChanged += x => importOptions.BitDepth = (AudioBitDepth)x;
                 is3DField.OnChanged += x => importOptions.Is3D = x;
-                
+
                 reimportButton.OnClick += TriggerReimport;
 
                 Layout.AddElement(formatField);

+ 11 - 1
Source/MBansheeEditor/Inspectors/AudioSourceInspector.cs

@@ -21,7 +21,8 @@ namespace BansheeEditor
         private GUIIntField priorityField = new GUIIntField(new LocEdString("Priority"));
         private GUIFloatField minDistanceField = new GUIFloatField(new LocEdString("Min. distance"));
         private GUIFloatField attenuationField = new GUIFloatField(new LocEdString("Attenuation"));
-        
+        private GUIToggleField playOnStartField = new GUIToggleField(new LocEdString("Play on start"));
+
         private InspectableState modifyState;
         
         /// <inheritdoc/>
@@ -44,6 +45,7 @@ namespace BansheeEditor
             priorityField.Value = (int)source.Priority;
             minDistanceField.Value = source.MinDistance;
             attenuationField.Value = source.Attenuation;
+            playOnStartField.Value = source.PlayOnStart;
 
             InspectableState oldState = modifyState;
             if (modifyState.HasFlag(InspectableState.Modified))
@@ -102,6 +104,13 @@ namespace BansheeEditor
             attenuationField.OnConfirmed += ConfirmModify;
             attenuationField.OnFocusLost += ConfirmModify;
 
+            playOnStartField.OnChanged += x =>
+            {
+                source.PlayOnStart = x;
+                MarkAsModified();
+                ConfirmModify();
+            };
+
             Layout.AddElement(audioClipField);
             Layout.AddElement(volumeField);
             Layout.AddElement(pitchField);
@@ -109,6 +118,7 @@ namespace BansheeEditor
             Layout.AddElement(priorityField);
             Layout.AddElement(minDistanceField);
             Layout.AddElement(attenuationField);
+            Layout.AddElement(playOnStartField);
         }
         
         /// <summary>