CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Midi Piano Keyboard rOuTiNeS

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
aaaaamartin



Joined: 17 Apr 2005
Posts: 39
Location: Germany Stuttgart

View user's profile Send private message

Midi Piano Keyboard rOuTiNeS
PostPosted: Mon Jul 25, 2005 5:38 pm     Reply with quote

Hi folks,

I'm currently trying to get my Piano Keyboards to work @ 877.
I do have keyboards organized as 16x8 Matrix = 61 keys,
velocity sensitve though.

The point is, my routines are f*ck*d up.
They do work, but these are too slow ;)

Any help - links, code, ideas on doing this a little faster, still within c,
is highly appreciated.

I'm using a buffer struct storing key#;status;velocity.
This is "10 keys" wide.
Don't make me post the code Smile

I can't believe there is no one out there who is willing to share code for that !

Come on, aren't we all workin' on these keyboardcontrollers ;)

Thanks Martin
jbmiller
Guest







midi keyboard info
PostPosted: Tue Jul 26, 2005 3:22 pm     Reply with quote

First, 16*8 would give you 128 keys but you only need 61, so use an 8 * 8 matrix which is much faster.Been there-done that.

Second, profanity, even, of the *&^ type is NOT nice especially if you want help

Third, without posting YOUR code it is vitually impossible to figure out why your code is too slow.

Fourth, asking for other's code is ok but you'll get better responses without number 2 above

Fifth, Google is your friend, MIDI code is out there, been there- done that as the saying goes

Sixth, the actual hardware can be a problem......no mention of it...
4MHz CPU, 20 MHz ????

This board has a great number of great people using it, most are probably put off by the 'attitude' of your post. If you apologize, rework you questions and post your code, someone will help you.

Jay
Guest








PostPosted: Tue Jul 26, 2005 4:28 pm     Reply with quote

Hi jbmiller,

first of all sorry, I got stuck, all my friends tell me I#m programming keyboard controllers, what in my case is the truth ;)

To your questions:
1- Yes Matrix is 16x8 there are 122 closures, rest is unused. each key has two closures, they do close one by one depressing the key.
Time in between gives you velocity.

2- nothing here

3- way down

4- again nothing here

5- found something once, lost it, I'm stuck.

6- I can't believe it, it does work even on 16F84 for much bigger keyboards with bigger buffers and more sophisticated features.
I guess my code is crappy. I do not yet blame the compiler.
Would I complain about speed using 4MHz clock, when I can have 20 ?

Finally: Yes there are good people arround, but google can't see them,
that is why I post my question here, asking for ready made code, or hints.
Opensource is far away from PIC.

Now to the hardware:
Clock is 20 MHz there are two 74HCT138 connected to port_d using lower 5 Bits for enable and adress. Together this forms the columns as seen in the code.
Port_b has the rows connected.
A pair of columns let's say 0&1, 2&3 and so on belong to the same key.
This is refered to as make and brake closures and needed for the velocity.
Velocity is the loudness of each note played, attack in other words.

Here's my thought.
Code:

#include "D:\Picprojects\keying2\keying2.h"
#use rs232(baud=38400, xmit=PIN_C6, rcv=PIN_C7)

int x=0;
int col;
int row;
int bitcnt;
int key;
int emptyoffset;
int keyoffset;

int const buffsize = 10;

int buff [buffsize][3]; // 0 = key#; 1 = status; 2 = velocity

void
note_on (int key,int vel)
{
   putc (0x90);
   putc (key);
   putc (vel);
}

void
note_off (int key)
{
   putc (0x90);
   putc (key);
   putc (0x00);
}

//**************** debug ***********
dis_buff ()
{
   printf("\033[2J ");
   printf ("Buffer is :\r\n");
   for (x=0;x<=9;x++)
   {
      if (buff[x][0]!=255)
      {
         printf ("*********** * %01u * ************\r\n",x);
         printf ("KEY-%03u\r\n",buff[x][0]);
         printf ("STA-%03u\r\n",buff[x][1]);
         printf ("VEL-%03u\r\n",buff[x][2]);
      }     
   }
   //delay_ms (100);
}
//**************** debug ************

#int_rtcc
rtcc_isr () {
}

