Skip to content

Commit

Permalink
Some wave rendering related fixes and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Falcosoft committed May 3, 2024
1 parent 1f337de commit a00ebea
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 150 deletions.
14 changes: 8 additions & 6 deletions driver/MidiSynth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,15 @@ namespace VSTMIDIDRV {
return keepDriverLoaded != FALSE;
}

inline DWORD DwordMin(DWORD a, DWORD b) //min macro calls bassAsioOut->GetPos() 2 times that can cause problems...
template <typename T>
inline T Min(T a, T b) //min macro calls bassAsioOut->GetPos() 2 times that can cause problems...
{
if (a < b) return a;
return b;
}

inline DWORD DwordMax(DWORD a, DWORD b)
template <typename T>
inline T Max(T a, T b)
{
if (a > b) return a;
return b;
Expand Down Expand Up @@ -594,7 +596,7 @@ namespace VSTMIDIDRV {
if (midiVol[uDeviceID] != 1.0f) {
if ((msg & 0xF0) == 0x90) {
unsigned char velocity = ((unsigned char*)&msg)[2];
velocity = (midiVol[uDeviceID] == 0.0 || velocity == 0) ? 0 : (unsigned char)DwordMax(DWORD(velocity * midiVol[uDeviceID]), 1);
velocity = (midiVol[uDeviceID] == 0.0 || velocity == 0) ? 0 : (unsigned char)Max<DWORD>(DWORD(velocity * midiVol[uDeviceID]), 1);
((unsigned char*)&msg)[2] = velocity;
}
}
Expand Down Expand Up @@ -632,7 +634,7 @@ namespace VSTMIDIDRV {
if (!PreprocessMIDI(uDeviceID, msg))
return;

DWORD tmpTimestamp = useAsio ? DwordMin(bassAsioOut->GetPos() + midiLatency, bufferSize - 1) : (DWORD)((waveOut->GetPos(channels) + midiLatency) % bufferSize);
DWORD tmpTimestamp = useAsio ? Min<DWORD>(bassAsioOut->GetPos() + midiLatency, bufferSize - 1) : (DWORD)((waveOut->GetPos(channels) + midiLatency) % bufferSize);
midiStream->PutMessage(uDeviceID, msg, tmpTimestamp);
}

Expand All @@ -646,7 +648,7 @@ namespace VSTMIDIDRV {
for (DWORD i = 0; i < len; i++) {
if (bufpos[i] > 0x7F || i == len - 1) {
DWORD shortMsg = 0;
memcpy(&shortMsg, &bufpos[startPos], DwordMin(i - startPos + 1, 3));
memcpy(&shortMsg, &bufpos[startPos], Min<DWORD>(i - startPos + 1, 3));
PushMIDI(uDeviceID, shortMsg);
startPos = i;
}
Expand All @@ -659,7 +661,7 @@ namespace VSTMIDIDRV {
if (!PreprocessSysEx(uDeviceID, bufpos, len))
return;

DWORD tmpTimestamp = useAsio ? DwordMin(bassAsioOut->GetPos() + midiLatency, bufferSize - 1) : (DWORD)((waveOut->GetPos(channels) + midiLatency) % bufferSize);
DWORD tmpTimestamp = useAsio ? Min<DWORD>(bassAsioOut->GetPos() + midiLatency, bufferSize - 1) : (DWORD)((waveOut->GetPos(channels) + midiLatency) % bufferSize);
midiStream->PutSysEx(uDeviceID, bufpos, len, tmpTimestamp);
}

Expand Down
18 changes: 4 additions & 14 deletions driver/VSTDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ UINT GetWaveOutDeviceId() {
#pragma warning(disable:28159)
bool IsWinNT4()
{
OSVERSIONINFOEX osvi;
BOOL bOsVersionInfoEx;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
OSVERSIONINFOEX osvi = { 0 };
BOOL bOsVersionInfoEx;
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO*)&osvi);
if (bOsVersionInfoEx == FALSE) return false;
Expand All @@ -87,9 +86,8 @@ bool IsWinNT4()

bool IsVistaOrNewer()
{
OSVERSIONINFOEX osvi;
BOOL bOsVersionInfoEx;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
OSVERSIONINFOEX osvi = { 0 };
BOOL bOsVersionInfoEx;
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO*)&osvi);
if (bOsVersionInfoEx == FALSE) return false;
Expand Down Expand Up @@ -307,11 +305,7 @@ void VSTDriver::load_settings(TCHAR * szPath) {
size_t chunk_size = ftell(f);
fseek(f, 0, SEEK_SET);
blChunk.resize(chunk_size);
#if (defined(_MSC_VER) && (_MSC_VER < 1600))
if (chunk_size) fread(&blChunk.front(), 1, chunk_size, f);
#else
if (chunk_size) fread(blChunk.data(), 1, chunk_size, f);
#endif
fclose(f);
}
}
Expand Down Expand Up @@ -830,11 +824,7 @@ BOOL VSTDriver::OpenVSTDriver(TCHAR * szPath, int sampleRate) {
if (blChunk.size()) {
process_write_code( Command::SetChunkData );
process_write_code( (uint32_t)blChunk.size() );
#if (defined(_MSC_VER) && (_MSC_VER < 1600))
if (blChunk.size()) process_write_bytes( &blChunk.front(), (uint32_t)blChunk.size() );
#else
if (blChunk.size()) process_write_bytes( blChunk.data(), (uint32_t)blChunk.size() );
#endif
code = process_read_code();
if ( code != NoError ) {
process_terminate();
Expand Down
10 changes: 4 additions & 6 deletions drivercfg/MainDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ static void InvalidParamHandler(const wchar_t* expression, const wchar_t* functi
#pragma warning(disable:28159)
BOOL IsWin8OrNewer()
{
OSVERSIONINFOEX osvi;
BOOL bOsVersionInfoEx;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
OSVERSIONINFOEX osvi = { 0 };
BOOL bOsVersionInfoEx;
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO*)&osvi);
if (bOsVersionInfoEx == FALSE) return FALSE;
Expand All @@ -39,9 +38,8 @@ BOOL IsWin8OrNewer()
//Setting Midi mapper value this simple way does not work on Win XP either but on XP you can do it properly with built-in control panel anyway...
BOOL IsWinVistaOrWin7()
{
OSVERSIONINFOEX osvi;
BOOL bOsVersionInfoEx;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
OSVERSIONINFOEX osvi = { 0 };
BOOL bOsVersionInfoEx;
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO*)&osvi);
if (bOsVersionInfoEx == FALSE) return FALSE;
Expand Down
12 changes: 1 addition & 11 deletions drivercfg/Views.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,12 @@ static BOOL settings_load(VSTDriver * effect)
{
vector<uint8_t> chunk;
chunk.resize( chunk_size );
#if (defined(_MSC_VER) && (_MSC_VER < 1600))

if (chunk_size) {
retResult = ReadFile(fileHandle, (char*) &chunk.front(), (DWORD)chunk_size, &size, NULL);
if (effect) effect->setChunk( &chunk.front(), (unsigned int)chunk_size);
}
#else
if (chunk_size) {
retResult = ReadFile(fileHandle, (char*) chunk.data(), (DWORD)chunk_size, &size, NULL);
if (effect) effect->setChunk( chunk.data(), (unsigned int)chunk_size );
}

#endif
}
CloseHandle(fileHandle);
}
Expand Down Expand Up @@ -267,13 +261,9 @@ static BOOL settings_save(VSTDriver * effect)
{
vector<uint8_t> chunk;
if (effect) effect->getChunk( chunk );
#if (defined(_MSC_VER) && (_MSC_VER < 1600))

if (chunk.size() >= (2 * sizeof(uint32_t) + sizeof(bool))) retResult = WriteFile(fileHandle, &chunk.front(), (DWORD)chunk.size(), &size, NULL);
#else
if (chunk.size() >= (2 * sizeof(uint32_t) + sizeof(bool))) retResult = WriteFile(fileHandle, chunk.data(), (DWORD)chunk.size(), &size, NULL);

#endif
CloseHandle(fileHandle);
}

Expand Down
4 changes: 2 additions & 2 deletions version.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once
#define VERSION_MAJOR 2
#define VERSION_MINOR 4
#define VERSION_MINOR 5
#define VERSION_PATCH 0
#define VERSION_BUILD 2
#define VERSION_BUILD 1

#define stringify(a) stringify_(a)
#define stringify_(a) #a
Expand Down
83 changes: 25 additions & 58 deletions vsthost/vsthost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,6 @@ static bool need_idle = false;
static bool isYamahaPlugin = false;

static volatile bool is4channelMode = false;
static volatile bool isRecordingStarted = false;

static HANDLE highDpiMode = NULL;

Expand Down Expand Up @@ -581,11 +580,8 @@ void setChunk(AEffect* pEffect, std::vector<uint8_t> const& in)
unsigned size = (unsigned)in.size();
if (pEffect && size)
{
#if (defined(_MSC_VER) && (_MSC_VER < 1600))
const uint8_t* inc = &in.front();
#else
const uint8_t* inc = in.data();
#endif

uint32_t num_programs = pEffect->numPrograms;
uint32_t orgProgramIndex = (uint32_t)pEffect->dispatcher(pEffect, effGetProgram, 0, 0, NULL, 0.0);
uint32_t num_params;
Expand Down Expand Up @@ -672,13 +668,8 @@ BOOL settings_save(AEffect* pEffect)
DWORD size;
std::vector<uint8_t> chunk;
if (pEffect) getChunk(pEffect, chunk);
#if (defined(_MSC_VER) && (_MSC_VER < 1600))

if (chunk.size() >= (2 * sizeof(uint32_t) + sizeof(bool))) retResult = WriteFile(fileHandle, &chunk.front(), (DWORD)chunk.size(), &size, NULL);
#else

if (chunk.size() >= (2 * sizeof(uint32_t) + sizeof(bool))) retResult = WriteFile(fileHandle, chunk.data(), (DWORD)chunk.size(), &size, NULL);
#endif

CloseHandle(fileHandle);
}
Expand Down Expand Up @@ -1033,11 +1024,8 @@ void renderAudiosamples(int channelCount)

blState.resize(buffer_size);

#if (defined(_MSC_VER) && (_MSC_VER < 1600))
float_list_in = (float**)(blState.size() ? &blState.front() : NULL);
#else
float_list_in = (float**)blState.data();
#endif

float_list_out = float_list_in + portState[0].pEffect->numInputs;
float_null = (float*)(float_list_out + portState[0].pEffect->numOutputs * channelCount);
float_out = float_null + BUFFER_SIZE;
Expand Down Expand Up @@ -1137,11 +1125,8 @@ void renderAudiosamples(int channelCount)
portState[0].pEffect->processReplacing(portState[0].pEffect, float_list_in, float_list_out, count_to_do);
portState[1].pEffect->processReplacing(portState[1].pEffect, float_list_in, float_list_out + num_outputs, count_to_do);

#if (defined(_MSC_VER) && (_MSC_VER < 1600))
float* out = &sample_buffer.front();
#else
float* out = sample_buffer.data();
#endif

if (channelCount == 2)
{
if (num_outputs == 2)
Expand Down Expand Up @@ -1202,23 +1187,15 @@ void renderAudiosamples(int channelCount)
}
}
}
uint32_t byteSize = count_to_do * sizeof(float) * MAX_OUTPUTS * channelMult;

#if (defined(_MSC_VER) && (_MSC_VER < 1600))

uint32_t byteSize = count_to_do * sizeof(float) * MAX_OUTPUTS * channelMult;

put_bytes(&sample_buffer.front(), byteSize);

if (isRecordingStarted)
if (waveWriter.getIsRecordingStarted())
{
if (waveWriter.WriteData(&sample_buffer.front(), byteSize) == WaveResponse::WriteError) PostMessage(trayWndHandle, WM_COMMAND, (WPARAM)OTHER_MENU_OFFSET + 2, 0);
}
#else
put_bytes(sample_buffer.data(), byteSize);

if (isRecordingStarted)
{
if (waveWriter.WriteData(sample_buffer.data(), byteSize) == WaveResponse::WriteError) PostMessage(trayWndHandle, WM_COMMAND, (WPARAM)OTHER_MENU_OFFSET + 2, 0);
}
#endif

count -= count_to_do;
}
Expand Down Expand Up @@ -2016,8 +1993,8 @@ LRESULT CALLBACK TrayWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
CheckMenuItem(trayMenu, PORT_MENU_OFFSET + 1, portState[1].editorHandle != NULL && IsWindowVisible(portState[1].editorHandle) ? MF_CHECKED : MF_UNCHECKED);
EnableMenuItem(trayMenu, PORT_MENU_OFFSET, portState[0].isPortActive /* && hasEditor */ ? MF_ENABLED : MF_GRAYED);
EnableMenuItem(trayMenu, PORT_MENU_OFFSET + 1, portState[1].isPortActive /* && hasEditor*/ ? MF_ENABLED : MF_GRAYED);
EnableMenuItem(trayMenu, 3, (portState[0].isPortActive || portState[1].isPortActive) && !isRecordingStarted ? MF_BYPOSITION | MF_ENABLED : MF_BYPOSITION | MF_GRAYED);
EnableMenuItem(trayMenu, OTHER_MENU_OFFSET + 1, portState[0].isPortActive || portState[1].isPortActive || isRecordingStarted ? MF_ENABLED : MF_GRAYED);
EnableMenuItem(trayMenu, 3, (portState[0].isPortActive || portState[1].isPortActive) && !waveWriter.getIsRecordingStarted() ? MF_BYPOSITION | MF_ENABLED : MF_BYPOSITION | MF_GRAYED);
EnableMenuItem(trayMenu, OTHER_MENU_OFFSET + 1, portState[0].isPortActive || portState[1].isPortActive || waveWriter.getIsRecordingStarted() ? MF_ENABLED : MF_GRAYED);

CheckMenuItem(trayMenu, RESET_MENU_OFFSET + 1, lastUsedSysEx == RESET_MENU_OFFSET + 1 ? MF_CHECKED : MF_UNCHECKED);
CheckMenuItem(trayMenu, RESET_MENU_OFFSET + 2, lastUsedSysEx == RESET_MENU_OFFSET + 2 ? MF_CHECKED : MF_UNCHECKED);
Expand Down Expand Up @@ -2080,34 +2057,34 @@ LRESULT CALLBACK TrayWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
doOwnReset = true;
return 0;

case OTHER_MENU_OFFSET + 1:
if (!isRecordingStarted)
{
uint32_t response = waveWriter.Init(is4channelMode ? 4 : 2, sample_rate);
isRecordingStarted = response == WaveResponse::Success;
if (response == WaveResponse::CannotCreate)
case OTHER_MENU_OFFSET + 1:
{
bool isRecStarted;
if (!waveWriter.getIsRecordingStarted())
{
uint32_t response = waveWriter.Init(is4channelMode ? 4 : 2, sample_rate, portState[0].editorHandle ? portState[0].editorHandle : portState[1].editorHandle ? portState[1].editorHandle : hwnd);
isRecStarted = response == WaveResponse::Success;
if (response == WaveResponse::CannotCreate)
{
MessageBox(hwnd, _T("Wave file cannot be created.\r\nCheck write permission."), _T("VST Midi Driver"), MB_OK | MB_SYSTEMMODAL | MB_ICONERROR);
}
}
else
{
isRecordingStarted = false;
Sleep(10);
waveWriter.Close();
waveWriter.CloseRequest();
isRecStarted = false;
}

ModifyMenu(trayMenu, (UINT)wParam, MF_STRING, wParam, isRecordingStarted ? _T("Stop Audio Recording") : _T("Start Audio Recording"));
nIconData.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(isRecordingStarted ? 32513 : 32512));
ModifyMenu(trayMenu, (UINT)wParam, MF_STRING, wParam, isRecStarted ? _T("Stop Audio Recording") : _T("Start Audio Recording"));
nIconData.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(isRecStarted ? 32513 : 32512));
Shell_NotifyIcon(NIM_MODIFY, &nIconData);
return 0;
}

