home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / mac / SiteBldr / AMOVIE / SDK / _SETUP / COMMON.Z / synth.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-17  |  8.0 KB  |  249 lines

  1. //==========================================================================;
  2. //
  3. //  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4. //  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5. //  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6. //  PURPOSE.
  7. //
  8. //  Copyright (c) 1992 - 1996  Microsoft Corporation.  All Rights Reserved.
  9. //
  10. //--------------------------------------------------------------------------;
  11. //
  12. //  synth.h
  13. //
  14. //  Audio Signal Generator
  15.  
  16. #ifndef __AUDIOSYNTH__
  17. #define __AUDIOSYNTH__
  18.  
  19. //CLSID_SynthFilter
  20. //{79A98DE0-BC00-11ce-AC2E-444553540000}
  21. DEFINE_GUID(CLSID_SynthFilter,
  22. 0x79a98de0, 0xbc00, 0x11ce, 0xac, 0x2e, 0x44, 0x45, 0x53, 0x54, 0x0, 0x0);
  23.  
  24. //CLSID_SynthFilterPropertyPage
  25. //{79A98DE1-BC00-11ce-AC2E-444553540000}
  26. DEFINE_GUID(CLSID_SynthPropertyPage,
  27. 0x79a98de1, 0xbc00, 0x11ce, 0xac, 0x2e, 0x44, 0x45, 0x53, 0x54, 0x0, 0x0);
  28.  
  29. const double TWOPI = 6.283185308;
  30. const int MaxFrequency = 20000;
  31. const int MinFrequency = 0;
  32. const int DefaultFrequency = 440;       // A-440
  33. const int MaxAmplitude = 100;
  34. const int MinAmplitude = 0;
  35. const int DefaultSweepStart = DefaultFrequency;
  36. const int DefaultSweepEnd = 5000;
  37. const int WaveBufferSize = 2048;        // Size of each allocated buffer
  38.  
  39. enum Waveforms {
  40.     WAVE_SINE = 0,
  41.     WAVE_SQUARE,
  42.     WAVE_SAWTOOTH,
  43.     WAVE_SINESWEEP,
  44.     WAVE_LAST           // Always keep this guy last
  45. };
  46.  
  47. #define WM_PROPERTYPAGE_ENABLE  (WM_USER + 100)
  48.  
  49. // below stuff is implementation-only....
  50. #ifdef _AUDIOSYNTH_IMPLEMENTATION_
  51.  
  52. class CSynthStream;
  53.  
  54. // -------------------------------------------------------------------------
  55. // CAudioSynth
  56. // -------------------------------------------------------------------------
  57.  
  58. class CAudioSynth {
  59.  
  60. public:
  61.  
  62.     CAudioSynth(
  63.                 int Frequency = DefaultFrequency,
  64.                 int Waveform = WAVE_SINE,
  65.                 int iBitsPerSample = 8,
  66.                 int iChannels = 1,
  67.                 int iSamplesPerSec = 11025,
  68.                 int iAmplitude = 100
  69.                 );
  70.  
  71.     // Load the buffer with the current waveform
  72.     void FillAudioBuffer (BYTE pBuf[], int iSize);
  73.  
  74.     STDMETHODIMP get_Frequency(int *Frequency);
  75.     STDMETHODIMP put_Frequency(int  Frequency);
  76.     STDMETHODIMP get_Waveform(int *Waveform);
  77.     STDMETHODIMP put_Waveform(int  Waveform);
  78.     STDMETHODIMP get_Channels(int *Channels);
  79.     STDMETHODIMP put_Channels(int  Channels);
  80.     STDMETHODIMP get_BitsPerSample(int *BitsPerSample);
  81.     STDMETHODIMP put_BitsPerSample(int  BitsPersample);
  82.     STDMETHODIMP get_SamplesPerSec(int *SamplesPerSec);
  83.     STDMETHODIMP put_SamplesPerSec(int  SamplesPerSec);
  84.     STDMETHODIMP get_Amplitude(int *Amplitude);
  85.     STDMETHODIMP put_Amplitude(int  Amplitude);
  86.     STDMETHODIMP get_SweepRange(int *SweepStart, int *SweepEnd);
  87.     STDMETHODIMP put_SweepRange(int  SweepStart, int  SweepEnd);
  88.  
  89. private:
  90.     CCritSec m_SynthLock;
  91.  
  92.     WAVEFORMATEX wfex;          // the current format
  93.     WAVEFORMATEX wfexLast;      // last known waveformat
  94.  
  95.     int m_iWaveform;            // WAVE_SINE ...
  96.     int m_iFrequency;           // if not using sweep, this is the frequency
  97.     int m_iAmplitude;           // 0 to 100
  98.  
  99.     int m_iWaveformLast;        // keep track of the last known format
  100.     int m_iFrequencyLast;       // so we can flush the cache if necessary
  101.     int m_iAmplitudeLast;
  102.  
  103.     int m_iCurrentSample;       // 0 to iSamplesPerSec-1
  104.     BYTE * m_bWaveCache;        // Wave Cache as BYTEs
  105.     WORD * m_wWaveCache;        // Wave Cache as WORDs
  106.     int m_iWaveCacheSize;       // how big is the cache?
  107.     int m_iWaveCacheCycles;     // how many cycles are in the cache
  108.     int m_iWaveCacheIndex;
  109.  
  110.     int m_iSweepStart;           // start of sweep
  111.     int m_iSweepEnd;             // end of sweep
  112. //    double m_SweepDuration;     // how long the sweep lasts
  113.  
  114.     void AllocWaveCache    (void) ;
  115.     void CalcCacheSine     (void) ;
  116.     void CalcCacheSquare   (void) ;
  117.     void CalcCacheSawtooth (void) ;
  118.     void CalcCacheSweep    (void) ;
  119.  
  120. };
  121.  
  122.  
  123.  
  124. // -------------------------------------------------------------------------
  125. // CSynthFilter
  126. // -------------------------------------------------------------------------
  127. // CSynthFilter manages filter level stuff
  128.  
  129. class CSynthFilter :    public ISynth,
  130.                         public CPersistStream,
  131.                         public ISpecifyPropertyPages,
  132.                         public CSource {
  133.  
  134. public:
  135.  
  136.     static CUnknown *CreateInstance(LPUNKNOWN lpunk, HRESULT *phr);
  137.     ~CSynthFilter();
  138.  
  139.     DECLARE_IUNKNOWN;
  140.  
  141.     // override this to reveal our property interface
  142.     STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void ** ppv);
  143.  
  144.     // --- ISpecifyPropertyPages ---
  145.  
  146.     // return our property pages
  147.     STDMETHODIMP GetPages(CAUUID * pPages);
  148.  
  149.     // setup helper
  150.     LPAMOVIESETUP_FILTER GetSetupData();
  151.  
  152.     // --- IPersistStream Interface
  153.  
  154.     STDMETHODIMP GetClassID(CLSID *pClsid);
  155.     int SizeMax();
  156.     HRESULT WriteToStream(IStream *pStream);
  157.     HRESULT ReadFromStream(IStream *pStream);
  158.  
  159.     //
  160.     // --- ISynth ---
  161.     //
  162.  
  163.     STDMETHODIMP get_Frequency(int *Frequency);
  164.     STDMETHODIMP put_Frequency(int Frequency);
  165.     STDMETHODIMP get_Waveform(int *Waveform);
  166.     STDMETHODIMP put_Waveform(int Waveform);
  167.     STDMETHODIMP get_Channels(int *Channels);
  168.     STDMETHODIMP put_Channels(int Channels);
  169.     STDMETHODIMP get_BitsPerSample(int *BitsPerSample);
  170.     STDMETHODIMP put_BitsPerSample(int BitsPersample);
  171.     STDMETHODIMP get_SamplesPerSec(int *SamplesPerSec);
  172.     STDMETHODIMP put_SamplesPerSec(int SamplesPerSec);
  173.     STDMETHODIMP get_Amplitude(int *Amplitude);
  174.     STDMETHODIMP put_Amplitude(int Amplitude);
  175.     STDMETHODIMP get_SweepRange(int *SweepStart, int *SweepEnd);
  176.     STDMETHODIMP put_SweepRange(int  SweepStart, int  SweepEnd);
  177.     STDMETHODIMP put_PropertyPagehWnd (HWND hWnd);
  178.     STDMETHODIMP get_FilterIsStopped (BOOL *fStopped);
  179.  
  180.     CAudioSynth *m_Synth;           // the current synthesizer
  181.     HWND        m_hWndPropertyPage; // hWnd of our PropertyPage
  182.  
  183. private:
  184.  
  185.     // it is only allowed to to create these objects with CreateInstance
  186.     CSynthFilter(LPUNKNOWN lpunk, HRESULT *phr);
  187.  
  188.     // When the format changes, reconnect...
  189.     void CSynthFilter::ReconnectWithNewFormat(void);
  190.  
  191. };
  192.  
  193.  
  194. // -------------------------------------------------------------------------
  195. // CSynthStream
  196. // -------------------------------------------------------------------------
  197. // CSynthStream manages the data flow from the output pin.
  198.  
  199. class CSynthStream : public CSourceStream {
  200.  
  201. public:
  202.  
  203.     CSynthStream(HRESULT *phr, CSynthFilter *pParent, LPCWSTR pPinName);
  204.     ~CSynthStream();
  205.  
  206.     BOOL ReadyToStop(void) {return FALSE;}
  207.  
  208.     // stuff an audio buffer with the current format
  209.     HRESULT FillBuffer(IMediaSample *pms);
  210.  
  211.     // ask for buffers of the size appropriate to the agreed media type.
  212.     HRESULT DecideBufferSize(IMemAllocator *pIMemAlloc,
  213.                              ALLOCATOR_PROPERTIES *pProperties);
  214.  
  215.     // verify we can handle this format
  216.     HRESULT CheckMediaType(const CMediaType *pMediaType);
  217.  
  218.     // set the agreed media type
  219.     HRESULT SetMediaType(const CMediaType *pMediaType);
  220.  
  221.     HRESULT GetMediaType(CMediaType *pmt);
  222.  
  223.     // resets the stream time to zero.
  224.     HRESULT OnThreadCreate(void);
  225.  
  226.     HRESULT Active   (void);
  227.     HRESULT Inactive (void);
  228.  
  229. private:
  230.  
  231.     // Access to this state information should be serialized with the filters
  232.     // critical section (m_pFilter->pStateLock())
  233.  
  234.     CCritSec    m_cSharedState;     // use this to lock access to m_rtSampleTime and m_Synth
  235.                                     // which are shared with the worker thread.
  236.  
  237.     CRefTime     m_rtSampleTime;    // The time to be stamped on each sample
  238.     CAudioSynth *m_Synth;           // the current synthesizer
  239. };
  240.  
  241.  
  242.  
  243. #endif // _AUDIOSYNTH_IMPLEMENTATION_ implementation only....
  244.  
  245. #endif /* __AUDIOSYNTH__ */
  246.  
  247.  
  248. 
  249.