Skip to content

Commit 4a8b6fb

Browse files
committed
xrSound: replace WAVEFORMATEX with our own structure
1 parent 0c2542f commit 4a8b6fb

8 files changed

+53
-59
lines changed

src/xrSound/SoundRender_Emitter_FSM.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
XRSOUND_API extern float psSoundCull;
1010

11-
inline u32 calc_cursor(const float& fTimeStarted, float& fTime, const float& fTimeTotal, const float& fFreq, const WAVEFORMATEX& wfx) //--#SM+#--
11+
inline u32 calc_cursor(const float& fTimeStarted, float& fTime, const float& fTimeTotal, const float& fFreq, const SoundSourceInfo& info) //--#SM+#--
1212
{
1313
if (fTime < fTimeStarted)
1414
fTime = fTimeStarted; // Андрюха посоветовал, ассерт что ниже вылетел из за паузы как то хитро
@@ -17,8 +17,8 @@ inline u32 calc_cursor(const float& fTimeStarted, float& fTime, const float& fTi
1717
{
1818
fTime -= fTimeTotal / fFreq;
1919
}
20-
u32 curr_sample_num = iFloor((fTime - fTimeStarted) * fFreq * wfx.nSamplesPerSec);
21-
return curr_sample_num * (wfx.wBitsPerSample / 8) * wfx.nChannels;
20+
const u32 curr_sample_num = iFloor((fTime - fTimeStarted) * fFreq * info.samplesPerSec);
21+
return curr_sample_num * (info.bitsPerSample / 8) * info.channels;
2222
}
2323

2424
void CSoundRender_Emitter::update(float fTime, float dt)
@@ -151,7 +151,7 @@ void CSoundRender_Emitter::update(float fTime, float dt)
151151
}
152152
else
153153
{
154-
const u32 ptr = calc_cursor(fTimeStarted, fTime, get_length_sec(), p_source.freq, source()->m_wformat); //--#SM+#--
154+
const u32 ptr = calc_cursor(fTimeStarted, fTime, get_length_sec(), p_source.freq, source()->m_info); //--#SM+#--
155155
set_cursor(ptr);
156156

157157
if (update_culling(dt))
@@ -162,7 +162,7 @@ void CSoundRender_Emitter::update(float fTime, float dt)
162162
u32 ptr = calc_cursor( fTimeStarted,
163163
fTime,
164164
get_length_sec(),
165-
source()->m_wformat);
165+
source()->m_info);
166166
set_cursor (ptr);
167167
*/
168168
SoundRender->i_start(this);
@@ -204,7 +204,7 @@ void CSoundRender_Emitter::update(float fTime, float dt)
204204
{
205205
// switch to: PLAY
206206
m_current_state = stPlayingLooped; // switch state
207-
const u32 ptr = calc_cursor(fTimeStarted, fTime, get_length_sec(), p_source.freq, source()->m_wformat); //--#SM+#--
207+
const u32 ptr = calc_cursor(fTimeStarted, fTime, get_length_sec(), p_source.freq, source()->m_info); //--#SM+#--
208208
set_cursor(ptr);
209209

210210
SoundRender->i_start(this);
@@ -253,7 +253,7 @@ void CSoundRender_Emitter::update(float fTime, float dt)
253253
fTimeToStop = fTime + fRemainingTime;
254254
}
255255

256-
const u32 ptr = calc_cursor(fTimeStarted, fTime, fLength, p_source.freq, source()->m_wformat);
256+
const u32 ptr = calc_cursor(fTimeStarted, fTime, fLength, p_source.freq, source()->m_info);
257257
set_cursor(ptr);
258258

259259
fTimeToRewind = 0.0f;

src/xrSound/SoundRender_Source.cpp

+16-15
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ void CSoundRender_Source::decompress(void* dest, u32 byte_offset, u32 size)
5757
std::lock_guard guard{ read_lock };
5858

5959
// seek
60-
const auto sample_offset = ogg_int64_t(byte_offset / m_wformat.nBlockAlign);
60+
const auto sample_offset = ogg_int64_t(byte_offset / m_info.blockAlign);
6161
const u32 cur_pos = u32(ov_pcm_tell(&ovf));
6262
if (cur_pos != sample_offset)
6363
ov_pcm_seek(&ovf, sample_offset);
6464

6565
// decompress
66-
if (m_wformat.wFormatTag == WAVE_FORMAT_IEEE_FLOAT)
66+
if (m_info.format == SoundFormat::Float32)
6767
i_decompress(static_cast<float*>(dest), size);
6868
else
6969
i_decompress(static_cast<char*>(dest), size);
@@ -85,7 +85,7 @@ void CSoundRender_Source::i_decompress(char* _dest, u32 size)
8585

