@@ -51,11 +51,11 @@ bool ov_can_continue_read(long res)
51
51
52
52
void CSoundRender_Source::decompress (void * dest, u32 byte_offset, u32 size)
53
53
{
54
- std::lock_guard guard{ read_lock };
55
-
56
54
if (!wave)
57
55
attach ();
58
56
57
+ std::lock_guard guard{ read_lock };
58
+
59
59
// seek
60
60
const auto sample_offset = ogg_int64_t (byte_offset / m_wformat.nBlockAlign );
61
61
const u32 cur_pos = u32 (ov_pcm_tell (&ovf));
@@ -145,21 +145,28 @@ constexpr ov_callbacks g_ov_callbacks =
145
145
146
146
void CSoundRender_Source::attach ()
147
147
{
148
- VERIFY (0 == wave);
149
- if (wave)
150
- return ;
151
- wave = FS.r_open (pname.c_str ());
152
- R_ASSERT3 (wave && wave->length (), " Can't open wave file:" , pname.c_str ());
153
- ov_open_callbacks (wave, &ovf, nullptr , 0 , g_ov_callbacks);
148
+ std::lock_guard guard{ read_lock };
149
+ ++refs;
150
+ VERIFY (refs > 0 );
151
+ if (refs == 1 )
152
+ {
153
+ wave = FS.r_open (pname.c_str ());
154
+ R_ASSERT3 (wave && wave->length (), " Can't open wave file:" , pname.c_str ());
155
+ ov_open_callbacks (wave, &ovf, nullptr , 0 , g_ov_callbacks);
156
+ }
154
157
}
155
158
156
159
void CSoundRender_Source::detach ()
157
160
{
158
- if (wave)
161
+ std::lock_guard guard{ read_lock };
162
+ --refs;
163
+ if (refs == 0 )
159
164
{
160
165
ov_clear (&ovf);
161
166
FS.r_close (wave);
162
167
}
168
+ if (refs < 0 )
169
+ refs = 0 ;
163
170
}
164
171
165
172
bool CSoundRender_Source::LoadWave (pcstr pName, bool crashOnError)
0 commit comments