
Security News
Feross on Risky Business Weekly Podcast: npm’s Ongoing Supply Chain Attacks
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
waveform-visualizer
Advanced tools
Visualizes waveform data (peaks), for example the ones that got generated using: https://github.com/chrisweb/waveform-data-generator
Waveform created using data that got generated using the waveform data generator
if you want to use the waveform visualizer in your own project you can install it from npm using the following command:
npm i waveform-visualizer
Check out the simple waveform example
Open your project in your favorite IDE (VSCode) and then install the waveform dependency
Now that the dependency is installed, import the waveform-visualizer package into your javascript code like this:
import { Waveform } from 'waveform-visualizer'
if you are using typescript, which is what I will do now for the rest of this example, also import the interfaces so that we have typed options objects (for the core and the layout options), like so:
import { Waveform, IWaveLayoutOptions, IWaveCoreOptions, IWaveClickCallback } from 'waveform-visualizer'
next create a layout options object, to tweak the look of the waveform a bit:
const waveLayoutOptions: IWaveLayoutOptions = {
waveTopPercentage: 50,
peakTopFillStyle: 'deepskyblue',
peakBottomFillStyle: 'dodgerblue ',
}
Note: a fill style can be a css color name (which is what we use here) or a color hex code or an rgb(a) color, a canvas gradient or a canvas pattern, for more details about what values can be used for the *FillStyle options, check out the layout options chapter
All layout options are optional, at this point we are only setting 3 options:
using these 3 options this is the result you will get:
if you prefer a wave which only displays the top peaks then set the value for waveTopPercentage to 100, the result look like this:
if you want to only display bottom peaks set waveTopPercentage to 0:
and if you want the top peaks to be bigger (70% of the height) than the bottom peaks (30% of the height), then set the value for waveTopPercentage to 70:
Note: You can also change the width of the peaks, the width of the space between peaks and a lot more via the layout options, for a full list of available options and their default values, check out the layout options chapter in this readme
Note 2: If you want to see an example of how to use gradient fill styles, check out the client code of the simple waveform example in this repository
next we will create a click callback function, this callback will get triggered by the waveform every time a user clicks on the waveform, it will accept a parameter called clickHorizontalPositionInPercent which is the horizontal position (in percent) of the waveform based on the mouse position, this means that if the user clicks exactly in the middle of the waveform then this value will be 50%, based on that information you can then tell your audio (sound) player to seek (move) to that position of the song:
const waveformClickCallback = (clickHorizontalPositionInPercent: number): void => {
console.log('waveformClickCallback: ', clickHorizontalPositionInPercent)
// tell the audio player to move to 50% of the sound
}
Next create a core options object and we will add 4 options:
const waveCoreOptions: IWaveCoreOptions = {
layout: waveLayoutOptions,
data: [10,16,14,15,17,15,17,18,24,23,21,40,64,66,66,63,68,70,68,70,71,69,68,66,66,67,71,30,29,29,30,67,59,59,69,67,60,64,66,69,64,64,70,66,58,61,36,31,31,31,37,35,31,31,35,36,38,34,36,33,31,50,71,68,55,69,69,62,60,78,59,92,100,98,78,65,63,72,71,68,66,65,65,61,63,62,65,65,66,69,64,70,46,28,29,23,62,64,57,64,70,71,56,61,67,69,62,70,66,58,61,47,33,35,33,33,34,32,32,66,69,57,55,85,78,59,66,65,58,81,86,81,78,61,68,73,57,55,74,84,70,53,75,62,68,72,72,80,66,66,37,30,24,23,67,62,58,66,67,60,58,64,67,65,64,70,66,57,63,68,70,58,61,68,64,59,64,62,73,63,71,71,61,57,47,20,21,20,21,22,22,21,22,25,25,29,25,24,19,21],
waveformClickCallback: onWaveClickHandler,
}
Note: if you want to generate peaks data for one of your songs or sounds, you can for example use another tool I created which is called waveform-data-generator, it is written in javascript (nodejs) and available on github too: https://github.com/chrisweb/waveform-data-generator
Now it is time to create an instance of the waveform visualizer and pass the core options object to it:
const waveform = new Waveform(waveCoreOptions)
Now we need to create an HTML canvas element to display the waveform, open your HTML document and create a new HTML canvas element like so:
<canvas id="waveformCanvas"></canvas>
then back into our javascript code, get the canvas element and then pass it to the waveform using the setCanvasElement method:
const canvasElement = document.getElementById(visualizerOptions.canvasElementId) as HTMLCanvasElement
waveform.setCanvasElement(canvasRef.current)
Note: all the options can be set when creating the waveform instance or like above using setters, there are also getters for each option in case you want to read an option and know what value is currently set, for a full list of all setters and getters check out the waveform methods chapter
If you are not using vanilla javascript but React, then I recommend to use React Hook called useRef instead:
const canvasRef = useRef<HTMLCanvasElement>()
waveform.setCanvasElement(canvasRef.current)
Note: for a full react example check out the next chapter
Final step, we need to tell the waveform to perform the initial draw so that something gets painted inside of our canvas element, like so:
waveform.draw()
The draw function has an optional parameter called range, the range must be a number between 0 and 100, it tells the waveform at what position your sound (song) currently it, if you have a player and that player allows you to set a callback function when the sound (song) is playing, then you should call the draw function again every time that playing progress callback gets triggered, like so:
const playerPlayingProgressCallback = (progressInPercent) => {
waveform.draw(progressInPercent)
}
To see an example of what it looks like when the range gets modified, check out the animated gif on top of this README or install the example from this repository, which uses an audio player that redraws the waveform element every time its progress callback gets triggered: simple waveform and audio player example
If you want to replace the current waveform display with another waveform because your audio player got told to play the next song, then you don't need to create another waveform, just replace the wave peaks from the previous song with the peaks of the current song,like so:
waveform.setWaveData(peaksArray)
Note: This code does the exact same thing as the previous example but uses React instead of vanilla javascript
React component example to create a waveform:
import { useRef, useCallback, useEffect } from 'react'
import { Waveform, IWaveLayoutOptions, IWaveCoreOptions } from 'waveform-visualizer'
const WaveformComponent = () => {
const canvasRef = useRef<HTMLCanvasElement>()
const waveformRef = useRef<Waveform | null>(null)
const onWaveClickHandler = useCallback((clickHorizontalPositionInPercent: number): void => {
console.log('waveformClickCallback: ', clickHorizontalPositionInPercent)
// tell your audio player to move to 50% of the sound
// if you need an audio player checkout my other project:
// https://github.com/chrisweb/web-audio-api-player
//playerRef.current.goToPosition(clickHorizontalPositionInPercent)
}, [])
// here is some pseudo code to show you what to when another sound (song) gets played
// we replace the wave data (peaks) of the previous song with the data for the current song
//playerRef.current.onPlayCallback((sound) => {
// const peaksArray = sound.waveData
// waveformRef.current.setWaveData(peaksArray)
//})
// and here is some pseudo code to show you have to change the range display
// when your player is playing a sound (song)
// the range value is the playing progress in percent, so a value between 0 and 100
//playerRef.current.onPlayingCallback((progressInPercent) => {
// waveformRef.current.draw(progressInPercent)
//})
const initializeWaveform = useCallback(() => {
const waveLayoutOptions: IWaveLayoutOptions = {
waveTopPercentage: 50,
peakTopFillStyle: 'deepskyblue',
peakBottomFillStyle: 'dodgerblue ',
}
const waveCoreOptions: IWaveCoreOptions = {
layout: waveLayoutOptions,
data: [10,16,14,15,17,15,17,18,24,23,21,40,64,66,66,63,68,70,68,70,71,69,68,66,66,67,71,30,29,29,30,67,59,59,69,67,60,64,66,69,64,64,70,66,58,61,36,31,31,31,37,35,31,31,35,36,38,34,36,33,31,50,71,68,55,69,69,62,60,78,59,92,100,98,78,65,63,72,71,68,66,65,65,61,63,62,65,65,66,69,64,70,46,28,29,23,62,64,57,64,70,71,56,61,67,69,62,70,66,58,61,47,33,35,33,33,34,32,32,66,69,57,55,85,78,59,66,65,58,81,86,81,78,61,68,73,57,55,74,84,70,53,75,62,68,72,72,80,66,66,37,30,24,23,67,62,58,66,67,60,58,64,67,65,64,70,66,57,63,68,70,58,61,68,64,59,64,62,73,63,71,71,61,57,47,20,21,20,21,22,22,21,22,25,25,29,25,24,19,21],
waveformClickCallback: onWaveClickHandler,
}
const waveform = new Waveform(waveCoreOptions)
waveformRef.current = waveform
waveform.setCanvasElement(canvasRef.current)
waveform.draw(0)
}, [onWaveClickHandler])
useEffect(() => {
initializeWaveform()
return () => {
if (waveformRef.current !== null) {
// on component umount, destroy the waveform
// the waveform will remove the click event listener
waveformRef.current.destroy()
}
}
}, [initializeWaveform])
return (
<div className="audioWaveForm">
<canvas ref={canvasRef} width="200px" height="60px" />
</div>
)
})
export default WaveformComponent
Note: every *FillStyle option accepts as value any CSS color (so any color string like 'transparent' or 'blue', any hex color like '#000' (hex for black is the default value) or an rgb(a) color like 'rgb(255,255,255)' or 'rgba(0,0,0,0.8)'), a canvas gradient, or a canvas pattern, read more: MDN: canvas fillStyle property
Note: there is no option for the width of a waveform, the width will be determined by the amount of peaks values are in your data array, the width you set for the peaks and the width for the space between each peak:
waveform width = (amount of peaks * peaks width) + ((amount of peaks -1) * peak space width)
install the latest nodejs (if you haven't already) nodejs
install or update to the latest git version git scm downloads (During installation at the step "choosing the default editor used by Git", if like me you are using visual studio code you might want to chose the new option "use visual studio code as Git's default editor") (also if like me you are on windows, at the step "adjusting your PATH environment", ensure the second radio button option is selected "git from the line and also from 3rd-party software" to ensure git is added to the windows PATH, which will allow you to use git with any command line tool like windows powershell for example)
git clone this repository to get a local copy
git clone git@github.com:chrisweb/waveform-visualizer.git
open your favorite command line tool and go to the root directory of this repository
update npm to latest version
npm install npm@latest -g
install the dependencies
npm i
to build the distributions, use the following command:
npm run build
in development you can use watch to have the build getting rebuild every time you edit code / save one of the typescript files
npm run watch
to lint the typescript files
npm run lint
you should first open a ticket and explain what fix or improvement you want to provide on the github issues page of this project (remember the github ticket number you will need it for the commit message later on)
go the github page of this project and hit the fork button
follow the instructions in the previous section "development: build", but instead of cloning this projects repository, clone your own fork of the project
git clone git@github.com:YOUR_USERNAME/waveform-visualizer.git
when you are done coding, commit your local changes (if your commit is related to a ticket start your commit message with the "#TICKER_NUMBER", this will "link" the commit to the ticket)
git commit -m "#TICKER_NUMBER commit message"
now open your forks github URL in your browser and hit the pull request button
login to npmjs.com
npm login
!!! before using the next the command ensure the version of your package in the package.json has been updated
publish a new version on npmjs
npm publish
This project is licensed under the MIT License - see the LICENSE file for details
FAQs
(audio) waveform visualizer
The npm package waveform-visualizer receives a total of 1 weekly downloads. As such, waveform-visualizer popularity was classified as not popular.
We found that waveform-visualizer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.