home *** CD-ROM | disk | FTP | other *** search
/ Game Audio Programming / GameAudioProgramming.iso / Game_Audio / AudioTest / System.cpp < prev    next >
C/C++ Source or Header  |  2002-07-12  |  14KB  |  407 lines

  1. /***********************************************************\
  2. Copyright (C) James Boer, 2002. 
  3. All rights reserved worldwide.
  4.  
  5. This software is provided "as is" without express or implied
  6. warranties. You may freely copy and compile this source into
  7. applications you distribute provided that the copyright text
  8. below is included in the resulting source code, for example:
  9. "Portions Copyright (C) James Boer, 2002"
  10. \***********************************************************/
  11.  
  12. #include "stdafx.h"
  13. #include "AudioTest.h"
  14. #include "System.h"
  15.  
  16. #include "Audio.h"
  17.  
  18. #include "AudioTestDlg.h"
  19. #include "Sound2D.h"
  20. #include "Sound3D.h"
  21. #include "Segment.h"
  22. #include "SoundScript.h"
  23. #include "MusicScript.h"
  24.  
  25. using namespace Audio;
  26.  
  27. const int STATS_TIMER                = 1;
  28.  
  29. #ifdef _DEBUG
  30. #define new DEBUG_NEW
  31. #undef THIS_FILE
  32. static char THIS_FILE[] = __FILE__;
  33. #endif
  34.  
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CSystem property page
  37.  
  38. IMPLEMENT_DYNCREATE(CSystem, CPropertyPage)
  39.  
  40. CSystem::CSystem() : CPropertyPage(CSystem::IDD)
  41. {
  42.     //{{AFX_DATA_INIT(CSystem)
  43.     //}}AFX_DATA_INIT
  44.     m_nSoundVolumeUpdateTimer = 0;
  45.     m_nMusicVolumeUpdateTimer = 0;
  46. }
  47.  
  48. CSystem::~CSystem()
  49. {
  50. }
  51.  
  52. void CSystem::DoDataExchange(CDataExchange* pDX)
  53. {
  54.     CPropertyPage::DoDataExchange(pDX);
  55.     //{{AFX_DATA_MAP(CSystem)
  56.     DDX_Control(pDX, IDC_SYSTEM_CACHE_BUFFERS, m_CacheBuffers);
  57.     DDX_Control(pDX, IDC_SYSTEM_AUTO_STREAM, m_AutoStream);
  58.     DDX_Control(pDX, IDC_SYSTEM_USE_ZOOMFX, m_UseZoomFX);
  59.     DDX_Control(pDX, IDC_SYSTEM_USE_EAX, m_UseEAX);
  60.     DDX_Control(pDX, IDC_EDIT_FORCE_2D_SOFTWARE, m_Force2DSoftware);
  61.     DDX_Control(pDX, IDC_EDIT_FORCE_3D_SOFTWARE, m_Force3DSoftware);
  62.     DDX_Control(pDX, IDC_EDIT_LOADED_SEGMENTS, m_SegmentsLoaded);
  63.     DDX_Control(pDX, IDC_EDIT_LOADED_3D, m_3DLoaded);
  64.     DDX_Control(pDX, IDC_EDIT_LOADED_2D, m_2DLoaded);
  65.     DDX_Control(pDX, IDC_SPIN_SYSTEM_SEGMENTMAX, m_SegmentMax);
  66.     DDX_Control(pDX, IDC_SPIN_SYSTEM_3DSWBUFFERMAX, m_3DSoftwareBufferMax);
  67.     DDX_Control(pDX, IDC_SPIN_SYSTEM_3DHWBUFFERMIN, m_3DHardwareBufferMin);
  68.     DDX_Control(pDX, IDC_SPIN_SYSTEM_3DHWBUFFERMAX, m_3DHardwareBufferMax);
  69.     DDX_Control(pDX, IDC_SPIN_SYSTEM_2DSWBUFFERMAX, m_2DSoftwareBufferMax);
  70.     DDX_Control(pDX, IDC_SPIN_SYSTEM_2DHWBUFFERMIN, m_2DHardwareBufferMin);
  71.     DDX_Control(pDX, IDC_SPIN_SYSTEM_2DHWBUFFERMAX, m_2DHardwareBufferMax);
  72.     DDX_Control(pDX, IDC_COMBO_OPTIMAL_RATE, m_OptimalRate);
  73.     DDX_Control(pDX, IDC_SYSTEM_USEMUSICREVERB, m_UseMusicReverb);
  74.     DDX_Control(pDX, IDC_SYSTEM_LOADASYNC, m_LoadAsync);
  75.     DDX_Control(pDX, IDC_SYSTEM_FORCESOFTWARE, m_ForceSoftware);
  76.     DDX_Control(pDX, IDC_SLIDER_SOUND, m_SoundVolume);
  77.     DDX_Control(pDX, IDC_SLIDER_MUSIC, m_MusicVolume);
  78.     DDX_Control(pDX, IDC_PAUSEALL, m_PauseAll);
  79.     DDX_Control(pDX, IDC_STOPALL, m_StopAll);
  80.     DDX_Control(pDX, IDC_RESUMEALL, m_ResumeAll);
  81.     //}}AFX_DATA_MAP
  82. }
  83.  
  84.  
  85. BEGIN_MESSAGE_MAP(CSystem, CPropertyPage)
  86.     //{{AFX_MSG_MAP(CSystem)
  87.     ON_BN_CLICKED(IDC_INITIALIZE, OnInitialize)
  88.     ON_BN_CLICKED(IDC_TERM, OnTerm)
  89.     ON_BN_CLICKED(IDC_RESUMEALL, OnResumeall)
  90.     ON_BN_CLICKED(IDC_PAUSEALL, OnPauseall)
  91.     ON_BN_CLICKED(IDC_STOPALL, OnStopall)
  92.     ON_WM_TIMER()
  93.     ON_WM_HSCROLL()
  94.     ON_WM_DESTROY()
  95.     //}}AFX_MSG_MAP
  96. END_MESSAGE_MAP()
  97.  
  98. /////////////////////////////////////////////////////////////////////////////
  99. // CSystem message handlers
  100.  
  101. BOOL CSystem::OnInitDialog() 
  102. {
  103.     CPropertyPage::OnInitDialog();
  104.  
  105.     m_hFont =  GetStockObject(DEFAULT_GUI_FONT);
  106.     
  107.     // Load the play button icons
  108.     m_hPlayIcon = LoadIcon(::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_PLAY));
  109.     m_hPauseIcon = LoadIcon(::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_PAUSE));
  110.     m_hStopIcon = LoadIcon(::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_STOP));
  111.  
  112.     // Set the master play control buttons
  113.     m_ResumeAll.SetIcon(m_hPlayIcon);
  114.     m_PauseAll.SetIcon(m_hPauseIcon);
  115.     m_StopAll.SetIcon(m_hStopIcon);
  116.  
  117.     // Set up the volume sliders
  118.     m_SoundVolume.SetRange(0, 10000);
  119.     m_SoundVolume.SetPos(10000);
  120.     m_SoundVolume.Invalidate();
  121.     m_MusicVolume.SetRange(0, 10000);
  122.     m_MusicVolume.SetPos(10000);
  123.     m_MusicVolume.Invalidate();
  124.  
  125.     // Set all system initialization options to their default values
  126.     AudioMgrInit init;
  127.     m_LoadAsync.SetCheck(init.m_bLoadAsyncronously ? true : false);
  128.     m_ForceSoftware.SetCheck(init.m_bForceSoftware ? true : false);
  129.     m_UseMusicReverb.SetCheck(init.m_bUseMusicReverb ? true : false);
  130.     m_UseEAX.SetCheck(init.m_bUseEAX ? true : false);
  131.     m_UseZoomFX.SetCheck(init.m_bUseZoomFX ? true : false);
  132.     m_AutoStream.SetCheck(init.m_bAutoStream ? true : false);
  133.     m_CacheBuffers.SetCheck(init.m_bCacheBuffers ? true : false);
  134.     m_SegmentMax.SetRange(1, 1024);
  135.     m_SegmentMax.SetPos(init.m_nSegmentMax);
  136.     m_3DSoftwareBufferMax.SetRange(1, 1024);
  137.     m_3DSoftwareBufferMax.SetPos(init.m_n3DSoftwareBufferMax);
  138.     m_3DHardwareBufferMin.SetRange(1, 1024);
  139.     m_3DHardwareBufferMin.SetPos(init.m_n3DHardwareBufferMin);
  140.     m_3DHardwareBufferMax.SetRange(1, 1024);
  141.     m_3DHardwareBufferMax.SetPos(init.m_n3DHardwareBufferMax);
  142.     m_2DSoftwareBufferMax.SetRange(1, 1024);
  143.     m_2DSoftwareBufferMax.SetPos(init.m_n2DSoftwareBufferMax);
  144.     m_2DHardwareBufferMin.SetRange(1, 1024);
  145.     m_2DHardwareBufferMin.SetPos(init.m_n2DHardwareBufferMin);
  146.     m_2DHardwareBufferMax.SetRange(1, 1024);
  147.     m_2DHardwareBufferMax.SetPos(init.m_n2DHardwareBufferMax);
  148.  
  149.     m_OptimalRate.AddString("16-bit, 11.025 KHz");
  150.     m_OptimalRate.AddString("16-bit, 22.05 KHz");
  151.     m_OptimalRate.AddString("16-bit, 44.1KHz");
  152.     m_OptimalRate.AddString("8-bit, 11.025 KHz");
  153.     m_OptimalRate.AddString("8-bit, 22.05 KHz");
  154.     m_OptimalRate.AddString("8-bit, 44.1KHz");
  155.  
  156.     int iCursor = 0;
  157.     if(init.m_nOptimalSampleBits == 8)
  158.         iCursor += 3;
  159.     if(init.m_nOptimalSampleRate == 22050)
  160.         iCursor += 1;
  161.     else if(init.m_nOptimalSampleRate == 44100)
  162.         iCursor += 2;
  163.     m_OptimalRate.SetCurSel(iCursor);
  164.     
  165.  
  166.     return TRUE;  // return TRUE unless you set the focus to a control
  167.                   // EXCEPTION: OCX Property Pages should return FALSE
  168. }
  169.  
  170. void CSystem::OnInitialize() 
  171. {
  172.     AudioMgrInit audiomgrinit;
  173.     audiomgrinit.m_hWnd = AfxGetMainWnd()->GetSafeHwnd();
  174.     audiomgrinit.m_bLoadAsyncronously = (m_LoadAsync.GetCheck() == 1) ? true : false;
  175.     audiomgrinit.m_bForceSoftware = (m_ForceSoftware.GetCheck() == 1) ? true : false;
  176.     audiomgrinit.m_bUseMusicReverb = (m_UseMusicReverb.GetCheck() == 1) ? true : false;
  177.     audiomgrinit.m_bUseEAX = (m_UseEAX.GetCheck() == 1) ? true : false;
  178.     audiomgrinit.m_bUseZoomFX = (m_UseZoomFX.GetCheck() == 1) ? true : false;
  179.     audiomgrinit.m_bAutoStream = (m_AutoStream.GetCheck() == 1) ? true : false;
  180.     audiomgrinit.m_bCacheBuffers = (m_CacheBuffers.GetCheck() == 1) ? true : false;
  181.     audiomgrinit.m_nSegmentMax = m_SegmentMax.GetPos();
  182.     audiomgrinit.m_n3DSoftwareBufferMax = m_3DSoftwareBufferMax.GetPos();
  183.     audiomgrinit.m_n3DHardwareBufferMin = m_3DHardwareBufferMin.GetPos();
  184.     audiomgrinit.m_n3DHardwareBufferMax = m_3DHardwareBufferMax.GetPos();
  185.     audiomgrinit.m_n2DSoftwareBufferMax = m_2DSoftwareBufferMax.GetPos();
  186.     audiomgrinit.m_n2DHardwareBufferMin = m_2DHardwareBufferMin.GetPos();
  187.     audiomgrinit.m_n2DHardwareBufferMax = m_2DHardwareBufferMax.GetPos();
  188.     int iCursor = m_OptimalRate.GetCurSel();
  189.     switch(iCursor)
  190.     {
  191.     case 0:
  192.         audiomgrinit.m_nOptimalSampleBits = 16;
  193.         audiomgrinit.m_nOptimalSampleRate = 11025;
  194.         break;
  195.     case 1:
  196.         audiomgrinit.m_nOptimalSampleBits = 16;
  197.         audiomgrinit.m_nOptimalSampleRate = 22050;
  198.         break;
  199.     case 2:
  200.         audiomgrinit.m_nOptimalSampleBits = 16;
  201.         audiomgrinit.m_nOptimalSampleRate = 44100;
  202.         break;
  203.     case 3:
  204.         audiomgrinit.m_nOptimalSampleBits = 8;
  205.         audiomgrinit.m_nOptimalSampleRate = 11025;
  206.         break;
  207.     case 4:
  208.         audiomgrinit.m_nOptimalSampleBits = 8;
  209.         audiomgrinit.m_nOptimalSampleRate = 22050;
  210.         break;
  211.     case 5:
  212.         audiomgrinit.m_nOptimalSampleBits = 8;
  213.         audiomgrinit.m_nOptimalSampleRate = 44100;
  214.         break;
  215.     };
  216.  
  217.     audiomgrinit.m_pMusicCallback = MusicMgr();
  218.  
  219.     if(!AudioMgr()->Init(audiomgrinit))
  220.         AfxMessageBox("Error initializing audio system");
  221.  
  222.     m_LoadAsync.EnableWindow(FALSE);
  223.     m_ForceSoftware.EnableWindow(FALSE);
  224.     m_UseMusicReverb.EnableWindow(FALSE);
  225.     m_UseEAX.EnableWindow(FALSE);
  226.     m_UseZoomFX.EnableWindow(FALSE);
  227.     m_AutoStream.EnableWindow(FALSE);
  228.     m_CacheBuffers.EnableWindow(FALSE);
  229.     m_SegmentMax.EnableWindow(FALSE);
  230.     m_3DSoftwareBufferMax.EnableWindow(FALSE);
  231.     m_3DHardwareBufferMin.EnableWindow(FALSE);
  232.     m_3DHardwareBufferMax.EnableWindow(FALSE);
  233.     m_2DSoftwareBufferMax.EnableWindow(FALSE);
  234.     m_2DHardwareBufferMin.EnableWindow(FALSE);
  235.     m_2DHardwareBufferMax.EnableWindow(FALSE);
  236.     m_OptimalRate.EnableWindow(FALSE);
  237.     CButton* pCheck = (CButton*) GetDlgItem(IDC_EDIT_SYSTEM_2DHWBUFFERMIN);
  238.     pCheck->EnableWindow(FALSE);
  239.     pCheck = (CButton*) GetDlgItem(IDC_EDIT_SYSTEM_3DHWBUFFERMIN);
  240.     pCheck->EnableWindow(FALSE);
  241.     pCheck = (CButton*) GetDlgItem(IDC_EDIT_SYSTEM_2DHWBUFFERMAX);
  242.     pCheck->EnableWindow(FALSE);
  243.     pCheck = (CButton*) GetDlgItem(IDC_EDIT_SYSTEM_3DHWBUFFERMAX);
  244.     pCheck->EnableWindow(FALSE);
  245.     pCheck = (CButton*) GetDlgItem(IDC_EDIT_SYSTEM_2DSWBUFFERMAX);
  246.     pCheck->EnableWindow(FALSE);
  247.     pCheck = (CButton*) GetDlgItem(IDC_EDIT_SYSTEM_3DSWBUFFERMAX);
  248.     pCheck->EnableWindow(FALSE);
  249.     pCheck = (CButton*) GetDlgItem(IDC_EDIT_SYSTEM_SEGMENTMAX);
  250.     pCheck->EnableWindow(FALSE);
  251.  
  252.  
  253.     IListener* pListener;
  254.     if(!AudioMgr()->GetListener(pListener))
  255.         AfxMessageBox("Error getting listener object");
  256.  
  257.     ListenerInit listenerinit;
  258.     if(!pListener->Init(listenerinit))
  259.         AfxMessageBox("Error initializing listener object");
  260.  
  261.     if(!SoundMgr()->Init())
  262.         AfxMessageBox("Error initializing sound manager");
  263.     
  264.     if(!MusicMgr()->Init())
  265.         AfxMessageBox("Error initializing music manager");
  266.  
  267.  
  268.     m_nSoundVolumeUpdateTimer = SetTimer(STATS_TIMER, 100, NULL);
  269.     UpdateStats();
  270.  
  271. }
  272.  
  273. void CSystem::OnTerm() 
  274. {
  275.     m_pTestDlg->m_pSound2D->ClearList();
  276.     m_pTestDlg->m_pSound3D->ClearList();
  277.     m_pTestDlg->m_pSegment->ClearList();
  278.     m_pTestDlg->m_pMusicScript->ClearList();
  279.     m_pTestDlg->m_pSoundScript->ClearList();
  280.  
  281.     MusicMgr()->Term();
  282.     SoundMgr()->Term();
  283.  
  284.     IListener* pListener;
  285.     if(AudioMgr()->GetListener(pListener))
  286.         pListener->Term();
  287.  
  288.     AudioMgr()->Term();
  289.  
  290.     KillTimer(m_nSoundVolumeUpdateTimer);
  291.     UpdateStats();
  292.  
  293.     m_LoadAsync.EnableWindow(TRUE);
  294.     m_ForceSoftware.EnableWindow(TRUE);
  295.     m_UseMusicReverb.EnableWindow(TRUE);
  296.     m_UseEAX.EnableWindow(TRUE);
  297.     m_UseZoomFX.EnableWindow(TRUE);
  298.     m_AutoStream.EnableWindow(TRUE);
  299.     m_CacheBuffers.EnableWindow(TRUE);
  300.     m_SegmentMax.EnableWindow(TRUE);
  301.     m_3DSoftwareBufferMax.EnableWindow(TRUE);
  302.     m_3DHardwareBufferMin.EnableWindow(TRUE);
  303.     m_3DHardwareBufferMax.EnableWindow(TRUE);
  304.     m_2DSoftwareBufferMax.EnableWindow(TRUE);
  305.     m_2DHardwareBufferMin.EnableWindow(TRUE);
  306.     m_2DHardwareBufferMax.EnableWindow(TRUE);
  307.     m_OptimalRate.EnableWindow(TRUE);
  308.     CButton* pCheck = (CButton*) GetDlgItem(IDC_EDIT_SYSTEM_2DHWBUFFERMIN);
  309.     pCheck->EnableWindow(TRUE);
  310.     pCheck = (CButton*) GetDlgItem(IDC_EDIT_SYSTEM_3DHWBUFFERMIN);
  311.     pCheck->EnableWindow(TRUE);
  312.     pCheck = (CButton*) GetDlgItem(IDC_EDIT_SYSTEM_2DHWBUFFERMAX);
  313.     pCheck->EnableWindow(TRUE);
  314.     pCheck = (CButton*) GetDlgItem(IDC_EDIT_SYSTEM_3DHWBUFFERMAX);
  315.     pCheck->EnableWindow(TRUE);
  316.     pCheck = (CButton*) GetDlgItem(IDC_EDIT_SYSTEM_2DSWBUFFERMAX);
  317.     pCheck->EnableWindow(TRUE);
  318.     pCheck = (CButton*) GetDlgItem(IDC_EDIT_SYSTEM_3DSWBUFFERMAX);
  319.     pCheck->EnableWindow(TRUE);
  320.     pCheck = (CButton*) GetDlgItem(IDC_EDIT_SYSTEM_SEGMENTMAX);
  321.     pCheck->EnableWindow(TRUE);
  322.  
  323.  
  324. }
  325.  
  326.  
  327. void CSystem::OnResumeall() 
  328. {
  329.     AudioMgr()->ResumeAll();
  330. }
  331.  
  332. void CSystem::OnPauseall() 
  333. {
  334.     AudioMgr()->PauseAll();
  335. }
  336.  
  337. void CSystem::OnStopall() 
  338. {
  339.     AudioMgr()->StopAll();
  340. }
  341.  
  342. void CSystem::UpdateStats()
  343. {
  344.     // Update stats and volume
  345.     AudioMgrStats stats;
  346.     AudioMgr()->GetStats(stats);
  347.  
  348.     CString sVal;
  349.     sVal.Format("%d", stats.m_n2DSoundsLoaded);
  350.     m_2DLoaded.SetWindowText(sVal);
  351.     sVal.Format("%d", stats.m_n3DSoundsLoaded);
  352.     m_3DLoaded.SetWindowText(sVal);
  353.     sVal.Format("%d", stats.m_nSegmentsLoaded);
  354.     m_SegmentsLoaded.SetWindowText(sVal);
  355.     sVal.Format("%s", (stats.m_bForce2DSoftware) ? "true" : "false");
  356.     m_Force2DSoftware.SetWindowText(sVal);
  357.     sVal.Format("%s", (stats.m_bForce3DSoftware) ? "true" : "false");
  358.     m_Force3DSoftware.SetWindowText(sVal);
  359.  
  360.     float fVol;
  361.     if(AudioMgr()->GetSoundVolume(fVol))
  362.         m_SoundVolume.SetPos(fVol*10000.0f);
  363.     if(AudioMgr()->GetMusicVolume(fVol))
  364.         m_MusicVolume.SetPos(fVol*10000.0f);
  365. }
  366.  
  367. // Handles the automatic fade demonstration code
  368. void CSystem::OnTimer(UINT nIDEvent) 
  369. {
  370.     if(nIDEvent == STATS_TIMER)
  371.     {
  372.         UpdateStats();
  373.     }
  374.     
  375.     CPropertyPage::OnTimer(nIDEvent);
  376. }
  377.  
  378. // Intercepts and handles volume control messages
  379. void CSystem::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
  380. {
  381.     if(pScrollBar->IsKindOf(RUNTIME_CLASS(CSliderCtrl)))
  382.     {
  383.         CSliderCtrl* pSlider = reinterpret_cast<CSliderCtrl*>(pScrollBar);
  384.         if(!pSlider)
  385.             return;
  386.         if(pSlider == &m_SoundVolume)
  387.             AudioMgr()->SetSoundVolume(float(pSlider->GetPos()) / 10000.0f);
  388.         else if(pSlider == &m_MusicVolume)
  389.             AudioMgr()->SetMusicVolume(float(pSlider->GetPos()) / 10000.0f);
  390.     }
  391.     CPropertyPage::OnHScroll(nSBCode, nPos, pScrollBar);
  392. }
  393.  
  394.  
  395. void CSystem::OnDestroy() 
  396. {
  397.     CPropertyPage::OnDestroy();
  398.     
  399.     if(m_nSoundVolumeUpdateTimer)
  400.         KillTimer(m_nSoundVolumeUpdateTimer);
  401.     if(m_nMusicVolumeUpdateTimer)
  402.         KillTimer(m_nMusicVolumeUpdateTimer);
  403.  
  404.     OnTerm();
  405.  
  406. }
  407.