Javascript script not loading

Using Javascript from SpiderBasic
Andy
Posts: 46
Joined: Sat Feb 15, 2020 5:19 pm
Location: Larnaca, Cyprus
Contact:

Javascript script not loading

Post by Andy »

Hi all
I am trying to use the Tonejs audio framework but it does not seem to load. I get an error telling me Tone is not defined. Can anyone help?

Code: Select all

! require(["https://cdnjs.cloudflare.com/ajax/libs/tone/14.8.49/Tone.min.js"]);

Procedure ButtonHandler()
  
  ! Tone.start();
  
EndProcedure

OpenWindow(0, 100, 100, 200, 50, "Click test", #PB_Window_SystemMenu)
ButtonGadget(0, 10, 10, 180, 30, "Click me")

BindGadgetEvent(0, @ButtonHandler())
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Javascript script not loading

Post by Peter »

Here's how it works:

Code: Select all

Procedure ButtonHandler()
  
  ! //create a synth and connect it to the main output (your speakers)
  ! const synth = new Tone.Synth().toDestination();
  
  ! //play a middle 'C' for the duration of an 8th note
  ! synth.triggerAttackRelease("C4", "8n");
  
EndProcedure

Procedure Main()
  
  OpenWindow(0, 100, 100, 200, 50, "Click test", #PB_Window_SystemMenu)
  ButtonGadget(0, 10, 10, 180, 30, "Click me")
  
  BindGadgetEvent(0, @ButtonHandler())
  
EndProcedure

! require(["https://cdnjs.cloudflare.com/ajax/libs/tone/14.8.49/Tone.min.js"], function(t) {
!   Tone = t;
Main()
! });
Andy
Posts: 46
Joined: Sat Feb 15, 2020 5:19 pm
Location: Larnaca, Cyprus
Contact:

Re: Javascript script not loading

Post by Andy »

Hi Peter, thanks for the solution. Can you tell me how you figured that out?

Regards.
Post Reply