Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 3 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
View
69 coolaudio/android_log.h
@@ -0,0 +1,69 @@
+/**
+ * @file android_log.h
+ * @author Kun Wang <ifreedom.cn@gmail.com>
+ * @date 2012/05/20 04:57:49
+ *
+ * Copyright 2012 Kun Wang <ifreedom.cn@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef _ANDROID_LOG___H
+#define _ANDROID_LOG___H
+
+#ifndef LOG_TAG
+#error "NO Log tag!"
+#endif
+
+static inline void LOGI(const char* fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+ __android_log_vprint(ANDROID_LOG_INFO,LOG_TAG, fmt, args);
+ va_end(args);
+}
+static inline void LOGE(const char* fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+ __android_log_vprint(ANDROID_LOG_ERROR,LOG_TAG, fmt, args);
+ va_end(args);
+}
+
+#define LOG_ERROR(str) \
+ if(alGetError() != AL_NO_ERROR) \
+ { \
+ LOGE(str); \
+ }
+
+#define LOG_ERROR_RET(str, ret) \
+ if(alGetError() != AL_NO_ERROR) \
+ { \
+ LOGE(str); \
+ return ret; \
+ }
+
+#define LOG_ERROR_GOTO(str) \
+ if(alGetError() != AL_NO_ERROR) \
+ { \
+ LOGE(str); \
+ goto error; \
+ }
+
+#define LOG_FUN_ERR() \
+ LOGE("%s(%d)-%s\n", __FILE__, __LINE__, __FUNCTION__)
+
+#define LOG_ASSERT(expr) \
+ do { if (!(expr)) { LOG_FUN_ERR(); } } while(0)
+
+#endif /* _ANDROID_LOG___H */
View
60 coolaudio/audio_openal.cpp
@@ -6,6 +6,7 @@
* Copyright 2012 Kun Wang <ifreedom.cn@gmail.com>
*
*/
+#include "nge_debug_log.h"
#include "audio_openal.h"
#ifndef _MSC_VER
@@ -16,9 +17,12 @@
#include <errno.h>
#include <time.h>
#include <assert.h>
-#include "nge_debug_log.h"
#include <list>
+#include <android/log.h>
+#define LOG_TAG "libcoolaudio"
+#include "android_log.h"
+
typedef pthread_mutex_t CRITICAL_SECTION;
void EnterCriticalSection(CRITICAL_SECTION *cs);
void LeaveCriticalSection(CRITICAL_SECTION *cs);
@@ -29,13 +33,13 @@ void EnterCriticalSection(CRITICAL_SECTION *cs)
{
int ret;
ret = pthread_mutex_lock(cs);
- assert(ret == 0);
+ LOG_ASSERT(ret == 0);
}
void LeaveCriticalSection(CRITICAL_SECTION *cs)
{
int ret;
ret = pthread_mutex_unlock(cs);
- assert(ret == 0);
+ LOG_ASSERT(ret == 0);
}
void InitializeCriticalSection(CRITICAL_SECTION *cs)
{
@@ -43,16 +47,17 @@ void InitializeCriticalSection(CRITICAL_SECTION *cs)
int ret;
ret = pthread_mutexattr_init(&attrib);
- assert(ret == 0);
+ LOG_ASSERT(ret == 0);
ret = pthread_mutexattr_settype(&attrib, PTHREAD_MUTEX_RECURSIVE);
#ifdef HAVE_PTHREAD_NP_H
if(ret != 0)
ret = pthread_mutexattr_setkind_np(&attrib, PTHREAD_MUTEX_RECURSIVE);
#endif
- assert(ret == 0);
+ LOG_ASSERT(ret == 0);
+
ret = pthread_mutex_init(cs, &attrib);
- assert(ret == 0);
+ LOG_ASSERT(ret == 0);
pthread_mutexattr_destroy(&attrib);
}
@@ -60,7 +65,7 @@ void DeleteCriticalSection(CRITICAL_SECTION *cs)
{
int ret;
ret = pthread_mutex_destroy(cs);
- assert(ret == 0);
+ LOG_ASSERT(ret == 0);
}
#endif
@@ -73,7 +78,7 @@ extern "C" BOOLEAN InitAL() {
{
alcGetError(NULL);
- nge_log("Audio Device open failed");
+ LOGE("Audio Device open failed");
return FALSE;
}
@@ -84,13 +89,15 @@ extern "C" BOOLEAN InitAL() {
alcDestroyContext(context);
alcCloseDevice(device);
- nge_log("Audio Context setup failed");
+ LOGE("Audio Context setup failed");
return FALSE;
}
alcGetError(device);
if (StartThread() != TRUE)
return FALSE;
+
+ LOGI("Audio Context setup success");
return TRUE;
}
@@ -120,7 +127,7 @@ extern "C" void DeInitAL() {
void ALSleep(float duration) {
if(duration < 0.0f)
{
- nge_log("ALSleep Invalid duration");
+ LOGE("ALSleep Invalid duration");
return;
}
@@ -137,15 +144,33 @@ void ALSleep(float duration) {
static pthread_t thread;
static CRITICAL_SECTION cs;
+static pthread_cond_t non_empty;
static bool need_run = true;
-static std::list<IPlayer*> playerList;
+static std::list<IPlayer*> playerList, deleteList;
static void* ThreadFunc(void* ptr) {
while (need_run) {
LockAudio();
+
+ if (playerList.empty()) {
+ pthread_cond_wait(&non_empty, &cs);
+ }
+
std::list<IPlayer*>::iterator iter = playerList.begin();
- for (;iter != playerList.end(); iter++)
- (*iter)->op->CheckUpdate(*iter);
+ for (;iter != playerList.end(); iter++) {
+ if ((*iter)->op->CheckUpdate(*iter) != 0) {
+ deleteList.push_back(*iter);
+ }
+ }
+
+ if (!deleteList.empty()) {
+ iter = deleteList.begin();
+ for (;iter != deleteList.end(); iter++) {
+ playerList.remove(*iter);
+ }
+ deleteList.clear();
+ }
+
UnlockAudio();
ALSleep(0.01f);
}
@@ -154,9 +179,9 @@ static void* ThreadFunc(void* ptr) {
BOOLEAN StartThread() {
need_run = true;
+ InitializeCriticalSection(&cs);
if(pthread_create(&thread, NULL, ThreadFunc, NULL) != 0)
return FALSE;
- InitializeCriticalSection(&cs);
return TRUE;
}
@@ -175,8 +200,15 @@ extern "C" inline void UnlockAudio() {
}
extern "C" void AddActivePlayer(IPlayer* player) {
+ bool empty = false;
LockAudio();
+ if (playerList.empty())
+ empty = true;
+
playerList.push_back(player);
+
+ if (empty)
+ pthread_cond_signal(&non_empty);
UnlockAudio();
}
View
2  coolaudio/audio_openal.h
@@ -24,7 +24,7 @@ typedef struct {
} IPlayer;
struct PlayerOperation_s {
- void (*CheckUpdate)(IPlayer* player);
+ int (*CheckUpdate)(IPlayer* player);
};
#ifdef __cplusplus
View
53 coolaudio/audio_openal_wav.c
@@ -16,31 +16,7 @@
#include <android/log.h>
#define LOG_TAG "libcoolaudio"
-static inline void LOGI(const char* fmt, ...) {
- va_list args;
- va_start(args, fmt);
- __android_log_vprint(ANDROID_LOG_INFO,LOG_TAG, fmt, args);
- va_end(args);
-}
-static inline void LOGE(const char* fmt, ...) {
- va_list args;
- va_start(args, fmt);
- __android_log_vprint(ANDROID_LOG_ERROR,LOG_TAG, fmt, args);
- va_end(args);
-}
-
-#define LOG_ERROR(str) \
- if(alGetError() != AL_NO_ERROR) \
- { \
- LOGE(str); \
- }
-
-#define LOG_ERROR_RET(str, ret) \
- if(alGetError() != AL_NO_ERROR) \
- { \
- LOGE(str); \
- return ret; \
- }
+#include "android_log.h"
#define ID_RIFF 0x46464952
#define ID_WAVE 0x45564157
@@ -135,7 +111,7 @@ typedef struct {
BOOLEAN isEof;
} AudioOpenALWav;
-void WavCheckUpdate(IPlayer* player);
+int WavCheckUpdate(IPlayer* player);
PlayerOperation WavPlayerOperation = {
WavCheckUpdate
};
@@ -201,10 +177,11 @@ audio_play_p CreateWavPlayer() {
a->times = 0;
a->isEof = FALSE;
+ LOGI("Create wav player success\n");
return &(a->op);
}
-static void audio_callback(THIS_DEF) {
+static int audio_callback(THIS_DEF) {
GET_AUDIO_FROM_THIS(audio);
if (audio->times == 0) /* loop? */ {
@@ -212,8 +189,8 @@ static void audio_callback(THIS_DEF) {
}
else if (audio->times == 1) {
audio->isEof = TRUE;
- wav_stop(This);
- return;
+ LOGI("wav playing complete\n");
+ return -1;
}
else {
audio->times--;
@@ -221,15 +198,19 @@ static void audio_callback(THIS_DEF) {
}
replay:
+ alSourcePause(audio->source);
alSourceRewind(audio->source);
alSourcePlay(audio->source);
+ return 0;
}
#define _METHOD(ret, method, arg) ret wav_##method arg
_METHOD(int, load, (THIS_DEF, const char* filename)) {
int fd = io_fopen(filename, IO_RDONLY);
- return wav_load_fp(This, fd, 1);
+ int ret = wav_load_fp(This, fd, TRUE);
+ LOGI("Load %s\n", filename);
+ return ret;
}
_METHOD(int, load_buf, (THIS_DEF, const char* buf, int size)) {
@@ -265,10 +246,10 @@ _METHOD(int, load_buf, (THIS_DEF, const char* buf, int size)) {
alDeleteBuffers(1, &audio->buffer);
}
alGenBuffers(1, &audio->buffer);
- LOG_ERROR_RET("Could not create buffers\n", 0);
+ LOG_ERROR_GOTO("Could not create buffers\n");
alBufferData(audio->buffer, format, buf+pos, info->size, info->rate);
- LOG_ERROR_RET("Error buffering data\n", 0);
+ LOG_ERROR_GOTO("Error buffering data\n");
alSourcei(audio->source, AL_BUFFER, audio->buffer);
@@ -313,6 +294,7 @@ _METHOD(int, play, (THIS_DEF, int times, int free_when_stop)) {
alSourcePlay(audio->source);
AddActivePlayer(&audio->alPlayer);
+ LOGI("wav playing play %d times\n", times);
return 0;
}
@@ -359,6 +341,7 @@ _METHOD(void, rewind, (THIS_DEF)) {
GET_AUDIO_FROM_THIS(audio);
CHECK_BUFFER_V(audio);
+ RemoveActivePlayer(&audio->alPlayer);
alSourceRewind(audio->source);
}
@@ -426,11 +409,13 @@ _METHOD(int, destroy, (THIS_DEF)) {
#undef _METHOD
-void WavCheckUpdate(IPlayer* player) {
+int WavCheckUpdate(IPlayer* player) {
GET_AUDIO_FROM_IPLAYER(audio, player);
ALint state = 0;
alGetSourcei(audio->source, AL_SOURCE_STATE, &state);
if (state == AL_STOPPED)
- audio_callback(&audio->op);
+ return audio_callback(&audio->op);
+
+ return 0;
}
View
10 samples/demo13/test.cpp
@@ -268,9 +268,13 @@ int mainloop()
break;
case 6:
- audio[0]->seek(audio[0], -3000, AUDIO_SEEK_CUR);
- nge_print("seek -3000 ms\n");
- step = 0;
+ if (audio[1]->iseof(audio[1])) {
+ audio[1]->playstop(audio[1]);
+ }
+ // for test seek
+ // audio[0]->seek(audio[0], -3000, AUDIO_SEEK_CUR);
+ // nge_print("seek -3000 ms\n");
+ step = 7;
break;
case 7:

No commit comments for this range

Something went wrong with that request. Please try again.