Skip to content

Commit

Permalink
Merge pull request #22 from OpenEarable/1.3.0
Browse files Browse the repository at this point in the history
1.3.0
  • Loading branch information
TobiasRoeddiger authored Oct 5, 2023
2 parents 7e71c9e + bdc3638 commit 9a50c22
Show file tree
Hide file tree
Showing 3 changed files with 301 additions and 257 deletions.
243 changes: 135 additions & 108 deletions assets/js/AudioManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,119 +5,146 @@ $(document).ready(function() {
PAUSE: 2,
STOP: 3
};

// WAV file controls
$("#buttonPlayWave").click(function() {
var fileName = $('#fileNameInput').val();

if (fileName === "") {
log("File name is empty. If a file is already playing, this command will stop the current audio.", "WARNING");
} else if (!fileName.endsWith('.wav')) {
log("File name is missing the '.wav' ending. Make sure it's correct!", "ERROR");
return;
function getSelectedAudioType() {
if ($("#file").is(":checked")) {
return "file";
} else if ($("#jingle").is(":checked")) {
return "jingle";
} else if ($("#frequency").is(":checked")) {
return "frequency";
}

log("Sending 'Play' with file name '" + fileName + "'.", "MESSAGE");
try {
openEarable.audioPlayer.wavFile(AUDIO_STATE.PLAY, fileName);
} catch (error) {
log("Error occurred while trying to play: " + error, "ERROR");
}
});

$("#buttonPauseWave").click(function() {
log("Sending 'Pause' command.", "MESSAGE");
try {
openEarable.audioPlayer.wavFile(AUDIO_STATE.PAUSE);
} catch (error) {
log("Error occurred while trying to pause: " + error, "ERROR");
}
});

$("#buttonStopWave").click(function() {
log("Sending 'Stop' command.", "MESSAGE");
try {
openEarable.audioPlayer.wavFile(AUDIO_STATE.STOP);
} catch (error) {
log("Error occurred while trying to stop: " + error, "ERROR");
return null;
}

$("#button-play-audio").click(function() {
let audioType = getSelectedAudioType();

if (audioType === "file") {
// WAV file play logic
var fileName = $('#fileNameInput').val();

if (fileName === "") {
log("File name is empty. If a file is already playing, this command will stop the current audio.", "WARNING");
} else if (!fileName.endsWith('.wav')) {
log("File name is missing the '.wav' ending. Make sure it's correct!", "ERROR");
return;
}

log("Sending 'Play' with file name '" + fileName + "'.", "MESSAGE");
try {
openEarable.audioPlayer.wavFile(AUDIO_STATE.PLAY, fileName);
} catch (error) {
log("Error occurred while trying to play: " + error, "ERROR");
}

} else if (audioType === "jingle") {
// Jingle play logic
var jingleType = $('#jingleSelect').val();
log("Sending 'Play' with jingle type '" + jingleType + "'.", "MESSAGE");
try {
openEarable.audioPlayer.jingle(AUDIO_STATE.PLAY, jingleType);
} catch (error) {
log("Error occurred while trying to play jingle: " + error, "ERROR");
}

} else if (audioType === "frequency") {
// Frequency play logic
var frequencyValue = parseFloat($("#frequencyNumberSelector").val());
var loudnessValue = parseFloat($("#loudnessInput").val()) / 100;
var waveType = parseInt($('#waveTypeSelect').val());

if (!frequencyValue) {
log("Frequency value is not valid.", "ERROR");
return;
}

log("Sending 'Play' with frequency value '" + frequencyValue + "' Hz, wave type '" + waveType + "', and loudness '" + loudnessValue + "'.", "MESSAGE");
try {
openEarable.audioPlayer.frequency(AUDIO_STATE.PLAY, waveType, frequencyValue, loudnessValue);
} catch (error) {
log("Error occurred while trying to play frequency: " + error, "ERROR");
}
}
});

// Jingle controls
$("#buttonPlayJingle").click(function() {
var jingleType = $('#jingleSelect').val();
log("Sending 'Play' with jingle type '" + jingleType + "'.", "MESSAGE");
try {
openEarable.audioPlayer.jingle(AUDIO_STATE.PLAY, jingleType);
} catch (error) {
log("Error occurred while trying to play jingle: " + error, "ERROR");
}
});

$("#buttonPauseJingle").click(function() {
var jingleType = $('#jingleSelect').val();

$("#button-pause-audio").click(function() {
let audioType = getSelectedAudioType();

log("Sending 'Pause' command.", "MESSAGE");
try {
openEarable.audioPlayer.jingle(AUDIO_STATE.PAUSE, jingleType);
} catch (error) {
log("Error occurred while trying to pause jingle: " + error, "ERROR");
}
});

$("#buttonStopJingle").click(function() {
var jingleType = $('#jingleSelect').val();

log("Sending 'Stop' command.", "MESSAGE");
try {
openEarable.audioPlayer.jingle(AUDIO_STATE.STOP, jingleType);
} catch (error) {
log("Error occurred while trying to stop jingle: " + error, "ERROR");

if (audioType === "file") {
var fileName = $('#fileNameInput').val();
// WAV file pause logic
log("Sending 'Pause' command.", "MESSAGE");
try {
openEarable.audioPlayer.wavFile(AUDIO_STATE.PAUSE, fileName);
} catch (error) {
log("Error occurred while trying to pause: " + error, "ERROR");
}

} else if (audioType === "jingle") {
// Jingle pause logic
var jingleType = $('#jingleSelect').val();
log("Sending 'Pause' command.", "MESSAGE");
try {
openEarable.audioPlayer.jingle(AUDIO_STATE.PAUSE, jingleType);
} catch (error) {
log("Error occurred while trying to pause jingle: " + error, "ERROR");
}

} else if (audioType === "frequency") {
// Frequency pause logic
log("Sending 'Pause' command.", "MESSAGE");

var frequencyValue = parseFloat($("#frequencyNumberSelector").val());
var waveType = parseInt($('#waveTypeSelect').val());
var loudnessValue = parseFloat($("#loudnessInput").val()) / 100;

try {
openEarable.audioPlayer.frequency(AUDIO_STATE.PAUSE, waveType, frequencyValue, loudnessValue);
} catch (error) {
log("Error occurred while trying to pause frequency: " + error, "ERROR");
}
}
});

// Frequency controls
$("#buttonPlayFrequency").click(function() {
var frequencyValue = parseFloat($("#frequencyNumberSelector").val());
var loudnessValue = parseFloat($("#loudnessInput").val());
var waveType = parseInt($('#waveTypeSelect').val());

if (!frequencyValue) {
log("Frequency value is not valid.", "ERROR");
return;
}

log("Sending 'Play' with frequency value '" + frequencyValue + "' Hz, wave type '" + waveType + "', and loudness '" + loudnessValue + "'.", "MESSAGE");
try {
openEarable.audioPlayer.frequency(AUDIO_STATE.PLAY, waveType, frequencyValue, loudnessValue);
} catch (error) {
log("Error occurred while trying to play frequency: " + error, "ERROR");
}
});

$("#buttonPauseFrequency").click(function() {
log("Sending 'Pause' command.", "MESSAGE");

var frequencyValue = parseFloat($("#frequencyNumberSelector").val());
var waveType = parseInt($('#waveTypeSelect').val());

try {
openEarable.audioPlayer.frequency(AUDIO_STATE.PAUSE, waveType, frequencyValue);
} catch (error) {
log("Error occurred while trying to pause frequency: " + error, "ERROR");
}
});

$("#buttonStopFrequency").click(function() {
log("Sending 'Stop' command.", "MESSAGE");

var frequencyValue = parseFloat($("#frequencyNumberSelector").val());
var waveType = parseInt($('#waveTypeSelect').val());

try {
openEarable.audioPlayer.frequency(AUDIO_STATE.STOP, waveType, frequencyValue);
} catch (error) {
log("Error occurred while trying to stop frequency: " + error, "ERROR");

$("#button-stop-audio").click(function() {
let audioType = getSelectedAudioType();
if (audioType === "file") {
var fileName = $('#fileNameInput').val();

// WAV file stop logic
log("Sending 'Stop' command.", "MESSAGE");

try {
openEarable.audioPlayer.wavFile(AUDIO_STATE.STOP, fileName);
} catch (error) {
log("Error occurred while trying to stop: " + error, "ERROR");
}

} else if (audioType === "jingle") {
// Jingle stop logic
var jingleType = $('#jingleSelect').val();

log("Sending 'Stop' command.", "MESSAGE");
try {
openEarable.audioPlayer.jingle(AUDIO_STATE.STOP, jingleType);
} catch (error) {
log("Error occurred while trying to stop jingle: " + error, "ERROR");
}

} else if (audioType === "frequency") {
// Frequency stop logic
log("Sending 'Stop' command.", "MESSAGE");

var frequencyValue = parseFloat($("#frequencyNumberSelector").val());
var waveType = parseInt($('#waveTypeSelect').val());
var loudnessValue = parseFloat($("#loudnessInput").val()) / 100;

try {
openEarable.audioPlayer.frequency(AUDIO_STATE.STOP, waveType, frequencyValue, loudnessValue);
} catch (error) {
log("Error occurred while trying to stop frequency: " + error, "ERROR");
}
}
});
});
});
11 changes: 6 additions & 5 deletions assets/js/OpenEarable.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ const JINGLE = {
ALARM: 4,
PING: 5,
OPEN: 6,
CLOSE: 7
CLOSE: 7,
CLICK: 8
};

/**
Expand All @@ -46,8 +47,8 @@ const JINGLE = {
const WAVE_TYPE = {
IDLE: 0,
SINE: 1,
TRIANGLE: 2,
SQUARE: 3,
SQUARE: 2,
TRIANGLE: 3,
SAW: 4
};

Expand Down Expand Up @@ -623,15 +624,15 @@ class AudioPlayer {
async frequency(state, waveType, frequency, loudness) {
try {
let type = 2; // 2 indicates it's a frequency
let data = new Uint8Array(8);
let data = new Uint8Array(12);
data[0] = type;
data[1] = state;
data[2] = waveType;

let freqBytes = new Float32Array([frequency]);
let loudnessBytes = new Float32Array([loudness]);
data.set(new Uint8Array(freqBytes.buffer), 3);
data.set(new Uint8Array(loudnessBytes.buffer), 4);
data.set(new Uint8Array(loudnessBytes.buffer), 7);

await this.bleManager.writeCharacteristic(
SERVICES.AUDIO_SERVICE.UUID,
Expand Down
Loading

0 comments on commit 9a50c22

Please sign in to comment.