Skip to content

Commit

Permalink
get_pos
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellga committed Jul 3, 2024
1 parent 3418b7f commit 416b160
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ realfft = "3.3"
rubato = "0.15.0"
ndarray = "0.15.6"
symphonia = { version = "0.5.4", default-features = false }
rodio = { version = "0.18.1", default-features = false }
rodio = { version = "0.19.0", default-features = false }
num-complex = { version = "0.4" }

harmonium-core = { path = "harmonium-core", default-features = false }
Expand Down
7 changes: 7 additions & 0 deletions harmonium-io/src/play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ impl HAudioSink {
self.sink.detach();
}

/// Returns the position of the sound that’s being played.
/// This takes into account any speedup or delay applied.
/// Example: if you apply a speedup of 2 to an mp3 decoder source and get_pos() returns 5s then the position in the mp3 recording is 10s from its start.
pub fn get_pos(&self) -> Duration {
self.sink.get_pos()
}

/// Returns true if this sink has no more sounds to play.
pub fn is_empty(&self) -> bool {
self.sink.empty()
Expand Down
7 changes: 7 additions & 0 deletions r-harmonium/R/000-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ HAudioSink_clear <- function(self) {
}
}

HAudioSink_get_pos <- function(self) {
function() {
.Call(savvy_HAudioSink_get_pos__impl, self)
}
}

HAudioSink_is_empty <- function(self) {
function() {
.Call(savvy_HAudioSink_is_empty__impl, self)
Expand Down Expand Up @@ -304,6 +310,7 @@ HAudioSink_invalidate <- function(self) {
e$append_from_harray <- HAudioSink_append_from_harray(ptr)
e$append_from_file <- HAudioSink_append_from_file(ptr)
e$clear <- HAudioSink_clear(ptr)
e$get_pos <- HAudioSink_get_pos(ptr)
e$is_empty <- HAudioSink_is_empty(ptr)
e$is_paused <- HAudioSink_is_paused(ptr)
e$len <- HAudioSink_len(ptr)
Expand Down
6 changes: 6 additions & 0 deletions r-harmonium/src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ SEXP savvy_HAudioSink_clear__impl(SEXP self__) {
return handle_result(res);
}

SEXP savvy_HAudioSink_get_pos__impl(SEXP self__) {
SEXP res = savvy_HAudioSink_get_pos__ffi(self__);
return handle_result(res);
}

SEXP savvy_HAudioSink_is_empty__impl(SEXP self__) {
SEXP res = savvy_HAudioSink_is_empty__ffi(self__);
return handle_result(res);
Expand Down Expand Up @@ -549,6 +554,7 @@ static const R_CallMethodDef CallEntries[] = {
{"savvy_HAudioSink_audio_output_devices__impl", (DL_FUNC) &savvy_HAudioSink_audio_output_devices__impl, 0},
{"savvy_HAudioSink_audio_supported_configs__impl", (DL_FUNC) &savvy_HAudioSink_audio_supported_configs__impl, 0},
{"savvy_HAudioSink_clear__impl", (DL_FUNC) &savvy_HAudioSink_clear__impl, 1},
{"savvy_HAudioSink_get_pos__impl", (DL_FUNC) &savvy_HAudioSink_get_pos__impl, 1},
{"savvy_HAudioSink_is_empty__impl", (DL_FUNC) &savvy_HAudioSink_is_empty__impl, 1},
{"savvy_HAudioSink_is_paused__impl", (DL_FUNC) &savvy_HAudioSink_is_paused__impl, 1},
{"savvy_HAudioSink_len__impl", (DL_FUNC) &savvy_HAudioSink_len__impl, 1},
Expand Down
12 changes: 6 additions & 6 deletions r-harmonium/src/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ panic = "unwind" # otherwise savvy crashes R when panic.
# prevents package from thinking it's in the workspace.

[dependencies]
#harmonium-core = { path = "../../../harmonium-core" }
#harmonium-io = { path = "../../../harmonium-io" }
#harmonium-resample = { path = "../../../harmonium-resample" }
#harmonium-fft = { path = "../../../harmonium-fft" }
#harmonium-window = { path = "../../../harmonium-window" }
# harmonium-core = { path = "../../../harmonium-core" }
# harmonium-io = { path = "../../../harmonium-io" }
# harmonium-resample = { path = "../../../harmonium-resample" }
# harmonium-fft = { path = "../../../harmonium-fft" }
# harmonium-window = { path = "../../../harmonium-window" }
harmonium-core = { git = "https://github.com/daniellga/harmonium.git" }
harmonium-io = { git = "https://github.com/daniellga/harmonium.git", features = ["all", "opt-simd"] }
harmonium-resample = { git = "https://github.com/daniellga/harmonium.git" }
Expand All @@ -32,7 +32,7 @@ rustfft = "6.2"
realfft = "3.3"
ndarray = "0.15.6"
num-complex = "0.4"
savvy = { git = "https://github.com/yutannihilation/savvy.git", features = ["complex"] }
savvy = { version = "0.6.5", features = ["complex"] }

[profile.dev]
debug = 1 # less precise locations. Reduce size of target dir.
Expand Down
1 change: 1 addition & 0 deletions r-harmonium/src/rust/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ SEXP savvy_HAudioSink_audio_default_device__ffi(void);
SEXP savvy_HAudioSink_audio_output_devices__ffi(void);
SEXP savvy_HAudioSink_audio_supported_configs__ffi(void);
SEXP savvy_HAudioSink_clear__ffi(SEXP self__);
SEXP savvy_HAudioSink_get_pos__ffi(SEXP self__);
SEXP savvy_HAudioSink_is_empty__ffi(SEXP self__);
SEXP savvy_HAudioSink_is_paused__ffi(SEXP self__);
SEXP savvy_HAudioSink_len__ffi(SEXP self__);
Expand Down
29 changes: 29 additions & 0 deletions r-harmonium/src/rust/src/haudiosink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,35 @@ impl HAudioSink {
Ok(())
}

/// HAudioSink
/// ## get_pos
///
/// `get_pos() -> double`
///
/// Returns the position of the sound that’s being played.
/// This takes into account any speedup or delay applied.
/// Example: if you apply a speedup of 2 to an mp3 decoder source and get_pos() returns 5s then the position in the mp3 recording is 10s from its start.
///
/// #### Returns
///
/// A `double`.
///
/// #### Examples
///
/// ```r
/// library(harmonium)
/// haudiosink = HAudioSink$new()
/// haudiosink$append_from_file(fpath = "./r-harmonium/testfiles/gs-16b-2c-44100hz.wav")
/// haudiosink$get_pos()
/// ```
///
/// _________
///
pub fn get_pos(&self) -> savvy::Result<Sexp> {
let duration = self.0.get_pos().as_secs_f64();
duration.try_into()
}

/// HAudioSink
/// ## is_empty
///
Expand Down

0 comments on commit 416b160

Please sign in to comment.