-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
122 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ docs | |
node_modules | ||
yarn-error.log | ||
.env | ||
examples/app.js | ||
examples/app.js | ||
examples/peaks/app.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { useState } from 'react' | ||
import { createRoot } from 'react-dom/client' | ||
import type WaveSurfer from 'wavesurfer.js' | ||
import WavesurferPlayer from '../../dist/index.js' | ||
import peaksDataAudio from './audio.json'; | ||
import peaksDataStereo from './stereo.json'; | ||
|
||
console.log('mounting React wavesurfer'); | ||
|
||
const medias = [ | ||
{ | ||
url: '../audio.wav', | ||
peaks: [peaksDataAudio.data], | ||
duration: peaksDataAudio.length | ||
}, | ||
{ | ||
url: '../stereo.mp3', | ||
peaks: [peaksDataStereo.data], | ||
duration: peaksDataStereo.length | ||
} | ||
] | ||
|
||
const randomColor = () => | ||
`rgb(${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)})` | ||
|
||
const App = () => { | ||
const [mediaIndex, setMediaIndex] = useState(0) | ||
const [waveColor, setWaveColor] = useState(randomColor) | ||
const [wavesurfer, setWavesurfer] = useState<WaveSurfer | null>(null) | ||
const [isPlaying, setIsPlaying] = useState(false) | ||
|
||
// Swap the audio URL | ||
const onUrlChange = () => { | ||
setIsPlaying(false) | ||
setMediaIndex((index) => (index + 1) % medias.length) | ||
} | ||
|
||
// Play/pause the audio | ||
const onPlayPause = () => { | ||
setIsPlaying((playing) => { | ||
if (wavesurfer?.isPlaying() !== !playing) { | ||
wavesurfer?.playPause() | ||
return !playing | ||
} | ||
return playing | ||
}) | ||
} | ||
|
||
// Randomize the wave color | ||
const onColorChange = () => { | ||
//setWaveColor(randomColor) // slow -- re-render | ||
wavesurfer?.setOptions({ waveColor: randomColor() }) // fast -- no re-render | ||
} | ||
|
||
const media = medias[mediaIndex] | ||
|
||
return ( | ||
<> | ||
<WavesurferPlayer | ||
height={100} | ||
waveColor={waveColor} | ||
url={media.url} | ||
peaks={media.peaks} | ||
duration={media.duration} | ||
onPause={() => setIsPlaying(false)} | ||
onPlay={() => setIsPlaying(true)} | ||
onReady={(ws) => setWavesurfer(ws)} | ||
/> | ||
|
||
<div style={{ margin: '1em 0', display: 'flex', gap: '1em' }}> | ||
<button onClick={onUrlChange}>Change audio</button> | ||
|
||
<button onClick={onColorChange}>Randomize color</button> | ||
|
||
<button onClick={onPlayPause} style={{ minWidth: '5em' }}> | ||
{isPlaying ? 'Pause' : 'Play'} | ||
</button> | ||
</div> | ||
</> | ||
) | ||
} | ||
|
||
// Create a React root and render the app | ||
const root = createRoot(document.getElementById('app')!) | ||
root.render(<App />) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"version":2,"channels":1,"sample_rate":16000,"samples_per_pixel":16000,"bits":8,"length":27,"data":[-63,57,-73,58,-61,47,-63,69,-55,58,-67,81,-74,81,-52,63,-63,71,-73,68,-75,72,-69,64,-67,66,-57,58,-49,56,-63,68,-69,62,-59,53,-54,58,-58,48,-70,68,-49,53,-51,51,-47,51,-55,43,-31,31,-6,6]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>React component for wavesufer.js</title> | ||
</head> | ||
|
||
<body> | ||
<div id="app" style="max-width: 900px; margin: auto"></div> | ||
<script type="module" src="./app.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"version":2,"channels":1,"sample_rate":48000,"samples_per_pixel":48000,"bits":8,"length":175,"data":[-7,7,-9,10,-26,25,-29,27,-37,37,-41,41,-39,39,-40,40,-41,43,-39,41,-37,37,-40,39,-36,37,-42,43,-37,37,-40,39,-36,34,-39,39,-42,41,-42,43,-35,34,-40,40,-39,39,-38,38,-29,34,-39,40,-39,39,-38,38,-35,33,-41,41,-38,38,-37,37,-36,36,-39,38,-40,39,-39,38,-36,36,-38,38,-37,37,-38,37,-37,38,-37,37,-38,38,-38,37,-37,37,-41,40,-36,38,-37,37,-38,35,-38,38,-40,39,-40,40,-36,35,-37,37,-36,37,-37,38,-27,31,-33,33,-37,37,-37,37,-35,37,-37,37,-37,37,-37,37,-37,36,-32,33,-38,39,-37,39,-36,36,-37,36,-35,35,-37,38,-34,34,-35,35,-36,36,-37,36,-34,34,-36,36,-37,37,-36,36,-35,35,-35,34,-37,38,-37,39,-36,35,-35,34,-35,36,-36,37,-36,37,-35,35,-35,35,-36,35,-35,36,-37,37,-35,36,-36,36,-34,34,-35,35,-36,37,-36,37,-34,35,-35,36,-34,34,-36,36,-35,35,-35,35,-34,35,-36,35,-35,36,-36,36,-35,35,-36,36,-35,34,-35,34,-36,37,-34,35,-33,35,-35,35,-33,34,-35,36,-36,36,-35,35,-34,35,-34,34,-35,35,-36,35,-35,35,-34,34,-35,35,-35,34,-36,34,-36,34,-34,34,-34,34,-34,34,-36,34,-35,35,-34,35,-34,34,-33,33,-34,34,-35,34,-33,35,-29,29,-34,34,-34,34,-35,35,-35,35,-34,33,-34,34,-34,34,-32,30,-33,29,-34,34,-34,34,-34,34,-33,33,-34,34,-26,24,-34,34,-29,33,-34,34,-34,34,-23,27,-21,28,-21,21,-22,22,-29,31,-18,17,-12,13,-11,8,-2,3,-2,5,-3,1,0,0]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters