light/dark mode

chapter 3

sampling

In music production, "sampling" can mean a few different things depending on the context. This word has already shown up in the previous lessons when creating audio buffers. When creating sound from raw audio buffers we must specify how many channels (individual speakers) we have as well as how many seconds long we want the buffer to be, to calculate this we must determine our sample rate typically 44,100 samples per second or 44.1 kHz. In this context a "sample" refers to a single value (between -1 and 1) representing the position of the speaker (generating the vibration) at that point in time.

In our previous examples we've generated audio buffers algorithmically, in this lesson we'll be creating buffers by loading data from previously recorded sounds. We can do this with the Web Audio API (see Web Audio API / Audio Buffers / From File), but in the exmples below we'll be using Tone.js to keep things "higher level". When we record analog sounds and digitize them, we are again creating audio buffers except this time by "sampling" from the analog world. In this context, "sampling" is a purely technical process that transforms continuous analog sound into the digital domain by slicing it into tiny snapshots (again, values between -1 and 1), each representing the amplitude of the sound wave at a specific moment in time.

amen break record

an audio documentary on the "Amen Break"

More broadly speaking, sampling generally refers to the process of taking a portion, or "sample," of an existing sound or audio recording and reusing it in a new context. This could involve lifting a short segment of a song—like a drum break, melody, or vocal phrase—and incorporating it into a new composition, often altering it with effects, pitch shifts, or looping.

Above is an interactive tool you can use to load any audio clip and set start/stop loop points. This can be used to more easily identify the offset value (how many seconds into the audio file do we want to start our sample) and duration (how many seconds after that offset should we stop our sample) for us to pass into our player's start() method.







delia derbyshire

Delia Derbyshire early pioneer in sampling and electronic music. you can watch a longer documentary about her and her work here.

Another meaning of sampling appears in synthesis, where a digital sampler is used to capture and manipulate sound waves, allowing them to be played back at different pitches and dynamics, similar to how an instrument is played. In the example below I'm using the Tone.Sampler, like the Synth we discussed in the last lesson, the Sampler is a Tone.js "Instrument" for creating and playing back sampled sounds. So rather than using the start() and stop() methods it has the triggerAttack, triggerRelease and triggerAttackRelease methods. The Sampler maps audio files to specific pitches, allowing you to create instruments where each key triggers a different sample or stretches a single sample across multiple keys.




You can record sounds and create your own audio files for sampling purposes, but it's often easier to start with some sound files others have made. The baseURL in the last example above points to a Tone.js domain, this isn't part of the Tone.js library per se, it's actually a repository of audio files the creators of Tone.js started with samples they use to write examples. While I highly recommend you search large open source sound repositories, like freesound.org or Wiki Media, for interesting sounds (or even YouTube via sites like this one), the samples provided by the creators of Tone.js can be an easy/quick place to start experimenting. Below I've created a tool for quickly searching the repository.

Attribution: Text and code written by Nick Briz. The code editor icons designed by Meko and licensed under Creative Commons Attribution License (CC BY 3.0). All sounds generated using the Web Audio API and/or Tone.js by Yotam Mann and other contributors.