@@ -10,6 +10,7 @@ http://portaudio.com/docs/v19-doxydocs/paex__sine_8c_source.html
10
10
*/
11
11
12
12
// Setup
13
+ const numSeconds = process . argv [ 2 ] || 5
13
14
const sampleRate = 48000
14
15
const channels = 2
15
16
const framesPerBuffer = 8192
@@ -27,41 +28,43 @@ const formats = {
27
28
}
28
29
29
30
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
- }
44
31
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
57
43
}
44
+ return 0
58
45
}
59
46
60
47
//Main function
61
48
function callbackSine ( ) {
62
49
// 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
+ }
65
68
66
69
// initialize PortAudio
67
70
JsAudio . initialize ( )
@@ -74,12 +77,19 @@ function callbackSine () {
74
77
sampleFormat : formats . paFloat32 ,
75
78
numInputChannels : channels ,
76
79
numOutputChannels : channels ,
77
- callback : callback . callback . bind ( callback )
80
+ callback : callback ,
81
+ userData : data
78
82
}
79
83
// open stream with options
80
- JsAudio . openDefaultStream ( streamOpts )
84
+ console . log ( JsAudio . openDefaultStream ( streamOpts ) )
81
85
// start stream
82
86
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)
83
93
}
84
94
85
95
// Run it
0 commit comments