•Instrument Picker Test/InstrumentPickerTest.c

 
/*--------------------------
    Inclusions
--------------------------*/
 
#include <QuickDraw.h>
#include <Windows.h>
#include <OSUtils.h>
 
#include "BigEasy2.h"
#include "BigEasyTextish.h"
#include "BigEasyDialogs.h"
#include <QuickTimeComponents.h>
 
/*--------------------------
    Limits and Konstants
--------------------------*/
enum
    {
    mOpen = 100,
    mFirstEditField,
    mEditSynthesizerType,
    mEditSynthesizerName,
    mEditInstrumentName,
    mEditInstrumentNumber,
    mEditGMNumber,
 
    mNewNoteChannel,
 
    mPickInstrument,
    mPickInstrumentNoMix,
    mPickInstrumentSameSynth,
    mPickInstrumentFascist,
 
    mClose
    };
 
typedef struct
    {
    WindowPtr w;
 
    NoteAllocator na;
    ToneDescription td;
    } Globals;
 
Globals g;
 
 
/*--------------------------
    Prototypes
--------------------------*/
static void DrawDoc(short n);
static void ClickDoc(short n,Point p,short mods);
static void KeyDoc(short n,short key,short code,short mods);
static void IdleDoc(short n, Boolean front);
static void GoAwayDoc(short n);
static void ActivateDoc(short n);
static void DeactivateDoc(short n);
static void LetsQuit(short n,short menuItem,short menuRef);
static void EditFieldMenu(short n,short menuItem,short menuRef);
static void DocNoteChannel(short n,short menuItem,short menuRef);
static void Pick(short n,short menuItem,short menuRef);
static void OpenWindow(short n,short menuItem,short menuRef);
static void InitVars(void);
static void EditField(short f);
static void InvalFields(void);
 
static pascal Boolean MyFilterProc(DialogPtr theDialog,
        EventRecord *theEvent,short *itemHit);
        
/*--------------------------
    Computer Programs
--------------------------*/
 
#define kLineHeight 12
 
#define kLine1 30
#define kLine2 42
#define kLine3 54
#define kLine4 66
#define kLine5 78
 
#define kCenterLine 125
 
#define kWindowWidth (kCenterLine*2)
#define kWindowHeight (kLine5 + 10)
 
#define kHeaderHeight 17
 
void DrawDoc(short n)
/*
 * Draws the window.
 */
    {
    #pragma unused (n)
 
    EraseRect(&gBigRect);
    TextSize(9);
    MoveTo(kCenterLine,12);
    DrawStringCenter("\pTone Description");
    MoveTo(0,kHeaderHeight-3);
    Line(9000,0);
    MoveTo(0,kHeaderHeight-1);
    Line(9000,0);
 
/* ---------------- */
    MoveTo(kCenterLine,kLine1);
    DrawStringRight("\pSynthesizer Type: ");
    if(g.td.synthesizerType)
        {
        DrawChar(g.td.synthesizerType>>24);
        DrawChar((g.td.synthesizerType>>16) & 0xFF);
        DrawChar((g.td.synthesizerType>>8) & 0xFF);
        DrawChar((g.td.synthesizerType) & 0xFF);
        }
    else
        DrawString("\p0");
 
 
/* ---------------- */
    MoveTo(kCenterLine,kLine2);
    DrawStringRight("\pSynthesizer Name: ");
    DrawString(g.td.synthesizerName);
 
/* ---------------- */
    MoveTo(kCenterLine,kLine3);
    DrawStringRight("\pInstrument Name: ");
    DrawString(g.td.instrumentName);
 
/* ---------------- */
    MoveTo(kCenterLine,kLine4);
    DrawStringRight("\pInstrument Number: ");
    DrawNum(g.td.instrumentNumber);
 
/* ---------------- */
    MoveTo(kCenterLine,kLine5);
    DrawStringRight("\pGM Number: ");
    DrawNum(g.td.gmNumber);
    }
 
 
static void Pick(short n,short menuItem,short menuRef)
    {
    unsigned long flags;
    ComponentResult result;
 
    if(menuRef == mPickInstrumentNoMix)
        flags = 1;
    else if(menuRef == mPickInstrumentSameSynth)
        flags = 2;
    else if(menuRef == mPickInstrumentFascist)
        flags = 3;
    else
        flags = 0;
 
 
    result = NAPickInstrument(g.na,MyFilterProc,"\pPick An Instrument:",&g.td,
            flags,0,0,0);
    InvalFields();
    }
 
void ClickDoc(short n,Point p,short mods)
/*
 * Come here for a click in the window.
 */
    {
    Str31 s;
 
 
 
    if(p.v < kLine1 && p.v > kLine1 - kLineHeight)
        EditField(1);
    else if(p.v < kLine2 && p.v > kLine2 - kLineHeight)
        EditField(2);
    else if(p.v < kLine3 && p.v > kLine3 - kLineHeight)
        EditField(3);
    else if(p.v < kLine4 && p.v > kLine4 - kLineHeight)
        EditField(4);
    else if(p.v < kLine5 && p.v > kLine5 - kLineHeight)
        EditField(5);
    else
        Pick(0,0,0);
    }
 
 
void EditField(short f)
/*
 * Edit a field, 1-5.
 */
    {
    Str31 s;
 
 
    switch(f)
        {
        case 1:
            if(g.td.synthesizerType)
                {
                s[0] = 4;
                s[1] = g.td.synthesizerType >> 24;
                s[2] = g.td.synthesizerType >> 16;
                s[3] = g.td.synthesizerType >> 8;
                s[4] = g.td.synthesizerType;
                }
            else
                {
                s[0] = 1;
                s[1] = '0';
                }
            EasyDialogGetString("\pTone Description",
                    "\pNew value for synthesizer type:",
                    s,4);
            if(s[0] == 1 && s[1] == '0')
                g.td.synthesizerType = 0;
            else
                {
                while(s[0] < 4)
                    s[++s[0]] = ' ';
                g.td.synthesizerType = (((unsigned long)s[1]) << 24)
                        + (((unsigned long)s[2]) << 16)
                        + (((unsigned long)s[3]) << 8)
                        + (((unsigned long)s[4]));
                }
            goto inval;
 
        case 2:
            EasyDialogGetString("\pTone Description",
                    "\pNew value for synthesizer name:",
                    g.td.synthesizerName,31);
            goto inval;
 
        case 3:
            EasyDialogGetString("\pTone Description",
                    "\pNew value for instrument name:",
                    g.td.instrumentName,31);
            goto inval;
 
        case 4:
            EasyDialogGetNumber("\pTone Description",
                    "\pNew value for instrument number:",
                    &g.td.instrumentNumber);
            goto inval;
 
        case 5:
            EasyDialogGetNumber("\pTone Description",
                    "\pNew value for GM number:",
                    &g.td.gmNumber);
    inval:
            InvalFields();
            break;
        }
    }
 
void InvalFields(void)
    {
    Rect r;
 
    r.top = kHeaderHeight;
    r.left = kCenterLine;
    r.right = kWindowWidth;
    r.bottom = kWindowHeight;
 
    SetPort(g.w);
    InvalRect(&r);
    }
 
void KeyDoc(short n,short key,short code,short mods)
    {
    #pragma unused (n,key,code,mods)
 
    if(key == ' ' || key == 13 || key == 3)
        Pick(0,0,0);
    else if(key >= '1' && key <= '5')
        EditField(key-'0');
    }
 
void IdleDoc(short n, Boolean front)
    {
    #pragma unused (n,front)
    }
 
void GoAwayDoc(short n)
/*
 * Close that window...
 */
    {
    UninstallWindow(n);
    }
 
void ActivateDoc(short n)
    {
#pragma unused (n)
    SetMenuItem(mClose,1,0,0,nil);              /* enable "Close" menu item     */
    SetMenuItem(mOpen,-1,0,0,nil);              /* disable "Open" menu item     */
    }
 
void DeactivateDoc(short n)
    {
#pragma unused (n)
    SetMenuItem(mClose,-1,0,0,nil);             /* disable "Close" menu item    */
    SetMenuItem(mOpen,1,0,0,nil);               /* enable "Open" menu item      */
    }
 
void LetsQuit(short n,short menuItem,short menuRef)
    {
#pragma unused (n,menuItem,menuRef)
    gQuitApp++;
    }
 
