Giter Site home page Giter Site logo

godot-simple-sampler's Introduction

Simple Sampler

Simple Sampler is a plugin for Godot for creating musical samplers in a game. Basically it allows you to play any musical note from a set of sound samples. Useful for making generative, interactive or procedural music, or for creating dynamic sound effects!

The repository includes a small demo that showcases what the plugin allows to do. Check out its scene and source code to quickly see how to use the samplers!

Installing

Copy the addons/SimpleSampler folder and put it in your project at the path addons/SimplerSampler. Then open Project > Project Settings and go to the Plugins tab. You should see the plugin Simple Sampler in the list. Activate it to use it in your project.

Usage

Basics

SimpleSampler gives you access to two new Nodes: Sampler Instrument and Sampler. For most of your projects, you should use Sampler Instrument, as it allows to play several notes simultaneously. But if you know that it won't be the case, Sampler might slightly lighter. Both nodes share the same methods.

Add new sampler

Samplers are musical instruments that take several sound samples as input, and can then play any note by calculating the sample to play and the pitch to apply.

To configure a Sampler, you need to register its note samples. For this, you must provide your own sound files (see the section below to know where you can find some). Each file must correspond to a single note. Of course, you don't need to have a file for every possible note (that would be too heavy)! Usually, having three samples per octave gives great results. Files can be single sounds, or loops, depending on how you import them. Samplers work well with .wav or .ogg!

Once you have your samples, you must add them to the Sampler, in the samples array. Simple Sampler provides a Resource to manage samples: NoteSample. Add one of them for each sample files you have.

For each sample, you must enter 3 properties:

  • The sound file (Stream)
  • Corresponding note (Tone)
  • Corresponding octave (Octave)

Configure samples

You must do this for every samples. The order you put them in the array doesn't matter. If you want to go faster, you can save NoteSample as resources. This allows you to use them elsewhere!

Once you have done that, your sampler is ready to use! To play a note, just call the function play_note with the note name (in uppercase) and the octave.

sampler.play_note("C", 4)

And that's it!

Also note that samplers are based on AudioStreamPlayers. Which means that you can set their volume or connect them to an audio bus just like any AudioStreamPlayer.

⚠️ Don't edit their volume or runtime though, as it is used by the sampler's envelop. Use a bus instead for these kind of dynamic effects.

If your sample is played on a loop, and you want to stop it, call the release method:

sampler.release()

Polyphony

As said above, SamplerInstruments are able to play several notes at the same time. This is useful for playing chords, but also for overlapping notes with long reverb or delay!

The number of simultaneous note possible is not infinite though. The limit must be set on the parameter Max Notes. If you go beyond that number, the first note played will be replaced.

To play a chord, simply call play_note several time:

# C minor 7
sampler.play_note("C")
sampler.play_note("Eb")
sampler.play_note("G")
sampler.play_note("A#")
# When omitting the octave parameter, the default value is 4

Calling release will release every currently playing note.

Envelop

Sampler and SamplerInstrument have a simplified sound envelop system to customize the sound of the instrument. It consists of three parameters: Attack, Sustain and Release. Their default value is -1, meaning that they are not active.

  • Attack is the length (in seconds) of a fade-in at the beginning of the sound. If negative, the sound will play immediately at its default volume.
  • Sustain is the length (in seconds) of the sound after the attack. Once this delay is passed, it will automatically call release. If negative, the sound will play entirely or in loop.
  • Release is the length (in seconds) of a fade-out once release has been called, either manually or after the sustain. If negative, the sound will stop immediately.

You can use those to have more dynamic instruments (with looping sounds), or if you find the length of your sample too long (using sustain and release allows you to interrupt it earlier).

Note Value Calculator

For a more advanced use, you can access the class NoteValueValculator used by the samplers:

var calculator: NoteValueCalculator = get_node("/root/NoteValue")

This tool converts notes into number values, and reciprocally. It can be useful if you want to do some arithmetic manipulation with the notes! Here are the functions it provides:

  • get_note_value(note: String, octave: int = 4) -> int returns the value of a note, where C0 is 0.
  • get_note_name(value: int) -> String returns the name of a note given its value
  • get_note_octave(value: int) -> int returns the octace of a note given its value

Those tools come in handy if you want to make procedural music based on mathematics and music theory!

Where can I find samples?

There are multiple places on the web where you can find audio samples for all kind of instruments. Here are some websites that provides free samples of great quality:

  • Philarmonia Orchestra : A collection of samples from the Philarmonia Orchestra's instruments. Comes with several length and variations (piano, forte, pizzicato, etc…)
  • FreePats project : A bank of free virtual instruments. A lot of them are available in the SFZ format, meaning you have access to the WAV files.
  • Karoryfer lecolds : A libray of sampled instruments. Most of them are not free, but you can still find some good ones at the end of the page that are under Creative Commons License.

There are surely plenty other places where you can find or purchase great sound samples. You can also make your own! As you've seen, you don't actually need a lot of them to make a sampler. So you can use the musical tool of your choice, play a bunch of notes, and make your own sample files!

Also, you can save and share your own samplers between projects (or even users!), either by saving NoteSample resources, or a Godot scene with a Sampler or SamplerInstrument as root.

godot-simple-sampler's People

Contributors

clementrivaille avatar aaronfranke avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.