Procházet zdrojové kódy

Adding OGG support into the gameplay library.

Dale Ducharme před 14 roky
rodič
revize
90bb35c538
4 změnil soubory, kde provedl 88 přidání a 19 odebrání
  1. 7 0
      gameplay/.cproject
  2. 4 4
      gameplay/gameplay.vcxproj
  3. 76 15
      gameplay/src/AudioBuffer.cpp
  4. 1 0
      gameplay/src/Base.h

+ 7 - 0
gameplay/.cproject

@@ -28,6 +28,7 @@
 								</option>
 								</option>
 								<option id="com.qnx.qcc.option.compiler.includePath.2133604142" name="Include Directories (-I)" superClass="com.qnx.qcc.option.compiler.includePath" valueType="includePath">
 								<option id="com.qnx.qcc.option.compiler.includePath.2133604142" name="Include Directories (-I)" superClass="com.qnx.qcc.option.compiler.includePath" valueType="includePath">
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}}\..\external-deps\bullet\include&quot;"/>
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}}\..\external-deps\bullet\include&quot;"/>
+									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}}\..\external-deps\oggvorbis\include&quot;"/>
 									<listOptionValue builtIn="false" value="${QNX_TARGET}/../target-override/usr/include"/>
 									<listOptionValue builtIn="false" value="${QNX_TARGET}/../target-override/usr/include"/>
 								</option>
 								</option>
 								<inputType id="com.qnx.qcc.inputType.compiler.997142816" superClass="com.qnx.qcc.inputType.compiler"/>
 								<inputType id="com.qnx.qcc.inputType.compiler.997142816" superClass="com.qnx.qcc.inputType.compiler"/>
@@ -80,6 +81,7 @@
 								</option>
 								</option>
 								<option id="com.qnx.qcc.option.compiler.includePath.1670164593" name="Include Directories (-I)" superClass="com.qnx.qcc.option.compiler.includePath" valueType="includePath">
 								<option id="com.qnx.qcc.option.compiler.includePath.1670164593" name="Include Directories (-I)" superClass="com.qnx.qcc.option.compiler.includePath" valueType="includePath">
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}}\..\external-deps\bullet\include&quot;"/>
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}}\..\external-deps\bullet\include&quot;"/>
+									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}}\..\external-deps\oggvorbis\include&quot;"/>
 									<listOptionValue builtIn="false" value="${QNX_TARGET}/../target-override/usr/include"/>
 									<listOptionValue builtIn="false" value="${QNX_TARGET}/../target-override/usr/include"/>
 								</option>
 								</option>
 								<inputType id="com.qnx.qcc.inputType.compiler.1380846613" superClass="com.qnx.qcc.inputType.compiler"/>
 								<inputType id="com.qnx.qcc.inputType.compiler.1380846613" superClass="com.qnx.qcc.inputType.compiler"/>
@@ -131,6 +133,7 @@
 								</option>
 								</option>
 								<option id="com.qnx.qcc.option.compiler.includePath.1503059677" name="Include Directories (-I)" superClass="com.qnx.qcc.option.compiler.includePath" valueType="includePath">
 								<option id="com.qnx.qcc.option.compiler.includePath.1503059677" name="Include Directories (-I)" superClass="com.qnx.qcc.option.compiler.includePath" valueType="includePath">
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}}\..\external-deps\bullet\include&quot;"/>
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}}\..\external-deps\bullet\include&quot;"/>
+									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}}\..\external-deps\oggvorbis\include&quot;"/>
 									<listOptionValue builtIn="false" value="${QNX_TARGET}/../target-override/usr/include"/>
 									<listOptionValue builtIn="false" value="${QNX_TARGET}/../target-override/usr/include"/>
 								</option>
 								</option>
 								<inputType id="com.qnx.qcc.inputType.compiler.81809638" superClass="com.qnx.qcc.inputType.compiler"/>
 								<inputType id="com.qnx.qcc.inputType.compiler.81809638" superClass="com.qnx.qcc.inputType.compiler"/>
