Skip to content

Commit 8ed6e45

Browse files
authored
Merge pull request #203 from alnitak/resetBuffer
Reset buffer
2 parents a4b0ead + d1f23db commit 8ed6e45

15 files changed

+145
-45
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"name": "Flutter debug",
99
"type": "dart",
1010
"request": "launch",
11-
"program": "lib/main.dart",
11+
"program": "lib/buffer_stream/websocket.dart",
1212
"flutterMode": "debug",
1313
// "env": {
1414
// "NO_OPUS_OGG_LIBS": "1"

example/lib/buffer_stream/websocket.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ class _WebsocketExampleState extends State<WebsocketExample> {
198198
sampleRate: sampleRate[srId],
199199
channels: Channels.values[chId],
200200
format: BufferType.values[fmtId],
201-
bufferingType: BufferingType.released,
201+
// ignore: avoid_redundant_argument_values
202+
bufferingType: BufferingType.preserved,
202203
onBuffering: (isBuffering, handle, time) async {
203204
debugPrint('started buffering? $isBuffering with '
204205
'handle: $handle at time $time');
@@ -299,6 +300,14 @@ class _WebsocketExampleState extends State<WebsocketExample> {
299300
child: const Text('play'),
300301
),
301302
const SizedBox(width: 8),
303+
OutlinedButton(
304+
onPressed: () async {
305+
if (currentSound == null) return;
306+
SoLoud.instance.resetBufferStream(currentSound!);
307+
},
308+
child: const Text('reset buffer'),
309+
),
310+
const SizedBox(width: 8),
302311
OutlinedButton(
303312
onPressed: () async {
304313
currentSound = null;

example/pubspec.lock

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,42 @@ packages:
55
dependency: transitive
66
description:
77
name: async
8-
sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c"
8+
sha256: d2872f9c19731c2e5f10444b14686eb7cc85c76274bd6c16e1816bff9a3bab63
99
url: "https://pub.dev"
1010
source: hosted
11-
version: "2.11.0"
11+
version: "2.12.0"
1212
boolean_selector:
1313
dependency: transitive
1414
description:
1515
name: boolean_selector
16-
sha256: "6cfb5af12253eaf2b368f07bacc5a80d1301a071c73360d746b7f2e32d762c66"
16+
sha256: "8aab1771e1243a5063b8b0ff68042d67334e3feab9e95b9490f9a6ebf73b42ea"
1717
url: "https://pub.dev"
1818
source: hosted
19-
version: "2.1.1"
19+
version: "2.1.2"
2020
characters:
2121
dependency: transitive
2222
description:
2323
name: characters
24-
sha256: "04a925763edad70e8443c99234dc3328f442e811f1d8fd1a72f1c8ad0f69a605"
24+
sha256: f71061c654a3380576a52b451dd5532377954cf9dbd272a78fc8479606670803
2525
url: "https://pub.dev"
2626
source: hosted
27-
version: "1.3.0"
27+
version: "1.4.0"
2828
clock:
2929
dependency: transitive
3030
description:
3131
name: clock
32-
sha256: cb6d7f03e1de671e34607e909a7213e31d7752be4fb66a86d29fe1eb14bfb5cf
32+
sha256: fddb70d9b5277016c77a80201021d40a2247104d9f4aa7bab7157b7e3f05b84b
3333
url: "https://pub.dev"
3434
source: hosted
35-
version: "1.1.1"
35+
version: "1.1.2"
3636
collection:
3737
dependency: transitive
3838
description:
3939
name: collection
40-
sha256: a1ace0a119f20aabc852d165077c036cd864315bd99b7eaa10a60100341941bf
40+
sha256: "2f5709ae4d3d59dd8f7cd309b4e023046b57d8a6c82130785d2b0e5868084e76"
4141
url: "https://pub.dev"
4242
source: hosted
43-
version: "1.19.0"
43+
version: "1.19.1"
4444
cross_file:
4545
dependency: transitive
4646
description:
@@ -69,10 +69,10 @@ packages:
6969
dependency: transitive
7070
description:
7171
name: fake_async
72-
sha256: "511392330127add0b769b75a987850d136345d9227c6b94c96a04cf4a391bf78"
72+
sha256: "6a95e56b2449df2273fd8c45a662d6947ce1ebb7aafe80e550a3f68297f3cacc"
7373
url: "https://pub.dev"
7474
source: hosted
75-
version: "1.3.1"
75+
version: "1.3.2"
7676
ffi:
7777
dependency: transitive
7878
description:
@@ -108,7 +108,7 @@ packages:
108108
path: ".."
109109
relative: true
110110
source: path
111-
version: "3.0.2"
111+
version: "3.0.3"
112112
flutter_test:
113113
dependency: "direct dev"
114114
description: flutter
@@ -139,18 +139,18 @@ packages:
139139
dependency: transitive
140140
description:
141141
name: leak_tracker
142-
sha256: "7bb2830ebd849694d1ec25bf1f44582d6ac531a57a365a803a6034ff751d2d06"
142+
sha256: c35baad643ba394b40aac41080300150a4f08fd0fd6a10378f8f7c6bc161acec
143143
url: "https://pub.dev"
144144
source: hosted
145-
version: "10.0.7"
145+
version: "10.0.8"
146146
leak_tracker_flutter_testing:
147147
dependency: transitive
148148
description:
149149
name: leak_tracker_flutter_testing
150-
sha256: "9491a714cca3667b60b5c420da8217e6de0d1ba7a5ec322fab01758f6998f379"
150+
sha256: f8b613e7e6a13ec79cfdc0e97638fddb3ab848452eff057653abd3edba760573
151151
url: "https://pub.dev"
152152
source: hosted
153-
version: "3.0.8"
153+
version: "3.0.9"
154154
leak_tracker_testing:
155155
dependency: transitive
156156
description:
@@ -171,10 +171,10 @@ packages:
171171
dependency: transitive
172172
description:
173173
name: matcher
174-
sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb
174+
sha256: dc58c723c3c24bf8d3e2d3ad3f2f9d7bd9cf43ec6feaa64181775e60190153f2
175175
url: "https://pub.dev"
176176
source: hosted
177-
version: "0.12.16+1"
177+
version: "0.12.17"
178178
material_color_utilities:
179179
dependency: transitive
180180
description:
@@ -187,18 +187,18 @@ packages:
187187
dependency: transitive
188188
description:
189189
name: meta
190-
sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7
190+
sha256: e3641ec5d63ebf0d9b41bd43201a66e3fc79a65db5f61fc181f04cd27aab950c
191191
url: "https://pub.dev"
192192
source: hosted
193-
version: "1.15.0"
193+
version: "1.16.0"
194194
path:
195195
dependency: transitive
196196
description:
197197
name: path
198-
sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af"
198+
sha256: "75cca69d1490965be98c73ceaea117e8a04dd21217b37b292c9ddbec0d955bc5"
199199
url: "https://pub.dev"
200200
source: hosted
201-
version: "1.9.0"
201+
version: "1.9.1"
202202
path_provider:
203203
dependency: transitive
204204
description:
@@ -272,50 +272,50 @@ packages:
272272
dependency: transitive
273273
description:
274274
name: source_span
275-
sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c"
275+
sha256: "254ee5351d6cb365c859e20ee823c3bb479bf4a293c22d17a9f1bf144ce86f7c"
276276
url: "https://pub.dev"
277277
source: hosted
278-
version: "1.10.0"
278+
version: "1.10.1"
279279
stack_trace:
280280
dependency: transitive
281281
description:
282282
name: stack_trace
283-
sha256: "9f47fd3630d76be3ab26f0ee06d213679aa425996925ff3feffdec504931c377"
283+
sha256: "8b27215b45d22309b5cddda1aa2b19bdfec9df0e765f2de506401c071d38d1b1"
284284
url: "https://pub.dev"
285285
source: hosted
286-
version: "1.12.0"
286+
version: "1.12.1"
287287
stream_channel:
288288
dependency: transitive
289289
description:
290290
name: stream_channel
291-
sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7
291+
sha256: "969e04c80b8bcdf826f8f16579c7b14d780458bd97f56d107d3950fdbeef059d"
292292
url: "https://pub.dev"
293293
source: hosted
294-
version: "2.1.2"
294+
version: "2.1.4"
295295
string_scanner:
296296
dependency: transitive
297297
description:
298298
name: string_scanner
299-
sha256: "688af5ed3402a4bde5b3a6c15fd768dbf2621a614950b17f04626c431ab3c4c3"
299+
sha256: "921cd31725b72fe181906c6a94d987c78e3b98c2e205b397ea399d4054872b43"
300300
url: "https://pub.dev"
301301
source: hosted
302-
version: "1.3.0"
302+
version: "1.4.1"
303303
term_glyph:
304304
dependency: transitive
305305
description:
306306
name: term_glyph
307-
sha256: a29248a84fbb7c79282b40b8c72a1209db169a2e0542bce341da992fe1bc7e84
307+
sha256: "7f554798625ea768a7518313e58f83891c7f5024f88e46e7182a4558850a4b8e"
308308
url: "https://pub.dev"
309309
source: hosted
310-
version: "1.2.1"
310+
version: "1.2.2"
311311
test_api:
312312
dependency: transitive
313313
description:
314314
name: test_api
315-
sha256: "664d3a9a64782fcdeb83ce9c6b39e78fd2971d4e37827b9b06c3aa1edc5e760c"
315+
sha256: fb31f383e2ee25fbbfe06b40fe21e1e458d14080e3c67e7ba0acfde4df4e0bbd
316316
url: "https://pub.dev"
317317
source: hosted
318-
version: "0.7.3"
318+
version: "0.7.4"
319319
typed_data:
320320
dependency: transitive
321321
description:
@@ -344,10 +344,10 @@ packages:
344344
dependency: transitive
345345
description:
346346
name: vm_service
347-
sha256: f6be3ed8bd01289b34d679c2b62226f63c0e69f9fd2e50a6b3c1c729a961041b
347+
sha256: "0968250880a6c5fe7edc067ed0a13d4bae1577fe2771dcf3010d52c4a9d3ca14"
348348
url: "https://pub.dev"
349349
source: hosted
350-
version: "14.3.0"
350+
version: "14.3.1"
351351
web:
352352
dependency: transitive
353353
description:
@@ -389,5 +389,5 @@ packages:
389389
source: hosted
390390
version: "1.1.0"
391391
sdks:
392-
dart: ">=3.5.0 <4.0.0"
392+
dart: ">=3.7.0-0 <4.0.0"
393393
flutter: ">=3.24.0"

lib/src/bindings/bindings_player.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ abstract class FlutterSoLoud {
158158
OnBufferingCallbackTFunction? onBuffering,
159159
);
160160

161+
/// Reset the buffer of the audio stream.
162+
/// [hash] the hash of the stream sound.
163+
/// Returns [PlayerErrors.noError] if success.
164+
@mustBeOverridden
165+
PlayerErrors resetBufferStream(SoundHash soundHash);
166+
161167
/// Add a chunk of audio data to the buffer stream.
162168
///
163169
/// [hash] the hash of the sound.

lib/src/bindings/bindings_player_ffi.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,18 @@ class FlutterSoLoudFfi extends FlutterSoLoud {
452452
.NativeFunction<ffi.Void Function(ffi.Bool, ffi.Int, ffi.Double)>>,
453453
)>();
454454

455+
@override
456+
PlayerErrors resetBufferStream(SoundHash soundHash) {
457+
final e = _resetBufferStream(soundHash.hash);
458+
return PlayerErrors.values[e];
459+
}
460+
461+
late final _resetBufferStreamPtr =
462+
_lookup<ffi.NativeFunction<ffi.UnsignedInt Function(ffi.UnsignedInt)>>(
463+
'resetBufferStream');
464+
late final _resetBufferStream =
465+
_resetBufferStreamPtr.asFunction<int Function(int)>();
466+
455467
@override
456468
PlayerErrors addAudioDataStream(
457469
int hash,

lib/src/bindings/bindings_player_web.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,12 @@ class FlutterSoLoudWeb extends FlutterSoLoud {
251251
return ret;
252252
}
253253

254+
@override
255+
PlayerErrors resetBufferStream(SoundHash soundHash) {
256+
final result = wasmResetBufferStream(soundHash.hash);
257+
return PlayerErrors.values[result];
258+
}
259+
254260
@override
255261
PlayerErrors addAudioDataStream(
256262
int hash,

lib/src/bindings/js_extension.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ external int wasmSetBufferStream(
6666
int onBufferingPtr,
6767
);
6868

69+
@JS('Module_soloud._resetBufferStream')
70+
external int wasmResetBufferStream(int hash);
71+
6972
@JS('Module_soloud._addAudioDataStream')
7073
external int wasmAddAudioDataStream(int hash, int audioChunkPtr, int dataLen);
7174

lib/src/soloud.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,29 @@ interface class SoLoud {
718718
return newSound;
719719
}
720720

721+
/// Resets the buffer of the data stream.
722+
///
723+
/// It happens that when playing a stream, maybe from the web, it is needed
724+
/// to change it to another source. The player continues to play the already
725+
/// added audio data to the buffer. This method can be used to reset the
726+
/// buffer and start with the new audio data.
727+
///
728+
/// [hash] the hash of the stream sound.
729+
///
730+
/// Throws [SoLoudNotInitializedException] if the engine is not initialized.
731+
/// Throws [SoLoudSoundHashNotFoundDartException] if the [sound] is not found.
732+
void resetBufferStream(AudioSource sound) {
733+
if (!isInitialized) {
734+
throw const SoLoudNotInitializedException();
735+
}
736+
final e = SoLoudController().soLoudFFI.resetBufferStream(sound.soundHash);
737+
738+
if (e != PlayerErrors.noError) {
739+
_logPlayerError(e, from: 'resetBufferStream() result');
740+
throw SoLoudCppException.fromPlayerError(e);
741+
}
742+
}
743+
721744
/// Add PCM audio data to the stream.
722745
///
723746
/// This method can be called within an `Isolate` making it possible

src/audiobuffer/audiobuffer.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace SoLoud
1515
{
16+
std::mutex buffer_lock_mutex;
17+
1618
BufferStreamInstance::BufferStreamInstance(BufferStream *aParent)
1719
{
1820
mParent = aParent;
@@ -25,6 +27,8 @@ namespace SoLoud
2527

2628
unsigned int BufferStreamInstance::getAudio(float *aBuffer, unsigned int aSamplesToRead, unsigned int aBufferSize)
2729
{
30+
std::lock_guard<std::mutex> lock(buffer_lock_mutex);
31+
2832
if (mParent->mBuffer.getFloatsBufferSize() == 0)
2933
{
3034
memset(aBuffer, 0, sizeof(float) * aSamplesToRead);
@@ -181,6 +185,15 @@ namespace SoLoud
181185
return PlayerErrors::noError;
182186
}
183187

188+
void BufferStream::resetBuffer()
189+
{
190+
std::lock_guard<std::mutex> lock(buffer_lock_mutex);
191+
buffer.clear();
192+
mBuffer.clear();
193+
mSampleCount = 0;
194+
mBytesReceived = 0;
195+
}
196+
184197
void BufferStream::setDataIsEnded()
185198
{
186199
if (buffer.size() > 0)

src/audiobuffer/audiobuffer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ namespace SoLoud
6464
time bufferingTimeNeeds = 2.0f, // 2 seconds of data to wait
6565
PCMformat pcmFormat = {44100, 2, 2, PCM_S16LE},
6666
dartOnBufferingCallback_t onBufferingCallback = nullptr);
67+
void resetBuffer();
6768
void setDataIsEnded();
6869
PlayerErrors addData(const void *aData, unsigned int numSamples, bool forceAdd = false);
6970
BufferingType getBufferingType();

0 commit comments

Comments
 (0)