source: trunk/Utilities/GeneratePcmWavFile/GeneratePcmWavFile.cpp @ 42

Last change on this file since 42 was 27, checked in by roman, 12 years ago

fix for syntax and zero dBFS /l:0 parameter

  • Property svn:keywords set to Id
File size: 20.7 KB
Line 
1////////////////////////////////////////////////////////////
2// Copyright (C) Roman Ryltsov, 2008-2011
3// Created by Roman Ryltsov roman@alax.info
4//
5// $Id: GeneratePcmWavFile.cpp 27 2011-10-10 12:44:58Z roman $
6
7#include "stdafx.h"
8#include "rodshow.h"
9
10////////////////////////////////////////////////////////////
11// CModule
12
13class CModule : 
14        public CAtlExeModuleT<CModule>
15{
16        typedef CThreadT<CModule> CThread;
17
18public:
19
20        ////////////////////////////////////////////////////////
21        // CGenericFilterGraph
22
23        class CGenericFilterGraph
24        {
25        public:
26#if _DEVELOPMENT
27                BOOL m_bShowDestructorMessageBox;
28#endif // _DEVELOPMENT
29                CComPtr<IFilterGraph2> m_pFilterGraph;
30                CComPtr<IMediaControl> m_pMediaControl;
31                CComPtr<IMediaEventEx> m_pMediaEventEx;
32                CComPtr<IMediaFilter> m_pMediaFilter;
33                CComPtr<IMediaPosition> m_pMediaPosition;
34
35        public:
36        // CGenericFilterGraph
37                CGenericFilterGraph(BOOL bShowDestructorMessageBox = FALSE) throw()
38                {
39                        bShowDestructorMessageBox;
40#if _DEVELOPMENT
41                        m_bShowDestructorMessageBox = bShowDestructorMessageBox;
42#endif // _DEVELOPMENT
43                }
44                ~CGenericFilterGraph() throw()
45                {
46#if _DEVELOPMENT
47                        if(m_pFilterGraph && m_bShowDestructorMessageBox)
48                                AtlMessageBox(GetActiveWindow(), _T("DirectShow Filter Graph is about to be Released - Break In"), _T("Debug"), MB_ICONWARNING | MB_OK);
49#endif // _DEVELOPMENT
50                }
51                VOID CoCreateInstance(const CLSID& ClassIdentifier = CLSID_FilterGraph)
52                {
53                        __C(m_pFilterGraph.CoCreateInstance(ClassIdentifier));
54                        m_pMediaControl = CComQIPtr<IMediaControl>(m_pFilterGraph);
55                        m_pMediaEventEx = CComQIPtr<IMediaEventEx>(m_pFilterGraph);
56                        m_pMediaFilter = CComQIPtr<IMediaFilter>(m_pFilterGraph);
57                        m_pMediaPosition = CComQIPtr<IMediaPosition>(m_pFilterGraph);
58                        __D(m_pMediaControl && m_pMediaEventEx && m_pMediaFilter && m_pMediaPosition, E_NOINTERFACE);
59                }
60                VOID SetShowDestructorMessageBox(BOOL bShowDestructorMessageBox = TRUE)
61                {
62                        bShowDestructorMessageBox;
63#if _DEVELOPMENT
64                        m_bShowDestructorMessageBox = bShowDestructorMessageBox;
65#endif // _DEVELOPMENT
66                }
67                operator const CComPtr<IFilterGraph2>& () const throw()
68                {
69                        return m_pFilterGraph;
70                }
71                const CComPtr<IFilterGraph2>& operator -> () const throw()
72                {
73                        return m_pFilterGraph;
74                }
75        };
76
77        ////////////////////////////////////////////////////////
78        // CSourceFilter
79
80        class ATL_NO_VTABLE CSourceFilter :
81                public CComObjectRootEx<CComMultiThreadModelNoCS>,
82                public CComCoClass<CSourceFilter>,
83                public CPushSourceFilterT<CSourceFilter>,
84                public CBasePersistT<CSourceFilter>,
85                public CAmFilterMiscFlagsT<CSourceFilter, AM_FILTER_MISC_FLAGS_IS_SOURCE>
86        {
87        public:
88
89        DECLARE_NO_REGISTRY()
90
91        DECLARE_PROTECT_FINAL_CONSTRUCT()
92
93        //DECLARE_QI_TRACE(CSourceFilter)
94
95        BEGIN_COM_MAP(CSourceFilter)
96                COM_INTERFACE_ENTRY(IBaseFilter)
97                COM_INTERFACE_ENTRY(IMediaFilter)
98                COM_INTERFACE_ENTRY_IID(__uuidof(IPersist), IBaseFilter)
99                COM_INTERFACE_ENTRY(IAMFilterMiscFlags)
100        END_COM_MAP()
101
102        public:
103
104                ////////////////////////////////////////////////////////
105                // CThreadContext
106
107                class COutputPin;
108
109                class CThreadContext :
110                        public CPushSourceFilterT<CSourceFilter>::CThreadContext
111                {
112                public:
113                        CEvent m_MediaSampleTimeEvent;
114                        REFERENCE_TIME m_nMediaSampleTime;
115                        SIZE_T m_nCurrentSampleIndex;
116
117                public:
118                // CThreadContext
119                        CThreadContext(CEvent& TerminationEvent) throw() :
120                                CPushSourceFilter::CThreadContext(TerminationEvent),
121                                m_nMediaSampleTime(0),
122                                m_nCurrentSampleIndex(0)
123                        {
124                        }
125                };
126
127                ////////////////////////////////////////////////////////
128                // COutputPin
129
130                class ATL_NO_VTABLE COutputPin :
131                        public CComObjectRootEx<CComMultiThreadModelNoCS>,
132                        public CPushSourceFilterT<CSourceFilter>::COutputPinT<COutputPin, CSourceFilter, CThreadContext>,
133                        public CAmPushSourceT<COutputPin, 0>
134                {
135                public:
136
137                //DECLARE_QI_TRACE(CSourceFilter::COutputPin)
138
139                BEGIN_COM_MAP(COutputPin)
140                        COM_INTERFACE_ENTRY(IPin)
141                        COM_INTERFACE_ENTRY(IAMPushSource)
142                        COM_INTERFACE_ENTRY(IAMLatency)
143                END_COM_MAP()
144
145                public:
146                        CMediaType m_pDataMediaType;
147                        REFERENCE_TIME m_nDataLength;
148                        DOUBLE m_fSignalPeriod;
149                        DOUBLE m_fSignalAmplitude;
150                        DOUBLE m_fNoiseAmplitude;
151
152                public:
153                // COutputPin
154                        COutputPin() throw() :
155                                m_nDataLength(0),
156                                m_fSignalPeriod(0),
157                                m_fSignalAmplitude(0),
158                                m_fNoiseAmplitude(0)
159                        {
160                                _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this);
161                        }
162                        ~COutputPin() throw()
163                        {
164                                _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this);
165                        }
166                        VOID EnumerateMediaTypes(CAtlList<CMediaType>& MediaTypeList)
167                        {
168                                CRoCriticalSectionLock DataLock(GetDataCriticalSection());
169                                _W(MediaTypeList.AddTail(m_pDataMediaType));
170                        }
171                        BOOL CheckMediaType(const CMediaType& pMediaType) const throw()
172                        {
173                                _A(pMediaType);
174                                //if(pMediaType->majortype != MEDIATYPE_Audio || pMediaType->subtype != MEDIASUBTYPE_PCM)
175                                //      return FALSE;
176                                //const CWaveFormatEx* pWaveFormatEx = pMediaType.GetWaveFormatEx();
177                                //if(!pWaveFormatEx)
178                                //      return FALSE;
179                                return m_pDataMediaType.Compare(pMediaType);
180                        }
181                        BOOL DecideMemAllocatorProperties(IMemAllocator* pMemAllocator, ALLOCATOR_PROPERTIES Properties) throw()
182                        {
183                                CRoCriticalSectionLock DataLock(GetDataCriticalSection());
184                                const CMediaType& pMediaType = GetMediaTypeReference();
185                                const CWaveFormatEx* pWaveFormatEx = pMediaType.GetWaveFormatEx();
186                                if(!pWaveFormatEx)
187                                        return FALSE;
188                                if(!__super::DecideMemAllocatorProperties(pMemAllocator, Properties))
189                                        return FALSE;
190                                static const ULONG g_nDefaultBufferLength = 500; // 500 ms
191                                const SIZE_T nDefaultBufferBlockCount = (pWaveFormatEx->nAvgBytesPerSec * g_nDefaultBufferLength / 1000 + pWaveFormatEx->nBlockAlign - 1) / pWaveFormatEx->nBlockAlign;
192                                const SIZE_T nDefaultBufferSize = nDefaultBufferBlockCount * pWaveFormatEx->nBlockAlign;
193                                if(Properties.cbBuffer < (LONG) nDefaultBufferSize)
194                                        Properties.cbBuffer = (LONG) nDefaultBufferSize;
195                                if(Properties.cbAlign < pWaveFormatEx->nBlockAlign)
196                                        Properties.cbAlign = pWaveFormatEx->nBlockAlign;
197                                if(!SetMemAllocatorBufferSize(pMemAllocator, Properties))
198                                        return FALSE;
199                                const SIZE_T nSampleCount = Properties.cbBuffer / pWaveFormatEx->nBlockAlign;
200                                const SIZE_T nEffectiveBufferSize = nSampleCount * pWaveFormatEx->nBlockAlign;
201                                const REFERENCE_TIME nBufferTime = (REFERENCE_TIME) (1000 * 10000) * nEffectiveBufferSize / pWaveFormatEx->nAvgBytesPerSec;
202                                SetLatency(nBufferTime);
203                                return TRUE;
204                        }
205                        VOID InitializeThread(CThreadContext& ThreadContext) throw()
206                        {
207                                CRoCriticalSectionLock DataLock(GetDataCriticalSection());
208                                __super::InitializeThread(ThreadContext);
209                                if(m_fSignalPeriod > 0)
210                                {
211                                        __D(m_pDataMediaType.GetWaveFormatEx()->wBitsPerSample == 16, E_NOTIMPL);
212                                        ThreadContext.m_nCurrentSampleIndex = 0;
213                                }
214                        }
215                        BOOL ComposeMediaSample(CThreadContext& ThreadContext, IMediaSample* pMediaSample)
216                        {
217                                CMediaSampleProperties Properties(pMediaSample);
218                                _A(!Properties.pMediaType);
219                                if(ThreadContext.m_nMediaSampleTime >= m_nDataLength)
220                                        return FALSE; // Finished
221                                CRoCriticalSectionLock DataLock(GetDataCriticalSection());
222                                const CWaveFormatEx* pWaveFormatEx = GetMediaTypeReference().GetWaveFormatEx();
223                                _A(pWaveFormatEx);
224                                const SIZE_T nRemainedDataSize = (SIZE_T) ((m_nDataLength - ThreadContext.m_nMediaSampleTime) * pWaveFormatEx->nAvgBytesPerSec / (1000 * 10000i64));
225                                SIZE_T nDataSize = Properties.cbBuffer;
226                                if(nDataSize > nRemainedDataSize)
227                                        nDataSize = nRemainedDataSize;
228                                nDataSize = (nDataSize / pWaveFormatEx->nBlockAlign) * pWaveFormatEx->nBlockAlign;
229                                if(pWaveFormatEx->wBitsPerSample == 8)
230                                {
231                                        FillMemory(Properties.pbBuffer, nDataSize, 0x80);
232                                } else
233                                {
234                                        ZeroMemory(Properties.pbBuffer, nDataSize);
235                                        #pragma region Sine Wave Data
236                                        if(m_fSignalPeriod > 0)
237                                        {
238                                                SIZE_T nSampleIndex = 0;
239                                                for(SIZE_T nIndex = 0; nIndex < nDataSize; nIndex += pWaveFormatEx->nBlockAlign, nSampleIndex++)
240                                                {
241                                                        const SHORT nValue = (SHORT) (m_fSignalAmplitude * sin(2 * M_PI * (ThreadContext.m_nCurrentSampleIndex + nSampleIndex) / m_fSignalPeriod));
242                                                        SHORT* pnSampleData = (SHORT*) (Properties.pbBuffer + nIndex);
243                                                        for(WORD nChannelIndex = 0; nChannelIndex < pWaveFormatEx->nChannels; nChannelIndex++)
244                                                                pnSampleData[nChannelIndex] = nValue;
245                                                }
246                                        }
247                                        #pragma endregion
248                                        #pragma region Noise Wave Data
249                                        if(m_fNoiseAmplitude > 0)
250                                        {
251                                                for(SIZE_T nIndex = 0; nIndex < nDataSize; nIndex += pWaveFormatEx->nBlockAlign)
252                                                {
253                                                        SHORT* pnSampleData = (SHORT*) (Properties.pbBuffer + nIndex);
254                                                        for(WORD nChannelIndex = 0; nChannelIndex < pWaveFormatEx->nChannels; nChannelIndex++)
255                                                                pnSampleData[nChannelIndex] += (SHORT) (m_fNoiseAmplitude * (DOUBLE) (rand() - RAND_MAX / 2) / (RAND_MAX / 2));
256                                                }
257                                        }
258                                        #pragma endregion
259                                }
260                                ThreadContext.m_nMediaSampleTime += nDataSize * (1000 * 10000i64) / pWaveFormatEx->nAvgBytesPerSec;
261                                ThreadContext.m_nCurrentSampleIndex += nDataSize / pWaveFormatEx->nBlockAlign;
262                                Properties.lActual = (LONG) nDataSize;
263                                Properties.Set();
264                                return TRUE;
265                        }
266                        VOID InitializeData(const CWaveFormatEx* pWaveFormatEx, REFERENCE_TIME nLength)
267                        {
268                                _A(pWaveFormatEx);
269                                _A(nLength > 0);
270                                __D(!m_pDataMediaType, E_UNNAMED);
271                                m_pDataMediaType.AllocateWaveFormatEx(pWaveFormatEx);
272                                m_nDataLength = nLength;
273                        }
274                        VOID InitializeSignal(DOUBLE fSignalPeriod, DOUBLE fSignalAmplitude, DOUBLE fNoiseAmplitude)
275                        {
276                                __D(fSignalPeriod >= 0 && fSignalAmplitude >= 0 && fNoiseAmplitude >= 0, E_INVALIDARG);
277                                m_fSignalPeriod = fSignalPeriod;
278                                m_fSignalAmplitude = fSignalAmplitude;
279                                m_fNoiseAmplitude = fNoiseAmplitude;
280                        }
281                };
282
283        private:
284                CObjectPtr<COutputPin> m_pOutputPin;
285
286        public:
287        // CSourceFilter
288                CSourceFilter() throw() :
289                        CBasePersistT<CSourceFilter>(GetDataCriticalSection())
290                {
291                        _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this);
292                }
293                ~CSourceFilter() throw()
294                {
295                        _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this);
296                }
297                HRESULT FinalConstruct() throw()
298                {
299                        _ATLTRY
300                        {
301                                m_pOutputPin.Construct()->Initialize(this, L"Output");
302                                AddPin(m_pOutputPin);
303                        }
304                        _ATLCATCH(Exception)
305                        {
306                                _C(Exception);
307                        }
308                        return S_OK;
309                }
310                VOID FinalRelease() throw()
311                {
312                        m_pOutputPin = NULL;
313                }
314                VOID DeliverBeginFlush(IPin*)
315                {
316                        m_pOutputPin->DeliverBeginFlush();
317                }
318                VOID DeliverEndFlush(IPin*)
319                {
320                        m_pOutputPin->DeliverEndFlush();
321                }
322                VOID DeliverNewSegment(IPin*, REFERENCE_TIME nStartTime, REFERENCE_TIME nStopTime, DOUBLE fRate)
323                {
324                        m_pOutputPin->DeliverNewSegment(nStartTime, nStopTime, fRate);
325                }
326                static BOOL CanCue() throw()
327                {
328                        return FALSE;
329                }
330                VOID CueFilter()
331                {
332                        m_pOutputPin->CuePin();
333                }
334                VOID RunFilter(REFERENCE_TIME nStartTime)
335                {
336                        m_pOutputPin->RunPin(nStartTime);
337                }
338                VOID PauseFilter() throw()
339                {
340                        m_pOutputPin->PausePin();
341                }
342                VOID StopFilter() throw()
343                {
344                        m_pOutputPin->StopPin();
345                }
346                const CObjectPtr<COutputPin>& GetOutputPin() const throw()
347                {
348                        return m_pOutputPin;
349                }
350                VOID Initialize(const CWaveFormatEx* pWaveFormatEx, REFERENCE_TIME nLength)
351                {
352                        m_pOutputPin->InitializeData(pWaveFormatEx, nLength);
353                }
354                VOID InitializeSignal(DOUBLE fSignalAmplitude, DOUBLE fSignalPeriod, DOUBLE fNoiseAmplitude)
355                {
356                        m_pOutputPin->InitializeSignal(fSignalAmplitude, fSignalPeriod, fNoiseAmplitude);
357                }
358        };
359
360private:
361        CWaveFormatEx m_WaveFormatEx;
362        REFERENCE_TIME m_nLength;
363        UINT m_nSignalFrequency;
364        UINT m_nSignalLoudness;
365        UINT m_nNoiseLoudness;
366        CPath m_sPath;
367
368public:
369// CModule
370        CModule() throw()
371        {
372#if defined(_DEBUG)
373                AtlTraceLoadSettings(NULL);
374#endif // defined(_DEBUG)
375                _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this);
376                ZeroMemory(&m_WaveFormatEx, sizeof m_WaveFormatEx);
377                m_nLength = 0;
378                m_nSignalFrequency = 0;
379                m_nSignalLoudness = 0;
380                m_nNoiseLoudness = 0;
381        }
382        ~CModule() throw()
383        {
384                _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this);
385        }
386        VOID SetSampleRate(UINT nSampleRate)
387        {
388                __D(nSampleRate > 0, E_INVALIDARG);
389                m_WaveFormatEx.nSamplesPerSec = (DWORD) nSampleRate;
390        }
391        VOID SetChannelCount(UINT nChannelCount)
392        {
393                __D(nChannelCount > 0, E_INVALIDARG);
394                m_WaveFormatEx.nChannels = (WORD) nChannelCount;
395        }
396        VOID SetSampleBitCount(UINT nSampleBitCount)
397        {
398                __D(nSampleBitCount == 8 || nSampleBitCount == 16, E_INVALIDARG);
399                m_WaveFormatEx.wBitsPerSample = (WORD) nSampleBitCount;
400        }
401        VOID SetLength(REFERENCE_TIME nLength)
402        {
403                __D(nLength > 0, E_INVALIDARG);
404                m_nLength = nLength;
405        }
406        VOID SetSignalFrequency(UINT nSignalFrequency)
407        {
408                __D(nSignalFrequency > 0, E_INVALIDARG);
409                m_nSignalFrequency = nSignalFrequency;
410        }
411        VOID SetSignalLoudness(UINT nSignalLoudness)
412        {
413                __D(nSignalLoudness >= 0, E_INVALIDARG);
414                m_nSignalLoudness = nSignalLoudness;
415        }
416        VOID SetNoiseLoudness(UINT nNoiseLoudness)
417        {
418                __D(nNoiseLoudness >= 0, E_INVALIDARG);
419                m_nNoiseLoudness = nNoiseLoudness;
420        }
421        VOID SetPath(LPCTSTR pszPath)
422        {
423                __D(_tcslen(pszPath), E_INVALIDARG);
424                __D(!_tcslen(m_sPath), E_INVALIDARG);
425                m_sPath = pszPath;
426        }
427        HRESULT PreMessageLoop(INT nShowCommand)
428        {
429                __C(__super::PreMessageLoop(nShowCommand));
430                return S_OK;
431        }
432        VOID RunMessageLoop()
433        {
434                #pragma region Input Validation and Syntax
435                if(!m_WaveFormatEx.nSamplesPerSec || !m_WaveFormatEx.nChannels || !m_WaveFormatEx.wBitsPerSample || !m_nLength || !_tcslen(m_sPath))
436                {
437                        _tprintf(_T("Syntax: GeneratePcmWavFile <options> <output-path>\n"));
438                        _tprintf(_T("  /s:N: Sampling Rate N, Hz\n"));
439                        _tprintf(_T("  /c:N: Channel Count N\n"));
440                        _tprintf(_T("  /b:N: Sample Bit Count N, 8 or 16\n"));
441                        _tprintf(_T("  /t:N: Length N, seconds\n"));
442                        _tprintf(_T("  /f:N: Sine Signal Frequency N, Hz\n"));
443                        _tprintf(_T("  /l:N: Sine Signal Loudness N, dB below full scale\n"));
444                        _tprintf(_T("  /n:N: Noise Signal Loudness N, dB below full scale\n"));
445                        AtlThrow(S_FALSE);
446                }
447                #pragma endregion
448                #pragma region Complete Audio Format
449                m_WaveFormatEx.wFormatTag = WAVE_FORMAT_PCM;
450                m_WaveFormatEx.nBlockAlign = m_WaveFormatEx.nChannels * ((m_WaveFormatEx.wBitsPerSample + 7) >> 3);
451                m_WaveFormatEx.nAvgBytesPerSec = m_WaveFormatEx.nSamplesPerSec * m_WaveFormatEx.nBlockAlign;
452                CWaveFormatEx* pWaveFormatEx = &m_WaveFormatEx;
453                // SUGG: Replace with WAVEFORMATEXTENSIBLE where appropriate
454                #pragma endregion
455                #pragma region Filter Graph
456                CGenericFilterGraph GenericFilterGraph(TRUE);
457                GenericFilterGraph.CoCreateInstance();
458                CObjectPtr<CSourceFilter> pSourceFilter;
459                pSourceFilter.Construct()->Initialize(pWaveFormatEx, m_nLength);
460                if(m_nSignalFrequency && m_nSignalLoudness >= 0 || m_nNoiseLoudness)
461                {
462                        __D(m_WaveFormatEx.wBitsPerSample == 16, E_NOTIMPL);
463                        DOUBLE fSignalPeriod = 0, fSignalAmplitude = 0;
464                        if(m_nSignalFrequency && m_nSignalLoudness >= 0)
465                        {
466                                fSignalPeriod = (DOUBLE) pWaveFormatEx->nSamplesPerSec / m_nSignalFrequency;
467                                fSignalAmplitude = 32767.0 / pow(10.0, m_nSignalLoudness / 20.0);
468                        }
469                        DOUBLE fNoiseAmplitude = 0;
470                        if(m_nNoiseLoudness)
471                        {
472                                fNoiseAmplitude = 32767.0 / pow(10.0, m_nNoiseLoudness / 20.0);
473                        }
474                        pSourceFilter->InitializeSignal(fSignalPeriod, fSignalAmplitude, fNoiseAmplitude);
475                }
476                __C(GenericFilterGraph->AddFilter(pSourceFilter, CT2CW(_T("Source"))));
477                CComPtr<IPin> pCurrentOutputPin = pSourceFilter->GetOutputPin();
478                #pragma region WAV Dest
479                CComPtr<IBaseFilter> pWavDestBaseFilter;
480                _ATLTRY
481                {
482                        __C(pWavDestBaseFilter.CoCreateInstance(_PersistHelper::ClassIdentifierFromString(CT2CW(_T("{3C78B8E2-6C4D-11D1-ADE2-0000F8754B99}")))));
483                        __C(GenericFilterGraph->AddFilter(pWavDestBaseFilter, CT2CW(_T(".WAV Multiplexer"))));
484                        __C(GenericFilterGraph->Connect(pCurrentOutputPin, _FilterGraphHelper::GetFilterPin(pWavDestBaseFilter, PINDIR_INPUT)));
485                        pCurrentOutputPin = _FilterGraphHelper::GetFilterPin(pWavDestBaseFilter, PINDIR_OUTPUT);
486                }
487                _ATLCATCH(Exception)
488                {
489                        _tprintf(_T("There was an error trying to instantiate WAV Dest filter, Exception 0x%08X\n"), (HRESULT) Exception);
490                        _ATLRETHROW;
491                }
492                #pragma endregion
493                #pragma region File Writer
494                CComPtr<IBaseFilter> pFileWriterBaseFilter;
495                __C(pFileWriterBaseFilter.CoCreateInstance(CLSID_FileWriter));
496                CComQIPtr<IFileSinkFilter2> pFileSinkFilter2 = pFileWriterBaseFilter;
497                __D(pFileSinkFilter2, E_NOINTERFACE);
498                _tprintf(_T("Writing into \"%s\"...\n"), m_sPath);
499                __C(pFileSinkFilter2->SetFileName(CT2CW(m_sPath), NULL));
500                __C(pFileSinkFilter2->SetMode(AM_FILE_OVERWRITE));
501                __C(GenericFilterGraph->AddFilter(pFileWriterBaseFilter, CT2CW(_T("File Writer"))));
502                __C(GenericFilterGraph->Connect(pCurrentOutputPin, _FilterGraphHelper::GetFilterPin(pFileWriterBaseFilter, PINDIR_INPUT)));
503                pCurrentOutputPin.Release();
504                #pragma endregion
505                _A(!pCurrentOutputPin);
506                _A(GenericFilterGraph.m_pMediaFilter);
507                __C(GenericFilterGraph.m_pMediaFilter->SetSyncSource(NULL));
508                #pragma endregion
509                _tprintf(_T("Running...\n"));
510                _A(GenericFilterGraph.m_pMediaControl);
511                __C(GenericFilterGraph.m_pMediaControl->Run());
512                GenericFilterGraph.SetShowDestructorMessageBox(FALSE);
513                _A(GenericFilterGraph.m_pMediaEventEx);
514                LONG nEventCode;
515                __C(GenericFilterGraph.m_pMediaEventEx->WaitForCompletion(INFINITE, &nEventCode));
516                _tprintf(_T("Complete, nEventCode 0x%02X\n"), nEventCode);
517                __C(GenericFilterGraph.m_pMediaControl->Stop());
518        }
519};
520
521////////////////////////////////////////////////////////////
522// Main
523
524int _tmain(int argc, _TCHAR* argv[])
525{
526        _ATLTRY
527        {
528                CModule Module;
529                #pragma region Parse Command Line
530                for(INT nIndex = 1; nIndex < argc; nIndex++)
531                {
532                        CString sArgument = argv[nIndex];
533                        _A(!sArgument.IsEmpty());
534                        #pragma region Switches
535                        if(_tcschr(_T("-/"), sArgument[0]))
536                        {
537                                sArgument.Delete(0);
538                                #pragma region Switch Value/Specification
539                                CString sArgumentValue;
540                                if(sArgument.GetLength() > 1)
541                                {
542                                        SIZE_T nIndex = 1;
543                                        if(sArgument[1] == _T(':'))
544                                                nIndex++;
545                                        sArgumentValue = (LPCTSTR) sArgument + nIndex;
546                                }
547                                INT nIntegerArgumentValue = 0;
548                                const BOOL bIntegerArgumentValueValid = !sArgumentValue.IsEmpty() ? AtlStringToInteger(sArgumentValue, nIntegerArgumentValue) : FALSE;
549                                #pragma endregion
550                                if(_tcschr(_T("Ss"), sArgument[0])) // Sample Rate
551                                {
552                                        __D(bIntegerArgumentValueValid, E_INVALIDARG);
553                                        //_tprintf(_T("Option: Sample Rate, %d\n"), nIntegerArgumentValue);
554                                        Module.SetSampleRate(nIntegerArgumentValue);
555                                } else
556                                if(_tcschr(_T("Cc"), sArgument[0])) // Channel Count
557                                {
558                                        __D(bIntegerArgumentValueValid, E_INVALIDARG);
559                                        //_tprintf(_T("Option: Channel Count, %d\n"), nIntegerArgumentValue);
560                                        Module.SetChannelCount(nIntegerArgumentValue);
561                                } else
562                                if(_tcschr(_T("Bb"), sArgument[0])) // Sample Bit Count
563                                {
564                                        __D(bIntegerArgumentValueValid, E_INVALIDARG);
565                                        //_tprintf(_T("Option: Sample Bit Count, %d\n"), nIntegerArgumentValue);
566                                        Module.SetSampleBitCount(nIntegerArgumentValue);
567                                } else
568                                if(_tcschr(_T("Tt"), sArgument[0])) // Length, seconds
569                                {
570                                        __D(bIntegerArgumentValueValid, E_INVALIDARG);
571                                        //_tprintf(_T("Option: Length, %d\n"), nIntegerArgumentValue);
572                                        Module.SetLength(nIntegerArgumentValue * 1000 * 10000i64);
573                                } else
574                                if(_tcschr(_T("Ff"), sArgument[0])) // Sine Signal Frequency, Hz
575                                {
576                                        __D(bIntegerArgumentValueValid, E_INVALIDARG);
577                                        //_tprintf(_T("Option: Sine Signal Frequency, %d\n"), nIntegerArgumentValue);
578                                        Module.SetSignalFrequency(nIntegerArgumentValue);
579                                } else
580                                if(_tcschr(_T("Ll"), sArgument[0])) // Sine Signal Loudness, dB below FS
581                                {
582                                        __D(bIntegerArgumentValueValid, E_INVALIDARG);
583                                        //_tprintf(_T("Option: Sine Signal Loudness, %d\n"), nIntegerArgumentValue);
584                                        Module.SetSignalLoudness(nIntegerArgumentValue);
585                                } else
586                                if(_tcschr(_T("Nn"), sArgument[0])) // Noise Signal Loudness, dB below FS
587                                {
588                                        __D(bIntegerArgumentValueValid, E_INVALIDARG);
589                                        //_tprintf(_T("Option: Noise Signal Loudness, %d\n"), nIntegerArgumentValue);
590                                        Module.SetNoiseLoudness(nIntegerArgumentValue);
591                                }
592                                continue;
593                        }
594                        #pragma endregion
595                        if(sArgument.GetLength() >= 2 && sArgument[0] == _T('"') && sArgument[sArgument.GetLength() - 1] == _T('"'))
596                                sArgument = sArgument.Mid(1, sArgument.GetLength() - 2);
597                        Module.SetPath(sArgument);
598                }
599                #pragma endregion
600                Module.WinMain(SW_SHOWNORMAL);
601        }
602        _ATLCATCH(Exception)
603        {
604                if(FAILED((HRESULT) Exception))
605                        _tprintf(_T("Error 0x%08x: %s\n"), (HRESULT) Exception, AtlFormatSystemMessage(Exception).TrimRight(_T("\t\n\r .")));
606                return (INT) (HRESULT) Exception;
607        }
608        _ATLCATCHALL()
609        {
610                _tprintf(_T("Fatal Error\n"));
611                return (INT) E_FAIL;
612        }
613        return 0;
614}
Note: See TracBrowser for help on using the repository browser.