Trigger JS Function inside Procedure ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Arbrakaan
Posts: 91
Joined: Mon Feb 24, 2014 10:54 pm
Location: Geneva
Contact:

Trigger JS Function inside Procedure ?

Post by Arbrakaan »

There is a way to trigger a JS function inside a procedure ?

Code: Select all

Procedure TimerEvents()
  Debug "Timer event: " + EventTimer()
  !maFunc(arg1,arg2);
 EndProcedure
  
  If OpenWindow(#Window_0, 0, 0, 800, 120, "Timer example",  #PB_Window_Background)
    
    AddWindowTimer(#Window_0, 0, 1000)
    
    BindEvent(#PB_Event_Timer, @TimerEvents(),#PB_All)
  EndIf
Fred
Site Admin
Posts: 1820
Joined: Mon Feb 24, 2014 10:51 am

Re: Trigger JS Function inside Procedure ?

Post by Fred »

Looks fine to me, do you get an error ?
User avatar
Arbrakaan
Posts: 91
Joined: Mon Feb 24, 2014 10:54 pm
Location: Geneva
Contact:

Re: Trigger JS Function inside Procedure ?

Post by Arbrakaan »

Thanks for the quick response !

I have this error : ReferenceError: play_buffersource is not defined

The scope of the function is not right, but how to change that into Global ?


Here is my current code :

Code: Select all

Enumeration
  #Window_0
  #Window_Master
EndEnumeration

ExamineDesktops()
InitSound()

EnableJS

var SAMPLE_RATE = 44100;
var FRQ = 440
var PI_2 = Math.PI * 2;


function play_buffersource(SAMPLE_RATE,FRQ) {
	if (! window.AudioContext) {
		if (! window.webkitAudioContext) {
			bad_browser();
			return;
		}
		window.AudioContext = window.webkitAudioContext;
	}

	var ctx = new AudioContext();
	var buffer = ctx.createBuffer(1, 2048, 44100);

	var buf = buffer.getChannelData(0);

	for (i = 0; i < 2048; ++i) {
		buf[i] = Math.sin(FRQ * PI_2 * i / SAMPLE_RATE);
	}

	var node = ctx.createBufferSource(0);
	node.buffer = buffer;
	node.connect(ctx.destination);
	node.start(ctx.currentTime + 0.1);

}

function cb(evt) {
	var buffer = evt.outputBuffer;

	for (var j = 0; j < buffer.numberOfChannels; ++j) {
		var buf = evt.outputBuffer.getChannelData(j);

		for (i = 0; i < buf.length; ++i) {
			f = [Math.sin, Math.cos][j % 2];
			buf[i] = Math.sin(440 * PI_2 * (cb.n + i) / SAMPLE_RATE) *
				f(2 * PI_2 * (cb.n + i) / SAMPLE_RATE);
		}
	}

	cb.n += i;
	cb.n % SAMPLE_RATE;

	if (cb.count++ > 10) {
		cb.node.disconnect();
	}
}

function play_jssource() {
	if (! window.AudioContext) {
		if (! window.webkitAudioContext) {
			bad_browser();
			return;
		}
		window.AudioContext = window.webkitAudioContext;
	}

	ctx = new AudioContext();

	var node = ctx.createScriptProcessor(16384, 0, 1);
	node.onaudioprocess = cb;
	node.connect(ctx.destination);

	cb.n = 0;
	cb.count = 0;
	cb.node = node;
	}

	DisableJS
	
	
	procedure music()
	  ;for i=0 to i=100
    ;next
	  ;!play_buffersource(44100,640);
    ;RemoveWindowTimer(0, 0)
	  Debug "Timer event: " + EventTimer()

	endprocedure

	
Procedure TimerEvents()
  ;Debug "Timer event: " + EventTimer()
  ;!play_buffersource(44100,640);
 EndProcedure
  
  If OpenWindow(#Window_0, 10, DesktopHeight(0)-180, 800, 120, "Timer example",  #PB_Window_TitleBar | #PB_Window_SizeGadget)
    AddWindowTimer(#Window_0, 0, 1000) ; First timer every seconds
    
    
    ContainerGadget(0, 10, 8, 300, 133, #PB_Container_Raised)
        TrackBarGadget(2, 10, 2, 40, 80, 0, 10000, #PB_TrackBar_Vertical)
        SetGadgetState(2, 8000)
        ButtonGadget(1, 10, 90, 15, 15, "x")
        CloseGadgetList()
        
     ContainerGadget(1, 320, 8, 300, 133, #PB_Container_Raised)
        TrackBarGadget(3, 10, 2, 20, 80, 0, 10000, #PB_TrackBar_Vertical)
        SetGadgetState(3, 8000)
        ButtonGadget(4, 10, 90, 15, 15, "x")
     CloseGadgetList()
      
      
    !play_buffersource(44100,640);
    BindEvent(#PB_Event_Timer, @TimerEvents(),#PB_All)
  EndIf
  
  
  
  If OpenWindow(#Window_Master, 50, 50, 600, 600, "Mixer", #PB_Window_TitleBar)
      StickyWindow(#Window_Master, 1) 
      
  endif


Fred
Site Admin
Posts: 1820
Joined: Mon Feb 24, 2014 10:51 am

Re: Trigger JS Function inside Procedure ?

Post by Fred »

Looks OK, I get a sound when running it from Chrome. (I had to uncheck "Enable Case correction" in The Preferences/Editor/Editing, or the JS will be wrong because of the "if" -> "If" etc.
Post Reply