-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRendering.cpp
95 lines (72 loc) · 2.88 KB
/
Rendering.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/** $VER: Rendering.cpp (2024.07.03) P. Stuer **/
#include "pch.h"
#include "UIElement.h"
#include "Encoding.h"
#include "Exceptions.h"
#include "Support.h"
#pragma hdrstop
/// <summary>
/// Starts the timer.
/// </summary>
void UIElement::StartTimer() noexcept
{
::SetTimer(m_hWnd, (UINT_PTR) this, 1000 / (DWORD) 50, (TIMERPROC) TimerCallback);
}
/// <summary>
/// Stops the timer.
/// </summary>
void UIElement::StopTimer() noexcept
{
::KillTimer(m_hWnd, (UINT_PTR) this);
}
/// <summary>
/// Handles a timer tick.
/// </summary>
void CALLBACK UIElement::TimerCallback(HWND hWnd, UINT msg, UINT_PTR timerId, DWORD time) noexcept
{
((UIElement *) timerId)->OnTimer();
}
/// <summary>
/// Handles a timer tick.
/// </summary>
void UIElement::OnTimer() noexcept
{
if (_IsFrozen || _IsHidden || ::IsIconic(core_api::get_main_window()) || (_WebView == nullptr) || !_IsNavigationCompleted)
return;
double PlaybackTime; // in seconds
if (!_VisualisationStream.is_valid() || !_VisualisationStream->get_absolute_time(PlaybackTime) || (PlaybackTime == _LastPlaybackTime))
return;
_LastPlaybackTime = PlaybackTime;
audio_chunk_impl Chunk;
const double WindowSize = _Configuration._WindowSize / ((_Configuration._WindowSizeUnit == WindowSizeUnit::Milliseconds) ? 1000. : (double) _SampleRate); // in seconds
const double WindoOffset = PlaybackTime - (WindowSize * (0.5 + _Configuration._ReactionAlignment)); // in seconds
if (!_VisualisationStream->get_chunk_absolute(Chunk, WindoOffset, WindowSize))
return;
const audio_sample * Samples = Chunk.get_data();
size_t SampleCount = Chunk.get_sample_count();
_SampleRate = Chunk.get_sample_rate();
uint32_t ChannelCount = Chunk.get_channel_count();
uint32_t ChannelConfig = Chunk.get_channel_config();
HRESULT hr = PostChunk(Samples, SampleCount, _SampleRate, ChannelCount, ChannelConfig);
if (!SUCCEEDED(hr))
return;
hr = _WebView->ExecuteScript(::FormatText(L"onTimer(%d, %d, %d, %d)", SampleCount, _SampleRate, ChannelCount, ChannelConfig).c_str(), nullptr); // Silently continue
if (!SUCCEEDED(hr))
{
console::print(::GetErrorMessage(hr, STR_COMPONENT_BASENAME " failed to call onTimer()").c_str());
StopTimer();
}
}
/// <summary>
/// Posts a chunk to the script via a shared buffer.
/// </summary>
HRESULT UIElement::PostChunk(const audio_sample * samples, size_t sampleCount, uint32_t sampleRate, uint32_t channelCount, uint32_t channelConfig) noexcept
{
HRESULT hr = _SharedBuffer.Ensure(_Environment, _WebView, sampleCount, sampleRate, channelCount, channelConfig);
if (SUCCEEDED(hr))
if (audio_sample_size == 64)
_SharedBuffer.Copy((const BYTE *) samples, sizeof(audio_sample) * sampleCount * channelCount);
else
_SharedBuffer.Convert((const float *) samples, sampleCount);
return S_OK;
}