Skip to content

Commit

Permalink
Merge pull request #46 from rasmusab/develop
Browse files Browse the repository at this point in the history
beepr 2.0
  • Loading branch information
rasmusab authored Jul 6, 2024
2 parents 07c898d + c9ff7ae commit 13dfa3e
Show file tree
Hide file tree
Showing 23 changed files with 207 additions and 60 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
^.*\.Rproj$
^\.Rproj\.user$
^scratchpad_sounds$
^scratchpad$
^blog_posts$
^cran-comments\.md$
^revdep$
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
.Rhistory
.RData
scratchpad_sounds
blog_posts
scratchpad
blog_posts
.DS_Store
10 changes: 6 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Package: beepr
Type: Package
Title: Easily Play Notification Sounds on any Platform
Version: 1.3
Version: 2.0
Encoding: UTF-8
Date: 2018-06-01
Date: 2024-07-06
Authors@R: c(
person("Rasmus", "Bååth", email = "rasmus.baath@gmail.com", role = c("aut", "cre")),
person("Amanda", "Dobbyn", email = "amanda.e.dobbyn@gmail.com", role = "ctb"))
Expand All @@ -15,6 +15,8 @@ License: GPL-3
URL: https://github.com/rasmusab/beepr
BugReports: https://github.com/rasmusab/beepr/issues
Imports:
stringr (>= 1.0.0),
audio
RoxygenNote: 6.0.1.9000
RoxygenNote: 7.3.1
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
export(beep)
export(beep_on_error)
import(audio)
import(stringr)
import(utils)
10 changes: 10 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
beepr 2.0
===========