8686
void CSoundRender_Source::i_decompress(float* _dest, u32 size)
8787
{
88-
s32 left = s32(size / m_wformat.nBlockAlign);
88+
s32 left = s32(size / m_info.blockAlign);
8989
while (left)
9090
{
9191
float** pcm;
@@ -98,7 +98,7 @@ void CSoundRender_Source::i_decompress(float* _dest, u32 size)
9898
samples = left;
9999

100100
for (long j = 0; j < samples; j++)
101-
for (long i = 0; i < m_wformat.nChannels; i++)
101+
for (long i = 0; i < m_info.channels; i++)
102102
*_dest++ = pcm[i][j];
103103

104104
left -= samples;
@@ -184,28 +184,29 @@ bool CSoundRender_Source::LoadWave(pcstr pName, bool crashOnError)
184184
return false;
185185
});
186186

187-
ZeroMemory(&m_wformat, sizeof(WAVEFORMATEX));
187+
m_info = {};
188188

189-
m_wformat.nSamplesPerSec = ovi->rate;
190-
m_wformat.nChannels = u16(ovi->channels);
189+
m_info.samplesPerSec = ovi->rate;
190+
m_info.channels = u16(ovi->channels);
191191

192192
if (SoundRender->supports_float_pcm)
193193
{
194-
m_wformat.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
195-
m_wformat.wBitsPerSample = 32;
194+
m_info.format = SoundFormat::Float32;
195+
m_info.bitsPerSample = 32;
196196
}
197197
else
198198
{
199-
m_wformat.wFormatTag = WAVE_FORMAT_PCM;
200-
m_wformat.wBitsPerSample = 16;
199+
m_info.format = SoundFormat::PCM;
200+
m_info.bitsPerSample = 16;
201201
}
202202

203-
m_wformat.nBlockAlign = m_wformat.wBitsPerSample / 8 * m_wformat.nChannels;
204-
m_wformat.nAvgBytesPerSec = m_wformat.nSamplesPerSec * m_wformat.nBlockAlign;
203+
m_info.blockAlign = m_info.bitsPerSample / 8 * m_info.channels;
204+
m_info.avgBytesPerSec = m_info.samplesPerSec * m_info.blockAlign;
205+
m_info.bytesPerBuffer = sdef_target_block * m_info.avgBytesPerSec / 1000;
205206

206207
const s64 pcm_total = ov_pcm_total(&ovf, -1);
207-
dwBytesTotal = u32(pcm_total * m_wformat.nBlockAlign);
208-
fTimeTotal = dwBytesTotal / float(m_wformat.nAvgBytesPerSec);
208+
dwBytesTotal = u32(pcm_total * m_info.blockAlign);
209+
fTimeTotal = dwBytesTotal / float(m_info.avgBytesPerSec);
209210

210211
const vorbis_comment* ovm = ov_comment(&ovf, -1);
211212
if (ovm->comments)

src/xrSound/SoundRender_Source.h

+20-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@
44

55
#include <vorbis/vorbisfile.h>
66

7+
enum class SoundFormat
8+
{
9+
Unknown,
10+
PCM,
11+
Float32,
12+
};
13+
14+
struct SoundSourceInfo
15+
{
16+
SoundFormat format{};
17+
u16 channels{}; // number of channels (i.e. mono, stereo...)
18+
u32 samplesPerSec{}; // sample rate
19+
u32 avgBytesPerSec{}; // for buffer estimation
20+
u16 blockAlign{}; // block size of data
21+
u16 bitsPerSample{}; // number of bits per sample of mono data
22+
u32 bytesPerBuffer{}; // target buffer size
23+
};
24+
725
class XRSOUND_API CSoundRender_Source final : public CSound_source
826
{
927
public:
@@ -18,7 +36,7 @@ class XRSOUND_API CSoundRender_Source final : public CSound_source
1836
float fTimeTotal;
1937
u32 dwBytesTotal;
2038

21-
WAVEFORMATEX m_wformat;
39+
SoundSourceInfo m_info{};
2240

2341
float m_fBaseVolume;
2442
float m_fMinDist;
@@ -48,6 +66,6 @@ class XRSOUND_API CSoundRender_Source final : public CSound_source
4866
[[nodiscard]] u32 game_type() const override { return m_uGameType; }
4967
[[nodiscard]] pcstr file_name() const override { return *fname; }
5068
[[nodiscard]] float base_volume() const { return m_fBaseVolume; }
51-
[[nodiscard]] u16 channels_num() const override { return m_wformat.nChannels; }
69+
[[nodiscard]] u16 channels_num() const override { return m_info.channels; }
5270
[[nodiscard]] u32 bytes_total() const override { return dwBytesTotal; }
5371
};

src/xrSound/SoundRender_Target.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ void CSoundRender_Target::start(CSoundRender_Emitter* E)
2929
//m_pEmitter->source()->attach();
3030

