Skip to content

Commit 8732e43

Browse files
committed
fix on macos
1 parent 1746c94 commit 8732e43

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

crates/recording/examples/recording-cli.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ pub async fn main() {
6363
},
6464
)
6565
.with_system_audio(true)
66-
// .with_mic_feed(Arc::new(mic_feed.ask(microphone::Lock).await.unwrap()))
66+
.with_mic_feed(std::sync::Arc::new(
67+
mic_feed.ask(microphone::Lock).await.unwrap(),
68+
))
6769
.build()
6870
.await
6971
.unwrap();

crates/recording/src/sources/audio_mixer.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use tracing::debug;
1515
// Current problem is generating an output timestamp that lines up with the input's timestamp
1616

1717
struct MixerSource {
18-
rx: Receiver<(ffmpeg::frame::Audio, Timestamp)>,
18+
rx: std::iter::Peekable<flume::IntoIter<(ffmpeg::frame::Audio, Timestamp)>>,
1919
info: AudioInfo,
2020
buffer: VecDeque<(ffmpeg::frame::Audio, Timestamp)>,
2121
buffer_last: Option<(Timestamp, Duration)>,
@@ -41,7 +41,7 @@ impl AudioMixerBuilder {
4141
pub fn add_source(&mut self, info: AudioInfo, rx: Receiver<(ffmpeg::frame::Audio, Timestamp)>) {
4242
self.sources.push(MixerSource {
4343
info,
44-
rx,
44+
rx: rx.into_iter().peekable(),
4545
buffer: VecDeque::new(),
4646
buffer_last: None,
4747
});
@@ -143,14 +143,14 @@ impl AudioMixer {
143143
48_000,
144144
2,
145145
);
146-
pub const BUFFER_TIMEOUT: Duration = Duration::from_millis(10);
146+
pub const BUFFER_TIMEOUT: Duration = Duration::from_millis(200);
147147

148148
fn buffer_sources(&mut self, now: Timestamp) {
149149
for source in &mut self.sources {
150150
let rate = source.info.rate();
151151

152152
if let Some(last) = source.buffer_last {
153-
let last_end = &last.0 + last.1;
153+
let last_end = last.0 + last.1;
154154
if let Some(elapsed_since_last) = now
155155
.duration_since(self.timestamps)
156156
.checked_sub(last_end.duration_since(self.timestamps))
@@ -165,13 +165,14 @@ impl AudioMixer {
165165
chunk_samples,
166166
source.info.channel_layout(),
167167
);
168+
frame.set_rate(source.info.rate() as u32);
169+
168170
for i in 0..frame.planes() {
169171
frame.data_mut(i).fill(0);
170172
}
171173

172-
frame.set_rate(source.info.rate() as u32);
173-
174174
let timestamp = last_end + (elapsed_since_last - remaining);
175+
dbg!(timestamp);
175176
source.buffer_last = Some((
176177
timestamp,
177178
Duration::from_secs_f64(chunk_samples as f64 / rate as f64),
@@ -183,7 +184,7 @@ impl AudioMixer {
183184
}
184185
}
185186

186-
while let Ok((frame, timestamp)) = source.rx.try_recv() {
187+
while let Some((frame, timestamp)) = source.rx.next() {
187188
// if gap between incoming and last, insert silence
188189
if let Some((buffer_last_timestamp, buffer_last_duration)) = source.buffer_last {
189190
let timestamp_elapsed = timestamp.duration_since(self.timestamps);
@@ -216,6 +217,7 @@ impl AudioMixer {
216217
frame.set_rate(source.info.rate() as u32);
217218

218219
let timestamp = buffer_last_timestamp + gap;
220+
dbg!(timestamp);
219221
source.buffer_last = Some((
220222
timestamp,
221223
Duration::from_secs_f64(silence_samples_count as f64 / rate as f64),
@@ -273,6 +275,7 @@ impl AudioMixer {
273275
frame.set_rate(source.info.rate() as u32);
274276

275277
let timestamp = start_timestamp + (elapsed_since_start - remaining);
278+
dbg!(timestamp);
276279
source.buffer_last = Some((
277280
timestamp,
278281
Duration::from_secs_f64(chunk_samples as f64 / rate as f64),

crates/timestamp/src/macos.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl Add<Duration> for MachAbsoluteTimestamp {
3737
let info = TimeBaseInfo::new();
3838
let freq = info.numer as f64 / info.denom as f64;
3939

40-
Self((self.0 as f64 * rhs.as_secs_f64() * freq) as u64)
40+
Self((self.0 as f64 + rhs.as_secs_f64() * freq) as u64)
4141
}
4242
}
4343

@@ -48,6 +48,6 @@ impl Sub<Duration> for MachAbsoluteTimestamp {
4848
let info = TimeBaseInfo::new();
4949
let freq = info.numer as f64 / info.denom as f64;
5050

51-
Self((self.0 as f64 / freq - rhs.as_millis() as f64) as u64)
51+
Self((self.0 as f64 - rhs.as_secs_f64() * freq) as u64)
5252
}
5353
}

0 commit comments

Comments
 (0)