-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoundEffect.cpp
More file actions
79 lines (66 loc) · 1.96 KB
/
SoundEffect.cpp
File metadata and controls
79 lines (66 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
//// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
//// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//// PARTICULAR PURPOSE.
////
//// Copyright (c) Microsoft Corporation. All rights reserved
#include "pch.h"
#include "SoundEffect.h"
#include "DirectXSample.h"
SoundEffect::SoundEffect():
m_audioAvailable(false)
{
}
//----------------------------------------------------------------------
void SoundEffect::Initialize(
_In_ IXAudio2 *masteringEngine,
_In_ WAVEFORMATEX *sourceFormat,
_In_ Platform::Array<byte>^ soundData)
{
m_soundData = soundData;
if (masteringEngine == nullptr)
{
// Audio is not available so just return.
m_audioAvailable = false;
return;
}
// Create a source voice for this sound effect.
ThrowIfFailed(
masteringEngine->CreateSourceVoice(
&m_sourceVoice,
sourceFormat
)
);
m_audioAvailable = true;
}
//----------------------------------------------------------------------
void SoundEffect::PlaySound(_In_ float volume)
{
XAUDIO2_BUFFER buffer = {0};
if (!m_audioAvailable)
{
// Audio is not available so just return.
return;
}
// Interrupt sound effect if it is currently playing.
ThrowIfFailed(
m_sourceVoice->Stop()
);
ThrowIfFailed(
m_sourceVoice->FlushSourceBuffers()
);
// Queue the memory buffer for playback and start the voice.
buffer.AudioBytes = m_soundData->Length;
buffer.pAudioData = m_soundData->Data;
buffer.Flags = XAUDIO2_END_OF_STREAM;
ThrowIfFailed(
m_sourceVoice->SetVolume(volume)
);
ThrowIfFailed(
m_sourceVoice->SubmitSourceBuffer(&buffer)
);
ThrowIfFailed(
m_sourceVoice->Start()
);
}
//----------------------------------------------------------------------