Skip to content

Commit

Permalink
refactor: Audio class approach
Browse files Browse the repository at this point in the history
  • Loading branch information
Alok-Sci committed Feb 20, 2024
1 parent 18971cc commit a13f3d2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ <h1 class="mx-auto w-auto fw-bolder">Morse Translator</h1>

</footer>
</div>
<audio src="beep.mp3" ></audio>
<!-- <audio src="" ></audio> -->


<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.2/js/bootstrap.min.js"
Expand Down
44 changes: 22 additions & 22 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ const morse = {
"@": ".--.-."
};

const morseAudio = {
'.': 'dot',
"-": 'dash'
}

function convertToMorse(string) {
console.log(string);

Expand Down Expand Up @@ -92,40 +87,45 @@ function copyMorse(morseString) {
function playMorse(morse) {
console.log('morse is', morse);

const audioEl = document.querySelector('audio');
const dotDuration = 500;
const dashDuration = 1000;
const spaceDuration = 2000;
const dotDuration = 80;
const dashDuration = 80;
const gap = 500;
const spaceDuration = 1000;

morse.split('').forEach((code, index) => {
if (code === ' ') {
setTimeout(() => {
console.log('space');
}, index * spaceDuration);
}

else if (code === '.') {
setTimeout(() => {
playDot(audioEl, dotDuration);
}, index * spaceDuration);
} else if (code === '-') {
playDot(dotDuration);
}, index * gap);
}

else if (code === '-') {
setTimeout(() => {
playDash(audioEl, dashDuration);
}, index * spaceDuration);
playDash(dashDuration);
}, index * gap);
}
});
}
function playDot(audioEl, duration) {


function playDot(duration) {
setTimeout(() => {
audioEl.currentTime = 0;
audioEl.play();
console.log('played dot');
const audio = new Audio('dot.mp3');
audio.play();
console.log('played dot', audio);
}, duration);
}

function playDash(audioEl, duration) {
function playDash(duration) {
setTimeout(() => {
audioEl.currentTime = 0;
audioEl.play();
console.log('played dash');
const audio = new Audio('dash.mp3')
audio.play();
console.log('played dash', audio);
}, duration);
}

0 comments on commit a13f3d2

Please sign in to comment.