home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************\
- Copyright (C) James Boer, 2002.
- All rights reserved worldwide.
-
- This software is provided "as is" without express or implied
- warranties. You may freely copy and compile this source into
- applications you distribute provided that the copyright text
- below is included in the resulting source code, for example:
- "Portions Copyright (C) James Boer, 2002"
- \***********************************************************/
-
- #include "stdafx.h"
- #include "AudioTest.h"
- #include "Sound2D.h"
- #include "SoundOpen.h"
- #include "Audio.h"
-
- #include "AudioTestDlg.h"
- #include "System.h"
- #include "Sound3D.h"
- #include "Segment.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- using namespace Audio;
-
- const int CURSOR_TIMER = 5;
-
- #define GET_CURRENT_SOUND() \
- int iIndex = m_2DSounds.GetCurSel();\
- if(iIndex == LB_ERR) return;\
- ISound* pSound = static_cast<ISound*>(m_2DSounds.GetItemDataPtr(iIndex));\
- if(!pSound) return;
-
-
- /////////////////////////////////////////////////////////////////////////////
- // CSound2D property page
-
- IMPLEMENT_DYNCREATE(CSound2D, CPropertyPage)
-
- CSound2D::CSound2D() : CPropertyPage(CSound2D::IDD)
- {
- //{{AFX_DATA_INIT(CSound2D)
- //}}AFX_DATA_INIT
- m_nCursorTimer = 0;
- m_bInit = 0;
- }
-
- CSound2D::~CSound2D()
- {
- }
-
- void CSound2D::DoDataExchange(CDataExchange* pDX)
- {
- CPropertyPage::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CSound2D)
- DDX_Control(pDX, IDC_SLIDER_SOUNDCURSOR, m_Cursor);
- DDX_Control(pDX, IDC_SLIDER_SOUNDPITCH, m_Pitch);
- DDX_Control(pDX, IDC_SLIDER_SOUNDPAN, m_Pan);
- DDX_Control(pDX, IDC_SLIDER_SOUNDVOLUME, m_Volume);
- DDX_Control(pDX, IDC_PAUSE2D, m_Pause);
- DDX_Control(pDX, IDC_STOP2D, m_Stop);
- DDX_Control(pDX, IDC_PLAY2D, m_Play);
- DDX_Control(pDX, IDC_LIST_2DSOUNDS, m_2DSounds);
- //}}AFX_DATA_MAP
- }
-
-
- BEGIN_MESSAGE_MAP(CSound2D, CPropertyPage)
- //{{AFX_MSG_MAP(CSound2D)
- ON_BN_CLICKED(IDC_LOAD2D, OnLoad2d)
- ON_BN_CLICKED(IDC_UNLOAD2D, OnUnload2d)
- ON_BN_CLICKED(IDC_PLAY2D, OnPlay)
- ON_BN_CLICKED(IDC_PAUSE2D, OnPause)
- ON_BN_CLICKED(IDC_STOP2D, OnStop)
- ON_BN_CLICKED(IDC_2D_INITIALZE, On2dInitialze)
- ON_WM_HSCROLL()
- ON_LBN_SELCHANGE(IDC_LIST_2DSOUNDS, OnSelChangeList2dSounds)
- ON_BN_CLICKED(IDC_2D_DELETE, On2dDelete)
- ON_WM_TIMER()
- ON_WM_DESTROY()
- ON_LBN_DBLCLK(IDC_LIST_2DSOUNDS, OnDblclkList2dsounds)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CSound2D message handlers
-
-
- BOOL CSound2D::OnInitDialog()
- {
- CPropertyPage::OnInitDialog();
-
- // Load the play button icons
- m_hPlayIcon = LoadIcon(::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_PLAY));
- m_hPauseIcon = LoadIcon(::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_PAUSE));
- m_hStopIcon = LoadIcon(::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_STOP));
-
- // Set the master play control buttons
- m_Play.SetIcon(m_hPlayIcon);
- m_Pause.SetIcon(m_hPauseIcon);
- m_Stop.SetIcon(m_hStopIcon);
-
- // Set up the volume sliders
- m_Volume.SetRange(0, 10000);
- m_Volume.SetPos(10000);
- m_Volume.Invalidate();
- m_Pan.SetRange(0, 20000);
- m_Pan.SetPos(10000);
- m_Pan.Invalidate();
- m_Pitch.SetRange(0, 10000);
- m_Pitch.SetPos(5000);
- m_Pitch.Invalidate();
-
- m_nCursorTimer = SetTimer(CURSOR_TIMER, 100, NULL);
-
- m_bInit = true;
-
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
-
-
- void CSound2D::On2dInitialze()
- {
- CSoundOpen dlg(TRUE);
- if(dlg.DoModal() != IDOK)
- return;
-
- char drive[_MAX_DRIVE];
- char dir[_MAX_DIR];
- char fname[_MAX_FNAME];
- char ext[_MAX_EXT];
-
- POSITION pos = dlg.GetStartPosition();
- CString sPathName;
- while(pos)
- {
- sPathName = dlg.GetNextPathName(pos);
- _splitpath(sPathName, drive, dir, fname, ext);
- SoundInit init;
- init.m_bLooping = dlg.m_bLooping;
- init.m_bStreaming = dlg.m_bStreaming;
- init.m_bMusic = dlg.m_bMusic;
- init.m_sFileName = sPathName;
-
- ISound* pSound;
- if(!AudioMgr()->CreateSound(pSound))
- return;
- if(!pSound->Init(init))
- {
- AfxMessageBox("Error initializing audio file");
- return;
- }
- int iIndex = m_2DSounds.AddString(CString(fname) + CString(ext));
- if(iIndex != LB_ERR)
- m_2DSounds.SetItemDataPtr(iIndex, pSound);
- }
- }
-
-
- void CSound2D::OnLoad2d()
- {
- GET_CURRENT_SOUND();
- pSound->Load();
- m_pTestDlg->m_pSystem->UpdateStats();
- UpdateControls();
- }
-
- void CSound2D::OnUnload2d()
- {
- GET_CURRENT_SOUND();
- pSound->Unload();
- m_pTestDlg->m_pSystem->UpdateStats();
- UpdateControls();
- }
-
- void CSound2D::OnPlay()
- {
- GET_CURRENT_SOUND();
- pSound->Play();
- m_pTestDlg->m_pSystem->UpdateStats();
- UpdateControls();
- }
-
- void CSound2D::OnPause()
- {
- GET_CURRENT_SOUND();
- pSound->Pause();
- }
-
- void CSound2D::OnStop()
- {
- GET_CURRENT_SOUND();
- pSound->Stop();
- }
-
- void CSound2D::ClearList()
- {
- if(m_bInit)
- {
- ISound* pSound;
- int iSize = m_2DSounds.GetCount();
- for(int i = 0; i < iSize; i++)
- {
- pSound = static_cast<ISound*>(m_2DSounds.GetItemDataPtr(0));
- pSound->Destroy();
- m_2DSounds.DeleteString(0);
- }
- m_2DSounds.ResetContent();
- }
- }
-
- void CSound2D::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
- {
- if(pScrollBar->IsKindOf(RUNTIME_CLASS(CSliderCtrl)))
- {
- CSliderCtrl* pSlider = reinterpret_cast<CSliderCtrl*>(pScrollBar);
- if(!pSlider)
- return;
- GET_CURRENT_SOUND();
- if(pSlider == &m_Volume)
- {
- pSound->SetVolume(static_cast<float>(pSlider->GetPos()) / 10000.0f);
- }
- else if(pSlider == &m_Pan)
- {
- pSound->SetPan(static_cast<float>((pSlider->GetPos() - 10000) / 10000.0f));
- }
- else if(pSlider == &m_Pitch)
- {
- pSound->SetPitch((float(pSlider->GetPos() + 5000.0f) / 10000.0f) );
- }
- else if(pSlider == &m_Cursor)
- {
- pSound->SetReadCursor(nPos);
- }
- }
- CPropertyPage::OnHScroll(nSBCode, nPos, pScrollBar);
- }
-
- void CSound2D::OnSelChangeList2dSounds()
- {
- UpdateControls();
- }
-
- void CSound2D::On2dDelete()
- {
- GET_CURRENT_SOUND();
- pSound->Destroy();
- int iSize = m_2DSounds.DeleteString(iIndex);
- if(!iSize)
- return;
- if(iIndex > (iSize - 1))
- iIndex = iSize - 1;
- m_2DSounds.SetCurSel(iIndex);
-
- }
-
- void CSound2D::UpdateControls()
- {
- GET_CURRENT_SOUND();
- float fVal;
- uint32 nVal;
- pSound->GetVolume(fVal);
- m_Volume.SetPos(static_cast<int>(fVal * 10000.0f));
- pSound->GetPan(fVal);
- m_Pan.SetPos(static_cast<int>((fVal * 10000.0f) + 10000.0f));
- pSound->GetPitch(fVal);
- m_Pitch.SetPos(static_cast<int>((fVal * 10000.0f) - 5000.0f));
- pSound->GetSourceSize(nVal);
- m_Cursor.SetRange(0, nVal);
- }
-
- void CSound2D::OnTimer(UINT nIDEvent)
- {
- if(nIDEvent == CURSOR_TIMER)
- {
- GET_CURRENT_SOUND();
-
- unsigned long nVal;
- if(pSound->IsPlaying())
- {
- pSound->GetReadCursor(nVal);
- m_Cursor.SetPos(nVal);
- m_Cursor.Invalidate();
- }
- else
- {
- pSound->GetReadCursor(nVal);
- if(m_Cursor.GetPos() != nVal)
- {
- m_Cursor.SetPos(nVal);
- m_Cursor.Invalidate();
- }
- }
- }
-
- CPropertyPage::OnTimer(nIDEvent);
- }
-
- void CSound2D::OnDestroy()
- {
- CPropertyPage::OnDestroy();
-
- if(m_nCursorTimer)
- KillTimer(m_nCursorTimer);
- ClearList();
- }
-
- void CSound2D::OnDblclkList2dsounds()
- {
- OnPlay();
- }
-