modify audio render

This commit is contained in:
jiegeaiai 2024-10-14 22:54:55 +08:00
parent ffb26ec077
commit 2879e634f1
8 changed files with 65 additions and 14 deletions

View File

@ -6,9 +6,15 @@
#include "IAudioRender.h"
static std::unique_ptr<IAudioRender> audiosRender_;
bool __stdcall Initialize(const char* sender_name, const char* receiver_name) {
bool __stdcall Initialize(callback_t cb) {
assert(!audiosRender_);
audiosRender_.reset(IAudioRender::Create());
auto logCallback = [cb](int32 level, const int8* log, uint32 length) {
if (nullptr == cb) {
cb(level, log, length);
}
};
audiosRender_.reset(IAudioRender::Create(std::move(logCallback)));
return true;
}

View File

@ -11,8 +11,8 @@
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
AUDIO_RENDER_EXPORT bool __stdcall Initialize(const char* sender_name, const char* receiver_name);
typedef void (*callback_t)(int, const char*, int);
AUDIO_RENDER_EXPORT bool __stdcall Initialize(callback_t cb);
AUDIO_RENDER_EXPORT bool __stdcall Write(const unsigned char* data, unsigned int len);
AUDIO_RENDER_EXPORT void __stdcall Uninitialize();

View File

@ -46,7 +46,7 @@
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
@ -120,6 +120,8 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;WIN32;AUDIO_RENDER_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>D:\Project\AudioRender\AudioRender;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<Optimization>Disabled</Optimization>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>

View File

@ -1,7 +1,22 @@
#include "AudioRenderStd.h"
#include <sstream>
#include "Windows/WavAudioRender.h"
IAudioRender* IAudioRender::Create() {
return new WavAudioRender;
IAudioRender* IAudioRender::Create(LogCallback callback) {
return new WavAudioRender(std::move(callback));
}
AudioRenderStd::AudioRenderStd(LogCallback callback) noexcept
: callback_(std::move(callback)){
}
void AudioRenderStd::WirteLog(int32 level, const std::string& log) {
if (nullptr == callback_) {
return;
}
callback_(level, log.c_str(), static_cast<uint32>(log.length()));
}

View File

@ -1,12 +1,25 @@
#pragma once
#include <string>
#include "IAudioRender.h"
class AudioRenderStd : public IAudioRender {
public:
enum LOG_LEVEL {
DEBUG,
LOG
};
public:
explicit AudioRenderStd(LogCallback callback) noexcept;
~AudioRenderStd() override = default;
uint64 GetClock() override {
return 0;
}
void WirteLog(int32 level, const std::string& log);
private:
LogCallback callback_{ nullptr };
};

View File

@ -1,6 +1,7 @@
#pragma once
#include <vector>
#include <functional>
using int8 = char;
using uint8 = unsigned char;
@ -20,7 +21,8 @@ struct AudioFrame {
class IAudioRender {
public:
static IAudioRender* Create();
using LogCallback = std::function<void(int32, const int8*, uint32)>;
static IAudioRender* Create(LogCallback callback);
public:
virtual ~IAudioRender() = default;

View File

@ -30,7 +30,8 @@ static void FreeBlocks(WAVEHDR* blockArray) {
HeapFree(GetProcessHeap(), 0, blockArray);
}
WavAudioRender::WavAudioRender() noexcept {
WavAudioRender::WavAudioRender(LogCallback callback) noexcept
: AudioRenderStd(callback){
HWAVEOUT hWaveOut = nullptr;
waveBlocks_ = AllocateBlocks(BlockSize_, BlockCount_);
@ -52,15 +53,20 @@ WavAudioRender::WavAudioRender() noexcept {
MMRESULT hr = waveOutOpen(&hWaveOut, WAVE_MAPPER, &waveform,
reinterpret_cast<DWORD_PTR>(WavAudioRender::waveOutProc),
reinterpret_cast<DWORD_PTR>(this), CALLBACK_FUNCTION);
if (MMSYSERR_NOERROR == hr) {
if (MMSYSERR_NOERROR != hr) {
WirteLog(DEBUG, "init success");
return;
}
hWavout_ = std::move(hWaveOut);
initialized_ = true;
WirteLog(DEBUG, "init success");
}
WavAudioRender::~WavAudioRender() {
initialized_ = false;
if (nullptr == hWavout_) {
WirteLog(DEBUG, "hWavout is nullptr");
return;
}
waveOutReset(hWavout_);
@ -83,10 +89,13 @@ WavAudioRender::~WavAudioRender() {
}
} while (true);
FreeBlocks(waveBlocks_);
WirteLog(DEBUG, "uninitialized success");
}
bool WavAudioRender::Write(const AudioFrame& audioFrame) {
WirteLog(DEBUG, "WavAudioRender::Write");
if (nullptr == hWavout_) {
WirteLog(DEBUG, "hWavout_ is nullptr");
return false;
}
@ -116,9 +125,12 @@ bool WavAudioRender::Write(const AudioFrame& audioFrame) {
--freeBlockCounter_;
}
while (!freeBlockCounter_) {
while (!freeBlockCounter_ && initialized_) {
Sleep(1);
}
if (!initialized_) {
break;
}
++waveCurrentBlock_;
waveCurrentBlock_ %= BlockCount_;

View File

@ -6,7 +6,7 @@
class WavAudioRender : public AudioRenderStd {
public:
explicit WavAudioRender() noexcept;
explicit WavAudioRender(LogCallback callback) noexcept;
~WavAudioRender() override;
bool Write(const AudioFrame& audioFrame) override;
@ -19,7 +19,7 @@ private:
private:
int64_t audioPts_{ 0 };
static constexpr int BlockSize_{ 6400 };
static constexpr int BlockSize_{ 3200 };
static constexpr int BlockCount_{ 10 };
SectionLock lock_;
@ -28,4 +28,5 @@ private:
WAVEHDR* waveBlocks_{ nullptr };
HWAVEOUT hWavout_{ nullptr };
bool initialized_{ false };
};