@@ -185,6 +188,7 @@
 								</option>
 								</option>
 								<option id="com.qnx.qcc.option.compiler.includePath.1769677874" name="Include Directories (-I)" superClass="com.qnx.qcc.option.compiler.includePath" valueType="includePath">
 								<option id="com.qnx.qcc.option.compiler.includePath.1769677874" name="Include Directories (-I)" superClass="com.qnx.qcc.option.compiler.includePath" valueType="includePath">
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}}\..\external-deps\bullet\include&quot;"/>
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}}\..\external-deps\bullet\include&quot;"/>
+									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}}\..\external-deps\oggvorbis\include&quot;"/>
 									<listOptionValue builtIn="false" value="${QNX_TARGET}/../target-override/usr/include"/>
 									<listOptionValue builtIn="false" value="${QNX_TARGET}/../target-override/usr/include"/>
 								</option>
 								</option>
 								<inputType id="com.qnx.qcc.inputType.compiler.2007171407" superClass="com.qnx.qcc.inputType.compiler"/>
 								<inputType id="com.qnx.qcc.inputType.compiler.2007171407" superClass="com.qnx.qcc.inputType.compiler"/>
@@ -237,6 +241,7 @@
 								</option>
 								</option>
 								<option id="com.qnx.qcc.option.compiler.includePath.847642559" name="Include Directories (-I)" superClass="com.qnx.qcc.option.compiler.includePath" valueType="includePath">
 								<option id="com.qnx.qcc.option.compiler.includePath.847642559" name="Include Directories (-I)" superClass="com.qnx.qcc.option.compiler.includePath" valueType="includePath">
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}}\..\external-deps\bullet\include&quot;"/>
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}}\..\external-deps\bullet\include&quot;"/>
+									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}}\..\external-deps\oggvorbis\include&quot;"/>
 									<listOptionValue builtIn="false" value="${QNX_TARGET}/../target-override/usr/include"/>
 									<listOptionValue builtIn="false" value="${QNX_TARGET}/../target-override/usr/include"/>
 								</option>
 								</option>
 								<inputType id="com.qnx.qcc.inputType.compiler.1038720310" superClass="com.qnx.qcc.inputType.compiler"/>
 								<inputType id="com.qnx.qcc.inputType.compiler.1038720310" superClass="com.qnx.qcc.inputType.compiler"/>
@@ -289,6 +294,7 @@
 								</option>
 								</option>
 								<option id="com.qnx.qcc.option.compiler.includePath.513622172" name="Include Directories (-I)" superClass="com.qnx.qcc.option.compiler.includePath" valueType="includePath">
 								<option id="com.qnx.qcc.option.compiler.includePath.513622172" name="Include Directories (-I)" superClass="com.qnx.qcc.option.compiler.includePath" valueType="includePath">
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}}\..\external-deps\bullet\include&quot;"/>
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}}\..\external-deps\bullet\include&quot;"/>
+									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}}\..\external-deps\oggvorbis\include&quot;"/>
 									<listOptionValue builtIn="false" value="${QNX_TARGET}/../target-override/usr/include"/>
 									<listOptionValue builtIn="false" value="${QNX_TARGET}/../target-override/usr/include"/>
 								</option>
 								</option>
 								<inputType id="com.qnx.qcc.inputType.compiler.1961855927" superClass="com.qnx.qcc.inputType.compiler"/>
 								<inputType id="com.qnx.qcc.inputType.compiler.1961855927" superClass="com.qnx.qcc.inputType.compiler"/>
