Jump to content

For a couple of days I have been working on a text to NATO alphabet translator (the Alpha, Bravo, Charlie stuff) and after a little I wanted to add morse code too, so that is implemented now too and now I want to be able to play the morse code too.

Either way, everything works, except for the playing of the morse code. The first character plays and after that it stops, even though it should play (the code does reach all the next sounds), so uhh, I will just show what I am working with:

//MorseSound.js for index.php

//The sound clips
var Short = new Audio('short.mp3');
var Long = new Audio('long.mp3');

//Many function not relevant now

//The functions above take the string of morse code (dots and dashes) and goes over them one by one and see if it is a dash or dot and after that goes to the PlaySound() function so it will play.
//This function work, the trouble begins later
function TranslateToSound()
{
	for (var e = 0; e < MorseArray.length + 1; e++)
	{
		//I do see all these console logs in the console, screenshot provided below
		if (MorseArray[e] == ".")
		{
			PlaySound(Short);
			console.log("Short");
		}
		if (MorseArray[e] == "-")
		{
			PlaySound(Long);
			console.log("Long");
		}
	}
}

function PlaySound(Type)
{
  	//This code only plays one sound and after that goes quiet, even though it still gets accesed.
  	//I have determined the problem must lie here (or in the entire HTML5 audio thing, as it is not meant for this type of stuff..
	Type.play();
}

 

qG7s73f.png

As you can see the morse code is three dots (short), three dashes (long), three dots (short) and it gets logged, but only one time the sound gets played (whichever is the first one)

If I do a console.log(Type) in the last function, I will see all mp3 clips get accessed properly too. 

 

If I put an interval of two seconds in TranslateToSound() function it still doesn't work.

 

Anybody got any ideas?

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
https://linustechtips.com/topic/566022-javascripthtml5-audio-help/
Share on other sites

Link to post
Share on other sites

12 minutes ago, Minibois said:

For a couple of days I have been working on a text to NATO alphabet translator (the Alpha, Bravo, Charlie stuff) and after a little I wanted to add morse code too, so that is implemented now too and now I want to be able to play the morse code too.

Either way, everything works, except for the playing of the morse code. The first character plays and after that it stops, even though it should play (the code does reach all the next sounds), so uhh, I will just show what I am working with:


//MorseSound.js for index.php

//The sound clips
var Short = new Audio('short.mp3');
var Long = new Audio('long.mp3');

//Many function not relevant now

//The functions above take the string of morse code (dots and dashes) and goes over them one by one and see if it is a dash or dot and after that goes to the PlaySound() function so it will play.
//This function work, the trouble begins later
function TranslateToSound()
{
	for (var e = 0; e < MorseArray.length + 1; e++)
	{
		//I do see all these console logs in the console, screenshot provided below
		if (MorseArray[e] == ".")
		{
			PlaySound(Short);
			console.log("Short");
		}
		if (MorseArray[e] == "-")
		{
			PlaySound(Long);
			console.log("Long");
		}
	}
}

function PlaySound(Type)
{
  	//This code only plays one sound and after that goes quiet, even though it still gets accesed.
  	//I have determined the problem must lie here (or in the entire HTML5 audio thing, as it is not meant for this type of stuff..
	Type.play();
}

 

qG7s73f.png

As you can see the morse code is three dots (short), three dashes (long), three dots (short) and it gets logged, but only one time the sound gets played (whichever is the first one)

If I do a console.log(Type) in the last function, I will see all mp3 clips get accessed properly too. 

 

If I put an interval of two seconds in TranslateToSound() function it still doesn't work.

 

Anybody got any ideas?

How long exactly are the MP3s?

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to post
Share on other sites

2 minutes ago, Stardar1 said:

How long exactly are the MP3s?

They are both 1 second long and the short one is 11,2KB and the long one is 12,4KB

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to post
Share on other sites

3 minutes ago, Minibois said:

They are both 1 second long and the short one is 11,2KB and the long one is 12,4KB

I have not used Javascript before, but I have used Java and other languages. 

 

describe "Type.play()" for me

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to post
Share on other sites

Or try this, do Type.play (or start), wait 1 second, then somehow STOP it

 

 

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to post
Share on other sites

6 minutes ago, Stardar1 said:

I have not used Javascript before, but I have used Java and other languages. 

 

describe "Type.play()" for me

 

5 minutes ago, Stardar1 said:

Or try this, do Type.play (or start), wait 1 second, then somehow STOP it

 

 

I tried pausing the sound (the .pause() function) but it didn't fix anything.

Meh, I'll check it out tomorrow with a fresh mind again and see if I can think of something else

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to post
Share on other sites

Just now, Minibois said:

 

I tried pausing the sound (the .pause() function) but it didn't fix anything.

Meh, I'll check it out tomorrow with a fresh mind again and see if I can think of something else

can you use .start() and .stop()?

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to post
Share on other sites

3 minutes ago, Stardar1 said:

can you use .start() and .stop()?

There is no stop function sadly

http://www.w3schools.com/tags/ref_av_dom.asp 

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to post
Share on other sites

Just now, Minibois said:

There is no stop function sadly

http://www.w3schools.com/tags/ref_av_dom.asp 

Try doing a load before a play. 

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to post
Share on other sites

6 minutes ago, Stardar1 said:

Try doing a load before a play. 

function PlaySound(Type)
{
	Type.load();
	Type.play();
}

I did this and it didn't change anything :( 

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to post
Share on other sites

Just now, Minibois said:

function PlaySound(Type)
{
	Type.load();
	Type.play();
}

I did this and it didn't change anything :( 

load, play, pause?

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to post
Share on other sites

4 minutes ago, Stardar1 said:

load, play, pause?

I tried this:

function PlaySound(Type)
{
	Type.load();
	Type.play();
	TypeNow = Type;
	setTimeout(StopSound,3000);
}

function StopSound
{
	TypeNow.pause();
}

But now no sound is playing

(Javascript doesn't have a simple sleep function, so it has to be done like this. That number is in milliseconds and I tried 1000 (1 second), but also a couple of other numbers just to see what works 

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to post
Share on other sites

Just now, Minibois said:

I tried this:


function PlaySound(Type)
{
	Type.load();
	Type.play();
	TypeNow = Type;
	setTimeout(StopSound,3000);
}

function StopSound
{
	TypeNow.pause();
}

But now no sound is playing

(Javascript doesn't have a simple sleep function, so it has to be done like this. That number is in milliseconds and I tried 1000 (1 second), but also a couple of other numbers just to see what works 

sleep functions are more needed now, and rarer and rarer. 

 

there's no unload...

 

I have no idea...

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×