Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
check:
Expand All @@ -20,22 +19,16 @@ jobs:
targets: ''

runs-on: ${{ matrix.platform }}
timeout-minutes: 45

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install Linux dependencies
if: matrix.platform == 'ubuntu-22.04'
run: |
sudo apt-get update
sudo apt-get install -y \
libasound2-dev \
libwebkit2gtk-4.1-dev \
libappindicator3-dev \
librsvg2-dev \
libssl-dev \
pkg-config
timeout-minutes: 15
run: bash scripts/install-linux-deps.sh

- name: Install Rust stable
uses: dtolnay/rust-toolchain@master
Expand Down
12 changes: 2 additions & 10 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,8 @@ jobs:

- name: Install Linux dependencies
if: matrix.platform == 'ubuntu-22.04'
run: |
sudo apt-get update
sudo apt-get install -y \
libasound2-dev \
libwebkit2gtk-4.1-dev \
libappindicator3-dev \
librsvg2-dev \
patchelf \
libssl-dev \
pkg-config
timeout-minutes: 15
run: bash scripts/install-linux-deps.sh patchelf

- name: Add macOS targets
if: matrix.platform == 'macos-latest'
Expand Down
31 changes: 31 additions & 0 deletions scripts/install-linux-deps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash
set -euo pipefail