3131
// Calc storage
32-
buf_block = sdef_target_block * E->source()->m_wformat.nAvgBytesPerSec / 1000;
3332
for (auto& buf : temp_buf)
34-
buf.resize(buf_block);
33+
buf.resize(E->source()->m_info.bytesPerBuffer);
3534
}
3635

3736
void CSoundRender_Target::render()
@@ -69,7 +68,7 @@ void CSoundRender_Target::fill_parameters()
6968
void CSoundRender_Target::fill_block(size_t idx)
7069
{
7170
R_ASSERT(m_pEmitter);
72-
m_pEmitter->fill_block(temp_buf[idx].data(), buf_block);
71+
m_pEmitter->fill_block(temp_buf[idx].data(), temp_buf[idx].size());
7372
}
7473

7574
void CSoundRender_Target::fill_all_blocks()

src/xrSound/SoundRender_Target.h

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class CSoundRender_Target
1010
CSoundRender_Emitter* m_pEmitter{};
1111
bool rendering{};
1212

13-
u32 buf_block{};
1413
xr_vector<u8> temp_buf[sdef_target_count];
1514
void fill_block(size_t idx);
1615
void fill_all_blocks();

src/xrSound/SoundRender_TargetA.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ void CSoundRender_TargetA::update()
110110
A_CHK(alSourceUnqueueBuffers(pSource, 1, &BufferID));
111111

112112
const auto id = get_block_id(BufferID);
113-
submit_buffer(BufferID, temp_buf[id].data());
113+
submit_buffer(BufferID, temp_buf[id].data(), temp_buf[id].size());
114114

115115
A_CHK(alSourceQueueBuffers(pSource, 1, &BufferID));
116116
processed--;
@@ -201,26 +201,26 @@ size_t CSoundRender_TargetA::get_block_id(ALuint BufferID) const
201201
return it - std::begin(pBuffers);
202202
}
203203

204-
void CSoundRender_TargetA::submit_buffer(ALuint BufferID, const void* data) const
204+
void CSoundRender_TargetA::submit_buffer(ALuint BufferID, const void* data, size_t dataSize) const
205205
{
206206
R_ASSERT(m_pEmitter);
207207

208-
const auto& wvf = m_pEmitter->source()->m_wformat;
209-
const bool mono = wvf.nChannels == 1;
208+
const auto& info = m_pEmitter->source()->m_info;
209+
const bool mono = info.channels == 1;
210210

211211
ALuint format;
212-
if (wvf.wFormatTag == WAVE_FORMAT_IEEE_FLOAT)
212+
if (info.format == SoundFormat::Float32)
213213
format = mono ? AL_FORMAT_MONO_FLOAT32 : AL_FORMAT_STEREO_FLOAT32;
214214
else
215215
{
216216
format = mono ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
217217
}
218218

219-
A_CHK(alBufferData(BufferID, format, data, buf_block, m_pEmitter->source()->m_wformat.nSamplesPerSec));
219+
A_CHK(alBufferData(BufferID, format, data, dataSize, info.samplesPerSec));
220220
}
221221

222222
void CSoundRender_TargetA::submit_all_buffers() const
223223
{
224224
for (size_t i = 0; i < sdef_target_count; ++i)
225-
submit_buffer(pBuffers[i], temp_buf[i].data());
225+
submit_buffer(pBuffers[i], temp_buf[i].data(), temp_buf[i].size());
226226
}

src/xrSound/SoundRender_TargetA.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class CSoundRender_TargetA : public CSoundRender_Target
1616
float cache_pitch{ 1.0f };
1717

1818
size_t get_block_id(ALuint BufferID) const;
19-
void submit_buffer(ALuint BufferID, const void* data) const;
19+
void submit_buffer(ALuint BufferID, const void* data, size_t dataSize) const;
2020
void submit_all_buffers() const;
2121

2222
public:

src/xrSound/stdafx.h

-23
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,3 @@
88
#include "xrCDB/xrCDB.h"
99

1010
#include "Sound.h"
11-
12-
#if defined(XR_PLATFORM_WINDOWS)
13-
// mmreg.h
14-
#define NOMMIDS
15-
#define NONEWRIFF
16-
#define NOJPEGDIB
17-
#define NONEWIC
18-
#define NOBITMAP
19-
#include <mmreg.h>
20-
#else
21-
#define WAVE_FORMAT_PCM 0x0001
22-
#define WAVE_FORMAT_IEEE_FLOAT 0x0003
23-
24-
typedef struct {
25-
WORD wFormatTag;
26-
WORD nChannels;
27-
DWORD nSamplesPerSec;
28-
DWORD nAvgBytesPerSec;
29-
WORD nBlockAlign;
30-
WORD wBitsPerSample;
31-
WORD cbSize;
32-
} WAVEFORMATEX, *LPWAVEFORMATEX;
33-
#endif

0 commit comments

Comments
 (0)