Skip to content
This repository was archived by the owner on Jun 27, 2022. It is now read-only.

Commit 15c2426

Browse files
committed
Refactor example to show how to use a userData Object
1 parent d017f57 commit 15c2426

File tree

1 file changed

+40
-30
lines changed

1 file changed

+40
-30
lines changed

examples/pa_api_callback_sine.js

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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
1314
const sampleRate = 48000
1415
const channels = 2
1516
const 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
6148
function 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

Comments
 (0)