Repository navigation
Expand file tree
/
Copy pathother-apps.Rmd
More file actions
201 lines (136 loc) · 8.67 KB
/
Copy pathother-apps.Rmd
File metadata and controls
201 lines (136 loc) · 8.67 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
---
title: "Interacting with other tools"
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
```{r setup, message=FALSE}
library(sonicscrewdriver)
```
## Introduction
The `sonicscrewdriver` package provides a number of functions for interacting with other audio tools and software. This vignette provides an overview of the functions available for interacting with other tools.
## Interfaces with other tools
### Audacity
Label files exported from [Audacity](https://www.audacityteam.org/) can be read into R using the `readAudacityLabels()` function. This function takes a path to a label file and returns a list of `Annotation` objects or a data frame with the start and end times of each label and the label text. The `writeAudacityLabels()` function can be used to write a list of `Annotation` objects to an Audacity label file.
### AudioBlast
The [audioBlast API](https://api.audioblast.org) can be accessed using the `audioBlast()` function. Files can be downloaded from the API using the `audioBlastDownload()` function.
### AudioMoth
[AudioMoth](https://www.openacousticdevices.info/audiomoth) configuration files can be read into R using the `audiomothConfig()` function, which takes a path to a configuration file and returns a data frame of the configuration settings.
The metadata an AudioMoth writes into a wave file is read by `readMetadata()`, as its `audiomoth` source:
```{r, eval=FALSE}
m <- readMetadata("20201103_112430.WAV")
m$sources$audiomoth$serial
m$sources$audiomoth$temperature
# The recording time, as a POSIXct in the zone the device was set to
m$fields$datetime
```
`audiomothWave()` did the same for a wave file alone and is deprecated in favour of this.
#### Triggered recordings
An AudioMoth recording on a trigger, such as an amplitude threshold, writes a single file whose name ends in `T.WAV`. The periods that did not trigger a recording are left out of it, each replaced by a short encoded block recording how long it was. A conventional wave tool reads such a file as the triggered recordings run together, which loses the timing of the events in the recording.
`audiomothExpand()` puts the omitted periods back as silence, so that the triggered recordings sit where they were recorded. It returns a Wave object, or writes an ordinary wave file if given a path to write to.
```{r, eval=FALSE}
w <- audiomothExpand("20201103_112430T.WAV")
# Or expand every triggered recording on a card to a wave file
for (f in list.files(pattern="T\\.WAV$")) {
audiomothExpand(f, sub("T\\.WAV$", ".WAV", f))
}
```
`readAudio()` recognises a triggered recording and expands it as it reads it, so no change is needed to code that already uses it. It is recognised by the trigger AudioMoth records in the comment it writes into the header, rather than by the `T` before the extension: a name is only a convention, which an ordinary recording can meet and a triggered one can be renamed out of. `windowing()` reads such a file the same way, so windows are scheduled over the recording rather than over what is left of it on disk.
```{r, eval=FALSE}
w <- readAudio("20201103_112430T.WAV", from=60, to=120, time.unit="seconds")
```
`audiomothCompress()` goes the other way, writing a Wave as a triggered file and leaving out the segments whose peak amplitude falls below a threshold. Audio left out is not recoverable, as it is not recoverable on the device. The threshold is recorded in the file's comment, as a device records it, which is what lets the file be read back as a triggered recording.
```{r, eval=FALSE}
audiomothCompress(w, "20201103_112430T.WAV", threshold=1000)
```
#### GUANO metadata
Current AudioMoth firmware writes [GUANO](https://github.com/riggsd/guano-spec) metadata, a format used by many bioacoustic recorders, which `guano()` reads from any wave file that carries it.
```{r, eval=FALSE}
m <- guano("20201103_112430.WAV")
m[["Loc Position"]]
m[["Timestamp"]]
```
Fields the specification defines are converted to the types it gives them, so a position is a pair of numbers and a timestamp is a `POSIXct`. Any other field, which includes every namespaced field such as `OAD|Battery Voltage`, is kept as written. Metadata can be edited as a list and written back with `guanoWrite()`, which replaces only the metadata and leaves the audio untouched.
```{r, eval=FALSE}
m[["Species Manual ID"]] <- "Pipistrellus pipistrellus"
guanoWrite(m, "20201103_112430.WAV")
```
`guanoCreate()` builds metadata from scratch, which `guanoWrite()` can add to a file that has none, and `audiomothCompress()` can write into a triggered file. Expanding a triggered file carries its metadata over.
```{r, eval=FALSE}
m <- guanoCreate(
Make = "Open Acoustic Devices",
Model = "AudioMoth",
Samplerate = 48000,
"Loc Position" = c(51.4964, -0.1224)
)
guanoWrite(m, "20201103_112430.WAV")
```
### BirdNET Analyzer
[BirdNET Analyzer](https://birdnet.cornell.edu) is a deep learning model for the automatic detection of bird sounds. The `birdNetInstall()` function can be used to install the `ssd_birdnet` environment required to use the `birdNetAnalyse()` function with `SonicScrewdriver`. The `birdNetAnalyse()` function takes a list of sound files and analyses them using the `BirdNET-Analyzer`. The function either returns a data frame with the results of the analysis or a list of `Annotation` objects.
```{r, eval=FALSE}
# Install the Python environment
pythonInstall()
```
```{r, eval=FALSE}
# Analyse sound files using BirdNET-Analyzer
f <- system.file("extdata", "AUDIOMOTH.WAV", package="sonicscrewdriver")
birdNetAnalyse(f, lat=51.5, lon=0.1, date=as.Date("2021-01-01"))
```
### scikit-maad
[scikit-maad](https://scikit-maad.github.io/index.html) is an open source Python package dedicated to the quantitative analysis of environmental audio recordings. `sonicscrewdriver` makes use of the `reticulate` package to create a Python environment and to interact with scikit-maad. In `sonicscrewdriver` scikit-maad functions are prefixed with `maad_`.
The functions provided accept standard Wave-like objects (i.e. `tuneR` `Wave` or `WaveMC` objects, as well as their tagged equivalents from this package).
```{r, eval=FALSE}
# Install the Python environment
pythonInstall()
```
```{r, eval=FALSE}
f <- system.file("extdata", "AUDIOMOTH.WAV", package="sonicscrewdriver")
w <- readWave(f)
maad_aci <- maad_acoustic_complexity_index(w)
```
## Exemplar applications
### Audio file with annotations from audioBlast to Audacity
The audio file of interest (id: [10754](https://bio.acousti.ca/node/10754)) is a recording of the Mole Cricket *Gryllotalpa vineae* from the [BioAcoustica](https://bio.acousti.ca) repository. Recordings and annotations from BioAcoustica are made available through the [audioBlast](https://audioblast.org) API.
First we will download the audio file and annotations from audioBlast.
```{r, results=FALSE}
# Find the file on audioblast
data <- audioblast("data", "recordings", source="bio.acousti.ca", id=10754)
# Download the file to the mole_cricket directory
audioblastDownload(data, dir="mole_cricket/")
```
This has downloaded the audio file to the `mole_cricket` directory. It has also saved a `metadata.csv` file that includes more information about the audio file retrieved from audioBlast.
Next, we will download the annotations for this file from audioBlast.
```{r, results=FALSE}
# Fetch annotations from audioBlast as `Annotation` objects
a <- audioblast("data", "annomate", source="bio.acousti.ca", source_id=10754, output="Annotation")
```
This has downloaded the annotations from audioBlast and converted them to a list of `Annotation` objects. Once annotation data are converted to `Annotation` objects they can be easily manipulated into a number of other useful formats.
In this case, we will convert the `Annotation` objects to an Audacity label file.
```{r}
# Convert the annotations to an Audacity label file
writeAudacityLabels(a, "mole_cricket/annotations.txt")
```
When we are finished, we can tidy our workspace by removing the `mole_cricket` directory.
```{r}
unlink("mole_cricket", recursive=TRUE)
```
## BirdNET Analyzer to Audacity label file
The `birdNetAnalyse()` function can be used to analyse sound files using the `BirdNET-Analyzer`.
Get the output of the BirdNET Analyzer as a list of `Annotation` objects.
```{r, eval=FALSE}
# Analyse sound files using BirdNET-Analyzer
f <- system.file("extdata", "AUDIOMOTH.WAV", package="sonicscrewdriver")
annotations <- birdNetAnalyse(f, output="Annotation")
```
Convert the `Annotation` objects to an Audacity label file.
```{r eval=FALSE}
# Convert the annotations to an Audacity label file
writeAudacityLabels(annotations, "birdnet_annotations.txt")
```
Cleaning up the workspace.
```{r, eval=FALSE}
unlink("birdnet_annotations.txt")
```