Преглед изворни кода

Added bit depth to audio readers

BearishSun пре 9 година
родитељ
комит
7a2a495f23

+ 0 - 1
Source/BansheeOpenAudio/Include/BsOAFLACReader.h

@@ -19,7 +19,6 @@ namespace BansheeEngine
 		AudioFileInfo info;
 		AudioFileInfo info;
 		UINT8* output = nullptr;
 		UINT8* output = nullptr;
 		Vector<UINT8> overflow;
 		Vector<UINT8> overflow;
-		UINT32 overflowBytesPerSample = 0;
 		UINT32 samplesToRead = 0;
 		UINT32 samplesToRead = 0;
 		bool error = false;
 		bool error = false;
 	};
 	};

+ 4 - 3
Source/BansheeOpenAudio/Include/BsOAPrerequisites.h

@@ -25,9 +25,10 @@ namespace BansheeEngine
 	/** Contains data describing an audio file. */
 	/** Contains data describing an audio file. */
 	struct AudioFileInfo
 	struct AudioFileInfo
 	{
 	{
-		UINT32 numSamples;
-		UINT32 sampleRate;
-		UINT32 numChannels;
+		UINT32 numSamples; /**< Total number of audio samples in the audio data (includes all channels). */
+		UINT32 sampleRate; /**< Number of audio samples per second, per channel. */
+		UINT32 numChannels; /**< Number of channels. Each channel has its own set of samples. */
+		UINT32 bitDepth; /**< Number of bits per sample. */
 	};
 	};
 }
 }
 
 

+ 5 - 14
Source/BansheeOpenAudio/Source/BsOAFLACReader.cpp

@@ -72,7 +72,7 @@ namespace BansheeEngine
 		if (!data->output) // Seek
 		if (!data->output) // Seek
 			return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
 			return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
 
 
-		UINT32 bytesPerSample = frame->header.bits_per_sample / 8;
+		UINT32 bytesPerSample = data->info.bitDepth / 8;
 		
 		
 		// If we received more data than we need, store it in the overflow buffer
 		// If we received more data than we need, store it in the overflow buffer
 		UINT32 frameSamples = frame->header.blocksize * frame->header.channels;
 		UINT32 frameSamples = frame->header.blocksize * frame->header.channels;
@@ -81,16 +81,6 @@ namespace BansheeEngine
 			UINT32 numExtraSamples = frameSamples - data->samplesToRead;
 			UINT32 numExtraSamples = frameSamples - data->samplesToRead;
 			UINT32 extraBytes = numExtraSamples * bytesPerSample;
 			UINT32 extraBytes = numExtraSamples * bytesPerSample;
 			data->overflow.reserve(extraBytes);
 			data->overflow.reserve(extraBytes);
-
-			if (data->overflowBytesPerSample != 0)
-			{
-				if (data->overflow.size() > 0) // Overflow already stores data but of a different size
-				{
-					assert(data->overflowBytesPerSample == bytesPerSample);
-				}
-			}
-			else
-				data->overflowBytesPerSample = bytesPerSample;
 		}
 		}
 
 
 		assert(bytesPerSample <= 4);
 		assert(bytesPerSample <= 4);
@@ -128,6 +118,7 @@ namespace BansheeEngine
 			data->info.numSamples = meta->data.stream_info.total_samples * meta->data.stream_info.channels;
 			data->info.numSamples = meta->data.stream_info.total_samples * meta->data.stream_info.channels;
 			data->info.sampleRate = meta->data.stream_info.sample_rate;
 			data->info.sampleRate = meta->data.stream_info.sample_rate;
 			data->info.numChannels = meta->data.stream_info.channels;
 			data->info.numChannels = meta->data.stream_info.channels;
+			data->info.bitDepth = meta->data.stream_info.bits_per_sample;
 		}
 		}
 	}
 	}
 
 
@@ -183,7 +174,6 @@ namespace BansheeEngine
 		FLAC__stream_decoder_init_stream(mDecoder, &streamRead, &streamSeek, &streamTell, &streamLength, &streamEof,
 		FLAC__stream_decoder_init_stream(mDecoder, &streamRead, &streamSeek, &streamTell, &streamLength, &streamEof,
 			&streamWrite, &streamMetadata, &streamError, &mData);
 			&streamWrite, &streamMetadata, &streamError, &mData);
 
 
-
 		if (!FLAC__stream_decoder_process_until_end_of_metadata(mDecoder))
 		if (!FLAC__stream_decoder_process_until_end_of_metadata(mDecoder))
 		{
 		{
 			close();
 			close();
@@ -210,9 +200,10 @@ namespace BansheeEngine
 		UINT64 overflowSize = mData.overflow.size();
 		UINT64 overflowSize = mData.overflow.size();
 		UINT64 overflowNumSamples = 0;
 		UINT64 overflowNumSamples = 0;
 		
 		
+		UINT32 bytesPerSample = mData.info.bitDepth / 8;
 		if (overflowSize > 0)
 		if (overflowSize > 0)
 		{
 		{
-			UINT32 sampleSize = numSamples * mData.overflowBytesPerSample;
+			UINT32 sampleSize = numSamples * bytesPerSample;
 			if (overflowSize > sampleSize)
 			if (overflowSize > sampleSize)
 			{
 			{
 				std::copy(mData.overflow.begin(), mData.overflow.begin() + sampleSize, samples);
 				std::copy(mData.overflow.begin(), mData.overflow.begin() + sampleSize, samples);
@@ -223,7 +214,7 @@ namespace BansheeEngine
 			else
 			else
 				std::copy(mData.overflow.begin(), mData.overflow.end(), samples);
 				std::copy(mData.overflow.begin(), mData.overflow.end(), samples);
 
 
-			overflowNumSamples = overflowSize / mData.overflowBytesPerSample;
+			overflowNumSamples = overflowSize / bytesPerSample;
 		}
 		}
 
 
 		mData.output = samples + overflowSize;
 		mData.output = samples + overflowSize;

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

@@ -80,6 +80,7 @@ namespace BansheeEngine
 		info.numChannels = vorbisInfo->channels;
 		info.numChannels = vorbisInfo->channels;
 		info.sampleRate = vorbisInfo->rate;
 		info.sampleRate = vorbisInfo->rate;
 		info.numSamples = (UINT32)(ov_pcm_total(&mOggVorbisFile, -1) * vorbisInfo->channels);
 		info.numSamples = (UINT32)(ov_pcm_total(&mOggVorbisFile, -1) * vorbisInfo->channels);
+		info.bitDepth = 16;
 
 
 		mChannelCount = info.numChannels;
 		mChannelCount = info.numChannels;
 		return true;
 		return true;

+ 1 - 0
Source/BansheeOpenAudio/Source/BsOAWaveReader.cpp

@@ -94,6 +94,7 @@ namespace BansheeEngine
 
 
 				info.numChannels = numChannels;
 				info.numChannels = numChannels;
 				info.sampleRate = sampleRate;
 				info.sampleRate = sampleRate;
+				info.bitDepth = bitDepth;
 
 
 				if (bitDepth != 8 && bitDepth != 16 && bitDepth != 24 && bitDepth != 32)
 				if (bitDepth != 8 && bitDepth != 16 && bitDepth != 24 && bitDepth != 32)
 				{
 				{