2024-05-04 05:40:20 +03:00
|
|
|
use std::fs::File;
|
|
|
|
|
2024-04-27 19:48:13 +03:00
|
|
|
use iced::{
|
2024-05-03 04:32:47 +03:00
|
|
|
alignment,
|
|
|
|
widget::{column, container, row, text::LineHeight, Container, Rule},
|
|
|
|
window, Color, Command, Subscription,
|
2024-04-27 19:48:13 +03:00
|
|
|
};
|
2024-05-01 03:27:34 +03:00
|
|
|
use tokio::sync::broadcast::{channel, Receiver, Sender};
|
2024-04-27 15:36:28 +03:00
|
|
|
|
2024-04-28 18:58:01 +03:00
|
|
|
use crate::{
|
2024-05-02 00:12:59 +03:00
|
|
|
gui_components::{button_with_centered_text, text_centered},
|
|
|
|
gui_utils,
|
|
|
|
utils::get_config,
|
|
|
|
Config, BUFFER_LENGTH,
|
2024-04-28 18:58:01 +03:00
|
|
|
};
|
2024-05-01 03:27:34 +03:00
|
|
|
|
2024-05-05 16:38:54 +03:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum Player {
|
|
|
|
Play,
|
|
|
|
Pause,
|
|
|
|
Stop,
|
|
|
|
}
|
|
|
|
|
2024-05-01 03:27:34 +03:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct Features {
|
|
|
|
stream: bool,
|
|
|
|
record: bool,
|
|
|
|
play_audio: bool,
|
|
|
|
}
|
|
|
|
|
2024-05-04 05:40:20 +03:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct AudioFile {
|
|
|
|
file: Option<File>,
|
|
|
|
decoded_to_playing_sender: Option<Sender<f32>>,
|
|
|
|
}
|
|
|
|
|
2024-04-28 18:58:01 +03:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum Event {
|
2024-05-01 03:27:34 +03:00
|
|
|
None,
|
2024-04-28 18:58:01 +03:00
|
|
|
Connect,
|
|
|
|
Disconnect,
|
|
|
|
Record,
|
|
|
|
StopRecord,
|
|
|
|
PlayAudio,
|
|
|
|
StopAudio,
|
2024-05-05 16:38:54 +03:00
|
|
|
PauseAudio,
|
|
|
|
ContinueAudio,
|
2024-04-28 18:58:01 +03:00
|
|
|
LoadConfig(Config),
|
2024-05-01 03:27:34 +03:00
|
|
|
IcedEvent(iced::Event),
|
2024-05-01 19:37:52 +03:00
|
|
|
CloseWindow(window::Id),
|
2024-04-28 18:58:01 +03:00
|
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
|
|
|
pub enum State {
|
2024-05-01 03:27:34 +03:00
|
|
|
None,
|
2024-04-28 18:58:01 +03:00
|
|
|
Connected,
|
|
|
|
Disconnected,
|
|
|
|
Recording,
|
|
|
|
StopRecording,
|
|
|
|
PlayingAudio,
|
|
|
|
StopAudio,
|
2024-05-05 16:38:54 +03:00
|
|
|
PausedAudio,
|
|
|
|
ContinuedAudio,
|
2024-04-28 18:58:01 +03:00
|
|
|
}
|
2024-04-27 15:36:28 +03:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum Message {
|
2024-04-28 18:58:01 +03:00
|
|
|
Event(Event),
|
|
|
|
State(State),
|
|
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct DataChannel {
|
2024-05-03 04:32:47 +03:00
|
|
|
microphone_stream_sender: Sender<f32>,
|
|
|
|
audio_stream_sender: Sender<f32>,
|
2024-04-28 18:58:01 +03:00
|
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct CommunicationChannel {
|
2024-05-03 04:32:47 +03:00
|
|
|
base_to_streaming_sender: Sender<bool>,
|
|
|
|
streaming_to_base_sender: Sender<bool>,
|
|
|
|
base_to_recording_sender: Sender<bool>,
|
|
|
|
recording_to_base_sender: Sender<bool>,
|
2024-05-05 16:38:54 +03:00
|
|
|
base_to_playing_sender: Sender<Player>,
|
|
|
|
playing_to_base_sender: Sender<Player>,
|
2024-04-28 18:58:01 +03:00
|
|
|
}
|
2024-05-01 03:27:34 +03:00
|
|
|
#[derive(Debug, PartialEq)]
|
2024-04-28 18:58:01 +03:00
|
|
|
enum Condition {
|
|
|
|
Active,
|
|
|
|
Loading,
|
|
|
|
Passive,
|
2024-04-27 15:36:28 +03:00
|
|
|
}
|
|
|
|
|
2024-04-28 18:58:01 +03:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct GUIStatus {
|
|
|
|
are_we_connect: Condition,
|
|
|
|
are_we_record: Condition,
|
|
|
|
are_we_play_audio: Condition,
|
2024-05-05 16:38:54 +03:00
|
|
|
are_we_paused_audio: Condition,
|
2024-04-28 18:58:01 +03:00
|
|
|
}
|
2024-04-27 15:36:28 +03:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Streamer {
|
|
|
|
config: Option<Config>,
|
2024-04-28 18:58:01 +03:00
|
|
|
data_channel: DataChannel,
|
|
|
|
communication_channel: CommunicationChannel,
|
2024-05-04 05:40:20 +03:00
|
|
|
audio_file: AudioFile,
|
2024-04-28 18:58:01 +03:00
|
|
|
gui_status: GUIStatus,
|
2024-04-27 15:36:28 +03:00
|
|
|
}
|
|
|
|
impl Default for Streamer {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Streamer {
|
|
|
|
fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
config: None,
|
2024-04-28 18:58:01 +03:00
|
|
|
data_channel: DataChannel {
|
2024-05-03 04:32:47 +03:00
|
|
|
microphone_stream_sender: channel(BUFFER_LENGTH).0,
|
|
|
|
audio_stream_sender: channel(BUFFER_LENGTH).0,
|
2024-04-28 18:58:01 +03:00
|
|
|
},
|
|
|
|
communication_channel: CommunicationChannel {
|
2024-05-03 04:32:47 +03:00
|
|
|
base_to_streaming_sender: channel(1).0,
|
|
|
|
streaming_to_base_sender: channel(1).0,
|
|
|
|
base_to_recording_sender: channel(1).0,
|
|
|
|
recording_to_base_sender: channel(1).0,
|
|
|
|
base_to_playing_sender: channel(1).0,
|
|
|
|
playing_to_base_sender: channel(1).0,
|
2024-04-28 18:58:01 +03:00
|
|
|
},
|
2024-05-04 05:40:20 +03:00
|
|
|
audio_file: AudioFile {
|
|
|
|
file: None,
|
|
|
|
decoded_to_playing_sender: None,
|
|
|
|
},
|
2024-04-28 18:58:01 +03:00
|
|
|
gui_status: GUIStatus {
|
|
|
|
are_we_connect: Condition::Passive,
|
|
|
|
are_we_record: Condition::Passive,
|
|
|
|
are_we_play_audio: Condition::Passive,
|
2024-05-05 16:38:54 +03:00
|
|
|
are_we_paused_audio: Condition::Passive,
|
2024-04-28 18:58:01 +03:00
|
|
|
},
|
2024-04-27 15:36:28 +03:00
|
|
|
}
|
|
|
|
}
|
2024-04-28 18:58:01 +03:00
|
|
|
pub fn update(&mut self, message: Message) -> Command<Message> {
|
2024-04-27 15:36:28 +03:00
|
|
|
match message {
|
2024-04-28 18:58:01 +03:00
|
|
|
Message::Event(event) => match event {
|
2024-05-01 03:27:34 +03:00
|
|
|
Event::None => Command::none(),
|
2024-04-28 18:58:01 +03:00
|
|
|
Event::Connect => {
|
|
|
|
println!("Connect");
|
|
|
|
self.gui_status.are_we_connect = Condition::Loading;
|
2024-05-01 19:37:52 +03:00
|
|
|
|
2024-05-06 04:09:00 +03:00
|
|
|
let microphone_stream_receiver =
|
2024-05-03 04:32:47 +03:00
|
|
|
self.data_channel.microphone_stream_sender.subscribe();
|
2024-05-06 04:09:00 +03:00
|
|
|
let audio_stream_receiver = self.data_channel.audio_stream_sender.subscribe();
|
2024-05-01 19:37:52 +03:00
|
|
|
let streamer_config = self.config.clone().unwrap();
|
2024-05-03 04:32:47 +03:00
|
|
|
let streaming_to_base_sender =
|
|
|
|
self.communication_channel.streaming_to_base_sender.clone();
|
|
|
|
let base_to_streaming_receiver = self
|
|
|
|
.communication_channel
|
|
|
|
.base_to_streaming_sender
|
|
|
|
.subscribe();
|
2024-05-01 19:37:52 +03:00
|
|
|
|
2024-04-28 18:58:01 +03:00
|
|
|
Command::perform(
|
|
|
|
async move {
|
2024-05-01 19:37:52 +03:00
|
|
|
gui_utils::connect(
|
2024-05-06 04:09:00 +03:00
|
|
|
microphone_stream_receiver,
|
|
|
|
audio_stream_receiver,
|
2024-05-01 19:37:52 +03:00
|
|
|
streamer_config,
|
2024-05-03 04:32:47 +03:00
|
|
|
streaming_to_base_sender,
|
|
|
|
base_to_streaming_receiver,
|
2024-05-01 19:37:52 +03:00
|
|
|
)
|
|
|
|
.await
|
2024-04-28 18:58:01 +03:00
|
|
|
},
|
|
|
|
Message::State,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Event::Disconnect => {
|
|
|
|
println!("Disconnect");
|
|
|
|
self.gui_status.are_we_connect = Condition::Loading;
|
2024-05-01 19:37:52 +03:00
|
|
|
|
2024-05-03 04:32:47 +03:00
|
|
|
let streaming_to_base_receiver = self
|
|
|
|
.communication_channel
|
|
|
|
.streaming_to_base_sender
|
|
|
|
.subscribe();
|
|
|
|
let base_to_streaming_sender =
|
|
|
|
self.communication_channel.base_to_streaming_sender.clone();
|
2024-05-01 19:37:52 +03:00
|
|
|
|
|
|
|
Command::perform(
|
2024-05-03 04:32:47 +03:00
|
|
|
async move {
|
|
|
|
gui_utils::disconnect(
|
|
|
|
streaming_to_base_receiver,
|
|
|
|
base_to_streaming_sender,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
},
|
2024-05-01 19:37:52 +03:00
|
|
|
Message::State,
|
|
|
|
)
|
2024-04-28 18:58:01 +03:00
|
|
|
}
|
|
|
|
Event::Record => {
|
|
|
|
println!("Record");
|
|
|
|
self.gui_status.are_we_record = Condition::Loading;
|
2024-05-01 19:37:52 +03:00
|
|
|
|
2024-05-03 04:32:47 +03:00
|
|
|
let microphone_stream_sender =
|
|
|
|
self.data_channel.microphone_stream_sender.clone();
|
|
|
|
let recording_to_base_sender =
|
|
|
|
self.communication_channel.recording_to_base_sender.clone();
|
|
|
|
let base_to_recording_receiver = self
|
|
|
|
.communication_channel
|
|
|
|
.base_to_recording_sender
|
|
|
|
.subscribe();
|
2024-05-01 19:37:52 +03:00
|
|
|
|
2024-04-28 18:58:01 +03:00
|
|
|
Command::perform(
|
|
|
|
async move {
|
2024-05-02 00:12:59 +03:00
|
|
|
gui_utils::start_recording(
|
2024-05-03 04:32:47 +03:00
|
|
|
microphone_stream_sender,
|
|
|
|
recording_to_base_sender,
|
|
|
|
base_to_recording_receiver,
|
2024-05-01 19:37:52 +03:00
|
|
|
)
|
|
|
|
.await
|
2024-04-28 18:58:01 +03:00
|
|
|
},
|
|
|
|
Message::State,
|
|
|
|
)
|
2024-04-27 22:14:50 +03:00
|
|
|
}
|
2024-04-28 18:58:01 +03:00
|
|
|
Event::StopRecord => {
|
|
|
|
println!("Stop Record");
|
|
|
|
self.gui_status.are_we_record = Condition::Loading;
|
2024-05-03 04:32:47 +03:00
|
|
|
let recording_to_base_receiver = self
|
|
|
|
.communication_channel
|
|
|
|
.recording_to_base_sender
|
|
|
|
.subscribe();
|
|
|
|
let base_to_recording_sender =
|
|
|
|
self.communication_channel.base_to_recording_sender.clone();
|
2024-05-01 19:37:52 +03:00
|
|
|
Command::perform(
|
|
|
|
async move {
|
2024-05-03 04:32:47 +03:00
|
|
|
gui_utils::stop_recording(
|
|
|
|
recording_to_base_receiver,
|
|
|
|
base_to_recording_sender,
|
|
|
|
)
|
|
|
|
.await
|
2024-05-01 19:37:52 +03:00
|
|
|
},
|
|
|
|
Message::State,
|
|
|
|
)
|
2024-04-27 22:14:50 +03:00
|
|
|
}
|
2024-04-28 18:58:01 +03:00
|
|
|
Event::PlayAudio => {
|
|
|
|
println!("Play Audio");
|
|
|
|
self.gui_status.are_we_play_audio = Condition::Loading;
|
2024-05-04 05:40:20 +03:00
|
|
|
|
|
|
|
let file = File::open("music.mp3").unwrap();
|
|
|
|
self.audio_file.file = Some(file);
|
|
|
|
|
|
|
|
self.audio_file.decoded_to_playing_sender = Some(
|
|
|
|
channel(
|
|
|
|
self.audio_file
|
|
|
|
.file
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.metadata()
|
|
|
|
.unwrap()
|
|
|
|
.len() as usize
|
2024-05-05 16:38:54 +03:00
|
|
|
* 4,
|
2024-05-04 05:40:20 +03:00
|
|
|
)
|
|
|
|
.0,
|
|
|
|
);
|
|
|
|
|
2024-05-06 04:09:00 +03:00
|
|
|
let audio_stream_sender = self.data_channel.audio_stream_sender.clone();
|
2024-05-03 04:32:47 +03:00
|
|
|
let playing_to_base_sender =
|
|
|
|
self.communication_channel.playing_to_base_sender.clone();
|
|
|
|
let base_to_playing_receiver = self
|
|
|
|
.communication_channel
|
|
|
|
.base_to_playing_sender
|
|
|
|
.subscribe();
|
2024-05-02 00:12:59 +03:00
|
|
|
|
2024-05-05 16:38:54 +03:00
|
|
|
let playing_to_base_receiver_is_audio_finished = self
|
|
|
|
.communication_channel
|
|
|
|
.playing_to_base_sender
|
|
|
|
.subscribe();
|
|
|
|
|
|
|
|
let playing_to_base_receiver_is_audio_stopped = self
|
2024-05-04 05:40:20 +03:00
|
|
|
.communication_channel
|
|
|
|
.playing_to_base_sender
|
|
|
|
.subscribe();
|
|
|
|
|
|
|
|
let base_to_playing_sender =
|
|
|
|
self.communication_channel.base_to_playing_sender.clone();
|
|
|
|
|
|
|
|
let file = self.audio_file.file.as_ref().unwrap().try_clone().unwrap();
|
|
|
|
let decoded_to_playing_sender_for_playing =
|
|
|
|
self.audio_file.decoded_to_playing_sender.clone().unwrap();
|
|
|
|
|
|
|
|
let decoded_to_playing_sender_for_is_finished =
|
|
|
|
self.audio_file.decoded_to_playing_sender.clone().unwrap();
|
|
|
|
let playing_command = Command::perform(
|
2024-04-28 18:58:01 +03:00
|
|
|
async move {
|
2024-05-03 04:32:47 +03:00
|
|
|
gui_utils::start_playing(
|
|
|
|
audio_stream_sender,
|
2024-05-04 05:40:20 +03:00
|
|
|
decoded_to_playing_sender_for_playing,
|
|
|
|
file,
|
2024-05-03 04:32:47 +03:00
|
|
|
playing_to_base_sender,
|
|
|
|
base_to_playing_receiver,
|
2024-05-02 00:12:59 +03:00
|
|
|
)
|
|
|
|
.await
|
2024-04-28 18:58:01 +03:00
|
|
|
},
|
|
|
|
Message::State,
|
2024-05-04 05:40:20 +03:00
|
|
|
);
|
|
|
|
let is_finished_command = Command::perform(
|
|
|
|
async move {
|
|
|
|
gui_utils::is_playing_finished(
|
2024-05-05 16:38:54 +03:00
|
|
|
playing_to_base_receiver_is_audio_finished,
|
|
|
|
playing_to_base_receiver_is_audio_stopped,
|
2024-05-04 05:40:20 +03:00
|
|
|
base_to_playing_sender,
|
|
|
|
decoded_to_playing_sender_for_is_finished,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
},
|
|
|
|
Message::State,
|
|
|
|
);
|
|
|
|
let commands = vec![playing_command, is_finished_command];
|
|
|
|
Command::batch(commands)
|
2024-04-28 18:58:01 +03:00
|
|
|
}
|
|
|
|
Event::StopAudio => {
|
|
|
|
println!("Stop Audio");
|
|
|
|
self.gui_status.are_we_play_audio = Condition::Loading;
|
2024-05-04 05:40:20 +03:00
|
|
|
|
2024-05-03 04:32:47 +03:00
|
|
|
let playing_to_base_receiver = self
|
|
|
|
.communication_channel
|
|
|
|
.playing_to_base_sender
|
|
|
|
.subscribe();
|
|
|
|
let base_to_playing_sender =
|
|
|
|
self.communication_channel.base_to_playing_sender.clone();
|
2024-05-04 05:40:20 +03:00
|
|
|
|
2024-04-28 18:58:01 +03:00
|
|
|
Command::perform(
|
|
|
|
async move {
|
2024-05-03 04:32:47 +03:00
|
|
|
gui_utils::stop_playing(
|
|
|
|
playing_to_base_receiver,
|
|
|
|
base_to_playing_sender,
|
|
|
|
)
|
|
|
|
.await
|
2024-04-28 18:58:01 +03:00
|
|
|
},
|
|
|
|
Message::State,
|
|
|
|
)
|
2024-04-27 22:14:50 +03:00
|
|
|
}
|
2024-05-05 16:38:54 +03:00
|
|
|
Event::PauseAudio => {
|
|
|
|
println!("Pause Audio");
|
|
|
|
self.gui_status.are_we_paused_audio = Condition::Loading;
|
|
|
|
|
|
|
|
let playing_to_base_receiver = self
|
|
|
|
.communication_channel
|
|
|
|
.playing_to_base_sender
|
|
|
|
.subscribe();
|
|
|
|
let base_to_playing_sender =
|
|
|
|
self.communication_channel.base_to_playing_sender.clone();
|
|
|
|
|
|
|
|
Command::perform(
|
|
|
|
async move {
|
|
|
|
gui_utils::pause_playing(
|
|
|
|
playing_to_base_receiver,
|
|
|
|
base_to_playing_sender,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
},
|
|
|
|
Message::State,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Event::ContinueAudio => {
|
|
|
|
println!("Continue Audio");
|
|
|
|
self.gui_status.are_we_paused_audio = Condition::Loading;
|
|
|
|
|
|
|
|
let playing_to_base_receiver = self
|
|
|
|
.communication_channel
|
|
|
|
.playing_to_base_sender
|
|
|
|
.subscribe();
|
|
|
|
let base_to_playing_sender =
|
|
|
|
self.communication_channel.base_to_playing_sender.clone();
|
|
|
|
|
|
|
|
Command::perform(
|
|
|
|
async move {
|
|
|
|
gui_utils::continue_playing(
|
|
|
|
playing_to_base_receiver,
|
|
|
|
base_to_playing_sender,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
},
|
|
|
|
Message::State,
|
|
|
|
)
|
|
|
|
}
|
2024-04-28 18:58:01 +03:00
|
|
|
Event::LoadConfig(config) => {
|
|
|
|
self.config = Some(config);
|
|
|
|
Command::none()
|
2024-04-27 22:14:50 +03:00
|
|
|
}
|
2024-05-01 03:27:34 +03:00
|
|
|
Event::IcedEvent(iced_event) => match iced_event {
|
|
|
|
iced::Event::Keyboard(_) => Command::none(),
|
|
|
|
iced::Event::Mouse(_) => Command::none(),
|
|
|
|
iced::Event::Window(id, window_event) => {
|
|
|
|
if let window::Event::CloseRequested = window_event {
|
|
|
|
self.exit(id)
|
|
|
|
} else {
|
|
|
|
Command::none()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
iced::Event::Touch(_) => Command::none(),
|
|
|
|
iced::Event::PlatformSpecific(_) => Command::none(),
|
|
|
|
},
|
2024-05-01 19:37:52 +03:00
|
|
|
Event::CloseWindow(id) => window::close(id),
|
2024-04-27 22:14:50 +03:00
|
|
|
},
|
2024-04-28 18:58:01 +03:00
|
|
|
Message::State(state) => match state {
|
2024-05-01 03:27:34 +03:00
|
|
|
State::None => Command::none(),
|
2024-04-28 18:58:01 +03:00
|
|
|
State::Connected => {
|
|
|
|
self.gui_status.are_we_connect = Condition::Active;
|
|
|
|
Command::none()
|
|
|
|
}
|
|
|
|
State::Disconnected => {
|
|
|
|
self.gui_status.are_we_connect = Condition::Passive;
|
|
|
|
Command::none()
|
2024-04-27 22:14:50 +03:00
|
|
|
}
|
2024-04-28 18:58:01 +03:00
|
|
|
State::Recording => {
|
|
|
|
self.gui_status.are_we_record = Condition::Active;
|
|
|
|
Command::none()
|
|
|
|
}
|
|
|
|
State::StopRecording => {
|
|
|
|
self.gui_status.are_we_record = Condition::Passive;
|
|
|
|
Command::none()
|
|
|
|
}
|
|
|
|
State::PlayingAudio => {
|
|
|
|
self.gui_status.are_we_play_audio = Condition::Active;
|
2024-05-05 16:38:54 +03:00
|
|
|
self.gui_status.are_we_paused_audio = Condition::Passive;
|
2024-04-28 18:58:01 +03:00
|
|
|
Command::none()
|
|
|
|
}
|
|
|
|
State::StopAudio => {
|
|
|
|
self.gui_status.are_we_play_audio = Condition::Passive;
|
|
|
|
Command::none()
|
2024-04-27 22:14:50 +03:00
|
|
|
}
|
2024-05-05 16:38:54 +03:00
|
|
|
State::PausedAudio => {
|
|
|
|
self.gui_status.are_we_paused_audio = Condition::Active;
|
|
|
|
Command::none()
|
|
|
|
}
|
|
|
|
State::ContinuedAudio => {
|
|
|
|
self.gui_status.are_we_paused_audio = Condition::Passive;
|
|
|
|
Command::none()
|
|
|
|
}
|
2024-04-27 22:14:50 +03:00
|
|
|
},
|
2024-04-28 18:58:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn view(&self) -> Container<Message> {
|
2024-05-02 00:12:59 +03:00
|
|
|
//let color_red = Color::from_rgb8(255, 0, 0);
|
|
|
|
let color_green = Color::from_rgb8(0, 255, 0);
|
2024-05-05 16:38:54 +03:00
|
|
|
let color_blue = Color::from_rgb8(0, 0, 255);
|
2024-05-02 00:12:59 +03:00
|
|
|
let color_yellow = Color::from_rgb8(255, 255, 0);
|
|
|
|
//let color_white = Color::from_rgb8(255, 255, 255);
|
2024-05-05 16:38:54 +03:00
|
|
|
let color_grey = Color::from_rgb8(128, 128, 128);
|
2024-05-02 00:12:59 +03:00
|
|
|
//let color_black = Color::from_rgb8(0, 0, 0);
|
|
|
|
let color_pink = Color::from_rgb8(255, 150, 150);
|
|
|
|
|
2024-05-03 04:32:47 +03:00
|
|
|
let header = text_centered("Radioxide")
|
|
|
|
.size(35)
|
|
|
|
.line_height(LineHeight::Relative(1.0));
|
2024-05-02 00:12:59 +03:00
|
|
|
|
|
|
|
let connection_text = text_centered("Connection");
|
|
|
|
let recording_text = text_centered("Microphone");
|
|
|
|
let play_audio_text = text_centered("Play Audio");
|
2024-05-05 16:38:54 +03:00
|
|
|
let pause_audio_text = text_centered("Pause Audio");
|
2024-05-02 00:12:59 +03:00
|
|
|
|
|
|
|
let connection_status_text;
|
|
|
|
let recording_status_text;
|
|
|
|
let play_audio_status_text;
|
2024-05-05 16:38:54 +03:00
|
|
|
let paused_audio_status_text;
|
2024-05-02 00:12:59 +03:00
|
|
|
|
2024-04-28 18:58:01 +03:00
|
|
|
let connect_button = match self.gui_status.are_we_connect {
|
|
|
|
Condition::Active => {
|
2024-05-02 00:12:59 +03:00
|
|
|
connection_status_text = text_centered("Active").color(color_green);
|
2024-04-28 18:58:01 +03:00
|
|
|
button_with_centered_text("Disconnect").on_press(Message::Event(Event::Disconnect))
|
|
|
|
}
|
2024-05-02 00:12:59 +03:00
|
|
|
Condition::Loading => {
|
|
|
|
connection_status_text = text_centered("Loading").color(color_yellow);
|
|
|
|
button_with_centered_text("Processing")
|
|
|
|
}
|
2024-04-28 18:58:01 +03:00
|
|
|
Condition::Passive => {
|
2024-05-02 00:12:59 +03:00
|
|
|
connection_status_text = text_centered("Passive").color(color_pink);
|
2024-04-28 18:58:01 +03:00
|
|
|
button_with_centered_text("Connect").on_press(Message::Event(Event::Connect))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let record_button = match self.gui_status.are_we_record {
|
|
|
|
Condition::Active => {
|
2024-05-02 00:12:59 +03:00
|
|
|
recording_status_text = text_centered("Active").color(color_green);
|
2024-05-03 04:32:47 +03:00
|
|
|
button_with_centered_text("Stop Mic").on_press(Message::Event(Event::StopRecord))
|
2024-05-02 00:12:59 +03:00
|
|
|
}
|
|
|
|
Condition::Loading => {
|
|
|
|
recording_status_text = text_centered("Loading").color(color_yellow);
|
|
|
|
button_with_centered_text("Processing")
|
|
|
|
}
|
|
|
|
Condition::Passive => {
|
|
|
|
recording_status_text = text_centered("Passive").color(color_pink);
|
2024-05-03 04:32:47 +03:00
|
|
|
button_with_centered_text("Start Mic").on_press(Message::Event(Event::Record))
|
2024-05-02 00:12:59 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let play_audio_button = match self.gui_status.are_we_play_audio {
|
|
|
|
Condition::Active => {
|
|
|
|
play_audio_status_text = text_centered("Active").color(color_green);
|
|
|
|
button_with_centered_text("Stop Audio").on_press(Message::Event(Event::StopAudio))
|
|
|
|
}
|
|
|
|
Condition::Loading => {
|
|
|
|
play_audio_status_text = text_centered("Loading").color(color_yellow);
|
|
|
|
button_with_centered_text("Processing")
|
2024-04-28 18:58:01 +03:00
|
|
|
}
|
|
|
|
Condition::Passive => {
|
2024-05-02 00:12:59 +03:00
|
|
|
play_audio_status_text = text_centered("Passive").color(color_pink);
|
|
|
|
button_with_centered_text("Play Audio").on_press(Message::Event(Event::PlayAudio))
|
2024-04-28 18:58:01 +03:00
|
|
|
}
|
2024-04-27 22:14:50 +03:00
|
|
|
};
|
2024-04-28 18:58:01 +03:00
|
|
|
|
2024-05-05 16:38:54 +03:00
|
|
|
let pause_audio_button = if let Condition::Active = self.gui_status.are_we_play_audio {
|
|
|
|
match self.gui_status.are_we_paused_audio {
|
|
|
|
Condition::Active => {
|
|
|
|
paused_audio_status_text = text_centered("Paused").color(color_blue);
|
|
|
|
button_with_centered_text("Continue Audio")
|
|
|
|
.on_press(Message::Event(Event::ContinueAudio))
|
|
|
|
}
|
|
|
|
Condition::Loading => {
|
|
|
|
paused_audio_status_text = text_centered("Loading").color(color_yellow);
|
|
|
|
button_with_centered_text("Processing")
|
|
|
|
}
|
|
|
|
Condition::Passive => {
|
|
|
|
paused_audio_status_text = text_centered("Playing").color(color_yellow);
|
|
|
|
button_with_centered_text("Pause Audio")
|
|
|
|
.on_press(Message::Event(Event::PauseAudio))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
paused_audio_status_text = text_centered("Waiting").color(color_grey);
|
|
|
|
button_with_centered_text("No Purpose")
|
|
|
|
};
|
|
|
|
|
2024-05-03 04:32:47 +03:00
|
|
|
let header_content = row![header].width(350).height(50);
|
2024-05-02 00:12:59 +03:00
|
|
|
let text_content = row![
|
|
|
|
connection_text,
|
|
|
|
Rule::vertical(1),
|
|
|
|
recording_text,
|
|
|
|
Rule::vertical(1),
|
|
|
|
play_audio_text,
|
2024-05-05 16:38:54 +03:00
|
|
|
Rule::vertical(1),
|
|
|
|
pause_audio_text,
|
2024-05-02 00:12:59 +03:00
|
|
|
]
|
|
|
|
.spacing(5)
|
|
|
|
.width(350)
|
|
|
|
.height(35);
|
2024-04-28 18:58:01 +03:00
|
|
|
|
2024-05-02 00:12:59 +03:00
|
|
|
let status_content = row![
|
|
|
|
connection_status_text,
|
|
|
|
Rule::vertical(1),
|
|
|
|
recording_status_text,
|
|
|
|
Rule::vertical(1),
|
2024-05-05 16:38:54 +03:00
|
|
|
play_audio_status_text,
|
|
|
|
Rule::vertical(1),
|
|
|
|
paused_audio_status_text,
|
|
|
|
]
|
|
|
|
.spacing(5)
|
|
|
|
.width(350)
|
|
|
|
.height(35);
|
|
|
|
let button_content = row![
|
|
|
|
connect_button,
|
|
|
|
record_button,
|
|
|
|
play_audio_button,
|
|
|
|
pause_audio_button
|
2024-05-02 00:12:59 +03:00
|
|
|
]
|
|
|
|
.spacing(5)
|
|
|
|
.width(350)
|
|
|
|
.height(35);
|
|
|
|
|
|
|
|
let content = column![
|
|
|
|
header_content,
|
|
|
|
Rule::horizontal(1),
|
|
|
|
text_content,
|
|
|
|
button_content,
|
|
|
|
status_content,
|
|
|
|
Rule::horizontal(1),
|
|
|
|
]
|
|
|
|
.spacing(20)
|
|
|
|
.width(350)
|
|
|
|
.height(300);
|
2024-05-03 04:32:47 +03:00
|
|
|
container(content)
|
|
|
|
.height(300)
|
|
|
|
.center_x()
|
|
|
|
.align_y(alignment::Vertical::Top)
|
2024-04-27 15:36:28 +03:00
|
|
|
}
|
2024-05-01 03:27:34 +03:00
|
|
|
pub fn subscription(&self) -> Subscription<Message> {
|
|
|
|
iced::event::listen()
|
|
|
|
.map(Event::IcedEvent)
|
|
|
|
.map(Message::Event)
|
|
|
|
}
|
2024-04-27 15:36:28 +03:00
|
|
|
pub fn load_config() -> Command<Message> {
|
2024-04-28 18:58:01 +03:00
|
|
|
Command::perform(
|
|
|
|
async move {
|
|
|
|
let config = get_config().await;
|
|
|
|
Event::LoadConfig(config)
|
|
|
|
},
|
|
|
|
Message::Event,
|
|
|
|
)
|
2024-04-27 15:36:28 +03:00
|
|
|
}
|
2024-05-01 03:27:34 +03:00
|
|
|
fn call_closer(
|
2024-05-03 04:32:47 +03:00
|
|
|
streaming_to_base_receiver: Receiver<bool>,
|
|
|
|
base_to_streaming_sender: Sender<bool>,
|
|
|
|
recording_to_base_receiver: Receiver<bool>,
|
|
|
|
base_to_recording_sender: Sender<bool>,
|
2024-05-05 16:38:54 +03:00
|
|
|
playing_to_base_receiver: Receiver<Player>,
|
|
|
|
base_to_playing_sender: Sender<Player>,
|
2024-05-01 03:27:34 +03:00
|
|
|
features_in_need: Features,
|
|
|
|
window_id: window::Id,
|
2024-05-01 19:37:52 +03:00
|
|
|
) -> Command<Message> {
|
|
|
|
Command::perform(
|
|
|
|
async move {
|
|
|
|
if features_in_need.stream {
|
2024-05-03 04:32:47 +03:00
|
|
|
gui_utils::disconnect(streaming_to_base_receiver, base_to_streaming_sender)
|
|
|
|
.await;
|
2024-05-01 19:37:52 +03:00
|
|
|
}
|
|
|
|
if features_in_need.record {
|
2024-05-03 04:32:47 +03:00
|
|
|
gui_utils::stop_recording(recording_to_base_receiver, base_to_recording_sender)
|
|
|
|
.await;
|
2024-05-01 19:37:52 +03:00
|
|
|
}
|
|
|
|
if features_in_need.play_audio {
|
2024-05-04 12:31:54 +03:00
|
|
|
gui_utils::stop_playing(playing_to_base_receiver, base_to_playing_sender).await;
|
2024-05-01 19:37:52 +03:00
|
|
|
}
|
|
|
|
Event::CloseWindow(window_id)
|
|
|
|
},
|
|
|
|
Message::Event,
|
|
|
|
)
|
2024-05-01 03:27:34 +03:00
|
|
|
}
|
|
|
|
fn exit(&self, window_id: window::Id) -> Command<Message> {
|
2024-05-01 19:37:52 +03:00
|
|
|
let mut features_in_need = Features {
|
2024-05-01 03:27:34 +03:00
|
|
|
stream: false,
|
|
|
|
record: false,
|
|
|
|
play_audio: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
if self.gui_status.are_we_connect == Condition::Active {
|
|
|
|
features_in_need.stream = true;
|
|
|
|
}
|
|
|
|
if self.gui_status.are_we_record == Condition::Active {
|
|
|
|
features_in_need.record = true;
|
|
|
|
}
|
|
|
|
if self.gui_status.are_we_play_audio == Condition::Active {
|
|
|
|
features_in_need.play_audio = true;
|
|
|
|
}
|
2024-05-03 04:32:47 +03:00
|
|
|
let streaming_to_base_receiver = self
|
|
|
|
.communication_channel
|
|
|
|
.streaming_to_base_sender
|
|
|
|
.subscribe();
|
|
|
|
let base_to_streaming_sender = self.communication_channel.base_to_streaming_sender.clone();
|
2024-05-01 03:27:34 +03:00
|
|
|
|
2024-05-03 04:32:47 +03:00
|
|
|
let recording_to_base_receiver = self
|
|
|
|
.communication_channel
|
|
|
|
.recording_to_base_sender
|
|
|
|
.subscribe();
|
|
|
|
let base_to_recording_sender = self.communication_channel.base_to_recording_sender.clone();
|
2024-05-01 03:27:34 +03:00
|
|
|
|
2024-05-03 04:32:47 +03:00
|
|
|
let playing_to_base_receiver = self
|
|
|
|
.communication_channel
|
|
|
|
.playing_to_base_sender
|
|
|
|
.subscribe();
|
|
|
|
let base_to_playing_sender = self.communication_channel.base_to_playing_sender.clone();
|
2024-05-01 03:27:34 +03:00
|
|
|
|
2024-05-01 19:37:52 +03:00
|
|
|
Self::call_closer(
|
2024-05-03 04:32:47 +03:00
|
|
|
streaming_to_base_receiver,
|
|
|
|
base_to_streaming_sender,
|
|
|
|
recording_to_base_receiver,
|
|
|
|
base_to_recording_sender,
|
|
|
|
playing_to_base_receiver,
|
|
|
|
base_to_playing_sender,
|
2024-05-01 19:37:52 +03:00
|
|
|
features_in_need,
|
|
|
|
window_id,
|
|
|
|
)
|
2024-05-01 03:27:34 +03:00
|
|
|
}
|
2024-05-01 19:37:52 +03:00
|
|
|
}
|