Skip to content

Commit

Permalink
pirPlaySounds defaulted to false, AudioFile interface and playRandom
Browse files Browse the repository at this point in the history
  • Loading branch information
supertick committed May 4, 2024
1 parent 3c7f06b commit e327428
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 24 deletions.
32 changes: 16 additions & 16 deletions src/main/java/org/myrobotlab/audio/AudioProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import java.io.File;
import java.io.IOException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

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

Expand Down Expand Up @@ -36,8 +35,9 @@ 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);

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

protected int currentTrackCount = 0;

Expand Down Expand Up @@ -154,7 +154,6 @@ public AudioData play(AudioData data) {
audioFile.invoke("publishAudioStart", data);
AudioFileConfig config = (AudioFileConfig) audioFile.getConfig();


while (isPlaying && (nBytesRead = din.read(buffer, 0, buffer.length)) != -1) {
++cnt;
// byte[] goofy = new byte[4096];
Expand Down Expand Up @@ -237,19 +236,20 @@ public AudioData play(AudioData data) {
peak = abs;
}
}

final double value = peak * (double) audioFile.getPeakMultiplier();
// skew publish forwards in time

// skew publish forwards in time
if (audioFile.getConfig().peakDelayMs == null) {
audioFile.invoke("publishPeak", value);
} else {
delayScheduler.schedule(() -> audioFile.invoke("publishPeak", value), audioFile.getConfig().peakDelayMs, TimeUnit.MILLISECONDS);
audioFile.invoke("publishPeak", value);
} else {
delayScheduler.schedule(() -> audioFile.invoke("publishPeak", value), audioFile.getConfig().peakDelayMs, TimeUnit.MILLISECONDS);
}

// reset to 0 after millis
if (audioFile.getConfig().publishPeakResetDelayMs != null) {
delayScheduler.schedule(() -> audioFile.invoke("publishPeak", 0), audioFile.getConfig().peakDelayMs + audioFile.getConfig().publishPeakResetDelayMs, TimeUnit.MILLISECONDS);
delayScheduler.schedule(() -> audioFile.invoke("publishPeak", 0), audioFile.getConfig().peakDelayMs + audioFile.getConfig().publishPeakResetDelayMs,
TimeUnit.MILLISECONDS);
}
}
}
Expand All @@ -267,11 +267,11 @@ public AudioData play(AudioData data) {
// System.gc();

if (audioFile.getConfig().peakDelayMs == null) {
audioFile.invoke("publishPeak", 0);
} else {
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
17 changes: 15 additions & 2 deletions src/main/java/org/myrobotlab/service/AudioFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ public void playResource(String filename, Boolean isBlocking) {
play(getResourceDir() + File.separator + filename, isBlocking);
}

/**
* Stops all tracks stops all audio processing
*/
public void silence() {
// stop all tracks
for (Map.Entry<String, AudioProcessor> entry : processors.entrySet()) {
Expand Down Expand Up @@ -356,6 +359,9 @@ public AudioData waitFor(String filename, Object waitForMe) {
return data;
}

/**
* Stops the current track and audio processor
*/
public void stop() {
AudioProcessor ap = processors.get(currentTrack);
// dump the current song
Expand Down Expand Up @@ -593,6 +599,14 @@ public void onPlayAudioFile(String file) {

@Override
public void onPlayRandomAudioFile(String dir) {
playRandom(dir);
}

/**
* Plays a random audio file
* @param dir
*/
public void playRandom(String dir) {
File test = new File(dir);
if (!test.exists() || !test.isDirectory()) {
error("%s is not a valid dir");
Expand All @@ -611,8 +625,7 @@ public void onPlayRandomAudioFile(String dir) {
play(randomFile.getAbsolutePath());

}



public double getPeakMultiplier() {
return ((AudioFileConfig)config).peakMultiplier;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public class InMoov2Config extends ServiceConfig {
* data/InMoov2/sounds/pir-activated.mp3 sound located in
* data/InMoov2/sounds/pir-deactivated.mp3
*/
public boolean pirPlaySounds = true;
public boolean pirPlaySounds = false;

public boolean pirWakeUp = true;

Expand Down
37 changes: 34 additions & 3 deletions src/main/java/org/myrobotlab/service/interfaces/AudioControl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.myrobotlab.service.interfaces;

import org.myrobotlab.service.data.AudioData;

public interface AudioControl {

public void setVolume(double volume);
Expand All @@ -17,8 +19,37 @@ public interface AudioControl {
* @param dir
*/
public void onPlayRandomAudioFile(String dir);

/**
* Plays a random audio file in the given directory
* @param dir
*/
public void playRandom(String dir);

/**
* Plays an audio file
* @param filename
* @return
*/
public AudioData play(String filename);

/**
* Pause the currently playing audio file
*/
public void pause();

// pause
// resume
// interrupt ?
/**
* Resumes the current audio file
*/
public void resume();

/**
* stops all audio processors and all tracks
*/
public void silence();

/**
* stops the current selected track and audio processor
*/
public void stop();
}
12 changes: 10 additions & 2 deletions src/main/resources/resource/WebGui/app/service/views/Gpt3Gui.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@
<td>prefix</td><td><textarea rows="7" class="form-control" ng-model="service.config.prefix"></textarea></td>
</tr>
<tr>
<td>token</td><td><input type="password" class="form-control" ng-model="service.config.token" /></td>
<td>wake word</td><td><input type="text" class="form-control" ng-model="service.config.wakeWord" /></td>
</tr>
<tr>
<td>sleep word</td><td><input type="text" class="form-control" ng-model="service.config.sleepWord" /></td>
</tr>
<tr>
<td>api key</td><td><input type="password" class="form-control" ng-model="service.config.token" /></td>
</tr>
<tr>
<td>
Expand All @@ -48,13 +54,15 @@
</table>
</div>
</div>
<!--
<div class="row">
<form class="form-inline">
token<br/>
<input class="form-control" type="password" ng-model="service.config.token" placeholder="bot token" title="token">
<button class="btn btn-default" ng-click="msg.setToken(service.config.token);msg.save()">set token</button>
</form>
</div>
-->
<div class="row" ng-show="service.config.token">
<form class="form-inline">
text<br/>
Expand All @@ -79,4 +87,4 @@
</tr>
</tbody>
</table>
</div>
</div>

0 comments on commit e327428

Please sign in to comment.