@@ -342,6 +348,7 @@
 								</option>
 								</option>
 								<option id="com.qnx.qcc.option.compiler.includePath.1685994750" name="Include Directories (-I)" superClass="com.qnx.qcc.option.compiler.includePath" valueType="includePath">
 								<option id="com.qnx.qcc.option.compiler.includePath.1685994750" name="Include Directories (-I)" superClass="com.qnx.qcc.option.compiler.includePath" valueType="includePath">
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}}\..\external-deps\bullet\include&quot;"/>
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}}\..\external-deps\bullet\include&quot;"/>
+									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}}\..\external-deps\oggvorbis\include&quot;"/>
 									<listOptionValue builtIn="false" value="${QNX_TARGET}/../target-override/usr/include"/>
 									<listOptionValue builtIn="false" value="${QNX_TARGET}/../target-override/usr/include"/>
 								</option>
 								</option>
 								<inputType id="com.qnx.qcc.inputType.compiler.1658185881" superClass="com.qnx.qcc.inputType.compiler"/>
 								<inputType id="com.qnx.qcc.inputType.compiler.1658185881" superClass="com.qnx.qcc.inputType.compiler"/>

+ 4 - 4
gameplay/gameplay.vcxproj

@@ -244,7 +244,7 @@
       <WarningLevel>Level3</WarningLevel>
       <WarningLevel>Level3</WarningLevel>
       <Optimization>Disabled</Optimization>
       <Optimization>Disabled</Optimization>
       <PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
-      <AdditionalIncludeDirectories>..\external-deps\bullet\include;..\external-deps\openal\include\AL;..\external-deps\alut\include\AL;..\external-deps\glew\include;..\external-deps\libpng\include;..\external-deps\zlib\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+      <AdditionalIncludeDirectories>..\external-deps\bullet\include;..\external-deps\openal\include\AL;..\external-deps\alut\include\AL;..\external-deps\oggvorbis\include;..\external-deps\glew\include;..\external-deps\libpng\include;..\external-deps\zlib\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
       <RuntimeTypeInfo>
       <RuntimeTypeInfo>
       </RuntimeTypeInfo>
       </RuntimeTypeInfo>
     </ClCompile>
     </ClCompile>
@@ -260,7 +260,7 @@
       <WarningLevel>Level3</WarningLevel>
       <WarningLevel>Level3</WarningLevel>
       <Optimization>Disabled</Optimization>
       <Optimization>Disabled</Optimization>
       <PreprocessorDefinitions>WIN32;_DEBUG;_LIB;GAMEPLAY_MEM_LEAK_DETECTION;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <PreprocessorDefinitions>WIN32;_DEBUG;_LIB;GAMEPLAY_MEM_LEAK_DETECTION;%(PreprocessorDefinitions)</PreprocessorDefinitions>
-      <AdditionalIncludeDirectories>..\external-deps\bullet\include;..\external-deps\openal\include\AL;..\external-deps\alut\include\AL;..\external-deps\glew\include;..\external-deps\libpng\include;..\external-deps\zlib\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+      <AdditionalIncludeDirectories>..\external-deps\bullet\include;..\external-deps\openal\include\AL;..\external-deps\alut\include\AL;..\external-deps\oggvorbis\include;..\external-deps\glew\include;..\external-deps\libpng\include;..\external-deps\zlib\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
       <RuntimeTypeInfo>true</RuntimeTypeInfo>
       <RuntimeTypeInfo>true</RuntimeTypeInfo>
     </ClCompile>
     </ClCompile>
     <Link>
     <Link>
@@ -281,7 +281,7 @@
       <FunctionLevelLinking>true</FunctionLevelLinking>
       <FunctionLevelLinking>true</FunctionLevelLinking>
       <IntrinsicFunctions>true</IntrinsicFunctions>
       <IntrinsicFunctions>true</IntrinsicFunctions>
       <PreprocessorDefinitions>WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <PreprocessorDefinitions>WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
-      <AdditionalIncludeDirectories>..\external-deps\bullet\include;..\external-deps\openal\include\AL;..\external-deps\alut\include\AL;..\external-deps\glew\include;..\external-deps\libpng\include;..\external-deps\zlib\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+      <AdditionalIncludeDirectories>..\external-deps\bullet\include;..\external-deps\openal\include\AL;..\external-deps\alut\include\AL;..\external-deps\oggvorbis\include;..\external-deps\glew\include;..\external-deps\libpng\include;..\external-deps\zlib\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
     </ClCompile>
     </ClCompile>
     <Link>
     <Link>
       <SubSystem>Windows</SubSystem>
       <SubSystem>Windows</SubSystem>
