Skip to content

Commit

Permalink
sound/matrix functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hatomist committed Jan 18, 2025
1 parent 17b13ac commit 1f6b210
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ members = [
]

[workspace.package]
version = "0.6.0"
version = "0.6.1"
authors = [
"Benjamin Bolte <[email protected]>",
"Denys Bezmenov <[email protected]>",
Expand Down
17 changes: 8 additions & 9 deletions kos/src/hal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ pub use crate::grpc_interface::google::longrunning::*;
pub use crate::grpc_interface::kos;
pub use crate::grpc_interface::kos::common::ActionResponse;
pub use crate::kos_proto::{
actuator::*, common::ActionResult, imu::*, inference::*, process_manager::*, led_matrix::*, sound::*,
actuator::*, common::ActionResult, imu::*, inference::*, led_matrix::*, process_manager::*,
sound::*,
};
use async_trait::async_trait;
use bytes::Bytes;
use eyre::Result;
use std::fmt::Display;
use futures::Stream;
use tokio::sync::mpsc::Sender;
use std::fmt::Display;
use std::pin::Pin;
use bytes::Bytes;
use tokio::sync::mpsc::Sender;

// Type alias for the audio stream
pub type AudioStream = Pin<Box<dyn Stream<Item = Bytes> + Send>>;
Expand Down Expand Up @@ -87,21 +88,21 @@ pub trait LEDMatrix: Send + Sync {
pub trait Sound: Send + Sync {
/// Get information about audio capabilities
async fn get_audio_info(&self) -> Result<GetAudioInfoResponse, tonic::Status>;

/// Start playing audio with the given configuration
async fn play_audio(
&self,
config: AudioConfig,
sender: Sender<Bytes>,
) -> Result<ActionResponse, tonic::Status>;

/// Start recording audio with the given configuration
async fn record_audio(
&self,
config: AudioConfig,
duration_ms: u32,
) -> Result<AudioStream, tonic::Status>;

/// Stop an ongoing recording session
async fn stop_recording(&self) -> Result<ActionResponse, tonic::Status>;
}
Expand All @@ -122,5 +123,3 @@ impl Display for CalibrationStatus {
}
}
}


2 changes: 1 addition & 1 deletion kos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use hal::actuator_service_server::ActuatorServiceServer;
use hal::imu_service_server::ImuServiceServer;
use hal::inference_service_server::InferenceServiceServer;
use hal::led_matrix_service_server::LedMatrixServiceServer;
use hal::sound_service_server::SoundServiceServer;
use hal::process_manager_service_server::ProcessManagerServiceServer;
use hal::sound_service_server::SoundServiceServer;
use services::OperationsServiceImpl;
use services::{
ActuatorServiceImpl, IMUServiceImpl, InferenceServiceImpl, LEDMatrixServiceImpl,
Expand Down
4 changes: 2 additions & 2 deletions kos/src/services/led_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl LedMatrixService for LEDMatrixServiceImpl {
request: Request<WriteBufferRequest>,
) -> Result<Response<ActionResponse>, Status> {
let buffer = request.into_inner().buffer;

let response = self
.led_matrix
.write_buffer(buffer.clone())
Expand All @@ -53,7 +53,7 @@ impl LedMatrixService for LEDMatrixServiceImpl {
request: Request<WriteColorBufferRequest>,
) -> Result<Response<ActionResponse>, Status> {
let request = request.into_inner();

let response = self
.led_matrix
.write_color_buffer(
Expand Down
2 changes: 1 addition & 1 deletion kos/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use actuator::*;
pub use imu::*;
pub use inference::*;
pub use krec_logger::*;
pub use led_matrix::*;
pub use operations::*;
pub use process_manager::*;
pub use led_matrix::*;
pub use sound::*;
29 changes: 18 additions & 11 deletions kos/src/services/sound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,28 @@ impl SoundService for SoundServiceImpl {
request: Request<tonic::Streaming<PlayAudioRequest>>,
) -> Result<Response<ActionResponse>, Status> {
let mut stream = request.into_inner();

// Get the first message which must contain the config
let first_msg = stream.message().await
.map_err(|e| Status::internal(format!("Failed to receive first audio message: {:?}", e)))?
let first_msg = stream
.message()
.await
.map_err(|e| {
Status::internal(format!("Failed to receive first audio message: {:?}", e))
})?
.ok_or_else(|| Status::invalid_argument("Empty audio stream"))?;

let config = first_msg.config
.ok_or_else(|| Status::invalid_argument("First message must contain audio configuration"))?;

let config = first_msg.config.ok_or_else(|| {
Status::invalid_argument("First message must contain audio configuration")
})?;

trace!("Starting audio playback with config: {:?}", config);

// Create channel for audio data
let (tx, _rx) = mpsc::channel(32);

// Start playback with the sender
let response = self.sound.play_audio(config, tx.clone()).await?;

// Spawn task to handle incoming audio data
tokio::spawn(async move {
while let Ok(Some(msg)) = stream.message().await {
Expand All @@ -66,21 +71,23 @@ impl SoundService for SoundServiceImpl {
Ok(Response::new(response))
}

type RecordAudioStream = Pin<Box<dyn Stream<Item = Result<RecordAudioResponse, Status>> + Send>>;
type RecordAudioStream =
Pin<Box<dyn Stream<Item = Result<RecordAudioResponse, Status>> + Send>>;

async fn record_audio(
&self,
request: Request<RecordAudioRequest>,
) -> Result<Response<Self::RecordAudioStream>, Status> {
let request = request.into_inner();

trace!(
"Starting audio recording with config: {:?}, duration: {}ms",
request.config,
request.duration_ms
);

let config = request.config
let config = request
.config
.ok_or_else(|| Status::invalid_argument("Audio configuration is required"))?;

let stream = self.sound.record_audio(config, request.duration_ms).await?;
Expand Down

0 comments on commit 1f6b210

Please sign in to comment.