voice vignette

version 0.4.20

Filipe J. Zabala

2023-04-18

0. Installation

https://github.com/filipezabala/voice

1. Extract features

Load packages and audio files

# packs
library(voice)

# get path to audio file
wavDir <- list.files(system.file('extdata', package = 'wrassp'),
                     pattern = glob2rx('*.wav'), full.names = TRUE)

Examples

# minimal usage
M <- voice::extract_features(wavDir)
M
#> # A tibble: 1,193 × 65
#>    section_seq section_seq_file wav_path        f0    f1    f2    f3    f4    f5
#>          <int>            <int> <chr>        <dbl> <int> <int> <int> <int> <int>
#>  1           1                1 /Library/Fr…   NA     NA  1854    NA  3113  4191
#>  2           2                2 /Library/Fr…   NA     NA  1886  2893  3708  4678
#>  3           3                3 /Library/Fr…   NA     NA  1749  2676  3509  4502
#>  4           4                4 /Library/Fr…   NA     NA  1888  2659  3658  4331
#>  5           5                5 /Library/Fr…   NA    185  1962  2639  3248  3653
#>  6           6                6 /Library/Fr…   NA    260  1973  2676  3239  3836
#>  7           7                7 /Library/Fr…   NA    254  2026  2993  3830  4602
#>  8           8                8 /Library/Fr…  116.   277  2037  2932  3479  4585
#>  9           9                9 /Library/Fr…  109.   261  2130  3016  3561  4720
#> 10          10               10 /Library/Fr…  107.   231  2301  2907  3889  4818
#> # ℹ 1,183 more rows
#> # ℹ 56 more variables: f6 <int>, f7 <int>, f8 <int>, rfc1 <dbl>, rfc2 <dbl>,
#> #   rfc3 <dbl>, rfc4 <dbl>, rfc5 <dbl>, rfc6 <dbl>, rfc7 <dbl>, rfc8 <dbl>,
#> #   rfc9 <dbl>, rfc10 <dbl>, rfc11 <dbl>, rfc12 <dbl>, rfc13 <dbl>,
#> #   rfc14 <dbl>, rfc15 <dbl>, rfc16 <dbl>, rfc17 <dbl>, rfc18 <dbl>,
#> #   rfc19 <dbl>, mfcc1 <dbl>, mfcc2 <dbl>, mfcc3 <dbl>, mfcc4 <dbl>,
#> #   mfcc5 <dbl>, mfcc6 <dbl>, mfcc7 <dbl>, mfcc8 <dbl>, mfcc9 <dbl>, …

2. Tag

# creating Extended synthetic data
E <- dplyr::tibble(subject_id = c(1,1,1,2,2,2,3,3,3), wav_path = wavDir)
E
#> # A tibble: 9 × 2
#>   subject_id wav_path                                                           
#>        <dbl> <chr>                                                              
#> 1          1 /Library/Frameworks/R.framework/Versions/4.2/Resources/library/wra…
#> 2          1 /Library/Frameworks/R.framework/Versions/4.2/Resources/library/wra…
#> 3          1 /Library/Frameworks/R.framework/Versions/4.2/Resources/library/wra…
#> 4          2 /Library/Frameworks/R.framework/Versions/4.2/Resources/library/wra…
#> 5          2 /Library/Frameworks/R.framework/Versions/4.2/Resources/library/wra…
#> 6          2 /Library/Frameworks/R.framework/Versions/4.2/Resources/library/wra…
#> 7          3 /Library/Frameworks/R.framework/Versions/4.2/Resources/library/wra…
#> 8          3 /Library/Frameworks/R.framework/Versions/4.2/Resources/library/wra…
#> 9          3 /Library/Frameworks/R.framework/Versions/4.2/Resources/library/wra…

# minimal usage
voice::tag(E)
#> # A tibble: 9 × 7
#>   wav_path   f0_tag_mean f0_tag_sd f0_tag_vc f0_tag_median f0_tag_iqr f0_tag_mad
#>   <chr>            <dbl>     <dbl>     <dbl>         <dbl>      <dbl>      <dbl>
#> 1 /Library/…        85.4      17.6     0.206          76.1       29.4       7.53
#> 2 /Library/…        85.4      15.6     0.183          80.1       27.8      14.4 
#> 3 /Library/…        84.6      13.0     0.154          78.8       23.9      14.0 
#> 4 /Library/…        84.8      14.5     0.171          79.1       28.1      11.9 
#> 5 /Library/…        86.0      14.7     0.170          78.7       30.0      11.0 
#> 6 /Library/…        82.9      15.6     0.188          74.8       23.8       4.78
#> 7 /Library/…        78.2      16.2     0.207          73.5       13.4       6.82
#> 8 /Library/…        84.5      14.5     0.172          78.1       17.8       8.95
#> 9 /Library/…        81.0      12.2     0.151          75.9       23.1       9.14

# canonical data
voice::tag(E, groupBy = 'subject_id')
#> # A tibble: 3 × 7
#>   subject_id f0_tag_mean f0_tag_sd f0_tag_vc f0_tag_median f0_tag_iqr f0_tag_mad
#>        <dbl>       <dbl>     <dbl>     <dbl>         <dbl>      <dbl>      <dbl>
#> 1          1        85.1      15.3     0.180          78.3       26.8      11.9 
#> 2          2        84.6      14.9     0.176          76.4       28.3       7.97
#> 3          3        81.0      14.6     0.180          75.6       21.6       8.68

3. Voice2Sheet (experimental)

3.1. Extract features

Get audio

url0 <- 'https://github.com/filipezabala/voiceAudios/blob/main/wav/doremi.wav?raw=true'
download.file(url0, paste0(tempdir(), '/doremi.wav'), mode = 'wb')
# embedr::embed_audio(url0)X

Extract features

M <- voice::extract_features(tempdir(), features = 'f0', round.to = 6)
summary(M)
#>   section_seq    section_seq_file   wav_path               f0       
#>  Min.   :  1.0   Min.   :  1.0    Length:591         Min.   :121.3  
#>  1st Qu.:148.5   1st Qu.:148.5    Class :character   1st Qu.:147.8  
#>  Median :296.0   Median :296.0    Mode  :character   Median :204.8  
#>  Mean   :296.0   Mean   :296.0                       Mean   :208.7  
#>  3rd Qu.:443.5   3rd Qu.:443.5                       3rd Qu.:259.6  
#>  Max.   :591.0   Max.   :591.0                       Max.   :339.1  
#>                                                      NA's   :111

Plot

# Plot
plot(M$f0)
legend(-40, 170, 'Do (C)' , bty = 'n')
legend(100, 190, 'Re (D)' , bty = 'n')
legend(240, 290, 'Mi (E)' , bty = 'n')
legend(370, 220, 'Fa (F)' , bty = 'n')
legend(500, 250, 'Sol (G)', bty = 'n')
legend(620, 270, 'La (A)' , bty = 'n')
legend(770, 300, 'Si (B)' , bty = 'n')
legend(900, 320, 'Do (C)' , bty = 'n')

3.2. Music sheet

Compress to 1%

# compress
M1 <- voice::interp_df(M, 0.01, id = 3)

# assign notes
M1$f0_spn <- voice::notes(M1$f0)

# duration
d1 <- voice::duration(M1$f0_spn)

# gm by Renfei Mao
library(gm)
m <- gm::Music()
m <- m +
  gm::Meter(4, 4) +
  gm::Line(pitches = as.list(as.character(d1$note)),
           durations = as.list(d1$dur_line)) +
  gm::Tempo(170)
gm::show(m, to = c('score', 'audio'))