2019-04-11 14:40:54 +08:00
|
|
|
|
// Copyright (c) 2016-2018 Kiwano - Nomango
|
2020-01-21 10:09:55 +08:00
|
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
2020-01-21 10:09:55 +08:00
|
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
|
// all copies or substantial portions of the Software.
|
2020-01-21 10:09:55 +08:00
|
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
2020-01-21 10:09:55 +08:00
|
|
|
|
#include <kiwano-audio/Transcoder.h>
|
2019-11-13 14:33:15 +08:00
|
|
|
|
#include <kiwano/core/ObjectBase.h>
|
|
|
|
|
|
#include <kiwano/core/Resource.h>
|
2019-12-31 10:37:29 +08:00
|
|
|
|
#include <kiwano/platform/win32/ComPtr.hpp>
|
2019-11-13 11:22:21 +08:00
|
|
|
|
#include <xaudio2.h>
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
|
namespace kiwano
|
2019-03-31 01:37:06 +08:00
|
|
|
|
{
|
2020-01-21 10:09:55 +08:00
|
|
|
|
namespace audio
|
|
|
|
|
|
{
|
2020-02-14 17:56:50 +08:00
|
|
|
|
class AudioModule;
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
KGE_DECLARE_SMART_PTR(Sound);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \addtogroup Audio
|
|
|
|
|
|
* @{
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
* @brief 音频对象
|
2020-01-21 10:09:55 +08:00
|
|
|
|
*/
|
2020-02-20 18:22:52 +08:00
|
|
|
|
class KGE_API Sound : public ObjectBase
|
2020-01-21 10:09:55 +08:00
|
|
|
|
{
|
2020-02-14 17:56:50 +08:00
|
|
|
|
friend class AudioModule;
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
public:
|
2020-02-06 16:54:47 +08:00
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 创建音频对象
|
|
|
|
|
|
/// @param res 本地音频文件路径
|
2020-02-19 12:09:50 +08:00
|
|
|
|
static SoundPtr Create(const String& file_path);
|
2020-02-06 16:54:47 +08:00
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 创建音频对象
|
|
|
|
|
|
/// @param res 音频资源
|
2020-02-19 12:09:50 +08:00
|
|
|
|
static SoundPtr Create(const Resource& res);
|
2020-02-06 16:54:47 +08:00
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
|
Sound();
|
|
|
|
|
|
|
|
|
|
|
|
virtual ~Sound();
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 打开本地音频文件
|
|
|
|
|
|
/// @param res 本地音频文件路径
|
2020-02-19 12:09:50 +08:00
|
|
|
|
bool Load(const String& file_path);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 打开音频资源
|
|
|
|
|
|
/// @param res 音频资源
|
2020-02-19 12:09:50 +08:00
|
|
|
|
bool Load(const Resource& res);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 是否有效
|
2020-01-21 10:09:55 +08:00
|
|
|
|
bool IsValid() const;
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 播放
|
|
|
|
|
|
/// @param loop_count 播放循环次数,设置 -1 为循环播放
|
2020-01-21 10:09:55 +08:00
|
|
|
|
void Play(int loop_count = 0);
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 暂停
|
2020-01-21 10:09:55 +08:00
|
|
|
|
void Pause();
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 继续
|
2020-01-21 10:09:55 +08:00
|
|
|
|
void Resume();
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 停止
|
2020-01-21 10:09:55 +08:00
|
|
|
|
void Stop();
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 关闭并销毁资源
|
2020-01-21 10:09:55 +08:00
|
|
|
|
void Close();
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 是否正在播放
|
2020-01-21 10:09:55 +08:00
|
|
|
|
bool IsPlaying() const;
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 获取音量
|
2020-01-21 10:09:55 +08:00
|
|
|
|
float GetVolume() const;
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 设置音量
|
|
|
|
|
|
/// @param volume 音量大小,1.0 为原始音量, 大于 1 为放大音量, 0 为最小音量
|
2020-01-21 10:09:55 +08:00
|
|
|
|
void SetVolume(float volume);
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
IXAudio2SourceVoice* GetXAudio2Voice() const;
|
|
|
|
|
|
|
|
|
|
|
|
void SetXAudio2Voice(IXAudio2SourceVoice* voice);
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
bool opened_;
|
|
|
|
|
|
bool playing_;
|
|
|
|
|
|
Transcoder transcoder_;
|
|
|
|
|
|
IXAudio2SourceVoice* voice_;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
|
|
|
|
inline IXAudio2SourceVoice* Sound::GetXAudio2Voice() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return voice_;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void Sound::SetXAudio2Voice(IXAudio2SourceVoice* voice)
|
|
|
|
|
|
{
|
|
|
|
|
|
voice_ = voice;
|
2019-03-31 01:37:06 +08:00
|
|
|
|
}
|
2020-01-21 10:09:55 +08:00
|
|
|
|
} // namespace audio
|
|
|
|
|
|
} // namespace kiwano
|