godot-bevy 0.11.0

Bridge between Bevy ECS and Godot 4 for Rust-powered game development
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//! Main audio plugin and systems
use crate::interop::{GodotAccess, GodotNodeHandle};
use crate::plugins::assets::GodotResource;
use crate::plugins::audio::output::{
    AudioPlayer, stop_and_free_audio_player, try_get_audio_player,
};
use crate::plugins::audio::{
    ActiveTween, AudioChannel, AudioChannelMarker, AudioCommand, AudioOutput, AudioPlayerType,
    AudioSettings, ChannelId, ChannelState, MainAudioTrack, PlayCommand, SoundId, TweenType,
};
use crate::plugins::scene_tree::SceneTreeRef;
use bevy_app::{App, Plugin, Update};
use bevy_asset::Assets;
use bevy_ecs::prelude::Resource;
use bevy_ecs::schedule::{IntoScheduleConfigs, SystemSet};
use bevy_ecs::system::{Res, ResMut};
use bevy_math::{Vec2, Vec3};
use bevy_time::Time;
use godot::classes::{AudioStream, AudioStreamPlayer, AudioStreamPlayer2D, AudioStreamPlayer3D};
use godot::obj::NewAlloc;
use std::collections::{HashMap, VecDeque};
use thiserror::Error;
use tracing::{trace, warn};

/// Plugin that provides a comprehensive audio API using Godot's audio system.
/// Supports 2D, 3D, and non-positional audio with channels, tweening, and spatial features.
#[derive(Default)]
pub struct GodotAudioPlugin;

#[derive(SystemSet, Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum AudioSystemSet {
    CollectCommands,
    ProcessCommands,
}

impl Plugin for GodotAudioPlugin {
    fn build(&self, app: &mut App) {
        app.init_resource::<GodotAudioChannels>()
            .init_resource::<AudioOutput>()
            .add_audio_channel::<MainAudioTrack>()
            .configure_sets(
                Update,
                AudioSystemSet::ProcessCommands.after(AudioSystemSet::CollectCommands),
            )
            .add_systems(
                Update,
                audio_main_thread.in_set(AudioSystemSet::ProcessCommands),
            );
    }
}

/// Main audio manager for playing sounds and music across different channels.
#[derive(Resource, Default)]
pub struct GodotAudioChannels {
    pub(crate) channels: HashMap<ChannelId, ChannelState>,
    pub(crate) command_queue: VecDeque<AudioCommand>,
}

/// Extension trait for App to add audio channels with automatic system registration
pub trait AudioApp {
    fn add_audio_channel<T: AudioChannelMarker>(&mut self) -> &mut Self;
}

impl AudioApp for App {
    fn add_audio_channel<T: AudioChannelMarker>(&mut self) -> &mut Self {
        let channel_id = ChannelId(T::CHANNEL_NAME);

        // Auto-register a dedicated system for this channel type
        self.add_systems(
            Update,
            process_channel_commands::<T>.in_set(AudioSystemSet::CollectCommands),
        );

        self.insert_resource(AudioChannel::<T>::new(channel_id));

        // Initialize channel state in the global manager
        self.world_mut()
            .resource_mut::<GodotAudioChannels>()
            .channels
            .insert(channel_id, ChannelState::default());

        self
    }
}

/// Dedicated system for processing commands from a specific channel type
fn process_channel_commands<T: AudioChannelMarker>(
    channel: Res<AudioChannel<T>>,
    mut audio_channels: ResMut<GodotAudioChannels>,
) {
    // Collect commands into the global queue so a single system can apply Godot calls.
    let mut commands = channel.commands.write();
    while let Some(command) = commands.pop_front() {
        audio_channels.command_queue.push_back(command);
    }
}

#[derive(Default)]
struct PendingSoundOps {
    volume: Option<f32>,
    pitch: Option<f32>,
    paused: Option<bool>,
}

