Skip to content

Commit

Permalink
・BGMの区間ループ対応
Browse files Browse the repository at this point in the history
・コメント修正
  • Loading branch information
tokineco committed Jun 10, 2016
1 parent f7a3d2c commit 87acd3c
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 31 deletions.
61 changes: 54 additions & 7 deletions AudioManager.cpp
@@ -1,10 +1,15 @@
// BGM: Win32-ogg, wav use SimpleAudioEngine. Other format use AudioEngine.
// SE : Win32-wav use SimpleAudioEngine. Other format use AudioEngine.
/****************************************************************************
Copyright (c) 2016 Yuji Toki(tokineco)
- MIT license
BGM: Win32-ogg, wav use SimpleAudioEngine. Other format use AudioEngine.
SE: Win32-wav use SimpleAudioEngine. Other format use AudioEngine.
****************************************************************************/

#pragma execution_character_set("utf-8")

#include "SimpleAudioEngine.h" // Windows-ogg, wav
#include "audio/include/AudioEngine.h" // iOS, Android
#include "SimpleAudioEngine.h" // BGM:Windows-ogg, wav, SE:Win32-wav
#include "audio/include/AudioEngine.h"
#include "json/rapidjson.h"
#include "json/document.h"

Expand Down Expand Up @@ -77,16 +82,17 @@ bool AudioManager::readAudioListFile(const std::string fileName) {

if (doc.HasParseError()) {
// 解析エラー
log("JSON error : %u", doc.GetParseError());
CCLOG("JSON parse error.");
return false;
}

if (doc.IsObject()) {

log("%s", strData.c_str());
CCLOG("%s", strData.c_str());

// 初期化
_bgmList.clear();
_bgmLoopList.clear();
_seList.clear();

// BGM
Expand All @@ -96,9 +102,25 @@ bool AudioManager::readAudioListFile(const std::string fileName) {
for (rapidjson::Value::ConstMemberIterator it = bgms.MemberBegin(); it != bgms.MemberEnd(); it++) {
std::string key = it->name.GetString();
const rapidjson::Value& value = it->value;
// 通常のファイルパスの場合
if (value.GetType() == rapidjson::kStringType) {
_bgmList[key] = value.GetString();
}
// 配列の場合
else if (value.GetType() == rapidjson::kArrayType) {

// 1番目はファイルパス
_bgmList[key] = value[0].GetString();
// 2番目はループ後の再生開始位置
if (value.Size() > 1) {
_bgmLoopList[key][0] = (float)(value[1].GetDouble());
}
// 3番目はループ終端位置
if (value.Size() > 2) {
_bgmLoopList[key][1] = (float)(value[2].GetDouble());
}

}
}

// SE
Expand Down Expand Up @@ -192,7 +214,7 @@ std::string AudioManager::getFileName(AudioType type, std::string baseName) {
}

// それでも見つからなければ空文字を返して、その先でエラーとする
log("file not found %s.", baseName.c_str());
CCLOG("file not found %s.", baseName.c_str());
return baseName;

}
Expand Down Expand Up @@ -274,6 +296,31 @@ void AudioManager::update(float dt) {
default:
break;
}

// ループチェック
if (this->isPlayingBgm() && _bgmLoopList.count(_bgmFileName) > 0) {

std::string fileName = getFileName(AudioType::BGM, _bgmFileName);

if (fileName != "" && !isSimpleAudioEngine(AudioType::BGM, fileName)) {
// 現在のBGM情報を取得
float currentTime = AudioEngine::getCurrentTime(_bgmId); // 現在の位置
float duration = AudioEngine::getDuration(_bgmId); // オーディオの長さ

// 区間設定情報
float startPos = _bgmLoopList[_bgmFileName][0];
float endPos = duration;
if (sizeof(_bgmLoopList[_bgmFileName]) / sizeof(_bgmLoopList[_bgmFileName][0]) > 1) {
endPos = _bgmLoopList[_bgmFileName][1];
}

if (currentTime >= endPos) {
CCLOG("loop and move. current time is %f sec.", startPos);
AudioEngine::setCurrentTime(_bgmId, startPos);
}
}
}

}


Expand Down
11 changes: 9 additions & 2 deletions AudioManager.h
@@ -1,5 +1,10 @@
// BGM: Win32-ogg, wav use SimpleAudioEngine. Other format use AudioEngine.
// SE: Win32-wav use SimpleAudioEngine. Other format use AudioEngine.
/****************************************************************************
Copyright (c) 2016 Yuji Toki(tokineco)
- MIT license
BGM: Win32-ogg, wav use SimpleAudioEngine. Other format use AudioEngine.
SE: Win32-wav use SimpleAudioEngine. Other format use AudioEngine.
****************************************************************************/

#ifndef __AudioManager__
#define __AudioManager__
Expand Down Expand Up @@ -31,6 +36,8 @@ class AudioManager {

// BGMファイルリスト
std::map<std::string, std::string> _bgmList;
// BGMファイル区間設定リスト
std::map<std::string, float[2]> _bgmLoopList;
// SEファイルリスト
std::map<std::string, std::string> _seList;

Expand Down
11 changes: 6 additions & 5 deletions Converter.cpp
@@ -1,6 +1,7 @@
//
// Converter.cpp
//
/****************************************************************************
Copyright (c) 2016 Yuji Toki(tokineco)
- MIT license
****************************************************************************/

#pragma execution_character_set("utf-8")

Expand Down Expand Up @@ -29,11 +30,11 @@ cocos2d::Color4B Converter::fromARGB(std::string code) {

} catch (...) {
// Error
log("illegal color code : %s", code.c_str());
CCLOG("illegal color code : %s", code.c_str());
}
} else {
// Error
log("not supeert format : %s", code.c_str());
CCLOG("not supeert format : %s", code.c_str());
}

return cocos2d::Color4B::BLACK;
Expand Down
7 changes: 4 additions & 3 deletions Converter.h
@@ -1,6 +1,7 @@
//
// Converter.h
//
/****************************************************************************
Copyright (c) 2016 Yuji Toki(tokineco)
- MIT license
****************************************************************************/

#ifndef __Converter__
#define __Converter__
Expand Down
6 changes: 4 additions & 2 deletions GameHelper2dx.h
@@ -1,6 +1,7 @@
/****************************************************************************
Copyright (c) 2016 Yuji Toki(tokineco)
* required Cocos2d-x v3.8 over.
Copyright (c) 2016 Yuji Toki(tokineco)
- required Cocos2d-x v3.8 over
- MIT license
****************************************************************************/

#ifndef __GAMEHELPER2DX__
Expand All @@ -10,6 +11,7 @@ Copyright (c) 2016 Yuji Toki(tokineco)
#include "AudioManager.h"
#include "Converter.h"
#include "MathHelper.h"

//#include "SDKBOXHelper.h"

#endif /* defined(__GAMEHELPER2DX__) */
7 changes: 4 additions & 3 deletions MathHelper.cpp
@@ -1,6 +1,7 @@
//
// MathHelper.cpp
//
/****************************************************************************
Copyright (c) 2016 Yuji Toki(tokineco)
- MIT license
****************************************************************************/

#pragma execution_character_set("utf-8")

Expand Down
7 changes: 4 additions & 3 deletions MathHelper.h
@@ -1,6 +1,7 @@
//
// MathHelper.h
//
/****************************************************************************
Copyright (c) 2016 Yuji Toki(tokineco)
- MIT license
****************************************************************************/

#ifndef __MathHelper__
#define __MathHelper__
Expand Down
13 changes: 10 additions & 3 deletions SDKBOXHelper.cpp
@@ -1,6 +1,13 @@
//
// SDKBOXHelper.cpp
//
/****************************************************************************
Copyright (c) 2016 Yuji Toki(tokineco)
- MIT license
- In-App Purchase
- Google Analytics
* 使用しない場合はプロジェクトから外してください
****************************************************************************/

#pragma execution_character_set("utf-8")

#include "SDKBOXHelper.h"
Expand Down
13 changes: 10 additions & 3 deletions SDKBOXHelper.h
@@ -1,6 +1,13 @@
//
// SDKBOXHelper.h
//
/****************************************************************************
Copyright (c) 2016 Yuji Toki(tokineco)
- MIT license
- In-App Purchase
- Google Analytics
* 使用しない場合はプロジェクトから外してください
****************************************************************************/

#ifndef __SDKBOXHelper__
#define __SDKBOXHelper__

Expand Down

0 comments on commit 87acd3c

Please sign in to comment.