⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.cpp

📁 CELLULAR AUTOMATA ONE DIMENSIONAL
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//===========================================================================
//                                                    AN INTRODUCTION TO MIDI
//                                                               Nick Gessler
//                                                           27 February 2005
//===========================================================================

// physical channels 0-15
// keys 0-127
// patches 0-127
// program change 0-127

//===========================================================================
//                                                          CELLULAR AUTOMATA
//                                                            ONE DIMENSIONAL
//===========================================================================
//   Given a 3-cell neighborhood,
//   there are 14 possible rules for a birth:
//     1) 0 neighbors
//     2) 1 neighbor
//     3) 2 neighbors
//     4) 3 neighbors
//     5) 0 or 1 neighbors
//     6) 0 or 2 neighbors
//     7) 0 or 3 neighbors
//     8) 1 or 2 neighbors
//     9) 1 or 3 neighbors
//    10) 2 or 3 nieghbors
//    11) 0 or 1 or 2 neighbors
//    12) 0 or 1 or 3 neighbors
//    13) 0 or 2 or 3 neighbors
//    14) 1 or 2 or 3 neighbors
//    15) 0 or 1 or 2 or 3 neighbors is a trivial case
//  For less than 3 conditions, duplicate the settings in the ComboBoxes
//  For instance, set "0 or 1" as "001" or "110" etc...
//===========================================================================


#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include <mmsystem.h>                                         // enables MIDI
//---------------------------------------------------------------------------
   #pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
   : TForm(Owner)
{
}
//---------------------------------------------------------------------------

//===========================================================================
//                                                                  VARIABLES
//===========================================================================

//============================================================ MIDI Variables
int midiport = 0;
HMIDIOUT device;

int pos;
int posToKey[28] = {
   35, 36, 38,40,41,43,45,47,48,50,52,53,55,
   57,59,60,62,64,65,67,69,71,72,74,76,77,79,81
};
int tune[32] = {
   0,0,
   60,65,64,62,60,57,0,0,
   60,65,64,62,63,64,0,0,
   64,64,60,62,60,64,60,62,
   48,52,48,50,48,41,
};
int length = 32;
int x, y;
int key, lastKey;
int instrument;
int tempo = 220;
int loudness = 100;

union {
   public:
   unsigned long word;
   unsigned char data[4];
} message;

//============================================================ CA VARIABLES
int generation = 0;
int parent[47], child[47];
int count;
int choice;
int interval = 5;

//===========================================================================
//                                                             MIDI FUNCTIONS
//===========================================================================

//----------------------------------------------------- Select Key from Image
void selectKeyFromImage (void) {

   // The white keys:
   if (x > 32 && x < 496 && y > 59 && y < 126) {
      pos = (x-32)/16.57142857;
      key = posToKey[pos];
   }
   else key = 00;                              // flag for "off the keyboard"

   // The black keys:
   if (y > 59 && y < 105) {
      if (x > 61 && x < 70) key = 37;
      if (x > 78 && x < 87) key = 39;
      if (x > 111 && x < 120) key = 42;
      if (x > 127 && x < 136) key = 44;
      if (x > 144 && x < 153) key = 46;
      if (x > 177 && x < 186) key = 49;
      if (x > 194 && x < 203) key = 51;
      if (x > 227 && x < 236) key = 54;
      if (x > 243 && x < 252) key = 56;
      if (x > 260 && x < 269) key = 58;
      if (x > 293 && x < 302) key = 61;
      if (x > 309 && x < 318) key = 63;
      if (x > 343 && x < 352) key = 66;
      if (x > 359 && x < 368) key = 68;
      if (x > 376 && x < 385) key = 70;
      if (x > 409 && x < 418) key = 73;
      if (x > 425 && x < 434) key = 75;
      if (x > 459 && x < 468) key = 78;
      if (x > 475 && x < 484) key = 80;
   }

   if ((key != lastKey && Form1->RadioGroupKeyStrike->ItemIndex == 1)
         && key != 0
         || Form1->RadioGroupKeyStrike->ItemIndex == 0
         && key != 0) {
      // we ignore multiple mouse move events from the same key
      // and we ignore mouse moves and downs off the keyboard
      message.data[0] = 0x90;                                       // key on
      message.data[1] = key;
      midiOutShortMsg(device, message.word);
      lastKey = key;
   }
}

//-------------------------------------------------------------- Play a Scale
void playScale (void) {
   for (int key = 35; key < 82; key++) {
      message.data[0] = 0x90;                                       // key on
      message.data[1] = key;
      midiOutShortMsg(device, message.word);
      Sleep(tempo);
      if (Form1->RadioGroupKeySustain->ItemIndex == 1) {
         message.data[0] = 0x80;                                   // key off
         message.data[1] = key;
         midiOutShortMsg(device, message.word);
      }
      Application->ProcessMessages();
   }
}

//----------------------------------------------------------------- Play Tune
void playTune (void) {
   for (int times = 0; times < 3; times++) {
      for (int note = 0; note < length; note++) {
         if (tune[note] != 0) {
            Application->ProcessMessages();
            message.data[0] = 0x90;                                // key on
            message.data[1] = tune[note] + times;
            midiOutShortMsg(device, message.word);
         }

         Sleep(tempo);
         if (Form1->RadioGroupKeySustain->ItemIndex == 1) {
            message.data[0] = 0x80;                                // key off
            message.data[1] = tune[note] + times;
            midiOutShortMsg(device, message.word);
         }
      }
      tempo -= 30;
   }
   tempo = 220;
}

//------------------------------------------------------------- Play Random 1
void playRandom1 (void) {
   int key;
   for (int i = 0; i < 50; i++) {
      key = random(46) + 36;
      message.data[0] = 0x90;                                       // key on
      message.data[1] = key;
      midiOutShortMsg(device, message.word);
      Sleep(tempo);
      if (Form1->RadioGroupKeySustain->ItemIndex == 1) {
         message.data[0] = 0x80;                                   // key off
         message.data[1] = key;
         midiOutShortMsg(device, message.word);
      }
      Application->ProcessMessages();
   }
}

//------------------------------------------------------------- Play Random 2
void playRandom2 (void) {
   int key;
   for (int i = 0; i < 50; i++) {
      key = random(46) + 36;
      message.data[0] = 0x90;                                       // key on
      message.data[1] = key;
      midiOutShortMsg(device, message.word);
      Sleep(random(300) + 100);
      if (Form1->RadioGroupKeySustain->ItemIndex == 1) {
         message.data[0] = 0x80;                                   // key off
         message.data[1] = key;
         midiOutShortMsg(device, message.word);
      }
      Application->ProcessMessages();
   }
}

//===========================================================================
//                                             1D Cellular Automata Functions
//===========================================================================

//---------------------------------------------------------- Randomize Parent
void randomizeParent (void) {
   for (int i = 0; i < 47; i++) {
      parent[i] = random(2);
   }
}

//---------------------------------------------------------------- Initialize
void initialize (void) {
   Randomize();
   Form1->PaintBox1->Refresh();
   generation = 0;
   Form1->EditGeneration->Text = generation;
   randomizeParent();
}

//----------------------------------------------------------------------- Run
void run (void) {
   initialize();
   for (int i = 0; i < 40; i++) {
      generation ++;
      Form1->EditGeneration->Text = generation;

                        // calculate child generation  from parent generation

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -