-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSpecDSound.cpp
More file actions
182 lines (147 loc) · 7 KB
/
SpecDSound.cpp
File metadata and controls
182 lines (147 loc) · 7 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*****************************************************************************/
/* SpecDSound.cpp: implementation of the DirectSound VSTHost classes */
/*****************************************************************************/
/******************************************************************************
Copyright (C) 2006 Hermann Seib
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#include "stdafx.h"
#include "SpecDSound.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
/*===========================================================================*/
/* Global Data */
/*===========================================================================*/
CSpecDSoundInDevice DSoundIn; /* DirectSound Input Device */
CSpecDSoundOutDevice DSoundOut; /* DirectSound Output Device */
/*===========================================================================*/
/* Helper functions */
/*===========================================================================*/
/*****************************************************************************/
/* Saturate : keeps value in range without branching */
/*****************************************************************************/
inline float Saturate(float input, float fMax)
{
static const float fGrdDiv = 0.5f;
float x1 = fabsf(input + fMax);
float x2 = fabsf(input - fMax);
return fGrdDiv * (x1 - x2);
}
/*===========================================================================*/
/* CSpecDSoundInDevice class members */
/*===========================================================================*/
/*****************************************************************************/
/* CSpecDSoundInDevice : constructor */
/*****************************************************************************/
CSpecDSoundInDevice::CSpecDSoundInDevice()
{
nCurBuf = -1; /* no current buffer */
}
/*****************************************************************************/
/* ~CSpecDSoundInDevice : destructor */
/*****************************************************************************/
CSpecDSoundInDevice::~CSpecDSoundInDevice()
{
}
/*****************************************************************************/
/* OnSwitch : called when the input buffer has been switched */
/*****************************************************************************/
void CSpecDSoundInDevice::OnSwitch(int nBuffer)
{
nCurBuf = nBuffer;
if (pWorkThread)
{
// pWorkThread->Process();
}
}
/*===========================================================================*/
/* CSpecDSoundOutDevice class members */
/*===========================================================================*/
/*****************************************************************************/
/* CSpecDSoundOutDevice : constructor */
/*****************************************************************************/
CSpecDSoundOutDevice::CSpecDSoundOutDevice()
{
pwb = NULL;
nCurBuf = -1; /* no current buffer */
}
/*****************************************************************************/
/* ~CSpecDSoundOutDevice : destructor */
/*****************************************************************************/
CSpecDSoundOutDevice::~CSpecDSoundOutDevice()
{
if (pwb)
delete[] pwb;
}
/*****************************************************************************/
/* OnOpen : called when Open has been successfully completed */
/*****************************************************************************/
void CSpecDSoundOutDevice::OnOpen()
{
if (pwb)
delete[] pwb;
/* create 16bit calculation buffer */
int nShorts = GetBufferSize() / sizeof(short);
pwb = new short[nShorts];
if (pwb)
{
memset(pwb, 0, GetBufferSize()); /* empty it */
DWORD dwLen = nShorts >> 1;
WriteBuffer(0, pwb, dwLen); /* and initialize the DS buffer */
WriteBuffer(1, pwb, dwLen);
SetDriverPos(0);
}
}
/*****************************************************************************/
/* OnSwitch : called when the output buffer has been switched */
/*****************************************************************************/
void CSpecDSoundOutDevice::OnSwitch(int nBuffer)
{
nCurBuf = nBuffer;
}
/*****************************************************************************/
/* SendData : sends out a block of data */
/*****************************************************************************/
void CSpecDSoundOutDevice::SendData(float **pVstData, int nLength)
{
if ((!IsStarted()) || /* if not playing */
(!pwb)) /* or no temp buffer */
return; /* return without action */
int i, j, k, use; /* convert to 16bit stereo buffer */
short *pValue = pwb;
DWORD dwBufLen = GetBufferSize();
use = (nLength <= (int)dwBufLen) ? nLength : (int)dwBufLen;
float fsmpl, fMax = 0.f; /* diagnostic report value */
for (j = 0; j < use; j++) /* then convert all samples to target*/
{
for (k = 0; k < 2; k++) /* format */
{
fsmpl = pVstData[k][j];
#if defined(_DEBUG) || defined(_DEBUGFILE)
float fCur = fabsf(fsmpl); /* just us diagnostics in here :-) */
if (fCur > fMax)
fMax = fCur;
#endif
/* really HARD clipping method... */
*pValue++ = (short) ((Saturate(fsmpl, 1.0f) * 32767.505f) - .5f);
}
}
i = 1 - GetDriverBuf();
WriteBuffer(i, pwb, dwBufLen); /* copy that into DS buffer */
#if defined(_DEBUG) || defined(_DEBUGFILE)
TRACE("SendData(%d): Output buffer %d; max=%1.5f\n", use, i, fMax);
#endif
}