Skip to content

Commit

Permalink
Implemented peakDelayMs in AudioFile
Browse files Browse the repository at this point in the history
  • Loading branch information
supertick committed Nov 13, 2023
1 parent b7909ca commit 820d0c4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
22 changes: 21 additions & 1 deletion src/main/java/org/myrobotlab/audio/AudioProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
Expand Down Expand Up @@ -32,6 +36,8 @@ public class AudioProcessor extends Thread {
// it seems to make sense - some how the file gets decoded enough - so that
// a audio decoder can be slected from some
// internal registry ... i think

private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); transient private final ScheduledExecutorService delayScheduler = Executors.newScheduledThreadPool(1);

protected int currentTrackCount = 0;

Expand Down Expand Up @@ -231,7 +237,14 @@ public AudioData play(AudioData data) {
peak = abs;
}
}
audioFile.invoke("publishPeak", peak * (double) audioFile.getPeakMultiplier());

final double value = peak * (double) audioFile.getPeakMultiplier();

if (audioFile.getConfig().peakDelayMs == null) {
audioFile.invoke("publishPeak", value);
} else {
delayScheduler.schedule(() -> audioFile.invoke("publishPeak", value), audioFile.getConfig().peakDelayMs, TimeUnit.MILLISECONDS);
}
}
}
// Stop
Expand All @@ -247,6 +260,12 @@ public AudioData play(AudioData data) {

// System.gc();

if (audioFile.getConfig().peakDelayMs == null) {
audioFile.invoke("publishPeak", 0);
} else {
delayScheduler.schedule(() -> audioFile.invoke("publishPeak", 0), audioFile.getConfig().peakDelayMs, TimeUnit.MILLISECONDS);
}

audioFile.invoke("publishPeak", 0);
audioFile.invoke("publishAudioEnd", data);

Expand Down Expand Up @@ -313,6 +332,7 @@ public void run() {
}
// default waits on queued audio requests
log.info("audio processor {} exiting", getName());
delayScheduler.shutdown();
}

public void setVolume(Double volume) {
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/org/myrobotlab/service/config/AudioFileConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ public class AudioFileConfig extends ServiceConfig {
public double volume = 1.0;
public String currentPlaylist = "default";
public Map<String, List<String>> playlists;
public double peakMultiplier = 100.0;

@Deprecated /* use regular "listeners" from ServiceConfig parent */
public String[] audioListeners;


/**
* a multiplier to scale amplitude of output waveform
*/
public double peakMultiplier = 100.0;

/**
* sample interval for peak
*/
public double peakSampleInterval = 15;

/**
* delay to synchronize publishing of peak with actual sound in milliseconds
*/
Expand Down

0 comments on commit 820d0c4

Please sign in to comment.