Go Down

Topic: Implementing MIDI Control Change for “Tsunami Wav Trigger”? (Read 1 time) previous topic - next topic

Grumpy_Mike

Quote
What do I need to do?
Don't use so many spaces in between code lines, that won't stop it working but it will look nicer.
All the sound generators I have seen do not use banks 0 to 4 to generate different sound they use more.

I am not sure what you are trying to do because changing the bank will only make a difference to the timbre of the note when you change the "instrument" with a program change. You don't seem to be changing the instrument just the bank, that in itself will do nothing until you change the instrument. However, this code should sort out the attack and release parameters at least.

Not only read up how to write code but also what MIDI messages do, because unless you know what messages you want to send it is impossible to write code to send them.   

Code: [Select]

int channel = 0;
int releasePram = 18;
int attack = 18;

void setup() {
  // Set MIDI baud rate:
  Serial.begin(31250);
  midirelease(channel, releasePram);    // =500ms
  midiattack(channel, attack);
}

void loop()
{
 // midiattack(channel, attack);   // only send it when you need to not hundreds of times a second
 // midirelease( channel, release ); // only send it when you need to not hundreds of times a second

  // Play notes in banks 0, 1, 2 and 3
  // (all on channel 0)
  //
  for ( int bank = 0; bank < 4; bank ++ )
  {
    midiBank( channel, bank );

    // play notes from G-3 (0x43) to G-4 (0x4F):
    for (int note = 0x43; note < 0x4F; note ++)
    {
      noteOn( channel, note, 0x45);
      delay(1000);
      noteOff( channel, note, 0x00);
      delay(1000);
    }
    delay( 1500 );
  }
}

void midirelease ( byte channel, byte pram)
{
  midiMsg(0xC0 | channel, 0x48, pram);
}

void midiattack ( byte channel, byte attack)
{
    midiMsg(0xC0 | channel, 0x49, attack);
}

void midiBank( byte channel, byte bank )
{
  // set MIDI bank LSB
  midiMsg(0xC0 | channel, 0x20, bank);
}

// plays a MIDI note. - restrict note and velocity to a maximum of 7F
// Send a MIDI note-on message.
void noteOn(byte channel, byte note, byte velocity)
{
  midiMsg( 0x90 | channel, note & 0x7F, velocity & 0x7F );
}

// Send a MIDI note-off message.
void noteOff(byte channel, byte note, byte velocity)
{
  midiMsg(0x90 | channel, note & 0x7F, velocity & 0x7F );
}

// Send a general MIDI message
//
void midiMsg(byte cmd, byte data1, byte data2 )
{
  Serial.write(cmd);
  Serial.write(data1);
  Serial.write(data2);
}


142victoria

The reason for  changing the bank is that when using MIDI, Tsunami maps MIDI Note number to track number, so MIDI Notes 1 - 128 map to Tracks 1 - 128. Similarly, a MIDI Bank feature shifts offsets the track number by 128, so MIDI Bank 1 maps MIDI Notes 1 - 128 to Tracks 129 - 256, and so on up to MIDI Bank 32. The MIDI Bank is incremented and decremented with  the MIDI Program
Change message.
Sorry I should have made this clear in my first post.

Thank you so much for your code.
Unfortunately this at first did not work, but I figured out that the attack / release was set as  0xC0 Program Change message rather than 0xB0 Controller Change message.
This test sketch now works. Cheers.

Attached is working code.
Code: [Select]


#include <MIDI.h>
int bank = 1;
int channel = 0;
int release = 64; // =1000ms
int attack = 64;

void setup() {
  // Set MIDI baud rate:
  Serial.begin(31250);
  midirelease(channel, release);   
  midiattack(channel, attack);
}

void loop() {

  // Play notes in banks 0, 1, 2 and 3
  // (all on channel 0)
  //
  for ( int bank = 0; bank < 4; bank ++ )
  {
    midiBank( channel, bank );

    // play notes from G-3 (0x43) to G-4 (0x4F):
    // for (int note = 0x1E; note < 0x5A; note ++)
    for (int note = 0x43; note < 0x4F; note ++)
    {
      noteOn( channel, note, 0x127);
      delay(3000);

      noteOff( channel, note, 0x00);
      delay(3000);
    }
    delay( 1500 );
  }
}

// plays a MIDI note. Doesn't check to see that cmd is greater than 127, or that
// data values are less than 127:

void midirelease ( byte channel, byte release)
{
  midiMsg( (0xB0 | channel ), 0x48, release);
}

void midiattack ( byte channel, byte attack)
{
  midiMsg( (0xB0 | channel ), 0x49, attack);
}

void midiBank( byte channel, byte bank )
{
  // Program Change, Bank select LSB = bank
  midiMsg( (0xC0 | channel ), 0x20, bank );
}

// Send a MIDI note-on message.
void noteOn(byte channel, byte note, byte velocity)
{
  midiMsg( (0x90 | channel), note, velocity);
}

// Send a MIDI note-off message.
void noteOff(byte channel, byte note, byte velocity)
{
  midiMsg( (0x90 | channel), note, velocity );
}

// Send a general MIDI message
//
void midiMsg(byte cmd, byte data1, byte data2 )
{
  Serial.write(cmd);
  Serial.write(data1);
  Serial.write(data2);
}







Go Up