aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-05-28 22:57:57 +0800
committerchai <chaifix@163.com>2018-05-28 22:57:57 +0800
commit90a2ec2b18d7d3a36fa3106c4d063b1048d8d98e (patch)
tree42d04c0fd9bc843d9d0e54b347a00cd03533de14
parentaa9393254a8a9deec3db5b17539a9be273965b07 (diff)
修改读取wav错误
-rw-r--r--bin/jin.exebin1539584 -> 1533440 bytes
-rw-r--r--build/vs2015/jin.vcxproj1
-rw-r--r--build/vs2015/jin.vcxproj.filters3
-rw-r--r--src/3rdparty/wav/wav.c6
-rw-r--r--src/3rdparty/wav/wav.h2
-rw-r--r--src/libjin/audio/sdl/audio.cpp2
-rw-r--r--src/libjin/audio/sdl/audio.h2
-rw-r--r--src/libjin/audio/sdl/source.cpp63
-rw-r--r--src/libjin/audio/sdl/source.h14
-rw-r--r--src/libjin/audio/source.h6
-rw-r--r--src/libjin/common/factory.h12
-rw-r--r--src/libjin/utils/macros.h2
-rw-r--r--src/libjin/utils/unittest.cpp20
13 files changed, 87 insertions, 46 deletions
diff --git a/bin/jin.exe b/bin/jin.exe
index 69ee6ee..1ae3818 100644
--- a/bin/jin.exe
+++ b/bin/jin.exe
Binary files differ
diff --git a/build/vs2015/jin.vcxproj b/build/vs2015/jin.vcxproj
index 6ccfda0..834175d 100644
--- a/build/vs2015/jin.vcxproj
+++ b/build/vs2015/jin.vcxproj
@@ -228,6 +228,7 @@
<ClInclude Include="..\..\src\libjin\audio\sdl\source.h" />
<ClInclude Include="..\..\src\libjin\audio\source.h" />
<ClInclude Include="..\..\src\libjin\common\data.h" />
+ <ClInclude Include="..\..\src\libjin\common\factory.h" />
<ClInclude Include="..\..\src\libjin\common\subsystem.h" />
<ClInclude Include="..\..\src\libjin\core\core.h" />
<ClInclude Include="..\..\src\libjin\core\game.h" />
diff --git a/build/vs2015/jin.vcxproj.filters b/build/vs2015/jin.vcxproj.filters
index baf3df7..97e5992 100644
--- a/build/vs2015/jin.vcxproj.filters
+++ b/build/vs2015/jin.vcxproj.filters
@@ -626,6 +626,9 @@
<ClInclude Include="..\..\src\libjin\physics\rigid.h">
<Filter>src\libjin\physics</Filter>
</ClInclude>
+ <ClInclude Include="..\..\src\libjin\common\factory.h">
+ <Filter>src\libjin\common</Filter>
+ </ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\src\3rdparty\lua51\Makefile">
diff --git a/src/3rdparty/wav/wav.c b/src/3rdparty/wav/wav.c
index bfe7a41..af33946 100644
--- a/src/3rdparty/wav/wav.c
+++ b/src/3rdparty/wav/wav.c
@@ -1,5 +1,5 @@
/**
-* Copyright (c) 2015 rxi
+* Copyright (c) 2018 chai
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MIT license. See LICENSE for details.
@@ -29,7 +29,9 @@ next:
return p + 8;
}
-int wav_read(wav_t *w, const void *data, size_t len) {
+int wav_read(wav_t *w, const void *mem, size_t len) {
+ char* data = (char*)malloc(len);
+ memcpy(data, mem, len);
int bitdepth, channels, samplerate, format;
size_t sz;
const char *p = (const char*)data;
diff --git a/src/3rdparty/wav/wav.h b/src/3rdparty/wav/wav.h
index 0849dfe..c12cd5d 100644
--- a/src/3rdparty/wav/wav.h
+++ b/src/3rdparty/wav/wav.h
@@ -1,5 +1,5 @@
/**
-* Copyright (c) 2015 rxi
+* Copyright (c) 2018 chai
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MIT license. See LICENSE for details.
diff --git a/src/libjin/audio/sdl/audio.cpp b/src/libjin/audio/sdl/audio.cpp
index b5cbc1b..5277a3d 100644
--- a/src/libjin/audio/sdl/audio.cpp
+++ b/src/libjin/audio/sdl/audio.cpp
@@ -24,7 +24,7 @@ namespace audio
zero(spec);
spec.freq = 44100;
spec.format = AUDIO_S16SYS;
- spec.channels = 2;
+ spec.channels = 1;
spec.samples = 2048;
spec.userdata = this;
spec.callback = defaultCallback;
diff --git a/src/libjin/audio/sdl/audio.h b/src/libjin/audio/sdl/audio.h
index 28b73f0..a0e63da 100644
--- a/src/libjin/audio/sdl/audio.h
+++ b/src/libjin/audio/sdl/audio.h
@@ -9,8 +9,6 @@ namespace jin
namespace audio
{
- class SDLSource;
-
class SDLAudio : public Audio
{
diff --git a/src/libjin/audio/sdl/source.cpp b/src/libjin/audio/sdl/source.cpp
index 1537d2f..bfe090b 100644
--- a/src/libjin/audio/sdl/source.cpp
+++ b/src/libjin/audio/sdl/source.cpp
@@ -1,4 +1,5 @@
#include <exception>
+#include <fstream>
#include "../../math/math.h"
#include "../../utils/macros.h"
@@ -12,7 +13,7 @@ namespace jin
namespace audio
{
- struct SDLSourceCommand
+ typedef struct SDLSourceCommand
{
typedef enum Action
{
@@ -38,11 +39,11 @@ namespace audio
SDLSource* source;
};
- typedef enum STATUS
+ typedef MASK enum STATUS
{
- PLAYING = 2,
- PAUSED = 4,
- STOPPED = 8
+ PLAYING = 1,
+ PAUSED = 2,
+ STOPPED = 4
};
#define Command SDLSourceCommand
@@ -54,26 +55,56 @@ namespace audio
shared std::vector<SDLSource*> Manager::sources;
shared Manager* Manager::manager = nullptr;
- SDLSource::SDLSource(Type format, void* mem, int size)
+ SDLSource* SDLSource::createSource(SourceType format, const char* file)
{
- memset(&status, 0, sizeof(status));
- memset(&raw, 0, sizeof(raw));
+ std::ifstream fs(file, std::ios::binary);
+ fs.seekg(0,std::ios::end);
+ int size = fs.tellg();
+ fs.seekg(0, std::ios::beg);
+ char* buffer = (char*)malloc(size);
+ memset(buffer, 0, size);
+ fs.read(buffer, size);
+ SDLSource* source = createSource(format, buffer, size);
+ free(buffer);
+ return source;
+ }
+
+ SDLSource* SDLSource::createSource(SourceType format, void* mem, size_t size)
+ {
+ if (mem == nullptr)
+ return nullptr;
+ SDLSource* source = new SDLSource();
+ memset(&source->status, 0, sizeof(status));
+ memset(&source->raw, 0, sizeof(raw));
try
{
switch (format)
{
case WAV:
- loadWAV(mem, size);
+ source->loadWAV(mem, size);
break;
case OGG:
- loadOGG(mem, size);
+ source->loadOGG(mem, size);
break;
}
}
- catch(SourceException& exp)
+ catch (SourceException& exp)
{
-
+ delete source;
+ return nullptr;
};
+ return source;
+ }
+
+ SDLSource::SDLSource()
+ {
+ }
+
+ SDLSource::~SDLSource()
+ {
+ delete raw.data;
+ raw.end = 0;
+ raw.data = 0;
}
void SDLSource::loadWAV(void* mem, int size)
@@ -81,7 +112,7 @@ namespace audio
wav_t wav;
if (wav_read(&wav, mem, size) == 0)
{
- raw.data = wav.data;
+ raw.data = wav.data;
raw.size = wav.length * wav.bitdepth / 8;
raw.end = (char*)raw.data + raw.size;
raw.rate = wav.samplerate;
@@ -108,12 +139,6 @@ namespace audio
}
}
- SDLSource::~SDLSource()
- {
-
- }
-
-
#define ActionNone(T)\
do{\
Command* cmd = Manager::get()->getCommand();\
diff --git a/src/libjin/audio/sdl/source.h b/src/libjin/audio/sdl/source.h
index 29a3676..900568f 100644
--- a/src/libjin/audio/sdl/source.h
+++ b/src/libjin/audio/sdl/source.h
@@ -13,22 +13,18 @@ namespace jin
namespace audio
{
- struct SDLSourceCommand;
+ typedef struct SDLSourceCommand;
class SDLSource : public Source
{
public:
- enum Type
- {
- WAV = 1,
- OGG = 2,
- };
-
- SDLSource(Type format, void* mem, int size) ;
~SDLSource();
+ static SDLSource* createSource(SourceType format, const char* file);
+ static SDLSource* createSource(SourceType format, void* mem, size_t size);
+
/* ISource interface */
void play() override;
void stop() override;
@@ -45,6 +41,8 @@ namespace audio
private:
+ SDLSource();
+
friend class SDLSourceManager;
void loadWAV(void* mem, int size);
diff --git a/src/libjin/audio/source.h b/src/libjin/audio/source.h
index 2933fe9..10756a0 100644
--- a/src/libjin/audio/source.h
+++ b/src/libjin/audio/source.h
@@ -8,6 +8,12 @@ namespace jin
namespace audio
{
+ enum SourceType
+ {
+ WAV,
+ OGG,
+ };
+
class ISource
{
diff --git a/src/libjin/common/factory.h b/src/libjin/common/factory.h
new file mode 100644
index 0000000..af22605
--- /dev/null
+++ b/src/libjin/common/factory.h
@@ -0,0 +1,12 @@
+#ifndef __JIN_COMMON_FACTORY_H
+#define __JIN_COMMON_FACTORY_H
+
+class Factory
+{
+
+public:
+
+
+};
+
+#endif \ No newline at end of file
diff --git a/src/libjin/utils/macros.h b/src/libjin/utils/macros.h
index 5f0b3b8..4f86807 100644
--- a/src/libjin/utils/macros.h
+++ b/src/libjin/utils/macros.h
@@ -6,6 +6,8 @@
#define shared // ķ
+#define MASK // enum
+
#define CallOnce(func) static char __dummy__=(func, 1) // ֻһ
#define onlyonce // ֻһ
diff --git a/src/libjin/utils/unittest.cpp b/src/libjin/utils/unittest.cpp
index 2c2985b..ecddffd 100644
--- a/src/libjin/utils/unittest.cpp
+++ b/src/libjin/utils/unittest.cpp
@@ -9,26 +9,20 @@
using namespace jin::audio;
using namespace std;
-int length = 2226052;
-char buf[2226052];
+
int main(int argc, char* argv[])
{
Audio* audio = SDLAudio::get();
audio->init(0);
- ifstream fs;
- fs.open("a.ogg", ios::binary);
- fs.read(buf, length);
- SDLSource source(SDLSource::OGG, buf, length);
- source.play();
- source.setLoop(true);
+ //SDLSource* source = SDLSource::createSource(SourceType::OGG, "a.ogg");
+ SDLSource* source2 = SDLSource::createSource(SourceType::WAV, "a.wav");
+ source2->play();
+ source2->setLoop(true);
+ //source->play();
+ //source->setLoop(true);
int i = 0;
while (true)
{
- i += 1;
- if (i == 10)
- {
- source.resume();
- }
SDL_Delay(1000);
}
audio->quit();