Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating custom signals is kinda unergonomic #14

Open
4 tasks
harudagondi opened this issue Aug 8, 2022 · 0 comments
Open
4 tasks

Creating custom signals is kinda unergonomic #14

harudagondi opened this issue Aug 8, 2022 · 0 comments
Labels
help wanted Extra attention is needed usability quality-of-life changes

Comments

@harudagondi
Copy link
Owner

harudagondi commented Aug 8, 2022

According to the noise example:

(1) Create a Noise struct to and derive TypeUuid

#[derive(TypeUuid)]
#[uuid = "7cc24057-b499-4f7a-8f8a-e37dfa64be32"]
struct Noise;

(2) Create a NoiseSignal struct and implement Signal (the actual code that creates sound).

struct NoiseSignal;
impl Signal for NoiseSignal {
type Frame = Stereo;
fn sample(&self, _interval: f32, out: &mut [Self::Frame]) {
for out_frame in out {
let mono = fastrand::f32();
out_frame[0] = mono;
out_frame[1] = mono;
}
}
}

(3) Implement ToSignal for Noise.

impl ToSignal for Noise {
type Settings = ();
type Signal = NoiseSignal;
fn to_signal(&self, _settings: Self::Settings) -> Self::Signal {
NoiseSignal
}
}

(4) Add the Noise struct as an audio source (also indicating the number of channels it uses).

.add_audio_source::<2, _, Noise>()

(5) Create a noise handle and noise sink type to easily hold the sink as a resource.

#[derive(Deref)]
struct NoiseHandle(Handle<Noise>);
struct NoiseSink(Handle<AudioHandle<Noise>>, Handle<AudioSink<Noise>>);

(6) Add Noise as an asset.

fn init_assets(mut commands: Commands, mut assets: ResMut<Assets<Noise>>) {
let handle = assets.add(Noise);
commands.insert_resource(NoiseHandle(handle));
}

(7) Play the audio.

fn play_noise(
mut commands: Commands,
mut audio: ResMut<Audio<Stereo, Noise>>,
noise: Res<NoiseHandle>,
) {
let handles = audio.play(noise.clone(), ());
commands.insert_resource(NoiseSink(handles.0, handles.1));
}

Ergonomics

This is a lot of code for just adding a custom signal. Here are the things I would love to see to improve this.

  • Collapse steps (1) to (3) into a single step.
  • (Probably allow closures that return frames to implement Signal, and things that return those closures to implement ToSignal)
  • Ideally remove the second generic in (4) to reduce noise and confusion.
  • ToSignal implementing types that also implement FromWorld should already be added as an asset to remove step (6).

Related Issues:

@harudagondi harudagondi added enhancement New feature or request help wanted Extra attention is needed labels Aug 8, 2022
@harudagondi harudagondi added usability quality-of-life changes and removed enhancement New feature or request labels Aug 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed usability quality-of-life changes
Projects
None yet
Development

No branches or pull requests

1 participant