Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                #main
              
            
!

CSS

              
                html, body
	height: 100%

body
	margin: 0

#main,
#app,
#visualizer
	height: 100%

#app
	display: flex
	justify-content: center
	align-items: center
	background: url("https://i.imgur.com/Zk6TR5k.jpg") center center / cover no-repeat

	button
		cursor: pointer
		height: 30px
		width: 97px
		border: none
		background: url("https://coddec.github.io/Classic-Shell/www.classicshell.net/forum/download/filefc1a.png?id=1542")
		background-position: 0 -12px
	
		&:hover
			background-position: 0 -66px
	
		&:active
			background-position: 0 -120px

#visualizer
	display: flex
	flex-direction: column
	width: 100%
	padding: 4px
	box-sizing: border-box
	background: radial-gradient(white 30%, blue)

	#controls
		flex: 1
		display: flex
		justify-content: center
		align-items: center
		background: linear-gradient(dimgray, ghostwhite)

	#bars
		flex: 4
		display: flex
		justify-content: center
		align-items: flex-end
		gap: 1px
		background: black
		padding-top: 4vh
		padding-bottom: 4vh

		.bar
			width: 4px
			background: chartreuse
	
			// Given 128 bars, hide the >= 84th bars, because there's no info for those frequencies
			// (May change depending on song)
			&:nth-child(n + 84)
				display: none
              
            
!

JS

              
                // Shoutout to Sarah Drasner for heavily commenting her visualizer setup!
// https://codepen.io/sdras/pen/PVjPKa

const AMPLITUDE_MAX = 255;

// In case the Sandstorm audio source ever fails, uncomment this royalty-free audio source
// https://pixabay.com/music/synthwave-synthwave-80s-110045/
// const audioSource = 'https://cdn.pixabay.com/audio/2022/04/25/audio_5d61b5204f.mp3';
const audioSource = 'https://ia902201.us.archive.org/16/items/darude-sandstorm_202201/Darude%20-%20Sandstorm.mp3';

function App() {
	const [userClicked, setUserClicked] = React.useState(false);
	
	// Chrome requires a user click/gesture before AudioContext can be created
	// https://goo.gl/7K7WLu
	return (
		<div id="app">
			{userClicked ? (
				<Visualizer frequencyCount={128} audioSource={audioSource} />
			) : (
				<button onClick={() => setUserClicked(true)} aria-label="start" />
			)}
		</div>
	)
}

class Visualizer extends React.Component {
	constructor(props) {
		super(props);
		
		this.state = {
			frequencyAmplitudes: Array.from(props.frequencyCount)
		};
	}
	
	// Ensure this.audio has been set by ref callback
	componentDidMount() {
		// Wire up our audio
		this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
		this.source = this.audioContext.createMediaElementSource(this.audioRef);
		this.volumeControl = this.audioContext.createGain();
		this.source.connect(this.audioContext.destination);
		this.source.connect(this.volumeControl);
		
		// Create our analyZer πŸ‡ΊπŸ‡Έ
		this.analyzer = this.audioContext.createAnalyser();
		this.analyzer.fftSize = this.props.frequencyCount * 2;
		this.volumeControl.connect(this.analyzer);
		this.analyzer.connect(this.audioContext.destination);
		
		// Analyzer will know user's set volume
		this.volumeControl.gain.value = this.audioRef.volume;		
		
		// Kick off our animation
		this.animate();
	}

	// Constantly get frequency data from audio so that amplitude can be updated
	animate() {
		let frequencyData = new Uint8Array(this.props.frequencyCount);
		this.analyzer.getByteFrequencyData(frequencyData);
		
		// Without the "bind(this)" silliness, we'll lose the component's context
		requestAnimationFrame(this.animate.bind(this)); // lol
		
		// Trigger React render with updated frequencyData
		this.setState({
			frequencyAmplitudes: Array.from(frequencyData).map(amplitude => amplitude * 100 / AMPLITUDE_MAX)
		});
	}
	
	render() {
		const bars = this.state.frequencyAmplitudes.map(val => {
			return <Bar amplitude={val} />
		});
		
		return (
			<div id="visualizer">
				<div id="bars">
					{bars}
				</div>
				<div id="controls">
					<audio ref={element => this.audioRef = element} src={this.props.audioSource} controls autoplay="true" crossorigin="anonymous"></audio>
				</div>
			</div>
		)
	}
}

function Bar({ amplitude }) {
	return <div className="bar" style={{ height: amplitude + '%' }}></div>
}

ReactDOM.render(<App />, document.getElementById('main'));
              
            
!
999px

Console