|
| 1 | +/** |
| 2 | + * @name Create Audio |
| 3 | + * @description <a href="https://p5js.org/reference/#/p5/createAudio" target="_blank">createAudio()</a> |
| 4 | + * creates an audio player. This example displays the player's controls |
| 5 | + * and adjusts its speed. The playback speed is normal when the mouse |
| 6 | + * is on the left edge of the window, and it gets faster as the mouse |
| 7 | + * moves to the right. More information on using media elements such as |
| 8 | + * audio players is on the |
| 9 | + * <a href="https://p5js.org/reference/#/p5.MediaElement" target="_blank">p5.MediaElement</a> |
| 10 | + * reference page. The audio file is a |
| 11 | + * <a href="https://freesound.org/people/josefpres/sounds/711156/" target="_blank"> |
| 12 | + * public domain piano loop by Josef Pres</a>. |
| 13 | + */ |
| 14 | + |
| 15 | +// Declare variable to store audio player |
| 16 | +let audioPlayer; |
| 17 | + |
| 18 | +function setup() { |
| 19 | + // Remove canvas |
| 20 | + noCanvas(); |
| 21 | + |
| 22 | + // Create audio player using path to audio file |
| 23 | + // This can also be a URL for a public file |
| 24 | + // On the p5 Editor, a file may be uploaded to Sketch Files |
| 25 | + // by clicking the > button on the upper left, followed by the + button |
| 26 | + audioPlayer = createAudio('assets/piano-loop.mp3'); |
| 27 | + |
| 28 | + // Add description for assistive technologies to explain playback speed |
| 29 | + audioPlayer.attribute( |
| 30 | + 'aria-description', |
| 31 | + 'The playback speed of this audio player is controlled by the position of the mouse. The further to the right the mouse is, the faster the audio will play.' |
| 32 | + ); |
| 33 | + |
| 34 | + // Display player controls |
| 35 | + audioPlayer.showControls(); |
| 36 | +} |
| 37 | + |
| 38 | +function draw() { |
| 39 | + // Set playback speed to 1-2x normal based on mouse position |
| 40 | + audioPlayer.speed(1 + mouseX / windowWidth); |
| 41 | +} |
0 commit comments