Skip to content

Commit 3238936

Browse files
authored
Merge pull request #148 from lechten/audio-speed
Audio speed
2 parents 40899c3 + b836644 commit 3238936

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

audio-slideshow/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Reveal.initialize({
4949
autoplay: false, // automatically start slideshow
5050
defaultDuration: 5, // default duration in seconds if no audio is available
5151
defaultAudios: true, // try to play audios with names such as audio/1.2.ogg
52+
defaultPlaybackRate = 1.0, // speed of audio
5253
playerOpacity: 0.05, // opacity value of audio player if unfocused
5354
playerStyle: 'position: fixed; bottom: 4px; left: 25%; width: 50%; height:75px; z-index: 33;', // style used for container of audio controls
5455
startAtFragment: false, // when moving to a slide, start at the current fragment or at the start of the slide
@@ -57,6 +58,13 @@ Reveal.initialize({
5758
});
5859
```
5960

61+
### Playback speed
62+
63+
This plugin has a parameter ```defaultPlaybackRate```, which configures with what speed audio is played by default. Note that this option changes the speed of *all* audios, which may interfere with other plugins such as ```RevealChalkboard``` or ```RevealAnimate```, which may have their own notion of "correct" playback rate.
64+
65+
In addition, users may use adjust the speed of audio using the audio controls (usually, via right-click). The plugin remembers the currently set speed and uses that on subsequent audios as well.
66+
67+
6068
## Preparing an audio slideshow
6169

6270
For each slide or fragment you can explicitly specify a file to be played when the slide or fragment is shown by setting the ```data-audio-src``` attribute for the slide or fragment.

audio-slideshow/plugin.js

+27-23
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ const initAudioSlideshow = function(Reveal){
3030
var defaultNotes = false; // use slide notes as default for the text to speech converter
3131
var defaultText = false; // use slide text as default for the text to speech converter
3232
var defaultDuration = 5; // value in seconds
33+
var defaultPlaybackRate = 1.0; // default speed of audio
34+
var currentPlaybackRate = 1.0; // current speed of audio
3335
var defaultAudios = true; // try to obtain audio for slide and fragment numbers
3436
var advance = 0; // advance to next slide after given time in milliseconds after audio has played, use negative value to not advance
3537
var autoplay = false; // automatically start slideshow
@@ -132,7 +134,7 @@ const initAudioSlideshow = function(Reveal){
132134
function setup() {
133135
// wait for markdown and highlight plugin to be done
134136
if (
135-
document.querySelector( 'section[data-markdown]:not([data-markdown-parsed])' )
137+
document.querySelector( 'section[data-markdown]:not([data-markdown-parsed])' )
136138
|| document.querySelector( 'code[data-line-numbers*="|"]')
137139
) {
138140
setTimeout( setup, 100 );
@@ -149,6 +151,11 @@ const initAudioSlideshow = function(Reveal){
149151
if ( config.defaultText != null ) defaultText = config.defaultText;
150152
if ( config.defaultDuration != null ) defaultDuration = config.defaultDuration;
151153
if ( config.defaultAudios != null ) defaultAudios = config.defaultAudios;
154+
if ( config.defaultPlaybackRate != null ) {
155+
defaultPlaybackRate = config.defaultPlaybackRate;
156+
currentPlaybackRate = config.defaultPlaybackRate;
157+
}
158+
152159
if ( config.advance != null ) advance = config.advance;
153160
if ( config.autoplay != null ) autoplay = config.autoplay;
154161
if ( config.playerOpacity != null ) playerOpacity = config.playerOpacity;
@@ -283,6 +290,8 @@ const initAudioSlideshow = function(Reveal){
283290
videoElement.currentTime = audioElement.currentTime;
284291
} );
285292
audioElement.addEventListener( 'play', function( event ) {
293+
audioElement.playbackRate = currentPlaybackRate;
294+
videoElement.playbackRate = currentPlaybackRate;
286295
videoElement.currentTime = audioElement.currentTime;
287296
if ( videoElement.paused ) videoElement.play();
288297
} );
@@ -294,6 +303,10 @@ const initAudioSlideshow = function(Reveal){
294303
videoElement.volume = audioElement.volume;
295304
videoElement.muted = audioElement.muted;
296305
} );
306+
audioElement.addEventListener( 'ratechange', function( event ) {
307+
videoElement.playbackRate = audioElement.playbackRate;
308+
currentPlaybackRate = audioElement.playbackRate;
309+
} );
297310
audioElement.addEventListener( 'seeked', function( event ) {
298311
videoElement.currentTime = audioElement.currentTime;
299312
} );
@@ -335,6 +348,8 @@ const initAudioSlideshow = function(Reveal){
335348
audioElement.setAttribute( 'controls', '' );
336349
audioElement.setAttribute( 'preload', 'none' );
337350

351+
audioElement.playbackRate = defaultPlaybackRate;
352+
338353
if ( videoElement ) {
339354
// connect play, pause, volumechange, mute, timeupdate events to video
340355
if ( videoElement.duration ) {
@@ -383,29 +398,11 @@ const initAudioSlideshow = function(Reveal){
383398
evt.timestamp = 1000 * audioElement.currentTime;
384399
document.dispatchEvent( evt );
385400

401+
// Make sure that the currentPlaybackRate is used, which
402+
// might have been set by the user.
403+
audioElement.playbackRate = currentPlaybackRate;
404+
386405
if ( timer ) { clearTimeout( timer ); timer = null; }
387-
// preload next audio element so that it is available after slide change
388-
var indices = Reveal.getIndices();
389-
var nextId = "audioplayer-" + indices.h + '.' + indices.v;
390-
if ( indices.f != undefined && indices.f >= 0 ) {
391-
nextId = nextId + '.' + (indices.f + 1);
392-
}
393-
else {
394-
nextId = nextId + '.0';
395-
}
396-
var nextAudio = document.getElementById( nextId );
397-
if ( !nextAudio ) {
398-
nextId = "audioplayer-" + indices.h + '.' + (indices.v+1);
399-
nextAudio = document.getElementById( nextId );
400-
if ( !nextAudio ) {
401-
nextId = "audioplayer-" + (indices.h+1) + '.0';
402-
nextAudio = document.getElementById( nextId );
403-
}
404-
}
405-
if ( nextAudio ) {
406-
//console.debug( "Preload: " + nextAudio.id );
407-
nextAudio.load();
408-
}
409406
} );
410407
audioElement.addEventListener( 'pause', function( event ) {
411408
if ( timer ) { clearTimeout( timer ); timer = null; }
@@ -417,6 +414,9 @@ const initAudioSlideshow = function(Reveal){
417414
document.dispatchEvent( evt );
418415
if ( timer ) { clearTimeout( timer ); timer = null; }
419416
} );
417+
audioElement.addEventListener( 'ratechange', function( event ) {
418+
currentPlaybackRate = audioElement.playbackRate;
419+
} );
420420

421421
if ( audioFile != null ) {
422422
// Support comma separated lists of audio sources
@@ -457,6 +457,10 @@ const initAudioSlideshow = function(Reveal){
457457
container.appendChild( audioElement );
458458
}
459459
}
460+
461+
function getPlaybackRate() {
462+
return currentPlaybackRate;
463+
}
460464
};
461465

462466
/*****************************************************************

0 commit comments

Comments
 (0)