Skip to content

feat: Add smoothTransition option for AudioVisualizer. #773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: Add smoothTransition option for AudioVisualizer.
  • Loading branch information
cloudwebrtc committed Apr 30, 2025
commit 7acc1e31f44192ca41cbe2afc669038f4bfc114c
4 changes: 3 additions & 1 deletion android/src/main/kotlin/io/livekit/plugin/LiveKitPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class LiveKitPlugin: FlutterPlugin, MethodCallHandler {
var audioTrack: LKAudioTrack? = null
val barCount = call.argument<Int>("barCount") ?: 7
val isCentered = call.argument<Boolean>("isCentered") ?: true
var smoothTransition = call.argument<Boolean>("smoothTransition") ?: true

val track = flutterWebRTCPlugin.getLocalTrack(trackId)
if (track != null) {
Expand All @@ -75,7 +76,8 @@ class LiveKitPlugin: FlutterPlugin, MethodCallHandler {
}

val visualizer = Visualizer(
barCount = barCount, isCentered = isCentered,
barCount = barCount, isCentered = isCentered,
smoothTransition = smoothTransition,
audioTrack = audioTrack, binaryMessenger = binaryMessenger!!,
visualizerId = visualizerId)

Expand Down
9 changes: 6 additions & 3 deletions android/src/main/kotlin/io/livekit/plugin/Visualizer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import kotlin.math.*
class Visualizer(
private var barCount: Int,
private var isCentered: Boolean,
private var smoothTransition: Boolean,
audioTrack: LKAudioTrack,
binaryMessenger: BinaryMessenger,
visualizerId: String
Expand Down Expand Up @@ -79,9 +80,11 @@ class Visualizer(
amplitudes = centerBands(amplitudes)
}

bands = bands.mapIndexed { index, value ->
smoothTransition(value, amplitudes[index], 0.3f)
}.toFloatArray()
if(this.smoothTransition) {
bands = bands.mapIndexed { index, value ->
smoothTransition(value, amplitudes[index], 0.3f)
}.toFloatArray()
}

handler.post {
eventSink?.success(bands)
Expand Down
2 changes: 2 additions & 0 deletions lib/src/support/native.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Native {
bool isCentered = true,
int barCount = 7,
String visualizerId = '',
bool smoothTransition = true,
}) async {
try {
final result = await channel.invokeMethod<bool>(
Expand All @@ -64,6 +65,7 @@ class Native {
'isCentered': isCentered,
'barCount': barCount,
'visualizerId': visualizerId,
'smoothTransition': smoothTransition,
},
);
return result == true;
Expand Down
2 changes: 2 additions & 0 deletions lib/src/track/audio_visualizer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import 'audio_visualizer_native.dart'
class AudioVisualizerOptions {
final bool centeredBands;
final int barCount;
final bool smoothTransition;
const AudioVisualizerOptions({
this.centeredBands = true,
this.barCount = 7,
this.smoothTransition = true,
});
}

Expand Down
1 change: 1 addition & 0 deletions lib/src/track/audio_visualizer_native.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class AudioVisualizerNative extends AudioVisualizer {
isCentered: visualizerOptions.centeredBands,
barCount: visualizerOptions.barCount,
visualizerId: visualizerId,
smoothTransition: visualizerOptions.smoothTransition,
);

_eventChannel = EventChannel(
Expand Down
4 changes: 3 additions & 1 deletion lib/src/track/audio_visualizer_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ class AudioVisualizerWeb extends AudioVisualizer {
frequencies = frequencies.sublist(
options.loPass!.toInt(), options.hiPass!.toInt());

final normalizedFrequencies = normalizeFrequencies(frequencies);
final normalizedFrequencies = visualizerOptions.smoothTransition
? normalizeFrequencies(frequencies)
: frequencies;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
final normalizedFrequencies = visualizerOptions.smoothTransition
? normalizeFrequencies(frequencies)
: frequencies;
final normalizedFrequencies = normalizeFrequencies(frequencies);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved

final chunkSize = (normalizedFrequencies.length / (bands + 1)).ceil();
Float32List chunks = Float32List(visualizerOptions.barCount);

Expand Down
3 changes: 3 additions & 0 deletions shared_swift/LiveKitPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public class LiveKitPlugin: NSObject, FlutterPlugin {
let visualizerId = args["visualizerId"] as? String
let barCount = args["barCount"] as? Int ?? 7
let isCentered = args["isCentered"] as? Bool ?? true
let smoothTransition = args["smoothTransition"] as? Bool ?? true

if visualizerId == nil {
result(FlutterError(code: "visualizerId", message: "visualizerId is required", details: nil))
Expand All @@ -129,6 +130,7 @@ public class LiveKitPlugin: NSObject, FlutterPlugin {
binaryMessenger: self.binaryMessenger!,
bandCount: barCount,
isCentered: isCentered,
smoothTransition: smoothTransition,
visualizerId: unwrappedVisualizerId)

tracks[unwrappedTrackId] = lkLocalTrack
Expand All @@ -143,6 +145,7 @@ public class LiveKitPlugin: NSObject, FlutterPlugin {
binaryMessenger: self.binaryMessenger!,
bandCount: barCount,
isCentered: isCentered,
smoothTransition: smoothTransition,
visualizerId: unwrappedVisualizerId)
tracks[unwrappedTrackId] = lkRemoteTrack
processors[unwrappedVisualizerId] = processor
Expand Down
9 changes: 7 additions & 2 deletions shared_swift/Visualizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class Visualizer: NSObject, RTCAudioRenderer, FlutterStreamHandler {

public let isCentered: Bool
public let smoothingFactor: Float

public let smoothTransition: Bool
public var bands: [Float]

private let _processor: AudioVisualizeProcessor
Expand All @@ -53,11 +53,13 @@ public class Visualizer: NSObject, RTCAudioRenderer, FlutterStreamHandler {
binaryMessenger: FlutterBinaryMessenger,
bandCount: Int = 7,
isCentered: Bool = true,
smoothTransition: Bool = true,
smoothingFactor: Float = 0.3,
visualizerId: String)
{
self.isCentered = isCentered
self.smoothingFactor = smoothingFactor
self.smoothTransition = smoothTransition
bands = Array(repeating: 0.0, count: bandCount)
_processor = AudioVisualizeProcessor(bandsCount: bandCount)
_track = track
Expand Down Expand Up @@ -86,7 +88,10 @@ public class Visualizer: NSObject, RTCAudioRenderer, FlutterStreamHandler {
guard let self else { return }

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