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

copy OggBufferCallbacks in OggDecoder.h

mikymod пре 12 година
родитељ
комит
687b6db182
2 измењених фајлова са 198 додато и 1 уклоњено
  1. 80 1
      engine/audio/OggDecoder.h
  2. 118 0
      engine/audio/SoundRenderer.h

+ 80 - 1
engine/audio/OggBufferCallback.h → engine/audio/OggDecoder.h

@@ -26,9 +26,10 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
+#include <vorbis/vorbisfile.h>
 #include <cstring>
 
-#include "Assert.h"
+#define SOUND_STREAM_BUFFER_SIZE (4096 * 8) // 32K... should be tested
 
 namespace crown
 {
@@ -123,4 +124,82 @@ long int ogg_buffer_tell(void* src)
     return ob->cur_ptr - ob->buffer_ptr;
 }
 
+//-----------------------------------------------------------------------------
+//-----------------------------------------------------------------------------
+
+/// __OggDecorer__ decodes ogg buffers and provides peaces of decoded audio data.
+class OggDecorer
+{
+public:
+
+	//-----------------------------------------------------------------------------
+	OggDecorer() {}
+
+	//-----------------------------------------------------------------------------
+	void open(const uint8_t* buffer)
+	{
+		ov_callbacks callbacks;
+		callbacks.read_func = ogg_buffer_read;
+		callbacks.seek_func = ogg_buffer_seek;
+		callbacks.close_func = ogg_buffer_close;
+		callbacks.tell_func = ogg_buffer_tell;
+
+		int32_t result = ov_open_callbacks(buffer, m_stream, NULL, 0, callbacks);
+		CE_ASSERT(result == 0, "Unable to open stream buffer");
+
+		m_info = ov_info(&m_stream, -1);
+
+		m_comment = ov_comment(&m_stream, -1);
+	}
+
+	//-----------------------------------------------------------------------------
+	void stream()
+	{
+	    int32_t  size = 0;
+	    int32_t  section;
+	    int32_t  result;
+
+	    while (size < SOUND_STREAM_BUFFER_SIZE)
+	    {
+	    	result = ov_read(&m_stream, m_data + size, SOUND_STREAM_BUFFER_SIZE - size, 0, 2, 1, &section);
+
+	    	CE_ASSERT(result >= 0, "Fail to read and decode ogg stream");
+
+	    	if (result > 0)
+	    	{
+	    		size += result;
+	    	}
+	    	else
+	    	{
+	    		break;
+	    	}
+	    }	
+	}
+	
+	//-----------------------------------------------------------------------------
+	void close()
+	{
+		ov_clear(m_stream);
+		memset(m_data, 0, SOUND_STREAM_BUFFER_SIZE);
+		m_info = NULL;
+	}
+
+	//-----------------------------------------------------------------------------
+	const uint8_t* data()
+	{
+		return m_data;
+	}
+
+private:
+
+	OggVorbis_File  		m_stream;
+	vorbis_info*			m_info;
+	vorbis_comment*			m_comment;
+
+	uint8_t					m_data[SOUND_STREAM_BUFFER_SIZE];
+};
+
+
+
+
 } // namespace crown

+ 118 - 0
engine/audio/SoundRenderer.h

@@ -0,0 +1,118 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include "IdTable.h"
+#include "Allocator.h"
+
+namespace crown
+{
+
+class Vec3;
+
+typedef 	Id 		SoundBufferId;
+typedef 	Id 		SoundSourceId;
+
+class SoundRendererBackend;
+
+class SoundRenderer
+{
+public:
+							SoundRenderer(Allocator& allocator);
+
+							~SoundRenderer();
+
+	void					init();
+
+	void					shutdown();
+
+	/// Sets listener parameters. @a position affects audibility of sounds, 
+	/// @a velocity affects doppler shift and @a orientation affects how a sound could be heard
+	void					set_listener(const Vec3& pos, const Vec3& vel, const Vec3& or_up, const Vec3& or_at) const;
+
+	/// Creates buffer's @a data, with a specific @a size, which contains sound raw data.
+	/// More parameters must be specified, such as @a sample_rate, that is the number of sample per unit of time 
+	/// taken from a continuous signal to make a discrete signal, @a channels which specifies if sound is mono or stereo and @a bits
+	/// which specifies the magnitude of samples information.
+	/// N.B: stereo sound cannot be attenuated
+	SoundBufferId			create_buffer(const void* data, const uint32_t size, const uint32_t sample_rate, const uint32_t channels, const uint32_t bits);
+
+	/// Destroys buffer
+	void					destroy_buffer(SoundBufferId id);
+
+	/// Creates a source of sound 
+	SoundSourceId			create_source(bool loop);
+
+	/// Destroys a sound, specified by @a id, previously created
+	void					destroy_source(SoundSourceId id);
+
+	/// Plays a sound, specified by source @a sid and a buffer @a bid
+	void					play_source(SoundSourceId sid, SoundBufferId bid);
+
+	/// Pauses a sound, specified by @a id
+	void					pause_source(SoundSourceId id);
+
+	///	Sets source's @a min_distance. From @a min_distance to @a max_distance, sound
+	/// scales from full volume to silence
+	void					set_source_min_distance(SoundSourceId id,  const float min_distance);
+
+	///	Sets source's @a max_distance. From @a min_distance to @a max_distance, sound
+	/// scales from full volume to silence
+	void					set_source_max_distance(SoundSourceId id,  const float max_distance);
+
+	/// Sets source's @a position. It affects sound audibility
+	void					set_source_position(SoundSourceId id, const Vec3& pos);
+
+	/// Sets source's @a velocity. It affects doppler shift
+	void					set_source_velocity(SoundSourceId id, const Vec3& vel);
+
+	/// Sets source's @a direction. It affects how a sound could be heard
+	void					set_source_direction(SoundSourceId id, const Vec3& dir);
+
+	/// Sets source's @a pitch.
+	void					set_source_pitch(SoundSourceId id, const float pitch);
+
+	/// Sets source's @a gain, that is measure sound's amplification
+	void					set_source_gain(SoundSourceId id, const float gain);
+
+	/// Sets source's @a rolloff factor. Greater it is, greater sound's attenuation is
+	void					set_source_rolloff(SoundSourceId id, const float rolloff);
+
+	/// Is source #@a id playing?
+	bool					source_playing(SoundSourceId id);
+
+private:
+
+	Allocator& 				m_allocator;
+
+	SoundRendererBackend*	m_backend;
+
+	IdTable 				m_buffers_id_table;
+	IdTable 				m_sources_id_table;
+};
+
+} // namespace crown