Skip to content

Commit f291dad

Browse files
committed
Improve Buffer/Sound RAII
1 parent ab749dd commit f291dad

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

lib/src/capo.cpp

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,46 +140,66 @@ class Decoder : public ma_decoder {
140140
std::size_t m_reserve{};
141141
};
142142

143-
class Sound : public ma_sound {
143+
class Buffer : public ma_audio_buffer {
144144
public:
145-
Sound(Sound const&) = delete;
146-
Sound(Sound&&) = delete;
147-
auto operator=(Sound const&) -> Sound& = delete;
148-
auto operator=(Sound&&) -> Sound& = delete;
145+
Buffer(Buffer const&) = delete;
146+
Buffer(Buffer&&) = delete;
147+
auto operator=(Buffer const&) -> Buffer& = delete;
148+
auto operator=(Buffer&&) -> Buffer& = delete;
149149

150-
explicit Sound(ma_engine& engine, Pcm const& pcm) : ma_sound({}) {
150+
explicit Buffer(Pcm const& pcm) : ma_audio_buffer({}) {
151151
auto config = ma_audio_buffer_config_init(ma_format_f32, pcm.channels, pcm.get_frame_count(),
152152
pcm.samples.data(), nullptr);
153153
config.sampleRate = Pcm::sample_rate_v;
154-
auto result = ma_audio_buffer_init(&config, &m_buffer);
154+
auto result = ma_audio_buffer_init(&config, this);
155155
if (result != MA_SUCCESS) {
156156
failed = true;
157157
return;
158158
}
159+
}
159160

160-
result = ma_sound_init_from_data_source(&engine, &m_buffer, 0, nullptr, this);
161-
if (result != MA_SUCCESS) {
162-
ma_audio_buffer_uninit(&m_buffer);
161+
~Buffer() {
162+
if (failed) { return; }
163+
ma_audio_buffer_uninit(this);
164+
}
165+
166+
bool failed{};
167+
};
168+
169+
class Sound : public ma_sound {
170+
public:
171+
Sound(Sound const&) = delete;
172+
Sound(Sound&&) = delete;
173+
auto operator=(Sound const&) -> Sound& = delete;
174+
auto operator=(Sound&&) -> Sound& = delete;
175+
176+
explicit Sound(ma_engine& engine, Pcm const& pcm) : ma_sound({}) {
177+
m_buffer.emplace(pcm);
178+
if (m_buffer->failed) {
163179
failed = true;
180+
return;
164181
}
182+
183+
auto const result = ma_sound_init_from_data_source(&engine, &m_buffer, 0, nullptr, this);
184+
if (result != MA_SUCCESS) { failed = true; }
165185
}
166186

167187
explicit Sound(ma_engine& engine, char const* path) : ma_sound({}) {
168-
auto const result = ma_sound_init_from_file(&engine, path, MA_SOUND_FLAG_STREAM, nullptr, nullptr, this);
188+
static constexpr auto flags_v = MA_SOUND_FLAG_STREAM;
189+
auto const result = ma_sound_init_from_file(&engine, path, flags_v, nullptr, nullptr, this);
169190
if (result != MA_SUCCESS) { failed = true; }
170191
}
171192

172193
~Sound() {
173194
if (failed) { return; }
174195
ma_sound_stop(this);
175196
ma_sound_uninit(this);
176-
ma_audio_buffer_uninit(&m_buffer);
177197
}
178198

179199
bool failed{};
180200

181201
private:
182-
ma_audio_buffer m_buffer{};
202+
std::optional<Buffer> m_buffer{};
183203
};
184204

185205
class Source : public ISource {

0 commit comments

Comments
 (0)