Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

Save Automatically?

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

              
                #app Loading…

              
            
!

CSS

              
                $faderWidth: 6px
$faderHeight: 200px
$faderContainerWidth: $faderWidth * 8
$cfg: #111
$cfg-emphasis: #000
$cui: #ff0
$cui-emphasis: $cui - #222
$cui-error: #f00
$cbg: #c6f

body
	margin: 0
	background: $cbg
	color: $cfg
	font: italic 600 24px sans-serif
	overflow: hidden

body, #app, .app-container, .faderbank, .faders
	display: flex
	align-items: center
	justify-content: center

#app
	height: 100vh
	width: 100vw

.faderbank
	flex-direction: column

.faderbank + .faderbank
	padding-left: $faderWidth * 3

.faders
	padding: $faderWidth * 4 0
	position: relative

	&::before, &::after
		content: ''
		position: absolute
		height: $faderWidth * 2
		border: solid $cfg

	&::before
		bottom: 0
		width: 100%
		border-width: 0 3px 3px 3px

	&::after
		bottom: -$faderWidth * 2
		border-width: 0 0 0 3px

.fader-container
	width: $faderContainerWidth
	height: $faderHeight

// Cross-browser fader styling from http://danielstern.ca/range.css/
.fader
	-webkit-appearance: none
	cursor: pointer
	width: $faderHeight
	height: $faderWidth
	margin: 0
	padding: 0
	border: 0
	outline: 0
	border-radius: $faderWidth / 2
	background-color: $cfg
	transform: translate(($faderContainerWidth - $faderHeight) / 2, ($faderHeight - $faderContainerWidth) / 2 + 13px) rotate(-90deg)

	// Note: keeping these cross-browser styles in separate (redundant) rules so the browser understands it.
	&::-webkit-slider-runnable-track
		width: $faderHeight
		height: $faderWidth
		cursor: pointer
		background: $cfg
		border: 0
		border-radius: $faderWidth / 2

	&::-moz-range-track
		width: $faderHeight
		height: $faderWidth
		cursor: pointer
		background: $cfg
		border: 0
		border-radius: $faderWidth / 2

	&::-webkit-slider-thumb
		box-sizing: content-box
		width: $faderWidth * 3
		height: $faderWidth * 3
		background: $cui
		border: $faderWidth solid $cfg
		border-radius: 50%
		cursor: pointer
		-webkit-appearance: none
		appearance: none
		margin-top: -$faderWidth * 2

	.locked &::-webkit-slider-thumb
		background: $cui-error !important

	&::-moz-range-thumb
		width: $faderWidth * 3
		height: $faderWidth * 3
		background: $cui
		border: $faderWidth solid $cfg
		border-radius: 50%
		cursor: pointer
		-webkit-appearance: none
		appearance: none

	.locked &::-moz-range-thumb
		background: $cui-error !important

	&::-ms-thumb
		width: $faderWidth * 3
		height: $faderWidth * 3
		background: $cui
		border: $faderWidth solid $cfg
		border-radius: 50%
		cursor: pointer
		-webkit-appearance: none
		appearance: none
		// Needed to keep the Edge thumb centred
		margin-top: 0px

	&.locked::-ms-thumb
		background: $cui-error !important

	&::-ms-track
		width: $faderHeight
		height: $faderWidth
		cursor: pointer
		background: transparent
		border-color: transparent
		border-width: 9px 0
		color: transparent

	&::-ms-fill-lower, &::-ms-fill-upper
		background: $cfg
		border: 0
		border-radius: $faderWidth / 2

	&:active
		&::-webkit-slider-thumb
			background: $cui-emphasis

		&::-moz-range-thumb
			background: $cui-emphasis

		&::-ms-thumb
			background: $cui-emphasis

	&:focus, &:hover
		outline: none
		background: $cfg-emphasis

		::-webkit-slider-runnable-track
			background: $cfg-emphasis

		&::-ms-fill-lower
			background: $cfg

		&::-ms-fill-upper
			background: $cfg-emphasis

	&:focus
		box-shadow: 0 0 2px rgba($cfg-emphasis, 0.5)

@supports (-ms-ime-align:auto)
	// Pre-Chromium Edge only styles, selector taken from hhttps://stackoverflow.com/a/32202953/7077589,
	.fader
		margin: 0

.label
	margin: $faderWidth * 3 0 0
	padding: 0

button
	border: $faderWidth solid $cfg
	padding: 1em 1em .9em
	background: $cui
	font: inherit
	cursor: pointer
	border-radius: $faderWidth

	&:active
		background: $cui-emphasis

              
            
