home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / mac / SiteBldr / AMOVIE / SDK / _SETUP / COMMON.Z / synthprp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-15  |  14.0 KB  |  538 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. // synthprop.cpp - Synthesizer Property Page
  13. //
  14.  
  15. #include <windows.h>
  16. #include <streams.h>
  17. #include <commctrl.h>
  18. #include <memory.h>
  19. #include <olectl.h>
  20.  
  21. #include "isynth.h"
  22. #include "synth.h"
  23. #include "synthprp.h"
  24. #include "resource.h"
  25.  
  26.  
  27. // -------------------------------------------------------------------------
  28. // CSynthProperties
  29. // -------------------------------------------------------------------------
  30.  
  31. //
  32. // CreateInstance
  33. //
  34.  
  35. CUnknown *CSynthProperties::CreateInstance(LPUNKNOWN lpunk, HRESULT *phr) 
  36. {
  37.     CUnknown *punk = new CSynthProperties(lpunk, phr);
  38.     if (punk == NULL) {
  39.         *phr = E_OUTOFMEMORY;
  40.     }
  41.  
  42.     return punk;
  43. }
  44.  
  45.  
  46. //
  47. // Constructor
  48. //
  49. // Creaete a Property page object for the synthesizer
  50.  
  51. CSynthProperties::CSynthProperties(LPUNKNOWN lpunk, HRESULT *phr)
  52.     : CBasePropertyPage(NAME("Synth Property Page"), lpunk, phr, 
  53.         IDD_SYNTHPROP1,IDS_SYNTHPROPNAME)
  54.     , m_pSynth(NULL)
  55.     , m_iSweepStart(DefaultSweepStart)
  56.     , m_iSweepEnd(DefaultSweepEnd)
  57. {
  58.     ASSERT(phr);
  59.  
  60.     InitCommonControls();
  61. }
  62.  
  63. //
  64. // OnConnect
  65. //
  66. // Give us the filter to communicate with
  67.  
  68. HRESULT CSynthProperties::OnConnect(IUnknown *pUnknown)
  69. {
  70.     ASSERT(m_pSynth == NULL);
  71.  
  72.     // Ask the filter for it's control interface
  73.  
  74.     HRESULT hr = pUnknown->QueryInterface(IID_ISynth,(void **)&m_pSynth);
  75.     if (FAILED(hr)) {
  76.         return E_NOINTERFACE;
  77.     }
  78.  
  79.     ASSERT(m_pSynth);
  80.  
  81.     // Get current filter state
  82.     m_pSynth->get_BitsPerSample(&m_iBitsPerSampleOriginal);
  83.     m_pSynth->get_Waveform(&m_iWaveformOriginal);
  84.     m_pSynth->get_Frequency(&m_iFrequencyOriginal);
  85.     m_pSynth->get_Channels(&m_iChannelsOriginal);
  86.     m_pSynth->get_SamplesPerSec(&m_iSamplesPerSecOriginal);
  87.     m_pSynth->get_Amplitude(&m_iAmplitudeOriginal);
  88.  
  89.     return NOERROR;
  90. }
  91.  
  92.  
  93. //
  94. // OnDisconnect
  95. //
  96. // Release the interface
  97.  
  98. HRESULT CSynthProperties::OnDisconnect()
  99. {
  100.     // Release the interface
  101.  
  102.     if (m_pSynth == NULL) {
  103.         return E_UNEXPECTED;
  104.     }
  105.  
  106.     m_pSynth->put_Waveform(m_iWaveformOriginal);
  107.     m_pSynth->put_Frequency(m_iFrequencyOriginal);
  108.     m_pSynth->put_Amplitude(m_iAmplitudeOriginal);
  109.  
  110.     BOOL fStopped;
  111.     m_pSynth->get_FilterIsStopped (&fStopped);
  112.  
  113.     if (fStopped) {
  114.         m_pSynth->put_Channels(m_iChannelsOriginal);
  115.         m_pSynth->put_BitsPerSample(m_iBitsPerSampleOriginal);
  116.         m_pSynth->put_SamplesPerSec(m_iSamplesPerSecOriginal);
  117.     }
  118.  
  119.     m_pSynth->Release();
  120.     m_pSynth = NULL;
  121.     return NOERROR;
  122. }
  123.  
  124.  
  125. //
  126. // OnActivate
  127. //
  128. // Called on dialog creation
  129.  
  130. HRESULT CSynthProperties::OnActivate(void)
  131. {
  132.     InitPropertiesDialog(m_hwnd);
  133.  
  134.     // The synth needs to notify us if it goes into run mode, 
  135.     // to prevent trying to change formats while running, so pass it our hwnd
  136.  
  137.     m_pSynth->put_PropertyPagehWnd (m_hwnd);
  138.  
  139.     // Disable the format change buttons if filter is active
  140.  
  141.     BOOL fStopped;
  142.     m_pSynth->get_FilterIsStopped (&fStopped);
  143.  
  144.     if (!fStopped)
  145.         PostMessage (m_hwnd, WM_PROPERTYPAGE_ENABLE, 0, 0);
  146.  
  147.     ASSERT(m_hwndFreqSlider);
  148.  
  149.     return NOERROR;
  150. }
  151.  
  152. //
  153. // OnDeactivate
  154. //
  155. // Called on dialog destruction
  156.  
  157. HRESULT
  158. CSynthProperties::OnDeactivate(void)
  159. {
  160.     return NOERROR;
  161. }
  162.  
  163.  
  164. //
  165. // OnApplyChanges
  166. //
  167. // User pressed the Apply button, remember the current settings
  168.  
  169. HRESULT CSynthProperties::OnApplyChanges(void)
  170. {
  171.     m_pSynth->get_BitsPerSample(&m_iBitsPerSampleOriginal);
  172.     m_pSynth->get_Waveform(&m_iWaveformOriginal);
  173.     m_pSynth->get_Frequency(&m_iFrequencyOriginal);
  174.     m_pSynth->get_Channels(&m_iChannelsOriginal);
  175.     m_pSynth->get_SamplesPerSec(&m_iSamplesPerSecOriginal);
  176.     m_pSynth->get_Amplitude(&m_iAmplitudeOriginal);
  177.  
  178.     return NOERROR;
  179. }
  180.  
  181.  
  182. //
  183. // OnReceiveMessages
  184. //
  185. // Handles the messages for our property window
  186.  
  187. BOOL CSynthProperties::OnReceiveMessage( HWND hwnd
  188.                                 , UINT uMsg
  189.                                 , WPARAM wParam
  190.                                 , LPARAM lParam) 
  191. {
  192.     switch (uMsg) {
  193.  
  194.     case WM_PROPERTYPAGE_ENABLE:
  195.         // Our private message that our owning filter sends us when changing to a Run / Stop / Pause
  196.         // state.  if lParam, then enable the controls which affect the format; if not lParam, then
  197.         // disable the controls that affect the format.
  198.  
  199.         EnableWindow (GetDlgItem (hwnd, IDC_SAMPLINGFREQ11), (BOOL) lParam);
  200.         EnableWindow (GetDlgItem (hwnd, IDC_SAMPLINGFREQ22), (BOOL) lParam);
  201.         EnableWindow (GetDlgItem (hwnd, IDC_SAMPLINGFREQ44), (BOOL) lParam);
  202.  
  203.         EnableWindow (GetDlgItem (hwnd, IDC_BITSPERSAMPLE8),  (BOOL) lParam);
  204.         EnableWindow (GetDlgItem (hwnd, IDC_BITSPERSAMPLE16), (BOOL) lParam);
  205.  
  206.         EnableWindow (GetDlgItem (hwnd, IDC_CHANNELS1), (BOOL) lParam);
  207.         EnableWindow (GetDlgItem (hwnd, IDC_CHANNELS2), (BOOL) lParam);
  208.         break;
  209.  
  210.     case WM_VSCROLL:
  211.         if ((HWND) lParam == m_hwndFreqSlider)
  212.             OnFreqSliderNotification(LOWORD (wParam), HIWORD (wParam));
  213.         else if ((HWND) lParam == m_hwndAmplitudeSlider)
  214.             OnAmpSliderNotification(LOWORD (wParam), HIWORD (wParam));
  215.         SetDirty();
  216.         return TRUE;
  217.  
  218.     case WM_COMMAND:
  219.  
  220.         switch (LOWORD(wParam)) {
  221.  
  222.         case IDC_FREQUENCYTEXT:
  223.         {
  224.             int iNotify = HIWORD (wParam);
  225.  
  226.             if (iNotify == EN_KILLFOCUS) {
  227.                 BOOL fOK;
  228.  
  229.                 int iPos = GetDlgItemInt (hwnd, IDC_FREQUENCYTEXT, &fOK, FALSE);
  230.                 int iMaxFreq;
  231.  
  232.                 m_pSynth->get_SamplesPerSec(&iMaxFreq);
  233.                 iMaxFreq /= 2;
  234.  
  235.                 if (!fOK || (iPos > iMaxFreq || iPos < 0)) {
  236.                     int iCurrentFreq;
  237.                     m_pSynth->get_Frequency(&iCurrentFreq);
  238.                     SetDlgItemInt (hwnd, IDC_FREQUENCYTEXT, iCurrentFreq, FALSE);
  239.                     break;
  240.                 }
  241.  
  242.                 SendMessage(m_hwndFreqSlider, TBM_SETPOS, TRUE, (LPARAM) iMaxFreq - iPos);
  243.                 m_pSynth->put_Frequency(iPos);
  244.                 SetDirty();
  245.             }
  246.         }
  247.         break;
  248.  
  249.         case IDC_AMPLITUDETEXT:
  250.         {
  251.             int iNotify = HIWORD (wParam);
  252.  
  253.             if (iNotify == EN_KILLFOCUS) {
  254.                 BOOL fOK;
  255.  
  256.                 int iPos = GetDlgItemInt (hwnd, IDC_AMPLITUDETEXT, &fOK, FALSE);
  257.  
  258.                 if (!fOK || (iPos > MaxAmplitude || iPos < 0)) {
  259.                     int iCurrentAmplitude;
  260.  
  261.                     m_pSynth->get_Amplitude(&iCurrentAmplitude);
  262.                     SetDlgItemInt (hwnd, IDC_AMPLITUDETEXT, iCurrentAmplitude, FALSE);
  263.                     break;
  264.                 }
  265.  
  266.                 SendMessage(m_hwndAmplitudeSlider, TBM_SETPOS, TRUE,
  267.                         (LPARAM) MaxAmplitude - iPos);
  268.                 m_pSynth->put_Amplitude(iPos);
  269.                 SetDirty();
  270.             }
  271.         }
  272.         break;
  273.  
  274.         case IDC_SAMPLINGFREQ11:
  275.             m_pSynth->put_SamplesPerSec(11025);
  276.             RecalcFreqSlider();
  277.             SetDirty();
  278.             break;
  279.         case IDC_SAMPLINGFREQ22:
  280.             m_pSynth->put_SamplesPerSec(22050);
  281.             RecalcFreqSlider();
  282.             SetDirty();
  283.             break;
  284.         case IDC_SAMPLINGFREQ44:
  285.             m_pSynth->put_SamplesPerSec(44100);
  286.             RecalcFreqSlider();
  287.             SetDirty();
  288.             break;
  289.  
  290.  
  291.         case IDC_BITSPERSAMPLE8:
  292.             m_pSynth->put_BitsPerSample(8);
  293.             SetDirty();
  294.             break;
  295.         case IDC_BITSPERSAMPLE16:
  296.             m_pSynth->put_BitsPerSample(16);
  297.             SetDirty();
  298.             break;
  299.  
  300.  
  301.         case IDC_CHANNELS1:
  302.             m_pSynth->put_Channels(1);
  303.             SetDirty();
  304.             break;
  305.         case IDC_CHANNELS2:
  306.             m_pSynth->put_Channels(2);
  307.             SetDirty();
  308.             break;
  309.  
  310.  
  311.         case IDC_WAVESINE:
  312.             m_pSynth->put_Waveform(WAVE_SINE);
  313.             SetDirty();
  314.             break;
  315.         case IDC_WAVESQUARE:
  316.             m_pSynth->put_Waveform(WAVE_SQUARE);
  317.             SetDirty();
  318.             break;
  319.         case IDC_WAVESAWTOOTH:
  320.             m_pSynth->put_Waveform(WAVE_SAWTOOTH);
  321.             SetDirty();
  322.             break;
  323.         case IDC_WAVESWEEP:
  324.             m_pSynth->put_Waveform(WAVE_SINESWEEP);
  325.             SetDirty();
  326.             break;
  327.  
  328.         default:
  329.             break;
  330.  
  331.         }
  332.         return TRUE;
  333.  
  334.     case WM_DESTROY:
  335.         m_pSynth->put_PropertyPagehWnd (NULL);
  336.         return TRUE;
  337.  
  338.     default:
  339.         return FALSE;
  340.  
  341.     }
  342.     return TRUE;
  343. }
  344.  
  345.  
  346. //
  347. // InitPropertiesDialog
  348. //
  349.  
  350. void 
  351. CSynthProperties::InitPropertiesDialog(HWND hwndParent) 
  352. {
  353.     m_hwndFreqSlider = GetDlgItem (hwndParent, IDC_FREQTRACKBAR);
  354.     m_hwndFreqText  = GetDlgItem (hwndParent, IDC_FREQUENCYTEXT);
  355.     m_hwndAmplitudeSlider = GetDlgItem (hwndParent, IDC_AMPLITUDETRACKBAR);
  356.     m_hwndAmplitudeText  = GetDlgItem (hwndParent, IDC_AMPLITUDETEXT);
  357.  
  358.     // Sampling Frequency
  359.     int i;
  360.     switch (m_iSamplesPerSecOriginal) {
  361.     case 11025: i = IDC_SAMPLINGFREQ11; break;
  362.     case 22050: i = IDC_SAMPLINGFREQ22; break;
  363.     case 44100: i = IDC_SAMPLINGFREQ44; break;
  364.     default:
  365.         ASSERT(0);
  366.     }
  367.     CheckRadioButton(hwndParent,
  368.         IDC_SAMPLINGFREQ11,
  369.         IDC_SAMPLINGFREQ44,
  370.         i);
  371.  
  372.     // BitsPerSample
  373.     CheckRadioButton(hwndParent,
  374.                 IDC_BITSPERSAMPLE8,
  375.                 IDC_BITSPERSAMPLE16,
  376.                 IDC_BITSPERSAMPLE8 + m_iBitsPerSampleOriginal / 8 - 1);
  377.  
  378.     // Waveform 0 == sine, 1 == square, ...
  379.     CheckRadioButton(hwndParent,
  380.                 IDC_WAVESINE,
  381.                 IDC_WAVESWEEP,
  382.                 IDC_WAVESINE + m_iWaveformOriginal);
  383.  
  384.     // Channels
  385.     CheckRadioButton(hwndParent,
  386.                 IDC_CHANNELS1,
  387.                 IDC_CHANNELS2,
  388.                 IDC_CHANNELS1 + m_iChannelsOriginal - 1);
  389.  
  390.     //
  391.     // Frequency trackbar
  392.     //
  393.  
  394.     RecalcFreqSlider();
  395.  
  396.     //
  397.     //  Amplitude trackbar
  398.     //
  399.  
  400.     SendMessage(m_hwndAmplitudeSlider, TBM_SETRANGE, TRUE,
  401.         MAKELONG(MinAmplitude, MaxAmplitude) );
  402.  
  403.     SendMessage(m_hwndAmplitudeSlider, TBM_SETTIC, 0,
  404.         ((MinAmplitude + MaxAmplitude) / 2));
  405.  
  406.     SendMessage(m_hwndAmplitudeSlider, TBM_SETPOS, TRUE,
  407.         (LPARAM) (MaxAmplitude - m_iAmplitudeOriginal));
  408.  
  409.     SetDlgItemInt (hwndParent, IDC_AMPLITUDETEXT,
  410.         m_iAmplitudeOriginal, TRUE);
  411. }
  412.  
  413.  
  414. //
  415. // RecalcFreqSlider
  416. //
  417. // Set the range, current settings for the Freq scrollbar
  418.  
  419. void 
  420. CSynthProperties::RecalcFreqSlider(void) 
  421. {
  422.     int iPos, iMaxFreq;
  423.  
  424.     // Limit the frequency to one half the sampling frequency
  425.  
  426.     m_pSynth->get_SamplesPerSec(&iMaxFreq);
  427.     iMaxFreq /= 2;
  428.     m_pSynth->get_Frequency(&iPos);
  429.     if (iPos > iMaxFreq)
  430.         iPos = iMaxFreq;
  431.  
  432.     SendMessage(m_hwndFreqSlider, TBM_SETRANGE, TRUE,
  433.         MAKELONG(0, iMaxFreq));
  434.  
  435.     SendMessage(m_hwndFreqSlider, TBM_SETTIC, 0,
  436.         iMaxFreq / 2);
  437.  
  438.     SendMessage(m_hwndFreqSlider, TBM_SETPOS, TRUE,
  439.         (LPARAM) (iMaxFreq - iPos));
  440.  
  441.     SendMessage(m_hwndFreqSlider, TBM_SETPAGESIZE, 0, 10);
  442.  
  443.     SendMessage(m_hwndFreqSlider, TBM_SETSEL, TRUE,
  444.         MAKELONG (iMaxFreq - m_iSweepEnd, iMaxFreq - m_iSweepStart));
  445.  
  446.     SetDlgItemInt (m_hwnd, IDC_FREQUENCYTEXT,
  447.         iPos, TRUE);
  448.  
  449. }
  450.  
  451. //
  452. // OnFreqSliderNotification
  453. //
  454. // Handle the notification meesages from the slider control
  455.  
  456. void 
  457. CSynthProperties::OnFreqSliderNotification(WPARAM wParam, WORD wPosition)
  458. {
  459.     int MaxFreq;
  460.     int Freq;
  461.     int SliderPos;
  462.  
  463.     switch (wParam) {
  464.  
  465.     case TB_ENDTRACK:
  466.     case TB_THUMBTRACK:
  467.     case TB_LINEDOWN:
  468.     case TB_LINEUP: {
  469.         // max frequency of slider is half the sampling frequency
  470.         m_pSynth->get_SamplesPerSec (&MaxFreq);
  471.         MaxFreq /= 2;
  472.         SliderPos = (int) SendMessage(m_hwndFreqSlider, TBM_GETPOS, 0, 0L);
  473.         Freq = MaxFreq - SliderPos;
  474.         m_pSynth->put_Frequency (Freq);
  475.  
  476.         // Set the end of sweep to the current slider pos
  477.         if (!(GetKeyState (VK_SHIFT) & 0x8000)) {
  478.             m_iSweepEnd = Freq;
  479.         }
  480.  
  481.         // Set the start of the sweep range if SHIFT key is pressed
  482.         if (GetKeyState (VK_SHIFT) & 0x8000) {
  483.             m_iSweepStart = Freq;
  484.         }
  485.         m_pSynth->put_SweepRange (m_iSweepStart, m_iSweepEnd);
  486.  
  487.         if (m_iSweepEnd > m_iSweepStart)
  488.             SendMessage(m_hwndFreqSlider, TBM_SETSEL, TRUE,
  489.                 MAKELONG (MaxFreq - m_iSweepEnd, MaxFreq - m_iSweepStart));
  490.         else
  491.             SendMessage(m_hwndFreqSlider, TBM_SETSEL, TRUE,
  492.                 MAKELONG (MaxFreq - m_iSweepStart, MaxFreq - m_iSweepEnd));
  493.  
  494.         SetDlgItemInt (m_hwnd, IDC_FREQUENCYTEXT, Freq, TRUE);
  495.  
  496.     }
  497.     break;
  498.  
  499.     }
  500. }
  501.  
  502. //
  503. // OnAmpSliderNotification
  504. //
  505. // Handle the notification meesages from the slider control
  506.  
  507. void 
  508. CSynthProperties::OnAmpSliderNotification(WPARAM wParam, WORD wPosition)
  509. {
  510.     switch (wParam) {
  511.  
  512.     case TB_ENDTRACK:
  513.     case TB_THUMBTRACK:
  514.     case TB_LINEDOWN:
  515.     case TB_LINEUP: {
  516.         int Level = (int) SendMessage(m_hwndAmplitudeSlider, TBM_GETPOS, 0, 0L);
  517.         m_pSynth->put_Amplitude (MaxAmplitude - Level);
  518.         SetDlgItemInt (m_hwnd, IDC_AMPLITUDETEXT, MaxAmplitude - Level, TRUE);
  519.     }
  520.     break;
  521.  
  522.     }
  523. }
  524.  
  525. //
  526. // SetDirty
  527. //
  528. // notifies the property page site of changes
  529.  
  530. void 
  531. CSynthProperties::SetDirty()
  532. {
  533.     m_bDirty = TRUE;
  534.     if (m_pPageSite)
  535.         m_pPageSite->OnStatusChange(PROPPAGESTATUS_DIRTY);
  536. }
  537.  
  538.