www.pudn.com > FREQUENCYANALYZER.rar > Display.cpp, change:2000-10-28,size:2887b


#include "display.h" 
#include "views.h" 
#include "canvas.h" 
#include "recorder.h" 
#include "fft.h" 
#include "winex.h" 
#include "wassert.h" 
 
Painter::Painter ( 
        HWND hwnd, 
        ViewWave& viewWave, 
        ViewFreq& viewFreq, 
        int samplesPerBuf,  
        int samplesPerSec,  
        int fftPoints) 
:   _hwnd (hwnd),  
    _viewWave (viewWave), 
    _viewFreq (viewFreq), 
    _samplesPerBuf (samplesPerBuf), 
    _samplesPerSecond (samplesPerSec), 
    _fftPoints (fftPoints), 
    _bitsPerSample (16), 
    _pFftTransformer (fftPoints, samplesPerSec), 
    _pRecorder (samplesPerBuf, samplesPerSec) 
{ 
    // Let the caller Resume explicitly 
    //_thread.Resume (); 
} 
 
void Painter::Run () 
{ 
    for (;;) 
    { 
        _event.Wait (); 
        if (_isDying) 
            return; 
 
        Lock lock (_mutex); 
        if (_pRecorder->IsBufferDone ()) 
            LokWaveInData (); 
    } 
} 
 
void Painter::FlushThread () 
{ 
    _event.Release (); 
} 
 
bool Painter::ReInit ( 
        int samplesPerBuf,  
        int samplesPerSec,  
        int fftPoints, 
        int bitsPerSample) 
{ 
    Lock lock (_mutex); 
 
    if (_pRecorder->BitsPerSample() == bitsPerSample && 
        _pRecorder->SamplesPerSecond() == samplesPerSec && 
        _pFftTransformer->Points() == fftPoints && 
        _pRecorder->SampleCount() == samplesPerBuf) 
    { 
        return true; 
    } 
 
    _samplesPerBuf = samplesPerBuf; 
    _samplesPerSecond = samplesPerSec; 
    _fftPoints = fftPoints; 
    _bitsPerSample = bitsPerSample; 
 
    bool isStarted = _pRecorder->IsStarted(); 
    if (isStarted) 
        _pRecorder->Stop(); 
 
    bool isError = false; 
 
    try 
    { 
        _pFftTransformer.ReInit ( _fftPoints, _samplesPerSecond); 
        _pRecorder.ReInit (_bitsPerSample, _samplesPerBuf, _samplesPerSecond); 
    } 
    catch ( WinException e ) 
    { 
        char buf[50]; 
        wsprintf ( buf, "%s, Error %d", e.GetMessage(), e.GetError() ); 
        MessageBox (0, buf, "Exception", MB_ICONEXCLAMATION | MB_OK); 
        isError = true; 
    } 
    catch (...) 
    { 
        MessageBox (0, "Unknown", "Exception", MB_ICONEXCLAMATION | MB_OK); 
        isError = true; 
    } 
 
    if (isStarted) 
    { 
        isError = !_pRecorder->Start(_event); 
    } 
 
    return !isError; 
} 
 
bool Painter::Start () 
{ 
    Lock lock (_mutex); 
    return _pRecorder->Start (_event); 
} 
 
void Painter::Stop () 
{ 
    Lock lock (_mutex); 
    _pRecorder->Stop (); 
} 
 
void Painter::LokWaveInData () 
{ 
    SampleIter iter (_pRecorder.GetAccess()); 
    // Quickly release the buffer 
    if (!_pRecorder->BufferDone ()) 
        return; 
 
    _pFftTransformer->CopyIn (iter); 
    _pFftTransformer->Transform(); 
    _viewFreq.Update (_pFftTransformer.GetAccess()); 
    _viewWave.Update (_pFftTransformer.GetAccess()); 
}