Skip to content

Commit

Permalink
libmodplug integration
Browse files Browse the repository at this point in the history
  • Loading branch information
jarikomppa committed May 16, 2014
1 parent fb7d5a8 commit 1b83f68
Show file tree
Hide file tree
Showing 9 changed files with 335 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "ext/libmodplug"]
path = ext/libmodplug
url = https://github.com/jarikomppa/libmodplug.git
61 changes: 58 additions & 3 deletions build/premake4.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ newoption {
description = "Include xaudio2 in build"
}

newoption {
trigger = "with-libmodplug",
description = "Include libmodplug in build"
}

-- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< --

solution "SoLoud"
Expand All @@ -57,6 +62,9 @@ solution "SoLoud"
}

links {"StaticLib"}
if _OPTIONS["with-libmodplug"] then
links {"libmodplug"}
end

configuration "Debug"
defines { "DEBUG" }
Expand Down Expand Up @@ -220,12 +228,51 @@ solution "SoLoud"
targetname "multimusic"
flags { "EnableSSE2", "NoMinimalRebuild", "OptimizeSpeed", "NoEditAndContinue", "No64BitChecks" }

-- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< --

if _OPTIONS["with-libmodplug"] then
project "libmodplug"
kind "StaticLib"
targetdir "../lib"
language "C++"

defines { "MODPLUG_STATIC" }

files
{
"../ext/libmodplug/src/**.cpp*"
}

includedirs
{
"../ext/libmodplug/src/**"
}

configuration "Debug"
defines { "DEBUG" }
flags {"Symbols" }
objdir (buildroot .. "/debug")
targetname "libmodplug_d"
flags { "Symbols" }


configuration "Release"
defines { "NDEBUG" }
flags {"Optimize"}
objdir (buildroot .. "/release")
targetname "libmodplug"
flags { "EnableSSE2", "NoMinimalRebuild", "OptimizeSpeed", "NoEditAndContinue", "No64BitChecks" }
end

-- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< --

project "StaticLib"
kind "StaticLib"
targetdir "../lib"
language "C++"

defines { "MODPLUG_STATIC" }

files
{
"../src/audiosource/**.c*",
Expand Down Expand Up @@ -296,8 +343,7 @@ end
includedirs {
"../include"
}



end

configuration "Debug"
Expand All @@ -323,6 +369,9 @@ end
files {
"../src/tools/codegen/**.cpp"
}
if _OPTIONS["with-libmodplug"] then
defines { "WITH_MODPLUG" }
end
configuration "Debug"
defines { "DEBUG" }
flags {"Symbols" }
Expand Down Expand Up @@ -351,7 +400,10 @@ end
}

links {"StaticLib"}

if _OPTIONS["with-libmodplug"] then
links {"libmodplug"}
end


configuration "Debug"
defines { "DEBUG" }
Expand Down Expand Up @@ -386,6 +438,9 @@ end
}

links {"StaticLib"}
if _OPTIONS["with-libmodplug"] then
links {"libmodplug"}
end

if (os.is("Windows")) then
linkoptions { "/DEF:\"../../src/c_api/soloud.def\"" }
Expand Down
1 change: 1 addition & 0 deletions ext/libmodplug
Submodule libmodplug added at 5190c5
10 changes: 10 additions & 0 deletions include/soloud_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ typedef void * Speech;
typedef void * Wav;
typedef void * WavStream;
typedef void * Sfxr;
typedef void * Modplug;

/*
* Soloud
Expand Down Expand Up @@ -209,6 +210,15 @@ int Sfxr_loadParams(Sfxr * aSfxr, const char * aFilename);
void Sfxr_loadPreset(Sfxr * aSfxr, int aPresetNo, int aRandSeed);
void Sfxr_setLooping(Sfxr * aSfxr, int aLoop);
void Sfxr_setFilter(Sfxr * aSfxr, int aFilterId, Filter * aFilter);

/*
* Modplug
*/
void Modplug_destroy(Modplug * aModplug);
Modplug * Modplug_create();
int Modplug_load(Modplug * aModplug, const char * aFilename);
void Modplug_setLooping(Modplug * aModplug, int aLoop);
void Modplug_setFilter(Modplug * aModplug, int aFilterId, Filter * aFilter);
#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
59 changes: 59 additions & 0 deletions include/soloud_modplug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
libmodplug module for SoLoud audio engine
Copyright (c) 2014 Jari Komppa
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/

#ifndef MODPLUG_H
#define MODPLUG_H

#include "soloud.h"

