Skip to content

Commit

Permalink
added changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellga committed Jun 22, 2024
1 parent 53ac6d2 commit 26d61e9
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 152 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

# [Unreleased]

# [0.1.0] - 2024-XX-XX.

### Added
- Initial version
65 changes: 38 additions & 27 deletions _docs/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,95 +6,106 @@ Harmonium is an audio interface inspired by Python's [librosa](https://github.co

# Basic usage

Load the library
Load the library.
```r
library(harmonium)
```

Create an `HArray` from an existing array
Create an `HArray` from an existing array.
```r
arr = matrix(c(1,2,3,4,5,6,7,8,9,10,11,12), ncol = 2)
dtype = HDataType$float64
harray = HArray$new_from_values(arr, dtype = dtype)
dtype = HDataType$Float64
harray = HArray$new_from_values(arr, dtype)
print(harray)
```

Or decode from an existing audio file
Or decode from an existing audio file.
```r
l = HFile$decode("file.wav", dtype)
harray = l[[0]]
sr = l[[1]] # sampling rate
decoded_audio = HFile$decode("file.wav", dtype)
harray = decoded_audio$harray()
sr = decoded_audio$sr() # sampling rate
print(harray)
```

Verify the dimensions
Verify the dimensions.
```r
harray$len()
HAudioOp$nchannels(harray)
HAudioOp$nframes(harray)
```

Verify the data type
Verify the data type.
```r
harray$dtype()
```

Convert to an R array
Convert to an R array.
```r
harray$collect()
```

Convert to mono
Convert to mono.
```r
HAudioOp$to_mono(harray)
print(harray)
```

Resample the haudio
Resample the haudio.
```r
arr = matrix(as.double(1:8192), ncol = 2)
dtype = HDataType$Float64
harray = HArray$new_from_values(arr, dtype)
sr_in = 48000L
sr_out = 22050L
nbr_channels = HAudioOp$nchannels(harray)
dtype = HDataType$float64
res_type = HResamplerType$fft_fixed_in
resampler = HResampler$new_fft(sr_in, sr_out, nbr_channels, res_type)
sr_out = 44100L
chunk_size = 1024L
sub_chunks = 2L
nbr_channels = 2L
res_type = HResamplerType$FftFixedIn
dtype = HDataType$Float64
hresampler = HResampler$new_fft(sr_in, sr_out, chunk_size, sub_chunks, nbr_channels, res_type, dtype)
hresampler$process(harray)
```

Play the haudio
Play the haudio.
```r
dtype = HDataType$Float32
decoded_audio = HFile$decode("file.wav", dtype)
harray = decoded_audio$harray()
sr = decoded_audio$sr() # sampling rate
haudiosink = HAudioSink$new()
haudiosink$append_from_harray(harray)
haudiosink$append_from_harray(harray, sr)
```

Or play directly from the file
Or play directly from the file.
```r
haudiosink = HAudioSink$new()
haudiosink$append_from_file("file.wav")
```

Get the number of audios to be played
Get the number of audios to be played.
```r
haudiosink$len()
```

Double the audio volume and the playback speed
Double the audio volume and the playback speed.
```r
haudiosink$set_volume(2)
haudiosink$set_speed(2)
```

Pause the playback and confirms it is paused
Pause the playback and confirms it is paused.
```r
haudiosink$pause()
haudiosink$is_paused()
```

It is also possible to get metadata from an audio file
It is also possible to get metadata from an audio file.
```r
metatadatype = HMetadataType$text
metatadatype = HMetadataType$Text
HFile$metadata("file.wav", metatadatype)
```

