Skip to content

Commit 5499192

Browse files
committed
fix(game): properly get replaygain from audio file
1 parent 2967e68 commit 5499192

File tree

6 files changed

+65
-103
lines changed

6 files changed

+65
-103
lines changed

apps/game/src-tauri/src/commands/meta.rs

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
pub mod meta;
21
pub mod microphones;
32
pub mod pitch;
43
pub mod songs;

apps/game/src-tauri/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ impl Default for AppState {
3636
pub fn run() {
3737
let builder = Builder::<tauri::Wry>::new()
3838
.commands(collect_commands![
39-
meta::get_replay_gain,
4039
microphones::get_microphones,
4140
pitch::start_recording,
4241
pitch::stop_recording,

apps/game/src-tauri/src/ultrastar/parser.rs

Lines changed: 65 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,18 @@ fn tag_to_note_type(tag: &str) -> NoteType {
2222
}
2323

2424
fn parse_us_int(value: &str, property: &str) -> Result<i32, AppError> {
25-
value
26-
.replace(",", ".")
27-
.parse::<i32>()
28-
.map_err(|_| {
29-
AppError::UltrastarError(format!(
30-
"Failed to parse integer for {}: {}",
31-
property, value
32-
))
33-
})
25+
value.replace(",", ".").parse::<i32>().map_err(|_| {
26+
AppError::UltrastarError(format!(
27+
"Failed to parse integer for {}: {}",
28+
property, value
29+
))
30+
})
3431
}
3532

3633
fn parse_us_float(value: &str, property: &str) -> Result<f64, AppError> {
37-
value
38-
.replace(",", ".")
39-
.parse::<f64>()
40-
.map_err(|_| {
41-
AppError::UltrastarError(format!(
42-
"Failed to parse float for {}: {}",
43-
property, value
44-
))
45-
})
34+
value.replace(",", ".").parse::<f64>().map_err(|_| {
35+
AppError::UltrastarError(format!("Failed to parse float for {}: {}", property, value))
36+
})
4637
}
4738

4839
fn parse_us_bool(value: &str) -> bool {
@@ -235,50 +226,73 @@ pub fn parse_local_txt_file(
235226
let content = fs::read_to_string(txt)?;
236227
let song = parse_ultrastar_txt(&content)?;
237228

238-
let create_url =
239-
|filename: &Option<String>, file_type: &str| -> Result<Option<String>, AppError> {
240-
if let Some(filename) = filename {
241-
let normalized_target = filename.nfc().collect::<String>().to_lowercase();
229+
let find_file = |filename: &Option<String>| -> Option<&FileEntry> {
230+
if let Some(filename) = filename {
231+
let normalized_target = filename.nfc().collect::<String>().to_lowercase();
232+
files.iter().find(|f| {
233+
let normalized_file = f.filename.nfc().collect::<String>().to_lowercase();
234+
normalized_file == normalized_target
235+
})
236+
} else {
237+
None
238+
}
239+
};
242240

243-
if let Some(file_entry) = files.iter().find(|f| {
244-
let normalized_file = f.filename.nfc().collect::<String>().to_lowercase();
245-
normalized_file == normalized_target
246-
}) {
247-
let path = dunce::canonicalize(&file_entry.path)?;
248-
let path_string = path.to_string_lossy();
249-
let encoded = urlencoding::encode(&path_string);
250-
return Ok(Some(format!("{}/{}", media_base_url, encoded)));
251-
} else {
252-
// File was specified but not found
253-
if file_type == "audio" {
254-
return Err(AppError::UltrastarError(format!(
255-
"Audio file '{}' was specified but not found",
256-
filename
257-
)));
258-
} else {
259-
log::warn!(
260-
"{} file '{}' was specified but not found",
261-
file_type,
262-
filename
263-
);
264-
}
265-
}
241+
let create_url_from_file =
242+
|file_entry: Option<&FileEntry>| -> Result<Option<String>, AppError> {
243+
if let Some(file_entry) = file_entry {
244+
let path = dunce::canonicalize(&file_entry.path)?;
245+
let path_string = path.to_string_lossy();
246+
let encoded = urlencoding::encode(&path_string);
247+
Ok(Some(format!("{}/{}", media_base_url, encoded)))
248+
} else {
249+
Ok(None)
266250
}
267-
Ok(None)
268251
};
269252

270-
let audio_url = create_url(&song.audio, "Audio")?;
271-
let video_url = create_url(&song.video, "Video")?;
272-
let cover_url = create_url(&song.cover, "Cover")?;
273-
let background_url = create_url(&song.background, "Background")?;
253+
let audio_file = find_file(&song.audio);
254+
let video_file = find_file(&song.video);
255+
let cover_file = find_file(&song.cover);
256+
let background_file = find_file(&song.background);
257+
258+
if song.audio.is_some() && audio_file.is_none() {
259+
return Err(AppError::UltrastarError(format!(
260+
"Audio file '{}' was specified but not found",
261+
song.audio.as_ref().unwrap()
262+
)));
263+
}
264+
265+
if song.video.is_some() && video_file.is_none() {
266+
log::warn!(
267+
"Video file '{}' was specified but not found",
268+
song.video.as_ref().unwrap()
269+
);
270+
}
271+
if song.cover.is_some() && cover_file.is_none() {
272+
log::warn!(
273+
"Cover file '{}' was specified but not found",
274+
song.cover.as_ref().unwrap()
275+
);
276+
}
277+
if song.background.is_some() && background_file.is_none() {
278+
log::warn!(
279+
"Background file '{}' was specified but not found",
280+
song.background.as_ref().unwrap()
281+
);
282+
}
283+
284+
let audio_url = create_url_from_file(audio_file)?;
285+
let video_url = create_url_from_file(video_file)?;
286+
let cover_url = create_url_from_file(cover_file)?;
287+
let background_url = create_url_from_file(background_file)?;
274288

275289
if audio_url.is_none() && video_url.is_none() {
276290
return Err(AppError::UltrastarError(
277291
"Song must have either audio or video file available".to_string(),
278292
));
279293
}
280294

281-
let replay_gain = get_replay_gain(&txt).ok();
295+
let replay_gain = audio_file.and_then(|file| get_replay_gain(&file.path).ok());
282296

283297
Ok(LocalSong {
284298
song,

apps/game/src/bindings.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@
55

66

77
export const commands = {
8-
async getReplayGain(path: string) : Promise<Result<ReplayGainInfo, AppError>> {
9-
try {
10-
return { status: "ok", data: await TAURI_INVOKE("get_replay_gain", { path }) };
11-
} catch (e) {
12-
if(e instanceof Error) throw e;
13-
else return { status: "error", error: e as any };
14-
}
15-
},
168
async getMicrophones() : Promise<Result<Microphone[], AppError>> {
179
try {
1810
return { status: "ok", data: await TAURI_INVOKE("get_microphones") };
@@ -88,7 +80,6 @@ export type Note = { type: NoteType; startBeat: number; length: number; text: st
8880
export type NoteType = "Normal" | "Golden" | "Freestyle" | "Rap" | "RapGolden"
8981
export type Phrase = { disappearBeat: number; notes: Note[] }
9082
export type ProgressEvent = { song: string }
91-
export type ReplayGainInfo = { track_gain: number | null; track_peak: number | null; album_gain: number | null; album_peak: number | null }
9283
export type SongGroup = { path: string; songs: LocalSong[] }
9384
export type StartParsingEvent = { total_songs: number }
9485
export type Voice = { phrases: Phrase[] }

apps/game/src/routes/loading.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ function LoadingComponent() {
4141
const onProgress = (event: Event<ProgressEvent>) => {
4242
setCurrentSongs((currentSongs) => currentSongs + 1);
4343
setCurrentSong(event.payload.song);
44-
console.log(event.payload.song);
4544
};
4645

4746
const onStartParsing = (event: Event<StartParsingEvent>) => {

0 commit comments

Comments
 (0)