/// System that applies queued audio commands using Godot APIs.
fn audio_main_thread(
    mut audio_channels: ResMut<GodotAudioChannels>,
    mut audio_output: ResMut<AudioOutput>,
    mut assets: ResMut<Assets<GodotResource>>,
    mut scene_tree: SceneTreeRef,
    time: Res<Time>,
    mut godot: GodotAccess,
) {
    let mut pending_ops: HashMap<SoundId, PendingSoundOps> = HashMap::new();
    let mut pending_stops: Vec<(SoundId, GodotNodeHandle)> = Vec::new();

    while let Some(command) = audio_channels.command_queue.pop_front() {
        match command {
            AudioCommand::Play(play_cmd) => {
                if process_play_command(
                    &play_cmd,
                    &mut assets,
                    &mut scene_tree,
                    &mut audio_output,
                    &mut godot,
                )
                .is_none()
                {
                    // Asset not ready, re-queue for next frame
                    audio_channels
                        .command_queue
                        .push_front(AudioCommand::Play(play_cmd));
                    warn!("Audio asset not ready, re-queued for next frame");
                    break; // Stop processing this frame to avoid infinite retry loop
                }
            }
            AudioCommand::Stop(channel_id, tween) => {
                let sound_ids = collect_channel_sound_ids(&audio_output, channel_id);

                if let Some(tween) = tween {
                    // Implement fade-out tweening with real current volumes
                    for sound_id in sound_ids {
                        // Get the actual current volume instead of assuming 1.0
                        let current_volume = audio_output
                            .current_volumes
                            .get(&sound_id)
                            .copied()
                            .unwrap_or(1.0);
                        let fade_out_tween =
                            ActiveTween::new_fade_out(current_volume, tween.clone());
                        audio_output.active_tweens.insert(sound_id, fade_out_tween);
                        trace!(
                            "Started fade-out from volume {} for sound: {:?}",
                            current_volume, sound_id
                        );
                    }
                } else {
                    // Immediate stop
                    for sound_id in sound_ids {
                        schedule_stop_sound(
                            &mut audio_output,
                            &mut pending_ops,
                            &mut pending_stops,
                            sound_id,
                        );
                    }
                }
                trace!("Processed stop command for channel: {:?}", channel_id);
            }
            AudioCommand::Pause(channel_id, _tween) => {
                let sound_ids = collect_channel_sound_ids(&audio_output, channel_id);
                for sound_id in sound_ids {
                    pending_ops.entry(sound_id).or_default().paused = Some(true);
                }
                trace!("Paused channel: {:?}", channel_id);
            }
            AudioCommand::Resume(channel_id, _tween) => {
                let sound_ids = collect_channel_sound_ids(&audio_output, channel_id);
                for sound_id in sound_ids {
                    pending_ops.entry(sound_id).or_default().paused = Some(false);
                }
                trace!("Resumed channel: {:?}", channel_id);
            }
            AudioCommand::SetVolume(channel_id, volume, _tween) => {
                let sound_ids = collect_channel_sound_ids(&audio_output, channel_id);
                for sound_id in sound_ids {
                    audio_output.current_volumes.insert(sound_id, volume);
                    pending_ops.entry(sound_id).or_default().volume = Some(volume);
                }
                trace!("Set volume to {} for channel: {:?}", volume, channel_id);
            }
            AudioCommand::SetPitch(channel_id, pitch, _tween) => {
                let sound_ids = collect_channel_sound_ids(&audio_output, channel_id);
                for sound_id in sound_ids {
                    pending_ops.entry(sound_id).or_default().pitch = Some(pitch);
                }
                trace!("Set pitch to {} for channel: {:?}", pitch, channel_id);
            }
            AudioCommand::SetPanning(_channel_id, _panning, _tween) => {
                // TODO: Implement panning for individual sounds
                warn!("Panning not yet implemented for individual sounds");
            }
            AudioCommand::StopSound(sound_id, _tween) => {
                schedule_stop_sound(
                    &mut audio_output,
                    &mut pending_ops,
                    &mut pending_stops,
                    sound_id,
                );
                trace!("Stopped sound: {:?}", sound_id);
            }
        }
    }

    let delta = time.delta();
    let mut completed_tweens = Vec::new();
    let mut sounds_to_stop = Vec::new();
    let mut volume_updates = Vec::new();
    let mut pitch_updates = Vec::new();

    // First pass: update tweens and collect parameter changes
    for (&sound_id, tween) in audio_output.active_tweens.iter_mut() {
        let current_value = tween.update(delta);

        match tween.tween_type {
            TweenType::Volume | TweenType::FadeOut => {
                volume_updates.push((sound_id, current_value));
            }
            TweenType::Pitch => {
                pitch_updates.push((sound_id, current_value));
            }
        }

        if tween.is_complete() {
            completed_tweens.push(sound_id);

            // If this was a fade-out, mark sound for removal
            if matches!(tween.tween_type, TweenType::FadeOut) {
                sounds_to_stop.push(sound_id);
            }
        }
    }

    for (sound_id, volume) in volume_updates {
        if audio_output.playing_sounds.contains_key(&sound_id) {
            audio_output.current_volumes.insert(sound_id, volume);
            pending_ops.entry(sound_id).or_default().volume = Some(volume);
        }
    }

    for (sound_id, pitch) in pitch_updates {
        if audio_output.playing_sounds.contains_key(&sound_id) {
            pending_ops.entry(sound_id).or_default().pitch = Some(pitch);
        }
    }

    for sound_id in completed_tweens {
        audio_output.active_tweens.remove(&sound_id);
        trace!("Completed tween for sound: {:?}", sound_id);
    }

    for sound_id in sounds_to_stop {
        schedule_stop_sound(
            &mut audio_output,
            &mut pending_ops,
            &mut pending_stops,
            sound_id,
        );
        trace!("Stopped sound after fade-out: {:?}", sound_id);
    }

    for (sound_id, handle) in pending_stops {
        stop_and_free_audio_player(&mut godot, handle);
        trace!("Stopped sound: {:?}", sound_id);
    }

    let playing_sounds: Vec<(SoundId, GodotNodeHandle)> = audio_output
        .playing_sounds
        .iter()
        .map(|(sound_id, handle)| (*sound_id, *handle))
        .collect();

    let mut finished_sounds = Vec::new();
    for (sound_id, handle) in playing_sounds {
        let Some(mut player) = try_get_audio_player(&mut godot, handle) else {
            finished_sounds.push(sound_id);
            continue;
        };

        if let Some(ops) = pending_ops.get(&sound_id) {
            apply_pending_ops(&mut player, ops);
        }

        let is_playing = player.is_playing();
        if !is_playing {
            let mut node = player.into_node();
            if let Some(mut parent) = node.get_parent() {
                parent.remove_child(&node);
            }
            node.queue_free();
            finished_sounds.push(sound_id);
        }
    }

    for sound_id in finished_sounds {
        audio_output.playing_sounds.remove(&sound_id);
        audio_output.sound_to_channel.remove(&sound_id);
        audio_output.active_tweens.remove(&sound_id);
        audio_output.current_volumes.remove(&sound_id);
        trace!("Cleaned up finished sound: {:?}", sound_id);
    }
}

