/*
(c) Copyright 1988-1994 Microsoft Corporation.

Microsoft Bars&Pipes Professional Source Code License

This License governs use of the accompanying Software.  
Microsoft hopes you find this Software useful.

You are licensed to do anything you want with the Software. 

In return, we simply require that you agree:

1.      Not to remove any copyright notices from the Software.

2.      That the Software comes "as is", with no warranties.  
        None whatsoever. This means no implied warranty of merchantability or 
        fitness for a particular purpose or any warranty of non-infringement.  
        Also, you must pass this disclaimer on whenever you distribute the Software.

3.      That we will not be liable for any of those types of damages known as indirect, 
        special, consequential, or incidental related to the Software or this License, 
        to the maximum extent the law permits. Also, you must pass this limitation of 
        liability on whenever you distribute the Software.

4.      That if you sue anyone over patents that you think may apply to the Software 
        for that person’s use of the Software, your license to the Software ends automatically.

5.      That the patent rights Microsoft is licensing only apply to the Software, 
        not to any derivatives you make.

6.      That your rights under the License end automatically if you breach this in any way.
*/

#include "/CompilerSpecific.h"
#include <libraries/dos.h>
#include <proto/exec.h>
#include <proto/graphics.h>
#include <proto/intuition.h>
#include <exec/memory.h>
#include <string.h>
#include <intuition/intuition.h>
#include "/v.h"

#define ID_SFZO 0x53465A4F

CHIP static UWORD Sforzando[]=
{
/*-------- plane # 0 --------*/

  0x0000,  0x0000,
  0x3fff,  0xfc00,
  0x3000,  0x0c00,
  0x3000,  0x0c00,
  0x3000,  0x0c00,
  0x3000,  0x0c00,
  0x3000,  0x0c00,
  0x3000,  0x0c00,
  0x3000,  0x0c00,
  0x3000,  0x0c00,
  0x3fff,  0xfc00,
  0x0000,  0x0000,

/*-------- plane # 1 --------*/

  0x0000,  0x0000,
  0x0000,  0x0400,
  0x0fff,  0xfc00,
  0x0fff,  0xfc00,
  0xcfff,  0xff00,
  0xcfff,  0xff00,
  0xcfff,  0xff00,
  0xcfff,  0xff00,
  0x0fff,  0xfc00,
  0x0fff,  0xfc00,
  0x1fff,  0xfc00,
  0x0000,  0x0000,

/*-------- plane # 2 --------*/

  0x0000,  0x0000,
  0x0000,  0x0000,
  0x0001,  0x8000,
  0x0004,  0x0000,
  0x00ce,  0xf000,
  0x0248,  0x2000,
  0x0188,  0x4000,
  0x0091,  0x0000,
  0x0492,  0x0000,
  0x0627,  0x8000,
  0x0000,  0x0000,
  0x0000,  0x0000
};

static struct Image Sforzandoimage=
{
  0,0,24,12,3,
  Sforzando,
  0x7,0x0,NULL
};

struct SfzTool {
    struct Tool tool;
    short type;
};
 
extern struct Functions *functions;

extern printf();


SAVEDS static struct Event *processeventcode(struct NoteEvent *event)
{
    struct SfzTool *tool = (struct SfzTool *) event->tool;
    struct NoteEvent *new;
    unsigned long time;
    long duration;
    short interval, count, change;
    short counter;
    char velocity, volume;
    event->tool = event->tool->next;
    if (event->status == MIDI_NOTEON) {
        velocity = event->velocity;
        event->velocity = 120;
        if (event->type & EVENT_PADEDIT) duration = event->duration - 48;
        else duration = 720;
        time = event->time;
        volume = 120;
        for (count = 3;count;count--) {
            time += 16;
            volume -= 35;
            new = (struct NoteEvent *) (*functions->allocevent)();
            if (new) {
                new->time = time;
                new->value = event->value;
                new->velocity = volume;
                new->type = EVENT_VOICE;
                new->status = MIDI_PTOUCH;
                new->tool = event->tool;
                event = (struct NoteEvent *) (*functions->List_Cat)(event,new);
            }
        }
        if (tool->type == 1) return((struct Event *)event);
        if (tool->type == 2) velocity = 110;
        interval = duration >> 4;
        change = velocity - 15;
        counter = 240; /* 15 << 4 */
        for (count = 16;count;count--) {
            counter += change;
            volume  = counter >> 4;
            new = (struct NoteEvent *) (*functions->allocevent)();
            if (new) {
                new->time = time;
                new->value = event->value;
                new->velocity = volume;
                new->type = EVENT_VOICE;
                new->status = MIDI_PTOUCH;
                event = (struct NoteEvent *) (*functions->List_Cat)(event,new);
            }
            time += interval;
        }
    }
    return((struct Event *)event);
}


static struct IntuiText sfzIText1 = {
        4,0,JAM1,
        5,1,
        NULL,
        "Sforzando Crescendo",
        NULL
};

