summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrant Shangreaux <grant@unabridgedsoftware.com>2021-10-16 22:54:29 -0500
committerGrant Shangreaux <grant@unabridgedsoftware.com>2021-10-16 22:54:29 -0500
commit18a9fd9f4c2911f09f6fdbee49fb29dc591536c4 (patch)
treeeb22a6a062d66d7e78c7317708ed2837e9a9a797
parent3aa16685da4b62a0281a2816c9fcc234232611d1 (diff)
Add: Node based Synth struct in Rust lib and load class in Godot
-rw-r--r--klangfarb/Main.gdns4
-rw-r--r--klangfarbrs/src/lib.rs40
2 files changed, 27 insertions, 17 deletions
diff --git a/klangfarb/Main.gdns b/klangfarb/Main.gdns
index 5855b12..673e307 100644
--- a/klangfarb/Main.gdns
+++ b/klangfarb/Main.gdns
@@ -3,6 +3,6 @@
[ext_resource path="res://libklangfarbrs.gdnlib" type="GDNativeLibrary" id=1]
[resource]
-resource_name = "HelloWorld"
-class_name = "HelloWorld"
+resource_name = "Synth"
+class_name = "Synth"
library = ExtResource( 1 )
diff --git a/klangfarbrs/src/lib.rs b/klangfarbrs/src/lib.rs
index f116430..2dbc299 100644
--- a/klangfarbrs/src/lib.rs
+++ b/klangfarbrs/src/lib.rs
@@ -26,38 +26,47 @@ impl HelloWorld {
}
#[derive(NativeClass)]
-#[inherit(Resource)]
+#[inherit(Node)]
pub struct Synth {
frequency: f32
}
#[methods]
impl Synth {
+ fn new(_owner: &Node) -> Self {
+ Synth { frequency: 220.0 }
+ }
+
#[export]
fn set_freq(
&mut self,
- _owner: &Resource,
+ _owner: &Node,
freq: f32
) {
self.frequency = freq;
}
#[export]
- fn _ready(&self, _owner: &Resource) {
- let host = cpal::default_host();
- let device = host
- .default_output_device()
- .expect("failed to find a default output device");
- let config = device.default_output_config();
-
- match config.sample_format() {
- cpal::SampleFormat::F32 => run::<f32>(&device, &config.into()),
- cpal::SampleFormat::I16 => run::<i16>(&device, &config.into()),
- cpal::SampleFormat::U16 => run::<u16>(&device, &config.into()),
- };
+ fn _ready(&self, _owner: &Node) {
+ godot_print!("POOOP");
+ play();
+ }
+}
- ();
+fn play() -> Result<(), anyhow::Error> {
+ let host = cpal::default_host();
+ let device = host
+ .default_output_device()
+ .expect("failed to find a default output device");
+ let config = device.default_output_config()?;
+
+ match config.sample_format() {
+ cpal::SampleFormat::F32 => run::<f32>(&device, &config.into())?,
+ cpal::SampleFormat::I16 => run::<i16>(&device, &config.into())?,
+ cpal::SampleFormat::U16 => run::<u16>(&device, &config.into())?,
}
+
+ Ok(())
}
fn run<T>(device: &cpal::Device, config: &cpal::StreamConfig) -> Result<(), anyhow::Error>
@@ -126,6 +135,7 @@ fn write_data<T>(
fn init(handle: InitHandle) {
// Register the new `HelloWorld` type we just declared.
handle.add_class::<HelloWorld>();
+ handle.add_class::<Synth>();
}
// Macro that creates the entry-points of the dynamic library.