fn collect_channel_sound_ids(output: &AudioOutput, channel_id: ChannelId) -> Vec<SoundId> {
    output
        .sound_to_channel
        .iter()
        .filter(|(_, ch)| **ch == channel_id)
        .map(|(sound_id, _)| *sound_id)
        .collect()
}

fn schedule_stop_sound(
    output: &mut AudioOutput,
    pending_ops: &mut HashMap<SoundId, PendingSoundOps>,
    pending_stops: &mut Vec<(SoundId, GodotNodeHandle)>,
    sound_id: SoundId,
) {
    if let Some(handle) = output.playing_sounds.remove(&sound_id) {
        output.sound_to_channel.remove(&sound_id);
        output.current_volumes.remove(&sound_id);
        pending_ops.remove(&sound_id);
        pending_stops.push((sound_id, handle));
    }
}

fn apply_pending_ops(player: &mut AudioPlayer, ops: &PendingSoundOps) {
    if let Some(volume) = ops.volume {
        player.set_volume_db(volume_to_db(volume));
    }
    if let Some(pitch) = ops.pitch {
        player.set_pitch_scale(pitch);
    }
    if let Some(paused) = ops.paused {
        player.set_stream_paused(paused);
    }
}

/// Process a play command and return the sound ID if successful
fn process_play_command(
    play_cmd: &PlayCommand,
    assets: &mut Assets<GodotResource>,
    scene_tree: &mut SceneTreeRef,
    output: &mut AudioOutput,
    godot: &mut GodotAccess,
) -> Option<SoundId> {
    let audio_stream = if let Some(asset) = assets.get_mut(&play_cmd.handle) {
        asset.try_cast::<AudioStream>()
    } else {
        // Asset not ready yet, re-queue for next frame
        warn!("Audio asset not ready: {:?}", play_cmd.handle);
        return None;
    };

    let Some(audio_stream) = audio_stream else {
        warn!("Failed to cast to AudioStream: {:?}", play_cmd.handle);
        return None;
    };

    // Configure looping if requested
    let audio_stream = configure_looping(audio_stream, play_cmd.settings.looping);

    // Check if fade-in is needed
    let (initial_volume, fade_in_tween) = if let Some(fade_in) = &play_cmd.settings.fade_in {
        (0.0, Some((play_cmd.settings.volume, fade_in.clone())))
    } else {
        (play_cmd.settings.volume, None)
    };

    // Create settings with initial volume for fade-in
    let mut initial_settings = play_cmd.settings.clone();
    initial_settings.volume = initial_volume;

    // Create appropriate player based on type
    let player_handle = match &play_cmd.player_type {
        AudioPlayerType::NonPositional => create_audio_player(audio_stream, &initial_settings),
        AudioPlayerType::Spatial2D { position } => {
            create_audio_player_2d(audio_stream, &initial_settings, *position)
        }
        AudioPlayerType::Spatial3D { position } => {
            create_audio_player_3d(audio_stream, &initial_settings, *position)
        }
    };

    if let Some(handle) = player_handle {
        if let Some(mut root) = scene_tree.get().get_root() {
            // Get the node from the handle and add it to the scene tree
            let node = godot.get::<godot::classes::Node>(handle);
            root.add_child(&node);
        }

        // Now that the node is in the scene tree, start playback
        start_audio_playback(godot, handle);

        output.playing_sounds.insert(play_cmd.sound_id, handle);
        output
            .sound_to_channel
            .insert(play_cmd.sound_id, play_cmd.channel_id);

        // Track initial volume (either fade-in start volume or target volume)
        let initial_volume = if fade_in_tween.is_some() {
            0.0
        } else {
            initial_settings.volume
        };
        output
            .current_volumes
            .insert(play_cmd.sound_id, initial_volume);

        // Set up fade-in tween if needed
        if let Some((target_volume, fade_in)) = fade_in_tween {
            let tween = ActiveTween::new_fade_in(target_volume, fade_in);
            output.active_tweens.insert(play_cmd.sound_id, tween);
            trace!("Started fade-in for sound: {:?}", play_cmd.sound_id);
        }

        trace!(
            "Started playing audio: {:?} in channel: {:?}",
            play_cmd.sound_id, play_cmd.channel_id
        );
        Some(play_cmd.sound_id)
    } else {
        None
    }
}

