//=========================================================================== // MUSIC BOX // Nick Gessler // 16 November 2009 // 4 + (9 bands of 6 tracks each(54)) + 3 = 61 digital tracks // sampled at 629 sample sectors per revolution ??? analog sectors //=========================================================================== #include #pragma hdrstop #include "Unit1.h" #include "math.h" #include // we need this //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- //================================================================= VARIABLES int midiport = 0; HMIDIOUT device; union { public: unsigned long word; unsigned char data[4]; } message; int instrument = 10; int mnote; int note[61]; int lastNote[61]; int centX = 442, centY = 453; float c; // cosine float s; // sin float r; // rotation int rr; //float t = 5.6557377049180327868852459016393; // track width float t = 5.79497377049180327868852459016393; // track width float k = 0.010; // sector width of .002 gives solid circles int notes[61][100]; int color; int red; int it; //iterations int track; //================================================================= FUNCTIONS //-------------------------------------------------------- MIDI RAMP FUNCTION int midiRamp(int part, int whole) { // there are 47 MIDI notes which range from 35 to 81 mnote = (part * 47) / whole; mnote = mnote + 35; return mnote; } void play (void) { Form1->Image1->Canvas->Pixels[442][453] = clYellow; it = 0; //for (r = 0; r < 2*M_PI; r += .010) { // cw // .002 is good for (r = 2*M_PI; r >0 ; r -= .005) { // ccw // .002 is good it++; c = cos(r); s = sin(r); // outside disk Form1->Image1->Canvas->Pixels[centX + 430 * c][centY + 430 * s] = clYellow; // inside track //Form1->Image1->Canvas->Pixels[centX + 52 * c][centY + 52 * s] = clWhite; // outside track //Form1->Image1->Canvas->Pixels[centX + 397 * c][centY + 397 * s] = clWhite; // every track //Form1->EditSektor->Text = r; for (track = 0; track <= 60; track++) { // 61 rr = r; color = Form1->Image1->Canvas->Pixels [centX + (52 + track * t) * c][centY + (52 + track * t) * s]; red = GetRValue(color); // Form1->EditTrack->Text = track; // Form1->EditRed->Text = red; if (red < 50 && track < 59) { // ignor the sprocket holes note[track] = midiRamp(track, 60); // low notes inside //note[track] = midiRamp(60 - track, 60); // low notes outside if (note[track] != lastNote[track]) { message.data[0] = 0x90; // note on message.data[1] = note[track]; // note # midiOutShortMsg(device, message.word); // send } lastNote[track] = note[track]; } else lastNote[track] = 999; Sleep(1); Application->ProcessMessages(); } } Form1->EditSektors->Text = it; } void tracks (void) { Form1->Image1->Canvas->Pixels[442][453] = clYellow; for (r = 0; r < 2*M_PI; r += .010) { // .002 is good c = cos(r); s = sin(r); // outside disk Form1->Image1->Canvas->Pixels[centX + 430 * c][centY + 430 * s] = clRed; // inside track Form1->Image1->Canvas->Pixels[centX + 52 * c][centY + 52 * s] = clRed; // outside track Form1->Image1->Canvas->Pixels[centX + 397 * c][centY + 397 * s] = clLime; // every track for (int i = 0; i < 61; i++) { Form1->Image1->Canvas->Pixels[centX + (52 + i * t) * c] [centY + (52 + i * t) * s] = clFuchsia; } } } //============================================================ EVENT HANDLERS __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { DoubleBuffered = true; Paint(); midiOutOpen(&device, midiport, 0, 0, CALLBACK_NULL); message.data[0] = 0xC0; // choose instrument command message.data[1] = instrument; // instrument message.data[2] = 100; // once set, no need to repeat message.data[3] = 0; // once set, no need to repeat midiOutShortMsg(device, message.word); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormPaint(TObject *Sender) { Form1->Image1->Canvas->Pixels[442][453] = clRed; } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonPlayClick(TObject *Sender) { play(); } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonTracksClick(TObject *Sender) { tracks(); } //---------------------------------------------------------------------------