Music often makes use of scales, or predetermined pitch sets, to create melodic movement. In western musical traditions pitches are given letter names like ‘A’ and ‘C#.’
There are many different types of scales from all of over the world. You can learn more about different kinds of scales here, and here.
This sketch demonstrates how one might use the ‘D Minor Pentatonic’ scale by providing the frequency values for each note. It also shows how you can provide a scale as an array of note names.
Valid note names are C, C#/Db, D, D#/Eb, E, F, F#/Gb, G, G#/Ab, A, A#/Bb, and B. There is a 10 octave range which you can specify after the note name using values 1-10. For example, ‘middle’ or ‘concert C’ can be denoted by the note name, C4.
Valid frequency values are 0 - 22,000, measured in Hertz (Hz), where 20 - 22,000 represents the range of human hearing (though keep in mind not many people can hear that high 😅). Values lower than 20 are usually used as ‘control values’ for other audio parameters.
Playing Melodies: Edited and maintained by p5.js Contributors and Processing Foundation. Licensed under CC BY-NC-SA 4.0.
Related Examples

An image of sound waves rendered in p5.js.
Making a Beep
Creating tones with an Oscillator.

An image of sound waves rendered in p5.js.
Filtering Sound
Simulate the sound of waves and wind with white noise.

An image of sound waves rendered in p5.js.
Playing Melodies
Create melodic music using an array of pitch values.

An image of sound waves rendered in p5.js.
Echo Synth
Trigger a short sound with an echo effect.

An image of sound waves rendered in p5.js.
Sample Playback
Load a soundfile and change its properties.

An image of sound waves rendered in p5.js.
Measuring Amplitude (Visualization)
Analyze the amplitude of sound with p5.Amplitude.

An image of sound waves rendered in p5.js.
Visualizing Sound
Spectral analysis for audio-responsive animations.

An image of sound waves rendered in p5.js.
Connecting to ToneJS
Working with AudioContexts and cross-library synthesis.