namespace SoLoud
{
class Modplug;

class ModplugInstance : public AudioSourceInstance
{
Modplug *mParent;
void *mModplugfile;
int mPlaying;

public:
ModplugInstance(Modplug *aParent);
virtual ~ModplugInstance();
virtual void getAudio(float *aBuffer, int aSamples);
virtual int hasEnded();
};

class Modplug : public AudioSource
{
public:
char *mData;
int mDataLen;
Modplug();
virtual ~Modplug();
int load(const char* aFilename);
virtual AudioSourceInstance *createInstance();
};
};

#endif
162 changes: 162 additions & 0 deletions src/audiosource/modplug/soloud_modplug.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
libmodplug module for SoLoud audio engine
Copyright (c) 2014 Jari Komppa
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/

#include <stdlib.h>
#include <stdio.h>
#include "soloud_modplug.h"
#include "../ext/libmodplug/src/modplug.h"


namespace SoLoud
{

ModplugInstance::ModplugInstance(Modplug *aParent)
{
mParent = aParent;
ModPlugFile* mpf = ModPlug_Load((const void*)mParent->mData, mParent->mDataLen);
mModplugfile = (void*)mpf;
mPlaying = mpf != NULL;
}

void ModplugInstance::getAudio(float *aBuffer, int aSamples)
{
if (mModplugfile == NULL)
return;
int buf[1024];
int s = aSamples;
int outofs = 0;

while (s && mPlaying)
{
int samples = 512;
if (s < samples) s = samples;
int res = ModPlug_Read((ModPlugFile *)mModplugfile, (void*)&buf[0], sizeof(int) * 2 * samples);
int samples_in_buffer = res / (sizeof(int) * 2);
if (s != samples_in_buffer) mPlaying = 0;

int i;
for (i = 0; i < samples_in_buffer; i++)
{
aBuffer[outofs] = buf[i*2+0] / (float)0x7fffffff;
aBuffer[outofs + aSamples] = buf[i*2+1] / (float)0x7fffffff;
outofs++;
}
s -= samples_in_buffer;
}

if (outofs < aSamples)
{
// TODO: handle looping
int i;
for (i = outofs; i < aSamples; i++)
aBuffer[i] = aBuffer[i + aSamples] = 0;
}

}

int ModplugInstance::hasEnded()
{
return !mPlaying;
}

ModplugInstance::~ModplugInstance()
{
if (mModplugfile)
{
ModPlug_Unload((ModPlugFile*)mModplugfile);
}
mModplugfile = 0;
}

int Modplug::load(const char* aFilename)
{
FILE * f = fopen(aFilename, "rb");
if (!f)
{
return -1;
}

if (mData)
{
delete[] mData;
}

fseek(f, 0, SEEK_END);
mDataLen = ftell(f);
fseek(f, 0, SEEK_SET);
mData = new char[mDataLen];
if (!mData)
{
mData = 0;
mDataLen = 0;
fclose(f);
return -2;
}
fread(mData,1,mDataLen,f);
fclose(f);

ModPlugFile* mpf = ModPlug_Load((const void*)mData, mDataLen);
if (!mpf)
{
delete[] mData;
mDataLen = 0;
return -3;
}
ModPlug_Unload(mpf);
return 0;
}

Modplug::Modplug()
{
mBaseSamplerate = 44100;
mChannels = 2;
mData = 0;
mDataLen = 0;

ModPlug_Settings mps;
ModPlug_GetSettings(&mps);
mps.mChannels = 2;
mps.mBits = 32;
mps.mFrequency = 44100;
mps.mResamplingMode = MODPLUG_RESAMPLE_LINEAR;
mps.mStereoSeparation = 128;
mps.mMaxMixChannels = 64;
mps.mLoopCount = 0;
mps.mFlags = MODPLUG_ENABLE_OVERSAMPLING;
ModPlug_SetSettings(&mps);
}

Modplug::~Modplug()
{
delete[] mData;
mData = 0;
mDataLen = 0;
}

AudioSourceInstance * Modplug::createInstance()
{
return new ModplugInstance(this);
}

};
6 changes: 5 additions & 1 deletion src/c_api/soloud.def
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
LIBRARY soloud
EXPORTS
Soloud_destroy
Soloud_create
Expand Down Expand Up @@ -91,3 +90,8 @@ EXPORTS
Sfxr_loadPreset
Sfxr_setLooping
Sfxr_setFilter
Modplug_destroy
Modplug_create
Modplug_load
Modplug_setLooping
Modplug_setFilter
Loading

0 comments on commit 1b83f68

Please sign in to comment.