Skip to content

Commit f70a95f

Browse files
committed
implement playback
1 parent c4216e1 commit f70a95f

File tree

7 files changed

+125
-99
lines changed

7 files changed

+125
-99
lines changed

src/audio/cutter.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ impl Cutter {
8383
}
8484

8585
pub async fn run_internal(mut self) {
86+
// TODO await here
8687
while !self.handles.is_empty() || !self.cuts.is_empty() {
8788
if self.handles.len() < MAX_NUM_PROCESSES {
8889
if let Some(cut) = self.pop_front() {

src/audio/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod cut;
22
mod cutter;
33
mod cutting_strategy;
44
mod excerpt;
5-
mod playback;
5+
pub mod playback;
66
mod sample_reader;
77
mod time;
88

src/audio/playback.rs

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use std::sync::atomic::AtomicBool;
22
use std::sync::atomic::Ordering;
33
use std::sync::Arc;
4-
use std::thread;
5-
use std::time::Duration;
6-
use std::time::SystemTime;
74

5+
use futures::channel::mpsc::Sender;
86
use hound::WavSpec;
97
use rodio::OutputStream;
108
use rodio::Sink;
@@ -15,6 +13,7 @@ use crate::audio::AudioTime;
1513
use super::sample_reader::WavFileReader;
1614
use super::SampleReader;
1715

16+
#[derive(Clone)]
1817
pub struct WavSource {
1918
spec: WavSpec,
2019
samples: Vec<i16>,
@@ -61,51 +60,16 @@ impl Iterator for WavSource {
6160
}
6261
}
6362

64-
pub fn play_audio(
65-
buffer: &mut WavFileReader,
66-
start_time: AudioTime,
67-
end_time: AudioTime,
68-
) -> PlaybackThreadHandle {
69-
let source = WavSource::new(buffer, start_time, end_time);
70-
let stop = Arc::new(AtomicBool::new(false));
71-
thread::spawn({
72-
let stop = stop.clone();
73-
move || {
74-
let (_stream, stream_handle) = OutputStream::try_default().unwrap();
75-
let sink = Sink::try_new(&stream_handle).unwrap();
76-
sink.append(source);
77-
sink.play();
78-
while !stop.load(Ordering::SeqCst) {
79-
thread::sleep(Duration::from_millis(1));
80-
}
81-
}
82-
});
83-
PlaybackThreadHandle {
84-
stop,
85-
start_system_time: SystemTime::now(),
86-
start_audio_time: start_time,
87-
}
88-
}
63+
#[derive(Clone, Debug)]
64+
pub struct Progress {}
8965

90-
pub struct PlaybackThreadHandle {
91-
stop: Arc<AtomicBool>,
92-
start_system_time: SystemTime,
93-
start_audio_time: AudioTime,
94-
}
95-
96-
impl PlaybackThreadHandle {
97-
pub fn shut_down(&self) {
98-
self.stop.store(true, Ordering::SeqCst);
99-
}
100-
101-
pub fn get_current_audio_time(&self) -> AudioTime {
102-
let time_expired = SystemTime::now().duration_since(self.start_system_time);
103-
let time_expired_secs = time_expired
104-
.unwrap_or(Duration::from_millis(0))
105-
.as_secs_f64();
106-
AudioTime::from_time_same_spec(
107-
self.start_audio_time.time + time_expired_secs,
108-
self.start_audio_time,
109-
)
66+
pub async fn play_audio(source: WavSource, _: Sender<Progress>) {
67+
let stop = Arc::new(AtomicBool::new(false));
68+
let (_stream, stream_handle) = OutputStream::try_default().unwrap();
69+
let sink = Sink::try_new(&stream_handle).unwrap();
70+
sink.append(source);
71+
sink.play();
72+
while !stop.load(Ordering::SeqCst) {
73+
println!("hi");
11074
}
11175
}

src/audio/sample_reader.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ pub trait SampleReader {
1313
end: AudioTime,
1414
) -> Result<Vec<i16>, OutOfBoundsError>;
1515

16-
fn start(&self) -> AudioTime;
17-
fn end(&self) -> AudioTime;
1816
fn spec(&self) -> WavSpec;
1917
}
2018

@@ -68,14 +66,6 @@ impl SampleReader for WavReader<BufReader<File>> {
6866
}
6967
}
7068

71-
fn start(&self) -> AudioTime {
72-
AudioTime::from_time_and_spec(0.0, self.spec())
73-
}
74-
75-
fn end(&self) -> AudioTime {
76-
AudioTime::from_sample_and_spec(self.len(), self.spec())
77-
}
78-
7969
fn spec(&self) -> WavSpec {
8070
self.spec()
8171
}

src/gui/mod.rs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,6 @@ impl Gui {
3636
}
3737
}
3838

39-
// impl Application for Gui {
40-
// type Executor = executor::Default;
41-
42-
// type Message = Message;
43-
44-
// type Theme = Theme;
45-
46-
// type Flags = Config;
47-
48-
// fn title(&self) -> String {
49-
// "Striputary".to_string()
50-
// }
51-
52-
// fn theme(&self) -> Theme {
53-
// Theme::GruvboxDark
54-
// }
55-
// }
56-
5739
impl Gui {
5840
fn new(config: &Config) -> (Self, Task<Message>) {
5941
(
@@ -94,11 +76,11 @@ impl Gui {
9476
}
9577

9678
fn subscription(&self) -> Subscription<Message> {
97-
Subscription::batch(
98-
self.session
99-
.iter()
100-
.map(|s| s.subscription().map(|m| Message::SessionMessage(m))),
101-
)
79+
if let Some(ref session) = self.session {
80+
session.subscription().map(|m| Message::SessionMessage(m))
81+
} else {
82+
Subscription::none()
83+
}
10284
}
10385
}
10486

src/gui/plot.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ pub struct Plot {
6161
volume_data: Vec<Data>,
6262
song_before: Option<Song>,
6363
song_after: Option<Song>,
64-
start: AudioTime,
65-
end: AudioTime,
6664
cut_time: AudioTime,
6765
finished_cut_before: bool,
6866
finished_cut_after: bool,
@@ -103,15 +101,13 @@ impl Plot {
103101
volume_data,
104102
song_before,
105103
song_after,
106-
start,
107-
end,
108104
cut_time,
109105
finished_cut_before: false,
110106
finished_cut_after: false,
111107
}
112108
}
113109

114-
pub fn get_plot_path(&self, data: &[Data], bounds: &Bounds) -> Path {
110+
fn get_plot_path(&self, data: &[Data], bounds: &Bounds) -> Path {
115111
let mut path = Builder::new();
116112

117113
if data.len() > 0 {
@@ -125,7 +121,7 @@ impl Plot {
125121

126122
/// Return the path left of the marker and the path right
127123
/// of the marker, so they can be colored individually.
128-
pub fn get_plot_paths(&self, bounds: &Bounds) -> (Path, Path) {
124+
fn get_plot_paths(&self, bounds: &Bounds) -> (Path, Path) {
129125
let cutoff = self
130126
.volume_data
131127
.iter()
@@ -139,7 +135,7 @@ impl Plot {
139135
)
140136
}
141137

142-
pub fn get_marker_path(&self, bounds: &Bounds) -> Path {
138+
fn get_marker_path(&self, bounds: &Bounds) -> Path {
143139
let mut path = Builder::new();
144140
path.move_to(bounds.data_to_point(&Data {
145141
time: self.cut_time,
@@ -179,10 +175,22 @@ impl Plot {
179175
Bounds {
180176
width: bounds.width,
181177
height: bounds.height,
182-
min_time: self.volume_data.first().unwrap().time,
183-
max_time: self.volume_data.last().unwrap().time,
178+
min_time: self.min_time(),
179+
max_time: self.max_time(),
184180
}
185181
}
182+
183+
pub fn max_time(&self) -> AudioTime {
184+
self.volume_data.last().unwrap().time
185+
}
186+
187+
fn min_time(&self) -> AudioTime {
188+
self.volume_data.first().unwrap().time
189+
}
190+
191+
pub fn cut_time(&self) -> AudioTime {
192+
self.cut_time
193+
}
186194
}
187195

188196
pub struct PlotMarkerMoved {

0 commit comments

Comments
 (0)