# The hosted Jammy runner's Azure mirror can stall for hours. Replace only that
# Ubuntu endpoint, including entries in the runner's mirror list and deb822 files.
shopt -s nullglob
sources=(/etc/apt/sources.list /etc/apt/apt-mirrors.txt /etc/apt/sources.list.d/*.list /etc/apt/sources.list.d/*.sources)
for source in "${sources[@]}"; do
if [[ -f "$source" ]]; then
sudo sed -i \
-e 's|http://azure.archive.ubuntu.com/ubuntu|https://archive.ubuntu.com/ubuntu|g' \
-e 's|https://azure.archive.ubuntu.com/ubuntu|https://archive.ubuntu.com/ubuntu|g' \
"$source"
fi
done

apt_options=(
-o Acquire::Retries=3
-o Acquire::http::Timeout=30
-o Acquire::https::Timeout=30
)
# Fail on incomplete index downloads instead of proceeding with stale indexes.
sudo apt-get "${apt_options[@]}" -o APT::Update::Error-Mode=any update
sudo apt-get "${apt_options[@]}" install -y --no-install-recommends \
libasound2-dev \
libwebkit2gtk-4.1-dev \
libappindicator3-dev \
librsvg2-dev \
libssl-dev \
pkg-config \
"$@"
38 changes: 36 additions & 2 deletions src-tauri/src/commands/audio/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,36 @@ pub async fn export_audio_mix(
},
);

let sleep_enabled = sleep_mode.unwrap_or(false);
let gains = if sleep_enabled {
let _ = app.emit(
"mix-progress",
MixProgress {
percent: 15.0,
stage: "正在测量逐句响度".into(),
},
);
super::loudness::clip_gains(&audio_paths).await?
} else {
Vec::new()
};
let intermediate =
super::loudness::TemporaryMedia::beside(std::path::Path::new(&output_path), "wav");
let mix_path = if sleep_enabled {
intermediate.0.to_string_lossy().into_owned()
} else {
output_path.clone()
};

// Build FFmpeg command
let ffmpeg_args = build_ffmpeg_args(
&audio_paths,
bgm_path.as_deref(),
bgm_volume,
&gaps_ms,
&output_path,
sleep_mode.unwrap_or(false),
&mix_path,
sleep_enabled,
&gains,
);

let _ = app.emit(
Expand Down Expand Up @@ -150,6 +172,18 @@ pub async fn export_audio_mix(
)));
}

if sleep_enabled {
let _ = app.emit(
"mix-progress",
MixProgress {
percent: 75.0,
stage: "正在平衡睡眠音轨响度".into(),
},
);
super::loudness::master_sleep(&intermediate.0, std::path::Path::new(&output_path), false)
.await?;
}

let _ = app.emit(
"mix-progress",
MixProgress {
Expand Down
80 changes: 22 additions & 58 deletions src-tauri/src/commands/audio/ffmpeg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
/// Each voice clip is normalized to -16 LUFS (EBU R128) using the `loudnorm` filter
/// to ensure consistent volume across TTS fragments before concatenation.
///
/// When `sleep_mode` is true, additional audio processing is applied to create a
/// soothing, sleep-friendly sound: slight pitch reduction, warmth boost (bass EQ),
/// gentle high-frequency rolloff, and reduced overall loudness target (-20 LUFS).
/// When `sleep_mode` is true, level speech using measured fixed gains. The caller masters the complete mix
/// (including BGM) with the shared two-pass sleep processor.
pub fn build_ffmpeg_args(
audio_paths: &[String],
bgm_path: Option<&str>,
bgm_volume: f32,
gaps_ms: &[i32],
output_path: &str,
sleep_mode: bool,
gains_db: &[f64],
) -> Vec<String> {
let n = audio_paths.len();
let mut args = Vec::new();
Expand All @@ -32,44 +32,14 @@ pub fn build_ffmpeg_args(
args.push(bgm.to_string());
}

// Single file without BGM or gaps: still normalize for consistency
if n == 1 && bgm_path.is_none() && (gaps_ms.is_empty() || gaps_ms[0] == 0) {
let mut filter = "[0:a]loudnorm=I=-16:TP=-1.5:LRA=11[voice]".to_string();
if sleep_mode {
// Sleep mode: warm tone + gentle rolloff + quieter target
filter.push_str(
";[voice]equalizer=f=200:t=q:w=0.8:g=3,\
equalizer=f=3000:t=q:w=1.0:g=-2,\
lowpass=f=8000:p=1,\
loudnorm=I=-20:TP=-2:LRA=7[out]",
);
args.push("-filter_complex".to_string());
args.push(filter);
args.push("-map".to_string());
args.push("[out]".to_string());
} else {
args.push("-filter_complex".to_string());
args.push(filter);
args.push("-map".to_string());
args.push("[voice]".to_string());
}
args.push(output_path.to_string());
return args;
}

let mut filter = String::new();

// Step 1: Normalize each voice clip to -16 LUFS (EBU R128)
// This ensures consistent volume across TTS fragments without distortion.
// loudnorm parameters:
// I=-16 target integrated loudness (LUFS)
// TP=-1.5 true peak limit (dBTP) — prevents clipping
// LRA=11 loudness range target (LU) — preserves natural dynamics
for i in 0..n {
filter.push_str(&format!(
"[{i}:a]loudnorm=I=-16:TP=-1.5:LRA=11[norm{i}];",
i = i
));
let leveling = if sleep_mode {
format!("volume={:.4}dB", gains_db.get(i).copied().unwrap_or(0.0))
} else {
"loudnorm=I=-16:TP=-1.5:LRA=11".to_string()
};
filter.push_str(&format!("[{i}:a]{leveling},aresample=48000,aformat=sample_fmts=fltp:channel_layouts=stereo,asetpts=PTS-STARTPTS[norm{i}];"));
}

// Check if any gap > 0 exists between clips
Expand All @@ -83,7 +53,7 @@ pub fn build_ffmpeg_args(
if gap > 0 {
let gap_sec = gap as f64 / 1000.0;
filter.push_str(&format!(
"anullsrc=r=44100:cl=stereo[sil{s}];[sil{s}]atrim=0:{dur}[gap{s}];",
"anullsrc=r=48000:cl=stereo[sil{s}];[sil{s}]atrim=0:{dur}[gap{s}];",
s = i,
dur = gap_sec
));
Expand Down Expand Up @@ -113,28 +83,18 @@ pub fn build_ffmpeg_args(
}
}

// Sleep mode: apply soothing audio processing to the concatenated voice
// - equalizer f=200 g=3: gentle bass warmth (adds body/comfort to voice)
// - equalizer f=3000 g=-2: slight upper-mid reduction (less harsh/bright)
// - lowpass f=8000: roll off high frequencies (removes sibilance/sharpness)
// - loudnorm I=-20: quieter target loudness (sleep-appropriate level)
let voice_label = if sleep_mode {
filter.push_str(
";[voice]equalizer=f=200:t=q:w=0.8:g=3,\
equalizer=f=3000:t=q:w=1.0:g=-2,\
lowpass=f=8000:p=1,\
loudnorm=I=-20:TP=-2:LRA=7[sleepvoice]",
);
"[sleepvoice]"
} else {
"[voice]"
};
let voice_label = "[voice]";

if bgm_path.is_some() {
let bgm_idx = n;
let mixing = if sleep_mode {
"dropout_transition=0:normalize=0"
} else {
"dropout_transition=2"
};
filter.push_str(&format!(
";[{}:a]volume={}[bgm];{}[bgm]amix=inputs=2:duration=first:dropout_transition=2[out]",
bgm_idx, bgm_volume, voice_label
";[{}:a]volume={}[bgm];{}[bgm]amix=inputs=2:duration=first:{}[out]",
bgm_idx, bgm_volume, voice_label, mixing
));
args.push("-filter_complex".to_string());
args.push(filter);
Expand All @@ -147,6 +107,10 @@ pub fn build_ffmpeg_args(
args.push(voice_label.to_string());
}

if sleep_mode {
// Floating-point intermediate avoids clipping before final mastering.
args.extend(["-c:a".to_string(), "pcm_f32le".to_string()]);
}
args.push(output_path.to_string());
args
}
Expand Down
Loading
Loading