-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStrophe_Automatic_Sampling.qmd
156 lines (137 loc) · 7.01 KB
/
Strophe_Automatic_Sampling.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
---
title: "Automatic random construction of the audio files for the ecology practical"
author: "Paul PETIT"
format: pdf
editor: visual
---
::: {style="text-align: left"}
## Packages, functions and initialization
```{r setup, include=FALSE}
library(tidyverse)
library(here)
library(tuneR)
here("Strophe_Sampling_Functions.R") |> source()
set.seed(256345)
```
Note: The `set.seed` command ensures the reproducibility of the sampling step.
If you want to repeat the experiment without reproducing it identically, you must change this number!
We will use the `tuneR` package to manipulate audio files with `R`.
Additionally, we import functions from a separate `R Script` to focus on the essentials in this notebook.
Here is a brief description of the functions and variables imported:
- `silences_44k` and `silences_48k` are two lists containing the possible lengths of silences.
- `individual_mix_audio_list` takes the path to a given bird and uses its audio recordings to return a newly ordered list of the strophes of that bird's song.
- `add_silences` takes the mixed audio list for a bird returned by `individual_mix_audio_list` and adds silences before returning a final audio file.
## Constructing the List of Created Audio Files
Next, we define the names of the species and individuals we will use.
```{r variables}
species <- list.files(here("Strophe_Bank_WAV"))[-3] # This index of 3 is because the third file should be Simplified_Names_File.md but if it's not the case, change it.
types <- list.files(here("Strophe_Bank_WAV", "Sylvia_atricapilla"))
indiv <- paste0("Individual", 1:4)
```
Then, we create a compartmentalized list to store all the audio files we will produce.
The first layer designates the species, the second layer is the type of audio (complete strophes, whistles, or warbles) for *Sylvia atricapilla*, and the final layer ($2^{nd}$ except for *S. atricapilla* where it's the $3^{rd}$) corresponds to the identification number of the individual.
```{r storage_list}
final_audio_list <- lapply(species, function(sp) {
if (sp == "Sylvia_atricapilla"){
sp_list <- lapply(types, function(tp){
type_list <- setNames(
vector("list", length(indiv)), indiv
)
return(type_list)
})
names(sp_list) <- types
} else{
sp_list <- setNames(vector("list", length(indiv)), indiv)
}
return(sp_list)
})
names(final_audio_list) <- species
```
Now, we just need to complete this list by creating the needed audio files.
```{r}
for (spec in species){
if (spec == "Sylvia_atricapilla"){
for (type in types){
for (ind in indiv){
list_audio <- here(
"Strophe_Bank_WAV", spec, type, ind
) |> individual_mix_audio_list()
samp_rates <- vector("integer", length(list_audio))
for (i in length(list_audio)){
samp_rates <- list_audio[[i]]@samp.rate
}
if (all(samp_rates==48000)){
silences <- silences_48k
} else if (all(samp_rates==44100)){
silences <- silences_44k
} else{
stop("Error! There is an issue with sampling rates values.")
}
final_audio_list[[spec]][[type]][[ind]] <-
add_silences(list_audio, silences)
}
}
} else{
for (ind in indiv){
list_audio <- here("Strophe_Bank_WAV", spec, ind) |>
individual_mix_audio_list()
samp_rates <- vector("integer", length(list_audio))
for (i in length(list_audio)){
samp_rates <- list_audio[[i]]@samp.rate
}
if (all(samp_rates==48000)){
silences <- silences_48k
} else if (all(samp_rates==44100)){
silences <- silences_44k
} else{
stop("Error! There is an issue with sampling rates values.")
}
final_audio_list[[spec]][[type]][[ind]] <-
add_silences(list_audio, silences)
}
}
}
```
## Downloading the Audio Files
Finally, each audio file needs to be downloaded into a specified folder with a relevant name.
To do this, we will create the subfolders needed within the `results` folder as we go along.
```{r}
for (sp in species){
if (sp == "Sylvia_atricapilla"){
for (type in types){
path <- here("results", sp, type)
create_folder(path)
for (i in 1:4){
name_file <- paste(
type, sp, sep="_"
) |> paste0(i, ".wav")
audio_file <-
final_audio_list[[sp]][[type]][[indiv[i]]]
##### This part of the code is a solution I found to resolve an issue I enconter with writeWave function
audio_file <- as(audio_file, "WaveMC")
colnames(audio_file) <- c("FL", if (ncol(audio_file) > 1) "FR")
audio_file@.Data[which(is.na(audio_file@.Data)),1] <- 0
#####
writeWave(
audio_file,
here(path, name_file)
)
}
}
} else{
path <- here("results", sp)
create_folder(path)
for (i in 1:4){
name_file <- paste0(sp, i, ".wav")
audio_file <- final_audio_list[[sp]][[indiv[i]]]
#### This part of the code is a solution I found to resolve an issue I enconter with writeWave function
audio_file <- as(audio_file, "WaveMC")
colnames(audio_file) <- c("FL", if (ncol(audio_file) > 1) "FR")
audio_file@.Data[which(is.na(audio_file@.Data)),1] <- 0
#####
writeWave(audio_file, here(path, name_file))
}
}
}
```
:::