@@ -10,6 +10,7 @@ http://portaudio.com/docs/v19-doxydocs/paex__sine_8c_source.html
1010*/
1111
1212// Setup
13+ const numSeconds = process . argv [ 2 ] || 5
1314const sampleRate = 48000
1415const channels = 2
1516const framesPerBuffer = 8192
@@ -27,41 +28,43 @@ const formats = {
2728}
2829
2930
30- //Create callback class that outputs a sinewave
31- class SineCallback {
32- constructor ( tableSize ) {
33- this . left_phase = 0
34- this . right_phase = 0
35- this . tableSize = tableSize
36- this . sine = [ ]
37-
38- // initialise sinusoidal wavetable
39- for ( var i = 0 ; i < tableSize ; i ++ )
40- {
41- this . sine [ i ] = Math . sin ( ( i / tableSize ) * Math . PI * 2 )
42- }
43- }
4431
45- callback ( input , output , frameCount ) {
46- var outputBufferView = new Float32Array ( output )
47-
48- for ( var i = 0 ; i < frameCount * 2 ; i += 2 )
49- {
50- outputBufferView [ i ] = this . sine [ this . left_phase ] // left
51- outputBufferView [ i + 1 ] = this . sine [ this . right_phase ] // right
52- this . left_phase += 1
53- if ( this . left_phase >= this . tableSize ) this . left_phase -= this . tableSize
54- this . right_phase += 3 // higher pitch so we can distinguish left and right.
55- if ( this . right_phase >= this . tableSize ) this . right_phase -= this . tableSize
56- }
32+ function callback ( input , output , frameCount , data ) {
33+ var outputBufferView = new Float32Array ( output )
34+
35+ for ( var i = 0 ; i < frameCount * 2 ; i += 2 )
36+ {
37+ outputBufferView [ i ] = data . sine [ data . left_phase ] // left
38+ outputBufferView [ i + 1 ] = data . sine [ data . right_phase ] // right
39+ data . left_phase += 1
40+ if ( data . left_phase >= data . tableSize ) data . left_phase -= data . tableSize
41+ data . right_phase += 3 // higher pitch so we can distinguish left and right.
42+ if ( data . right_phase >= data . tableSize ) data . right_phase -= data . tableSize
5743 }
44+ return 0
5845}
5946
6047//Main function
6148function callbackSine ( ) {
6249 // initialize stream instance
63- let stream = new JsPaStream ( ) ,
64- callback = new SineCallback ( 200 )
50+ let stream = new JsPaStream ( )
51+ // initialize data that is sent to callback
52+ let data = {
53+ left_phase : 0 ,
54+ right_phase : 0 ,
55+ tableSize : tableSize ,
56+ sine : [ ]
57+ }
58+ console . log (
59+ 'PortAudio Test: output sine wave\n' ,
60+ `SR = ${ sampleRate } , BufSize = ${ framesPerBuffer } \n`
61+ )
62+
63+ // initialise sinusoidal wavetable
64+ for ( var i = 0 ; i < tableSize ; i ++ )
65+ {
66+ data . sine [ i ] = Math . sin ( ( i / tableSize ) * Math . PI * 2 )
67+ }
6568
6669 // initialize PortAudio
6770 JsAudio . initialize ( )
@@ -74,12 +77,19 @@ function callbackSine () {
7477 sampleFormat : formats . paFloat32 ,
7578 numInputChannels : channels ,
7679 numOutputChannels : channels ,
77- callback : callback . callback . bind ( callback )
80+ callback : callback ,
81+ userData : data
7882 }
7983 // open stream with options
80- JsAudio . openDefaultStream ( streamOpts )
84+ console . log ( JsAudio . openDefaultStream ( streamOpts ) )
8185 // start stream
8286 JsAudio . startStream ( stream )
87+ // log what we're doing
88+ console . log ( `Play for ${ numSeconds } seconds.\n` )
89+ // stop stream when timeout fires
90+ //setTimeout(() => {
91+ //JsAudio.closeStream(stream)
92+ //}, numSeconds * 1000)
8393}
8494
8595// Run it
0 commit comments