-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFormMain.cpp
More file actions
361 lines (302 loc) · 11.1 KB
/
FormMain.cpp
File metadata and controls
361 lines (302 loc) · 11.1 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <tchar.h>
#include <complex>
#include <System.Win.ComObj.hpp>
#include <anafestica/FileVersionInfo.h>
#include "SysUt.Fmt.h"
#include "FormMain.h"
using std::complex;
using std::abs;
using Anafestica::TFileVersionInfo;
using SysUt::Fmt;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmMain *frmMain;
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
: TfrmMain( Owner, StoreOpts::All, nullptr )
{
}
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain( TComponent* Owner, StoreOpts StoreOptions,
Anafestica::TConfigNode* const RootNode )
: TConfigRegistryForm( Owner, StoreOptions, RootNode )
, FFTPlan_( FFTIn_, FFTOut_ )
{
Init();
}
//---------------------------------------------------------------------------
__fastcall TfrmMain::~TfrmMain()
{
try {
Destroy();
}
catch ( ... ) {
}
}
//---------------------------------------------------------------------------
bool TfrmMain::GetLogView() const
{
return actLog ? actLog->Checked : true;
}
//---------------------------------------------------------------------------
void TfrmMain::SetLogView( bool Val )
{
if ( actLog ) {
actLog->Checked = Val;
if ( Val ) {
rbtnLog->Checked = true;
}
else {
rbtnLin->Checked = true;
}
}
}
//---------------------------------------------------------------------------
String TfrmMain::GetSource() const
{
if ( comboboxAudioSources->ItemIndex >= 0 ) {
return comboboxAudioSources->Items->Strings[comboboxAudioSources->ItemIndex];
}
return String();
}
//---------------------------------------------------------------------------
void TfrmMain::SetSource( String Val )
{
auto Idx = comboboxAudioSources->Items->IndexOf( Val );
if ( Idx >= 0 ) {
comboboxAudioSources->ItemIndex = Idx;
}
}
//---------------------------------------------------------------------------
void TfrmMain::Init()
{
comboboxAudioSources->Items->Clear();
WAVEINCAPS wic;
unsigned long iNumDevs;
/* Get the number of Digital Audio In devices in this computer */
iNumDevs = waveInGetNumDevs();
/* Go through all of those devices, displaying their names */
for ( unsigned long i = 0; i < iNumDevs ; ++i ) {
/* Get info about the next device */
if ( !waveInGetDevCaps( i, &wic, sizeof wic ) ) {
/* Display its Device ID and name */
comboboxAudioSources->Items->Append(
Fmt( _T( "Device ID #%u: %s\r\n" ), i, wic.szPname )
);
}
}
if ( comboboxAudioSources->Items->Count > 0 ) {
comboboxAudioSources->ItemIndex = 0;
}
SetupCaption();
RestoreProperties();
Chart2->BottomAxis->Maximum = SampleCount - 1;
bufferUserData_.emplace_back( 0, *Series1, *Series3 );
bufferUserData_.emplace_back( 1, *Series2, *Series4 );
}
//---------------------------------------------------------------------------
void TfrmMain::Destroy()
{
SaveProperties();
}
//---------------------------------------------------------------------------
String TfrmMain::GetModuleFileName()
{
return GetModuleName( reinterpret_cast<unsigned>( HInstance ) );
}
//---------------------------------------------------------------------------
void TfrmMain::SetupCaption()
{
TFileVersionInfo const Info( GetModuleFileName() );
Caption =
SysUt::Fmt( _T( "%s, Ver %s" ), Application->Title, Info.ProductVersion );
}
//---------------------------------------------------------------------------
void TfrmMain::RestoreProperties()
{
RESTORE_LOCAL_PROPERTY( LogView );
RESTORE_LOCAL_PROPERTY( Source );
}
//---------------------------------------------------------------------------
void TfrmMain::SaveProperties() const
{
SAVE_LOCAL_PROPERTY( LogView );
SAVE_LOCAL_PROPERTY( Source );
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::WndProc( Winapi::Messages::TMessage &Message )
{
TConfigRegistryForm::WndProc( Message );
const bool Log = LogView;
switch ( Message.Msg ) {
case MM_WIM_OPEN:
//::OutputDebugString( _T( "MM_WIM_OPEN" ) );
break;
case MM_WIM_DATA:
if ( WAVEHDR* Hdr = (WAVEHDR *)Message.LParam ) {
if ( DWORD BytesRecorded = Hdr->dwBytesRecorded ) {
auto const UserData =
reinterpret_cast<BufferUserData const *>( Hdr->dwUser );
TChartSeries& CurrentFreqSeries = UserData->GetFreqSeries();
CurrentFreqSeries.BeginUpdate();
TChartSeries& CurrentTimeSeries = UserData->GetTimeSeries();
CurrentTimeSeries.BeginUpdate();
// Apply windowing (Hanning) & plot the unwindowed
// time domain signal
for ( int Idx = 0 ; Idx < SampleCount ; ++Idx ) {
const double Signal = waveData_[UserData->GetIdx()][Idx];
const double W = 1.0 - cos( 2.0 * M_PI * Idx / SampleCount );
//CurrentTimeSeries.AddY( W * Signal );
CurrentTimeSeries.AddY( Signal );
FFTIn_[Idx][0] = W * Signal;
}
// real FFT
fftw_execute( FFTPlan_ );
// freq plot
auto const Sf = 2.0 / double( SampleCount );
for ( int Idx = 0 ; Idx < SampleCount / 2 ; ++Idx ) {
double Re = FFTOut_[Idx][0] / 32768.0;
double Im = FFTOut_[Idx][1] / 32768.0;
complex<double> Sample{ Re, Im };
double Freq = 22050.0 * Idx / SampleCount;
CurrentFreqSeries.AddXY(
Freq,
Log ?
20.0 * log10( abs( Sample ) * Sf + 1e-15 )
:
abs( Sample ) * Sf
);
}
CurrentFreqSeries.EndUpdate();
CurrentFreqSeries.Active = true;
CurrentTimeSeries.EndUpdate();
CurrentTimeSeries.Active = true;
if ( latestFreqSeriesShown_ ) {
latestFreqSeriesShown_->Active = false;
latestFreqSeriesShown_->Clear();
}
if ( latestTimeSeriesShown_ ) {
latestTimeSeriesShown_->Active = false;
latestTimeSeriesShown_->Clear();
}
latestFreqSeriesShown_ = &CurrentFreqSeries;
latestTimeSeriesShown_ = &CurrentTimeSeries;
}
if ( IsStillSampling() ) {
waveInAddBuffer( waveHandle_, Hdr, sizeof *Hdr );
}
else {
waveInClose( waveHandle_ );
}
}
break;
case MM_WIM_CLOSE:
//::OutputDebugString( _T( "MM_WIM_CLOSE" ) );
break;
}
}
//---------------------------------------------------------------------------
bool TfrmMain::IsStillSampling() const
{
return !stopped_;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::actStartExecute(TObject *Sender)
{
stopped_ = false;
waveFormat_.wFormatTag = WAVE_FORMAT_PCM;
waveFormat_.nChannels = 1;
waveFormat_.nSamplesPerSec = 22050;
waveFormat_.wBitsPerSample = 16;
waveFormat_.nAvgBytesPerSec = 22050 * 2;
waveFormat_.nBlockAlign = 2;
waveFormat_.cbSize = 0;
auto Res = ::waveInOpen(
&waveHandle_, comboboxAudioSources->ItemIndex, &waveFormat_, 0, 0,
WAVE_FORMAT_QUERY
);
if ( Res ) {
RaiseLastOSError();
}
Res = ::waveInOpen(
&waveHandle_, comboboxAudioSources->ItemIndex,
&waveFormat_, reinterpret_cast<DWORD_PTR>( Handle ),
0, CALLBACK_WINDOW
);
if ( Res ) {
RaiseLastOSError();
}
waveHeader_[0].dwBufferLength = waveData_[0].size() * sizeof( BufferCont::value_type );
waveHeader_[0].dwFlags = 0;
waveHeader_[0].lpData = reinterpret_cast<LPSTR>( &waveData_[0][0] );
waveHeader_[0].dwUser = reinterpret_cast<DWORD_PTR>( &bufferUserData_[0] );
Res = ::waveInPrepareHeader(
waveHandle_, &waveHeader_[0], sizeof waveHeader_[0] );
if ( Res )
RaiseLastOSError();
waveHeader_[1].dwBufferLength = waveData_[1].size() * sizeof( BufferCont::value_type );
waveHeader_[1].dwFlags = 0;
waveHeader_[1].lpData = reinterpret_cast<LPSTR>( &waveData_[1][0] );
waveHeader_[1].dwUser = reinterpret_cast<DWORD_PTR>( &bufferUserData_[1] );
Res = ::waveInPrepareHeader(
waveHandle_, &waveHeader_[1], sizeof waveHeader_[1]
);
if ( Res ) {
RaiseLastOSError();
}
Res = ::waveInAddBuffer(
waveHandle_, &waveHeader_[0], sizeof waveHeader_[0]
);
if ( Res ) {
RaiseLastOSError();
}
Res = ::waveInAddBuffer(
waveHandle_, &waveHeader_[1], sizeof waveHeader_[1]
);
if ( Res ) {
RaiseLastOSError();
}
Res = ::waveInStart( waveHandle_ );
if ( Res ) {
RaiseLastOSError();
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::actStartUpdate(TObject *Sender)
{
TAction& Act = static_cast<TAction&>( *Sender );
Act.Enabled = !IsStillSampling() && comboboxAudioSources->ItemIndex >= 0;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::actStopExecute(TObject *Sender)
{
stopped_ = true;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::actStopUpdate(TObject *Sender)
{
TAction& Act = static_cast<TAction&>( *Sender );
Act.Enabled = IsStillSampling();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::actLogExecute(TObject *Sender)
{
Chart1->LeftAxis->Maximum = 10.0;
Chart1->LeftAxis->Minimum = -144.0;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::actLinearExecute(TObject *Sender)
{
Chart1->LeftAxis->Maximum = 1.0;
Chart1->LeftAxis->Minimum = 0;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ApplicationEvents1Idle(TObject *Sender, bool &Done)
{
comboboxAudioSources->Enabled = !IsStillSampling();
}
//---------------------------------------------------------------------------