case OTHER_MENU_OFFSET + 2:
if (isRecordingStarted)
{
isRecordingStarted = false;
Sleep(10);
waveWriter.Close();
if (waveWriter.getIsRecordingStarted())
{
waveWriter.CloseRequest();

ModifyMenu(trayMenu, OTHER_MENU_OFFSET + 1, MF_STRING, OTHER_MENU_OFFSET + 1, _T("Start Audio Recording"));
nIconData.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(32512));
Expand Down Expand Up @@ -2514,27 +2491,17 @@ int CALLBACK _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCm
case Command::GetChunkData: // Get Chunk
{
getChunk(portState[0].pEffect, chunk);

put_code(Response::NoError);
put_code((uint32_t)chunk.size());
#if (defined(_MSC_VER) && (_MSC_VER < 1600))
if (chunk.size()) put_bytes(&chunk.front(), (uint32_t)chunk.size());
#else
if (chunk.size()) put_bytes(chunk.data(), (uint32_t)chunk.size());
#endif
}
break;

case Command::SetChunkData: // Set Chunk
{
uint32_t size = get_code();
chunk.resize(size);
#if (defined(_MSC_VER) && (_MSC_VER < 1600))
if (size) get_bytes(&chunk.front(), size);
#else
if (size) get_bytes(chunk.data(), size);
#endif

setChunk(portState[0].pEffect, chunk);
setChunk(portState[1].pEffect, chunk);

Expand Down
Loading

0 comments on commit a00ebea

Please sign in to comment.