Making a basic music sequencer with minim

edited November 2016 in Library Questions

I've started on a project to create a music sequencer with minim using instrument classes and playNote functions etc. I've got a sequence of notes working ok, but I have no way of looping them. Here's the main code:

import ddf.minim.*;
import ddf.minim.ugens.*;

Minim       minim;
AudioOutput out;
Delay myDelay;
int i;
int endSeq;

public void settings() {
  size(800, 300, P2D);
}

void setup() {

  minim = new Minim(this);
  out = minim.getLineOut();
  myDelay = new Delay(1.5,0.5, false,false);
  bassSeq(); // starts bass sequence function
  chordSeq(); // starts chord sequence function
}

void draw() {
}

And here's the bassSeq function which describes the whole bass sequence:

void bassSeq(){
float[] stepArray = {0.00, 0.75, 2.00, 2.5, 2.75, 3.5, 4, 4.5};
float[] decayArray = {1.0, 0.5, 0.5, 0.5, 0.5, 0.2, 0.5, 1.0};
float[] noteArray = {C2, F2, E2, G2, B2, C2, C2, B2};

for (i = 0; i <= 7; i++){

  out.pauseNotes();
  out.setTempo(125);
  out.playNote(stepArray[i], decayArray[i], new BassInstr(noteArray[i], 0.5, out ) );
  out.resumeNotes();

}
}

The problem is, if I put these functions in setup, they only play once. What ideally I'd want to do is iterate back to the start of i=0 so it gives the instruments the sequence from the start of the array after it is finished one loop. If I put the sequence functions in the draw loop, it sends the instrument notes incredibly fast (prob around 60fps if that's the default for draw). I've put a delay() function in the draw loop which does actually loop the music after the time but there's no way of giving delay a number that is totally accurate so it loops accurately.

Some help would be appreciated.