void EditFieldMenu(short n,short menuItem,short menuRef)
    {
    EditField(menuRef - mFirstEditField);
    }
 
 
static void DocNoteChannel(short n,short menuItem,short menuRef)
    {
    NoteChannel nc;
    long dummy;
    short p;
    ComponentResult thisError;
 
    NoteRequest nr;
 
    nr.tone = g.td;
    nr.polyphony = 4;
    nr.typicalPolyphony = 0x00010000;
 
    thisError = NANewNoteChannel(g.na,&nr,&nc);
    p = 36;
    while(!Button())
        {
        NAPlayNote(g.na,nc,p,128);
        Delay(15,&dummy);
        NAPlayNote(g.na,nc,p,0);
        p++;
        }
 
    thisError = NADisposeNoteChannel(g.na,nc);
    }
 
 
 
 
void OpenWindow(short n,short menuItem,short menuRef)
    {
#pragma unused (n,menuItem,menuRef)
    Rect r;
 
    SetRect(&r,0,0,kWindowWidth,kWindowHeight);
    OffsetRect(&r,20,40);
 
    g.w = InstallWindow(1,"\p",&r,0,wCopyDraw,
            DrawDoc,ClickDoc,KeyDoc,nil,
            ActivateDoc,DeactivateDoc,IdleDoc);
    }
 
 
#ifdef RegisterLocalComponents
Component RegisterNoteAllocator(void);
#endif
 
void InitVars()
/*
 * Called once at startup: yes, it
 * inits the vars.
 */
    {
    ComponentResult result;
 
    g.na = OpenDefaultComponent('nota',0);
 
    g.td.synthesizerType = 0;
    g.td.synthesizerName[0] = 0;
    g.td.instrumentName[0] = 0;
    g.td.instrumentNumber = 0;
    g.td.gmNumber = 0;
 
#ifdef RegisterLocalComponents
    RegisterNoteAllocator();
#endif
 
    }
 
void Bootstrap()
    {
    InitVars();
 
/*** File Menu ***/
    InstallMenu("\pFile",nil,0);
    InstallMenuItem("\pQuit/Q",LetsQuit,0);
 
/*** Edit Menu ***/
    InstallEditMenu(nil,nil,nil,nil,nil);
 
/*** Picker Menu ***/
    InstallMenu("\pPicker",nil,0);
    InstallMenuItem("\pSynthesizer TypeÉ/1",EditFieldMenu,mEditSynthesizerType);
    InstallMenuItem("\pSynthesizer NameÉ/2",EditFieldMenu,mEditSynthesizerName);
    InstallMenuItem("\pInstrument NameÉ/3",EditFieldMenu,mEditInstrumentName);
    InstallMenuItem("\pInstrument NumberÉ/4",EditFieldMenu,mEditInstrumentNumber);
    InstallMenuItem("\pGM NumberÉ/5",EditFieldMenu,mEditGMNumber);
    InstallMenuItem("\p(-",0,0);
    InstallMenuItem("\pPick InstrumentÉ/P",Pick,mPickInstrument);
    InstallMenuItem("\pPick InstrumentÉ [No Mix]/7",Pick,mPickInstrumentNoMix);
    InstallMenuItem("\pPick InstrumentÉ [Same Synth]/8",Pick,mPickInstrumentSameSynth);
    InstallMenuItem("\pPick InstrumentÉ [Fascist]/9",Pick,mPickInstrumentFascist);
    InstallMenuItem("\p(-",0,0);
    InstallMenuItem("\pNew Note Channel/N",DocNoteChannel,mNewNoteChannel);
 
    OpenWindow(0,0,0);
    }
 
void Hatstrap()
/*
  * clean up
  */
    {
    }
 
 
pascal Boolean MyFilterProc(DialogPtr theDialog,
        EventRecord *theEvent,short *itemHit)
    {
    GrafPort *oldPort;
 
    GetPort(&oldPort);
    SetPort(theDialog);
 
    if(theEvent->what == updateEvt && (DialogPtr)theEvent->message != theDialog)
            HandleUpdateEvent(theEvent);
 
    SetPort(oldPort);
    return false;
    }