fn create_audio_player(
    audio_stream: godot::obj::Gd<AudioStream>,
    settings: &AudioSettings,
) -> Option<GodotNodeHandle> {
    let mut player = AudioStreamPlayer::new_alloc();
    player.set_stream(&audio_stream);
    player.set_volume_db(volume_to_db(settings.volume));
    player.set_pitch_scale(settings.pitch);

    if let Some(panning) = settings.panning {
        // Convert from -1.0..1.0 to 0.0..1.0 for Godot
        let _godot_panning = (panning + 1.0) / 2.0;
        let bus_name: godot::builtin::StringName = "Master".into();
        player.set_bus(&bus_name);
    }

    // Don't play yet - need to add to scene tree first
    Some(GodotNodeHandle::new(
        player.upcast::<godot::classes::Node>(),
    ))
}

fn create_audio_player_2d(
    audio_stream: godot::obj::Gd<AudioStream>,
    settings: &AudioSettings,
    position: Vec2,
) -> Option<GodotNodeHandle> {
    let mut player = AudioStreamPlayer2D::new_alloc();
    player.set_stream(&audio_stream);
    player.set_volume_db(volume_to_db(settings.volume));
    player.set_pitch_scale(settings.pitch);
    player.set_position(godot::prelude::Vector2::new(position.x, position.y));

    // Don't play yet - need to add to scene tree first
    Some(GodotNodeHandle::new(
        player.upcast::<godot::classes::Node>(),
    ))
}