main()
{
port_b_pullups(TRUE);
set_rtcc (0);
setup_counters(RTCC_INTERNAL,RTCC_DIV_128);
//enable_interrupts (INT_RTCC);
//enable_interrupts(GLOBAL);

for (x=0; x<=(buffsize-1); x++)  // init buff;
{
   buff[x][0] = 255;
   buff[x][1] =   0;
   buff[x][2] =   0;
}

start:

for (col=0;col<=15;col++) // this goes to the HCT138 mux
{
   output_d (col);
   delay_us (30);
   row = input_b ();     // this is from the keyboard
   delay_us (30);
   key = col;           // key is the midi note value
   if (col%2==0)        // each key has two contacts (velocity !)
   {
      key = col+1;
   }
   key = key << 2;
   key +=32;
   for (bitcnt=0;bitcnt<=7;bitcnt++) // processing bit by bit for current column
   {
      emptyoffset = 255;
      keyoffset   = 255;
      for (x=0; x<=buffsize-1; x++) // this is the worse part
      {                             // not having String-Literal-Index for arrays
         if (buff[x][0] == 255)     // I need to walk through the whole buffer.
         {                         
            emptyoffset = x;        // free location in buffer or 255
         }
         if (buff[x][0] == key)
         {
            keyoffset = x;          // current key @ offset ? or 255
         }
      }
      if (col%2!=0) // all make contacts
      {
         if (!bit_test (row,bitcnt)) // closed  ?
         {
            if ((keyoffset == 255) && (emptyoffset != 255)) // not yet buffered & space left
            {
            buff[emptyoffset][0] = key; // put into buffer
            buff[emptyoffset][2] = 127; // velocity starts @ 127, then periodicaly reduced
            }
         }
         else // not closed
         {
            if (keyoffset != 255) // but in the buffer
            {
               if (buff[keyoffset][1] == 1) // note on sent before ?
               {
                  note_off (key);
               }
               buff[keyoffset][0] = 255; // kill buffer location
               buff[keyoffset][1] = 0;
               buff[keyoffset][2] = 0;
            }
         }
      }
      else // all break contacts
      {
         if (!bit_test (row,bitcnt)) // closed  ?
         {
            if ((keyoffset != 255) && (buff[keyoffset][1] == 0)) // buffered key ? need to send note_on ?
            {
               note_on (key, buff[keyoffset][2]);
               buff[keyoffset][1] = 1; // 1 means note_on has been sent
            }
         }
      }
      key++; // next key, waste time somewhere else
   }
}

for (x=0; x<=(buffsize-1); x++) // reduce velocity, needs work, too linear...
{
   (buff[x][2] >=3) ? (buff[x][2]-=3) : 0;
}

goto start;
}



Thanks for listening, Martin.

ps. by the way I came up with PIC and whole C in late April this year.
Guest








PostPosted: Tue Jul 26, 2005 4:28 pm     Reply with quote

Hi jbmiller,

first of all sorry, I got stuck, all my friends tell me I#m programming keyboard controllers, what in my case is the truth ;)

To your questions:
1- Yes Matrix is 16x8 there are 122 closures, rest is unused. each key has two closures, they do close one by one depressing the key.
Time in between gives you velocity.

2- nothing here

3- way down

4- again nothing here

5- found something once, lost it, I'm stuck.

6- I can't believe it, it does work even on 16F84 for much bigger keyboards with bigger buffers and more sophisticated features.
I guess my code is crappy. I do not yet blame the compiler.
Would I complain about speed using 4MHz clock, when I can have 20 ?

Finally: Yes there are good people arround, but google can't see them,
that is why I post my question here, asking for ready made code, or hints.
Opensource is far away from PIC.

Now to the hardware:
Clock is 20 MHz there are two 74HCT138 connected to port_d using lower 5 Bits for enable and adress. Together this forms the columns as seen in the code.
Port_b has the rows connected.
A pair of columns let's say 0&1, 2&3 and so on belong to the same key.
This is refered to as make and brake closures and needed for the velocity.
Velocity is the loudness of each note played, attack in other words.

Here's my thought.
Code:

#include "D:\Picprojects\keying2\keying2.h"
#use rs232(baud=38400, xmit=PIN_C6, rcv=PIN_C7)

