Skip to content

Commit

Permalink
feat(web): add setBalance (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustl22 committed Oct 8, 2022
1 parent d15ef5a commit 949b7f2
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
2 changes: 1 addition & 1 deletion feature_parity_table.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Note: LLM means Low Latency Mode.
<tr><td>stay awake</td><td>yes (except LLM)</td><td>yes</td><td>no</td><td>no</td><td>no</td><td>no</td></tr>
<tr><td>recording active</td><td>not yet</td><td>yes</td><td>no</td><td>no</td><td>no</td><td>no</td></tr>
<tr><td>playing route</td><td>yes (except LLM)</td><td>yes</td><td>no</td><td>no</td><td>no</td><td>no</td></tr>
<tr><td>balance</td><td>no</td><td>no</td><td>no</td><td>no</td><td>yes</td><td>yes</td></tr>
<tr><td>balance</td><td>no</td><td>no</td><td>no</td><td>yes</td><td>yes</td><td>yes</td></tr>
<tr><td colspan="7"><strong>Streams</strong></td></tr>
<tr><td>duration event</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td></tr>
<tr><td>position event</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td></tr>
Expand Down
57 changes: 57 additions & 0 deletions packages/audioplayers_web/lib/web_audio_js.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import 'dart:html';

import 'package:js/js.dart';

@JS('AudioContext')
@staticInterop
class JsAudioContext {
external JsAudioContext();
}

extension JsAudioContextExtension on JsAudioContext {
external MediaElementAudioSourceNode createMediaElementSource(
AudioElement element,);

external StereoPannerNode createStereoPanner();

external AudioNode get destination;
}

@JS()
@staticInterop
abstract class AudioNode {
external AudioNode();

}

extension AudioNodeExtension on AudioNode {
external AudioNode connect(AudioNode audioNode);
}

@JS()
@staticInterop
class AudioParam {
external AudioParam();
}

extension AudioParamExtension on AudioParam {
external num value;
}

@JS()
@staticInterop
class StereoPannerNode
implements AudioNode {
external StereoPannerNode();
}

extension StereoPannerNodeExtension on StereoPannerNode {
external AudioParam get pan;
}

@JS()
@staticInterop
class MediaElementAudioSourceNode
implements AudioNode {
external MediaElementAudioSourceNode();
}
14 changes: 13 additions & 1 deletion packages/audioplayers_web/lib/wrapped_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:html';
import 'package:audioplayers_platform_interface/api/release_mode.dart';
import 'package:audioplayers_platform_interface/streams_interface.dart';
import 'package:audioplayers_web/num_extension.dart';
import 'package:audioplayers_web/web_audio_js.dart';

class WrappedPlayer {
final String playerId;
Expand All @@ -17,6 +18,7 @@ class WrappedPlayer {
bool isPlaying = false;

AudioElement? player;
StereoPannerNode? stereoPanner;
StreamSubscription? playerTimeUpdateSubscription;
StreamSubscription? playerEndedSubscription;
StreamSubscription? playerLoadedDataSubscription;
Expand Down Expand Up @@ -44,7 +46,7 @@ class WrappedPlayer {
}

void setBalance(double balance) {
throw UnimplementedError('setBalance is not currently implemented on Web');
stereoPanner?.pan.value = balance;
}

void setPlaybackRate(double rate) {
Expand All @@ -58,9 +60,18 @@ class WrappedPlayer {
}

final p = player = AudioElement(currentUrl);
p.crossOrigin = 'anonymous'; // need for stereo panning to work
p.loop = shouldLoop();
p.volume = currentVolume;
p.playbackRate = currentPlaybackRate;

// setup stereo panning
final audioContext = JsAudioContext();
final source = audioContext.createMediaElementSource(player!);
stereoPanner = audioContext.createStereoPanner();
source.connect(stereoPanner!);
stereoPanner?.connect(audioContext.destination);

playerPlaySubscription = p.onPlay.listen((_) {
streamsInterface.emitDuration(
playerId,
Expand Down Expand Up @@ -99,6 +110,7 @@ class WrappedPlayer {
void release() {
_cancel();
player = null;
stereoPanner = null;

playerLoadedDataSubscription?.cancel();
playerLoadedDataSubscription = null;
Expand Down

0 comments on commit 949b7f2

Please sign in to comment.