fn create_audio_player_3d(
    audio_stream: godot::obj::Gd<AudioStream>,
    settings: &AudioSettings,
    position: Vec3,
) -> Option<GodotNodeHandle> {
    let mut player = AudioStreamPlayer3D::new_alloc();
    player.set_stream(&audio_stream);
    player.set_volume_db(volume_to_db(settings.volume));
    player.set_pitch_scale(settings.pitch);
    player.set_position(godot::prelude::Vector3::new(
        position.x, position.y, position.z,
    ));

    // Don't play yet - need to add to scene tree first
    Some(GodotNodeHandle::new(
        player.upcast::<godot::classes::Node>(),
    ))
}

fn configure_looping(
    audio_stream: godot::obj::Gd<AudioStream>,
    looping: bool,
) -> godot::obj::Gd<AudioStream> {
    if !looping {
        return audio_stream;
    }

    // Try to enable looping on different stream types
    if let Ok(mut ogg_stream) = audio_stream
        .clone()
        .try_cast::<godot::classes::AudioStreamOggVorbis>()
    {
        ogg_stream.set_loop(true);
        ogg_stream.upcast()
    } else if let Ok(mut wav_stream) = audio_stream
        .clone()
        .try_cast::<godot::classes::AudioStreamWav>()
    {
        wav_stream.set_loop_mode(godot::classes::audio_stream_wav::LoopMode::FORWARD);
        wav_stream.upcast()
    } else {
        warn!("Audio stream type doesn't support runtime loop configuration");
        audio_stream
    }
}

fn start_audio_playback(godot: &mut GodotAccess, handle: GodotNodeHandle) {
    // Try each player type and start playback
    if let Some(mut player) = godot.try_get::<AudioStreamPlayer>(handle) {
        player.play();
    } else if let Some(mut player) = godot.try_get::<AudioStreamPlayer2D>(handle) {
        player.play();
    } else if let Some(mut player) = godot.try_get::<AudioStreamPlayer3D>(handle) {
        player.play();
    }
}

/// Convert linear volume (0.0-1.0) to decibels for Godot
fn volume_to_db(volume: f32) -> f32 {
    if volume <= 0.0 {
        -80.0 // Silence
    } else {
        20.0 * volume.log10()
    }
}

/// Simplified GodotAudioChannels - most functionality moved to per-channel systems
impl GodotAudioChannels {
    /// Get stats about the audio system
    pub fn stats(&self) -> (usize, usize) {
        (self.command_queue.len(), self.channels.len())
    }
}

/// Possible errors that can be produced by the audio system
#[derive(Debug, Error)]
pub enum AudioError {
    #[error("Sound not found: {0:?}")]
    SoundNotFound(SoundId),
    #[error("Channel not found: {0:?}")]
    ChannelNotFound(ChannelId),
}