Skip to content

Commit

Permalink
Play sound on record (#7)
Browse files Browse the repository at this point in the history
* play MP3 file on record

- hard coded file name
- minimum error check
- Update launch.json
- playAtRecord.mp3

* sleep 5s for MP3 file to load

* Read and play a WAV

- delay on start with W10 computers might be cancelled

* Refactoring with SoundPlayer Class

* Add *.csv in gitignore (but keep them in repo)

- git update-index --skip-worktree data.csv
- git update-index --skip-worktree marker.csv

* Delete playAtRecord.mp3

* v1.3.0
  • Loading branch information
DenisMot committed Aug 17, 2023
1 parent f3d9044 commit 6461fad
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,6 @@ Temporary Items

old/
*.dylib

# do not track output files
*.csv
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//"args": "-screenDiagonal 356 -task linear -halfPeriod 1750 -tabletSize 311x216=62200x43200"
//"args": "-screenDiagonal 356 -task linear -halfPeriod 1750 -tabletSize 166x100=16588x10000 "
//"args": "-screenDiagonal 356 -task circular -halfPeriod 1750 -tabletSize 311x216=62200x43200 -circlePerimeter_mm 300 -indexOfDifficulty 10000"
"args": "-screenDiagonal 356 -task linear -halfPeriod 1750 -tabletSize 311x216=62200x43200"
//"args": "-screenDiagonal 356 -task linear -halfPeriod 1750 -tabletSize 311x216=62200x43200"

// "args": "-screenDiagonal 356 -task linear -halfPeriod 1750 "
// "args": "-screenDiagonal 356 -task linear -halfPeriod 1750"
Expand Down
Binary file added playAtRecord.wav
Binary file not shown.
17 changes: 10 additions & 7 deletions src/fr/lgi2p/digit/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
//import fr.lgi2p.digit.LSL.simple.LSLSendData;
import fr.lgi2p.digit.conf.Configuration;
import fr.lgi2p.digit.conf.Consts;
import fr.lgi2p.digit.output.OutputMouse;
import fr.lgi2p.digit.output.*;
import fr.lgi2p.digit.ui.DisplayTask;
import fr.lgi2p.digit.ui.PerformanceAtTask;
import fr.lgi2p.digit.util.Util;

public final class MainWindow implements MouseMotionListener, MouseListener, KeyListener, ActionListener {

SoundPlayer soundPlayer;

Calibration calibration;

static enum CyclingStatus {
Expand Down Expand Up @@ -103,10 +105,11 @@ public static MainWindow getInstance() {
return instance;
}


private MainWindow(Configuration configuration) {
this.configuration = configuration;
//this.outputMouse = new OutputMouse(configuration);
this.outputMouse = getOutputMouse();
this.outputMouse = new OutputMouse(configuration);
this.soundPlayer = new SoundPlayer();

macOsSpecification();
}
Expand Down Expand Up @@ -289,13 +292,13 @@ private void DoCycleChange() {
if (NbRecDone <= NbRestDone) {
outputMouse.writeMarker("DoCycleChange:DoRecord" + Message);
outputMouse.writeNumericMarker(RECORD_MARKER);
soundPlayer.start();
actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "record"));
} else {
outputMouse.writeMarker("DoCycleChange:DoPause" + Message);
outputMouse.writeNumericMarker(PAUSE_MARKER); // marker for sync with external devices reading only
// integers
outputMouse.writeData(0L, 0, 0, false); // show the end of a record with an "all zero line" to ease
// parsing the output file
outputMouse.writeNumericMarker(PAUSE_MARKER); // marker for external devices reading only integers
outputMouse.writeData(0L, 0, 0, false); // show the end of a record with an "all zero line"
soundPlayer.stop();
actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "pause"));
}
}
Expand Down
48 changes: 48 additions & 0 deletions src/fr/lgi2p/digit/output/SoundPlayer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package fr.lgi2p.digit.output;

import java.io.File;
import javax.sound.sampled.*;

public class SoundPlayer {
String fname = "playAtRecord.wav";
Clip soundPlayer = null;
File audioFile = null;

public SoundPlayer() {
this.audioFile = new File(fname);

if(audioFile.exists() && !audioFile.isDirectory()) {
System.out.println("playAtRecord.wav found... ");
try {
soundPlayer = AudioSystem.getClip();
AudioInputStream audioIn = AudioSystem.getAudioInputStream(this.audioFile);
soundPlayer.open(audioIn);

System.out.println("playAtRecord.wav is loading... ");
double secondsInAudio = (double) soundPlayer.getMicrosecondLength() / 1000000.0;
System.out.println("playAtRecord.wav lasts " + secondsInAudio + " seconds");
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}

public void start(){
if (soundPlayer != null) {
soundPlayer.setMicrosecondPosition(0);
soundPlayer.start();
}
}

public void stop(){
if (soundPlayer != null) {
soundPlayer.stop();
}
}

public void resume(){
if (soundPlayer != null) {
soundPlayer.start();
}
}
}

0 comments on commit 6461fad

Please sign in to comment.