@@ -293,4 +293,4 @@
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
   </ImportGroup>
-</Project>
+</Project>

+ 76 - 15
gameplay/src/AudioBuffer.cpp

@@ -9,6 +9,7 @@
 namespace gameplay
 namespace gameplay
 {
 {
 
 
+
 // Audio buffer cache
 // Audio buffer cache
 static std::vector<AudioBuffer*> __buffers;
 static std::vector<AudioBuffer*> __buffers;
 
 
@@ -81,7 +82,7 @@ AudioBuffer* AudioBuffer::create(const char* path)
             goto cleanup;
             goto cleanup;
         }
         }
     }
     }
-    else if(memcmp(header, "OGG", 3) == 0)
+    else if (memcmp(header, "OggS", 4) == 0)
     {
     {
         if (!AudioBuffer::loadOgg(file, alBuffer))
         if (!AudioBuffer::loadOgg(file, alBuffer))
         {
         {
@@ -94,7 +95,6 @@ AudioBuffer* AudioBuffer::create(const char* path)
         LOG_ERROR_VARG("Unsupported audio file: %s", path);
         LOG_ERROR_VARG("Unsupported audio file: %s", path);
     }
     }
     
     
-    
     fclose(file);
     fclose(file);
 
 
     buffer = new AudioBuffer(path, alBuffer);
     buffer = new AudioBuffer(path, alBuffer);
@@ -123,7 +123,7 @@ bool AudioBuffer::loadWav(FILE* file, ALuint buffer)
         return false;
         return false;
     
     
     // Check for a valid pcm format.
     // Check for a valid pcm format.
-    if(fread(stream, 1, 2, file) != 2 || stream[1] != 0 || stream[0] != 1)
+    if (fread(stream, 1, 2, file) != 2 || stream[1] != 0 || stream[0] != 1)
     {
     {
         LOG_ERROR("Unsupported audio file, not PCM format.");
         LOG_ERROR("Unsupported audio file, not PCM format.");
         return false;
         return false;
@@ -140,6 +140,7 @@ bool AudioBuffer::loadWav(FILE* file, ALuint buffer)
     ALuint frequency;
     ALuint frequency;
     if (fread(stream, 1, 4, file) != 4)
     if (fread(stream, 1, 4, file) != 4)
         return false;
         return false;
+
     frequency  = stream[3]<<24;
     frequency  = stream[3]<<24;
     frequency |= stream[2]<<16;
     frequency |= stream[2]<<16;
     frequency |= stream[1]<<8;
     frequency |= stream[1]<<8;
@@ -158,21 +159,21 @@ bool AudioBuffer::loadWav(FILE* file, ALuint buffer)
     bits  = stream[1]<<8;
     bits  = stream[1]<<8;
     bits |= stream[0];
     bits |= stream[0];
     
     
-    
+
     // Now convert the given channel count and bit depth into an OpenAL format. 
     // Now convert the given channel count and bit depth into an OpenAL format. 
     ALuint format = 0;
     ALuint format = 0;
-    if(bits == 8)
+    if (bits == 8)
     {
     {
-        if(channels == 1)
+        if (channels == 1)
             format = AL_FORMAT_MONO8;
             format = AL_FORMAT_MONO8;
-        else if(channels == 2)
+        else if (channels == 2)
             format = AL_FORMAT_STEREO8;
             format = AL_FORMAT_STEREO8;
     }
     }
-    else if(bits == 16)
+    else if (bits == 16)
     {
     {
-        if(channels == 1)
+        if (channels == 1)
             format = AL_FORMAT_MONO16;
             format = AL_FORMAT_MONO16;
-        else if(channels == 2)
+        else if (channels == 2)
             format = AL_FORMAT_STEREO16;
             format = AL_FORMAT_STEREO16;
     }
     }
     else
     else
@@ -191,24 +192,84 @@ bool AudioBuffer::loadWav(FILE* file, ALuint buffer)
     // Read how much data is remaining and buffer it up.
     // Read how much data is remaining and buffer it up.
     unsigned int dataSize;
     unsigned int dataSize;
     fread(&dataSize, sizeof(int), 1, file);
     fread(&dataSize, sizeof(int), 1, file);
-    unsigned char* data = new unsigned char[dataSize];
-    if (fread(data, sizeof(unsigned char), dataSize, file) != dataSize)
+
+    char* data = new char[dataSize];
+    if (fread(data, sizeof(char), dataSize, file) != dataSize)
     {
     {
         LOG_ERROR("WAV file missing data.");
         LOG_ERROR("WAV file missing data.");
         SAFE_DELETE_ARRAY(data);
         SAFE_DELETE_ARRAY(data);
         return false;
         return false;
     }
     }
+
     alBufferData(buffer, format, data, dataSize, frequency);
     alBufferData(buffer, format, data, dataSize, frequency);
     SAFE_DELETE_ARRAY(data);
     SAFE_DELETE_ARRAY(data);
     return true;
     return true;
 }
 }
     
     
-// TODO:
 bool AudioBuffer::loadOgg(FILE* file, ALuint buffer)
 bool AudioBuffer::loadOgg(FILE* file, ALuint buffer)
 {
 {
-    LOG_ERROR("Ogg Vorbis not supported yet");
+    OggVorbis_File ogg_file;
+    vorbis_info* info;
+    ALenum format;
+    int result;
+    int section;
+    unsigned int size = 0;
+
+    rewind(file);
+
+    if ((result = ov_open(file, &ogg_file, NULL, 0)) < 0)
+    {
+        fclose(file);
+        LOG_ERROR("Could not open Ogg stream.");
+        return false;
+    }
+
+    info = ov_info(&ogg_file, -1);
+
+    if (info->channels == 1)
+        format = AL_FORMAT_MONO16;
+    else
+        format = AL_FORMAT_STEREO16;
+
+	// size = #samples * #channels * 2 (for 16 bit)
+    unsigned int data_size = ov_pcm_total(&ogg_file, -1) * info->channels * 2;
+    char* data = new char[data_size];
+
+    while (size < data_size)
+    {
+    	result = ov_read(&ogg_file, data + size, data_size - size, 0, 2, 1, &section);
+    	if (result > 0)
+    	{
+    		size += result;
+    	}
+    	else if (result < 0)
+    	{
+    		SAFE_DELETE_ARRAY(data);
+    		LOG_ERROR("OGG file missing data.");
+    		return false;
+    	}
+    	else
+    	{
+    		break;
+    	}
+    }
     
     
-    return false;
+    if (size == 0)
+    {
+    	SAFE_DELETE_ARRAY(data);
+    	LOG_ERROR("Unable to read OGG data.");
+    	return false;
+    }
+
+    alBufferData(buffer, format, data, data_size, info->rate);
+
+    SAFE_DELETE_ARRAY(data);
+    ov_clear(&ogg_file);
+
+    // ov_clear actually closes the file pointer as well
+    file = 0;
+
+    return true;
 }
 }
 
 
 }
 }

+ 1 - 0
gameplay/src/Base.h

@@ -148,6 +148,7 @@ extern void printError(const char* format, ...);
 #include <OpenAL/al.h>
 #include <OpenAL/al.h>
 #include <OpenAL/alc.h>
 #include <OpenAL/alc.h>
 #endif
 #endif
+#include <vorbis/vorbisfile.h>
 
 
 // Screen/Window
 // Screen/Window
 #define WINDOW_WIDTH        1024
 #define WINDOW_WIDTH        1024