* `beep_on_error` rethrows the error after beeping. This is a breaking change from 1.3.
* Fixed a bug that resulted in R crashing when calling `beep()` multiple times in a short time frame. (#26)
* Removed the dependency on `stringr`. (#42)
* Allowed `beep(-1)` or `beep("none")` to result in no sound being played. (#29)
* Fixed a bug where the download sound-option did not work on Windows. (#41)
* Changed the Linux sound player priority order to aplay -> paplay -> vlc. (#39)

beepr 1.3
===========

Expand Down
17 changes: 8 additions & 9 deletions R/beepr-package.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
#' Easily Play Notification Sounds on any Platform
#'
#'
#' This package contains one function, beep(), with one purpose: To make it easy
#' to play notification sounds on whatever platform you are on. It is intended to
#' be useful, for example, if you are running a long analysis in the background
#' and want to know when it is ready.
#'
#' The package contains one main function \code{\link{beep}}, check it out to
#' see what it does. For sound on Windows and MacOS \pkg{beepr} depends on the
#'
#' The package contains one main function \code{\link{beep}}, check it out to
#' see what it does. For sound on Windows and MacOS \pkg{beepr} depends on the
#' \pkg{audio} package. For sound on Linux \pkg{beepr} depends on that either
#' the paplay utility from the Pulse Audio system, the aplay utility from the
#' ALSA system, or VLC media player (http://www.videolan.org/vlc/index.html) is
#' installed and on the PATH. Chances are that you already have one of these.
#'
#'
#' @examples
#' # Play a "ping" sound
#' beep()
#'
#'
#' @name beepr
#' @encoding UTF-8
#' @docType package
#' @author Rasmus Bååth < rasmus.baath@@gmail.com >
#' @import audio stringr utils
NULL
#' @import audio utils
"_PACKAGE"
58 changes: 30 additions & 28 deletions R/beepr.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
#' \code{"shotgun"} \item \code{"mario"} \item \code{"wilhelm"} \item
#' \code{"facebook"} \item \code{"sword"} } If \code{sound} does not match any
#' of the sounds above, or is a valid path or url, a random sound will be
#' played. Currently \code{beep} can only handle http urls, https is not
#' supported.
#'@param expr An optional expression to be excecuted before the sound.
#' played. If a negative number is given or the string "none" is given, no
#' sound will be played.
#'@param expr An optional expression to be executed before the sound.
#'
#'
#'@return NULL
Expand Down Expand Up @@ -54,16 +54,18 @@ beep <- function(sound=1, expr=NULL) {
facebook = "facebook.wav",
sword = "sword.wav")
sound_path <- NULL
if(is.na(sounds[sound]) || length(sounds[sound]) != 1) {
if(sound < 0 || sound == "none") {
# Play the sound of silence
return(invisible())
} else if(is.na(sounds[sound]) || length(sounds[sound]) != 1) {
if(is.character(sound)) {
sound <- str_trim(sound)
# Trimming white space from the string defining the sound
sound <- gsub("(^\\s+|\\s+$)", "", sound)
if(file.exists(sound)) {
sound_path <- sound
} else if(str_detect(sound, "^https://")) {
warning("Can't currently use https urls, only http.")
} else if(str_detect(sound, "^http://")) {
} else if(grepl(pattern = "^http(s?)://", x = sound)) {
temp_file <- tempfile(pattern="")
if(download.file(sound, destfile = temp_file, quiet = TRUE) == 0) { # The file was successfully downloaded
if(download.file(sound, destfile = temp_file, mode = "wb", quiet = TRUE) == 0) { # The file was successfully downloaded
sound_path <- temp_file
} else {
warning(paste("Tried but could not download", sound))
Expand Down Expand Up @@ -108,72 +110,72 @@ beep <- function(sound=1, expr=NULL) {
#' supported.
#'
#'
#'@return NULL
#'@return The value of \code{expr}, if no error occurs. If an error occurs then
#'\code{beep_on_error} will re-throw the error.
#'
#'@examples
#' \dontrun{
#' # Play a "ping" sound if \code{expr} produces an error
#' beep_on_error(log("foo"))
#'
#' # Stay silent if \code{expr} does not produce an error
#' beep_on_error(log(1))
#'
#' \dontrun{
#'
#' # Play the Wilhelm scream instead of a ping on error.
#' beep_on_error(runif("bar"), "wilhelm")
#' }
#'
#'@export

beep_on_error <- function(expr, sound = 1) {
q_expr <- substitute(expr)

msg <- paste0("An error occurred in ", deparse(q_expr))
e <- simpleError(msg)

tryCatch(expr, error = function(e) {
message(paste0(msg, ": ", e$message))
beep(sound)
stop(e)
})
}

is_wav_fname <- function(fname) {
str_detect(fname, regex("\\.wav$", ignore_case = TRUE))
grepl(pattern = "\\.wav$", x = fname, ignore.case = TRUE)
}

escape_spaces <- function(s) {
str_replace_all(s, " ", "\\\\ ")
gsub(pattern = " ", replacement = "\\\\ ", x = s)
}

play_vlc <- function(fname) {
fname <- escape_spaces(fname)
system(paste0("vlc -Idummy --no-loop --no-repeat --playlist-autostart --no-media-library --play-and-exit ", fname),
ignore.stdout = TRUE, ignore.stderr=TRUE,wait = FALSE)
ignore.stdout = TRUE, ignore.stderr=TRUE, wait = FALSE)
invisible(NULL)
}

play_paplay <- function(fname) {
fname <- escape_spaces(fname)
system(paste0("paplay ", fname), ignore.stdout = TRUE, ignore.stderr=TRUE,wait = FALSE)
system(paste0("paplay ", fname), ignore.stdout = TRUE, ignore.stderr=TRUE, wait = FALSE)
invisible(NULL)
}

play_aplay <- function(fname) {
fname <- escape_spaces(fname)
system(paste0("aplay --buffer-time=48000 -N -q ", fname), ignore.stdout = TRUE, ignore.stderr=TRUE,wait = FALSE)
system(paste0("aplay --buffer-time=48000 -N -q ", fname), ignore.stdout = TRUE, ignore.stderr=TRUE, wait = FALSE)
invisible(NULL)
}

package_state <- new.env(parent = emptyenv())
play_audio <- function(fname) {
if(!is.null(package_state$active_audio_instance)) {
close(package_state$active_audio_instance)
}
sfx <- load.wave(fname)
play(sfx)
package_state$active_audio_instance <- play(sfx)
}

play_file <- function(fname) {
if(Sys.info()["sysname"] == "Linux") {
if(is_wav_fname(fname) && nchar(Sys.which("paplay")) >= 1) {
play_paplay(fname)
} else if(is_wav_fname(fname) && nchar(Sys.which("aplay")) >= 1) {
if(is_wav_fname(fname) && nchar(Sys.which("aplay")) >= 1) {
play_aplay(fname)
} else if(is_wav_fname(fname) && nchar(Sys.which("paplay")) >= 1) {
play_paplay(fname)
} else if(nchar(Sys.which("vlc")) >= 1) {
play_vlc(fname)
} else {
Expand All @@ -182,4 +184,4 @@ play_file <- function(fname) {
} else {
play_audio(fname)
}
}
}
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

A derivative function, `beep_on_error()`, wraps an expression and will only beep if an error occurs when that expression is evaluated.

This package was previously called `pingr` and included a `ping()` function. It has been renamed in order to not be confused with the Unix tool [ping](http://en.wikipedia.org/wiki/Ping_%28networking_utility%29). For the rationale behind `beepr` see the original announcement: http://sumsar.net/blog/2014/01/announcing-pingr/


Installation
----------------
Expand All @@ -29,7 +27,7 @@ Requirements

If you are using Windows or OS X `beepr` relies on the `audio` package for sound playback and no external program is needed.

If you're on Linux `beepr` relies on you having either the `paplay` utility from the Pulse Audio system, the `aplay` utility from the ALSA system or [VLC media player](http://www.videolan.org/vlc/index.html) installed and on the PATH. Chances are that you alread have one of these. If you are on Debian/Ubuntu you can otherwise get VLC by running the following in a terminal:
If you're on Linux `beepr` relies on you having either the `paplay` utility from the Pulse Audio system, the `aplay` utility from the ALSA system or [VLC media player](http://www.videolan.org/vlc/index.html) installed and on the PATH. Chances are that you already have one of these. If you are on Debian/Ubuntu you can otherwise get VLC by running the following in a terminal:

```
sudo apt-get install vlc
Expand Down
11 changes: 6 additions & 5 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
## Test environments
* Ubuntu 17.10, R 3.4.4
* MacOS 14.1.1, R 4.4.1
* win-builder (devel and release)

## R CMD check results

0 errors | 0 warnings | 0 note

## Reverse dependencies
## revdepcheck results

* I have run R CMD check on the 2 downstream dependencies.
Checked MixedPsy : 0 errors | 0 warnings | 0 notes
Checked paramtest: 0 errors | 0 warnings | 0 notes
We checked 27 reverse dependencies, comparing R CMD check results across CRAN and dev versions of this package.

* We saw 0 new problems
* We failed to check 0 packages
Binary file modified inst/sounds/ready_master.wav
Binary file not shown.
Binary file modified inst/sounds/shotgun.wav
Binary file not shown.
Binary file modified inst/sounds/sword.wav
Binary file not shown.
Binary file modified inst/sounds/work_complete.wav
Binary file not shown.
6 changes: 3 additions & 3 deletions man/beep.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions man/beep_on_error.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions man/beepr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions revdep/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
checks
library
checks.noindex
library.noindex
cloud.noindex
data.sqlite
*.html
24 changes: 24 additions & 0 deletions revdep/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Platform

|field |value |
|:--------|:----------------------------------------|
|version |R version 4.4.1 (2024-06-14) |
|os |macOS Sonoma 14.1.1 |
|system |aarch64, darwin20 |
|ui |RStudio |
|language |(EN) |
|collate |en_US.UTF-8 |
|ctype |en_US.UTF-8 |
|tz |Europe/Stockholm |
|date |2024-07-06 |
|rstudio |2024.04.2+764 Chocolate Cosmos (desktop) |
|pandoc |3.0.1 @ /opt/homebrew/bin/pandoc |

# Dependencies

|package | old|new |Δ |
|:-------|---:|:--------|:--|
|beepr | 1.3|1.3.9000 |* |

# Revdeps

7 changes: 7 additions & 0 deletions revdep/cran.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## revdepcheck results

We checked 27 reverse dependencies, comparing R CMD check results across CRAN and dev versions of this package.

* We saw 0 new problems
* We failed to check 0 packages

1 change: 1 addition & 0 deletions revdep/failures.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*Wow, no problems at all. :)*
1 change: 1 addition & 0 deletions revdep/problems.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*Wow, no problems at all. :)*
Loading

0 comments on commit 13dfa3e

Please sign in to comment.