Skip to content

Commit

Permalink
Add getLength to Sound and ogg Music classes for SDL
Browse files Browse the repository at this point in the history
  • Loading branch information
hughsando committed Jun 26, 2015
1 parent 9ba31ef commit 1af8f20
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 18 deletions.
2 changes: 1 addition & 1 deletion haxelib.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"tags": [ "cpp", "neko", "nme", "opengl", "sdl", "android", "ios" ],
"description": "NME provides a backend for native iOS, Android, Windows, Mac and Linux applications, using a Flash inspired API",
"version": "5.4.0",
"binaryversion": "66",
"binaryversion": "67",
"releasenote": "see Changes.md",
"contributors": [ "gamehaxe" ],
"dependencies": {}
Expand Down
9 changes: 4 additions & 5 deletions project/Build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,8 @@

<section if="NME_OPENAL">
<file name="${SRC_DIR}/openal/OpenALSound.cpp" />
<file name="${SRC_DIR}/common/Audio.cpp" />
<file name="${SRC_DIR}/common/OggStream.cpp" />
</section>



<compilerflag value="-I${INC_DIR}/xcompile" if="xcompile" />
<compilerflag value="-I${NME_DEV}/include" />
<compilerflag value="-D_7ZIP_ST" unless="windows"/>
Expand Down Expand Up @@ -211,7 +208,9 @@
<file name="${SRC_DIR}/common/Lzma.cpp"/>
<file name="${SRC_DIR}/common/Thread.cpp"/>
<file name="${SRC_DIR}/common/Camera.cpp" if="NME_CAMERA" />
<!--<file name="common/Audio.cpp" if="openal"/>-->

<file name="${SRC_DIR}/common/Audio.cpp" />
<file name="${SRC_DIR}/common/OggStream.cpp" />

<file name="${SRC_DIR}/common/XML/tinystr.cpp"/>
<file name="${SRC_DIR}/common/XML/tinyxml.cpp"/>
Expand Down
4 changes: 2 additions & 2 deletions project/include/Audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#define LOG_SOUND(args,...) printf(args, ##__VA_ARGS__);
#endif
#else
#define LOG_SOUND(args...) { }
#define LOG_SOUND(args,...) { }
#endif


Expand All @@ -35,7 +35,7 @@ namespace nme
virtual ~AudioStream() {}

virtual bool open(const std::string &path, int startTime)=0;
virtual int getLength(const std::string &path) = 0;
virtual double getLength(const std::string &path) = 0;
virtual double getPosition() = 0;
virtual double setPosition(const float &inFloat) = 0;
virtual int fillBuffer(char *outBuffer, int inRequestBytes) = 0;
Expand Down
2 changes: 1 addition & 1 deletion project/src/common/OggStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class AudioStream_Ogg : public AudioStream



int getLength(const std::string &path)
double getLength(const std::string &path)
{
int result;
mPath = std::string(path.c_str());
Expand Down
45 changes: 37 additions & 8 deletions project/src/sdl/SDLSound.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <Audio.h>
#include <Sound.h>
#include <Display.h>
#include <SDL.h>
Expand All @@ -6,6 +7,7 @@
#include <hx/Thread.h>


using namespace nme::Audio;

namespace nme
{
Expand Down Expand Up @@ -412,12 +414,24 @@ class SDLSound : public Sound
}
double getLength()
{
if (mChunk==0) return 0;
if (mChunk==0)
return 0;
#if defined(DYNAMIC_SDL) || defined(WEBOS)
// ?
return 0.0;
#else
return 0.0;


int freq = 0;
Uint16 format = 0;
int channels = 0;
Mix_QuerySpec(&freq, &format, &channels);

if (freq==0)
return 0;

int bytesPerSample = 2;
return (double)1000.0*mChunk->alen/(freq*channels*bytesPerSample);

#endif
}
// Will return with one ref...
Expand Down Expand Up @@ -537,13 +551,15 @@ class SDLMusic : public Sound
bool loaded;
std::string filename;
std::vector<unsigned char> reso;
double duration;

public:
SDLMusic(const std::string &inFilename)
{
filename = inFilename;
loaded = false;
mMusic = 0;
duration = 0;

IncRef();
if (gSDLAudioState!=sdaNotInit)
Expand All @@ -566,8 +582,17 @@ class SDLMusic : public Sound
#endif

mMusic = Mix_LoadMUS(name);

if (!mMusic)
if (mMusic)
{
AudioFormat fmt = determineFormatFromFile(name);
if (fmt==eAF_ogg)
{
AudioStream *s = AudioStream::createOgg();
duration = s->getLength(name)*1000.0;
delete s;
}
}
else
{
ByteArray resource(filename.c_str());
if (resource.Ok())
Expand All @@ -582,6 +607,12 @@ class SDLMusic : public Sound
#else
mMusic = Mix_LoadMUS_RW(SDL_RWFromConstMem(&reso[0], resource.Size()));
#endif

if (mMusic)
{
// TODO
duration = 60000.0;
}
}
}
}
Expand Down Expand Up @@ -622,9 +653,7 @@ class SDLMusic : public Sound
}
double getLength()
{
if (mMusic==0) return 0;
// TODO:
return 60000;
return duration;
}
// Will return with one ref...
SoundChannel *openChannel(double startTime, int loops, const SoundTransform &inTransform)
Expand Down
2 changes: 1 addition & 1 deletion samples/06-Sound/Sample.hx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class Sample extends Sprite
}
}

function doDelay(_) { Sys.sleep(1.0); }
//function doDelay(_) { Sys.sleep(1.0); }

function playMusic(inId:String)
{
Expand Down

0 comments on commit 1af8f20

Please sign in to comment.