1 #include "stdafx.h"
2 #ifndef NO_XAUDIO2
3 #include "VBA.h"
4 
5 #include "XAudio2_Config.h"
6 
7 #include <xaudio2.h>
8 
9 
10 // XAudio2_Config dialog
11 
IMPLEMENT_DYNAMIC(XAudio2_Config,CDialog)12 IMPLEMENT_DYNAMIC(XAudio2_Config, CDialog)
13 
14 XAudio2_Config::XAudio2_Config(CWnd* pParent /*=NULL*/)
15 	: CDialog(XAudio2_Config::IDD, pParent)
16 	, m_selected_device_index(0)
17 	, m_enable_upmixing(false)
18 	, m_buffer_count(0)
19 {
20 }
21 
~XAudio2_Config()22 XAudio2_Config::~XAudio2_Config()
23 {
24 }
25 
DoDataExchange(CDataExchange * pDX)26 void XAudio2_Config::DoDataExchange(CDataExchange* pDX)
27 {
28 	CDialog::DoDataExchange(pDX);
29 	DDX_Control(pDX, IDC_COMBO_DEV, m_combo_dev);
30 	DDX_Control(pDX, IDC_SLIDER_BUFFER, m_slider_buffer);
31 	DDX_Control(pDX, IDC_INFO_BUFFER, m_info_buffer);
32 	DDX_Control(pDX, IDC_CHECK_UPMIX, m_check_upmix);
33 
34 	if( pDX->m_bSaveAndValidate == TRUE ) {
35 		if( CB_ERR != m_combo_dev.GetCurSel() ) {
36 			if( CB_ERR != m_combo_dev.GetItemData( m_combo_dev.GetCurSel() ) ) {
37 				m_selected_device_index = m_combo_dev.GetItemData( m_combo_dev.GetCurSel() );
38 			}
39 		}
40 
41 		m_enable_upmixing = ( m_check_upmix.GetCheck() == BST_CHECKED );
42 
43 		m_buffer_count = (UINT32)m_slider_buffer.GetPos();
44 	} else {
45 		m_check_upmix.SetCheck( m_enable_upmixing ? BST_CHECKED : BST_UNCHECKED );
46 	}
47 }
48 
49 
BEGIN_MESSAGE_MAP(XAudio2_Config,CDialog)50 BEGIN_MESSAGE_MAP(XAudio2_Config, CDialog)
51 	ON_WM_HSCROLL()
52 END_MESSAGE_MAP()
53 
54 
55 // XAudio2_Config message handlers
56 
57 BOOL XAudio2_Config::OnInitDialog()
58 {
59 	CDialog::OnInitDialog();
60 
61 	m_combo_dev.ResetContent();
62 
63 	m_slider_buffer.SetRange( 2, 10, FALSE );
64 	m_slider_buffer.SetTicFreq( 1 );
65 	m_slider_buffer.SetPos( (int)m_buffer_count );
66 
67 	CString info;
68 	int pos = m_slider_buffer.GetPos();
69 	info.Format( _T("%i frames = %.2f ms"), pos, (float)pos / 60.0f * 1000.0f );
70 	m_info_buffer.SetWindowText( info );
71 
72 	HRESULT hr;
73 	IXAudio2 *xa = NULL;
74 	UINT32 flags = 0;
75 #ifdef _DEBUG
76 	flags = XAUDIO2_DEBUG_ENGINE;
77 #endif
78 
79 	hr = XAudio2Create( &xa, flags );
80 	if( hr != S_OK ) {
81 		systemMessage( IDS_XAUDIO2_FAILURE, NULL );
82 	} else {
83 		UINT32 dev_count = 0;
84 		hr = xa->GetDeviceCount( &dev_count );
85 		if( hr != S_OK ) {
86 			systemMessage( IDS_XAUDIO2_CANNOT_ENUMERATE_DEVICES, NULL );
87 		} else {
88 			XAUDIO2_DEVICE_DETAILS dd;
89 			for( UINT32 i = 0; i < dev_count; i++ ) {
90 				hr = xa->GetDeviceDetails( i, &dd );
91 				if( hr != S_OK ) {
92 					continue;
93 				} else {
94 #ifdef _UNICODE
95 					int id = m_combo_dev.AddString( dd.DisplayName );
96 #else
97 					CHAR temp[256];
98 					ZeroMemory( temp, sizeof( temp ) );
99 					WideCharToMultiByte(
100 						CP_ACP,
101 						WC_NO_BEST_FIT_CHARS,
102 						dd.DisplayName,
103 						-1,
104 						temp,
105 						sizeof( temp ) - 1,
106 						NULL,
107 						NULL );
108 
109 					int id = m_combo_dev.AddString( temp );
110 #endif
111 					if( id < 0 ) {
112 						systemMessage( IDS_XAUDIO2_CANNOT_ENUMERATE_DEVICES, NULL );
113 						break;
114 					} else {
115 						m_combo_dev.SetItemData( id, i );
116 					}
117 				}
118 			}
119 
120 			// select the currently configured device {
121 			int count = m_combo_dev.GetCount();
122 			if( count > 0 ) {
123 				for( int i = 0; i < count; i++ ) {
124 					if( m_combo_dev.GetItemData( i ) == m_selected_device_index ) {
125 						m_combo_dev.SetCurSel( i );
126 						break;
127 					}
128 				}
129 			}
130 			// }
131 
132 		}
133 		xa->Release();
134 		xa = NULL;
135 	}
136 
137 	return TRUE;  // return TRUE unless you set the focus to a control
138 	// EXCEPTION: OCX Property Pages should return FALSE
139 }
140 
OnHScroll(UINT nSBCode,UINT nPos,CScrollBar * pScrollBar)141 void XAudio2_Config::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
142 {
143 	CString info;
144 	int pos = m_slider_buffer.GetPos();
145 	info.Format( _T("%i frames = %.2f ms"), pos, (float)pos / 60.0f * 1000.0f );
146 	m_info_buffer.SetWindowText( info );
147 
148 	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
149 }
150 
151 #endif
152