And to get some audio parameters directly from a file
And to get some audio parameters directly from a file.
```r
params = HFile$params("file.wav")
sr = params[[1]]
Expand Down
2 changes: 1 addition & 1 deletion r-harmonium/.testtest.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pkgbuild::clean_dll()
devtools::load_all(".", export_all = FALSE)
devtools::test()
devtools::check(document = FALSE, cran = FALSE, args = c("--no-manual", "--no-build-vignettes", "--no-codoc", "--no-examples", "--no-tests"))
devtools::check(document = FALSE, cran = FALSE, args = c("--no-manual", "--no-build-vignettes", "--no-codoc", "--no-examples", "--no-tests"))
20 changes: 10 additions & 10 deletions r-harmonium/src/rust/src/conversions.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use savvy::{OwnedIntegerSexp, Sexp, TypedSexp};

pub(crate) trait AsScalar<T> {
fn as_scalar(self) -> savvy::Result<T>;
pub(crate) trait ToScalar<T> {
fn to_scalar(self) -> savvy::Result<T>;
}

impl AsScalar<&'static str> for Sexp {
fn as_scalar(self) -> savvy::Result<&'static str> {
impl ToScalar<&'static str> for Sexp {
fn to_scalar(self) -> savvy::Result<&'static str> {
match self.into_typed() {
TypedSexp::String(string_sexp) if string_sexp.len() == 1 => unsafe {
Ok(string_sexp
Expand All @@ -22,8 +22,8 @@ impl AsScalar<&'static str> for Sexp {
}
}

impl AsScalar<i32> for Sexp {
fn as_scalar(self) -> savvy::Result<i32> {
impl ToScalar<i32> for Sexp {
fn to_scalar(self) -> savvy::Result<i32> {
match self.into_typed() {
TypedSexp::Integer(integer_sexp) if integer_sexp.len() == 1 => {
Ok(integer_sexp.as_slice()[0])
Expand All @@ -36,8 +36,8 @@ impl AsScalar<i32> for Sexp {
}
}

impl AsScalar<f64> for Sexp {
fn as_scalar(self) -> savvy::Result<f64> {
impl ToScalar<f64> for Sexp {
fn to_scalar(self) -> savvy::Result<f64> {
match self.into_typed() {
TypedSexp::Real(real_sexp) if real_sexp.len() == 1 => Ok(real_sexp.as_slice()[0]),
_ => {
Expand All @@ -48,8 +48,8 @@ impl AsScalar<f64> for Sexp {
}
}

impl AsScalar<bool> for Sexp {
fn as_scalar(self) -> savvy::Result<bool> {
impl ToScalar<bool> for Sexp {
fn to_scalar(self) -> savvy::Result<bool> {
match self.into_typed() {
TypedSexp::Logical(logical_sexp) if logical_sexp.len() == 1 => {
Ok(logical_sexp.as_slice_raw()[0] == 1)
Expand Down
6 changes: 3 additions & 3 deletions r-harmonium/src/rust/src/haudioop.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
conversions::{try_from_usize_to_int_sexp, AsScalar},
conversions::{try_from_usize_to_int_sexp, ToScalar},
harray::HArray,
};
use savvy::{savvy, Sexp};
Expand Down Expand Up @@ -117,8 +117,8 @@ impl HAudioOp {
///
fn db_to_amplitude(harray: &mut HArray, reference: Sexp, power: Sexp) -> savvy::Result<()> {
let inner_mut = harray.get_inner_mut();
let reference: f64 = reference.as_scalar()?;
let power: f64 = power.as_scalar()?;
let reference: f64 = reference.to_scalar()?;
let power: f64 = power.to_scalar()?;
inner_mut.db_to_amplitude(reference, power)
}

Expand Down
12 changes: 6 additions & 6 deletions r-harmonium/src/rust/src/haudiosink.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
conversions::{try_from_usize_to_int_sexp, AsScalar},
conversions::{try_from_usize_to_int_sexp, ToScalar},
errors::HErrorR,
harray::HArray,
hdatatype::HDataType,
Expand Down Expand Up @@ -76,7 +76,7 @@ impl HAudioSink {
/// _________
///
fn append_from_harray(&self, harray: &HArray, sr: Sexp) -> savvy::Result<()> {
let sr: i32 = sr.as_scalar()?;
let sr: i32 = sr.to_scalar()?;
let sr = sr
.try_into()
.map_err(|_| savvy::Error::new("Cannot convert i32 to u32."))?;
Expand Down Expand Up @@ -136,7 +136,7 @@ impl HAudioSink {
/// _________
///
fn append_from_file(&self, fpath: Sexp) -> savvy::Result<()> {
let fpath: &str = fpath.as_scalar()?;
let fpath: &str = fpath.to_scalar()?;

let (harray, sr) = decode::<f32>(fpath).map_err(HErrorR::from)?;
let audio = Audio::D2(&harray);
Expand Down Expand Up @@ -430,7 +430,7 @@ impl HAudioSink {
/// _________
///
fn set_speed(&self, value: Sexp) -> savvy::Result<()> {
let value: f64 = value.as_scalar()?;
let value: f64 = value.to_scalar()?;
self.0.set_speed(value as f32);
Ok(())
}
Expand Down Expand Up @@ -463,7 +463,7 @@ impl HAudioSink {
/// _________
///
fn set_volume(&self, value: Sexp) -> savvy::Result<()> {
let value: f64 = value.as_scalar()?;
let value: f64 = value.to_scalar()?;
self.0.set_volume(value as f32);
Ok(())
}
Expand Down Expand Up @@ -617,7 +617,7 @@ impl HAudioSink {
/// _________
///
fn try_seek(&self, pos: Sexp) -> savvy::Result<()> {
let pos: f64 = pos.as_scalar()?;
let pos: f64 = pos.to_scalar()?;
let pos = std::time::Duration::from_secs_f64(pos);
self.0.try_seek(pos).map_err(HErrorR::from)?;
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions r-harmonium/src/rust/src/hfft.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{conversions::AsScalar, errors::HErrorR, harray::HArray, hdatatype::HDataType};
use crate::{conversions::ToScalar, errors::HErrorR, harray::HArray, hdatatype::HDataType};
use ndarray::IxDyn;
use num_complex::Complex;
use realfft::RealFftPlanner;
Expand Down Expand Up @@ -402,7 +402,7 @@ impl HRealFftPlanner {
/// _________
///
fn irfft(&mut self, harray: &mut HArray, length: Sexp) -> savvy::Result<()> {
let length: i32 = length.as_scalar()?;
let length: i32 = length.to_scalar()?;
let length: usize = length
.try_into()
.map_err(|_| savvy::Error::new("Cannot convert i32 to usize."))?;
Expand Down
16 changes: 7 additions & 9 deletions r-harmonium/src/rust/src/hfile.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
conversions::AsScalar, errors::HErrorR, harray::HArray, hdatatype::HDataType,
conversions::ToScalar, errors::HErrorR, harray::HArray, hdatatype::HDataType,
hmetadatatype::HMetadataType,
};
use harmonium_core::conversions::IntoDynamic;
Expand Down Expand Up @@ -55,7 +55,6 @@ impl HDecodedAudio {
/// dtype = HDataType$Float32
/// hdecodedaudio = HFile$decode(fpath, dtype)
/// hdecodedaudio$harray()
///
/// ```
///
/// _________
Expand All @@ -82,7 +81,6 @@ impl HDecodedAudio {
/// dtype = HDataType$Float32
/// hdecodedaudio = HFile$decode(fpath, dtype)
/// hdecodedaudio$sr()
///
/// ```
///
/// _________
Expand Down Expand Up @@ -164,7 +162,7 @@ impl HFile {
/// _________
///
fn decode(fpath: Sexp, dtype: &HDataType) -> savvy::Result<HDecodedAudio> {
let fpath: &str = fpath.as_scalar()?;
let fpath: &str = fpath.to_scalar()?;
match dtype {
HDataType::Float32 => {
let (harray, sr) =
Expand Down Expand Up @@ -226,8 +224,8 @@ impl HFile {
frames: Sexp,
dtype: &HDataType,
) -> savvy::Result<HDecoderStream> {
let fpath: &str = fpath.as_scalar()?;
let frames: i32 = frames.as_scalar()?;
let fpath: &str = fpath.to_scalar()?;
let frames: i32 = frames.to_scalar()?;
let frames = usize::try_from(frames)
.map_err(|_| savvy::Error::new("Cannot convert i32 to usize."))?;
match dtype {
Expand Down Expand Up @@ -364,7 +362,7 @@ impl HFile {
/// _________
///
fn metadata(fpath: Sexp, metadata_type: &HMetadataType) -> savvy::Result<Sexp> {
let fpath: &str = fpath.as_scalar()?;
let fpath: &str = fpath.to_scalar()?;
let metadata_type = match metadata_type {
HMetadataType::All => decode::HMetadataType::All,
HMetadataType::Text => decode::HMetadataType::Text,
Expand Down Expand Up @@ -439,7 +437,7 @@ impl HFile {
/// _________
///
fn params(fpath: Sexp) -> savvy::Result<Sexp> {
let fpath: &str = fpath.as_scalar()?;
let fpath: &str = fpath.to_scalar()?;
let (sr, nframes, nchannels, duration) =
decode::get_params_from_file(fpath).map_err(HErrorR::from)?;
let arr = [sr as f64, nframes as f64, nchannels as f64, duration];
Expand Down Expand Up @@ -479,7 +477,7 @@ impl HFile {
/// _________
///
fn verify(fpath: Sexp) -> savvy::Result<Sexp> {
let fpath: &str = fpath.as_scalar()?;
let fpath: &str = fpath.to_scalar()?;
let verified = match decode::verify_file(fpath).map_err(HErrorR::from)? {
decode::HVerifyDecode::Passed => "passed",
decode::HVerifyDecode::Failed => "failed",
Expand Down
Loading

0 comments on commit 26d61e9

Please sign in to comment.