int x=0;
int col;
int row;
int bitcnt;
int key;
int emptyoffset;
int keyoffset;

int const buffsize = 10;

int buff [buffsize][3]; // 0 = key#; 1 = status; 2 = velocity

void
note_on (int key,int vel)
{
   putc (0x90);
   putc (key);
   putc (vel);
}

void
note_off (int key)
{
   putc (0x90);
   putc (key);
   putc (0x00);
}

//**************** debug ***********
dis_buff ()
{
   printf("\033[2J ");
   printf ("Buffer is :\r\n");
   for (x=0;x<=9;x++)
   {
      if (buff[x][0]!=255)
      {
         printf ("*********** * %01u * ************\r\n",x);
         printf ("KEY-%03u\r\n",buff[x][0]);
         printf ("STA-%03u\r\n",buff[x][1]);
         printf ("VEL-%03u\r\n",buff[x][2]);
      }     
   }
   //delay_ms (100);
}
//**************** debug ************

#int_rtcc
rtcc_isr () {
}

main()
{
port_b_pullups(TRUE);
set_rtcc (0);
setup_counters(RTCC_INTERNAL,RTCC_DIV_128);
//enable_interrupts (INT_RTCC);
//enable_interrupts(GLOBAL);

for (x=0; x<=(buffsize-1); x++)  // init buff;
{
   buff[x][0] = 255;
   buff[x][1] =   0;
   buff[x][2] =   0;
}

start:

for (col=0;col<=15;col++) // this goes to the HCT138 mux
{
   output_d (col);
   delay_us (30);
   row = input_b ();     // this is from the keyboard
   delay_us (30);
   key = col;           // key is the midi note value
   if (col%2==0)        // each key has two contacts (velocity !)
   {
      key = col+1;
   }
   key = key << 2;
   key +=32;
   for (bitcnt=0;bitcnt<=7;bitcnt++) // processing bit by bit for current column
   {
      emptyoffset = 255;
      keyoffset   = 255;
      for (x=0; x<=buffsize-1; x++) // this is the worse part
      {                             // not having String-Literal-Index for arrays
         if (buff[x][0] == 255)     // I need to walk through the whole buffer.
         {                         
            emptyoffset = x;        // free location in buffer or 255
         }
         if (buff[x][0] == key)
         {
            keyoffset = x;          // current key @ offset ? or 255
         }
      }
      if (col%2!=0) // all make contacts
      {
         if (!bit_test (row,bitcnt)) // closed  ?
         {
            if ((keyoffset == 255) && (emptyoffset != 255)) // not yet buffered & space left
            {
            buff[emptyoffset][0] = key; // put into buffer
            buff[emptyoffset][2] = 127; // velocity starts @ 127, then periodicaly reduced
            }
         }
         else // not closed
         {
            if (keyoffset != 255) // but in the buffer
            {
               if (buff[keyoffset][1] == 1) // note on sent before ?
               {
                  note_off (key);
               }
               buff[keyoffset][0] = 255; // kill buffer location
               buff[keyoffset][1] = 0;
               buff[keyoffset][2] = 0;
            }
         }
      }
      else // all break contacts
      {
         if (!bit_test (row,bitcnt)) // closed  ?
         {
            if ((keyoffset != 255) && (buff[keyoffset][1] == 0)) // buffered key ? need to send note_on ?
            {
               note_on (key, buff[keyoffset][2]);
               buff[keyoffset][1] = 1; // 1 means note_on has been sent
            }
         }
      }
      key++; // next key, waste time somewhere else
   }
}

for (x=0; x<=(buffsize-1); x++) // reduce velocity, needs work, too linear...
{
   (buff[x][2] >=3) ? (buff[x][2]-=3) : 0;
}

goto start;
}



Thanks for listening, Martin.

ps. by the way I came up with PIC and whole C in late April this year.
aaaaamartin



Joined: 17 Apr 2005
Posts: 39
Location: Germany Stuttgart

View user's profile Send private message

PostPosted: Fri Jul 29, 2005 5:12 pm     Reply with quote

Thx for great help,

too many replys so I did it my self.

Willing to share code,

mail m.wochinger@gmx.de .

Martin
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group