Answers

  • Does out have a isPlaying property?

    Then check it in draw

    If it's false call bassSeq

  • Avoid double questions/ posts

  • I think isPlaying only applies to audio files, I can't get processing to recoginise the function.

  • Ended up getting it working by adding a variable to the note startime parameter, so once the last item of the array is passed to the playNote function, x is reset and the total number of steps is added to each consecutive startime value, like so:

      out.playNote(stepArray[x] + b, decayArray[x], new ChordInstr(Fs4, 0.2, out ) );
      out.playNote(stepArray[x] + b, decayArray[x], new ChordInstr(B4, 0.2, out ) );
      out.playNote(stepArray[x] + b, decayArray[x], new ChordInstr(D5, 0.2, out ) );
       x = x +1;
      out.resumeNotes();
      if (x ==3){
        x = 0;
        b = b + 6;
    
  • curiosity :

    could you post your entire code in one piece please?

    also remember, you can hit ctrl-t in processing to get better indents

    Thanks.

    Chrisir

  • Answer ✓

    Sure. It's in seperate tabs so might be a bit messy as a whole

    import ddf.minim.*;
    import ddf.minim.ugens.*;
    
    Minim       minim;
    AudioOutput out;
    Delay myDelay;
    
    int endSeq;
    int x;
    
    public void settings() {
      size(800, 300, P2D);
    }
    
    void setup() {
    
      minim = new Minim(this);
      out = minim.getLineOut();
      //myDelay = new Delay(1.5, 0.5, false, false);
      chordSeq();
      bassSeq();
    }
    
    void draw() {
    }
    
    
    void chordSeq() {
      float[] stepArray = {0.01, 2.00, 4.00, 6.00, };
      float[] decayArray = {1, 1, 1, 1};
      float[] noteArray = {C2, F2, E2, G2, B2, C2, C2, B2};
      int x = 0;
      float b = 0;
    
      //while(millis()<10000000){
      for (int i = 0; i <= 300; i++) {
    
    
        out.pauseNotes();
        out.setTempo(125);
    
        out.playNote(stepArray[x] + b, decayArray[x], new ChordInstr(Fs4, 0.2, out ) );
        out.playNote(stepArray[x] + b, decayArray[x], new ChordInstr(B4, 0.2, out ) );
        out.playNote(stepArray[x] + b, decayArray[x], new ChordInstr(D5, 0.2, out ) );
        x = x +1;
        out.resumeNotes();
        if (x ==3) {
          x = 0;
          b = b + 6;
        }
      }
      //println(i);
      //}
    }
    
    void bassSeq() {
      float[] stepArray = {0.00, 0.75, 2.00, 2.5, 2.75, 3.5, 4, 4.5};
      float[] decayArray = {1.0, 0.5, 0.5, 0.5, 0.5, 0.2, 0.5, 1.0};
      float[] noteArray = {C2, F2, E2, G2, B2, C2, C2, B2};
      int x = 0;
      float b = 0;
      for ( int i = 0; i <= 300; i++) {
    
        out.pauseNotes();
        out.setTempo(125);
    
    
        out.playNote(stepArray[x]+ b, decayArray[x], new BassInstr(noteArray[x], 0.5, out ) );
        out.resumeNotes();
        x++;
        if (x == 8) {
          b = b + 7;
          x = 0;
        }
    
        println(b);
      }
    }
    
    class ChordInstr implements Instrument { // creates an instrument class
      Oscil       wave;
      Damp damp; //initialises envelope function
      ChordInstr(float frequency, float amplitude, AudioOutput output) {
    
        out = output;
    
        wave = new Oscil(frequency, amplitude, Waves.SINE);
        damp = new Damp(1, 3);
        wave.patch(damp);
        wave.patch(myDelay);
      }
    
      void noteOn(float dur) {
    
        damp.setDampTimeFromDuration(dur);
        damp.activate();
        damp.patch(out);
      }
    
      void noteOff() {
        damp.unpatchAfterDamp(out);
      }
    }
    
    class BassInstr implements Instrument { // creates an instrument class
      Oscil       wave;
      Damp damp; //initialises envelope function
      BassInstr(float frequency, float amplitude, AudioOutput output) {
    
        out = output;
    
        wave = new Oscil(frequency, amplitude, Waves.TRIANGLE);
        damp = new Damp(0.0001, 1.5);
        wave.patch(damp);
        wave.patch(myDelay);
      }
    
      void noteOn(float dur) {
    
        damp.setDampTimeFromDuration(dur);
        damp.activate();
        damp.patch(out);
      }
    
      void noteOff() {
        damp.unpatchAfterDamp(out);
      }
    }
    
  • but what are the values for

      float[] noteArray = {C2, F2, E2, G2, B2, C2, C2, B2};
    

    ?

  • and what is Delay?

    thanks!

  • I created a list of variables that contain the note name and their corresponding frequency values. This saves me from having to find the frequency of each note I want to play.

    float C0  = 16.35;
    float Cs0  =    17.32;
    float D0    = 18.35;
    float Ds0 = 19.45;
    float E0    = 20.60;
    float F0    = 21.83;
    float Fs0 =     23.12;
    float G0    = 24.50;
    float Gs0 = 25.96;
    float A0    = 27.50;
    float As0 =     29.14;
    float B0    = 30.87;
    float C1    = 32.70;
    float Cs1=      34.65;
    float D1    = 36.71;
    float Ds1=  38.89;
    float E1    = 41.20;
    float F1    = 43.65;
    float Fs1=  46.25;
    float G1    = 49.00;
    float Gs1   =   51.91;
    float A1    = 55.00;
    float As1   =   58.27;
    float B1    = 61.74;
    float C2    = 65.41;
    float Cs2   =   69.30;
    float D2    = 73.42;
    float Ds2    =      77.78;
    float E2    = 82.41;
    float F2    = 87.31;
    float  Fs2=     92.50;
    float G2    = 98.00;
    float  Gs2=     103.83;
    float A2    = 110.00;
    float  As2=     116.54;
    float B2    = 123.47;
    float C3    = 130.81;
    float  Cs3=     138.59;
    float D3    = 146.83;
    float  Ds3=     155.56;
    float E3    = 164.81;
    float F3    = 174.61;
    float  Fs3=     185.00;
    float G3    = 196.00;
    float  Gs=      207.65;
    float A3    = 220.00;
    float  As3=     233.08;
    float B3    = 246.94;
    float C4    = 261.63;
    float  Cs4=     277.18;
    float D4    = 293.66;
    float  Ds4=     311.13;
    float E4    = 329.63;
    float F4    = 349.23;
    float  Fs4=     369.99;
    float G4    = 392.00;
    float  Gs4=     415.30;
    float A4    = 440.00;
    float  As4=     466.16;
    float B4    = 493.88;
    float C5    = 523.25;
    float  Cs5=     554.37;
    float D5    = 587.33;
    float  Ds5=     622.25;
    float E5    = 659.25;
    float F5    = 698.46;
    float  Fs5=     739.99;
    float G5    = 783.99;
    float  Gs5=     830.61;
    float A5    = 880.00;
    float  As5=     932.33;
    float B5    = 987.77;
    float C6    = 1046.50;
    float  Cs6=     1108.73;
    float D6    = 1174.66;
    float  Ds6=     1244.51;
    float E6    = 1318.51;
    float F6    = 1396.91;
    float  Fs6=     1479.98;
    float G6    = 1567.98;
    float  Gs6=     1661.22;
    float A6    = 1760.00;
    float  As6=     1864.66;
    float B6    = 1975.53;
    float C7    = 2093.00;
    float  Cs7=     2217.46;
    float D7    = 2349.32;
    float  Ds7=     2489.02;
    float E7    = 2637.02;
    float F7    = 2793.83;
    float  Fs7=     2959.96;
    float G7    = 3135.96;
    float  Gs7=     3322.44;
    float A7    = 3520.00;
    float  As7=     3729.31;
    float B7    = 3951.07;
    float C8    = 4186.01;
    float  Cs8=     4434.92;
    float D8    = 4698.63;
    float  Ds8=     4978.03;
    float E8    = 5274.04;
    float F8    = 5587.65;
    float  Fs8=     5919.91;
    float G8    = 6271.93;
    float  Gs8=     6644.88;
    float A8    = 7040.00;
    float  As8=     7458.62;
    float B8    = 7902.13;
    

    'Delay' is just an effect class with minim. It was a remaining piece of code from a tutorial I was doing, it's not used in this case.

  • thanks.

    but it still doesn't run.

    Now I get an error for :

      out.playNote(stepArray[x] + b, decayArray[x], new ChordInstr(Fs4, 0.2, out ) );
    

    maybe because ChordInstr is not an Instrument?

    How can I make it work?

  • What does the error say?

  • The command playNote expects float, float, Instrument

  • i need to check, maybe I know why

    does the sketch run for you?

  • message me your email i will send you zip.

  • I, too, would like to learn from your code. Thanks for sharing what you've posted so far. Any chance you could post the whole thing? By which I mean enough to make this fragment work.

Sign In or Register to comment.