Skip to content

Commit

Permalink
wip.
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwebrtc committed Nov 19, 2024
1 parent ced2713 commit deb8a6f
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 3 deletions.
117 changes: 116 additions & 1 deletion example/lib/widgets/participant.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,18 @@ abstract class _ParticipantWidgetState<T extends ParticipantWidget>
right: 30,
child: ParticipantStatsWidget(
participant: widget.participant,
)),
)),
Positioned(
top: 10,
right: 10,
left: 10,
bottom: 10,
child: SoundWaveformWidget(
participant: widget.participant,
width: 8,
),
),

],
),
);
Expand Down Expand Up @@ -360,3 +371,107 @@ class RemoteTrackQualityMenuWidget extends StatelessWidget {
),
);
}

class SoundWaveformWidget extends StatefulWidget {
final int count;
final double width;
final double minHeight;
final double maxHeight;
final int durationInMilliseconds;
const SoundWaveformWidget({
super.key,
this.participant,
this.count = 7,
this.width = 5,
this.minHeight = 8,
this.maxHeight = 100,
this.durationInMilliseconds = 500,
});
final Participant? participant;
@override
State<SoundWaveformWidget> createState() => _SoundWaveformWidgetState();
}

class _SoundWaveformWidgetState extends State<SoundWaveformWidget>
with TickerProviderStateMixin {
late AnimationController controller;
List<double> samples = [0,0,0,0,0,0,0];
EventsListener<TrackEvent>? _listener;

_onParticipantChanged() {
for( var track in widget.participant?.audioTrackPublications ?? []) {
if (track.track != null) {
_setUpListener(track.track as AudioTrack);
}
}
}

void _setUpListener(AudioTrack track) {
_listener?.dispose();
_listener = track.createListener();
_listener?.on<AudioVisualizerEvent>((e) {
setState(() {
samples = e.event.map((e) => ((e as num) * 100).toDouble()).toList();
});
});
}
@override
void initState() {
super.initState();

widget.participant?.addListener(_onParticipantChanged);


controller = AnimationController(
vsync: this,
duration: Duration(
milliseconds: widget.durationInMilliseconds,
))
..repeat();
}

@override
void dispose() {
controller.dispose();
widget.participant?.removeListener(_onParticipantChanged);
_listener?.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
final count = widget.count;
final minHeight = widget.minHeight;
final maxHeight = widget.maxHeight;
return AnimatedBuilder(
animation: controller,
builder: (c, child) {
//double t = controller.value;
//int current = (samples.length * t).floor();
return Row(
mainAxisSize: MainAxisSize.min,
children: List.generate(
count,
(i) => AnimatedContainer(
duration: Duration(
milliseconds: widget.durationInMilliseconds ~/ count),
margin: i == (samples.length - 1)
? EdgeInsets.zero
: const EdgeInsets.only(right: 5),
height: samples[i] < minHeight
? minHeight
: samples[i] > maxHeight
? maxHeight
: samples[i],
width: widget.width,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(9999),
),
),
),
);
},
);
}
}
2 changes: 1 addition & 1 deletion lib/src/track/track.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ abstract class Track extends DisposableChangeNotifier
_eventChannel = EventChannel('io.livekit.audio.visualizer/eventChannel-${mediaStreamTrack.id}');

_eventChannel?.receiveBroadcastStream().listen((event) {
logger.fine('[$objectId] visualizer event(${event})');
//logger.fine('[$objectId] visualizer event(${event})');
events.emit(AudioVisualizerEvent(
track: this,
event: event,
Expand Down
3 changes: 2 additions & 1 deletion shared_swift/Visualizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ public class AudioProcessor: NSObject, RTCAudioRenderer, FlutterStreamHandler {

DispatchQueue.main.async { [weak self] in
guard let self else { return }
eventSink?(newBands)

self.bands = zip(self.bands, newBands).map { old, new in
self._smoothTransition(from: old, to: new, factor: self.smoothingFactor)
}
eventSink?(self.bands)
}
}

Expand Down

0 comments on commit deb8a6f

Please sign in to comment.