event.c++ 5.71 KB
//====================================================================
// event.c++
//
// Synopsis:
//
// Author(s)
//  Steve Shepard
//
// Copyright 1993, Silicon Graphics, Inc.
// All Rights Reserved.
//
// This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
// the contents of this file may not be disclosed to third parties, copied or
// duplicated in any form, in whole or in part, without the prior written
// permission of Silicon Graphics, Inc.
//
// RESTRICTED RIGHTS LEGEND:
// Use, duplication or disclosure by the Government is subject to restrictions
// as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
// and Computer Software clause at DFARS 252.227-7013, and/or in similar or
// successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
// rights reserved under the Copyright Laws of the United States.
//====================================================================

#include "event.h"
#include <math.h>

void Event::GetTime(Time& t)
{
    t = eventTime;
}

void Event::SetTime(const Time& t)
{
    eventTime = t;
}

MetaEvent::MetaEvent()
{
    type = 0xff;
    subtype = -1;
}

MetaEvent::MetaEvent(char stype)
{
    type = 0xff;
    subtype = stype;
}

int MetaEvent::Init(FILE *file)
{
    int bytes = 0;
    int i;

    bytes += parseVarLen(&len, file);
    
    for (i = 0; i < len; i++) {
        char tmp = getc(file);
        bytes++;
    }

    return bytes;
}

void MetaEvent::Print()
{
    eventTime.Print();    
    printf("meta\t");

    switch (subtype) {
        case (0x0):
            printf("Sequence Number\n");
            break;

        case (0x2f):
            printf("End of track\n");
            break;

        case (0x54):
            printf("SMPTE offset\n");
            break;

        case (0x58):
            printf("Time Signature\n");
            break;

        case (0x59):
            printf("Key Signature\n");
            break;

        default:
            printf("unknown type = %x\n", type);
            break;                
    }
}

char MetaEvent::GetType()
{
    return type;
}

char MetaEvent::GetSubtype()
{
    return subtype;
}

void MetaEvent::SetSubtype(char c)
{
    subtype = c;
}

MTextEvent::MTextEvent()
{
    ptr = 0;
    len = 0;
}

MTextEvent::~MTextEvent()
{
    if (ptr)
        delete ptr;
}

int MTextEvent::Init(FILE *file)
{
    int bytes = 0;

    bytes += parseVarLen(&len, file);
    
    ptr = new char[len];
    assert(ptr);
    
    fread (ptr, sizeof(char), (int) len, file);
    bytes += (int)len;
    
    return bytes;
}

void MTextEvent::Print()
{
    eventTime.Print();
    printf("meta\t");

    char str[256];
    
    switch (subtype) {
        case (0x1):
            strcpy(str, "Text Event:");
            break;

        case (0x2):
            strcpy (str, "Copyright Notice:");
            break;
                
        case (0x3):
            strcpy(str, "Sequence/Track: ");
            break;

        case (0x4):
            strcpy(str, "Instrument: ");
            break;

        case (0x5):
            strcpy(str, "Lyric:");
            break;

        case (0x6):
            strcpy(str, "Marker:");
            break;

        case (0x7):
            strcpy(str, "Cue Point:");
            break;

        default:
            printf("MTextEvent:: unknown type = %x\n", type);
            break;                
    }

    strncat(str, ptr, (int)len);
    printf("%s\n", str);
}

int MSetTempo::Init(FILE *file)
{
    int bytes = 0;

    bytes += parseVarLen(&len, file);
    
    tempo = getc(file);
    tempo = (tempo << 8) | getc(file);
    tempo = (tempo << 8) | getc(file);

    bytes += 3;
    
    return bytes;
}

long MSetTempo::GetTempo()
{
    return tempo;
}

void MSetTempo::Print()
{
    eventTime.Print();
    float bpm = 60.0*1000000.0/(tempo);
    
    printf("meta\t");
    printf("Tempo: %.2f\n", bpm);
}

int MTimeSig::Init(FILE *file)
{
    int bytes = 0;

    bytes += parseVarLen(&len, file);
    
    nn = getc(file);
    dd = getc(file);
    cc = getc(file);
    bb = getc(file);

    bytes += 4;
    
    return bytes;
}

void MTimeSig::Print()
{
    eventTime.Print();
    int denom = (int)pow(2,dd);
    
    printf("meta\t");
    printf("Time Sig: %d/%d\n", nn, denom);
}


MIDIEvent::MIDIEvent()
{
    status.byte = 0;
}

MIDIEvent::MIDIEvent(char statusByte)
{
    status.byte = statusByte;
}

int MIDIEvent::Init(FILE *file)
{
    int bytes = 0;
    
    if (status.byte == 0) {
        status.byte = getc(file);
        bytes++;
    }    

    if ((status.type == 0xc) || (status.type == 0xd)) {
        byte1 = getc(file);
        byte2 = 0;
        bytes++;
    } else {
        byte1 = getc(file);
        byte2 = getc(file);
        bytes += 2;
    }
    return bytes;
}

void MIDIEvent::Print()
{
    int chan = status.byte & 0xf;
    int type = (status.byte >> 4) & 0xf;
    
    eventTime.Print();
    
    printf("midi\t");

    switch (type) {
        case (0x8):
            printf("Note Off\n");
            break;

        case (0x9):
            printf("Note On: c: %d k: %d, v: %d\n", chan, byte1, byte2);
            break;

        case (0xa):
            printf("Key Pressure\n");
            break;

        case (0xb):
            printf("Control Change c: %d ctrl: %d val: %d\n", chan, byte1,
                   byte2);
            break;

        case (0xc):
            printf("Program Change: c: %d p: %d\n", chan, byte1);
            break;

        case (0xd):
            printf("Channel Pressure\n");
            break;

        case (0xe):
            printf("Pitch Bend\n");
            break;

        default:
            printf("unknown midi event: %x\n", status.type);
            break;
    }
}

char MIDIEvent::GetType()
{
    return status.byte;
}

char MIDIEvent::GetSubtype() 
{
    return 0;
}