home *** CD-ROM | disk | FTP | other *** search
/ Game Audio Programming / GameAudioProgramming.iso / Game_Audio / AudioTest / Sound2D.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-07-15  |  7.2 KB  |  319 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 "Sound2D.h"
  15. #include "SoundOpen.h"
  16. #include "Audio.h"
  17.  
  18. #include "AudioTestDlg.h"
  19. #include "System.h"
  20. #include "Sound3D.h"
  21. #include "Segment.h"
  22.  
  23. #ifdef _DEBUG
  24. #define new DEBUG_NEW
  25. #undef THIS_FILE
  26. static char THIS_FILE[] = __FILE__;
  27. #endif
  28.  
  29. using namespace Audio;
  30.  
  31. const int CURSOR_TIMER = 5;
  32.  
  33. #define GET_CURRENT_SOUND() \
  34.     int iIndex = m_2DSounds.GetCurSel();\
  35.     if(iIndex == LB_ERR) return;\
  36.     ISound* pSound = static_cast<ISound*>(m_2DSounds.GetItemDataPtr(iIndex));\
  37.     if(!pSound) return;
  38.  
  39.  
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CSound2D property page
  42.  
  43. IMPLEMENT_DYNCREATE(CSound2D, CPropertyPage)
  44.  
  45. CSound2D::CSound2D() : CPropertyPage(CSound2D::IDD)
  46. {
  47.     //{{AFX_DATA_INIT(CSound2D)
  48.     //}}AFX_DATA_INIT
  49.     m_nCursorTimer = 0;
  50.     m_bInit = 0;
  51. }
  52.  
  53. CSound2D::~CSound2D()
  54. {
  55. }
  56.  
  57. void CSound2D::DoDataExchange(CDataExchange* pDX)
  58. {
  59.     CPropertyPage::DoDataExchange(pDX);
  60.     //{{AFX_DATA_MAP(CSound2D)
  61.     DDX_Control(pDX, IDC_SLIDER_SOUNDCURSOR, m_Cursor);
  62.     DDX_Control(pDX, IDC_SLIDER_SOUNDPITCH, m_Pitch);
  63.     DDX_Control(pDX, IDC_SLIDER_SOUNDPAN, m_Pan);
  64.     DDX_Control(pDX, IDC_SLIDER_SOUNDVOLUME, m_Volume);
  65.     DDX_Control(pDX, IDC_PAUSE2D, m_Pause);
  66.     DDX_Control(pDX, IDC_STOP2D, m_Stop);
  67.     DDX_Control(pDX, IDC_PLAY2D, m_Play);
  68.     DDX_Control(pDX, IDC_LIST_2DSOUNDS, m_2DSounds);
  69.     //}}AFX_DATA_MAP
  70. }
  71.  
  72.  
  73. BEGIN_MESSAGE_MAP(CSound2D, CPropertyPage)
  74.     //{{AFX_MSG_MAP(CSound2D)
  75.     ON_BN_CLICKED(IDC_LOAD2D, OnLoad2d)
  76.     ON_BN_CLICKED(IDC_UNLOAD2D, OnUnload2d)
  77.     ON_BN_CLICKED(IDC_PLAY2D, OnPlay)
  78.     ON_BN_CLICKED(IDC_PAUSE2D, OnPause)
  79.     ON_BN_CLICKED(IDC_STOP2D, OnStop)
  80.     ON_BN_CLICKED(IDC_2D_INITIALZE, On2dInitialze)
  81.     ON_WM_HSCROLL()
  82.     ON_LBN_SELCHANGE(IDC_LIST_2DSOUNDS, OnSelChangeList2dSounds)
  83.     ON_BN_CLICKED(IDC_2D_DELETE, On2dDelete)
  84.     ON_WM_TIMER()
  85.     ON_WM_DESTROY()
  86.     ON_LBN_DBLCLK(IDC_LIST_2DSOUNDS, OnDblclkList2dsounds)
  87.     //}}AFX_MSG_MAP
  88. END_MESSAGE_MAP()
  89.  
  90. /////////////////////////////////////////////////////////////////////////////
  91. // CSound2D message handlers
  92.  
  93.  
  94. BOOL CSound2D::OnInitDialog() 
  95. {
  96.     CPropertyPage::OnInitDialog();
  97.     
  98.     // Load the play button icons
  99.     m_hPlayIcon = LoadIcon(::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_PLAY));
  100.     m_hPauseIcon = LoadIcon(::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_PAUSE));
  101.     m_hStopIcon = LoadIcon(::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_STOP));
  102.     
  103.     // Set the master play control buttons
  104.     m_Play.SetIcon(m_hPlayIcon);
  105.     m_Pause.SetIcon(m_hPauseIcon);
  106.     m_Stop.SetIcon(m_hStopIcon);
  107.  
  108.     // Set up the volume sliders
  109.     m_Volume.SetRange(0, 10000);
  110.     m_Volume.SetPos(10000);
  111.     m_Volume.Invalidate();
  112.     m_Pan.SetRange(0, 20000);
  113.     m_Pan.SetPos(10000);
  114.     m_Pan.Invalidate();
  115.     m_Pitch.SetRange(0, 10000);
  116.     m_Pitch.SetPos(5000);
  117.     m_Pitch.Invalidate();
  118.  
  119.     m_nCursorTimer = SetTimer(CURSOR_TIMER, 100, NULL);
  120.  
  121.     m_bInit = true;
  122.  
  123.     return TRUE;  // return TRUE unless you set the focus to a control
  124.                   // EXCEPTION: OCX Property Pages should return FALSE
  125. }
  126.  
  127.  
  128. void CSound2D::On2dInitialze() 
  129. {
  130.     CSoundOpen dlg(TRUE);
  131.     if(dlg.DoModal() != IDOK)
  132.         return;
  133.  
  134.     char drive[_MAX_DRIVE];
  135.     char dir[_MAX_DIR];
  136.     char fname[_MAX_FNAME];
  137.     char ext[_MAX_EXT];
  138.  
  139.     POSITION pos = dlg.GetStartPosition();
  140.     CString sPathName;
  141.     while(pos)
  142.     {
  143.         sPathName = dlg.GetNextPathName(pos);
  144.         _splitpath(sPathName, drive, dir, fname, ext);
  145.         SoundInit init;
  146.         init.m_bLooping = dlg.m_bLooping;
  147.         init.m_bStreaming = dlg.m_bStreaming;
  148.         init.m_bMusic = dlg.m_bMusic;
  149.         init.m_sFileName = sPathName;
  150.  
  151.         ISound* pSound;
  152.         if(!AudioMgr()->CreateSound(pSound))
  153.             return;
  154.         if(!pSound->Init(init))
  155.         {
  156.             AfxMessageBox("Error initializing audio file");
  157.             return;
  158.         }
  159.         int iIndex = m_2DSounds.AddString(CString(fname) + CString(ext));
  160.         if(iIndex != LB_ERR)
  161.             m_2DSounds.SetItemDataPtr(iIndex, pSound);
  162.     }
  163. }
  164.  
  165.  
  166. void CSound2D::OnLoad2d() 
  167. {
  168.     GET_CURRENT_SOUND();
  169.     pSound->Load();
  170.     m_pTestDlg->m_pSystem->UpdateStats();
  171.     UpdateControls();
  172. }
  173.  
  174. void CSound2D::OnUnload2d() 
  175. {
  176.     GET_CURRENT_SOUND();
  177.     pSound->Unload();
  178.     m_pTestDlg->m_pSystem->UpdateStats();
  179.     UpdateControls();
  180. }
  181.  
  182. void CSound2D::OnPlay() 
  183. {
  184.     GET_CURRENT_SOUND();
  185.     pSound->Play();
  186.     m_pTestDlg->m_pSystem->UpdateStats();
  187.     UpdateControls();
  188. }
  189.  
  190. void CSound2D::OnPause() 
  191. {
  192.     GET_CURRENT_SOUND();
  193.     pSound->Pause();
  194. }
  195.  
  196. void CSound2D::OnStop() 
  197. {
  198.     GET_CURRENT_SOUND();
  199.     pSound->Stop();
  200. }
  201.  
  202. void CSound2D::ClearList()
  203. {
  204.     if(m_bInit)
  205.     {
  206.         ISound* pSound;
  207.         int iSize = m_2DSounds.GetCount();
  208.         for(int i = 0; i < iSize; i++)
  209.         {
  210.             pSound = static_cast<ISound*>(m_2DSounds.GetItemDataPtr(0));
  211.             pSound->Destroy();
  212.             m_2DSounds.DeleteString(0);
  213.         }
  214.         m_2DSounds.ResetContent();
  215.     }
  216. }
  217.  
  218. void CSound2D::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
  219. {
  220.     if(pScrollBar->IsKindOf(RUNTIME_CLASS(CSliderCtrl)))
  221.     {
  222.         CSliderCtrl* pSlider = reinterpret_cast<CSliderCtrl*>(pScrollBar);
  223.         if(!pSlider)
  224.             return;
  225.         GET_CURRENT_SOUND();
  226.         if(pSlider == &m_Volume)
  227.         {
  228.             pSound->SetVolume(static_cast<float>(pSlider->GetPos()) / 10000.0f);
  229.         }
  230.         else if(pSlider == &m_Pan)
  231.         {
  232.             pSound->SetPan(static_cast<float>((pSlider->GetPos() - 10000) / 10000.0f));
  233.         }
  234.         else if(pSlider == &m_Pitch)
  235.         {
  236.             pSound->SetPitch((float(pSlider->GetPos() + 5000.0f) / 10000.0f) );
  237.         }
  238.         else if(pSlider == &m_Cursor)
  239.         {
  240.             pSound->SetReadCursor(nPos);
  241.         }
  242.     }
  243.     CPropertyPage::OnHScroll(nSBCode, nPos, pScrollBar);
  244. }
  245.  
  246. void CSound2D::OnSelChangeList2dSounds() 
  247. {
  248.     UpdateControls();
  249. }
  250.  
  251. void CSound2D::On2dDelete() 
  252. {
  253.     GET_CURRENT_SOUND();
  254.     pSound->Destroy();
  255.     int iSize = m_2DSounds.DeleteString(iIndex);
  256.     if(!iSize)
  257.         return;
  258.     if(iIndex > (iSize - 1))
  259.         iIndex = iSize - 1;
  260.     m_2DSounds.SetCurSel(iIndex);
  261.  
  262. }
  263.  
  264. void CSound2D::UpdateControls()
  265. {
  266.     GET_CURRENT_SOUND();
  267.     float fVal;
  268.     uint32 nVal;
  269.     pSound->GetVolume(fVal);
  270.     m_Volume.SetPos(static_cast<int>(fVal * 10000.0f));
  271.     pSound->GetPan(fVal);
  272.     m_Pan.SetPos(static_cast<int>((fVal * 10000.0f) + 10000.0f));
  273.     pSound->GetPitch(fVal);
  274.     m_Pitch.SetPos(static_cast<int>((fVal * 10000.0f) - 5000.0f));
  275.     pSound->GetSourceSize(nVal);
  276.     m_Cursor.SetRange(0, nVal);
  277. }
  278.  
  279. void CSound2D::OnTimer(UINT nIDEvent) 
  280. {
  281.     if(nIDEvent == CURSOR_TIMER)
  282.     {
  283.         GET_CURRENT_SOUND();
  284.  
  285.         unsigned long nVal;
  286.         if(pSound->IsPlaying())
  287.         {
  288.             pSound->GetReadCursor(nVal);
  289.             m_Cursor.SetPos(nVal);
  290.             m_Cursor.Invalidate();
  291.         }
  292.         else
  293.         {
  294.             pSound->GetReadCursor(nVal);
  295.             if(m_Cursor.GetPos() != nVal)
  296.             {
  297.                 m_Cursor.SetPos(nVal);
  298.                 m_Cursor.Invalidate();
  299.             }
  300.         }
  301.     }
  302.  
  303.     CPropertyPage::OnTimer(nIDEvent);
  304. }
  305.  
  306. void CSound2D::OnDestroy() 
  307. {
  308.     CPropertyPage::OnDestroy();
  309.     
  310.     if(m_nCursorTimer)
  311.         KillTimer(m_nCursorTimer);
  312.     ClearList();
  313. }
  314.  
  315. void CSound2D::OnDblclkList2dsounds() 
  316. {
  317.     OnPlay();
  318. }
  319.