!

JS

              
                const {useEffect, useState} = React;
const {gainToDb, Transport, Destination, Player} = Tone;

let players;
const loops = [
	{file: 'Beat1', initialVolume: 0.75},
	{file: 'Bass1', initialVolume: 0.75},
	{file: 'Layers1', initialVolume: 0.5},
	{file: 'Layers2', initialVolume: 0.25},
	{file: 'Chords1', initialVolume: 0},
	{file: 'Beat2', initialVolume: 0},
	{file: 'Flute1', initialVolume: 0},
	{file: 'Bass2', initialVolume: 0},
];
const playerPromises = loops.map(({file, initialVolume}) => new Promise((resolve, reject) => {
	const player = new Player(`https://rileyjshaw.com/mp3/loops/${file}.mp3`, () => resolve(player)).toDestination();
	player.volume.value = gainToDb(initialVolume);
}));
Promise.all(playerPromises).then(_players => {
	players = _players;
	players.forEach(player => {
		player.sync();
		player.start();
	});
	Transport.bpm.value = 100;
	Transport.loop = true;
	Transport.loopStart = 0;
	Transport.loopEnd = '8:0:0';
	ReactDOM.render(<App />, document.getElementById('app'));
});

const nTracks = loops.length;

const add = (a, b) => a + b;
const bound = (n, lo, hi) => Math.max(lo, Math.min(hi, n)) || lo;

const FaderBank = ({values, label, onChange, className}) => <div className={`faderbank ${className ?? ""}`}>
	<div className="faders">
		{values.map((value, i) => <Fader
			key={i}
			value={value}
			onChange={({target: {value}}) => onChange(+value, i)}
		/>)}
	</div>
	<p className="label">{label}</p>
</div>;

const Fader = ({onChange, value}) => <div className="fader-container">
	<input
		type="range"
		onChange={onChange}
		value={value}
		min={0}
		max={1}
		step="any"
		className="fader"
	/>
</div>;

const App = () => {
	const [started, setStarted] = useState(false);
	const [linkedFaders, setLinkedFaders] = useState({
		volume: loops.map(loop => loop.initialVolume).reduce(add) / nTracks,
		tracks: loops.map(loop => loop.initialVolume),
		volumeLocked: !loops.some(({initialVolume}) => initialVolume && initialVolume < 1),
	});
	const [gain, setGain] = useState(0.5);

	const updateVolume = newVolume => {
		setLinkedFaders(({tracks}) => {
			const nActiveTracks = tracks.filter(track => track).length;
			const limitedVolume = bound(newVolume, 0, nActiveTracks / nTracks);
			const factor =  limitedVolume * nTracks / tracks.reduce(add);

			return {
				volume: limitedVolume,
				tracks: tracks.map(unscaled => bound(factor * unscaled, 0, 1)),
				volumeLocked: limitedVolume !== newVolume,
			};
		});
	};
	const updateTrack = (newValue, i) => {
		setLinkedFaders(({volume, tracks}) => {
			const newTracksUnscaled = [...tracks.slice(0, i), +newValue, ...tracks.slice(i + 1)];
			const factor = (volume * nTracks - newValue) / (newTracksUnscaled.reduce(add) - newValue);
			const newTracks = newTracksUnscaled.map((unscaled, j) => i === j ? unscaled : bound(factor * unscaled, 0, 1));
			return {
				volume: newTracks.reduce(add) / nTracks,
				tracks: newTracks,
				volumeLocked: !newTracks.some(track => track && track < 1),
			};
		});
	};
	const updateGain = (newGain) => setGain(newGain);
	
	// Update track volume.
	useEffect(() => {
		linkedFaders.tracks.forEach((volume, i) => {
			players[i].volume.value = gainToDb(volume);
		});
	}, linkedFaders.tracks);
	
	// Update master gain.
	useEffect(() => {
		Destination.volume.value = gainToDb(gain);
	}, [gain]);

	return <div className="app-container">
		{started
			? <>
				<FaderBank label="Volume" values={[linkedFaders.volume]} onChange={updateVolume} className={linkedFaders.volumeLocked && "locked"} />
				<FaderBank label="Tracks" values={linkedFaders.tracks} onChange={updateTrack} />
				<FaderBank label="Gain" values={[gain]} onChange={updateGain} />
			</>
			: <button onClick={() => {
				setStarted(true);
				Tone.start();
				Transport.start();
			}}>Start demo</button>}
	</div>;
};

              
            
!
999px

Console