Skip to content

Using Notes

Samuel H edited this page Jul 22, 2021 · 1 revision

In the previous tutorial have seen how to play a note. In this tutorial, we will see how to use multiple notes.

Linux/Mac/Cygwin users: Start off by creating a folder and name it "Tutorial2". Create a file inside of Tutorial2 and name it "note.cpp". Then, navigate to the cloned TSAL folder and into the genericMakefile folder. Copy the Makefile into Tutorial2 and change the "TARGET" line so that "program" is now "note". Follow the steps in the Building Programs page on how to compile and run the program (this is a single-file program).

First off let's add out mixer and synth:

#include "tsal.hpp"
using namespace tsal;

int main() {
  Mixer mixer;
  ThreadSynth synth(&mixer);
  mixer.add(synth);
  synth.setEnvelopeActive(false);

The idea is to play different notes in ascending or descending order. To do this, we will use a for loop with a constant of size, SIZE.

#include "tsal.hpp"
using namespace tsal;

int main() {
  Mixer mixer;
  ThreadSynth synth(&mixer);
  mixer.add(synth);
  synth.setEnvelopeActive(false);

  const int SIZE = 500;

  for (int i = 0; i < SIZE; i++) {
    MidiNote note = Util::scaleToNote(i, std::make_pair(0, SIZE), std::make_pair(C3, C7));
    synth.play(note, Timing::MILLISECOND, 1);
  }

The scaleToNote takes in i which ranges from 0 to 500, and then uses the last two arguments to map 0 to SIZE with notes from C3 to C7. This way it plays a sound of ascending notes from C3 to C7 is based on our i and SIZE.

synth.play takes in the note we generated and the duration of the sound.

Now that you have learned how to utilize synth and notes, we will look into Intro to Multithreading in the next tutorial.

Clone this wiki locally