/* * Copyright (c) 2022 Contributors to the Rrise project */ use rrise::{ query_params::{get_rtpc_value, RtpcValueType}, settings::*, sound_engine::*, *, }; use rrise_headers::rr; use bevy::prelude::*; use crossbeam_channel::{Receiver, Sender}; use std::path::PathBuf; #[cfg(windows)] use cc; const DEFAULT_LISTENER_ID: AkGameObjectID = 1; const THE_GAME_OBJECT: AkGameObjectID = 100; fn main() -> Result<(), AkResult> { // Run the Bevy app App::new() .add_plugins(DefaultPlugins) .insert_resource(Meters { meters: [ (rr::xbus::Meters_00, 0.), (rr::xbus::Meters_01, 0.), (rr::xbus::Meters_02, 0.), (rr::xbus::Meters_03, 0.), (rr::xbus::Meters_04, 0.), (rr::xbus::Meters_05, 0.), (rr::xbus::Meters_06, 0.), (rr::xbus::Meters_07, 0.), (rr::xbus::Meters_08, 0.), (rr::xbus::Meters_09, 0.), (rr::xbus::Meters_10, 0.), ], }) .insert_resource(match crossbeam_channel::unbounded() { (sender, receiver) => CallbackChannel { sender, receiver }, }) .add_startup_system(init_sound_engine.pipe(system_adapter::unwrap)) .add_startup_system_to_stage( StartupStage::PostStartup, setup_audio.pipe(system_adapter::unwrap), ) // PostStartup so that Bevy Window already initialized .add_startup_system(setup) .add_system_to_stage( CoreStage::PreUpdate, audio_metering.pipe(system_adapter::unwrap), ) .add_system_to_stage( CoreStage::PostUpdate, audio_rendering.pipe(system_adapter::unwrap), ) .add_system(visualize_music) .add_system(process_callbacks) .run(); // Terminate Wwise stop_all(None); unregister_all_game_obj()?; term_sound_engine()?; Ok(()) } #[derive(Component)] struct BandMeter(usize); #[derive(Component)] struct BeatBarText; type Meter = (AkUniqueID, AkRtpcValue); #[derive(Resource)] struct Meters { meters: [Meter; 11], } #[derive(Clone, Resource)] struct CallbackChannel { sender: Sender, receiver: Receiver, } fn audio_metering(mut meters: ResMut) -> Result<(), AkResult> { for meter in &mut meters.meters { let rtpc_value = get_rtpc_value(meter.0, None, None, RtpcValueType::Global(0.))?; meter.1 = match rtpc_value { RtpcValueType::Global(v) => v, _ => 0., }; } Ok(()) } fn audio_rendering() -> Result<(), AkResult> { const ALLOW_SYNC_RENDER: bool = true; render_audio(ALLOW_SYNC_RENDER) } fn visualize_music(mut visual_meters: Query<(&mut Transform, &BandMeter)>, meters: Res) { for (mut tfm, meter_index) in visual_meters.iter_mut() { let y_scale = 10. * (meters.meters[meter_index.0].1 + 48.) / 54.; tfm.scale = Vec3::new(1., y_scale, 1.); } } fn process_callbacks( mut beat_bar_text: Query<&mut Text, With>, time: Res