Skip to content

Commit

Permalink
Make SmafPlayer to accept raw smaf
Browse files Browse the repository at this point in the history
  • Loading branch information
dlunch committed Apr 28, 2024
1 parent 99aec76 commit 6c935d5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
7 changes: 2 additions & 5 deletions smaf_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use midir::{MidiOutput, MidiOutputConnection};
use rodio::{buffer::SamplesBuffer, OutputStream, OutputStreamHandle, Sink};
use tokio::time::sleep;

use smaf::Smaf;
use smaf_player::{AudioBackend, SmafPlayer};

struct AudioBackendImpl {
Expand Down Expand Up @@ -76,16 +75,14 @@ pub async fn main() {
let file = args().nth(1).expect("No file given");
let data = fs::read(file).expect("Failed to read file");

let smaf = Smaf::parse(&data).expect("Failed to parse file");

let midi_out = MidiOutput::new("smaf_cli").unwrap();
let midi_ports = midi_out.ports();
let out_port = midi_ports.last().unwrap();
let midi_out = midi_out.connect(out_port, "smaf_cli").unwrap();

let (_output_stream, stream_handle) = OutputStream::try_default().unwrap();

let player = SmafPlayer::new();
let player = SmafPlayer::new(data);

player.play(&smaf, &AudioBackendImpl::new(midi_out, stream_handle)).await
player.play(&AudioBackendImpl::new(midi_out, stream_handle)).await
}
8 changes: 6 additions & 2 deletions smaf_player/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,14 @@ impl Player for PCMAudioTrackPlayer<'_> {

#[derive(Default, Clone)]
pub struct SmafPlayer {
raw: Vec<u8>,
stopped: Arc<AtomicBool>,
}

impl SmafPlayer {
pub fn new() -> Self {
pub fn new(raw: Vec<u8>) -> Self {
Self {
raw,
stopped: Arc::new(AtomicBool::new(false)),
}
}
Expand All @@ -239,7 +241,9 @@ impl SmafPlayer {
self.stopped.store(true, Ordering::Relaxed);
}

pub async fn play(&self, smaf: &Smaf<'_>, backend: &dyn AudioBackend) {
pub async fn play(&self, backend: &dyn AudioBackend) {
let smaf = Smaf::parse(&self.raw).unwrap();

let players = smaf.chunks.iter().filter_map(|x| match x {
SmafChunk::ScoreTrack(_, x) => Some(ScoreTrackPlayer::new(x, backend).play(self.stopped.clone())),
SmafChunk::PCMAudioTrack(_, x) => Some(PCMAudioTrackPlayer::new(x, backend).play(self.stopped.clone())),
Expand Down

0 comments on commit 6c935d5

Please sign in to comment.