Skip to content

Commit

Permalink
Feature: rustg_sound_length() (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kapu1178 authored Nov 23, 2024
1 parent 79b8d73 commit df8ea42
Show file tree
Hide file tree
Showing 7 changed files with 389 additions and 0 deletions.
185 changes: 185 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ ammonia = { version = "4.0.0", optional = true }
fast_poisson = { version = "=0.5.2", optional = true, features = [
"single_precision",
] } # Higher versions have problems with x86 due to 'kiddo'.
symphonia = { version = "0.5.4", optional = true, features = [
"all-codecs",
] }

[features]
default = [
Expand All @@ -79,6 +82,7 @@ default = [
"noise",
"rustls_tls",
"sanitize",
"sound_len",
"sql",
"time",
"toml",
Expand All @@ -98,6 +102,7 @@ all = [
"noise",
"rustls_tls",
"sanitize",
"sound_len",
"sql",
"time",
"toml",
Expand All @@ -123,6 +128,7 @@ http = ["ureq", "serde", "serde_json", "once_cell", "jobs"]
json = ["serde", "serde_json"]
log = ["chrono"]
sanitize = ["ammonia", "serde_json"]
sound_len = ["symphonia"]
sql = ["mysql", "serde", "serde_json", "once_cell", "dashmap", "jobs"]
time = []
toml = ["serde", "serde_json", "toml-dep"]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ The default features are:
* json: Function to check JSON validity.
* log: Faster log output.
* noise: 2d Perlin noise.
* sound_len: A mostly codec-agnostic library for reading the duration of an audio file.
* sql: Asynchronous MySQL/MariaDB client library.
* time: High-accuracy time measuring.
* toml: TOML parser.
Expand Down
42 changes: 42 additions & 0 deletions dmsrc/sound_len.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/// Provided a static RSC file path or a raw text file path, returns the duration of the file in deciseconds as a float.
/proc/rustg_sound_length(file_path)
var/static/list/sound_cache
if(isnull(sound_cache))
sound_cache = list()

. = 0

if(!istext(file_path))
if(!isfile(file_path))
CRASH("rustg_sound_length error: Passed non-text object")

if(length("[file_path]")) // Runtime generated RSC references stringify into 0-length strings.
file_path = "[file_path]"
else
CRASH("rustg_sound_length does not support non-static file refs.")

var/cached_length = sound_cache[file_path]
if(!isnull(cached_length))
return cached_length

var/ret = RUSTG_CALL(RUST_G, "sound_len")(file_path)
var/as_num = text2num(ret)
if(isnull(ret))
. = 0
CRASH("rustg_sound_length error: [ret]")

sound_cache[file_path] = as_num
return as_num


#define RUSTG_SOUNDLEN_SUCCESSES "successes"
#define RUSTG_SOUNDLEN_ERRORS "errors"
/**
* Returns a nested key-value list containing "successes" and "errors"
* The format is as follows:
* list(
* RUSTG_SOUNDLEN_SUCCESES = list("sounds/test.ogg" = 25.34),
* RUSTG_SOUNDLEN_ERRORS = list("sound/bad.png" = "SoundLen: Unable to decode file."),
*)
*/
#define rustg_sound_length_list(file_paths) json_decode(RUSTG_CALL(RUST_G, "sound_len_list")(json_encode(file_paths)))
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ pub enum Error {
#[cfg(feature = "http")]
#[error(transparent)]
Request(#[from] Box<ureq::Error>),
#[cfg(feature = "sound_len")]
#[error("SoundLen error: {0}")]
SoundLen(String),
#[cfg(feature = "toml")]
#[error(transparent)]
TomlDeserialization(#[from] toml_dep::de::Error),
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub mod redis_pubsub;
pub mod redis_reliablequeue;
#[cfg(feature = "sanitize")]
pub mod sanitize;
#[cfg(feature = "sound_len")]
pub mod sound_len;
#[cfg(feature = "sql")]
pub mod sql;
#[cfg(feature = "time")]
Expand Down
Loading

0 comments on commit df8ea42

Please sign in to comment.