-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRawLoad.cpp
100 lines (75 loc) · 2.92 KB
/
RawLoad.cpp
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
Copyright 2011 Matt DeVore
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "StdAfx.h"
#include "Logger.h"
using std::endl;
// module that loads raw sound data into sound buffers
static IDirectSoundBuffer *FillSoundBuffer(
IDirectSoundBuffer *target, const void *data, DWORD bytes) {
BYTE *audio_ptr_1 = 0, *audio_ptr_2 = 0;
DWORD audio_length_1 = 0, audio_length_2 = 0;
const BYTE *snd_buffer = (const BYTE *)data;
if (FAILED((*target).Lock(0, bytes,
(void **)&audio_ptr_1, &audio_length_1,
(void **)&audio_ptr_2, &audio_length_2,
DSBLOCK_FROMWRITECURSOR))) {
target->Release();
logger << "Could not lock sound buffer" << endl;
return 0;
}
memcpy(audio_ptr_1, snd_buffer, audio_length_1);
memcpy(audio_ptr_2, snd_buffer + audio_length_1, audio_length_2);
target->Unlock(audio_ptr_1, audio_length_1, audio_ptr_2, audio_length_2);
return target;
}
// this function returns zero on success
static AutoComPtr<IDirectSoundBuffer> CreateSB
(IDirectSound *ds, DWORD desc_flags, DWORD frequency,
DWORD bits_per_sample, DWORD channels, DWORD buffer_size) {
AutoComPtr<IDirectSoundBuffer> new_buff;
DSBUFFERDESC dsbd;
WAVEFORMATEX wfm;
if (!ds) {
logger << "Direct sound is not available; could not create sound buffer" <<
endl;
return new_buff;
}
assert(channels && bits_per_sample && frequency);
// copy wave format structure
wfm.cbSize = 0;
wfm.nAvgBytesPerSec
= DWORD(wfm.nBlockAlign = WORD(channels * bits_per_sample / 8));
wfm.nAvgBytesPerSec *= frequency;
wfm.nChannels = (WORD)channels;
wfm.nSamplesPerSec = frequency;
wfm.wBitsPerSample = (WORD)bits_per_sample;
wfm.wFormatTag=WAVE_FORMAT_PCM;
InitDXStruct(&dsbd);
dsbd.dwBufferBytes = buffer_size;
dsbd.dwFlags = desc_flags;
dsbd.lpwfxFormat = &wfm;
LogResult("Create sound buffer",
ds->CreateSoundBuffer(&dsbd, new_buff.GetPtrToPtr(), 0));
return new_buff;
}
AutoComPtr<IDirectSoundBuffer> CreateSBFromRawData
(IDirectSound *ds, void *data, DWORD size, DWORD desc_flags,
DWORD frequency, DWORD bits_per_sample, DWORD channels) {
AutoComPtr<IDirectSoundBuffer> new_buff = CreateSB(ds, desc_flags,
frequency, bits_per_sample,
channels, size);
if (new_buff) {
FillSoundBuffer(new_buff.Get(), data, size);
}
return new_buff;
}