static struct Gadget sfzGadget3 = {
        NULL,
        14,38,
        160,9,
        GADGHBOX+GADGHIMAGE,
        RELVERIFY+TOGGLESELECT,
        BOOLGADGET,
        NULL,
        NULL,
        &sfzIText1,
        NULL,
        NULL,
        3,
        NULL
};

static struct IntuiText sfzIText2 = {
        4,0,JAM1,
        21,1,
        NULL,
        "Sforzando Piano",
        NULL
};

static struct Gadget sfzGadget2 = {
        &sfzGadget3,
        14,27,
        160,9,
        GADGHBOX+GADGHIMAGE,
        RELVERIFY+TOGGLESELECT,
        BOOLGADGET,
        NULL,
        NULL,
        &sfzIText2,
        NULL,
        NULL,
        2,
        NULL
};

static struct IntuiText sfzIText3 = {
        4,0,JAM1,
        45,1,
        NULL,
        "Sforzando",
        NULL
};

static struct Gadget sfzGadget1 = {
        &sfzGadget2,
        14,16,
        160,9,
        GADGHBOX+GADGHIMAGE,
        RELVERIFY+TOGGLESELECT,
        BOOLGADGET,
        NULL,
        NULL,
        &sfzIText3,
        NULL,
        NULL,
        1,
        NULL
};

#define sfzGadgetList1 sfzGadget1

static struct NewWindow sfzNewWindowStructure1 = {
        154,58,
        188,53,
        0,1,
        GADGETUP+CLOSEWINDOW,
        WINDOWDRAG+WINDOWDEPTH+WINDOWCLOSE+ACTIVATE+NOCAREREFRESH,
        &sfzGadget1,
        NULL,
        "Sfzorando",
        NULL,
        NULL,
        5,5,
        -1,-1,
        CUSTOMSCREEN
};



SAVEDS void edittoolcode(struct SfzTool *tool)
{
    struct IntuiMessage *message;
    struct Window *window;
    long class,code;
    short refresh = 1;
    struct Gadget *gadget;
    struct NewWindow *newwindow;
    sfzNewWindowStructure1.Screen = functions->screen;
    if (tool->tool.touched & TOUCH_EDIT) {
        sfzNewWindowStructure1.LeftEdge = tool->tool.left;
        sfzNewWindowStructure1.TopEdge = tool->tool.top;
    }
    newwindow = (struct NewWindow *) 
        (*functions->DupeNewWindow)(&sfzNewWindowStructure1);
    if (!newwindow) return;
    newwindow->Title = NULL;
    newwindow->Flags |= BORDERLESS;
    newwindow->Flags &= ~0xF;
    window = (struct Window *) 
        (*functions->FlashyOpenWindow)(newwindow);
    if (!window) return;
    tool->tool.window = window;
    (*functions->EmbossWindowOn)(window,WINDOWCLOSE|WINDOWDEPTH|WINDOWDRAG,
        "Sforzando",(short)-1,(short)-1,0,0);
    (*functions->EmbossOn)(window,1,1);
    (*functions->EmbossOn)(window,2,1);
    (*functions->EmbossOn)(window,3,1);
    for (;;) {
        if (refresh) {
            (*functions->SelectEmbossed)(window,1,tool->type == 0);
            (*functions->SelectEmbossed)(window,2,tool->type == 1);
            (*functions->SelectEmbossed)(window,3,tool->type == 2);
        }
        refresh = 0;
        message = (struct IntuiMessage *) (*functions->GetIntuiMessage)(window);
        class = message->Class;
        code = message->Code;
        gadget = (struct Gadget *) message->IAddress;
        class = (*functions->SystemGadgets)(window,class,gadget,code);
        ReplyMsg((struct Message *)message);
        if (class == CLOSEWINDOW) break;
        else if (class == GADGETUP) {
            tool->type = gadget->GadgetID - 1;
            refresh = 1;
        }
    }
    tool->tool.window = NULL;
    tool->tool.left = window->LeftEdge;
    tool->tool.top = window->TopEdge;
    tool->tool.touched = TOUCH_INIT | TOUCH_EDIT;
    (*functions->EmbossOff)(window,1);
    (*functions->EmbossOff)(window,2);
    (*functions->EmbossOff)(window,3);
    (*functions->FlashyCloseWindow)(window);
    (*functions->DeleteNewWindow)(newwindow);
}

static struct ToolMaster master;

extern long stdout;


SAVEDS struct ToolMaster *inittoolmaster()

{
    memset((char *)&master,0,sizeof(struct ToolMaster));
    master.toolsize = sizeof(struct SfzTool);
    master.toolid = ID_SFZO;
    master.edittool = (void_cast)edittoolcode;
    master.image = &Sforzandoimage;
    strcpy(master.name,"Sforzando");
    master.processevent = (event_cast)processeventcode;
    master.tooltype = TOOL_NORMAL;
    return(&master);
}
