generated from napi-rs/package-template
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { AudioContext } from '../index.mjs'; | ||
|
||
const latencyHint = process.env.WEB_AUDIO_LATENCY === 'playback' ? 'playback' : 'interactive'; | ||
const audioContext = new AudioContext({ latencyHint }); | ||
|
||
audioContext.listener.positionZ.value = 1; | ||
audioContext.listener.positionX.value = -10; | ||
audioContext.listener.positionX.linearRampToValueAtTime(10, 4); | ||
|
||
const osc = audioContext.createOscillator(); | ||
const panner = audioContext.createPanner(); | ||
osc.connect(panner); | ||
panner.connect(audioContext.destination); | ||
osc.start(); | ||
|
||
let direction = 1; | ||
setInterval(function loop() { | ||
console.log(audioContext.listener.positionX.value); | ||
if (Math.abs(audioContext.listener.positionX.value) >= 10.) { | ||
direction *= -1; | ||
const now = audioContext.currentTime; | ||
audioContext.listener.positionX.linearRampToValueAtTime(10 * direction, now + 4); | ||
} | ||
}, 500); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
// @TODO | ||
// This should be generated as any other AudioNode | ||
|
||
use crate::*; | ||
use napi::*; | ||
use napi_derive::js_function; | ||
use web_audio_api::AudioListener; | ||
|
||
pub struct NapiAudioListener(AudioListener); | ||
|
||
impl NapiAudioListener { | ||
pub fn new(audio_listener: AudioListener) -> Self { | ||
Self(audio_listener) | ||
} | ||
|
||
pub fn create_js_object(env: &Env) -> Result<JsObject> { | ||
let mut obj = env.create_object()?; | ||
obj.define_properties(&[ | ||
Property::new("Symbol.toStringTag")? | ||
.with_value(&env.create_string("AudioListener")?) | ||
.with_property_attributes(PropertyAttributes::Static), | ||
Property::new("positionX")?.with_getter(get_position_x), | ||
Property::new("positionY")?.with_getter(get_position_y), | ||
Property::new("positionZ")?.with_getter(get_position_z), | ||
Property::new("forwardX")?.with_getter(get_forward_x), | ||
Property::new("forwardY")?.with_getter(get_forward_y), | ||
Property::new("forwardZ")?.with_getter(get_forward_z), | ||
Property::new("upX")?.with_getter(get_up_x), | ||
Property::new("upY")?.with_getter(get_up_y), | ||
Property::new("upZ")?.with_getter(get_up_z), | ||
])?; | ||
|
||
Ok(obj) | ||
} | ||
|
||
pub fn unwrap(&self) -> &AudioListener { | ||
&self.0 | ||
} | ||
} | ||
|
||
#[js_function] | ||
fn get_position_x(ctx: CallContext) -> Result<JsObject> { | ||
let js_this = ctx.this_unchecked::<JsObject>(); | ||
let napi_node = ctx.env.unwrap::<NapiAudioListener>(&js_this)?; | ||
let node = napi_node.unwrap(); | ||
|
||
let native_param = node.position_x().clone(); | ||
let napi_param = NapiAudioParam::new(native_param); | ||
let mut js_obj = NapiAudioParam::create_js_object(ctx.env)?; | ||
ctx.env.wrap(&mut js_obj, napi_param)?; | ||
Ok(js_obj) | ||
} | ||
|
||
#[js_function] | ||
fn get_position_y(ctx: CallContext) -> Result<JsObject> { | ||
let js_this = ctx.this_unchecked::<JsObject>(); | ||
let napi_node = ctx.env.unwrap::<NapiAudioListener>(&js_this)?; | ||
let node = napi_node.unwrap(); | ||
|
||
let native_param = node.position_y().clone(); | ||
let napi_param = NapiAudioParam::new(native_param); | ||
let mut js_obj = NapiAudioParam::create_js_object(ctx.env)?; | ||
ctx.env.wrap(&mut js_obj, napi_param)?; | ||
Ok(js_obj) | ||
} | ||
|
||
#[js_function] | ||
fn get_position_z(ctx: CallContext) -> Result<JsObject> { | ||
let js_this = ctx.this_unchecked::<JsObject>(); | ||
let napi_node = ctx.env.unwrap::<NapiAudioListener>(&js_this)?; | ||
let node = napi_node.unwrap(); | ||
|
||
let native_param = node.position_z().clone(); | ||
let napi_param = NapiAudioParam::new(native_param); | ||
let mut js_obj = NapiAudioParam::create_js_object(ctx.env)?; | ||
ctx.env.wrap(&mut js_obj, napi_param)?; | ||
Ok(js_obj) | ||
} | ||
|
||
#[js_function] | ||
fn get_forward_x(ctx: CallContext) -> Result<JsObject> { | ||
let js_this = ctx.this_unchecked::<JsObject>(); | ||
let napi_node = ctx.env.unwrap::<NapiAudioListener>(&js_this)?; | ||
let node = napi_node.unwrap(); | ||
|
||
let native_param = node.forward_x().clone(); | ||
let napi_param = NapiAudioParam::new(native_param); | ||
let mut js_obj = NapiAudioParam::create_js_object(ctx.env)?; | ||
ctx.env.wrap(&mut js_obj, napi_param)?; | ||
Ok(js_obj) | ||
} | ||
|
||
#[js_function] | ||
fn get_forward_y(ctx: CallContext) -> Result<JsObject> { | ||
let js_this = ctx.this_unchecked::<JsObject>(); | ||
let napi_node = ctx.env.unwrap::<NapiAudioListener>(&js_this)?; | ||
let node = napi_node.unwrap(); | ||
|
||
let native_param = node.forward_y().clone(); | ||
let napi_param = NapiAudioParam::new(native_param); | ||
let mut js_obj = NapiAudioParam::create_js_object(ctx.env)?; | ||
ctx.env.wrap(&mut js_obj, napi_param)?; | ||
Ok(js_obj) | ||
} | ||
|
||
#[js_function] | ||
fn get_forward_z(ctx: CallContext) -> Result<JsObject> { | ||
let js_this = ctx.this_unchecked::<JsObject>(); | ||
let napi_node = ctx.env.unwrap::<NapiAudioListener>(&js_this)?; | ||
let node = napi_node.unwrap(); | ||
|
||
let native_param = node.forward_z().clone(); | ||
let napi_param = NapiAudioParam::new(native_param); | ||
let mut js_obj = NapiAudioParam::create_js_object(ctx.env)?; | ||
ctx.env.wrap(&mut js_obj, napi_param)?; | ||
Ok(js_obj) | ||
} | ||
|
||
#[js_function] | ||
fn get_up_x(ctx: CallContext) -> Result<JsObject> { | ||
let js_this = ctx.this_unchecked::<JsObject>(); | ||
let napi_node = ctx.env.unwrap::<NapiAudioListener>(&js_this)?; | ||
let node = napi_node.unwrap(); | ||
|
||
let native_param = node.up_x().clone(); | ||
let napi_param = NapiAudioParam::new(native_param); | ||
let mut js_obj = NapiAudioParam::create_js_object(ctx.env)?; | ||
ctx.env.wrap(&mut js_obj, napi_param)?; | ||
Ok(js_obj) | ||
} | ||
|
||
#[js_function] | ||
fn get_up_y(ctx: CallContext) -> Result<JsObject> { | ||
let js_this = ctx.this_unchecked::<JsObject>(); | ||
let napi_node = ctx.env.unwrap::<NapiAudioListener>(&js_this)?; | ||
let node = napi_node.unwrap(); | ||
|
||
let native_param = node.up_y().clone(); | ||
let napi_param = NapiAudioParam::new(native_param); | ||
let mut js_obj = NapiAudioParam::create_js_object(ctx.env)?; | ||
ctx.env.wrap(&mut js_obj, napi_param)?; | ||
Ok(js_obj) | ||
} | ||
|
||
#[js_function] | ||
fn get_up_z(ctx: CallContext) -> Result<JsObject> { | ||
let js_this = ctx.this_unchecked::<JsObject>(); | ||
let napi_node = ctx.env.unwrap::<NapiAudioListener>(&js_this)?; | ||
let node = napi_node.unwrap(); | ||
|
||
let native_param = node.up_z().clone(); | ||
let napi_param = NapiAudioParam::new(native_param); | ||
let mut js_obj = NapiAudioParam::create_js_object(ctx.env)?; | ||
ctx.env.wrap(&mut js_obj, napi_param)?; | ||
Ok(js_obj) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters