Skip to content

Commit 1897912

Browse files
authored
Merge pull request #13441 from jordan-woyak/wm-speaker-cleanup
WiimoteEmu: Eliminate dynamic memory allocation in speaker data decoding.
2 parents 0299540 + 3bf2de3 commit 1897912

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

Diff for: Source/Core/Core/HW/WiimoteCommon/WiimoteReport.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,12 @@ static_assert(sizeof(OutputReportReadData) == 6, "Wrong size");
149149
struct OutputReportSpeakerData
150150
{
151151
static constexpr OutputReportID REPORT_ID = OutputReportID::SpeakerData;
152+
static constexpr size_t DATA_SIZE = 20;
152153

153154
u8 rumble : 1;
154155
u8 : 2;
155156
u8 length : 5;
156-
u8 data[20];
157+
std::array<u8, DATA_SIZE> data;
157158
};
158159
static_assert(sizeof(OutputReportSpeakerData) == 21, "Wrong size");
159160

Diff for: Source/Core/Core/HW/WiimoteEmu/EmuSubroutines.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ void Wiimote::HandleSpeakerData(const WiimoteCommon::OutputReportSpeakerData& rp
390390
{
391391
// Speaker data reports result in a write to the speaker hardware at offset 0x00.
392392
m_i2c_bus.BusWrite(SpeakerLogic::I2C_ADDR, SpeakerLogic::SPEAKER_DATA_OFFSET, rpt.length,
393-
rpt.data);
393+
std::data(rpt.data));
394394
}
395395
}
396396

Diff for: Source/Core/Core/HW/WiimoteEmu/Speaker.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33

44
#include "Core/HW/WiimoteEmu/Speaker.h"
55

6-
#include <memory>
6+
#include <cassert>
77

88
#include "AudioCommon/AudioCommon.h"
9+
910
#include "Common/CommonTypes.h"
1011
#include "Common/Logging/Log.h"
12+
1113
#include "Core/ConfigManager.h"
1214
#include "Core/HW/WiimoteEmu/WiimoteEmu.h"
1315
#include "Core/System.h"
16+
1417
#include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h"
1518
#include "InputCommon/ControllerEmu/Setting/NumericSetting.h"
1619

@@ -62,8 +65,9 @@ void SpeakerLogic::SpeakerData(const u8* data, int length, float speaker_pan)
6265

6366
// Even if volume is zero we process samples to maintain proper decoder state.
6467

65-
// TODO consider using static max size instead of new
66-
std::unique_ptr<s16[]> samples(new s16[length * 2]);
68+
// Potentially 40 resulting samples.
69+
std::array<s16, WiimoteCommon::OutputReportSpeakerData::DATA_SIZE * 2> samples;
70+
assert(length * 2 <= samples.size());
6771

6872
unsigned int sample_rate_dividend, sample_length;
6973
u8 volume_divisor;
@@ -130,7 +134,7 @@ void SpeakerLogic::SpeakerData(const u8* data, int length, float speaker_pan)
130134
// ADPCM sample rate is thought to be x2.(3000 x2 = 6000).
131135
const unsigned int sample_rate = sample_rate_dividend / reg_data.sample_rate;
132136
sound_stream->GetMixer()->PushWiimoteSpeakerSamples(
133-
samples.get(), sample_length, Mixer::FIXED_SAMPLE_RATE_DIVIDEND / (sample_rate * 2));
137+
samples.data(), sample_length, Mixer::FIXED_SAMPLE_RATE_DIVIDEND / (sample_rate * 2));
134138
}
135139

136140
void SpeakerLogic::Reset()

0 commit comments

Comments
 (0)